1 | /***************************************************************************\ |
---|
2 | * * |
---|
3 | * BitlBee - An IRC to IM gateway * |
---|
4 | * libpurple module - Main file * |
---|
5 | * * |
---|
6 | * Copyright 2009-2012 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
7 | * * |
---|
8 | * This program is free software; you can redistribute it and/or modify * |
---|
9 | * it under the terms of the GNU General Public License as published by * |
---|
10 | * the Free Software Foundation; either version 2 of the License, or * |
---|
11 | * (at your option) any later version. * |
---|
12 | * * |
---|
13 | * This program is distributed in the hope that it will be useful, * |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
---|
16 | * GNU General Public License for more details. * |
---|
17 | * * |
---|
18 | * You should have received a copy of the GNU General Public License along * |
---|
19 | * with this program; if not, write to the Free Software Foundation, Inc., * |
---|
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
---|
21 | * * |
---|
22 | \***************************************************************************/ |
---|
23 | |
---|
24 | #include "bitlbee.h" |
---|
25 | #include "bpurple.h" |
---|
26 | #include "help.h" |
---|
27 | |
---|
28 | #include <stdarg.h> |
---|
29 | |
---|
30 | #include <glib.h> |
---|
31 | #include <purple.h> |
---|
32 | |
---|
33 | GSList *purple_connections; |
---|
34 | |
---|
35 | /* This makes me VERY sad... :-( But some libpurple callbacks come in without |
---|
36 | any context so this is the only way to get that. Don't want to support |
---|
37 | libpurple in daemon mode anyway. */ |
---|
38 | static bee_t *local_bee; |
---|
39 | |
---|
40 | static char *set_eval_display_name(set_t *set, char *value); |
---|
41 | |
---|
42 | void purple_request_input_callback(guint id, struct im_connection *ic, |
---|
43 | const char *message, const char *who); |
---|
44 | |
---|
45 | /* purple_request_input specific stuff */ |
---|
46 | typedef void (*ri_callback_t)(gpointer, const gchar *); |
---|
47 | |
---|
48 | struct request_input_data { |
---|
49 | ri_callback_t data_callback; |
---|
50 | void *user_data; |
---|
51 | struct im_connection *ic; |
---|
52 | char *buddy; |
---|
53 | guint id; |
---|
54 | }; |
---|
55 | |
---|
56 | struct im_connection *purple_ic_by_pa(PurpleAccount *pa) |
---|
57 | { |
---|
58 | GSList *i; |
---|
59 | struct purple_data *pd; |
---|
60 | |
---|
61 | for (i = purple_connections; i; i = i->next) { |
---|
62 | pd = ((struct im_connection *) i->data)->proto_data; |
---|
63 | if (pd->account == pa) { |
---|
64 | return i->data; |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | return NULL; |
---|
69 | } |
---|
70 | |
---|
71 | static struct im_connection *purple_ic_by_gc(PurpleConnection *gc) |
---|
72 | { |
---|
73 | return purple_ic_by_pa(purple_connection_get_account(gc)); |
---|
74 | } |
---|
75 | |
---|
76 | static gboolean purple_menu_cmp(const char *a, const char *b) |
---|
77 | { |
---|
78 | while (*a && *b) { |
---|
79 | while (*a == '_') { |
---|
80 | a++; |
---|
81 | } |
---|
82 | while (*b == '_') { |
---|
83 | b++; |
---|
84 | } |
---|
85 | if (g_ascii_tolower(*a) != g_ascii_tolower(*b)) { |
---|
86 | return FALSE; |
---|
87 | } |
---|
88 | |
---|
89 | a++; |
---|
90 | b++; |
---|
91 | } |
---|
92 | |
---|
93 | return (*a == '\0' && *b == '\0'); |
---|
94 | } |
---|
95 | |
---|
96 | static void purple_init(account_t *acc) |
---|
97 | { |
---|
98 | PurplePlugin *prpl = purple_plugins_find_with_id((char *) acc->prpl->data); |
---|
99 | PurplePluginProtocolInfo *pi = prpl->info->extra_info; |
---|
100 | PurpleAccount *pa; |
---|
101 | GList *i, *st; |
---|
102 | set_t *s; |
---|
103 | char help_title[64]; |
---|
104 | GString *help; |
---|
105 | static gboolean dir_fixed = FALSE; |
---|
106 | |
---|
107 | /* Layer violation coming up: Making an exception for libpurple here. |
---|
108 | Dig in the IRC state a bit to get a username. Ideally we should |
---|
109 | check if s/he identified but this info doesn't seem *that* important. |
---|
110 | It's just that fecking libpurple can't *not* store this shit. |
---|
111 | |
---|
112 | Remember that libpurple is not really meant to be used on public |
---|
113 | servers anyway! */ |
---|
114 | if (!dir_fixed) { |
---|
115 | irc_t *irc = acc->bee->ui_data; |
---|
116 | char *dir; |
---|
117 | |
---|
118 | dir = g_strdup_printf("%s/purple/%s", global.conf->configdir, irc->user->nick); |
---|
119 | purple_util_set_user_dir(dir); |
---|
120 | g_free(dir); |
---|
121 | |
---|
122 | purple_blist_load(); |
---|
123 | purple_prefs_load(); |
---|
124 | dir_fixed = TRUE; |
---|
125 | } |
---|
126 | |
---|
127 | help = g_string_new(""); |
---|
128 | g_string_printf(help, "BitlBee libpurple module %s (%s).\n\nSupported settings:", |
---|
129 | (char *) acc->prpl->name, prpl->info->name); |
---|
130 | |
---|
131 | if (pi->user_splits) { |
---|
132 | GList *l; |
---|
133 | g_string_append_printf(help, "\n* username: Username"); |
---|
134 | for (l = pi->user_splits; l; l = l->next) { |
---|
135 | g_string_append_printf(help, "%c%s", |
---|
136 | purple_account_user_split_get_separator(l->data), |
---|
137 | purple_account_user_split_get_text(l->data)); |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | /* Convert all protocol_options into per-account setting variables. */ |
---|
142 | for (i = pi->protocol_options; i; i = i->next) { |
---|
143 | PurpleAccountOption *o = i->data; |
---|
144 | const char *name; |
---|
145 | char *def = NULL; |
---|
146 | set_eval eval = NULL; |
---|
147 | void *eval_data = NULL; |
---|
148 | GList *io = NULL; |
---|
149 | GSList *opts = NULL; |
---|
150 | |
---|
151 | name = purple_account_option_get_setting(o); |
---|
152 | |
---|
153 | switch (purple_account_option_get_type(o)) { |
---|
154 | case PURPLE_PREF_STRING: |
---|
155 | def = g_strdup(purple_account_option_get_default_string(o)); |
---|
156 | |
---|
157 | g_string_append_printf(help, "\n* %s (%s), %s, default: %s", |
---|
158 | name, purple_account_option_get_text(o), |
---|
159 | "string", def); |
---|
160 | |
---|
161 | break; |
---|
162 | |
---|
163 | case PURPLE_PREF_INT: |
---|
164 | def = g_strdup_printf("%d", purple_account_option_get_default_int(o)); |
---|
165 | eval = set_eval_int; |
---|
166 | |
---|
167 | g_string_append_printf(help, "\n* %s (%s), %s, default: %s", |
---|
168 | name, purple_account_option_get_text(o), |
---|
169 | "integer", def); |
---|
170 | |
---|
171 | break; |
---|
172 | |
---|
173 | case PURPLE_PREF_BOOLEAN: |
---|
174 | if (purple_account_option_get_default_bool(o)) { |
---|
175 | def = g_strdup("true"); |
---|
176 | } else { |
---|
177 | def = g_strdup("false"); |
---|
178 | } |
---|
179 | eval = set_eval_bool; |
---|
180 | |
---|
181 | g_string_append_printf(help, "\n* %s (%s), %s, default: %s", |
---|
182 | name, purple_account_option_get_text(o), |
---|
183 | "boolean", def); |
---|
184 | |
---|
185 | break; |
---|
186 | |
---|
187 | case PURPLE_PREF_STRING_LIST: |
---|
188 | def = g_strdup(purple_account_option_get_default_list_value(o)); |
---|
189 | |
---|
190 | g_string_append_printf(help, "\n* %s (%s), %s, default: %s", |
---|
191 | name, purple_account_option_get_text(o), |
---|
192 | "list", def); |
---|
193 | g_string_append(help, "\n Possible values: "); |
---|
194 | |
---|
195 | for (io = purple_account_option_get_list(o); io; io = io->next) { |
---|
196 | PurpleKeyValuePair *kv = io->data; |
---|
197 | opts = g_slist_append(opts, kv->value); |
---|
198 | /* TODO: kv->value is not a char*, WTF? */ |
---|
199 | if (strcmp(kv->value, kv->key) != 0) { |
---|
200 | g_string_append_printf(help, "%s (%s), ", (char *) kv->value, kv->key); |
---|
201 | } else { |
---|
202 | g_string_append_printf(help, "%s, ", (char *) kv->value); |
---|
203 | } |
---|
204 | } |
---|
205 | g_string_truncate(help, help->len - 2); |
---|
206 | eval = set_eval_list; |
---|
207 | eval_data = opts; |
---|
208 | |
---|
209 | break; |
---|
210 | |
---|
211 | default: |
---|
212 | /** No way to talk to the user right now, invent one when |
---|
213 | this becomes important. |
---|
214 | irc_rootmsg( acc->irc, "Setting with unknown type: %s (%d) Expect stuff to break..\n", |
---|
215 | name, purple_account_option_get_type( o ) ); |
---|
216 | */ |
---|
217 | g_string_append_printf(help, "\n* [%s] UNSUPPORTED (type %d)", |
---|
218 | name, purple_account_option_get_type(o)); |
---|
219 | name = NULL; |
---|
220 | } |
---|
221 | |
---|
222 | if (name != NULL) { |
---|
223 | s = set_add(&acc->set, name, def, eval, acc); |
---|
224 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
225 | s->eval_data = eval_data; |
---|
226 | g_free(def); |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | g_snprintf(help_title, sizeof(help_title), "purple %s", (char *) acc->prpl->name); |
---|
231 | help_add_mem(&global.help, help_title, help->str); |
---|
232 | g_string_free(help, TRUE); |
---|
233 | |
---|
234 | s = set_add(&acc->set, "display_name", NULL, set_eval_display_name, acc); |
---|
235 | s->flags |= ACC_SET_ONLINE_ONLY; |
---|
236 | |
---|
237 | if (pi->options & OPT_PROTO_MAIL_CHECK) { |
---|
238 | s = set_add(&acc->set, "mail_notifications", "false", set_eval_bool, acc); |
---|
239 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
240 | } |
---|
241 | |
---|
242 | if (strcmp(prpl->info->name, "Gadu-Gadu") == 0) { |
---|
243 | s = set_add(&acc->set, "gg_sync_contacts", "true", set_eval_bool, acc); |
---|
244 | } |
---|
245 | |
---|
246 | /* Go through all away states to figure out if away/status messages |
---|
247 | are possible. */ |
---|
248 | pa = purple_account_new(acc->user, (char *) acc->prpl->data); |
---|
249 | for (st = purple_account_get_status_types(pa); st; st = st->next) { |
---|
250 | PurpleStatusPrimitive prim = purple_status_type_get_primitive(st->data); |
---|
251 | |
---|
252 | if (prim == PURPLE_STATUS_AVAILABLE) { |
---|
253 | if (purple_status_type_get_attr(st->data, "message")) { |
---|
254 | acc->flags |= ACC_FLAG_STATUS_MESSAGE; |
---|
255 | } |
---|
256 | } else if (prim != PURPLE_STATUS_OFFLINE) { |
---|
257 | if (purple_status_type_get_attr(st->data, "message")) { |
---|
258 | acc->flags |= ACC_FLAG_AWAY_MESSAGE; |
---|
259 | } |
---|
260 | } |
---|
261 | } |
---|
262 | purple_accounts_remove(pa); |
---|
263 | } |
---|
264 | |
---|
265 | static void purple_sync_settings(account_t *acc, PurpleAccount *pa) |
---|
266 | { |
---|
267 | PurplePlugin *prpl = purple_plugins_find_with_id(pa->protocol_id); |
---|
268 | PurplePluginProtocolInfo *pi = prpl->info->extra_info; |
---|
269 | GList *i; |
---|
270 | |
---|
271 | for (i = pi->protocol_options; i; i = i->next) { |
---|
272 | PurpleAccountOption *o = i->data; |
---|
273 | const char *name; |
---|
274 | set_t *s; |
---|
275 | |
---|
276 | name = purple_account_option_get_setting(o); |
---|
277 | s = set_find(&acc->set, name); |
---|
278 | if (s->value == NULL) { |
---|
279 | continue; |
---|
280 | } |
---|
281 | |
---|
282 | switch (purple_account_option_get_type(o)) { |
---|
283 | case PURPLE_PREF_STRING: |
---|
284 | case PURPLE_PREF_STRING_LIST: |
---|
285 | purple_account_set_string(pa, name, set_getstr(&acc->set, name)); |
---|
286 | break; |
---|
287 | |
---|
288 | case PURPLE_PREF_INT: |
---|
289 | purple_account_set_int(pa, name, set_getint(&acc->set, name)); |
---|
290 | break; |
---|
291 | |
---|
292 | case PURPLE_PREF_BOOLEAN: |
---|
293 | purple_account_set_bool(pa, name, set_getbool(&acc->set, name)); |
---|
294 | break; |
---|
295 | |
---|
296 | default: |
---|
297 | break; |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | if (pi->options & OPT_PROTO_MAIL_CHECK) { |
---|
302 | purple_account_set_check_mail(pa, set_getbool(&acc->set, "mail_notifications")); |
---|
303 | } |
---|
304 | } |
---|
305 | |
---|
306 | static void purple_login(account_t *acc) |
---|
307 | { |
---|
308 | struct im_connection *ic = imcb_new(acc); |
---|
309 | struct purple_data *pd; |
---|
310 | |
---|
311 | if ((local_bee != NULL && local_bee != acc->bee) || |
---|
312 | (global.conf->runmode == RUNMODE_DAEMON && !getenv("BITLBEE_DEBUG"))) { |
---|
313 | imcb_error(ic, "Daemon mode detected. Do *not* try to use libpurple in daemon mode! " |
---|
314 | "Please use inetd or ForkDaemon mode instead."); |
---|
315 | imc_logout(ic, FALSE); |
---|
316 | return; |
---|
317 | } |
---|
318 | local_bee = acc->bee; |
---|
319 | |
---|
320 | /* For now this is needed in the _connected() handlers if using |
---|
321 | GLib event handling, to make sure we're not handling events |
---|
322 | on dead connections. */ |
---|
323 | purple_connections = g_slist_prepend(purple_connections, ic); |
---|
324 | |
---|
325 | ic->proto_data = pd = g_new0(struct purple_data, 1); |
---|
326 | pd->account = purple_account_new(acc->user, (char *) acc->prpl->data); |
---|
327 | pd->input_requests = g_hash_table_new_full(g_direct_hash, g_direct_equal, |
---|
328 | NULL, g_free); |
---|
329 | pd->next_request_id = 0; |
---|
330 | purple_account_set_password(pd->account, acc->pass); |
---|
331 | purple_sync_settings(acc, pd->account); |
---|
332 | |
---|
333 | purple_account_set_enabled(pd->account, "BitlBee", TRUE); |
---|
334 | } |
---|
335 | |
---|
336 | static void purple_logout(struct im_connection *ic) |
---|
337 | { |
---|
338 | struct purple_data *pd = ic->proto_data; |
---|
339 | |
---|
340 | if (!pd) { |
---|
341 | return; |
---|
342 | } |
---|
343 | |
---|
344 | purple_account_set_enabled(pd->account, "BitlBee", FALSE); |
---|
345 | purple_connections = g_slist_remove(purple_connections, ic); |
---|
346 | purple_accounts_remove(pd->account); |
---|
347 | g_hash_table_destroy(pd->input_requests); |
---|
348 | g_free(pd); |
---|
349 | } |
---|
350 | |
---|
351 | static int purple_buddy_msg(struct im_connection *ic, char *who, char *message, int flags) |
---|
352 | { |
---|
353 | PurpleConversation *conv; |
---|
354 | struct purple_data *pd = ic->proto_data; |
---|
355 | |
---|
356 | if (!strncmp(who, PURPLE_REQUEST_HANDLE, sizeof(PURPLE_REQUEST_HANDLE) - 1)) { |
---|
357 | guint request_id = atoi(who + sizeof(PURPLE_REQUEST_HANDLE)); |
---|
358 | purple_request_input_callback(request_id, ic, message, who); |
---|
359 | return 1; |
---|
360 | } |
---|
361 | |
---|
362 | if ((conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, |
---|
363 | who, pd->account)) == NULL) { |
---|
364 | conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, |
---|
365 | pd->account, who); |
---|
366 | } |
---|
367 | |
---|
368 | purple_conv_im_send(purple_conversation_get_im_data(conv), message); |
---|
369 | |
---|
370 | return 1; |
---|
371 | } |
---|
372 | |
---|
373 | static GList *purple_away_states(struct im_connection *ic) |
---|
374 | { |
---|
375 | struct purple_data *pd = ic->proto_data; |
---|
376 | GList *st, *ret = NULL; |
---|
377 | |
---|
378 | for (st = purple_account_get_status_types(pd->account); st; st = st->next) { |
---|
379 | PurpleStatusPrimitive prim = purple_status_type_get_primitive(st->data); |
---|
380 | if (prim != PURPLE_STATUS_AVAILABLE && prim != PURPLE_STATUS_OFFLINE) { |
---|
381 | ret = g_list_append(ret, (void *) purple_status_type_get_name(st->data)); |
---|
382 | } |
---|
383 | } |
---|
384 | |
---|
385 | return ret; |
---|
386 | } |
---|
387 | |
---|
388 | static void purple_set_away(struct im_connection *ic, char *state_txt, char *message) |
---|
389 | { |
---|
390 | struct purple_data *pd = ic->proto_data; |
---|
391 | GList *status_types = purple_account_get_status_types(pd->account), *st; |
---|
392 | PurpleStatusType *pst = NULL; |
---|
393 | GList *args = NULL; |
---|
394 | |
---|
395 | for (st = status_types; st; st = st->next) { |
---|
396 | pst = st->data; |
---|
397 | |
---|
398 | if (state_txt == NULL && |
---|
399 | purple_status_type_get_primitive(pst) == PURPLE_STATUS_AVAILABLE) { |
---|
400 | break; |
---|
401 | } |
---|
402 | |
---|
403 | if (state_txt != NULL && |
---|
404 | g_strcasecmp(state_txt, purple_status_type_get_name(pst)) == 0) { |
---|
405 | break; |
---|
406 | } |
---|
407 | } |
---|
408 | |
---|
409 | if (message && purple_status_type_get_attr(pst, "message")) { |
---|
410 | args = g_list_append(args, "message"); |
---|
411 | args = g_list_append(args, message); |
---|
412 | } |
---|
413 | |
---|
414 | purple_account_set_status_list(pd->account, |
---|
415 | st ? purple_status_type_get_id(pst) : "away", |
---|
416 | TRUE, args); |
---|
417 | |
---|
418 | g_list_free(args); |
---|
419 | } |
---|
420 | |
---|
421 | static char *set_eval_display_name(set_t *set, char *value) |
---|
422 | { |
---|
423 | account_t *acc = set->data; |
---|
424 | struct im_connection *ic = acc->ic; |
---|
425 | |
---|
426 | if (ic) { |
---|
427 | imcb_log(ic, "Changing display_name not currently supported with libpurple!"); |
---|
428 | } |
---|
429 | |
---|
430 | return NULL; |
---|
431 | } |
---|
432 | |
---|
433 | /* Bad bad gadu-gadu, not saving buddy list by itself */ |
---|
434 | static void purple_gg_buddylist_export(PurpleConnection *gc) |
---|
435 | { |
---|
436 | struct im_connection *ic = purple_ic_by_gc(gc); |
---|
437 | |
---|
438 | if (set_getstr(&ic->acc->set, "gg_sync_contacts")) { |
---|
439 | GList *actions = gc->prpl->info->actions(gc->prpl, gc); |
---|
440 | GList *p; |
---|
441 | for (p = g_list_first(actions); p; p = p->next) { |
---|
442 | if (((PurplePluginAction *) p->data) && |
---|
443 | purple_menu_cmp(((PurplePluginAction *) p->data)->label, |
---|
444 | "Upload buddylist to Server") == 0) { |
---|
445 | PurplePluginAction action; |
---|
446 | action.plugin = gc->prpl; |
---|
447 | action.context = gc; |
---|
448 | action.user_data = NULL; |
---|
449 | ((PurplePluginAction *) p->data)->callback(&action); |
---|
450 | break; |
---|
451 | } |
---|
452 | } |
---|
453 | g_list_free(actions); |
---|
454 | } |
---|
455 | } |
---|
456 | |
---|
457 | static void purple_gg_buddylist_import(PurpleConnection *gc) |
---|
458 | { |
---|
459 | struct im_connection *ic = purple_ic_by_gc(gc); |
---|
460 | |
---|
461 | if (set_getstr(&ic->acc->set, "gg_sync_contacts")) { |
---|
462 | GList *actions = gc->prpl->info->actions(gc->prpl, gc); |
---|
463 | GList *p; |
---|
464 | for (p = g_list_first(actions); p; p = p->next) { |
---|
465 | if (((PurplePluginAction *) p->data) && |
---|
466 | purple_menu_cmp(((PurplePluginAction *) p->data)->label, |
---|
467 | "Download buddylist from Server") == 0) { |
---|
468 | PurplePluginAction action; |
---|
469 | action.plugin = gc->prpl; |
---|
470 | action.context = gc; |
---|
471 | action.user_data = NULL; |
---|
472 | ((PurplePluginAction *) p->data)->callback(&action); |
---|
473 | break; |
---|
474 | } |
---|
475 | } |
---|
476 | g_list_free(actions); |
---|
477 | } |
---|
478 | } |
---|
479 | |
---|
480 | static void purple_add_buddy(struct im_connection *ic, char *who, char *group) |
---|
481 | { |
---|
482 | PurpleBuddy *pb; |
---|
483 | PurpleGroup *pg = NULL; |
---|
484 | struct purple_data *pd = ic->proto_data; |
---|
485 | |
---|
486 | if (group && !(pg = purple_find_group(group))) { |
---|
487 | pg = purple_group_new(group); |
---|
488 | purple_blist_add_group(pg, NULL); |
---|
489 | } |
---|
490 | |
---|
491 | pb = purple_buddy_new(pd->account, who, NULL); |
---|
492 | purple_blist_add_buddy(pb, NULL, pg, NULL); |
---|
493 | purple_account_add_buddy(pd->account, pb); |
---|
494 | |
---|
495 | purple_gg_buddylist_export(pd->account->gc); |
---|
496 | } |
---|
497 | |
---|
498 | static void purple_remove_buddy(struct im_connection *ic, char *who, char *group) |
---|
499 | { |
---|
500 | PurpleBuddy *pb; |
---|
501 | struct purple_data *pd = ic->proto_data; |
---|
502 | |
---|
503 | pb = purple_find_buddy(pd->account, who); |
---|
504 | if (pb != NULL) { |
---|
505 | PurpleGroup *group; |
---|
506 | |
---|
507 | group = purple_buddy_get_group(pb); |
---|
508 | purple_account_remove_buddy(pd->account, pb, group); |
---|
509 | |
---|
510 | purple_blist_remove_buddy(pb); |
---|
511 | } |
---|
512 | |
---|
513 | purple_gg_buddylist_export(pd->account->gc); |
---|
514 | } |
---|
515 | |
---|
516 | static void purple_add_permit(struct im_connection *ic, char *who) |
---|
517 | { |
---|
518 | struct purple_data *pd = ic->proto_data; |
---|
519 | |
---|
520 | purple_privacy_permit_add(pd->account, who, FALSE); |
---|
521 | } |
---|
522 | |
---|
523 | static void purple_add_deny(struct im_connection *ic, char *who) |
---|
524 | { |
---|
525 | struct purple_data *pd = ic->proto_data; |
---|
526 | |
---|
527 | purple_privacy_deny_add(pd->account, who, FALSE); |
---|
528 | } |
---|
529 | |
---|
530 | static void purple_rem_permit(struct im_connection *ic, char *who) |
---|
531 | { |
---|
532 | struct purple_data *pd = ic->proto_data; |
---|
533 | |
---|
534 | purple_privacy_permit_remove(pd->account, who, FALSE); |
---|
535 | } |
---|
536 | |
---|
537 | static void purple_rem_deny(struct im_connection *ic, char *who) |
---|
538 | { |
---|
539 | struct purple_data *pd = ic->proto_data; |
---|
540 | |
---|
541 | purple_privacy_deny_remove(pd->account, who, FALSE); |
---|
542 | } |
---|
543 | |
---|
544 | static void purple_get_info(struct im_connection *ic, char *who) |
---|
545 | { |
---|
546 | struct purple_data *pd = ic->proto_data; |
---|
547 | |
---|
548 | serv_get_info(purple_account_get_connection(pd->account), who); |
---|
549 | } |
---|
550 | |
---|
551 | static void purple_keepalive(struct im_connection *ic) |
---|
552 | { |
---|
553 | } |
---|
554 | |
---|
555 | static int purple_send_typing(struct im_connection *ic, char *who, int flags) |
---|
556 | { |
---|
557 | PurpleTypingState state = PURPLE_NOT_TYPING; |
---|
558 | struct purple_data *pd = ic->proto_data; |
---|
559 | |
---|
560 | if (flags & OPT_TYPING) { |
---|
561 | state = PURPLE_TYPING; |
---|
562 | } else if (flags & OPT_THINKING) { |
---|
563 | state = PURPLE_TYPED; |
---|
564 | } |
---|
565 | |
---|
566 | serv_send_typing(purple_account_get_connection(pd->account), who, state); |
---|
567 | |
---|
568 | return 1; |
---|
569 | } |
---|
570 | |
---|
571 | static void purple_chat_msg(struct groupchat *gc, char *message, int flags) |
---|
572 | { |
---|
573 | PurpleConversation *pc = gc->data; |
---|
574 | |
---|
575 | purple_conv_chat_send(purple_conversation_get_chat_data(pc), message); |
---|
576 | } |
---|
577 | |
---|
578 | struct groupchat *purple_chat_with(struct im_connection *ic, char *who) |
---|
579 | { |
---|
580 | /* No, "of course" this won't work this way. Or in fact, it almost |
---|
581 | does, but it only lets you send msgs to it, you won't receive |
---|
582 | any. Instead, we have to click the virtual menu item. |
---|
583 | PurpleAccount *pa = ic->proto_data; |
---|
584 | PurpleConversation *pc; |
---|
585 | PurpleConvChat *pcc; |
---|
586 | struct groupchat *gc; |
---|
587 | |
---|
588 | gc = imcb_chat_new( ic, "BitlBee-libpurple groupchat" ); |
---|
589 | gc->data = pc = purple_conversation_new( PURPLE_CONV_TYPE_CHAT, pa, "BitlBee-libpurple groupchat" ); |
---|
590 | pc->ui_data = gc; |
---|
591 | |
---|
592 | pcc = PURPLE_CONV_CHAT( pc ); |
---|
593 | purple_conv_chat_add_user( pcc, ic->acc->user, "", 0, TRUE ); |
---|
594 | purple_conv_chat_invite_user( pcc, who, "Please join my chat", FALSE ); |
---|
595 | //purple_conv_chat_add_user( pcc, who, "", 0, TRUE ); |
---|
596 | */ |
---|
597 | |
---|
598 | /* There went my nice afternoon. :-( */ |
---|
599 | |
---|
600 | struct purple_data *pd = ic->proto_data; |
---|
601 | PurplePlugin *prpl = purple_plugins_find_with_id(pd->account->protocol_id); |
---|
602 | PurplePluginProtocolInfo *pi = prpl->info->extra_info; |
---|
603 | PurpleBuddy *pb = purple_find_buddy(pd->account, who); |
---|
604 | PurpleMenuAction *mi; |
---|
605 | GList *menu; |
---|
606 | |
---|
607 | void (*callback)(PurpleBlistNode *, gpointer); /* FFFFFFFFFFFFFUUUUUUUUUUUUUU */ |
---|
608 | |
---|
609 | if (!pb || !pi || !pi->blist_node_menu) { |
---|
610 | return NULL; |
---|
611 | } |
---|
612 | |
---|
613 | menu = pi->blist_node_menu(&pb->node); |
---|
614 | while (menu) { |
---|
615 | mi = menu->data; |
---|
616 | if (purple_menu_cmp(mi->label, "initiate chat") || |
---|
617 | purple_menu_cmp(mi->label, "initiate conference")) { |
---|
618 | break; |
---|
619 | } |
---|
620 | menu = menu->next; |
---|
621 | } |
---|
622 | |
---|
623 | if (menu == NULL) { |
---|
624 | return NULL; |
---|
625 | } |
---|
626 | |
---|
627 | /* Call the fucker. */ |
---|
628 | callback = (void *) mi->callback; |
---|
629 | callback(&pb->node, menu->data); |
---|
630 | |
---|
631 | return NULL; |
---|
632 | } |
---|
633 | |
---|
634 | void purple_chat_invite(struct groupchat *gc, char *who, char *message) |
---|
635 | { |
---|
636 | PurpleConversation *pc = gc->data; |
---|
637 | PurpleConvChat *pcc = PURPLE_CONV_CHAT(pc); |
---|
638 | struct purple_data *pd = gc->ic->proto_data; |
---|
639 | |
---|
640 | serv_chat_invite(purple_account_get_connection(pd->account), |
---|
641 | purple_conv_chat_get_id(pcc), |
---|
642 | message && *message ? message : "Please join my chat", |
---|
643 | who); |
---|
644 | } |
---|
645 | |
---|
646 | void purple_chat_kick(struct groupchat *gc, char *who, const char *message) |
---|
647 | { |
---|
648 | PurpleConversation *pc = gc->data; |
---|
649 | char *str = g_strdup_printf("kick %s %s", who, message); |
---|
650 | |
---|
651 | purple_conversation_do_command(pc, str, NULL, NULL); |
---|
652 | g_free(str); |
---|
653 | } |
---|
654 | |
---|
655 | void purple_chat_leave(struct groupchat *gc) |
---|
656 | { |
---|
657 | PurpleConversation *pc = gc->data; |
---|
658 | |
---|
659 | purple_conversation_destroy(pc); |
---|
660 | } |
---|
661 | |
---|
662 | struct groupchat *purple_chat_join(struct im_connection *ic, const char *room, const char *nick, const char *password, |
---|
663 | set_t **sets) |
---|
664 | { |
---|
665 | struct purple_data *pd = ic->proto_data; |
---|
666 | PurplePlugin *prpl = purple_plugins_find_with_id(pd->account->protocol_id); |
---|
667 | PurplePluginProtocolInfo *pi = prpl->info->extra_info; |
---|
668 | GHashTable *chat_hash; |
---|
669 | PurpleConversation *conv; |
---|
670 | GList *info, *l; |
---|
671 | |
---|
672 | if (!pi->chat_info || !pi->chat_info_defaults || |
---|
673 | !(info = pi->chat_info(purple_account_get_connection(pd->account)))) { |
---|
674 | imcb_error(ic, "Joining chatrooms not supported by this protocol"); |
---|
675 | return NULL; |
---|
676 | } |
---|
677 | |
---|
678 | if ((conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_CHAT, |
---|
679 | room, pd->account))) { |
---|
680 | purple_conversation_destroy(conv); |
---|
681 | } |
---|
682 | |
---|
683 | chat_hash = pi->chat_info_defaults( |
---|
684 | purple_account_get_connection(pd->account), room |
---|
685 | ); |
---|
686 | |
---|
687 | for (l = info; l; l = l->next) { |
---|
688 | struct proto_chat_entry *pce = l->data; |
---|
689 | |
---|
690 | if (strcmp(pce->identifier, "handle") == 0) { |
---|
691 | g_hash_table_replace(chat_hash, "handle", g_strdup(nick)); |
---|
692 | } else if (strcmp(pce->identifier, "password") == 0) { |
---|
693 | g_hash_table_replace(chat_hash, "password", g_strdup(password)); |
---|
694 | } else if (strcmp(pce->identifier, "passwd") == 0) { |
---|
695 | g_hash_table_replace(chat_hash, "passwd", g_strdup(password)); |
---|
696 | } |
---|
697 | } |
---|
698 | |
---|
699 | serv_join_chat(purple_account_get_connection(pd->account), chat_hash); |
---|
700 | |
---|
701 | return NULL; |
---|
702 | } |
---|
703 | |
---|
704 | void purple_transfer_request(struct im_connection *ic, file_transfer_t *ft, char *handle); |
---|
705 | |
---|
706 | static void purple_ui_init(); |
---|
707 | |
---|
708 | GHashTable *prplcb_ui_info() |
---|
709 | { |
---|
710 | static GHashTable *ret; |
---|
711 | |
---|
712 | if (ret == NULL) { |
---|
713 | ret = g_hash_table_new(g_str_hash, g_str_equal); |
---|
714 | g_hash_table_insert(ret, "name", "BitlBee"); |
---|
715 | g_hash_table_insert(ret, "version", BITLBEE_VERSION); |
---|
716 | } |
---|
717 | |
---|
718 | return ret; |
---|
719 | } |
---|
720 | |
---|
721 | static PurpleCoreUiOps bee_core_uiops = |
---|
722 | { |
---|
723 | NULL, |
---|
724 | NULL, |
---|
725 | purple_ui_init, |
---|
726 | NULL, |
---|
727 | prplcb_ui_info, |
---|
728 | }; |
---|
729 | |
---|
730 | static void prplcb_conn_progress(PurpleConnection *gc, const char *text, size_t step, size_t step_count) |
---|
731 | { |
---|
732 | struct im_connection *ic = purple_ic_by_gc(gc); |
---|
733 | |
---|
734 | imcb_log(ic, "%s", text); |
---|
735 | } |
---|
736 | |
---|
737 | static void prplcb_conn_connected(PurpleConnection *gc) |
---|
738 | { |
---|
739 | struct im_connection *ic = purple_ic_by_gc(gc); |
---|
740 | const char *dn; |
---|
741 | set_t *s; |
---|
742 | |
---|
743 | imcb_connected(ic); |
---|
744 | |
---|
745 | if ((dn = purple_connection_get_display_name(gc)) && |
---|
746 | (s = set_find(&ic->acc->set, "display_name"))) { |
---|
747 | g_free(s->value); |
---|
748 | s->value = g_strdup(dn); |
---|
749 | } |
---|
750 | |
---|
751 | // user list needs to be requested for Gadu-Gadu |
---|
752 | purple_gg_buddylist_import(gc); |
---|
753 | |
---|
754 | if (gc->flags & PURPLE_CONNECTION_HTML) { |
---|
755 | ic->flags |= OPT_DOES_HTML; |
---|
756 | } |
---|
757 | } |
---|
758 | |
---|
759 | static void prplcb_conn_disconnected(PurpleConnection *gc) |
---|
760 | { |
---|
761 | struct im_connection *ic = purple_ic_by_gc(gc); |
---|
762 | |
---|
763 | if (ic != NULL) { |
---|
764 | imc_logout(ic, !gc->wants_to_die); |
---|
765 | } |
---|
766 | } |
---|
767 | |
---|
768 | static void prplcb_conn_notice(PurpleConnection *gc, const char *text) |
---|
769 | { |
---|
770 | struct im_connection *ic = purple_ic_by_gc(gc); |
---|
771 | |
---|
772 | if (ic != NULL) { |
---|
773 | imcb_log(ic, "%s", text); |
---|
774 | } |
---|
775 | } |
---|
776 | |
---|
777 | static void prplcb_conn_report_disconnect_reason(PurpleConnection *gc, PurpleConnectionError reason, const char *text) |
---|
778 | { |
---|
779 | struct im_connection *ic = purple_ic_by_gc(gc); |
---|
780 | |
---|
781 | /* PURPLE_CONNECTION_ERROR_NAME_IN_USE means concurrent login, |
---|
782 | should probably handle that. */ |
---|
783 | if (ic != NULL) { |
---|
784 | imcb_error(ic, "%s", text); |
---|
785 | } |
---|
786 | } |
---|
787 | |
---|
788 | static PurpleConnectionUiOps bee_conn_uiops = |
---|
789 | { |
---|
790 | prplcb_conn_progress, |
---|
791 | prplcb_conn_connected, |
---|
792 | prplcb_conn_disconnected, |
---|
793 | prplcb_conn_notice, |
---|
794 | NULL, |
---|
795 | NULL, |
---|
796 | NULL, |
---|
797 | prplcb_conn_report_disconnect_reason, |
---|
798 | }; |
---|
799 | |
---|
800 | static void prplcb_blist_update(PurpleBuddyList *list, PurpleBlistNode *node) |
---|
801 | { |
---|
802 | if (node->type == PURPLE_BLIST_BUDDY_NODE) { |
---|
803 | PurpleBuddy *bud = (PurpleBuddy *) node; |
---|
804 | PurpleGroup *group = purple_buddy_get_group(bud); |
---|
805 | struct im_connection *ic = purple_ic_by_pa(bud->account); |
---|
806 | PurpleStatus *as; |
---|
807 | int flags = 0; |
---|
808 | |
---|
809 | if (ic == NULL) { |
---|
810 | return; |
---|
811 | } |
---|
812 | |
---|
813 | if (bud->server_alias) { |
---|
814 | imcb_rename_buddy(ic, bud->name, bud->server_alias); |
---|
815 | } else if (bud->alias) { |
---|
816 | imcb_rename_buddy(ic, bud->name, bud->alias); |
---|
817 | } |
---|
818 | |
---|
819 | if (group) { |
---|
820 | imcb_add_buddy(ic, bud->name, purple_group_get_name(group)); |
---|
821 | } |
---|
822 | |
---|
823 | flags |= purple_presence_is_online(bud->presence) ? OPT_LOGGED_IN : 0; |
---|
824 | flags |= purple_presence_is_available(bud->presence) ? 0 : OPT_AWAY; |
---|
825 | |
---|
826 | as = purple_presence_get_active_status(bud->presence); |
---|
827 | |
---|
828 | imcb_buddy_status(ic, bud->name, flags, purple_status_get_name(as), |
---|
829 | purple_status_get_attr_string(as, "message")); |
---|
830 | |
---|
831 | imcb_buddy_times(ic, bud->name, |
---|
832 | purple_presence_get_login_time(bud->presence), |
---|
833 | purple_presence_get_idle_time(bud->presence)); |
---|
834 | } |
---|
835 | } |
---|
836 | |
---|
837 | static void prplcb_blist_new(PurpleBlistNode *node) |
---|
838 | { |
---|
839 | if (node->type == PURPLE_BLIST_BUDDY_NODE) { |
---|
840 | PurpleBuddy *bud = (PurpleBuddy *) node; |
---|
841 | struct im_connection *ic = purple_ic_by_pa(bud->account); |
---|
842 | |
---|
843 | if (ic == NULL) { |
---|
844 | return; |
---|
845 | } |
---|
846 | |
---|
847 | imcb_add_buddy(ic, bud->name, NULL); |
---|
848 | |
---|
849 | prplcb_blist_update(NULL, node); |
---|
850 | } |
---|
851 | } |
---|
852 | |
---|
853 | static void prplcb_blist_remove(PurpleBuddyList *list, PurpleBlistNode *node) |
---|
854 | { |
---|
855 | /* |
---|
856 | PurpleBuddy *bud = (PurpleBuddy*) node; |
---|
857 | |
---|
858 | if( node->type == PURPLE_BLIST_BUDDY_NODE ) |
---|
859 | { |
---|
860 | struct im_connection *ic = purple_ic_by_pa( bud->account ); |
---|
861 | |
---|
862 | if( ic == NULL ) |
---|
863 | return; |
---|
864 | |
---|
865 | imcb_remove_buddy( ic, bud->name, NULL ); |
---|
866 | } |
---|
867 | */ |
---|
868 | } |
---|
869 | |
---|
870 | static PurpleBlistUiOps bee_blist_uiops = |
---|
871 | { |
---|
872 | NULL, |
---|
873 | prplcb_blist_new, |
---|
874 | NULL, |
---|
875 | prplcb_blist_update, |
---|
876 | prplcb_blist_remove, |
---|
877 | }; |
---|
878 | |
---|
879 | void prplcb_conv_new(PurpleConversation *conv) |
---|
880 | { |
---|
881 | if (conv->type == PURPLE_CONV_TYPE_CHAT) { |
---|
882 | struct im_connection *ic = purple_ic_by_pa(conv->account); |
---|
883 | struct groupchat *gc; |
---|
884 | |
---|
885 | gc = imcb_chat_new(ic, conv->name); |
---|
886 | if (conv->title != NULL) { |
---|
887 | imcb_chat_name_hint(gc, conv->title); |
---|
888 | imcb_chat_topic(gc, NULL, conv->title, 0); |
---|
889 | } |
---|
890 | |
---|
891 | conv->ui_data = gc; |
---|
892 | gc->data = conv; |
---|
893 | |
---|
894 | /* libpurple brokenness: Whatever. Show that we join right away, |
---|
895 | there's no clear "This is you!" signaling in _add_users so |
---|
896 | don't even try. */ |
---|
897 | imcb_chat_add_buddy(gc, gc->ic->acc->user); |
---|
898 | } |
---|
899 | } |
---|
900 | |
---|
901 | void prplcb_conv_free(PurpleConversation *conv) |
---|
902 | { |
---|
903 | struct groupchat *gc = conv->ui_data; |
---|
904 | |
---|
905 | imcb_chat_free(gc); |
---|
906 | } |
---|
907 | |
---|
908 | void prplcb_conv_add_users(PurpleConversation *conv, GList *cbuddies, gboolean new_arrivals) |
---|
909 | { |
---|
910 | struct groupchat *gc = conv->ui_data; |
---|
911 | GList *b; |
---|
912 | |
---|
913 | for (b = cbuddies; b; b = b->next) { |
---|
914 | PurpleConvChatBuddy *pcb = b->data; |
---|
915 | |
---|
916 | imcb_chat_add_buddy(gc, pcb->name); |
---|
917 | } |
---|
918 | } |
---|
919 | |
---|
920 | void prplcb_conv_del_users(PurpleConversation *conv, GList *cbuddies) |
---|
921 | { |
---|
922 | struct groupchat *gc = conv->ui_data; |
---|
923 | GList *b; |
---|
924 | |
---|
925 | for (b = cbuddies; b; b = b->next) { |
---|
926 | imcb_chat_remove_buddy(gc, b->data, ""); |
---|
927 | } |
---|
928 | } |
---|
929 | |
---|
930 | void prplcb_conv_chat_msg(PurpleConversation *conv, const char *who, const char *message, PurpleMessageFlags flags, |
---|
931 | time_t mtime) |
---|
932 | { |
---|
933 | struct groupchat *gc = conv->ui_data; |
---|
934 | PurpleBuddy *buddy; |
---|
935 | |
---|
936 | /* ..._SEND means it's an outgoing message, no need to echo those. */ |
---|
937 | if (flags & PURPLE_MESSAGE_SEND) { |
---|
938 | return; |
---|
939 | } |
---|
940 | |
---|
941 | buddy = purple_find_buddy(conv->account, who); |
---|
942 | if (buddy != NULL) { |
---|
943 | who = purple_buddy_get_name(buddy); |
---|
944 | } |
---|
945 | |
---|
946 | imcb_chat_msg(gc, who, (char *) message, 0, mtime); |
---|
947 | } |
---|
948 | |
---|
949 | static void prplcb_conv_im(PurpleConversation *conv, const char *who, const char *message, PurpleMessageFlags flags, |
---|
950 | time_t mtime) |
---|
951 | { |
---|
952 | struct im_connection *ic = purple_ic_by_pa(conv->account); |
---|
953 | PurpleBuddy *buddy; |
---|
954 | |
---|
955 | /* ..._SEND means it's an outgoing message, no need to echo those. */ |
---|
956 | if (flags & PURPLE_MESSAGE_SEND) { |
---|
957 | return; |
---|
958 | } |
---|
959 | |
---|
960 | buddy = purple_find_buddy(conv->account, who); |
---|
961 | if (buddy != NULL) { |
---|
962 | who = purple_buddy_get_name(buddy); |
---|
963 | } |
---|
964 | |
---|
965 | imcb_buddy_msg(ic, (char *) who, (char *) message, 0, mtime); |
---|
966 | } |
---|
967 | |
---|
968 | /* No, this is not a ui_op but a signal. */ |
---|
969 | static void prplcb_buddy_typing(PurpleAccount *account, const char *who, gpointer null) |
---|
970 | { |
---|
971 | PurpleConversation *conv; |
---|
972 | PurpleConvIm *im; |
---|
973 | int state; |
---|
974 | |
---|
975 | if ((conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, who, account)) == NULL) { |
---|
976 | return; |
---|
977 | } |
---|
978 | |
---|
979 | im = PURPLE_CONV_IM(conv); |
---|
980 | switch (purple_conv_im_get_typing_state(im)) { |
---|
981 | case PURPLE_TYPING: |
---|
982 | state = OPT_TYPING; |
---|
983 | break; |
---|
984 | case PURPLE_TYPED: |
---|
985 | state = OPT_THINKING; |
---|
986 | break; |
---|
987 | default: |
---|
988 | state = 0; |
---|
989 | } |
---|
990 | |
---|
991 | imcb_buddy_typing(purple_ic_by_pa(account), who, state); |
---|
992 | } |
---|
993 | |
---|
994 | static PurpleConversationUiOps bee_conv_uiops = |
---|
995 | { |
---|
996 | prplcb_conv_new, /* create_conversation */ |
---|
997 | prplcb_conv_free, /* destroy_conversation */ |
---|
998 | prplcb_conv_chat_msg, /* write_chat */ |
---|
999 | prplcb_conv_im, /* write_im */ |
---|
1000 | NULL, /* write_conv */ |
---|
1001 | prplcb_conv_add_users, /* chat_add_users */ |
---|
1002 | NULL, /* chat_rename_user */ |
---|
1003 | prplcb_conv_del_users, /* chat_remove_users */ |
---|
1004 | NULL, /* chat_update_user */ |
---|
1005 | NULL, /* present */ |
---|
1006 | NULL, /* has_focus */ |
---|
1007 | NULL, /* custom_smiley_add */ |
---|
1008 | NULL, /* custom_smiley_write */ |
---|
1009 | NULL, /* custom_smiley_close */ |
---|
1010 | NULL, /* send_confirm */ |
---|
1011 | }; |
---|
1012 | |
---|
1013 | struct prplcb_request_action_data { |
---|
1014 | void *user_data, *bee_data; |
---|
1015 | PurpleRequestActionCb yes, no; |
---|
1016 | int yes_i, no_i; |
---|
1017 | }; |
---|
1018 | |
---|
1019 | static void prplcb_request_action_yes(void *data) |
---|
1020 | { |
---|
1021 | struct prplcb_request_action_data *pqad = data; |
---|
1022 | |
---|
1023 | if (pqad->yes) { |
---|
1024 | pqad->yes(pqad->user_data, pqad->yes_i); |
---|
1025 | } |
---|
1026 | } |
---|
1027 | |
---|
1028 | static void prplcb_request_action_no(void *data) |
---|
1029 | { |
---|
1030 | struct prplcb_request_action_data *pqad = data; |
---|
1031 | |
---|
1032 | if (pqad->no) { |
---|
1033 | pqad->no(pqad->user_data, pqad->no_i); |
---|
1034 | } |
---|
1035 | } |
---|
1036 | |
---|
1037 | /* q->free() callback from query_del()*/ |
---|
1038 | static void prplcb_request_action_free(void *data) |
---|
1039 | { |
---|
1040 | struct prplcb_request_action_data *pqad = data; |
---|
1041 | |
---|
1042 | pqad->bee_data = NULL; |
---|
1043 | purple_request_close(PURPLE_REQUEST_ACTION, pqad); |
---|
1044 | } |
---|
1045 | |
---|
1046 | static void *prplcb_request_action(const char *title, const char *primary, const char *secondary, |
---|
1047 | int default_action, PurpleAccount *account, const char *who, |
---|
1048 | PurpleConversation *conv, void *user_data, size_t action_count, |
---|
1049 | va_list actions) |
---|
1050 | { |
---|
1051 | struct prplcb_request_action_data *pqad; |
---|
1052 | int i; |
---|
1053 | char *q; |
---|
1054 | |
---|
1055 | pqad = g_new0(struct prplcb_request_action_data, 1); |
---|
1056 | |
---|
1057 | for (i = 0; i < action_count; i++) { |
---|
1058 | char *caption; |
---|
1059 | void *fn; |
---|
1060 | |
---|
1061 | caption = va_arg(actions, char*); |
---|
1062 | fn = va_arg(actions, void*); |
---|
1063 | |
---|
1064 | if (strstr(caption, "Accept") || strstr(caption, "OK")) { |
---|
1065 | pqad->yes = fn; |
---|
1066 | pqad->yes_i = i; |
---|
1067 | } else if (strstr(caption, "Reject") || strstr(caption, "Cancel")) { |
---|
1068 | pqad->no = fn; |
---|
1069 | pqad->no_i = i; |
---|
1070 | } |
---|
1071 | } |
---|
1072 | |
---|
1073 | pqad->user_data = user_data; |
---|
1074 | |
---|
1075 | /* TODO: IRC stuff here :-( */ |
---|
1076 | q = g_strdup_printf("Request: %s\n\n%s\n\n%s", title, primary, secondary); |
---|
1077 | pqad->bee_data = query_add(local_bee->ui_data, purple_ic_by_pa(account), q, |
---|
1078 | prplcb_request_action_yes, prplcb_request_action_no, |
---|
1079 | prplcb_request_action_free, pqad); |
---|
1080 | |
---|
1081 | g_free(q); |
---|
1082 | |
---|
1083 | return pqad; |
---|
1084 | } |
---|
1085 | |
---|
1086 | /* So it turns out some requests have no account context at all, because |
---|
1087 | * libpurple hates us. This means that query_del_by_conn() won't remove those |
---|
1088 | * on logout, and will segfault if the user replies. That's why this exists. |
---|
1089 | */ |
---|
1090 | static void prplcb_close_request(PurpleRequestType type, void *data) |
---|
1091 | { |
---|
1092 | struct prplcb_request_action_data *pqad; |
---|
1093 | struct request_input_data *ri; |
---|
1094 | struct purple_data *pd; |
---|
1095 | |
---|
1096 | if (!data) { |
---|
1097 | return; |
---|
1098 | } |
---|
1099 | |
---|
1100 | switch (type) { |
---|
1101 | case PURPLE_REQUEST_ACTION: |
---|
1102 | pqad = data; |
---|
1103 | /* if this is null, it's because query_del was run already */ |
---|
1104 | if (pqad->bee_data) { |
---|
1105 | query_del(local_bee->ui_data, pqad->bee_data); |
---|
1106 | } |
---|
1107 | g_free(pqad); |
---|
1108 | break; |
---|
1109 | case PURPLE_REQUEST_INPUT: |
---|
1110 | ri = data; |
---|
1111 | pd = ri->ic->proto_data; |
---|
1112 | imcb_remove_buddy(ri->ic, ri->buddy, NULL); |
---|
1113 | g_free(ri->buddy); |
---|
1114 | g_hash_table_remove(pd->input_requests, GUINT_TO_POINTER(ri->id)); |
---|
1115 | break; |
---|
1116 | default: |
---|
1117 | g_free(data); |
---|
1118 | break; |
---|
1119 | } |
---|
1120 | |
---|
1121 | } |
---|
1122 | |
---|
1123 | void* prplcb_request_input(const char *title, const char *primary, |
---|
1124 | const char *secondary, const char *default_value, gboolean multiline, |
---|
1125 | gboolean masked, gchar *hint, const char *ok_text, GCallback ok_cb, |
---|
1126 | const char *cancel_text, GCallback cancel_cb, PurpleAccount *account, |
---|
1127 | const char *who, PurpleConversation *conv, void *user_data) |
---|
1128 | { |
---|
1129 | struct im_connection *ic = purple_ic_by_pa(account); |
---|
1130 | struct purple_data *pd = ic->proto_data; |
---|
1131 | struct request_input_data *ri = g_new0(struct request_input_data, 1); |
---|
1132 | guint id = pd->next_request_id++; |
---|
1133 | |
---|
1134 | ri->id = id; |
---|
1135 | ri->ic = ic; |
---|
1136 | ri->buddy = g_strdup_printf("%s_%u", PURPLE_REQUEST_HANDLE, id); |
---|
1137 | ri->data_callback = (ri_callback_t) ok_cb; |
---|
1138 | ri->user_data = user_data; |
---|
1139 | g_hash_table_insert(pd->input_requests, GUINT_TO_POINTER(id), ri); |
---|
1140 | |
---|
1141 | imcb_add_buddy(ic, ri->buddy, NULL); |
---|
1142 | imcb_buddy_msg(ic, ri->buddy, secondary, 0, 0); |
---|
1143 | |
---|
1144 | return ri; |
---|
1145 | } |
---|
1146 | |
---|
1147 | void purple_request_input_callback(guint id, struct im_connection *ic, |
---|
1148 | const char *message, const char *who) |
---|
1149 | { |
---|
1150 | struct purple_data *pd = ic->proto_data; |
---|
1151 | struct request_input_data *ri; |
---|
1152 | |
---|
1153 | if (!(ri = g_hash_table_lookup(pd->input_requests, GUINT_TO_POINTER(id)))) { |
---|
1154 | return; |
---|
1155 | } |
---|
1156 | |
---|
1157 | ri->data_callback(ri->user_data, message); |
---|
1158 | |
---|
1159 | purple_request_close(PURPLE_REQUEST_INPUT, ri); |
---|
1160 | } |
---|
1161 | |
---|
1162 | |
---|
1163 | static PurpleRequestUiOps bee_request_uiops = |
---|
1164 | { |
---|
1165 | prplcb_request_input, |
---|
1166 | NULL, |
---|
1167 | prplcb_request_action, |
---|
1168 | NULL, |
---|
1169 | NULL, |
---|
1170 | prplcb_close_request, |
---|
1171 | NULL, |
---|
1172 | }; |
---|
1173 | |
---|
1174 | static void prplcb_privacy_permit_added(PurpleAccount *account, const char *name) |
---|
1175 | { |
---|
1176 | struct im_connection *ic = purple_ic_by_pa(account); |
---|
1177 | |
---|
1178 | if (!g_slist_find_custom(ic->permit, name, (GCompareFunc) ic->acc->prpl->handle_cmp)) { |
---|
1179 | ic->permit = g_slist_prepend(ic->permit, g_strdup(name)); |
---|
1180 | } |
---|
1181 | } |
---|
1182 | |
---|
1183 | static void prplcb_privacy_permit_removed(PurpleAccount *account, const char *name) |
---|
1184 | { |
---|
1185 | struct im_connection *ic = purple_ic_by_pa(account); |
---|
1186 | void *n; |
---|
1187 | |
---|
1188 | n = g_slist_find_custom(ic->permit, name, (GCompareFunc) ic->acc->prpl->handle_cmp); |
---|
1189 | ic->permit = g_slist_remove(ic->permit, n); |
---|
1190 | } |
---|
1191 | |
---|
1192 | static void prplcb_privacy_deny_added(PurpleAccount *account, const char *name) |
---|
1193 | { |
---|
1194 | struct im_connection *ic = purple_ic_by_pa(account); |
---|
1195 | |
---|
1196 | if (!g_slist_find_custom(ic->deny, name, (GCompareFunc) ic->acc->prpl->handle_cmp)) { |
---|
1197 | ic->deny = g_slist_prepend(ic->deny, g_strdup(name)); |
---|
1198 | } |
---|
1199 | } |
---|
1200 | |
---|
1201 | static void prplcb_privacy_deny_removed(PurpleAccount *account, const char *name) |
---|
1202 | { |
---|
1203 | struct im_connection *ic = purple_ic_by_pa(account); |
---|
1204 | void *n; |
---|
1205 | |
---|
1206 | n = g_slist_find_custom(ic->deny, name, (GCompareFunc) ic->acc->prpl->handle_cmp); |
---|
1207 | ic->deny = g_slist_remove(ic->deny, n); |
---|
1208 | } |
---|
1209 | |
---|
1210 | static PurplePrivacyUiOps bee_privacy_uiops = |
---|
1211 | { |
---|
1212 | prplcb_privacy_permit_added, |
---|
1213 | prplcb_privacy_permit_removed, |
---|
1214 | prplcb_privacy_deny_added, |
---|
1215 | prplcb_privacy_deny_removed, |
---|
1216 | }; |
---|
1217 | |
---|
1218 | static void prplcb_debug_print(PurpleDebugLevel level, const char *category, const char *arg_s) |
---|
1219 | { |
---|
1220 | fprintf(stderr, "DEBUG %s: %s", category, arg_s); |
---|
1221 | } |
---|
1222 | |
---|
1223 | static PurpleDebugUiOps bee_debug_uiops = |
---|
1224 | { |
---|
1225 | prplcb_debug_print, |
---|
1226 | }; |
---|
1227 | |
---|
1228 | static guint prplcb_ev_timeout_add(guint interval, GSourceFunc func, gpointer udata) |
---|
1229 | { |
---|
1230 | return b_timeout_add(interval, (b_event_handler) func, udata); |
---|
1231 | } |
---|
1232 | |
---|
1233 | static guint prplcb_ev_input_add(int fd, PurpleInputCondition cond, PurpleInputFunction func, gpointer udata) |
---|
1234 | { |
---|
1235 | return b_input_add(fd, cond | B_EV_FLAG_FORCE_REPEAT, (b_event_handler) func, udata); |
---|
1236 | } |
---|
1237 | |
---|
1238 | static gboolean prplcb_ev_remove(guint id) |
---|
1239 | { |
---|
1240 | b_event_remove((gint) id); |
---|
1241 | return TRUE; |
---|
1242 | } |
---|
1243 | |
---|
1244 | static PurpleEventLoopUiOps glib_eventloops = |
---|
1245 | { |
---|
1246 | prplcb_ev_timeout_add, |
---|
1247 | prplcb_ev_remove, |
---|
1248 | prplcb_ev_input_add, |
---|
1249 | prplcb_ev_remove, |
---|
1250 | }; |
---|
1251 | |
---|
1252 | static void *prplcb_notify_email(PurpleConnection *gc, const char *subject, const char *from, |
---|
1253 | const char *to, const char *url) |
---|
1254 | { |
---|
1255 | struct im_connection *ic = purple_ic_by_gc(gc); |
---|
1256 | |
---|
1257 | imcb_log(ic, "Received e-mail from %s for %s: %s <%s>", from, to, subject, url); |
---|
1258 | |
---|
1259 | return NULL; |
---|
1260 | } |
---|
1261 | |
---|
1262 | static void *prplcb_notify_userinfo(PurpleConnection *gc, const char *who, PurpleNotifyUserInfo *user_info) |
---|
1263 | { |
---|
1264 | struct im_connection *ic = purple_ic_by_gc(gc); |
---|
1265 | GString *info = g_string_new(""); |
---|
1266 | GList *l = purple_notify_user_info_get_entries(user_info); |
---|
1267 | char *key; |
---|
1268 | const char *value; |
---|
1269 | int n; |
---|
1270 | |
---|
1271 | while (l) { |
---|
1272 | PurpleNotifyUserInfoEntry *e = l->data; |
---|
1273 | |
---|
1274 | switch (purple_notify_user_info_entry_get_type(e)) { |
---|
1275 | case PURPLE_NOTIFY_USER_INFO_ENTRY_PAIR: |
---|
1276 | case PURPLE_NOTIFY_USER_INFO_ENTRY_SECTION_HEADER: |
---|
1277 | key = g_strdup(purple_notify_user_info_entry_get_label(e)); |
---|
1278 | value = purple_notify_user_info_entry_get_value(e); |
---|
1279 | |
---|
1280 | if (key) { |
---|
1281 | strip_html(key); |
---|
1282 | g_string_append_printf(info, "%s: ", key); |
---|
1283 | |
---|
1284 | if (value) { |
---|
1285 | n = strlen(value) - 1; |
---|
1286 | while (g_ascii_isspace(value[n])) { |
---|
1287 | n--; |
---|
1288 | } |
---|
1289 | g_string_append_len(info, value, n + 1); |
---|
1290 | } |
---|
1291 | g_string_append_c(info, '\n'); |
---|
1292 | g_free(key); |
---|
1293 | } |
---|
1294 | |
---|
1295 | break; |
---|
1296 | case PURPLE_NOTIFY_USER_INFO_ENTRY_SECTION_BREAK: |
---|
1297 | g_string_append(info, "------------------------\n"); |
---|
1298 | break; |
---|
1299 | } |
---|
1300 | |
---|
1301 | l = l->next; |
---|
1302 | } |
---|
1303 | |
---|
1304 | imcb_log(ic, "User %s info:\n%s", who, info->str); |
---|
1305 | g_string_free(info, TRUE); |
---|
1306 | |
---|
1307 | return NULL; |
---|
1308 | } |
---|
1309 | |
---|
1310 | static PurpleNotifyUiOps bee_notify_uiops = |
---|
1311 | { |
---|
1312 | NULL, |
---|
1313 | prplcb_notify_email, |
---|
1314 | NULL, |
---|
1315 | NULL, |
---|
1316 | NULL, |
---|
1317 | NULL, |
---|
1318 | prplcb_notify_userinfo, |
---|
1319 | }; |
---|
1320 | |
---|
1321 | static void *prplcb_account_request_authorize(PurpleAccount *account, const char *remote_user, |
---|
1322 | const char *id, const char *alias, const char *message, gboolean on_list, |
---|
1323 | PurpleAccountRequestAuthorizationCb authorize_cb, |
---|
1324 | PurpleAccountRequestAuthorizationCb deny_cb, void *user_data) |
---|
1325 | { |
---|
1326 | struct im_connection *ic = purple_ic_by_pa(account); |
---|
1327 | char *q; |
---|
1328 | |
---|
1329 | if (alias) { |
---|
1330 | q = g_strdup_printf("%s (%s) wants to add you to his/her contact " |
---|
1331 | "list. (%s)", alias, remote_user, message); |
---|
1332 | } else { |
---|
1333 | q = g_strdup_printf("%s wants to add you to his/her contact " |
---|
1334 | "list. (%s)", remote_user, message); |
---|
1335 | } |
---|
1336 | |
---|
1337 | imcb_ask_with_free(ic, q, user_data, authorize_cb, deny_cb, NULL); |
---|
1338 | g_free(q); |
---|
1339 | |
---|
1340 | return NULL; |
---|
1341 | } |
---|
1342 | |
---|
1343 | static PurpleAccountUiOps bee_account_uiops = |
---|
1344 | { |
---|
1345 | NULL, |
---|
1346 | NULL, |
---|
1347 | NULL, |
---|
1348 | prplcb_account_request_authorize, |
---|
1349 | NULL, |
---|
1350 | }; |
---|
1351 | |
---|
1352 | extern PurpleXferUiOps bee_xfer_uiops; |
---|
1353 | |
---|
1354 | static void purple_ui_init() |
---|
1355 | { |
---|
1356 | purple_connections_set_ui_ops(&bee_conn_uiops); |
---|
1357 | purple_blist_set_ui_ops(&bee_blist_uiops); |
---|
1358 | purple_conversations_set_ui_ops(&bee_conv_uiops); |
---|
1359 | purple_request_set_ui_ops(&bee_request_uiops); |
---|
1360 | purple_privacy_set_ui_ops(&bee_privacy_uiops); |
---|
1361 | purple_notify_set_ui_ops(&bee_notify_uiops); |
---|
1362 | purple_accounts_set_ui_ops(&bee_account_uiops); |
---|
1363 | purple_xfers_set_ui_ops(&bee_xfer_uiops); |
---|
1364 | |
---|
1365 | if (getenv("BITLBEE_DEBUG")) { |
---|
1366 | purple_debug_set_ui_ops(&bee_debug_uiops); |
---|
1367 | } |
---|
1368 | } |
---|
1369 | |
---|
1370 | void purple_initmodule() |
---|
1371 | { |
---|
1372 | struct prpl funcs; |
---|
1373 | GList *prots; |
---|
1374 | GString *help; |
---|
1375 | char *dir; |
---|
1376 | |
---|
1377 | if (B_EV_IO_READ != PURPLE_INPUT_READ || |
---|
1378 | B_EV_IO_WRITE != PURPLE_INPUT_WRITE) { |
---|
1379 | /* FIXME FIXME FIXME FIXME FIXME :-) */ |
---|
1380 | exit(1); |
---|
1381 | } |
---|
1382 | |
---|
1383 | dir = g_strdup_printf("%s/purple", global.conf->configdir); |
---|
1384 | purple_util_set_user_dir(dir); |
---|
1385 | g_free(dir); |
---|
1386 | |
---|
1387 | purple_debug_set_enabled(FALSE); |
---|
1388 | purple_core_set_ui_ops(&bee_core_uiops); |
---|
1389 | purple_eventloop_set_ui_ops(&glib_eventloops); |
---|
1390 | if (!purple_core_init("BitlBee")) { |
---|
1391 | /* Initializing the core failed. Terminate. */ |
---|
1392 | fprintf(stderr, "libpurple initialization failed.\n"); |
---|
1393 | abort(); |
---|
1394 | } |
---|
1395 | |
---|
1396 | if (proxytype != PROXY_NONE) { |
---|
1397 | PurpleProxyInfo *pi = purple_global_proxy_get_info(); |
---|
1398 | switch (proxytype) { |
---|
1399 | case PROXY_SOCKS4: |
---|
1400 | purple_proxy_info_set_type(pi, PURPLE_PROXY_SOCKS4); |
---|
1401 | break; |
---|
1402 | case PROXY_SOCKS5: |
---|
1403 | purple_proxy_info_set_type(pi, PURPLE_PROXY_SOCKS5); |
---|
1404 | break; |
---|
1405 | case PROXY_HTTP: |
---|
1406 | purple_proxy_info_set_type(pi, PURPLE_PROXY_HTTP); |
---|
1407 | break; |
---|
1408 | } |
---|
1409 | purple_proxy_info_set_host(pi, proxyhost); |
---|
1410 | purple_proxy_info_set_port(pi, proxyport); |
---|
1411 | purple_proxy_info_set_username(pi, proxyuser); |
---|
1412 | purple_proxy_info_set_password(pi, proxypass); |
---|
1413 | } |
---|
1414 | |
---|
1415 | purple_set_blist(purple_blist_new()); |
---|
1416 | |
---|
1417 | /* No, really. So far there were ui_ops for everything, but now suddenly |
---|
1418 | one needs to use signals for typing notification stuff. :-( */ |
---|
1419 | purple_signal_connect(purple_conversations_get_handle(), "buddy-typing", |
---|
1420 | &funcs, PURPLE_CALLBACK(prplcb_buddy_typing), NULL); |
---|
1421 | purple_signal_connect(purple_conversations_get_handle(), "buddy-typed", |
---|
1422 | &funcs, PURPLE_CALLBACK(prplcb_buddy_typing), NULL); |
---|
1423 | purple_signal_connect(purple_conversations_get_handle(), "buddy-typing-stopped", |
---|
1424 | &funcs, PURPLE_CALLBACK(prplcb_buddy_typing), NULL); |
---|
1425 | |
---|
1426 | memset(&funcs, 0, sizeof(funcs)); |
---|
1427 | funcs.login = purple_login; |
---|
1428 | funcs.init = purple_init; |
---|
1429 | funcs.logout = purple_logout; |
---|
1430 | funcs.buddy_msg = purple_buddy_msg; |
---|
1431 | funcs.away_states = purple_away_states; |
---|
1432 | funcs.set_away = purple_set_away; |
---|
1433 | funcs.add_buddy = purple_add_buddy; |
---|
1434 | funcs.remove_buddy = purple_remove_buddy; |
---|
1435 | funcs.add_permit = purple_add_permit; |
---|
1436 | funcs.add_deny = purple_add_deny; |
---|
1437 | funcs.rem_permit = purple_rem_permit; |
---|
1438 | funcs.rem_deny = purple_rem_deny; |
---|
1439 | funcs.get_info = purple_get_info; |
---|
1440 | funcs.keepalive = purple_keepalive; |
---|
1441 | funcs.send_typing = purple_send_typing; |
---|
1442 | funcs.handle_cmp = g_strcasecmp; |
---|
1443 | /* TODO(wilmer): Set these only for protocols that support them? */ |
---|
1444 | funcs.chat_msg = purple_chat_msg; |
---|
1445 | funcs.chat_with = purple_chat_with; |
---|
1446 | funcs.chat_invite = purple_chat_invite; |
---|
1447 | funcs.chat_kick = purple_chat_kick; |
---|
1448 | funcs.chat_leave = purple_chat_leave; |
---|
1449 | funcs.chat_join = purple_chat_join; |
---|
1450 | funcs.transfer_request = purple_transfer_request; |
---|
1451 | |
---|
1452 | help = g_string_new("BitlBee libpurple module supports the following IM protocols:\n"); |
---|
1453 | |
---|
1454 | /* Add a protocol entry to BitlBee's structures for every protocol |
---|
1455 | supported by this libpurple instance. */ |
---|
1456 | for (prots = purple_plugins_get_protocols(); prots; prots = prots->next) { |
---|
1457 | PurplePlugin *prot = prots->data; |
---|
1458 | struct prpl *ret; |
---|
1459 | |
---|
1460 | /* If we already have this one (as a native module), don't |
---|
1461 | add a libpurple duplicate. */ |
---|
1462 | if (find_protocol(prot->info->id)) { |
---|
1463 | continue; |
---|
1464 | } |
---|
1465 | |
---|
1466 | ret = g_memdup(&funcs, sizeof(funcs)); |
---|
1467 | ret->name = ret->data = prot->info->id; |
---|
1468 | if (strncmp(ret->name, "prpl-", 5) == 0) { |
---|
1469 | ret->name += 5; |
---|
1470 | } |
---|
1471 | register_protocol(ret); |
---|
1472 | |
---|
1473 | g_string_append_printf(help, "\n* %s (%s)", ret->name, prot->info->name); |
---|
1474 | |
---|
1475 | /* libpurple doesn't define a protocol called OSCAR, but we |
---|
1476 | need it to be compatible with normal BitlBee. */ |
---|
1477 | if (g_strcasecmp(prot->info->id, "prpl-aim") == 0) { |
---|
1478 | ret = g_memdup(&funcs, sizeof(funcs)); |
---|
1479 | ret->name = "oscar"; |
---|
1480 | ret->data = prot->info->id; |
---|
1481 | register_protocol(ret); |
---|
1482 | } |
---|
1483 | } |
---|
1484 | |
---|
1485 | g_string_append(help, "\n\nFor used protocols, more information about available " |
---|
1486 | "settings can be found using \x02help purple <protocol name>\x02 " |
---|
1487 | "(create an account using that protocol first!)"); |
---|
1488 | |
---|
1489 | /* Add a simple dynamically-generated help item listing all |
---|
1490 | the supported protocols. */ |
---|
1491 | help_add_mem(&global.help, "purple", help->str); |
---|
1492 | g_string_free(help, TRUE); |
---|
1493 | } |
---|