source: protocols/nogaim.c @ 3f44e43

Last change on this file since 3f44e43 was 3f44e43, checked in by dequis <dx@…>, at 2016-11-21T07:49:26Z

Merge branch 'master' into parson

  • Property mode set to 100644
File size: 22.2 KB
Line 
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
39GSList *connections;
40
41#ifdef WITH_PLUGINS
42GList *plugins = NULL;
43
44static gint pluginscmp(gconstpointer a, gconstpointer b, gpointer data)
45{
46        const struct plugin_info *ia = a;
47        const struct plugin_info *ib = b;
48
49        return g_strcasecmp(ia->name, ib->name);
50}
51
52gboolean load_plugin(char *path)
53{
54        GList *l;
55        struct plugin_info *i;
56        struct plugin_info *info;
57        struct plugin_info * (*info_function) (void) = NULL;
58        void (*init_function) (void);
59
60        GModule *mod = g_module_open(path, G_MODULE_BIND_LAZY);
61        gboolean loaded = FALSE;
62
63        if (!mod) {
64                log_message(LOGLVL_ERROR, "Error loading plugin `%s': %s\n", path, g_module_error());
65                return FALSE;
66        }
67
68        if (g_module_symbol(mod, "init_plugin_info", (gpointer *) &info_function)) {
69                info = info_function();
70
71                if (info->abiver != BITLBEE_ABI_VERSION_CODE) {
72                        log_message(LOGLVL_ERROR,
73                                    "`%s' uses ABI %u but %u is required\n",
74                                    path, info->abiver,
75                                    BITLBEE_ABI_VERSION_CODE);
76                        g_module_close(mod);
77                        return FALSE;
78                }
79
80                if (!info->name || !info->version) {
81                        log_message(LOGLVL_ERROR,
82                                    "Name or version missing from the "
83                                    "plugin info in `%s'\n", path);
84                        g_module_close(mod);
85                        return FALSE;
86                }
87
88                for (l = plugins; l; l = l->next) {
89                        i = l->data;
90
91                        if (g_strcasecmp(i->name, info->name) == 0) {
92                                loaded = TRUE;
93                                break;
94                        }
95                }
96
97                if (loaded) {
98                        log_message(LOGLVL_WARNING,
99                                    "%s plugin already loaded\n",
100                                    info->name);
101                        g_module_close(mod);
102                        return FALSE;
103                }
104        } else {
105                log_message(LOGLVL_WARNING, "Can't find function `init_plugin_info' in `%s'\n", path);
106        }
107
108        if (!g_module_symbol(mod, "init_plugin", (gpointer *) &init_function)) {
109                log_message(LOGLVL_WARNING, "Can't find function `init_plugin' in `%s'\n", path);
110                g_module_close(mod);
111                return FALSE;
112        }
113
114        if (info_function) {
115                plugins = g_list_insert_sorted_with_data(plugins, info,
116                                                         pluginscmp, NULL);
117        }
118
119        init_function();
120        return TRUE;
121}
122
123void load_plugins(void)
124{
125        GDir *dir;
126        GError *error = NULL;
127
128        dir = g_dir_open(global.conf->plugindir, 0, &error);
129
130        if (dir) {
131                const gchar *entry;
132                char *path;
133
134                while ((entry = g_dir_read_name(dir))) {
135                        if (!g_str_has_suffix(entry, "." G_MODULE_SUFFIX)) {
136                                continue;
137                        }
138
139                        path = g_build_filename(global.conf->plugindir, entry, NULL);
140                        if (!path) {
141                                log_message(LOGLVL_WARNING, "Can't build path for %s\n", entry);
142                                continue;
143                        }
144
145                        load_plugin(path);
146
147                        g_free(path);
148                }
149
150                g_dir_close(dir);
151        }
152}
153
154GList *get_plugins()
155{
156        return plugins;
157}
158#endif
159
160GList *protocols = NULL;
161GList *disabled_protocols = NULL;
162
163void register_protocol(struct prpl *p)
164{
165        int i;
166        gboolean refused = global.conf->protocols != NULL;
167
168        for (i = 0; global.conf->protocols && global.conf->protocols[i]; i++) {
169                if (g_strcasecmp(p->name, global.conf->protocols[i]) == 0) {
170                        refused = FALSE;
171                }
172        }
173
174        if (refused) {
175                disabled_protocols = g_list_append(disabled_protocols, p);
176        } else {
177                protocols = g_list_append(protocols, p);
178        }
179}
180
181static int proto_name_cmp(const void *proto_, const void *name)
182{
183        const struct prpl *proto = proto_;
184        return g_strcasecmp(proto->name, name);
185}
186
187struct prpl *find_protocol(const char *name)
188{
189        GList *gl = g_list_find_custom(protocols, name, proto_name_cmp);
190        return gl ? gl->data: NULL;
191}
192
193gboolean is_protocol_disabled(const char *name)
194{
195        return g_list_find_custom(disabled_protocols, name, proto_name_cmp) != NULL;
196}
197
198/* Returns heap allocated string with text attempting to explain why a protocol is missing
199 * Free the return value with g_free() */
200char *explain_unknown_protocol(const char *name)
201{
202        char *extramsg = NULL;
203
204        if (is_protocol_disabled(name)) {
205                return g_strdup("Protocol disabled in the global config (bitlbee.conf)");
206        }
207
208        if (strcmp(name, "yahoo") == 0) {
209                return g_strdup("The old yahoo protocol is gone, try the funyahoo++ libpurple plugin instead.");
210        }
211
212#ifdef WITH_PURPLE
213        if ((strcmp(name, "msn") == 0) ||
214            (strcmp(name, "loubserp-mxit") == 0) ||
215            (strcmp(name, "myspace") == 0)) {
216                return g_strdup("This protocol has been removed from your libpurple version.");
217        }
218
219        if (strcmp(name, "hipchat") == 0) {
220                return g_strdup("This account type isn't supported by libpurple's jabber.");
221        }
222
223#else
224        if (strcmp(name, "aim") == 0 || strcmp(name, "icq") == 0) {
225                return g_strdup("This account uses libpurple specific aliases for oscar. "
226                                "Re-add the account with `account add oscar ...'");
227        }
228
229        extramsg = "If this is a libpurple plugin, you might need to install bitlbee-libpurple instead.";
230#endif
231        return g_strconcat("The protocol plugin is not installed or could not be loaded. "
232                           "Use the `plugins' command to list available protocols. ",
233                           extramsg, NULL);
234}
235
236void nogaim_init()
237{
238        extern void msn_initmodule();
239        extern void oscar_initmodule();
240        extern void jabber_initmodule();
241        extern void twitter_initmodule();
242        extern void purple_initmodule();
243        extern void rpc_initmodule();
244
245#ifdef WITH_MSN
246        msn_initmodule();
247#endif
248
249#ifdef WITH_OSCAR
250        oscar_initmodule();
251#endif
252
253#ifdef WITH_JABBER
254        jabber_initmodule();
255#endif
256
257#ifdef WITH_TWITTER
258        twitter_initmodule();
259#endif
260
261#ifdef WITH_PURPLE
262        purple_initmodule();
263#endif
264
265#ifdef WITH_RPC
266        rpc_initmodule();
267#endif
268
269#ifdef WITH_PLUGINS
270        load_plugins();
271#endif
272}
273
274GList *get_protocols()
275{
276        return protocols;
277}
278
279GList *get_protocols_disabled()
280{
281        return disabled_protocols;
282}
283
284GSList *get_connections()
285{
286        return connections;
287}
288
289struct im_connection *imcb_new(account_t *acc)
290{
291        struct im_connection *ic;
292
293        ic = g_new0(struct im_connection, 1);
294
295        ic->bee = acc->bee;
296        ic->acc = acc;
297        acc->ic = ic;
298
299        connections = g_slist_append(connections, ic);
300
301        return(ic);
302}
303
304void imc_free(struct im_connection *ic)
305{
306        account_t *a;
307
308        /* Destroy the pointer to this connection from the account list */
309        for (a = ic->bee->accounts; a; a = a->next) {
310                if (a->ic == ic) {
311                        a->ic = NULL;
312                        break;
313                }
314        }
315
316        connections = g_slist_remove(connections, ic);
317        g_free(ic);
318}
319
320static void serv_got_crap(struct im_connection *ic, char *format, ...)
321{
322        va_list params;
323        char *text;
324        account_t *a;
325
326        if (!ic->bee->ui->log) {
327                return;
328        }
329
330        va_start(params, format);
331        text = g_strdup_vprintf(format, params);
332        va_end(params);
333
334        if ((g_strcasecmp(set_getstr(&ic->bee->set, "strip_html"), "always") == 0) ||
335            ((ic->flags & OPT_DOES_HTML) && set_getbool(&ic->bee->set, "strip_html"))) {
336                strip_html(text);
337        }
338
339        /* Try to find a different connection on the same protocol. */
340        for (a = ic->bee->accounts; a; a = a->next) {
341                if (a->prpl == ic->acc->prpl && a->ic != ic) {
342                        break;
343                }
344        }
345
346        /* If we found one, include the screenname in the message. */
347        if (a) {
348                ic->bee->ui->log(ic->bee, ic->acc->tag, text);
349        } else {
350                ic->bee->ui->log(ic->bee, ic->acc->prpl->name, text);
351        }
352
353        g_free(text);
354}
355
356void imcb_log(struct im_connection *ic, char *format, ...)
357{
358        va_list params;
359        char *text;
360
361        va_start(params, format);
362        text = g_strdup_vprintf(format, params);
363        va_end(params);
364
365        if (ic->flags & OPT_LOGGED_IN) {
366                serv_got_crap(ic, "%s", text);
367        } else {
368                serv_got_crap(ic, "Logging in: %s", text);
369        }
370
371        g_free(text);
372}
373
374void imcb_error(struct im_connection *ic, char *format, ...)
375{
376        va_list params;
377        char *text;
378
379        va_start(params, format);
380        text = g_strdup_vprintf(format, params);
381        va_end(params);
382
383        if (ic->flags & OPT_LOGGED_IN) {
384                serv_got_crap(ic, "Error: %s", text);
385        } else {
386                serv_got_crap(ic, "Login error: %s", text);
387        }
388
389        g_free(text);
390}
391
392static gboolean send_keepalive(gpointer d, gint fd, b_input_condition cond)
393{
394        struct im_connection *ic = d;
395
396        if ((ic->flags & OPT_PONGS) && !(ic->flags & OPT_PONGED)) {
397                /* This protocol is expected to ack keepalives and hasn't
398                   since the last time we were here. */
399                imcb_error(ic, "Connection timeout");
400                imc_logout(ic, TRUE);
401                return FALSE;
402        }
403        ic->flags &= ~OPT_PONGED;
404
405        if (ic->acc->prpl->keepalive) {
406                ic->acc->prpl->keepalive(ic);
407        }
408
409        return TRUE;
410}
411
412void start_keepalives(struct im_connection *ic, int interval)
413{
414        b_event_remove(ic->keepalive);
415        ic->keepalive = b_timeout_add(interval, send_keepalive, ic);
416
417        /* Connecting successfully counts as a first successful pong. */
418        if (ic->flags & OPT_PONGS) {
419                ic->flags |= OPT_PONGED;
420        }
421}
422
423void imcb_connected(struct im_connection *ic)
424{
425        /* MSN servers sometimes redirect you to a different server and do
426           the whole login sequence again, so these "late" calls to this
427           function should be handled correctly. (IOW, ignored) */
428        if (ic->flags & OPT_LOGGED_IN) {
429                return;
430        }
431
432        if ((ic->acc->flags & ACC_FLAG_LOCAL_CONTACTS) &&
433            !(ic->flags & OPT_LOCAL_CONTACTS_SENT) &&
434            ic->acc->prpl->add_buddy) {
435                GHashTableIter nicks;
436                gpointer handle;
437                g_hash_table_iter_init(&nicks, ic->acc->nicks);
438                while (g_hash_table_iter_next(&nicks, &handle, NULL)) {
439                        ic->acc->prpl->add_buddy(ic, (char *) handle, NULL);
440                }
441        }
442
443        imcb_log(ic, "Logged in");
444
445        ic->flags |= OPT_LOGGED_IN;
446        start_keepalives(ic, 60000);
447
448        /* Necessary to send initial presence status, even if we're not away. */
449        imc_away_send_update(ic);
450
451        /* Apparently we're connected successfully, so reset the
452           exponential backoff timer. */
453        ic->acc->auto_reconnect_delay = 0;
454
455        if (ic->bee->ui->imc_connected) {
456                ic->bee->ui->imc_connected(ic);
457        }
458}
459
460gboolean auto_reconnect(gpointer data, gint fd, b_input_condition cond)
461{
462        account_t *a = data;
463
464        a->reconnect = 0;
465        account_on(a->bee, a);
466
467        return(FALSE);          /* Only have to run the timeout once */
468}
469
470void cancel_auto_reconnect(account_t *a)
471{
472        b_event_remove(a->reconnect);
473        a->reconnect = 0;
474}
475
476void imc_logout(struct im_connection *ic, int allow_reconnect)
477{
478        bee_t *bee = ic->bee;
479        account_t *a;
480        GSList *l;
481        int delay;
482
483        /* Nested calls might happen sometimes, this is probably the best
484           place to catch them. */
485        if (ic->flags & OPT_LOGGING_OUT) {
486                return;
487        } else {
488                ic->flags |= OPT_LOGGING_OUT;
489        }
490
491        if (ic->bee->ui->imc_disconnected) {
492                ic->bee->ui->imc_disconnected(ic);
493        }
494
495        imcb_log(ic, "Signing off..");
496
497        /* TBH I don't remember anymore why I didn't just use ic->acc... */
498        for (a = bee->accounts; a; a = a->next) {
499                if (a->ic == ic) {
500                        break;
501                }
502        }
503
504        if (a && !allow_reconnect && !(ic->flags & OPT_LOGGED_IN) &&
505            set_getbool(&a->set, "oauth")) {
506                /* If this account supports OAuth, we're not logged in yet and
507                   not allowed to retry, assume there were auth issues. Give a
508                   helpful message on what might be necessary to fix this. */
509                imcb_log(ic, "If you're having problems logging in, try re-requesting "
510                         "an OAuth token: account %s set password \"\"", a->tag);
511        }
512
513        for (l = bee->users; l; ) {
514                bee_user_t *bu = l->data;
515                GSList *next = l->next;
516
517                if (bu->ic == ic) {
518                        bee_user_free(bee, bu);
519                }
520
521                l = next;
522        }
523
524        b_event_remove(ic->keepalive);
525        ic->keepalive = 0;
526        ic->acc->prpl->logout(ic);
527        b_event_remove(ic->inpa);
528
529        g_free(ic->away);
530        ic->away = NULL;
531
532        query_del_by_conn((irc_t *) ic->bee->ui_data, ic);
533
534        /* Throw away groupchats owned by this account. Historically this was only
535           ever done by IM modules which is a bug. But it gives them opportunity
536           to clean up protocol-specific bits as well so keep it that way, just
537           do another cleanup here as a fallback. Don't want to leave any dangling
538           pointers! */
539        while (ic->groupchats) {
540                imcb_chat_free(ic->groupchats->data);
541        }
542
543        if (!a) {
544                /* Uhm... This is very sick. */
545        } else if (allow_reconnect && set_getbool(&bee->set, "auto_reconnect") &&
546                   set_getbool(&a->set, "auto_reconnect") &&
547                   (delay = account_reconnect_delay(a)) > 0) {
548                imcb_log(ic, "Reconnecting in %d seconds..", delay);
549                a->reconnect = b_timeout_add(delay * 1000, auto_reconnect, a);
550        }
551
552        imc_free(ic);
553}
554
555void imcb_ask(struct im_connection *ic, char *msg, void *data,
556              query_callback doit, query_callback dont)
557{
558        query_add((irc_t *) ic->bee->ui_data, ic, msg, doit, dont, g_free, data);
559}
560
561void imcb_ask_with_free(struct im_connection *ic, char *msg, void *data,
562                        query_callback doit, query_callback dont, query_callback myfree)
563{
564        query_add((irc_t *) ic->bee->ui_data, ic, msg, doit, dont, myfree, data);
565}
566
567void imcb_add_buddy(struct im_connection *ic, const char *handle, const char *group)
568{
569        bee_user_t *bu;
570        bee_t *bee = ic->bee;
571        bee_group_t *oldg;
572
573        if (!(bu = bee_user_by_handle(bee, ic, handle))) {
574                bu = bee_user_new(bee, ic, handle, 0);
575        }
576
577        oldg = bu->group;
578        bu->group = bee_group_by_name(bee, group, TRUE);
579
580        if (bee->ui->user_group && bu->group != oldg) {
581                bee->ui->user_group(bee, bu);
582        }
583}
584
585void imcb_rename_buddy(struct im_connection *ic, const char *handle, const char *fullname)
586{
587        bee_t *bee = ic->bee;
588        bee_user_t *bu = bee_user_by_handle(bee, ic, handle);
589
590        if (!bu || !fullname) {
591                return;
592        }
593
594        if (!bu->fullname || strcmp(bu->fullname, fullname) != 0) {
595                g_free(bu->fullname);
596                bu->fullname = g_strdup(fullname);
597
598                if (bee->ui->user_fullname) {
599                        bee->ui->user_fullname(bee, bu);
600                }
601        }
602}
603
604void imcb_remove_buddy(struct im_connection *ic, const char *handle, char *group)
605{
606        bee_user_free(ic->bee, bee_user_by_handle(ic->bee, ic, handle));
607}
608
609/* Implements either imcb_buddy_nick_hint() or imcb_buddy_nick_change() depending on the value of 'change' */
610static void buddy_nick_hint_or_change(struct im_connection *ic, const char *handle, const char *nick, gboolean change)
611{
612        bee_t *bee = ic->bee;
613        bee_user_t *bu = bee_user_by_handle(bee, ic, handle);
614
615        if (!bu || !nick) {
616                return;
617        }
618
619        g_free(bu->nick);
620        bu->nick = g_strdup(nick);
621
622        if (change && bee->ui->user_nick_change) {
623                bee->ui->user_nick_change(bee, bu, nick);
624        } else if (!change && bee->ui->user_nick_hint) {
625                bee->ui->user_nick_hint(bee, bu, nick);
626        }
627}
628
629/* Soft variant, for newly created users. Does nothing if it's already online */
630void imcb_buddy_nick_hint(struct im_connection *ic, const char *handle, const char *nick)
631{
632        buddy_nick_hint_or_change(ic, handle, nick, FALSE);
633}
634
635/* Hard variant, always changes the nick */
636void imcb_buddy_nick_change(struct im_connection *ic, const char *handle, const char *nick)
637{
638        buddy_nick_hint_or_change(ic, handle, nick, TRUE);
639}
640
641/* Returns the local contacts for an IM account (based on assigned nicks).
642   Linked list should be freed, the strings themselves not! So look at it
643   like a GSList<const char*> I guess? Empty list means NULL retval (as
644   always with GSList). */
645GSList *imcb_get_local_contacts(struct im_connection *ic)
646{
647        GHashTableIter nicks;
648        GSList *ret = NULL;
649       
650        if (!(ic->acc->flags & ACC_FLAG_LOCAL_CONTACTS)) {
651                /* Only allow protocols that indicate local contact list
652                   support to use this function. */
653                return ret;
654        }
655       
656        g_hash_table_iter_init(&nicks, ic->acc->nicks);
657        gpointer handle;
658        while (g_hash_table_iter_next(&nicks, &handle, NULL)) {
659                ret = g_slist_prepend(ret, (char *) handle);
660        }
661       
662        /* If the protocol asked for the list, assume we won't have to send it
663           anymore in imcb_connected(). */
664        ic->flags |= OPT_LOCAL_CONTACTS_SENT;
665       
666        return ret;
667}
668
669
670struct imcb_ask_cb_data {
671        struct im_connection *ic;
672        char *handle;
673};
674
675static void imcb_ask_cb_free(void *data)
676{
677        struct imcb_ask_cb_data *cbd = data;
678
679        g_free(cbd->handle);
680        g_free(cbd);
681}
682
683static void imcb_ask_auth_cb_no(void *data)
684{
685        struct imcb_ask_cb_data *cbd = data;
686
687        cbd->ic->acc->prpl->auth_deny(cbd->ic, cbd->handle);
688
689        imcb_ask_cb_free(cbd);
690}
691
692static void imcb_ask_auth_cb_yes(void *data)
693{
694        struct imcb_ask_cb_data *cbd = data;
695
696        cbd->ic->acc->prpl->auth_allow(cbd->ic, cbd->handle);
697
698        imcb_ask_cb_free(cbd);
699}
700
701void imcb_ask_auth(struct im_connection *ic, const char *handle, const char *realname)
702{
703        struct imcb_ask_cb_data *data = g_new0(struct imcb_ask_cb_data, 1);
704        char *s, *realname_ = NULL;
705
706        if (realname != NULL) {
707                realname_ = g_strdup_printf(" (%s)", realname);
708        }
709
710        s = g_strdup_printf("The user %s%s wants to add you to his/her buddy list.",
711                            handle, realname_ ? realname_ : "");
712
713        g_free(realname_);
714
715        data->ic = ic;
716        data->handle = g_strdup(handle);
717        query_add((irc_t *) ic->bee->ui_data, ic, s,
718                  imcb_ask_auth_cb_yes, imcb_ask_auth_cb_no, imcb_ask_cb_free, data);
719
720        g_free(s);
721}
722
723static void imcb_ask_add_cb_yes(void *data)
724{
725        struct imcb_ask_cb_data *cbd = data;
726
727        if (cbd->ic->acc->prpl->add_buddy) {
728                cbd->ic->acc->prpl->add_buddy(cbd->ic, cbd->handle, NULL);
729        }
730
731        imcb_ask_cb_free(data);
732}
733
734void imcb_ask_add(struct im_connection *ic, const char *handle, const char *realname)
735{
736        struct imcb_ask_cb_data *data;
737        char *s;
738
739        /* TODO: Make a setting for this! */
740        if (bee_user_by_handle(ic->bee, ic, handle) != NULL) {
741                return;
742        }
743
744        data = g_new0(struct imcb_ask_cb_data, 1);
745
746        s = g_strdup_printf("The user %s is not in your buddy list yet. Do you want to add him/her now?", handle);
747
748        data->ic = ic;
749        data->handle = g_strdup(handle);
750        query_add((irc_t *) ic->bee->ui_data, ic, s,
751                  imcb_ask_add_cb_yes, imcb_ask_cb_free, imcb_ask_cb_free, data);
752
753        g_free(s);
754}
755
756struct bee_user *imcb_buddy_by_handle(struct im_connection *ic, const char *handle)
757{
758        return bee_user_by_handle(ic->bee, ic, handle);
759}
760
761/* The plan is to not allow straight calls to prpl functions anymore, but do
762   them all from some wrappers. We'll start to define some down here: */
763
764int imc_chat_msg(struct groupchat *c, char *msg, int flags)
765{
766        char *buf = NULL;
767
768        if ((c->ic->flags & OPT_DOES_HTML) && (g_strncasecmp(msg, "<html>", 6) != 0)) {
769                buf = escape_html(msg);
770                msg = buf;
771        }
772
773        c->ic->acc->prpl->chat_msg(c, msg, flags);
774        g_free(buf);
775
776        return 1;
777}
778
779static char *imc_away_state_find(GList *gcm, char *away, char **message);
780
781int imc_away_send_update(struct im_connection *ic)
782{
783        char *away, *msg = NULL;
784
785        if (ic->acc->prpl->away_states == NULL ||
786            ic->acc->prpl->set_away == NULL) {
787                return 0;
788        }
789
790        away = set_getstr(&ic->acc->set, "away") ?
791               : set_getstr(&ic->bee->set, "away");
792        if (away && *away) {
793                GList *m = ic->acc->prpl->away_states(ic);
794                if (m == NULL) {
795                        return 0;
796                }
797                msg = ic->acc->flags & ACC_FLAG_AWAY_MESSAGE ? away : NULL;
798                away = imc_away_state_find(m, away, &msg) ? :
799                       (imc_away_state_find(m, "away", &msg) ? : m->data);
800        } else if (ic->acc->flags & ACC_FLAG_STATUS_MESSAGE) {
801                away = NULL;
802                msg = set_getstr(&ic->acc->set, "status") ?
803                      : set_getstr(&ic->bee->set, "status");
804        }
805
806        ic->acc->prpl->set_away(ic, away, msg);
807
808        return 1;
809}
810
811static char *imc_away_alias_list[8][5] =
812{
813        { "Away from computer", "Away", "Extended away", NULL },
814        { "NA", "N/A", "Not available", NULL },
815        { "Busy", "Do not disturb", "DND", "Occupied", NULL },
816        { "Be right back", "BRB", NULL },
817        { "On the phone", "Phone", "On phone", NULL },
818        { "Out to lunch", "Lunch", "Food", NULL },
819        { "Invisible", "Hidden" },
820        { NULL }
821};
822
823static char *imc_away_state_find(GList *gcm, char *away, char **message)
824{
825        GList *m;
826        int i, j;
827
828        for (m = gcm; m; m = m->next) {
829                if (g_strncasecmp(m->data, away, strlen(m->data)) == 0) {
830                        /* At least the Yahoo! module works better if message
831                           contains no data unless it adds something to what
832                           we have in state already. */
833                        if (strlen(m->data) == strlen(away)) {
834                                *message = NULL;
835                        }
836
837                        return m->data;
838                }
839        }
840
841        for (i = 0; *imc_away_alias_list[i]; i++) {
842                int keep_message;
843
844                for (j = 0; imc_away_alias_list[i][j]; j++) {
845                        if (g_strncasecmp(away, imc_away_alias_list[i][j], strlen(imc_away_alias_list[i][j])) == 0) {
846                                keep_message = strlen(away) != strlen(imc_away_alias_list[i][j]);
847                                break;
848                        }
849                }
850
851                if (!imc_away_alias_list[i][j]) {       /* If we reach the end, this row */
852                        continue;                       /* is not what we want. Next!    */
853
854                }
855                /* Now find an entry in this row which exists in gcm */
856                for (j = 0; imc_away_alias_list[i][j]; j++) {
857                        for (m = gcm; m; m = m->next) {
858                                if (g_strcasecmp(imc_away_alias_list[i][j], m->data) == 0) {
859                                        if (!keep_message) {
860                                                *message = NULL;
861                                        }
862
863                                        return imc_away_alias_list[i][j];
864                                }
865                        }
866                }
867
868                /* No need to look further, apparently this state doesn't
869                   have any good alias for this protocol. */
870                break;
871        }
872
873        return NULL;
874}
875
876void imc_add_allow(struct im_connection *ic, char *handle)
877{
878        if (g_slist_find_custom(ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp) == NULL) {
879                ic->permit = g_slist_prepend(ic->permit, g_strdup(handle));
880        }
881
882        ic->acc->prpl->add_permit(ic, handle);
883}
884
885void imc_rem_allow(struct im_connection *ic, char *handle)
886{
887        GSList *l;
888
889        if ((l = g_slist_find_custom(ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp))) {
890                g_free(l->data);
891                ic->permit = g_slist_delete_link(ic->permit, l);
892        }
893
894        ic->acc->prpl->rem_permit(ic, handle);
895}
896
897void imc_add_block(struct im_connection *ic, char *handle)
898{
899        if (g_slist_find_custom(ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp) == NULL) {
900                ic->deny = g_slist_prepend(ic->deny, g_strdup(handle));
901        }
902
903        ic->acc->prpl->add_deny(ic, handle);
904}
905
906void imc_rem_block(struct im_connection *ic, char *handle)
907{
908        GSList *l;
909
910        if ((l = g_slist_find_custom(ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp))) {
911                g_free(l->data);
912                ic->deny = g_slist_delete_link(ic->deny, l);
913        }
914
915        ic->acc->prpl->rem_deny(ic, handle);
916}
917
918/* Deprecated: using this function resulted in merging several handles accidentally
919 * Also the irc layer handles this decently nowadays */
920void imcb_clean_handle(struct im_connection *ic, char *handle)
921{
922}
Note: See TracBrowser for help on using the repository browser.