source: protocols/nogaim.c @ af9f2ca

Last change on this file since af9f2ca was 1e52e1f, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-07-11T10:30:27Z

When cleaning up queries, q->data is free()d. Even if it turns out to be
the "struct irc" containing all data belonging to a session. Sanitise
memory management a little bit here. (There are some memory leaks in here
too that need to be fixed at some point.)

  • Property mode set to 100644
File size: 16.5 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2010 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., 59 Temple Place,
31  Suite 330, Boston, MA  02111-1307  USA
32*/
33
34#define BITLBEE_CORE
35#include <ctype.h>
36
37#include "nogaim.h"
38
39GSList *connections;
40
41#ifdef WITH_PLUGINS
42gboolean load_plugin(char *path)
43{
44        void (*init_function) (void);
45       
46        GModule *mod = g_module_open(path, G_MODULE_BIND_LAZY);
47
48        if(!mod) {
49                log_message(LOGLVL_ERROR, "Can't find `%s', not loading (%s)\n", path, g_module_error());
50                return FALSE;
51        }
52
53        if(!g_module_symbol(mod,"init_plugin",(gpointer *) &init_function)) {
54                log_message(LOGLVL_WARNING, "Can't find function `init_plugin' in `%s'\n", path);
55                return FALSE;
56        }
57
58        init_function();
59
60        return TRUE;
61}
62
63void load_plugins(void)
64{
65        GDir *dir;
66        GError *error = NULL;
67
68        dir = g_dir_open(global.conf->plugindir, 0, &error);
69
70        if (dir) {
71                const gchar *entry;
72                char *path;
73
74                while ((entry = g_dir_read_name(dir))) {
75                        path = g_build_filename(global.conf->plugindir, entry, NULL);
76                        if(!path) {
77                                log_message(LOGLVL_WARNING, "Can't build path for %s\n", entry);
78                                continue;
79                        }
80
81                        load_plugin(path);
82
83                        g_free(path);
84                }
85
86                g_dir_close(dir);
87        }
88}
89#endif
90
91GList *protocols = NULL;
92 
93void register_protocol (struct prpl *p)
94{
95        int i;
96        gboolean refused = global.conf->protocols != NULL;
97 
98        for (i = 0; global.conf->protocols && global.conf->protocols[i]; i++)
99        {
100                if (g_strcasecmp(p->name, global.conf->protocols[i]) == 0)
101                        refused = FALSE;
102        }
103
104        if (refused)
105                log_message(LOGLVL_WARNING, "Protocol %s disabled\n", p->name);
106        else
107                protocols = g_list_append(protocols, p);
108}
109
110struct prpl *find_protocol(const char *name)
111{
112        GList *gl;
113       
114        for( gl = protocols; gl; gl = gl->next )
115        {
116                struct prpl *proto = gl->data;
117               
118                if( g_strcasecmp( proto->name, name ) == 0 )
119                        return proto;
120        }
121       
122        return NULL;
123}
124
125void nogaim_init()
126{
127        extern void msn_initmodule();
128        extern void oscar_initmodule();
129        extern void byahoo_initmodule();
130        extern void jabber_initmodule();
131        extern void twitter_initmodule();
132        extern void purple_initmodule();
133
134#ifdef WITH_MSN
135        msn_initmodule();
136#endif
137
138#ifdef WITH_OSCAR
139        oscar_initmodule();
140#endif
141       
142#ifdef WITH_YAHOO
143        byahoo_initmodule();
144#endif
145       
146#ifdef WITH_JABBER
147        jabber_initmodule();
148#endif
149
150#ifdef WITH_TWITTER
151        twitter_initmodule();
152#endif
153
154#ifdef WITH_PURPLE
155        purple_initmodule();
156#endif
157
158#ifdef WITH_PLUGINS
159        load_plugins();
160#endif
161}
162
163GSList *get_connections() { return connections; }
164
165struct im_connection *imcb_new( account_t *acc )
166{
167        struct im_connection *ic;
168       
169        ic = g_new0( struct im_connection, 1 );
170       
171        ic->bee = acc->bee;
172        ic->acc = acc;
173        acc->ic = ic;
174       
175        connections = g_slist_append( connections, ic );
176       
177        return( ic );
178}
179
180void imc_free( struct im_connection *ic )
181{
182        account_t *a;
183       
184        /* Destroy the pointer to this connection from the account list */
185        for( a = ic->bee->accounts; a; a = a->next )
186                if( a->ic == ic )
187                {
188                        a->ic = NULL;
189                        break;
190                }
191       
192        connections = g_slist_remove( connections, ic );
193        g_free( ic );
194}
195
196static void serv_got_crap( struct im_connection *ic, char *format, ... )
197{
198        va_list params;
199        char *text;
200        account_t *a;
201       
202        va_start( params, format );
203        text = g_strdup_vprintf( format, params );
204        va_end( params );
205
206        if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) ||
207            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) )
208                strip_html( text );
209       
210        /* Try to find a different connection on the same protocol. */
211        for( a = ic->bee->accounts; a; a = a->next )
212                if( a->prpl == ic->acc->prpl && a->ic != ic )
213                        break;
214       
215        /* If we found one, include the screenname in the message. */
216        if( a )
217                /* FIXME(wilmer): ui_log callback or so */
218                irc_usermsg( ic->bee->ui_data, "%s(%s) - %s", ic->acc->prpl->name, ic->acc->user, text );
219        else
220                irc_usermsg( ic->bee->ui_data, "%s - %s", ic->acc->prpl->name, text );
221       
222        g_free( text );
223}
224
225void imcb_log( struct im_connection *ic, char *format, ... )
226{
227        va_list params;
228        char *text;
229       
230        va_start( params, format );
231        text = g_strdup_vprintf( format, params );
232        va_end( params );
233       
234        if( ic->flags & OPT_LOGGED_IN )
235                serv_got_crap( ic, "%s", text );
236        else
237                serv_got_crap( ic, "Logging in: %s", text );
238       
239        g_free( text );
240}
241
242void imcb_error( struct im_connection *ic, char *format, ... )
243{
244        va_list params;
245        char *text;
246       
247        va_start( params, format );
248        text = g_strdup_vprintf( format, params );
249        va_end( params );
250       
251        if( ic->flags & OPT_LOGGED_IN )
252                serv_got_crap( ic, "Error: %s", text );
253        else
254                serv_got_crap( ic, "Couldn't log in: %s", text );
255       
256        g_free( text );
257}
258
259static gboolean send_keepalive( gpointer d, gint fd, b_input_condition cond )
260{
261        struct im_connection *ic = d;
262       
263        if( ic->acc->prpl->keepalive )
264                ic->acc->prpl->keepalive( ic );
265       
266        return TRUE;
267}
268
269void imcb_connected( struct im_connection *ic )
270{
271        /* MSN servers sometimes redirect you to a different server and do
272           the whole login sequence again, so these "late" calls to this
273           function should be handled correctly. (IOW, ignored) */
274        if( ic->flags & OPT_LOGGED_IN )
275                return;
276       
277        imcb_log( ic, "Logged in" );
278       
279        ic->keepalive = b_timeout_add( 60000, send_keepalive, ic );
280        ic->flags |= OPT_LOGGED_IN;
281       
282        /* Necessary to send initial presence status, even if we're not away. */
283        imc_away_send_update( ic );
284       
285        /* Apparently we're connected successfully, so reset the
286           exponential backoff timer. */
287        ic->acc->auto_reconnect_delay = 0;
288       
289        if( ic->bee->ui->imc_connected )
290                ic->bee->ui->imc_connected( ic );
291       
292        /*
293        for( c = irc->chatrooms; c; c = c->next )
294        {
295                if( c->acc != ic->acc )
296                        continue;
297               
298                if( set_getbool( &c->set, "auto_join" ) )
299                        chat_join( irc, c, NULL );
300        }
301        */
302}
303
304gboolean auto_reconnect( gpointer data, gint fd, b_input_condition cond )
305{
306        account_t *a = data;
307       
308        a->reconnect = 0;
309        account_on( a->bee, a );
310       
311        return( FALSE );        /* Only have to run the timeout once */
312}
313
314void cancel_auto_reconnect( account_t *a )
315{
316        b_event_remove( a->reconnect );
317        a->reconnect = 0;
318}
319
320void imc_logout( struct im_connection *ic, int allow_reconnect )
321{
322        bee_t *bee = ic->bee;
323        account_t *a;
324        GSList *l;
325        int delay;
326       
327        /* Nested calls might happen sometimes, this is probably the best
328           place to catch them. */
329        if( ic->flags & OPT_LOGGING_OUT )
330                return;
331        else
332                ic->flags |= OPT_LOGGING_OUT;
333       
334        if( ic->bee->ui->imc_disconnected )
335                ic->bee->ui->imc_disconnected( ic );
336       
337        imcb_log( ic, "Signing off.." );
338       
339        b_event_remove( ic->keepalive );
340        ic->keepalive = 0;
341        ic->acc->prpl->logout( ic );
342        b_event_remove( ic->inpa );
343       
344        g_free( ic->away );
345        ic->away = NULL;
346       
347        for( l = bee->users; l; )
348        {
349                bee_user_t *bu = l->data;
350                GSList *next = l->next;
351               
352                if( bu->ic == ic )
353                        bee_user_free( bee, bu );
354               
355                l = next;
356        }
357       
358        query_del_by_conn( (irc_t*) ic->bee->ui_data, ic );
359       
360        for( a = bee->accounts; a; a = a->next )
361                if( a->ic == ic )
362                        break;
363       
364        if( !a )
365        {
366                /* Uhm... This is very sick. */
367        }
368        else if( allow_reconnect && set_getbool( &bee->set, "auto_reconnect" ) &&
369                 set_getbool( &a->set, "auto_reconnect" ) &&
370                 ( delay = account_reconnect_delay( a ) ) > 0 )
371        {
372                imcb_log( ic, "Reconnecting in %d seconds..", delay );
373                a->reconnect = b_timeout_add( delay * 1000, auto_reconnect, a );
374        }
375       
376        imc_free( ic );
377}
378
379void imcb_ask( struct im_connection *ic, char *msg, void *data,
380               query_callback doit, query_callback dont )
381{
382        query_add( (irc_t *) ic->bee->ui_data, ic, msg, doit, dont, g_free, data );
383}
384
385void imcb_add_buddy( struct im_connection *ic, const char *handle, const char *group )
386{
387        bee_user_t *bu;
388        bee_t *bee = ic->bee;
389       
390        if( !( bu = bee_user_by_handle( bee, ic, handle ) ) )
391                bu = bee_user_new( bee, ic, handle, 0 );
392       
393        bu->group = bee_group_by_name( bee, group, TRUE );
394       
395        if( bee->ui->user_group )
396                bee->ui->user_group( bee, bu );
397}
398
399void imcb_rename_buddy( struct im_connection *ic, const char *handle, const char *fullname )
400{
401        bee_t *bee = ic->bee;
402        bee_user_t *bu = bee_user_by_handle( bee, ic, handle );
403       
404        if( !bu || !fullname ) return;
405       
406        if( !bu->fullname || strcmp( bu->fullname, fullname ) != 0 )
407        {
408                g_free( bu->fullname );
409                bu->fullname = g_strdup( fullname );
410               
411                if( bee->ui->user_fullname )
412                        bee->ui->user_fullname( bee, bu );
413        }
414}
415
416void imcb_remove_buddy( struct im_connection *ic, const char *handle, char *group )
417{
418        bee_user_free( ic->bee, bee_user_by_handle( ic->bee, ic, handle ) );
419}
420
421/* Mainly meant for ICQ (and now also for Jabber conferences) to allow IM
422   modules to suggest a nickname for a handle. */
423void imcb_buddy_nick_hint( struct im_connection *ic, const char *handle, const char *nick )
424{
425        bee_t *bee = ic->bee;
426        bee_user_t *bu = bee_user_by_handle( bee, ic, handle );
427       
428        if( !bu || !nick ) return;
429       
430        g_free( bu->nick );
431        bu->nick = g_strdup( nick );
432       
433        if( bee->ui->user_nick_hint )
434                bee->ui->user_nick_hint( bee, bu, nick );
435}
436
437
438struct imcb_ask_cb_data
439{
440        struct im_connection *ic;
441        char *handle;
442};
443
444static void imcb_ask_auth_cb_no( void *data )
445{
446        struct imcb_ask_cb_data *cbd = data;
447       
448        cbd->ic->acc->prpl->auth_deny( cbd->ic, cbd->handle );
449       
450        g_free( cbd->handle );
451        g_free( cbd );
452}
453
454static void imcb_ask_auth_cb_yes( void *data )
455{
456        struct imcb_ask_cb_data *cbd = data;
457       
458        cbd->ic->acc->prpl->auth_allow( cbd->ic, cbd->handle );
459       
460        g_free( cbd->handle );
461        g_free( cbd );
462}
463
464void imcb_ask_auth( struct im_connection *ic, const char *handle, const char *realname )
465{
466        struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 );
467        char *s, *realname_ = NULL;
468       
469        if( realname != NULL )
470                realname_ = g_strdup_printf( " (%s)", realname );
471       
472        s = g_strdup_printf( "The user %s%s wants to add you to his/her buddy list.",
473                             handle, realname_ ?: "" );
474       
475        g_free( realname_ );
476       
477        data->ic = ic;
478        data->handle = g_strdup( handle );
479        query_add( (irc_t *) ic->bee->ui_data, ic, s,
480                   imcb_ask_auth_cb_yes, imcb_ask_auth_cb_no, g_free, data );
481}
482
483
484static void imcb_ask_add_cb_no( void *data )
485{
486        g_free( ((struct imcb_ask_cb_data*)data)->handle );
487        g_free( data );
488}
489
490static void imcb_ask_add_cb_yes( void *data )
491{
492        struct imcb_ask_cb_data *cbd = data;
493       
494        cbd->ic->acc->prpl->add_buddy( cbd->ic, cbd->handle, NULL );
495       
496        return imcb_ask_add_cb_no( data );
497}
498
499void imcb_ask_add( struct im_connection *ic, const char *handle, const char *realname )
500{
501        struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 );
502        char *s;
503       
504        /* TODO: Make a setting for this! */
505        if( bee_user_by_handle( ic->bee, ic, handle ) != NULL )
506                return;
507       
508        s = g_strdup_printf( "The user %s is not in your buddy list yet. Do you want to add him/her now?", handle );
509       
510        data->ic = ic;
511        data->handle = g_strdup( handle );
512        query_add( (irc_t *) ic->bee->ui_data, ic, s,
513                   imcb_ask_add_cb_yes, imcb_ask_add_cb_no, g_free, data );
514}
515
516struct bee_user *imcb_buddy_by_handle( struct im_connection *ic, const char *handle )
517{
518        return bee_user_by_handle( ic->bee, ic, handle );
519}
520
521/* The plan is to not allow straight calls to prpl functions anymore, but do
522   them all from some wrappers. We'll start to define some down here: */
523
524int imc_chat_msg( struct groupchat *c, char *msg, int flags )
525{
526        char *buf = NULL;
527       
528        if( ( c->ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
529        {
530                buf = escape_html( msg );
531                msg = buf;
532        }
533       
534        c->ic->acc->prpl->chat_msg( c, msg, flags );
535        g_free( buf );
536       
537        return 1;
538}
539
540static char *imc_away_state_find( GList *gcm, char *away, char **message );
541
542int imc_away_send_update( struct im_connection *ic )
543{
544        char *away, *msg = NULL;
545       
546        if( ic->acc->prpl->away_states == NULL ||
547            ic->acc->prpl->set_away == NULL )
548                return 0;
549       
550        away = set_getstr( &ic->acc->set, "away" ) ?
551             : set_getstr( &ic->bee->set, "away" );
552        if( away && *away )
553        {
554                GList *m = ic->acc->prpl->away_states( ic );
555                msg = ic->acc->flags & ACC_FLAG_AWAY_MESSAGE ? away : NULL;
556                away = imc_away_state_find( m, away, &msg ) ? : m->data;
557        }
558        else if( ic->acc->flags & ACC_FLAG_STATUS_MESSAGE )
559        {
560                away = NULL;
561                msg = set_getstr( &ic->acc->set, "status" ) ?
562                    : set_getstr( &ic->bee->set, "status" );
563        }
564       
565        ic->acc->prpl->set_away( ic, away, msg );
566       
567        return 1;
568}
569
570static char *imc_away_alias_list[8][5] =
571{
572        { "Away from computer", "Away", "Extended away", NULL },
573        { "NA", "N/A", "Not available", NULL },
574        { "Busy", "Do not disturb", "DND", "Occupied", NULL },
575        { "Be right back", "BRB", NULL },
576        { "On the phone", "Phone", "On phone", NULL },
577        { "Out to lunch", "Lunch", "Food", NULL },
578        { "Invisible", "Hidden" },
579        { NULL }
580};
581
582static char *imc_away_state_find( GList *gcm, char *away, char **message )
583{
584        GList *m;
585        int i, j;
586       
587        for( m = gcm; m; m = m->next )
588                if( g_strncasecmp( m->data, away, strlen( m->data ) ) == 0 )
589                {
590                        /* At least the Yahoo! module works better if message
591                           contains no data unless it adds something to what
592                           we have in state already. */
593                        if( strlen( m->data ) == strlen( away ) )
594                                *message = NULL;
595                       
596                        return m->data;
597                }
598       
599        for( i = 0; *imc_away_alias_list[i]; i ++ )
600        {
601                int keep_message;
602               
603                for( j = 0; imc_away_alias_list[i][j]; j ++ )
604                        if( g_strncasecmp( away, imc_away_alias_list[i][j], strlen( imc_away_alias_list[i][j] ) ) == 0 )
605                        {
606                                keep_message = strlen( away ) != strlen( imc_away_alias_list[i][j] );
607                                break;
608                        }
609               
610                if( !imc_away_alias_list[i][j] )        /* If we reach the end, this row */
611                        continue;                       /* is not what we want. Next!    */
612               
613                /* Now find an entry in this row which exists in gcm */
614                for( j = 0; imc_away_alias_list[i][j]; j ++ )
615                {
616                        for( m = gcm; m; m = m->next )
617                                if( g_strcasecmp( imc_away_alias_list[i][j], m->data ) == 0 )
618                                {
619                                        if( !keep_message )
620                                                *message = NULL;
621                                       
622                                        return imc_away_alias_list[i][j];
623                                }
624                }
625               
626                /* No need to look further, apparently this state doesn't
627                   have any good alias for this protocol. */
628                break;
629        }
630       
631        return NULL;
632}
633
634void imc_add_allow( struct im_connection *ic, char *handle )
635{
636        if( g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL )
637        {
638                ic->permit = g_slist_prepend( ic->permit, g_strdup( handle ) );
639        }
640       
641        ic->acc->prpl->add_permit( ic, handle );
642}
643
644void imc_rem_allow( struct im_connection *ic, char *handle )
645{
646        GSList *l;
647       
648        if( ( l = g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) )
649        {
650                g_free( l->data );
651                ic->permit = g_slist_delete_link( ic->permit, l );
652        }
653       
654        ic->acc->prpl->rem_permit( ic, handle );
655}
656
657void imc_add_block( struct im_connection *ic, char *handle )
658{
659        if( g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL )
660        {
661                ic->deny = g_slist_prepend( ic->deny, g_strdup( handle ) );
662        }
663       
664        ic->acc->prpl->add_deny( ic, handle );
665}
666
667void imc_rem_block( struct im_connection *ic, char *handle )
668{
669        GSList *l;
670       
671        if( ( l = g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) )
672        {
673                g_free( l->data );
674                ic->deny = g_slist_delete_link( ic->deny, l );
675        }
676       
677        ic->acc->prpl->rem_deny( ic, handle );
678}
679
680void imcb_clean_handle( struct im_connection *ic, char *handle )
681{
682        /* Accepts a handle and does whatever is necessary to make it
683           BitlBee-friendly. Currently this means removing everything
684           outside 33-127 (ASCII printable excl spaces), @ (only one
685           is allowed) and ! and : */
686        char out[strlen(handle)+1];
687        int s, d;
688       
689        s = d = 0;
690        while( handle[s] )
691        {
692                if( handle[s] > ' ' && handle[s] != '!' && handle[s] != ':' &&
693                    ( handle[s] & 0x80 ) == 0 )
694                {
695                        if( handle[s] == '@' )
696                        {
697                                /* See if we got an @ already? */
698                                out[d] = 0;
699                                if( strchr( out, '@' ) )
700                                        continue;
701                        }
702                       
703                        out[d++] = handle[s];
704                }
705                s ++;
706        }
707        out[d] = handle[s];
708       
709        strcpy( handle, out );
710}
Note: See TracBrowser for help on using the repository browser.