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