source: protocols/purple/purple.c @ 537d9b9

Last change on this file since 537d9b9 was 537d9b9, checked in by dequis <dx@…>, at 2016-11-20T08:40:36Z

Merge master up to commit '9f03c47' into parson

  • Property mode set to 100644
File size: 50.7 KB
RevLine 
[796da03]1/***************************************************************************\
2*                                                                           *
3*  BitlBee - An IRC to IM gateway                                           *
4*  libpurple module - Main file                                             *
5*                                                                           *
[0e788f5]6*  Copyright 2009-2012 Wilmer van der Gaast <wilmer@gaast.net>              *
[796da03]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
[b3117f2]24#include "bitlbee.h"
[d93c8beb]25#include "bpurple.h"
[e5d8d21]26#include "help.h"
[b3117f2]27
[0ac1a375]28#include <stdarg.h>
29
[796da03]30#include <glib.h>
31#include <purple.h>
32
33GSList *purple_connections;
34
[0ac1a375]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. */
[4aa0f6b]38static bee_t *local_bee;
[0ac1a375]39
[5ebff60]40static char *set_eval_display_name(set_t *set, char *value);
[f85e9d6]41
[6a48992]42void purple_request_input_callback(guint id, struct im_connection *ic,
43                                   const char *message, const char *who);
44
[ea90275]45void purple_transfer_cancel_all(struct im_connection *ic);
46
[6a48992]47/* purple_request_input specific stuff */
48typedef void (*ri_callback_t)(gpointer, const gchar *);
49
50struct request_input_data {
51        ri_callback_t data_callback;
52        void *user_data;
[2c5ab49]53        struct im_connection *ic;
54        char *buddy;
55        guint id;
[6a48992]56};
57
[01d56c0]58struct purple_roomlist_data {
59        GSList *chats;
60        gint topic;
61        gboolean initialized;
62};
63
64
[5ebff60]65struct im_connection *purple_ic_by_pa(PurpleAccount *pa)
[860ba6a]66{
67        GSList *i;
[d93c8beb]68        struct purple_data *pd;
[5ebff60]69
70        for (i = purple_connections; i; i = i->next) {
[d93c8beb]71                pd = ((struct im_connection *) i->data)->proto_data;
72                if (pd->account == pa) {
[860ba6a]73                        return i->data;
[5ebff60]74                }
75        }
76
[860ba6a]77        return NULL;
78}
79
[5ebff60]80static struct im_connection *purple_ic_by_gc(PurpleConnection *gc)
[7da726b]81{
[5ebff60]82        return purple_ic_by_pa(purple_connection_get_account(gc));
[7da726b]83}
84
[5ebff60]85static gboolean purple_menu_cmp(const char *a, const char *b)
[8ad5c34]86{
[5ebff60]87        while (*a && *b) {
88                while (*a == '_') {
89                        a++;
90                }
91                while (*b == '_') {
92                        b++;
93                }
94                if (g_ascii_tolower(*a) != g_ascii_tolower(*b)) {
[8ad5c34]95                        return FALSE;
[5ebff60]96                }
97
98                a++;
99                b++;
[8ad5c34]100        }
[5ebff60]101
102        return (*a == '\0' && *b == '\0');
[8ad5c34]103}
104
[50988d1]105static char *purple_get_account_prpl_id(account_t *acc)
106{
107        /* "oscar" is how non-purple bitlbee calls it,
108         * and it might be icq or aim, depending on the username */
109        if (g_strcmp0(acc->prpl->name, "oscar") == 0) {
110                return (g_ascii_isdigit(acc->user[0])) ? "prpl-icq" : "prpl-aim";
111        }
112
113        return acc->prpl->data;
114}
115
[5ebff60]116static void purple_init(account_t *acc)
[796da03]117{
[50988d1]118        char *prpl_id = purple_get_account_prpl_id(acc);
119        PurplePlugin *prpl = purple_plugins_find_with_id(prpl_id);
[0f7ee7e5]120        PurplePluginProtocolInfo *pi = prpl->info->extra_info;
[52cae01]121        PurpleAccount *pa;
122        GList *i, *st;
[bab1c86]123        set_t *s;
[7c5affca]124        char help_title[64];
125        GString *help;
[3c9b095]126        static gboolean dir_fixed = FALSE;
[5ebff60]127
[3c9b095]128        /* Layer violation coming up: Making an exception for libpurple here.
129           Dig in the IRC state a bit to get a username. Ideally we should
130           check if s/he identified but this info doesn't seem *that* important.
131           It's just that fecking libpurple can't *not* store this shit.
[5ebff60]132
[3c9b095]133           Remember that libpurple is not really meant to be used on public
134           servers anyway! */
[5ebff60]135        if (!dir_fixed) {
[e4c3041]136                PurpleCertificatePool *pool;
[3c9b095]137                irc_t *irc = acc->bee->ui_data;
138                char *dir;
[5ebff60]139
140                dir = g_strdup_printf("%s/purple/%s", global.conf->configdir, irc->user->nick);
141                purple_util_set_user_dir(dir);
142                g_free(dir);
143
[3c9b095]144                purple_blist_load();
145                purple_prefs_load();
[12f041d]146
147                if (proxytype == PROXY_SOCKS4A) {
148                        /* do this here after loading prefs. yes, i know, it sucks */
149                        purple_prefs_set_bool("/purple/proxy/socks4_remotedns", TRUE);
150                }
151
[e4c3041]152                /* re-create the certificate cache directory */
153                pool = purple_certificate_find_pool("x509", "tls_peers");
154                dir = purple_certificate_pool_mkpath(pool, NULL);
155                purple_build_dir(dir, 0700);
[1ec454c]156                g_free(dir);
[e4c3041]157
[3c9b095]158                dir_fixed = TRUE;
159        }
[5ebff60]160
161        help = g_string_new("");
162        g_string_printf(help, "BitlBee libpurple module %s (%s).\n\nSupported settings:",
163                        (char *) acc->prpl->name, prpl->info->name);
164
165        if (pi->user_splits) {
[eb4c81a]166                GList *l;
[5ebff60]167                g_string_append_printf(help, "\n* username: Username");
168                for (l = pi->user_splits; l; l = l->next) {
169                        g_string_append_printf(help, "%c%s",
170                                               purple_account_user_split_get_separator(l->data),
171                                               purple_account_user_split_get_text(l->data));
172                }
[eb4c81a]173        }
[5ebff60]174
[52cae01]175        /* Convert all protocol_options into per-account setting variables. */
[5ebff60]176        for (i = pi->protocol_options; i; i = i->next) {
[0f7ee7e5]177                PurpleAccountOption *o = i->data;
178                const char *name;
179                char *def = NULL;
180                set_eval eval = NULL;
[4dc6b8d]181                void *eval_data = NULL;
182                GList *io = NULL;
183                GSList *opts = NULL;
[5ebff60]184
185                name = purple_account_option_get_setting(o);
186
187                switch (purple_account_option_get_type(o)) {
[0f7ee7e5]188                case PURPLE_PREF_STRING:
[5ebff60]189                        def = g_strdup(purple_account_option_get_default_string(o));
190
191                        g_string_append_printf(help, "\n* %s (%s), %s, default: %s",
192                                               name, purple_account_option_get_text(o),
193                                               "string", def);
194
[0f7ee7e5]195                        break;
[5ebff60]196
[0f7ee7e5]197                case PURPLE_PREF_INT:
[5ebff60]198                        def = g_strdup_printf("%d", purple_account_option_get_default_int(o));
[0f7ee7e5]199                        eval = set_eval_int;
[5ebff60]200
201                        g_string_append_printf(help, "\n* %s (%s), %s, default: %s",
202                                               name, purple_account_option_get_text(o),
203                                               "integer", def);
204
[0f7ee7e5]205                        break;
[5ebff60]206
[0f7ee7e5]207                case PURPLE_PREF_BOOLEAN:
[5ebff60]208                        if (purple_account_option_get_default_bool(o)) {
209                                def = g_strdup("true");
210                        } else {
211                                def = g_strdup("false");
212                        }
[0f7ee7e5]213                        eval = set_eval_bool;
[5ebff60]214
215                        g_string_append_printf(help, "\n* %s (%s), %s, default: %s",
216                                               name, purple_account_option_get_text(o),
217                                               "boolean", def);
218
[0f7ee7e5]219                        break;
[5ebff60]220
[4dc6b8d]221                case PURPLE_PREF_STRING_LIST:
[5ebff60]222                        def = g_strdup(purple_account_option_get_default_list_value(o));
223
224                        g_string_append_printf(help, "\n* %s (%s), %s, default: %s",
225                                               name, purple_account_option_get_text(o),
226                                               "list", def);
227                        g_string_append(help, "\n  Possible values: ");
228
229                        for (io = purple_account_option_get_list(o); io; io = io->next) {
[4dc6b8d]230                                PurpleKeyValuePair *kv = io->data;
[5ebff60]231                                opts = g_slist_append(opts, kv->value);
[c96c72f]232                                /* TODO: kv->value is not a char*, WTF? */
[5ebff60]233                                if (strcmp(kv->value, kv->key) != 0) {
234                                        g_string_append_printf(help, "%s (%s), ", (char *) kv->value, kv->key);
235                                } else {
236                                        g_string_append_printf(help, "%s, ", (char *) kv->value);
237                                }
[4dc6b8d]238                        }
[5ebff60]239                        g_string_truncate(help, help->len - 2);
[4dc6b8d]240                        eval = set_eval_list;
241                        eval_data = opts;
[5ebff60]242
[4dc6b8d]243                        break;
[5ebff60]244
[0f7ee7e5]245                default:
[4aa0f6b]246                        /** No way to talk to the user right now, invent one when
247                        this becomes important.
[e67e513]248                        irc_rootmsg( acc->irc, "Setting with unknown type: %s (%d) Expect stuff to break..\n",
[4dc6b8d]249                                     name, purple_account_option_get_type( o ) );
[4aa0f6b]250                        */
[5ebff60]251                        g_string_append_printf(help, "\n* [%s] UNSUPPORTED (type %d)",
252                                               name, purple_account_option_get_type(o));
[b3117f2]253                        name = NULL;
[0f7ee7e5]254                }
[5ebff60]255
256                if (name != NULL) {
257                        s = set_add(&acc->set, name, def, eval, acc);
[0f7ee7e5]258                        s->flags |= ACC_SET_OFFLINE_ONLY;
[4dc6b8d]259                        s->eval_data = eval_data;
[5ebff60]260                        g_free(def);
[0f7ee7e5]261                }
262        }
[5ebff60]263
264        g_snprintf(help_title, sizeof(help_title), "purple %s", (char *) acc->prpl->name);
265        help_add_mem(&global.help, help_title, help->str);
266        g_string_free(help, TRUE);
267
268        s = set_add(&acc->set, "display_name", NULL, set_eval_display_name, acc);
[f85e9d6]269        s->flags |= ACC_SET_ONLINE_ONLY;
[5ebff60]270
271        if (pi->options & OPT_PROTO_MAIL_CHECK) {
272                s = set_add(&acc->set, "mail_notifications", "false", set_eval_bool, acc);
[bab1c86]273                s->flags |= ACC_SET_OFFLINE_ONLY;
[dd43c62]274
[b38f655]275                s = set_add(&acc->set, "mail_notifications_handle", NULL, NULL, acc);
[dd43c62]276                s->flags |= ACC_SET_OFFLINE_ONLY | SET_NULL_OK;
[bab1c86]277        }
[5ebff60]278
279        if (strcmp(prpl->info->name, "Gadu-Gadu") == 0) {
280                s = set_add(&acc->set, "gg_sync_contacts", "true", set_eval_bool, acc);
281        }
282
[52cae01]283        /* Go through all away states to figure out if away/status messages
284           are possible. */
[50988d1]285        pa = purple_account_new(acc->user, prpl_id);
[5ebff60]286        for (st = purple_account_get_status_types(pa); st; st = st->next) {
287                PurpleStatusPrimitive prim = purple_status_type_get_primitive(st->data);
288
289                if (prim == PURPLE_STATUS_AVAILABLE) {
290                        if (purple_status_type_get_attr(st->data, "message")) {
[52cae01]291                                acc->flags |= ACC_FLAG_STATUS_MESSAGE;
[5ebff60]292                        }
293                } else if (prim != PURPLE_STATUS_OFFLINE) {
294                        if (purple_status_type_get_attr(st->data, "message")) {
[52cae01]295                                acc->flags |= ACC_FLAG_AWAY_MESSAGE;
[5ebff60]296                        }
[52cae01]297                }
298        }
[5ebff60]299        purple_accounts_remove(pa);
[93e0901]300       
301        /* Last, some protocols want their contact lists locally. */
302        if (strcmp(acc->prpl->name, "whatsapp") == 0 || strcmp(acc->prpl->name, "gg") == 0) {
303                acc->flags |= ACC_FLAG_LOCAL_CONTACTS;
304        }
[796da03]305}
306
[5ebff60]307static void purple_sync_settings(account_t *acc, PurpleAccount *pa)
[b74b287]308{
[5ebff60]309        PurplePlugin *prpl = purple_plugins_find_with_id(pa->protocol_id);
[b74b287]310        PurplePluginProtocolInfo *pi = prpl->info->extra_info;
311        GList *i;
[5ebff60]312
313        for (i = pi->protocol_options; i; i = i->next) {
[b74b287]314                PurpleAccountOption *o = i->data;
315                const char *name;
316                set_t *s;
[5ebff60]317
318                name = purple_account_option_get_setting(o);
319                s = set_find(&acc->set, name);
320                if (s->value == NULL) {
[b74b287]321                        continue;
[5ebff60]322                }
323
324                switch (purple_account_option_get_type(o)) {
[b74b287]325                case PURPLE_PREF_STRING:
[4dc6b8d]326                case PURPLE_PREF_STRING_LIST:
[5ebff60]327                        purple_account_set_string(pa, name, set_getstr(&acc->set, name));
[b74b287]328                        break;
[5ebff60]329
[b74b287]330                case PURPLE_PREF_INT:
[5ebff60]331                        purple_account_set_int(pa, name, set_getint(&acc->set, name));
[b74b287]332                        break;
[5ebff60]333
[b74b287]334                case PURPLE_PREF_BOOLEAN:
[5ebff60]335                        purple_account_set_bool(pa, name, set_getbool(&acc->set, name));
[b74b287]336                        break;
[5ebff60]337
[b74b287]338                default:
339                        break;
340                }
341        }
[5ebff60]342
343        if (pi->options & OPT_PROTO_MAIL_CHECK) {
344                purple_account_set_check_mail(pa, set_getbool(&acc->set, "mail_notifications"));
345        }
[b74b287]346}
347
[5ebff60]348static void purple_login(account_t *acc)
[796da03]349{
[5ebff60]350        struct im_connection *ic = imcb_new(acc);
[d93c8beb]351        struct purple_data *pd;
[5ebff60]352
353        if ((local_bee != NULL && local_bee != acc->bee) ||
354            (global.conf->runmode == RUNMODE_DAEMON && !getenv("BITLBEE_DEBUG"))) {
355                imcb_error(ic,  "Daemon mode detected. Do *not* try to use libpurple in daemon mode! "
356                           "Please use inetd or ForkDaemon mode instead.");
357                imc_logout(ic, FALSE);
[6967d01]358                return;
359        }
[4aa0f6b]360        local_bee = acc->bee;
[5ebff60]361
[796da03]362        /* For now this is needed in the _connected() handlers if using
363           GLib event handling, to make sure we're not handling events
364           on dead connections. */
[5ebff60]365        purple_connections = g_slist_prepend(purple_connections, ic);
366
[d93c8beb]367        ic->proto_data = pd = g_new0(struct purple_data, 1);
[50988d1]368        pd->account = purple_account_new(acc->user, purple_get_account_prpl_id(acc));
[6a48992]369        pd->input_requests = g_hash_table_new_full(g_direct_hash, g_direct_equal,
370                                                   NULL, g_free);
371        pd->next_request_id = 0;
[d93c8beb]372        purple_account_set_password(pd->account, acc->pass);
373        purple_sync_settings(acc, pd->account);
[5ebff60]374
[d93c8beb]375        purple_account_set_enabled(pd->account, "BitlBee", TRUE);
[dd43c62]376
[b38f655]377        if (set_getbool(&acc->set, "mail_notifications") && set_getstr(&acc->set, "mail_notifications_handle")) {
378                imcb_add_buddy(ic, set_getstr(&acc->set, "mail_notifications_handle"), NULL);
[dd43c62]379        }
[796da03]380}
381
[5ebff60]382static void purple_logout(struct im_connection *ic)
[796da03]383{
[d93c8beb]384        struct purple_data *pd = ic->proto_data;
[5ebff60]385
[2dd23da]386        if (!pd) {
387                return;
388        }
389
[1ec454c]390        while (ic->groupchats) {
391                imcb_chat_free(ic->groupchats->data);
392        }
393
[ea90275]394        if (pd->filetransfers) {
395                purple_transfer_cancel_all(ic);
396        }
397
[d93c8beb]398        purple_account_set_enabled(pd->account, "BitlBee", FALSE);
[5ebff60]399        purple_connections = g_slist_remove(purple_connections, ic);
[d93c8beb]400        purple_accounts_remove(pd->account);
[6e991a9]401        imcb_chat_list_free(ic);
[fecdd71]402        g_free(pd->chat_list_server);
[6a48992]403        g_hash_table_destroy(pd->input_requests);
[d93c8beb]404        g_free(pd);
[796da03]405}
406
[5ebff60]407static int purple_buddy_msg(struct im_connection *ic, char *who, char *message, int flags)
[796da03]408{
[389f7be]409        PurpleConversation *conv;
[d93c8beb]410        struct purple_data *pd = ic->proto_data;
[5ebff60]411
[6a48992]412        if (!strncmp(who, PURPLE_REQUEST_HANDLE, sizeof(PURPLE_REQUEST_HANDLE) - 1)) {
413                guint request_id = atoi(who + sizeof(PURPLE_REQUEST_HANDLE));
414                purple_request_input_callback(request_id, ic, message, who);
415                return 1;
416        }
417
[5ebff60]418        if ((conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM,
[d93c8beb]419                                                          who, pd->account)) == NULL) {
[5ebff60]420                conv = purple_conversation_new(PURPLE_CONV_TYPE_IM,
[d93c8beb]421                                               pd->account, who);
[389f7be]422        }
[5ebff60]423
424        purple_conv_im_send(purple_conversation_get_im_data(conv), message);
425
[0cbef26]426        return 1;
[796da03]427}
428
[5ebff60]429static GList *purple_away_states(struct im_connection *ic)
[796da03]430{
[d93c8beb]431        struct purple_data *pd = ic->proto_data;
[ec5e57d]432        GList *st, *ret = NULL;
[5ebff60]433
[d93c8beb]434        for (st = purple_account_get_status_types(pd->account); st; st = st->next) {
[5ebff60]435                PurpleStatusPrimitive prim = purple_status_type_get_primitive(st->data);
436                if (prim != PURPLE_STATUS_AVAILABLE && prim != PURPLE_STATUS_OFFLINE) {
437                        ret = g_list_append(ret, (void *) purple_status_type_get_name(st->data));
438                }
[279607e]439        }
[5ebff60]440
[ec5e57d]441        return ret;
[796da03]442}
443
[5ebff60]444static void purple_set_away(struct im_connection *ic, char *state_txt, char *message)
[796da03]445{
[d93c8beb]446        struct purple_data *pd = ic->proto_data;
447        GList *status_types = purple_account_get_status_types(pd->account), *st;
[ec5e57d]448        PurpleStatusType *pst = NULL;
[279607e]449        GList *args = NULL;
[5ebff60]450
451        for (st = status_types; st; st = st->next) {
[ec5e57d]452                pst = st->data;
[5ebff60]453
454                if (state_txt == NULL &&
455                    purple_status_type_get_primitive(pst) == PURPLE_STATUS_AVAILABLE) {
[279607e]456                        break;
[5ebff60]457                }
[279607e]458
[5ebff60]459                if (state_txt != NULL &&
460                    g_strcasecmp(state_txt, purple_status_type_get_name(pst)) == 0) {
[ec5e57d]461                        break;
[5ebff60]462                }
[ec5e57d]463        }
[5ebff60]464
465        if (message && purple_status_type_get_attr(pst, "message")) {
466                args = g_list_append(args, "message");
467                args = g_list_append(args, message);
[279607e]468        }
469
[d93c8beb]470        purple_account_set_status_list(pd->account,
471                                       st ? purple_status_type_get_id(pst) : "away",
[5ebff60]472                                       TRUE, args);
473
474        g_list_free(args);
[796da03]475}
476
[5ebff60]477static char *set_eval_display_name(set_t *set, char *value)
[f85e9d6]478{
479        account_t *acc = set->data;
480        struct im_connection *ic = acc->ic;
[5ebff60]481
482        if (ic) {
483                imcb_log(ic, "Changing display_name not currently supported with libpurple!");
484        }
485
[f85e9d6]486        return NULL;
487}
488
[694be84]489/* Bad bad gadu-gadu, not saving buddy list by itself */
[5ebff60]490static void purple_gg_buddylist_export(PurpleConnection *gc)
[694be84]491{
[5ebff60]492        struct im_connection *ic = purple_ic_by_gc(gc);
493
494        if (set_getstr(&ic->acc->set, "gg_sync_contacts")) {
495                GList *actions = gc->prpl->info->actions(gc->prpl, gc);
[694be84]496                GList *p;
[5ebff60]497                for (p = g_list_first(actions); p; p = p->next) {
498                        if (((PurplePluginAction *) p->data) &&
499                            purple_menu_cmp(((PurplePluginAction *) p->data)->label,
500                                            "Upload buddylist to Server") == 0) {
[694be84]501                                PurplePluginAction action;
502                                action.plugin = gc->prpl;
503                                action.context = gc;
504                                action.user_data = NULL;
[5ebff60]505                                ((PurplePluginAction *) p->data)->callback(&action);
[694be84]506                                break;
507                        }
508                }
[5ebff60]509                g_list_free(actions);
[694be84]510        }
511}
512
[5ebff60]513static void purple_gg_buddylist_import(PurpleConnection *gc)
[694be84]514{
[5ebff60]515        struct im_connection *ic = purple_ic_by_gc(gc);
516
517        if (set_getstr(&ic->acc->set, "gg_sync_contacts")) {
518                GList *actions = gc->prpl->info->actions(gc->prpl, gc);
[694be84]519                GList *p;
[5ebff60]520                for (p = g_list_first(actions); p; p = p->next) {
521                        if (((PurplePluginAction *) p->data) &&
522                            purple_menu_cmp(((PurplePluginAction *) p->data)->label,
523                                            "Download buddylist from Server") == 0) {
[694be84]524                                PurplePluginAction action;
525                                action.plugin = gc->prpl;
526                                action.context = gc;
527                                action.user_data = NULL;
[5ebff60]528                                ((PurplePluginAction *) p->data)->callback(&action);
[694be84]529                                break;
530                        }
531                }
[5ebff60]532                g_list_free(actions);
[694be84]533        }
534}
535
[5ebff60]536static void purple_add_buddy(struct im_connection *ic, char *who, char *group)
[796da03]537{
[b3117f2]538        PurpleBuddy *pb;
[3e59c8d]539        PurpleGroup *pg = NULL;
[d93c8beb]540        struct purple_data *pd = ic->proto_data;
[5ebff60]541
542        if (group && !(pg = purple_find_group(group))) {
543                pg = purple_group_new(group);
544                purple_blist_add_group(pg, NULL);
[3e59c8d]545        }
[694be84]546
[d93c8beb]547        pb = purple_buddy_new(pd->account, who, NULL);
[5ebff60]548        purple_blist_add_buddy(pb, NULL, pg, NULL);
[d93c8beb]549        purple_account_add_buddy(pd->account, pb);
[5ebff60]550
[d93c8beb]551        purple_gg_buddylist_export(pd->account->gc);
[796da03]552}
553
[5ebff60]554static void purple_remove_buddy(struct im_connection *ic, char *who, char *group)
[796da03]555{
[b3117f2]556        PurpleBuddy *pb;
[d93c8beb]557        struct purple_data *pd = ic->proto_data;
[5ebff60]558
[d93c8beb]559        pb = purple_find_buddy(pd->account, who);
[5ebff60]560        if (pb != NULL) {
[a91550c]561                PurpleGroup *group;
[5ebff60]562
563                group = purple_buddy_get_group(pb);
[d93c8beb]564                purple_account_remove_buddy(pd->account, pb, group);
[5ebff60]565
566                purple_blist_remove_buddy(pb);
[b3117f2]567        }
[694be84]568
[d93c8beb]569        purple_gg_buddylist_export(pd->account->gc);
[796da03]570}
571
[5ebff60]572static void purple_add_permit(struct im_connection *ic, char *who)
[05a8932]573{
[d93c8beb]574        struct purple_data *pd = ic->proto_data;
[5ebff60]575
[d93c8beb]576        purple_privacy_permit_add(pd->account, who, FALSE);
[05a8932]577}
578
[5ebff60]579static void purple_add_deny(struct im_connection *ic, char *who)
[05a8932]580{
[d93c8beb]581        struct purple_data *pd = ic->proto_data;
[5ebff60]582
[d93c8beb]583        purple_privacy_deny_add(pd->account, who, FALSE);
[05a8932]584}
585
[5ebff60]586static void purple_rem_permit(struct im_connection *ic, char *who)
[05a8932]587{
[d93c8beb]588        struct purple_data *pd = ic->proto_data;
[5ebff60]589
[d93c8beb]590        purple_privacy_permit_remove(pd->account, who, FALSE);
[05a8932]591}
592
[5ebff60]593static void purple_rem_deny(struct im_connection *ic, char *who)
[05a8932]594{
[d93c8beb]595        struct purple_data *pd = ic->proto_data;
[5ebff60]596
[d93c8beb]597        purple_privacy_deny_remove(pd->account, who, FALSE);
[05a8932]598}
599
[5ebff60]600static void purple_get_info(struct im_connection *ic, char *who)
[e77c264]601{
[d93c8beb]602        struct purple_data *pd = ic->proto_data;
603
604        serv_get_info(purple_account_get_connection(pd->account), who);
[e77c264]605}
606
[5ebff60]607static void purple_keepalive(struct im_connection *ic)
[796da03]608{
609}
610
[5ebff60]611static int purple_send_typing(struct im_connection *ic, char *who, int flags)
[796da03]612{
[487f555]613        PurpleTypingState state = PURPLE_NOT_TYPING;
[d93c8beb]614        struct purple_data *pd = ic->proto_data;
[5ebff60]615
616        if (flags & OPT_TYPING) {
[487f555]617                state = PURPLE_TYPING;
[5ebff60]618        } else if (flags & OPT_THINKING) {
[487f555]619                state = PURPLE_TYPED;
[5ebff60]620        }
621
[d93c8beb]622        serv_send_typing(purple_account_get_connection(pd->account), who, state);
[5ebff60]623
[bad41f56]624        return 1;
[796da03]625}
626
[5ebff60]627static void purple_chat_msg(struct groupchat *gc, char *message, int flags)
[f485008]628{
629        PurpleConversation *pc = gc->data;
[5ebff60]630
631        purple_conv_chat_send(purple_conversation_get_chat_data(pc), message);
[f485008]632}
633
[5ebff60]634struct groupchat *purple_chat_with(struct im_connection *ic, char *who)
[8ad5c34]635{
636        /* No, "of course" this won't work this way. Or in fact, it almost
637           does, but it only lets you send msgs to it, you won't receive
638           any. Instead, we have to click the virtual menu item.
639        PurpleAccount *pa = ic->proto_data;
640        PurpleConversation *pc;
641        PurpleConvChat *pcc;
642        struct groupchat *gc;
[5ebff60]643
[8ad5c34]644        gc = imcb_chat_new( ic, "BitlBee-libpurple groupchat" );
645        gc->data = pc = purple_conversation_new( PURPLE_CONV_TYPE_CHAT, pa, "BitlBee-libpurple groupchat" );
646        pc->ui_data = gc;
[5ebff60]647
[8ad5c34]648        pcc = PURPLE_CONV_CHAT( pc );
649        purple_conv_chat_add_user( pcc, ic->acc->user, "", 0, TRUE );
650        purple_conv_chat_invite_user( pcc, who, "Please join my chat", FALSE );
651        //purple_conv_chat_add_user( pcc, who, "", 0, TRUE );
652        */
[5ebff60]653
[8ad5c34]654        /* There went my nice afternoon. :-( */
[5ebff60]655
[d93c8beb]656        struct purple_data *pd = ic->proto_data;
657        PurplePlugin *prpl = purple_plugins_find_with_id(pd->account->protocol_id);
[8ad5c34]658        PurplePluginProtocolInfo *pi = prpl->info->extra_info;
[d93c8beb]659        PurpleBuddy *pb = purple_find_buddy(pd->account, who);
[8ad5c34]660        PurpleMenuAction *mi;
661        GList *menu;
[5ebff60]662
[8ad5c34]663        void (*callback)(PurpleBlistNode *, gpointer); /* FFFFFFFFFFFFFUUUUUUUUUUUUUU */
[5ebff60]664
665        if (!pb || !pi || !pi->blist_node_menu) {
[8ad5c34]666                return NULL;
[5ebff60]667        }
668
669        menu = pi->blist_node_menu(&pb->node);
670        while (menu) {
[8ad5c34]671                mi = menu->data;
[5ebff60]672                if (purple_menu_cmp(mi->label, "initiate chat") ||
673                    purple_menu_cmp(mi->label, "initiate conference")) {
[8ad5c34]674                        break;
[5ebff60]675                }
[8ad5c34]676                menu = menu->next;
677        }
[5ebff60]678
679        if (menu == NULL) {
[8ad5c34]680                return NULL;
[5ebff60]681        }
682
[8ad5c34]683        /* Call the fucker. */
[5ebff60]684        callback = (void *) mi->callback;
[459dec8]685        callback(&pb->node, mi->data);
[5ebff60]686
[8ad5c34]687        return NULL;
688}
689
[5ebff60]690void purple_chat_invite(struct groupchat *gc, char *who, char *message)
[8ad5c34]691{
692        PurpleConversation *pc = gc->data;
[5ebff60]693        PurpleConvChat *pcc = PURPLE_CONV_CHAT(pc);
[d93c8beb]694        struct purple_data *pd = gc->ic->proto_data;
[5ebff60]695
[d93c8beb]696        serv_chat_invite(purple_account_get_connection(pd->account),
[5ebff60]697                         purple_conv_chat_get_id(pcc),
698                         message && *message ? message : "Please join my chat",
699                         who);
[8ad5c34]700}
701
[524e931]702void purple_chat_set_topic(struct groupchat *gc, char *topic)
703{
704        PurpleConversation *pc = gc->data;
705        PurpleConvChat *pcc = PURPLE_CONV_CHAT(pc);
706        struct purple_data *pd = gc->ic->proto_data;
707        PurplePlugin *prpl = purple_plugins_find_with_id(pd->account->protocol_id);
708        PurplePluginProtocolInfo *pi = prpl->info->extra_info;
709
710        if (pi->set_chat_topic) {
711                pi->set_chat_topic(purple_account_get_connection(pd->account),
712                                   purple_conv_chat_get_id(pcc),
713                                   topic);
714        }
715}
716
[5ebff60]717void purple_chat_kick(struct groupchat *gc, char *who, const char *message)
[e41cc40]718{
719        PurpleConversation *pc = gc->data;
[5ebff60]720        char *str = g_strdup_printf("kick %s %s", who, message);
721
722        purple_conversation_do_command(pc, str, NULL, NULL);
723        g_free(str);
[e41cc40]724}
725
[5ebff60]726void purple_chat_leave(struct groupchat *gc)
[15794dc]727{
728        PurpleConversation *pc = gc->data;
[5ebff60]729
730        purple_conversation_destroy(pc);
[15794dc]731}
732
[5ebff60]733struct groupchat *purple_chat_join(struct im_connection *ic, const char *room, const char *nick, const char *password,
734                                   set_t **sets)
[c3caa46]735{
[d93c8beb]736        struct purple_data *pd = ic->proto_data;
737        PurplePlugin *prpl = purple_plugins_find_with_id(pd->account->protocol_id);
[c3caa46]738        PurplePluginProtocolInfo *pi = prpl->info->extra_info;
739        GHashTable *chat_hash;
740        PurpleConversation *conv;
[1a8875f]741        struct groupchat *gc;
[c3caa46]742        GList *info, *l;
[5ebff60]743
744        if (!pi->chat_info || !pi->chat_info_defaults ||
[d93c8beb]745            !(info = pi->chat_info(purple_account_get_connection(pd->account)))) {
[5ebff60]746                imcb_error(ic, "Joining chatrooms not supported by this protocol");
[c3caa46]747                return NULL;
748        }
[5ebff60]749
[d93c8beb]750        if ((conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_CHAT,
751                                                          room, pd->account))) {
[5ebff60]752                purple_conversation_destroy(conv);
753        }
754
[d93c8beb]755        chat_hash = pi->chat_info_defaults(
756                purple_account_get_connection(pd->account), room
757        );
[5ebff60]758
759        for (l = info; l; l = l->next) {
[c3caa46]760                struct proto_chat_entry *pce = l->data;
[5ebff60]761
762                if (strcmp(pce->identifier, "handle") == 0) {
763                        g_hash_table_replace(chat_hash, "handle", g_strdup(nick));
764                } else if (strcmp(pce->identifier, "password") == 0) {
765                        g_hash_table_replace(chat_hash, "password", g_strdup(password));
766                } else if (strcmp(pce->identifier, "passwd") == 0) {
767                        g_hash_table_replace(chat_hash, "passwd", g_strdup(password));
768                }
[1ec454c]769
770                g_free(pce);
[c3caa46]771        }
[5ebff60]772
[1ec454c]773        g_list_free(info);
774
[1a8875f]775        /* do this before serv_join_chat to handle cases where prplcb_conv_new is called immediately (not async) */
776        gc = imcb_chat_new(ic, room);
777
[d93c8beb]778        serv_join_chat(purple_account_get_connection(pd->account), chat_hash);
[5ebff60]779
[1ec454c]780        g_hash_table_destroy(chat_hash);
781
[1a8875f]782        return gc;
[c3caa46]783}
784
[725f942]785void purple_chat_list(struct im_connection *ic, const char *server)
786{
787        PurpleRoomlist *list;
788        struct purple_data *pd = ic->proto_data;
[01d56c0]789        PurplePlugin *prpl = purple_plugins_find_with_id(pd->account->protocol_id);
790        PurplePluginProtocolInfo *pi = prpl->info->extra_info;
791
792        if (!pi || !pi->roomlist_get_list) {
793                imcb_log(ic, "Room listing unsupported by this purple plugin");
794                return;
795        }
[725f942]796
[fecdd71]797        g_free(pd->chat_list_server);
798        pd->chat_list_server = (server && *server) ? g_strdup(server) : NULL;
799
[725f942]800        list = purple_roomlist_get_list(pd->account->gc);
801
802        if (list) {
[01d56c0]803                struct purple_roomlist_data *rld = list->ui_data;
804                rld->initialized = TRUE;
805
[725f942]806                purple_roomlist_ref(list);
807        }
[c3caa46]808}
809
[5ebff60]810void purple_transfer_request(struct im_connection *ic, file_transfer_t *ft, char *handle);
[edfc6db]811
[860ba6a]812static void purple_ui_init();
813
[dca8eff]814GHashTable *prplcb_ui_info()
815{
816        static GHashTable *ret;
[5ebff60]817
818        if (ret == NULL) {
[dca8eff]819                ret = g_hash_table_new(g_str_hash, g_str_equal);
[5ebff60]820                g_hash_table_insert(ret, "name", "BitlBee");
821                g_hash_table_insert(ret, "version", BITLBEE_VERSION);
[dca8eff]822        }
[5ebff60]823
[dca8eff]824        return ret;
825}
826
[5ebff60]827static PurpleCoreUiOps bee_core_uiops =
[860ba6a]828{
[98d46d5]829        NULL,                      /* ui_prefs_init */
830        NULL,                      /* debug_ui_init */
831        purple_ui_init,            /* ui_init */
832        NULL,                      /* quit */
833        prplcb_ui_info,            /* get_ui_info */
[860ba6a]834};
835
[5ebff60]836static void prplcb_conn_progress(PurpleConnection *gc, const char *text, size_t step, size_t step_count)
[860ba6a]837{
[5ebff60]838        struct im_connection *ic = purple_ic_by_gc(gc);
839
840        imcb_log(ic, "%s", text);
[860ba6a]841}
842
[5ebff60]843static void prplcb_conn_connected(PurpleConnection *gc)
[860ba6a]844{
[5ebff60]845        struct im_connection *ic = purple_ic_by_gc(gc);
[f85e9d6]846        const char *dn;
847        set_t *s;
[5ebff60]848
849        imcb_connected(ic);
850
851        if ((dn = purple_connection_get_display_name(gc)) &&
852            (s = set_find(&ic->acc->set, "display_name"))) {
853                g_free(s->value);
854                s->value = g_strdup(dn);
[f85e9d6]855        }
[694be84]856
857        // user list needs to be requested for Gadu-Gadu
[5ebff60]858        purple_gg_buddylist_import(gc);
859
[48b5fef]860        ic->flags |= OPT_DOES_HTML;
[860ba6a]861}
862
[5ebff60]863static void prplcb_conn_disconnected(PurpleConnection *gc)
[860ba6a]864{
[5ebff60]865        struct im_connection *ic = purple_ic_by_gc(gc);
866
867        if (ic != NULL) {
868                imc_logout(ic, !gc->wants_to_die);
[b74b287]869        }
[860ba6a]870}
871
[5ebff60]872static void prplcb_conn_notice(PurpleConnection *gc, const char *text)
[860ba6a]873{
[5ebff60]874        struct im_connection *ic = purple_ic_by_gc(gc);
875
876        if (ic != NULL) {
877                imcb_log(ic, "%s", text);
878        }
[860ba6a]879}
880
[5ebff60]881static void prplcb_conn_report_disconnect_reason(PurpleConnection *gc, PurpleConnectionError reason, const char *text)
[860ba6a]882{
[5ebff60]883        struct im_connection *ic = purple_ic_by_gc(gc);
884
[860ba6a]885        /* PURPLE_CONNECTION_ERROR_NAME_IN_USE means concurrent login,
886           should probably handle that. */
[5ebff60]887        if (ic != NULL) {
888                imcb_error(ic, "%s", text);
889        }
[860ba6a]890}
891
892static PurpleConnectionUiOps bee_conn_uiops =
893{
[98d46d5]894        prplcb_conn_progress,                    /* connect_progress */
895        prplcb_conn_connected,                   /* connected */
896        prplcb_conn_disconnected,                /* disconnected */
897        prplcb_conn_notice,                      /* notice */
898        NULL,                                    /* report_disconnect */
899        NULL,                                    /* network_connected */
900        NULL,                                    /* network_disconnected */
901        prplcb_conn_report_disconnect_reason,    /* report_disconnect_reason */
[860ba6a]902};
903
[5ebff60]904static void prplcb_blist_update(PurpleBuddyList *list, PurpleBlistNode *node)
[7da726b]905{
[5ebff60]906        if (node->type == PURPLE_BLIST_BUDDY_NODE) {
907                PurpleBuddy *bud = (PurpleBuddy *) node;
908                PurpleGroup *group = purple_buddy_get_group(bud);
909                struct im_connection *ic = purple_ic_by_pa(bud->account);
[4f103ea]910                PurpleStatus *as;
911                int flags = 0;
[5ebff60]912
913                if (ic == NULL) {
[db4cd40]914                        return;
[5ebff60]915                }
916
917                if (bud->server_alias) {
918                        imcb_rename_buddy(ic, bud->name, bud->server_alias);
919                } else if (bud->alias) {
920                        imcb_rename_buddy(ic, bud->name, bud->alias);
921                }
922
923                if (group) {
924                        imcb_add_buddy(ic, bud->name, purple_group_get_name(group));
925                }
926
927                flags |= purple_presence_is_online(bud->presence) ? OPT_LOGGED_IN : 0;
928                flags |= purple_presence_is_available(bud->presence) ? 0 : OPT_AWAY;
929
930                as = purple_presence_get_active_status(bud->presence);
931
932                imcb_buddy_status(ic, bud->name, flags, purple_status_get_name(as),
933                                  purple_status_get_attr_string(as, "message"));
934
935                imcb_buddy_times(ic, bud->name,
936                                 purple_presence_get_login_time(bud->presence),
937                                 purple_presence_get_idle_time(bud->presence));
[7da726b]938        }
939}
940
[5ebff60]941static void prplcb_blist_new(PurpleBlistNode *node)
[a08e875]942{
[5ebff60]943        if (node->type == PURPLE_BLIST_BUDDY_NODE) {
944                PurpleBuddy *bud = (PurpleBuddy *) node;
945                struct im_connection *ic = purple_ic_by_pa(bud->account);
946
947                if (ic == NULL) {
[a08e875]948                        return;
[5ebff60]949                }
950
951                imcb_add_buddy(ic, bud->name, NULL);
952
953                prplcb_blist_update(NULL, node);
[a08e875]954        }
955}
956
[5ebff60]957static void prplcb_blist_remove(PurpleBuddyList *list, PurpleBlistNode *node)
[7da726b]958{
[a91550c]959/*
[5ebff60]960        PurpleBuddy *bud = (PurpleBuddy*) node;
961
962        if( node->type == PURPLE_BLIST_BUDDY_NODE )
963        {
964                struct im_connection *ic = purple_ic_by_pa( bud->account );
965
966                if( ic == NULL )
967                        return;
968
969                imcb_remove_buddy( ic, bud->name, NULL );
970        }
[a91550c]971*/
[7da726b]972}
973
974static PurpleBlistUiOps bee_blist_uiops =
975{
[98d46d5]976        NULL,                      /* new_list */
977        prplcb_blist_new,          /* new_node */
978        NULL,                      /* show */
979        prplcb_blist_update,       /* update */
980        prplcb_blist_remove,       /* remove */
[7da726b]981};
982
[5ebff60]983void prplcb_conv_new(PurpleConversation *conv)
[f485008]984{
[5ebff60]985        if (conv->type == PURPLE_CONV_TYPE_CHAT) {
986                struct im_connection *ic = purple_ic_by_pa(conv->account);
[f485008]987                struct groupchat *gc;
[5ebff60]988
[a3019499]989                gc = bee_chat_by_title(ic->bee, ic, conv->name);
990
991                if (!gc) {
992                        gc = imcb_chat_new(ic, conv->name);
993                        if (conv->title != NULL) {
994                                imcb_chat_name_hint(gc, conv->title);
995                        }
996                }
997
998                /* don't set the topic if it's just the name */
999                if (conv->title != NULL && strcmp(conv->name, conv->title) != 0) {
[5ebff60]1000                        imcb_chat_topic(gc, NULL, conv->title, 0);
[fd213fe]1001                }
[36ee8c6]1002
[f485008]1003                conv->ui_data = gc;
1004                gc->data = conv;
[5ebff60]1005
[c3caa46]1006                /* libpurple brokenness: Whatever. Show that we join right away,
1007                   there's no clear "This is you!" signaling in _add_users so
1008                   don't even try. */
[5ebff60]1009                imcb_chat_add_buddy(gc, gc->ic->acc->user);
[f485008]1010        }
1011}
1012
[5ebff60]1013void prplcb_conv_free(PurpleConversation *conv)
[f485008]1014{
1015        struct groupchat *gc = conv->ui_data;
[5ebff60]1016
1017        imcb_chat_free(gc);
[f485008]1018}
1019
[5ebff60]1020void prplcb_conv_add_users(PurpleConversation *conv, GList *cbuddies, gboolean new_arrivals)
[f485008]1021{
1022        struct groupchat *gc = conv->ui_data;
1023        GList *b;
[5ebff60]1024
1025        for (b = cbuddies; b; b = b->next) {
[f485008]1026                PurpleConvChatBuddy *pcb = b->data;
[5ebff60]1027
1028                imcb_chat_add_buddy(gc, pcb->name);
[f485008]1029        }
1030}
1031
[5ebff60]1032void prplcb_conv_del_users(PurpleConversation *conv, GList *cbuddies)
[f485008]1033{
1034        struct groupchat *gc = conv->ui_data;
1035        GList *b;
[5ebff60]1036
1037        for (b = cbuddies; b; b = b->next) {
1038                imcb_chat_remove_buddy(gc, b->data, "");
1039        }
[f485008]1040}
1041
[ba7618d]1042/* Generic handler for IM or chat messages, covers write_chat, write_im and write_conv */
[0e48e54]1043static void handle_conv_msg(PurpleConversation *conv, const char *who, const char *message_, guint32 bee_flags, time_t mtime)
[f485008]1044{
[ba7618d]1045        struct im_connection *ic = purple_ic_by_pa(conv->account);
[f485008]1046        struct groupchat *gc = conv->ui_data;
[0e48e54]1047        char *message = g_strdup(message_);
[f485008]1048        PurpleBuddy *buddy;
[5ebff60]1049
1050        buddy = purple_find_buddy(conv->account, who);
1051        if (buddy != NULL) {
1052                who = purple_buddy_get_name(buddy);
1053        }
1054
[ba7618d]1055        if (conv->type == PURPLE_CONV_TYPE_IM) {
[0e48e54]1056                imcb_buddy_msg(ic, who, message, bee_flags, mtime);
[ba7618d]1057        } else if (gc) {
[0e48e54]1058                imcb_chat_msg(gc, who, message, bee_flags, mtime);
[ba7618d]1059        }
[0e48e54]1060
1061        g_free(message);
[f485008]1062}
1063
[ba7618d]1064/* Handles write_im and write_chat. Removes echoes of locally sent messages */
1065static void prplcb_conv_msg(PurpleConversation *conv, const char *who, const char *message, PurpleMessageFlags flags, time_t mtime)
[d250b2a]1066{
[ba7618d]1067        if (!(flags & PURPLE_MESSAGE_SEND)) {
1068                handle_conv_msg(conv, who, message, 0, mtime);
[5ebff60]1069        }
[ba7618d]1070}
[5ebff60]1071
[ba7618d]1072/* Handles write_conv. Only passes self messages from other locations through.
1073 * That is, only writes of PURPLE_MESSAGE_SEND.
1074 * There are more events which might be handled in the future, but some are tricky.
1075 * (images look like <img id="123">, what do i do with that?) */
1076static void prplcb_conv_write(PurpleConversation *conv, const char *who, const char *alias, const char *message,
1077                              PurpleMessageFlags flags, time_t mtime)
1078{
1079        if (flags & PURPLE_MESSAGE_SEND) {
1080                handle_conv_msg(conv, who, message, OPT_SELFMESSAGE, mtime);
[5ebff60]1081        }
[d250b2a]1082}
1083
[bad41f56]1084/* No, this is not a ui_op but a signal. */
[5ebff60]1085static void prplcb_buddy_typing(PurpleAccount *account, const char *who, gpointer null)
[bad41f56]1086{
1087        PurpleConversation *conv;
1088        PurpleConvIm *im;
1089        int state;
[5ebff60]1090
1091        if ((conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, who, account)) == NULL) {
[bad41f56]1092                return;
[5ebff60]1093        }
1094
[bad41f56]1095        im = PURPLE_CONV_IM(conv);
[5ebff60]1096        switch (purple_conv_im_get_typing_state(im)) {
[bad41f56]1097        case PURPLE_TYPING:
1098                state = OPT_TYPING;
1099                break;
1100        case PURPLE_TYPED:
1101                state = OPT_THINKING;
1102                break;
1103        default:
1104                state = 0;
1105        }
[5ebff60]1106
1107        imcb_buddy_typing(purple_ic_by_pa(account), who, state);
[bad41f56]1108}
1109
[5ebff60]1110static PurpleConversationUiOps bee_conv_uiops =
[860ba6a]1111{
[f485008]1112        prplcb_conv_new,           /* create_conversation  */
1113        prplcb_conv_free,          /* destroy_conversation */
[ba7618d]1114        prplcb_conv_msg,           /* write_chat           */
1115        prplcb_conv_msg,           /* write_im             */
1116        prplcb_conv_write,         /* write_conv           */
[f485008]1117        prplcb_conv_add_users,     /* chat_add_users       */
[860ba6a]1118        NULL,                      /* chat_rename_user     */
[f485008]1119        prplcb_conv_del_users,     /* chat_remove_users    */
[860ba6a]1120        NULL,                      /* chat_update_user     */
1121        NULL,                      /* present              */
1122        NULL,                      /* has_focus            */
1123        NULL,                      /* custom_smiley_add    */
1124        NULL,                      /* custom_smiley_write  */
1125        NULL,                      /* custom_smiley_close  */
1126        NULL,                      /* send_confirm         */
1127};
1128
[5ebff60]1129struct prplcb_request_action_data {
[0ac1a375]1130        void *user_data, *bee_data;
1131        PurpleRequestActionCb yes, no;
1132        int yes_i, no_i;
1133};
1134
[5ebff60]1135static void prplcb_request_action_yes(void *data)
[0ac1a375]1136{
1137        struct prplcb_request_action_data *pqad = data;
[5ebff60]1138
1139        if (pqad->yes) {
1140                pqad->yes(pqad->user_data, pqad->yes_i);
1141        }
[0ac1a375]1142}
1143
[5ebff60]1144static void prplcb_request_action_no(void *data)
[0ac1a375]1145{
1146        struct prplcb_request_action_data *pqad = data;
[5ebff60]1147
1148        if (pqad->no) {
1149                pqad->no(pqad->user_data, pqad->no_i);
1150        }
[2c5ab49]1151}
1152
1153/* q->free() callback from query_del()*/
1154static void prplcb_request_action_free(void *data)
1155{
1156        struct prplcb_request_action_data *pqad = data;
1157
1158        pqad->bee_data = NULL;
1159        purple_request_close(PURPLE_REQUEST_ACTION, pqad);
[0ac1a375]1160}
1161
[5ebff60]1162static void *prplcb_request_action(const char *title, const char *primary, const char *secondary,
1163                                   int default_action, PurpleAccount *account, const char *who,
1164                                   PurpleConversation *conv, void *user_data, size_t action_count,
1165                                   va_list actions)
[0ac1a375]1166{
[5ebff60]1167        struct prplcb_request_action_data *pqad;
[0ac1a375]1168        int i;
1169        char *q;
[5ebff60]1170
1171        pqad = g_new0(struct prplcb_request_action_data, 1);
1172
1173        for (i = 0; i < action_count; i++) {
[0ac1a375]1174                char *caption;
1175                void *fn;
[5ebff60]1176
1177                caption = va_arg(actions, char*);
1178                fn = va_arg(actions, void*);
1179
1180                if (strstr(caption, "Accept") || strstr(caption, "OK")) {
[0ac1a375]1181                        pqad->yes = fn;
1182                        pqad->yes_i = i;
[5ebff60]1183                } else if (strstr(caption, "Reject") || strstr(caption, "Cancel")) {
[0ac1a375]1184                        pqad->no = fn;
1185                        pqad->no_i = i;
1186                }
1187        }
[5ebff60]1188
[0ac1a375]1189        pqad->user_data = user_data;
[5ebff60]1190
[4aa0f6b]1191        /* TODO: IRC stuff here :-( */
[5ebff60]1192        q = g_strdup_printf("Request: %s\n\n%s\n\n%s", title, primary, secondary);
[56985aa]1193        pqad->bee_data = query_add(local_bee->ui_data, purple_ic_by_pa(account), q,
[2c5ab49]1194                                   prplcb_request_action_yes, prplcb_request_action_no,
1195                                   prplcb_request_action_free, pqad);
[5ebff60]1196
1197        g_free(q);
1198
[e5d8d21]1199        return pqad;
[0ac1a375]1200}
1201
[3bb333c]1202/* So it turns out some requests have no account context at all, because
1203 * libpurple hates us. This means that query_del_by_conn() won't remove those
1204 * on logout, and will segfault if the user replies. That's why this exists.
1205 */
1206static void prplcb_close_request(PurpleRequestType type, void *data)
1207{
[2c5ab49]1208        struct prplcb_request_action_data *pqad;
1209        struct request_input_data *ri;
1210        struct purple_data *pd;
1211
1212        if (!data) {
1213                return;
1214        }
1215
1216        switch (type) {
1217        case PURPLE_REQUEST_ACTION:
1218                pqad = data;
1219                /* if this is null, it's because query_del was run already */
1220                if (pqad->bee_data) {
1221                        query_del(local_bee->ui_data, pqad->bee_data);
1222                }
1223                g_free(pqad);
1224                break;
1225        case PURPLE_REQUEST_INPUT:
1226                ri = data;
1227                pd = ri->ic->proto_data;
1228                imcb_remove_buddy(ri->ic, ri->buddy, NULL);
1229                g_free(ri->buddy);
1230                g_hash_table_remove(pd->input_requests, GUINT_TO_POINTER(ri->id));
1231                break;
1232        default:
1233                g_free(data);
1234                break;
[3bb333c]1235        }
1236
[177ffd7]1237}
1238
[6a48992]1239void* prplcb_request_input(const char *title, const char *primary,
1240        const char *secondary, const char *default_value, gboolean multiline,
1241        gboolean masked, gchar *hint, const char *ok_text, GCallback ok_cb,
1242        const char *cancel_text, GCallback cancel_cb, PurpleAccount *account,
1243        const char *who, PurpleConversation *conv, void *user_data)
1244{
1245        struct im_connection *ic = purple_ic_by_pa(account);
1246        struct purple_data *pd = ic->proto_data;
[fecdd71]1247        struct request_input_data *ri;
1248        guint id;
1249
1250        /* hack so that jabber's chat list doesn't ask for conference server twice */
1251        if (pd->chat_list_server && title && g_strcmp0(title, "Enter a Conference Server") == 0) {
1252                ((ri_callback_t) ok_cb)(user_data, pd->chat_list_server);
1253                g_free(pd->chat_list_server);
1254                pd->chat_list_server = NULL;
1255                return NULL;
1256        }
1257
1258        id = pd->next_request_id++;
1259        ri = g_new0(struct request_input_data, 1);
[6a48992]1260
[2c5ab49]1261        ri->id = id;
1262        ri->ic = ic;
1263        ri->buddy = g_strdup_printf("%s_%u", PURPLE_REQUEST_HANDLE, id);
[6a48992]1264        ri->data_callback = (ri_callback_t) ok_cb;
1265        ri->user_data = user_data;
1266        g_hash_table_insert(pd->input_requests, GUINT_TO_POINTER(id), ri);
1267
[2c5ab49]1268        imcb_add_buddy(ic, ri->buddy, NULL);
[1239d05]1269
1270        if (title && *title) {
1271                imcb_buddy_msg(ic, ri->buddy, title, 0, 0);
1272        }
1273
1274        if (primary && *primary) {
1275                imcb_buddy_msg(ic, ri->buddy, primary, 0, 0);
1276        }
1277
1278        if (secondary && *secondary) {
1279                imcb_buddy_msg(ic, ri->buddy, secondary, 0, 0);
1280        }
[6a48992]1281
[2c5ab49]1282        return ri;
[6a48992]1283}
1284
1285void purple_request_input_callback(guint id, struct im_connection *ic,
1286                                   const char *message, const char *who)
1287{
1288        struct purple_data *pd = ic->proto_data;
[2c5ab49]1289        struct request_input_data *ri;
[6a48992]1290
[2c5ab49]1291        if (!(ri = g_hash_table_lookup(pd->input_requests, GUINT_TO_POINTER(id)))) {
1292                return;
[6a48992]1293        }
1294
[2c5ab49]1295        ri->data_callback(ri->user_data, message);
1296
1297        purple_request_close(PURPLE_REQUEST_INPUT, ri);
[6a48992]1298}
1299
1300
[0ac1a375]1301static PurpleRequestUiOps bee_request_uiops =
1302{
[98d46d5]1303        prplcb_request_input,      /* request_input */
1304        NULL,                      /* request_choice */
1305        prplcb_request_action,     /* request_action */
1306        NULL,                      /* request_fields */
1307        NULL,                      /* request_file */
1308        prplcb_close_request,      /* close_request */
1309        NULL,                      /* request_folder */
[0ac1a375]1310};
1311
[5ebff60]1312static void prplcb_privacy_permit_added(PurpleAccount *account, const char *name)
[05a8932]1313{
[5ebff60]1314        struct im_connection *ic = purple_ic_by_pa(account);
1315
1316        if (!g_slist_find_custom(ic->permit, name, (GCompareFunc) ic->acc->prpl->handle_cmp)) {
1317                ic->permit = g_slist_prepend(ic->permit, g_strdup(name));
1318        }
[05a8932]1319}
1320
[5ebff60]1321static void prplcb_privacy_permit_removed(PurpleAccount *account, const char *name)
[05a8932]1322{
[5ebff60]1323        struct im_connection *ic = purple_ic_by_pa(account);
[05a8932]1324        void *n;
[5ebff60]1325
1326        n = g_slist_find_custom(ic->permit, name, (GCompareFunc) ic->acc->prpl->handle_cmp);
1327        ic->permit = g_slist_remove(ic->permit, n);
[05a8932]1328}
1329
[5ebff60]1330static void prplcb_privacy_deny_added(PurpleAccount *account, const char *name)
[05a8932]1331{
[5ebff60]1332        struct im_connection *ic = purple_ic_by_pa(account);
1333
1334        if (!g_slist_find_custom(ic->deny, name, (GCompareFunc) ic->acc->prpl->handle_cmp)) {
1335                ic->deny = g_slist_prepend(ic->deny, g_strdup(name));
1336        }
[05a8932]1337}
1338
[5ebff60]1339static void prplcb_privacy_deny_removed(PurpleAccount *account, const char *name)
[05a8932]1340{
[5ebff60]1341        struct im_connection *ic = purple_ic_by_pa(account);
[05a8932]1342        void *n;
[5ebff60]1343
1344        n = g_slist_find_custom(ic->deny, name, (GCompareFunc) ic->acc->prpl->handle_cmp);
1345        ic->deny = g_slist_remove(ic->deny, n);
[05a8932]1346}
1347
1348static PurplePrivacyUiOps bee_privacy_uiops =
1349{
[98d46d5]1350        prplcb_privacy_permit_added,       /* permit_added */
1351        prplcb_privacy_permit_removed,     /* permit_removed */
1352        prplcb_privacy_deny_added,         /* deny_added */
1353        prplcb_privacy_deny_removed,       /* deny_removed */
[05a8932]1354};
1355
[725f942]1356static void prplcb_roomlist_create(PurpleRoomlist *list)
1357{
1358        struct purple_roomlist_data *rld;
1359
1360        list->ui_data = rld = g_new0(struct purple_roomlist_data, 1);
1361        rld->topic = -1;
1362}
1363
1364static void prplcb_roomlist_set_fields(PurpleRoomlist *list, GList *fields)
1365{
1366        gint topic = -1;
1367        GList *l;
1368        guint i;
1369        PurpleRoomlistField *field;
1370        struct purple_roomlist_data *rld = list->ui_data;
1371
1372        for (i = 0, l = fields; l; i++, l = l->next) {
1373                field = l->data;
1374
1375                /* Use the first visible string field as a fallback topic */
1376                if (i != 0 && topic < 0 && !field->hidden &&
1377                    field->type == PURPLE_ROOMLIST_FIELD_STRING) {
1378                        topic = i;
1379                }
1380
1381                if ((g_strcasecmp(field->name, "description") == 0) ||
1382                    (g_strcasecmp(field->name, "topic") == 0)) {
1383                        if (field->type == PURPLE_ROOMLIST_FIELD_STRING) {
1384                                rld->topic = i;
1385                        }
1386                }
1387        }
1388
1389        if (rld->topic < 0) {
1390                rld->topic = topic;
1391        }
1392}
1393
1394static void prplcb_roomlist_add_room(PurpleRoomlist *list, PurpleRoomlistRoom *room)
1395{
1396        bee_chat_info_t *ci;
1397        const char *title;
1398        const char *topic;
1399        GList *fields;
1400        struct purple_roomlist_data *rld = list->ui_data;
1401
1402        fields = purple_roomlist_room_get_fields(room);
1403        title = purple_roomlist_room_get_name(room);
1404
1405        if (rld->topic >= 0) {
1406                topic = g_list_nth_data(fields, rld->topic);
1407        } else {
1408                topic = NULL;
1409        }
1410
1411        ci = g_new(bee_chat_info_t, 1);
1412        ci->title = g_strdup(title);
1413        ci->topic = g_strdup(topic);
1414        rld->chats = g_slist_prepend(rld->chats, ci);
1415}
1416
1417static void prplcb_roomlist_in_progress(PurpleRoomlist *list, gboolean in_progress)
1418{
1419        struct im_connection *ic;
[fecdd71]1420        struct purple_data *pd;
[725f942]1421        struct purple_roomlist_data *rld = list->ui_data;
1422
[01d56c0]1423        if (in_progress || !rld) {
[725f942]1424                return;
1425        }
1426
1427        ic = purple_ic_by_pa(list->account);
[6e991a9]1428        imcb_chat_list_free(ic);
[725f942]1429
[fecdd71]1430        pd = ic->proto_data;
1431        g_free(pd->chat_list_server);
1432        pd->chat_list_server = NULL;
1433
[725f942]1434        ic->chatlist = g_slist_reverse(rld->chats);
1435        rld->chats = NULL;
1436
[a08b2db]1437        imcb_chat_list_finish(ic);
[01d56c0]1438
1439        if (rld->initialized) {
1440                purple_roomlist_unref(list);
1441        }
[725f942]1442}
1443
1444static void prplcb_roomlist_destroy(PurpleRoomlist *list)
1445{
1446        g_free(list->ui_data);
1447        list->ui_data = NULL;
1448}
1449
1450static PurpleRoomlistUiOps bee_roomlist_uiops =
1451{
1452        NULL,                         /* show_with_account */
1453        prplcb_roomlist_create,       /* create */
1454        prplcb_roomlist_set_fields,   /* set_fields */
1455        prplcb_roomlist_add_room,     /* add_room */
1456        prplcb_roomlist_in_progress,  /* in_progress */
1457        prplcb_roomlist_destroy,      /* destroy */
1458};
1459
[5ebff60]1460static void prplcb_debug_print(PurpleDebugLevel level, const char *category, const char *arg_s)
[0cbef26]1461{
[5ebff60]1462        fprintf(stderr, "DEBUG %s: %s", category, arg_s);
[0cbef26]1463}
1464
1465static PurpleDebugUiOps bee_debug_uiops =
1466{
[98d46d5]1467        prplcb_debug_print,        /* print */
[0cbef26]1468};
1469
[5ebff60]1470static guint prplcb_ev_timeout_add(guint interval, GSourceFunc func, gpointer udata)
[4164e62]1471{
[5ebff60]1472        return b_timeout_add(interval, (b_event_handler) func, udata);
[4164e62]1473}
1474
[5ebff60]1475static guint prplcb_ev_input_add(int fd, PurpleInputCondition cond, PurpleInputFunction func, gpointer udata)
[4164e62]1476{
[5ebff60]1477        return b_input_add(fd, cond | B_EV_FLAG_FORCE_REPEAT, (b_event_handler) func, udata);
[4164e62]1478}
1479
[5ebff60]1480static gboolean prplcb_ev_remove(guint id)
[4164e62]1481{
[5ebff60]1482        b_event_remove((gint) id);
[4164e62]1483        return TRUE;
1484}
1485
[5ebff60]1486static PurpleEventLoopUiOps glib_eventloops =
[4164e62]1487{
[98d46d5]1488        prplcb_ev_timeout_add,     /* timeout_add */
1489        prplcb_ev_remove,          /* timeout_remove */
1490        prplcb_ev_input_add,       /* input_add */
1491        prplcb_ev_remove,          /* input_remove */
[4164e62]1492};
1493
[05aba55]1494/* Absolutely no connection context at all. Thanks purple! brb crying */
1495static void *prplcb_notify_message(PurpleNotifyMsgType type, const char *title,
1496                                   const char *primary, const char *secondary)
1497{
1498        char *text = g_strdup_printf("%s%s - %s%s%s",
1499                (type == PURPLE_NOTIFY_MSG_ERROR) ? "Error: " : "",
1500                title,
1501                primary ?: "",
1502                (primary && secondary) ? " - " : "",
1503                secondary ?: ""
1504        );
1505
1506        if (local_bee->ui->log) {
1507                local_bee->ui->log(local_bee, "purple", text);
1508        }
1509
1510        g_free(text);
1511
1512        return NULL;
1513}
1514
[5ebff60]1515static void *prplcb_notify_email(PurpleConnection *gc, const char *subject, const char *from,
1516                                 const char *to, const char *url)
[bab1c86]1517{
[5ebff60]1518        struct im_connection *ic = purple_ic_by_gc(gc);
1519
[0864a52]1520        imcb_notify_email(ic, "Received e-mail from %s for %s: %s <%s>", from, to, subject, url);
[5ebff60]1521
[bab1c86]1522        return NULL;
1523}
1524
[5ebff60]1525static void *prplcb_notify_userinfo(PurpleConnection *gc, const char *who, PurpleNotifyUserInfo *user_info)
[e77c264]1526{
[5ebff60]1527        struct im_connection *ic = purple_ic_by_gc(gc);
1528        GString *info = g_string_new("");
1529        GList *l = purple_notify_user_info_get_entries(user_info);
[e77c264]1530        char *key;
1531        const char *value;
1532        int n;
[5ebff60]1533
1534        while (l) {
[e77c264]1535                PurpleNotifyUserInfoEntry *e = l->data;
[5ebff60]1536
1537                switch (purple_notify_user_info_entry_get_type(e)) {
[e77c264]1538                case PURPLE_NOTIFY_USER_INFO_ENTRY_PAIR:
1539                case PURPLE_NOTIFY_USER_INFO_ENTRY_SECTION_HEADER:
[5ebff60]1540                        key = g_strdup(purple_notify_user_info_entry_get_label(e));
1541                        value = purple_notify_user_info_entry_get_value(e);
1542
1543                        if (key) {
1544                                strip_html(key);
1545                                g_string_append_printf(info, "%s: ", key);
1546
1547                                if (value) {
1548                                        n = strlen(value) - 1;
1549                                        while (g_ascii_isspace(value[n])) {
1550                                                n--;
1551                                        }
1552                                        g_string_append_len(info, value, n + 1);
[e77c264]1553                                }
[5ebff60]1554                                g_string_append_c(info, '\n');
1555                                g_free(key);
[e77c264]1556                        }
[5ebff60]1557
[e77c264]1558                        break;
1559                case PURPLE_NOTIFY_USER_INFO_ENTRY_SECTION_BREAK:
[5ebff60]1560                        g_string_append(info, "------------------------\n");
[e77c264]1561                        break;
1562                }
[5ebff60]1563
[e77c264]1564                l = l->next;
1565        }
[5ebff60]1566
1567        imcb_log(ic, "User %s info:\n%s", who, info->str);
1568        g_string_free(info, TRUE);
1569
[e77c264]1570        return NULL;
1571}
1572
[437bd9b]1573static PurpleNotifyUiOps bee_notify_uiops =
[bab1c86]1574{
[98d46d5]1575        prplcb_notify_message,     /* notify_message */
1576        prplcb_notify_email,       /* notify_email */
1577        NULL,                      /* notify_emails */
1578        NULL,                      /* notify_formatted */
1579        NULL,                      /* notify_searchresults */
1580        NULL,                      /* notify_searchresults_new_rows */
1581        prplcb_notify_userinfo,    /* notify_userinfo */
[bab1c86]1582};
1583
[5ebff60]1584static void *prplcb_account_request_authorize(PurpleAccount *account, const char *remote_user,
1585                                              const char *id, const char *alias, const char *message, gboolean on_list,
1586                                              PurpleAccountRequestAuthorizationCb authorize_cb,
1587                                              PurpleAccountRequestAuthorizationCb deny_cb, void *user_data)
[d0527c1]1588{
[5ebff60]1589        struct im_connection *ic = purple_ic_by_pa(account);
[d0527c1]1590        char *q;
[5ebff60]1591
1592        if (alias) {
1593                q = g_strdup_printf("%s (%s) wants to add you to his/her contact "
1594                                    "list. (%s)", alias, remote_user, message);
1595        } else {
1596                q = g_strdup_printf("%s wants to add you to his/her contact "
1597                                    "list. (%s)", remote_user, message);
1598        }
1599
1600        imcb_ask_with_free(ic, q, user_data, authorize_cb, deny_cb, NULL);
1601        g_free(q);
1602
[3e59c8d]1603        return NULL;
[d0527c1]1604}
1605
1606static PurpleAccountUiOps bee_account_uiops =
1607{
[98d46d5]1608        NULL,                              /* notify_added */
1609        NULL,                              /* status_changed */
1610        NULL,                              /* request_add */
1611        prplcb_account_request_authorize,  /* request_authorize */
1612        NULL,                              /* close_account_request */
[d0527c1]1613};
1614
[2309152]1615extern PurpleXferUiOps bee_xfer_uiops;
[edfc6db]1616
[860ba6a]1617static void purple_ui_init()
1618{
[5ebff60]1619        purple_connections_set_ui_ops(&bee_conn_uiops);
1620        purple_blist_set_ui_ops(&bee_blist_uiops);
1621        purple_conversations_set_ui_ops(&bee_conv_uiops);
1622        purple_request_set_ui_ops(&bee_request_uiops);
1623        purple_privacy_set_ui_ops(&bee_privacy_uiops);
[725f942]1624        purple_roomlist_set_ui_ops(&bee_roomlist_uiops);
[5ebff60]1625        purple_notify_set_ui_ops(&bee_notify_uiops);
1626        purple_accounts_set_ui_ops(&bee_account_uiops);
1627        purple_xfers_set_ui_ops(&bee_xfer_uiops);
1628
1629        if (getenv("BITLBEE_DEBUG")) {
1630                purple_debug_set_ui_ops(&bee_debug_uiops);
1631        }
[860ba6a]1632}
1633
[796da03]1634void purple_initmodule()
1635{
[cd741d8]1636        struct prpl funcs;
[796da03]1637        GList *prots;
[e5d8d21]1638        GString *help;
[3c9b095]1639        char *dir;
[5ebff60]1640
[57f81ec]1641        if (purple_get_core() != NULL) {
1642                log_message(LOGLVL_ERROR, "libpurple already initialized. "
1643                            "Please use inetd or ForkDaemon mode instead.");
1644                return;
1645        }
1646
[5bb5ee3]1647        g_assert((int) B_EV_IO_READ == (int) PURPLE_INPUT_READ);
1648        g_assert((int) B_EV_IO_WRITE == (int) PURPLE_INPUT_WRITE);
[5ebff60]1649
1650        dir = g_strdup_printf("%s/purple", global.conf->configdir);
1651        purple_util_set_user_dir(dir);
1652        g_free(dir);
1653
[5dbf66e]1654        dir = g_strdup_printf("%s/purple", global.conf->plugindir);
1655        purple_plugins_add_search_path(dir);
1656        g_free(dir);
1657
[5ebff60]1658        purple_debug_set_enabled(FALSE);
1659        purple_core_set_ui_ops(&bee_core_uiops);
1660        purple_eventloop_set_ui_ops(&glib_eventloops);
1661        if (!purple_core_init("BitlBee")) {
[796da03]1662                /* Initializing the core failed. Terminate. */
[5ebff60]1663                fprintf(stderr, "libpurple initialization failed.\n");
[796da03]1664                abort();
1665        }
[5ebff60]1666
1667        if (proxytype != PROXY_NONE) {
[ff94563]1668                PurpleProxyInfo *pi = purple_global_proxy_get_info();
[5ebff60]1669                switch (proxytype) {
[12f041d]1670                case PROXY_SOCKS4A:
[7add7ec]1671                case PROXY_SOCKS4:
[5ebff60]1672                        purple_proxy_info_set_type(pi, PURPLE_PROXY_SOCKS4);
[7add7ec]1673                        break;
1674                case PROXY_SOCKS5:
[5ebff60]1675                        purple_proxy_info_set_type(pi, PURPLE_PROXY_SOCKS5);
[7add7ec]1676                        break;
1677                case PROXY_HTTP:
[5ebff60]1678                        purple_proxy_info_set_type(pi, PURPLE_PROXY_HTTP);
[7add7ec]1679                        break;
[5ebff60]1680                }
1681                purple_proxy_info_set_host(pi, proxyhost);
1682                purple_proxy_info_set_port(pi, proxyport);
1683                purple_proxy_info_set_username(pi, proxyuser);
1684                purple_proxy_info_set_password(pi, proxypass);
[7add7ec]1685        }
[5ebff60]1686
1687        purple_set_blist(purple_blist_new());
1688
[bad41f56]1689        /* No, really. So far there were ui_ops for everything, but now suddenly
1690           one needs to use signals for typing notification stuff. :-( */
[5ebff60]1691        purple_signal_connect(purple_conversations_get_handle(), "buddy-typing",
1692                              &funcs, PURPLE_CALLBACK(prplcb_buddy_typing), NULL);
1693        purple_signal_connect(purple_conversations_get_handle(), "buddy-typed",
1694                              &funcs, PURPLE_CALLBACK(prplcb_buddy_typing), NULL);
1695        purple_signal_connect(purple_conversations_get_handle(), "buddy-typing-stopped",
1696                              &funcs, PURPLE_CALLBACK(prplcb_buddy_typing), NULL);
1697
1698        memset(&funcs, 0, sizeof(funcs));
[cd741d8]1699        funcs.login = purple_login;
1700        funcs.init = purple_init;
1701        funcs.logout = purple_logout;
1702        funcs.buddy_msg = purple_buddy_msg;
1703        funcs.away_states = purple_away_states;
1704        funcs.set_away = purple_set_away;
1705        funcs.add_buddy = purple_add_buddy;
1706        funcs.remove_buddy = purple_remove_buddy;
[05a8932]1707        funcs.add_permit = purple_add_permit;
1708        funcs.add_deny = purple_add_deny;
1709        funcs.rem_permit = purple_rem_permit;
1710        funcs.rem_deny = purple_rem_deny;
[e77c264]1711        funcs.get_info = purple_get_info;
[cd741d8]1712        funcs.keepalive = purple_keepalive;
1713        funcs.send_typing = purple_send_typing;
1714        funcs.handle_cmp = g_strcasecmp;
[f485008]1715        /* TODO(wilmer): Set these only for protocols that support them? */
1716        funcs.chat_msg = purple_chat_msg;
[8ad5c34]1717        funcs.chat_with = purple_chat_with;
1718        funcs.chat_invite = purple_chat_invite;
[524e931]1719        funcs.chat_topic = purple_chat_set_topic;
[e41cc40]1720        funcs.chat_kick = purple_chat_kick;
[15794dc]1721        funcs.chat_leave = purple_chat_leave;
[c3caa46]1722        funcs.chat_join = purple_chat_join;
[725f942]1723        funcs.chat_list = purple_chat_list;
[edfc6db]1724        funcs.transfer_request = purple_transfer_request;
[5ebff60]1725
1726        help = g_string_new("BitlBee libpurple module supports the following IM protocols:\n");
1727
[bab1c86]1728        /* Add a protocol entry to BitlBee's structures for every protocol
[5ebff60]1729           supported by this libpurple instance. */
1730        for (prots = purple_plugins_get_protocols(); prots; prots = prots->next) {
[796da03]1731                PurplePlugin *prot = prots->data;
[9f03c47]1732                PurplePluginProtocolInfo *pi = prot->info->extra_info;
[cd741d8]1733                struct prpl *ret;
[5ebff60]1734
[c775a58]1735                /* If we already have this one (as a native module), don't
1736                   add a libpurple duplicate. */
[5ebff60]1737                if (find_protocol(prot->info->id)) {
[c775a58]1738                        continue;
[5ebff60]1739                }
1740
1741                ret = g_memdup(&funcs, sizeof(funcs));
[cd741d8]1742                ret->name = ret->data = prot->info->id;
[5ebff60]1743                if (strncmp(ret->name, "prpl-", 5) == 0) {
[cd741d8]1744                        ret->name += 5;
[5ebff60]1745                }
[9f03c47]1746
1747                if (pi->options & OPT_PROTO_NO_PASSWORD) {
1748                        ret->options |= PRPL_OPT_NO_PASSWORD;
1749                }
1750
1751                if (pi->options & OPT_PROTO_PASSWORD_OPTIONAL) {
1752                        ret->options |= PRPL_OPT_PASSWORD_OPTIONAL;
1753                }
1754
[5ebff60]1755                register_protocol(ret);
1756
1757                g_string_append_printf(help, "\n* %s (%s)", ret->name, prot->info->name);
1758
[bab1c86]1759                /* libpurple doesn't define a protocol called OSCAR, but we
1760                   need it to be compatible with normal BitlBee. */
[5ebff60]1761                if (g_strcasecmp(prot->info->id, "prpl-aim") == 0) {
1762                        ret = g_memdup(&funcs, sizeof(funcs));
[cd741d8]1763                        ret->name = "oscar";
[50988d1]1764                        /* purple_get_account_prpl_id() determines the actual protocol ID (icq/aim) */
1765                        ret->data = NULL;
[5ebff60]1766                        register_protocol(ret);
[cd741d8]1767                }
[796da03]1768        }
[5ebff60]1769
1770        g_string_append(help, "\n\nFor used protocols, more information about available "
1771                        "settings can be found using \x02help purple <protocol name>\x02 "
1772                        "(create an account using that protocol first!)");
1773
[bab1c86]1774        /* Add a simple dynamically-generated help item listing all
1775           the supported protocols. */
[5ebff60]1776        help_add_mem(&global.help, "purple", help->str);
1777        g_string_free(help, TRUE);
[796da03]1778}
Note: See TracBrowser for help on using the repository browser.