1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2012 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* |
---|
8 | * nogaim |
---|
9 | * |
---|
10 | * Gaim without gaim - for BitlBee |
---|
11 | * |
---|
12 | * This file contains functions called by the Gaim IM-modules. It's written |
---|
13 | * from scratch for BitlBee and doesn't contain any code from Gaim anymore |
---|
14 | * (except for the function names). |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | This program is free software; you can redistribute it and/or modify |
---|
19 | it under the terms of the GNU General Public License as published by |
---|
20 | the Free Software Foundation; either version 2 of the License, or |
---|
21 | (at your option) any later version. |
---|
22 | |
---|
23 | This program is distributed in the hope that it will be useful, |
---|
24 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
25 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
26 | GNU General Public License for more details. |
---|
27 | |
---|
28 | You should have received a copy of the GNU General Public License with |
---|
29 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
30 | if not, write to the Free Software Foundation, Inc., 51 Franklin St., |
---|
31 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
32 | */ |
---|
33 | |
---|
34 | #define BITLBEE_CORE |
---|
35 | #include <ctype.h> |
---|
36 | |
---|
37 | #include "nogaim.h" |
---|
38 | |
---|
39 | GSList *connections; |
---|
40 | |
---|
41 | #ifdef WITH_PLUGINS |
---|
42 | gboolean load_plugin(char *path) |
---|
43 | { |
---|
44 | void (*init_function) (void); |
---|
45 | |
---|
46 | GModule *mod = g_module_open(path, G_MODULE_BIND_LAZY); |
---|
47 | |
---|
48 | if (!mod) { |
---|
49 | log_message(LOGLVL_ERROR, "Can't find `%s', not loading (%s)\n", path, g_module_error()); |
---|
50 | return FALSE; |
---|
51 | } |
---|
52 | |
---|
53 | if (!g_module_symbol(mod, "init_plugin", (gpointer *) &init_function)) { |
---|
54 | log_message(LOGLVL_WARNING, "Can't find function `init_plugin' in `%s'\n", path); |
---|
55 | return FALSE; |
---|
56 | } |
---|
57 | |
---|
58 | init_function(); |
---|
59 | |
---|
60 | return TRUE; |
---|
61 | } |
---|
62 | |
---|
63 | void load_plugins(void) |
---|
64 | { |
---|
65 | GDir *dir; |
---|
66 | GError *error = NULL; |
---|
67 | |
---|
68 | dir = g_dir_open(global.conf->plugindir, 0, &error); |
---|
69 | |
---|
70 | if (dir) { |
---|
71 | const gchar *entry; |
---|
72 | char *path; |
---|
73 | |
---|
74 | while ((entry = g_dir_read_name(dir))) { |
---|
75 | path = g_build_filename(global.conf->plugindir, entry, NULL); |
---|
76 | if (!path) { |
---|
77 | log_message(LOGLVL_WARNING, "Can't build path for %s\n", entry); |
---|
78 | continue; |
---|
79 | } |
---|
80 | |
---|
81 | load_plugin(path); |
---|
82 | |
---|
83 | g_free(path); |
---|
84 | } |
---|
85 | |
---|
86 | g_dir_close(dir); |
---|
87 | } |
---|
88 | } |
---|
89 | #endif |
---|
90 | |
---|
91 | GList *protocols = NULL; |
---|
92 | GList *disabled_protocols = NULL; |
---|
93 | |
---|
94 | void register_protocol(struct prpl *p) |
---|
95 | { |
---|
96 | int i; |
---|
97 | gboolean refused = global.conf->protocols != NULL; |
---|
98 | |
---|
99 | for (i = 0; global.conf->protocols && global.conf->protocols[i]; i++) { |
---|
100 | if (g_strcasecmp(p->name, global.conf->protocols[i]) == 0) { |
---|
101 | refused = FALSE; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | if (refused) { |
---|
106 | disabled_protocols = g_list_append(disabled_protocols, p); |
---|
107 | } else { |
---|
108 | protocols = g_list_append(protocols, p); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | static int proto_name_cmp(const void *proto_, const void *name) |
---|
113 | { |
---|
114 | const struct prpl *proto = proto_; |
---|
115 | return g_strcasecmp(proto->name, name); |
---|
116 | } |
---|
117 | |
---|
118 | struct prpl *find_protocol(const char *name) |
---|
119 | { |
---|
120 | GList *gl = g_list_find_custom(protocols, name, proto_name_cmp); |
---|
121 | return gl ? gl->data: NULL; |
---|
122 | } |
---|
123 | |
---|
124 | gboolean is_protocol_disabled(const char *name) |
---|
125 | { |
---|
126 | return g_list_find_custom(disabled_protocols, name, proto_name_cmp) != NULL; |
---|
127 | } |
---|
128 | |
---|
129 | void nogaim_init() |
---|
130 | { |
---|
131 | extern void msn_initmodule(); |
---|
132 | extern void oscar_initmodule(); |
---|
133 | extern void byahoo_initmodule(); |
---|
134 | extern void jabber_initmodule(); |
---|
135 | extern void twitter_initmodule(); |
---|
136 | extern void purple_initmodule(); |
---|
137 | extern void rpc_initmodule(); |
---|
138 | |
---|
139 | #ifdef WITH_MSN |
---|
140 | msn_initmodule(); |
---|
141 | #endif |
---|
142 | |
---|
143 | #ifdef WITH_OSCAR |
---|
144 | oscar_initmodule(); |
---|
145 | #endif |
---|
146 | |
---|
147 | #ifdef WITH_YAHOO |
---|
148 | byahoo_initmodule(); |
---|
149 | #endif |
---|
150 | |
---|
151 | #ifdef WITH_JABBER |
---|
152 | jabber_initmodule(); |
---|
153 | #endif |
---|
154 | |
---|
155 | #ifdef WITH_TWITTER |
---|
156 | twitter_initmodule(); |
---|
157 | #endif |
---|
158 | |
---|
159 | #ifdef WITH_PURPLE |
---|
160 | purple_initmodule(); |
---|
161 | #endif |
---|
162 | |
---|
163 | #ifdef WITH_RPC |
---|
164 | rpc_initmodule(); |
---|
165 | #endif |
---|
166 | |
---|
167 | #ifdef WITH_PLUGINS |
---|
168 | load_plugins(); |
---|
169 | #endif |
---|
170 | } |
---|
171 | |
---|
172 | GSList *get_connections() |
---|
173 | { |
---|
174 | return connections; |
---|
175 | } |
---|
176 | |
---|
177 | struct im_connection *imcb_new(account_t *acc) |
---|
178 | { |
---|
179 | struct im_connection *ic; |
---|
180 | |
---|
181 | ic = g_new0(struct im_connection, 1); |
---|
182 | |
---|
183 | ic->bee = acc->bee; |
---|
184 | ic->acc = acc; |
---|
185 | acc->ic = ic; |
---|
186 | |
---|
187 | connections = g_slist_append(connections, ic); |
---|
188 | |
---|
189 | return(ic); |
---|
190 | } |
---|
191 | |
---|
192 | void imc_free(struct im_connection *ic) |
---|
193 | { |
---|
194 | account_t *a; |
---|
195 | |
---|
196 | /* Destroy the pointer to this connection from the account list */ |
---|
197 | for (a = ic->bee->accounts; a; a = a->next) { |
---|
198 | if (a->ic == ic) { |
---|
199 | a->ic = NULL; |
---|
200 | break; |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | connections = g_slist_remove(connections, ic); |
---|
205 | g_free(ic); |
---|
206 | } |
---|
207 | |
---|
208 | static void serv_got_crap(struct im_connection *ic, char *format, ...) |
---|
209 | { |
---|
210 | va_list params; |
---|
211 | char *text; |
---|
212 | account_t *a; |
---|
213 | |
---|
214 | if (!ic->bee->ui->log) { |
---|
215 | return; |
---|
216 | } |
---|
217 | |
---|
218 | va_start(params, format); |
---|
219 | text = g_strdup_vprintf(format, params); |
---|
220 | va_end(params); |
---|
221 | |
---|
222 | if ((g_strcasecmp(set_getstr(&ic->bee->set, "strip_html"), "always") == 0) || |
---|
223 | ((ic->flags & OPT_DOES_HTML) && set_getbool(&ic->bee->set, "strip_html"))) { |
---|
224 | strip_html(text); |
---|
225 | } |
---|
226 | |
---|
227 | /* Try to find a different connection on the same protocol. */ |
---|
228 | for (a = ic->bee->accounts; a; a = a->next) { |
---|
229 | if (a->prpl == ic->acc->prpl && a->ic != ic) { |
---|
230 | break; |
---|
231 | } |
---|
232 | } |
---|
233 | |
---|
234 | /* If we found one, include the screenname in the message. */ |
---|
235 | if (a) { |
---|
236 | ic->bee->ui->log(ic->bee, ic->acc->tag, text); |
---|
237 | } else { |
---|
238 | ic->bee->ui->log(ic->bee, ic->acc->prpl->name, text); |
---|
239 | } |
---|
240 | |
---|
241 | g_free(text); |
---|
242 | } |
---|
243 | |
---|
244 | void imcb_log(struct im_connection *ic, char *format, ...) |
---|
245 | { |
---|
246 | va_list params; |
---|
247 | char *text; |
---|
248 | |
---|
249 | va_start(params, format); |
---|
250 | text = g_strdup_vprintf(format, params); |
---|
251 | va_end(params); |
---|
252 | |
---|
253 | if (ic->flags & OPT_LOGGED_IN) { |
---|
254 | serv_got_crap(ic, "%s", text); |
---|
255 | } else { |
---|
256 | serv_got_crap(ic, "Logging in: %s", text); |
---|
257 | } |
---|
258 | |
---|
259 | g_free(text); |
---|
260 | } |
---|
261 | |
---|
262 | void imcb_error(struct im_connection *ic, char *format, ...) |
---|
263 | { |
---|
264 | va_list params; |
---|
265 | char *text; |
---|
266 | |
---|
267 | va_start(params, format); |
---|
268 | text = g_strdup_vprintf(format, params); |
---|
269 | va_end(params); |
---|
270 | |
---|
271 | if (ic->flags & OPT_LOGGED_IN) { |
---|
272 | serv_got_crap(ic, "Error: %s", text); |
---|
273 | } else { |
---|
274 | serv_got_crap(ic, "Login error: %s", text); |
---|
275 | } |
---|
276 | |
---|
277 | g_free(text); |
---|
278 | } |
---|
279 | |
---|
280 | static gboolean send_keepalive(gpointer d, gint fd, b_input_condition cond) |
---|
281 | { |
---|
282 | struct im_connection *ic = d; |
---|
283 | |
---|
284 | if ((ic->flags & OPT_PONGS) && !(ic->flags & OPT_PONGED)) { |
---|
285 | /* This protocol is expected to ack keepalives and hasn't |
---|
286 | since the last time we were here. */ |
---|
287 | imcb_error(ic, "Connection timeout"); |
---|
288 | imc_logout(ic, TRUE); |
---|
289 | return FALSE; |
---|
290 | } |
---|
291 | ic->flags &= ~OPT_PONGED; |
---|
292 | |
---|
293 | if (ic->acc->prpl->keepalive) { |
---|
294 | ic->acc->prpl->keepalive(ic); |
---|
295 | } |
---|
296 | |
---|
297 | return TRUE; |
---|
298 | } |
---|
299 | |
---|
300 | void start_keepalives(struct im_connection *ic, int interval) |
---|
301 | { |
---|
302 | b_event_remove(ic->keepalive); |
---|
303 | ic->keepalive = b_timeout_add(interval, send_keepalive, ic); |
---|
304 | |
---|
305 | /* Connecting successfully counts as a first successful pong. */ |
---|
306 | if (ic->flags & OPT_PONGS) { |
---|
307 | ic->flags |= OPT_PONGED; |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | void imcb_connected(struct im_connection *ic) |
---|
312 | { |
---|
313 | /* MSN servers sometimes redirect you to a different server and do |
---|
314 | the whole login sequence again, so these "late" calls to this |
---|
315 | function should be handled correctly. (IOW, ignored) */ |
---|
316 | if (ic->flags & OPT_LOGGED_IN) { |
---|
317 | return; |
---|
318 | } |
---|
319 | |
---|
320 | if ((ic->acc->flags & ACC_FLAG_LOCAL_CONTACTS) && |
---|
321 | !(ic->flags & OPT_LOCAL_CONTACTS_SENT) && |
---|
322 | ic->acc->prpl->add_buddy) { |
---|
323 | GHashTableIter nicks; |
---|
324 | gpointer handle; |
---|
325 | g_hash_table_iter_init(&nicks, ic->acc->nicks); |
---|
326 | while (g_hash_table_iter_next(&nicks, &handle, NULL)) { |
---|
327 | ic->acc->prpl->add_buddy(ic, (char *) handle, NULL); |
---|
328 | } |
---|
329 | } |
---|
330 | |
---|
331 | imcb_log(ic, "Logged in"); |
---|
332 | |
---|
333 | ic->flags |= OPT_LOGGED_IN; |
---|
334 | start_keepalives(ic, 60000); |
---|
335 | |
---|
336 | /* Necessary to send initial presence status, even if we're not away. */ |
---|
337 | imc_away_send_update(ic); |
---|
338 | |
---|
339 | /* Apparently we're connected successfully, so reset the |
---|
340 | exponential backoff timer. */ |
---|
341 | ic->acc->auto_reconnect_delay = 0; |
---|
342 | |
---|
343 | if (ic->bee->ui->imc_connected) { |
---|
344 | ic->bee->ui->imc_connected(ic); |
---|
345 | } |
---|
346 | } |
---|
347 | |
---|
348 | gboolean auto_reconnect(gpointer data, gint fd, b_input_condition cond) |
---|
349 | { |
---|
350 | account_t *a = data; |
---|
351 | |
---|
352 | a->reconnect = 0; |
---|
353 | account_on(a->bee, a); |
---|
354 | |
---|
355 | return(FALSE); /* Only have to run the timeout once */ |
---|
356 | } |
---|
357 | |
---|
358 | void cancel_auto_reconnect(account_t *a) |
---|
359 | { |
---|
360 | b_event_remove(a->reconnect); |
---|
361 | a->reconnect = 0; |
---|
362 | } |
---|
363 | |
---|
364 | void imc_logout(struct im_connection *ic, int allow_reconnect) |
---|
365 | { |
---|
366 | bee_t *bee = ic->bee; |
---|
367 | account_t *a; |
---|
368 | GSList *l; |
---|
369 | int delay; |
---|
370 | |
---|
371 | /* Nested calls might happen sometimes, this is probably the best |
---|
372 | place to catch them. */ |
---|
373 | if (ic->flags & OPT_LOGGING_OUT) { |
---|
374 | return; |
---|
375 | } else { |
---|
376 | ic->flags |= OPT_LOGGING_OUT; |
---|
377 | } |
---|
378 | |
---|
379 | if (ic->bee->ui->imc_disconnected) { |
---|
380 | ic->bee->ui->imc_disconnected(ic); |
---|
381 | } |
---|
382 | |
---|
383 | imcb_log(ic, "Signing off.."); |
---|
384 | |
---|
385 | /* TBH I don't remember anymore why I didn't just use ic->acc... */ |
---|
386 | for (a = bee->accounts; a; a = a->next) { |
---|
387 | if (a->ic == ic) { |
---|
388 | break; |
---|
389 | } |
---|
390 | } |
---|
391 | |
---|
392 | if (a && !allow_reconnect && !(ic->flags & OPT_LOGGED_IN) && |
---|
393 | set_getbool(&a->set, "oauth")) { |
---|
394 | /* If this account supports OAuth, we're not logged in yet and |
---|
395 | not allowed to retry, assume there were auth issues. Give a |
---|
396 | helpful message on what might be necessary to fix this. */ |
---|
397 | imcb_log(ic, "If you're having problems logging in, try re-requesting " |
---|
398 | "an OAuth token: account %s set password \"\"", a->tag); |
---|
399 | } |
---|
400 | |
---|
401 | for (l = bee->users; l; ) { |
---|
402 | bee_user_t *bu = l->data; |
---|
403 | GSList *next = l->next; |
---|
404 | |
---|
405 | if (bu->ic == ic) { |
---|
406 | bee_user_free(bee, bu); |
---|
407 | } |
---|
408 | |
---|
409 | l = next; |
---|
410 | } |
---|
411 | |
---|
412 | b_event_remove(ic->keepalive); |
---|
413 | ic->keepalive = 0; |
---|
414 | ic->acc->prpl->logout(ic); |
---|
415 | b_event_remove(ic->inpa); |
---|
416 | |
---|
417 | g_free(ic->away); |
---|
418 | ic->away = NULL; |
---|
419 | |
---|
420 | query_del_by_conn((irc_t *) ic->bee->ui_data, ic); |
---|
421 | |
---|
422 | /* Throw away groupchats owned by this account. Historically this was only |
---|
423 | ever done by IM modules which is a bug. But it gives them opportunity |
---|
424 | to clean up protocol-specific bits as well so keep it that way, just |
---|
425 | do another cleanup here as a fallback. Don't want to leave any dangling |
---|
426 | pointers! */ |
---|
427 | while (ic->groupchats) { |
---|
428 | imcb_chat_free(ic->groupchats->data); |
---|
429 | } |
---|
430 | |
---|
431 | if (!a) { |
---|
432 | /* Uhm... This is very sick. */ |
---|
433 | } else if (allow_reconnect && set_getbool(&bee->set, "auto_reconnect") && |
---|
434 | set_getbool(&a->set, "auto_reconnect") && |
---|
435 | (delay = account_reconnect_delay(a)) > 0) { |
---|
436 | imcb_log(ic, "Reconnecting in %d seconds..", delay); |
---|
437 | a->reconnect = b_timeout_add(delay * 1000, auto_reconnect, a); |
---|
438 | } |
---|
439 | |
---|
440 | imc_free(ic); |
---|
441 | } |
---|
442 | |
---|
443 | void imcb_ask(struct im_connection *ic, char *msg, void *data, |
---|
444 | query_callback doit, query_callback dont) |
---|
445 | { |
---|
446 | query_add((irc_t *) ic->bee->ui_data, ic, msg, doit, dont, g_free, data); |
---|
447 | } |
---|
448 | |
---|
449 | void imcb_ask_with_free(struct im_connection *ic, char *msg, void *data, |
---|
450 | query_callback doit, query_callback dont, query_callback myfree) |
---|
451 | { |
---|
452 | query_add((irc_t *) ic->bee->ui_data, ic, msg, doit, dont, myfree, data); |
---|
453 | } |
---|
454 | |
---|
455 | void imcb_add_buddy(struct im_connection *ic, const char *handle, const char *group) |
---|
456 | { |
---|
457 | bee_user_t *bu; |
---|
458 | bee_t *bee = ic->bee; |
---|
459 | bee_group_t *oldg; |
---|
460 | |
---|
461 | if (!(bu = bee_user_by_handle(bee, ic, handle))) { |
---|
462 | bu = bee_user_new(bee, ic, handle, 0); |
---|
463 | } |
---|
464 | |
---|
465 | oldg = bu->group; |
---|
466 | bu->group = bee_group_by_name(bee, group, TRUE); |
---|
467 | |
---|
468 | if (bee->ui->user_group && bu->group != oldg) { |
---|
469 | bee->ui->user_group(bee, bu); |
---|
470 | } |
---|
471 | } |
---|
472 | |
---|
473 | void imcb_rename_buddy(struct im_connection *ic, const char *handle, const char *fullname) |
---|
474 | { |
---|
475 | bee_t *bee = ic->bee; |
---|
476 | bee_user_t *bu = bee_user_by_handle(bee, ic, handle); |
---|
477 | |
---|
478 | if (!bu || !fullname) { |
---|
479 | return; |
---|
480 | } |
---|
481 | |
---|
482 | if (!bu->fullname || strcmp(bu->fullname, fullname) != 0) { |
---|
483 | g_free(bu->fullname); |
---|
484 | bu->fullname = g_strdup(fullname); |
---|
485 | |
---|
486 | if (bee->ui->user_fullname) { |
---|
487 | bee->ui->user_fullname(bee, bu); |
---|
488 | } |
---|
489 | } |
---|
490 | } |
---|
491 | |
---|
492 | void imcb_remove_buddy(struct im_connection *ic, const char *handle, char *group) |
---|
493 | { |
---|
494 | bee_user_free(ic->bee, bee_user_by_handle(ic->bee, ic, handle)); |
---|
495 | } |
---|
496 | |
---|
497 | /* Mainly meant for ICQ (and now also for Jabber conferences) to allow IM |
---|
498 | modules to suggest a nickname for a handle. */ |
---|
499 | void imcb_buddy_nick_hint(struct im_connection *ic, const char *handle, const char *nick) |
---|
500 | { |
---|
501 | bee_t *bee = ic->bee; |
---|
502 | bee_user_t *bu = bee_user_by_handle(bee, ic, handle); |
---|
503 | |
---|
504 | if (!bu || !nick) { |
---|
505 | return; |
---|
506 | } |
---|
507 | |
---|
508 | g_free(bu->nick); |
---|
509 | bu->nick = g_strdup(nick); |
---|
510 | |
---|
511 | if (bee->ui->user_nick_hint) { |
---|
512 | bee->ui->user_nick_hint(bee, bu, nick); |
---|
513 | } |
---|
514 | } |
---|
515 | |
---|
516 | /* Returns the local contacts for an IM account (based on assigned nicks). |
---|
517 | Linked list should be freed, the strings themselves not! So look at it |
---|
518 | like a GSList<const char*> I guess? Empty list means NULL retval (as |
---|
519 | always with GSList). */ |
---|
520 | GSList *imcb_get_local_contacts(struct im_connection *ic) |
---|
521 | { |
---|
522 | GHashTableIter nicks; |
---|
523 | GSList *ret = NULL; |
---|
524 | |
---|
525 | if (!(ic->acc->flags & ACC_FLAG_LOCAL_CONTACTS)) { |
---|
526 | /* Only allow protocols that indicate local contact list |
---|
527 | support to use this function. */ |
---|
528 | return ret; |
---|
529 | } |
---|
530 | |
---|
531 | g_hash_table_iter_init(&nicks, ic->acc->nicks); |
---|
532 | gpointer handle; |
---|
533 | while (g_hash_table_iter_next(&nicks, &handle, NULL)) { |
---|
534 | ret = g_slist_prepend(ret, (char *) handle); |
---|
535 | } |
---|
536 | |
---|
537 | /* If the protocol asked for the list, assume we won't have to send it |
---|
538 | anymore in imcb_connected(). */ |
---|
539 | ic->flags |= OPT_LOCAL_CONTACTS_SENT; |
---|
540 | |
---|
541 | return ret; |
---|
542 | } |
---|
543 | |
---|
544 | |
---|
545 | struct imcb_ask_cb_data { |
---|
546 | struct im_connection *ic; |
---|
547 | char *handle; |
---|
548 | }; |
---|
549 | |
---|
550 | static void imcb_ask_cb_free(void *data) |
---|
551 | { |
---|
552 | struct imcb_ask_cb_data *cbd = data; |
---|
553 | |
---|
554 | g_free(cbd->handle); |
---|
555 | g_free(cbd); |
---|
556 | } |
---|
557 | |
---|
558 | static void imcb_ask_auth_cb_no(void *data) |
---|
559 | { |
---|
560 | struct imcb_ask_cb_data *cbd = data; |
---|
561 | |
---|
562 | cbd->ic->acc->prpl->auth_deny(cbd->ic, cbd->handle); |
---|
563 | |
---|
564 | imcb_ask_cb_free(cbd); |
---|
565 | } |
---|
566 | |
---|
567 | static void imcb_ask_auth_cb_yes(void *data) |
---|
568 | { |
---|
569 | struct imcb_ask_cb_data *cbd = data; |
---|
570 | |
---|
571 | cbd->ic->acc->prpl->auth_allow(cbd->ic, cbd->handle); |
---|
572 | |
---|
573 | imcb_ask_cb_free(cbd); |
---|
574 | } |
---|
575 | |
---|
576 | void imcb_ask_auth(struct im_connection *ic, const char *handle, const char *realname) |
---|
577 | { |
---|
578 | struct imcb_ask_cb_data *data = g_new0(struct imcb_ask_cb_data, 1); |
---|
579 | char *s, *realname_ = NULL; |
---|
580 | |
---|
581 | if (realname != NULL) { |
---|
582 | realname_ = g_strdup_printf(" (%s)", realname); |
---|
583 | } |
---|
584 | |
---|
585 | s = g_strdup_printf("The user %s%s wants to add you to his/her buddy list.", |
---|
586 | handle, realname_ ? realname_ : ""); |
---|
587 | |
---|
588 | g_free(realname_); |
---|
589 | |
---|
590 | data->ic = ic; |
---|
591 | data->handle = g_strdup(handle); |
---|
592 | query_add((irc_t *) ic->bee->ui_data, ic, s, |
---|
593 | imcb_ask_auth_cb_yes, imcb_ask_auth_cb_no, imcb_ask_cb_free, data); |
---|
594 | |
---|
595 | g_free(s); |
---|
596 | } |
---|
597 | |
---|
598 | static void imcb_ask_add_cb_yes(void *data) |
---|
599 | { |
---|
600 | struct imcb_ask_cb_data *cbd = data; |
---|
601 | |
---|
602 | if (cbd->ic->acc->prpl->add_buddy) { |
---|
603 | cbd->ic->acc->prpl->add_buddy(cbd->ic, cbd->handle, NULL); |
---|
604 | } |
---|
605 | |
---|
606 | imcb_ask_cb_free(data); |
---|
607 | } |
---|
608 | |
---|
609 | void imcb_ask_add(struct im_connection *ic, const char *handle, const char *realname) |
---|
610 | { |
---|
611 | struct imcb_ask_cb_data *data; |
---|
612 | char *s; |
---|
613 | |
---|
614 | /* TODO: Make a setting for this! */ |
---|
615 | if (bee_user_by_handle(ic->bee, ic, handle) != NULL) { |
---|
616 | return; |
---|
617 | } |
---|
618 | |
---|
619 | data = g_new0(struct imcb_ask_cb_data, 1); |
---|
620 | |
---|
621 | s = g_strdup_printf("The user %s is not in your buddy list yet. Do you want to add him/her now?", handle); |
---|
622 | |
---|
623 | data->ic = ic; |
---|
624 | data->handle = g_strdup(handle); |
---|
625 | query_add((irc_t *) ic->bee->ui_data, ic, s, |
---|
626 | imcb_ask_add_cb_yes, imcb_ask_cb_free, imcb_ask_cb_free, data); |
---|
627 | |
---|
628 | g_free(s); |
---|
629 | } |
---|
630 | |
---|
631 | struct bee_user *imcb_buddy_by_handle(struct im_connection *ic, const char *handle) |
---|
632 | { |
---|
633 | return bee_user_by_handle(ic->bee, ic, handle); |
---|
634 | } |
---|
635 | |
---|
636 | /* The plan is to not allow straight calls to prpl functions anymore, but do |
---|
637 | them all from some wrappers. We'll start to define some down here: */ |
---|
638 | |
---|
639 | int imc_chat_msg(struct groupchat *c, char *msg, int flags) |
---|
640 | { |
---|
641 | char *buf = NULL; |
---|
642 | |
---|
643 | if ((c->ic->flags & OPT_DOES_HTML) && (g_strncasecmp(msg, "<html>", 6) != 0)) { |
---|
644 | buf = escape_html(msg); |
---|
645 | msg = buf; |
---|
646 | } |
---|
647 | |
---|
648 | c->ic->acc->prpl->chat_msg(c, msg, flags); |
---|
649 | g_free(buf); |
---|
650 | |
---|
651 | return 1; |
---|
652 | } |
---|
653 | |
---|
654 | static char *imc_away_state_find(GList *gcm, char *away, char **message); |
---|
655 | |
---|
656 | int imc_away_send_update(struct im_connection *ic) |
---|
657 | { |
---|
658 | char *away, *msg = NULL; |
---|
659 | |
---|
660 | if (ic->acc->prpl->away_states == NULL || |
---|
661 | ic->acc->prpl->set_away == NULL) { |
---|
662 | return 0; |
---|
663 | } |
---|
664 | |
---|
665 | away = set_getstr(&ic->acc->set, "away") ? |
---|
666 | : set_getstr(&ic->bee->set, "away"); |
---|
667 | if (away && *away) { |
---|
668 | GList *m = ic->acc->prpl->away_states(ic); |
---|
669 | msg = ic->acc->flags & ACC_FLAG_AWAY_MESSAGE ? away : NULL; |
---|
670 | away = imc_away_state_find(m, away, &msg) ? : |
---|
671 | (imc_away_state_find(m, "away", &msg) ? : m->data); |
---|
672 | } else if (ic->acc->flags & ACC_FLAG_STATUS_MESSAGE) { |
---|
673 | away = NULL; |
---|
674 | msg = set_getstr(&ic->acc->set, "status") ? |
---|
675 | : set_getstr(&ic->bee->set, "status"); |
---|
676 | } |
---|
677 | |
---|
678 | ic->acc->prpl->set_away(ic, away, msg); |
---|
679 | |
---|
680 | return 1; |
---|
681 | } |
---|
682 | |
---|
683 | static char *imc_away_alias_list[8][5] = |
---|
684 | { |
---|
685 | { "Away from computer", "Away", "Extended away", NULL }, |
---|
686 | { "NA", "N/A", "Not available", NULL }, |
---|
687 | { "Busy", "Do not disturb", "DND", "Occupied", NULL }, |
---|
688 | { "Be right back", "BRB", NULL }, |
---|
689 | { "On the phone", "Phone", "On phone", NULL }, |
---|
690 | { "Out to lunch", "Lunch", "Food", NULL }, |
---|
691 | { "Invisible", "Hidden" }, |
---|
692 | { NULL } |
---|
693 | }; |
---|
694 | |
---|
695 | static char *imc_away_state_find(GList *gcm, char *away, char **message) |
---|
696 | { |
---|
697 | GList *m; |
---|
698 | int i, j; |
---|
699 | |
---|
700 | for (m = gcm; m; m = m->next) { |
---|
701 | if (g_strncasecmp(m->data, away, strlen(m->data)) == 0) { |
---|
702 | /* At least the Yahoo! module works better if message |
---|
703 | contains no data unless it adds something to what |
---|
704 | we have in state already. */ |
---|
705 | if (strlen(m->data) == strlen(away)) { |
---|
706 | *message = NULL; |
---|
707 | } |
---|
708 | |
---|
709 | return m->data; |
---|
710 | } |
---|
711 | } |
---|
712 | |
---|
713 | for (i = 0; *imc_away_alias_list[i]; i++) { |
---|
714 | int keep_message; |
---|
715 | |
---|
716 | for (j = 0; imc_away_alias_list[i][j]; j++) { |
---|
717 | if (g_strncasecmp(away, imc_away_alias_list[i][j], strlen(imc_away_alias_list[i][j])) == 0) { |
---|
718 | keep_message = strlen(away) != strlen(imc_away_alias_list[i][j]); |
---|
719 | break; |
---|
720 | } |
---|
721 | } |
---|
722 | |
---|
723 | if (!imc_away_alias_list[i][j]) { /* If we reach the end, this row */ |
---|
724 | continue; /* is not what we want. Next! */ |
---|
725 | |
---|
726 | } |
---|
727 | /* Now find an entry in this row which exists in gcm */ |
---|
728 | for (j = 0; imc_away_alias_list[i][j]; j++) { |
---|
729 | for (m = gcm; m; m = m->next) { |
---|
730 | if (g_strcasecmp(imc_away_alias_list[i][j], m->data) == 0) { |
---|
731 | if (!keep_message) { |
---|
732 | *message = NULL; |
---|
733 | } |
---|
734 | |
---|
735 | return imc_away_alias_list[i][j]; |
---|
736 | } |
---|
737 | } |
---|
738 | } |
---|
739 | |
---|
740 | /* No need to look further, apparently this state doesn't |
---|
741 | have any good alias for this protocol. */ |
---|
742 | break; |
---|
743 | } |
---|
744 | |
---|
745 | return NULL; |
---|
746 | } |
---|
747 | |
---|
748 | void imc_add_allow(struct im_connection *ic, char *handle) |
---|
749 | { |
---|
750 | if (g_slist_find_custom(ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp) == NULL) { |
---|
751 | ic->permit = g_slist_prepend(ic->permit, g_strdup(handle)); |
---|
752 | } |
---|
753 | |
---|
754 | ic->acc->prpl->add_permit(ic, handle); |
---|
755 | } |
---|
756 | |
---|
757 | void imc_rem_allow(struct im_connection *ic, char *handle) |
---|
758 | { |
---|
759 | GSList *l; |
---|
760 | |
---|
761 | if ((l = g_slist_find_custom(ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp))) { |
---|
762 | g_free(l->data); |
---|
763 | ic->permit = g_slist_delete_link(ic->permit, l); |
---|
764 | } |
---|
765 | |
---|
766 | ic->acc->prpl->rem_permit(ic, handle); |
---|
767 | } |
---|
768 | |
---|
769 | void imc_add_block(struct im_connection *ic, char *handle) |
---|
770 | { |
---|
771 | if (g_slist_find_custom(ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp) == NULL) { |
---|
772 | ic->deny = g_slist_prepend(ic->deny, g_strdup(handle)); |
---|
773 | } |
---|
774 | |
---|
775 | ic->acc->prpl->add_deny(ic, handle); |
---|
776 | } |
---|
777 | |
---|
778 | void imc_rem_block(struct im_connection *ic, char *handle) |
---|
779 | { |
---|
780 | GSList *l; |
---|
781 | |
---|
782 | if ((l = g_slist_find_custom(ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp))) { |
---|
783 | g_free(l->data); |
---|
784 | ic->deny = g_slist_delete_link(ic->deny, l); |
---|
785 | } |
---|
786 | |
---|
787 | ic->acc->prpl->rem_deny(ic, handle); |
---|
788 | } |
---|
789 | |
---|
790 | /* Deprecated: using this function resulted in merging several handles accidentally |
---|
791 | * Also the irc layer handles this decently nowadays */ |
---|
792 | void imcb_clean_handle(struct im_connection *ic, char *handle) |
---|
793 | { |
---|
794 | } |
---|