-
Notifications
You must be signed in to change notification settings - Fork 1
/
bee.c
294 lines (229 loc) · 5.21 KB
/
bee.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <libwa/wa.h>
#include <libwa/session.h>
#include <bitlbee.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <wordexp.h>
typedef struct im_connection im_connection_t;
typedef struct {
wa_t *wa;
im_connection_t *ic;
pthread_t thread;
int pipe[2];
GAsyncQueue *q;
const char *config_dir;
int run;
} wabee_t;
enum event {
EVENT_CONNECTED,
EVENT_PRIV_MSG,
EVENT_UPDATE_USER,
};
typedef struct {
enum event type;
void *data;
} event_t;
static int
event_add(wabee_t *wb, enum event type, void *data);
static int
cb_priv_msg(void *ptr, priv_msg_t *pm)
{
fprintf(stderr, "queuing: user %s wrote: %s\n", pm->jid, pm->text);
return event_add(ptr, EVENT_PRIV_MSG, pm);
}
static int
cb_update_user(void *ptr, user_t *u)
{
return event_add(ptr, EVENT_UPDATE_USER, u);
}
static int
handle_priv_msg(wabee_t *wb, priv_msg_t *pm)
{
int opt = 0;
if(!pm->jid)
{
fprintf(stderr, "No recipient received, ignoring msg\n");
return 0;
}
if(!pm->text)
{
fprintf(stderr, "No text received, ignoring msg\n");
return 0;
}
fprintf(stderr, "User %s wrote: %s\n", pm->jid, pm->text);
if(pm->from_me)
opt |= OPT_SELFMESSAGE;
imcb_buddy_msg(wb->ic, pm->jid, pm->text, opt, 0);
g_free(pm);
return 0;
}
static int
handle_update_user(wabee_t *wb, user_t *u)
{
/* FIXME: Don't free the user u! */
assert(u);
//fprintf(stderr, "Updating user %s (%s)\n", u->name, u->jid);
// Search first
bee_user_t *bu = bee_user_by_handle(wb->ic->bee, wb->ic, u->jid);
if(!bu)
{
// User is new, create bee_user_t
imcb_add_buddy(wb->ic, u->jid, NULL);
}
if(u->name)
imcb_rename_buddy(wb->ic, u->jid, u->name);
if(u->notify)
imcb_buddy_nick_hint(wb->ic, u->jid, u->notify);
imcb_buddy_status(wb->ic, u->jid, OPT_LOGGED_IN | OPT_AWAY, NULL,
NULL);
return 0;
}
static int
event_add(wabee_t *wb, enum event type, void *data)
{
event_t *ev;
ev = g_new0(event_t, 1);
ev->type = type;
ev->data = data;
g_async_queue_push(wb->q, ev); /* No return errors? */
return 0;
}
static int
wabee_send_priv_msg(struct im_connection *ic, char *who, char *text,
int __attribute__((unused)) away)
{
wabee_t *wb = ic->proto_data;
return wa_send_priv_msg(wb->wa, who, text);
}
static gboolean
queue_dispatch(gpointer ptr, gint __attribute__((unused)) fd,
b_input_condition __attribute__((unused)) cond)
{
wabee_t *wb = ptr;
event_t *ev;
//fprintf(stderr, "Dispatching events\n");
while((ev = g_async_queue_try_pop(wb->q)))
{
switch(ev->type)
{
case EVENT_PRIV_MSG:
handle_priv_msg(wb, ev->data);
break;
case EVENT_UPDATE_USER:
handle_update_user(wb, ev->data);
break;
case EVENT_CONNECTED:
imcb_connected(wb->ic);
break;
default:
fprintf(stderr, "Unknown event type %d\n", ev->type);
}
g_free(ev);
}
return 1;
}
static void
wabee_init(account_t *acc)
{
set_add(&acc->set, "config_dir", "~/.bitlbee/wa", NULL, acc);
}
static wabee_t *
wabee_new(account_t *a)
{
wordexp_t p;
wabee_t *wb;
cb_t *cb;
char *sf;
wb = g_new0(wabee_t, 1);
cb = g_new0(cb_t, 1);
cb->ptr = wb;
cb->priv_msg = cb_priv_msg;
cb->update_user = cb_update_user;
sf = set_getstr(&a->set, "config_dir");
wordexp(sf, &p, WRDE_NOCMD);
wb->config_dir = p.we_wordv[0];
wb->run = 1;
wb->wa = wa_init(cb, wb->config_dir);
wb->ic = imcb_new(a);
wb->ic->proto_data = wb;
wb->q = g_async_queue_new();
b_timeout_add(50, queue_dispatch, wb);
return wb;
}
static void *
wabee_loop(void *ptr)
{
wabee_t *wb = (wabee_t *) ptr;
printf("s->path %s\n", wb->wa->s->path);
wa_login(wb->wa);
event_add(wb, EVENT_CONNECTED, NULL);
/*imcb_connected(wb->ic);*/
while(wb->run)
wa_dispatch(wb->wa, 200);
wa_free(wb->wa);
return NULL;
}
static int
wabee_run(wabee_t *wb)
{
if(pthread_create(&wb->thread, NULL, wabee_loop, wb))
{
fprintf(stderr, "error creating thread\n");
return -1;
}
return 0;
}
void
wabee_login(account_t *a)
{
/* This should run in his own thread. */
wabee_t *wb = wabee_new(a);
imcb_log(wb->ic, "Login started, check for QR in bitlbee stdout");
wabee_run(wb);
}
void
wabee_logout(struct im_connection *c)
{
wabee_t *wb = c->proto_data;
wb->run = 0;
}
#ifdef BITLBEE_ABI_VERSION_CODE
struct plugin_info *
init_plugin_info(void)
{
static struct plugin_info info = {
BITLBEE_ABI_VERSION_CODE,
"bitlbe-wa",
"0.0.1",
"Bitlbee plugin for WhatsApp",
"Rodrigo Arias <[email protected]>",
"https://github.com/rodarima/libwa"};
return &info;
}
#endif
G_MODULE_EXPORT void init_plugin(void)
{
struct prpl *ret = g_new0(struct prpl, 1);
ret->options = PRPL_OPT_NOOTR | PRPL_OPT_NO_PASSWORD;
ret->name = "whatsapp";
ret->init = wabee_init;
ret->login = wabee_login;
ret->logout = wabee_logout;
ret->buddy_msg = wabee_send_priv_msg;
// ret->get_info = wa_get_info;
// ret->add_buddy = wa_add_buddy;
// ret->remove_buddy = wa_remove_buddy;
// ret->chat_msg = wa_chat_msg;
// ret->chat_join = wa_chat_join;
// ret->chat_leave = wa_chat_leave;
// ret->add_permit = wa_add_permit;
// ret->rem_permit = wa_rem_permit;
// ret->add_deny = wa_add_deny;
// ret->rem_deny = wa_rem_deny;
// ret->buddy_data_add = wa_buddy_data_add;
// ret->buddy_data_free = wa_buddy_data_free;
ret->handle_cmp = g_ascii_strcasecmp;
register_protocol(ret);
}