source: protocols/nogaim.c @ 67ebc8e

Last change on this file since 67ebc8e was e132b60, checked in by Wilmer van der Gaast <wilmer@…>, at 2012-11-11T23:32:47Z

Extend keepalive code to time out connections when pings don't get
acknowledged, using this for Twitter streams and MSN so far.

  • Property mode set to 100644
File size: 17.6 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_rootmsg( ic->bee->ui_data, "%s - %s", ic->acc->tag, text );
219        else
220                irc_rootmsg( 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, "Login error: %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->flags & OPT_PONGS ) && !( ic->flags & OPT_PONGED ) )
264        {
265                /* This protocol is expected to ack keepalives and hasn't
266                   since the last time we were here. */
267                imcb_error( ic, "Connection timeout" );
268                imc_logout( ic, TRUE );
269                return FALSE;
270        }
271        ic->flags &= ~OPT_PONGED;
272       
273        if( ic->acc->prpl->keepalive )
274                ic->acc->prpl->keepalive( ic );
275       
276        return TRUE;
277}
278
279void start_keepalives( struct im_connection *ic, int interval )
280{
281        b_event_remove( ic->keepalive );
282        ic->keepalive = b_timeout_add( interval, send_keepalive, ic );
283       
284        /* Connecting successfully counts as a first successful pong. */
285        if( ic->flags & OPT_PONGS )
286                ic->flags |= OPT_PONGED;
287}
288
289void imcb_connected( struct im_connection *ic )
290{
291        /* MSN servers sometimes redirect you to a different server and do
292           the whole login sequence again, so these "late" calls to this
293           function should be handled correctly. (IOW, ignored) */
294        if( ic->flags & OPT_LOGGED_IN )
295                return;
296       
297        imcb_log( ic, "Logged in" );
298       
299        ic->flags |= OPT_LOGGED_IN;
300        if( !ic->keepalive )
301                start_keepalives( ic, 60000 );
302       
303        /* Necessary to send initial presence status, even if we're not away. */
304        imc_away_send_update( ic );
305       
306        /* Apparently we're connected successfully, so reset the
307           exponential backoff timer. */
308        ic->acc->auto_reconnect_delay = 0;
309       
310        if( ic->bee->ui->imc_connected )
311                ic->bee->ui->imc_connected( ic );
312}
313
314gboolean auto_reconnect( gpointer data, gint fd, b_input_condition cond )
315{
316        account_t *a = data;
317       
318        a->reconnect = 0;
319        account_on( a->bee, a );
320       
321        return( FALSE );        /* Only have to run the timeout once */
322}
323
324void cancel_auto_reconnect( account_t *a )
325{
326        b_event_remove( a->reconnect );
327        a->reconnect = 0;
328}
329
330void imc_logout( struct im_connection *ic, int allow_reconnect )
331{
332        bee_t *bee = ic->bee;
333        account_t *a;
334        GSList *l;
335        int delay;
336       
337        /* Nested calls might happen sometimes, this is probably the best
338           place to catch them. */
339        if( ic->flags & OPT_LOGGING_OUT )
340                return;
341        else
342                ic->flags |= OPT_LOGGING_OUT;
343       
344        if( ic->bee->ui->imc_disconnected )
345                ic->bee->ui->imc_disconnected( ic );
346       
347        imcb_log( ic, "Signing off.." );
348       
349        /* TBH I don't remember anymore why I didn't just use ic->acc... */
350        for( a = bee->accounts; a; a = a->next )
351                if( a->ic == ic )
352                        break;
353       
354        if( a && !allow_reconnect && !( ic->flags & OPT_LOGGED_IN ) &&
355            set_getbool( &a->set, "oauth" ) )
356        {
357                /* If this account supports OAuth, we're not logged in yet and
358                   not allowed to retry, assume there were auth issues. Give a
359                   helpful message on what might be necessary to fix this. */
360                imcb_log( ic, "If you're having problems logging in, try re-requesting "
361                          "an OAuth token: account %s set password \"\"", a->tag );
362        }
363       
364        for( l = bee->users; l; )
365        {
366                bee_user_t *bu = l->data;
367                GSList *next = l->next;
368               
369                if( bu->ic == ic )
370                        bee_user_free( bee, bu );
371               
372                l = next;
373        }
374       
375        b_event_remove( ic->keepalive );
376        ic->keepalive = 0;
377        ic->acc->prpl->logout( ic );
378        b_event_remove( ic->inpa );
379       
380        g_free( ic->away );
381        ic->away = NULL;
382       
383        query_del_by_conn( (irc_t*) ic->bee->ui_data, ic );
384       
385        if( !a )
386        {
387                /* Uhm... This is very sick. */
388        }
389        else if( allow_reconnect && set_getbool( &bee->set, "auto_reconnect" ) &&
390                 set_getbool( &a->set, "auto_reconnect" ) &&
391                 ( delay = account_reconnect_delay( a ) ) > 0 )
392        {
393                imcb_log( ic, "Reconnecting in %d seconds..", delay );
394                a->reconnect = b_timeout_add( delay * 1000, auto_reconnect, a );
395        }
396       
397        imc_free( ic );
398}
399
400void imcb_ask( struct im_connection *ic, char *msg, void *data,
401               query_callback doit, query_callback dont )
402{
403        query_add( (irc_t *) ic->bee->ui_data, ic, msg, doit, dont, g_free, data );
404}
405
406void imcb_ask_with_free( struct im_connection *ic, char *msg, void *data,
407                         query_callback doit, query_callback dont, query_callback myfree )
408{
409        query_add( (irc_t *) ic->bee->ui_data, ic, msg, doit, dont, myfree, data );
410}
411
412void imcb_add_buddy( struct im_connection *ic, const char *handle, const char *group )
413{
414        bee_user_t *bu;
415        bee_t *bee = ic->bee;
416        bee_group_t *oldg;
417       
418        if( !( bu = bee_user_by_handle( bee, ic, handle ) ) )
419                bu = bee_user_new( bee, ic, handle, 0 );
420       
421        oldg = bu->group;
422        bu->group = bee_group_by_name( bee, group, TRUE );
423       
424        if( bee->ui->user_group && bu->group != oldg )
425                bee->ui->user_group( bee, bu );
426}
427
428void imcb_rename_buddy( struct im_connection *ic, const char *handle, const char *fullname )
429{
430        bee_t *bee = ic->bee;
431        bee_user_t *bu = bee_user_by_handle( bee, ic, handle );
432       
433        if( !bu || !fullname ) return;
434       
435        if( !bu->fullname || strcmp( bu->fullname, fullname ) != 0 )
436        {
437                g_free( bu->fullname );
438                bu->fullname = g_strdup( fullname );
439               
440                if( bee->ui->user_fullname )
441                        bee->ui->user_fullname( bee, bu );
442        }
443}
444
445void imcb_remove_buddy( struct im_connection *ic, const char *handle, char *group )
446{
447        bee_user_free( ic->bee, bee_user_by_handle( ic->bee, ic, handle ) );
448}
449
450/* Mainly meant for ICQ (and now also for Jabber conferences) to allow IM
451   modules to suggest a nickname for a handle. */
452void imcb_buddy_nick_hint( struct im_connection *ic, const char *handle, const char *nick )
453{
454        bee_t *bee = ic->bee;
455        bee_user_t *bu = bee_user_by_handle( bee, ic, handle );
456       
457        if( !bu || !nick ) return;
458       
459        g_free( bu->nick );
460        bu->nick = g_strdup( nick );
461       
462        if( bee->ui->user_nick_hint )
463                bee->ui->user_nick_hint( bee, bu, nick );
464}
465
466
467struct imcb_ask_cb_data
468{
469        struct im_connection *ic;
470        char *handle;
471};
472
473static void imcb_ask_auth_cb_no( void *data )
474{
475        struct imcb_ask_cb_data *cbd = data;
476       
477        cbd->ic->acc->prpl->auth_deny( cbd->ic, cbd->handle );
478       
479        g_free( cbd->handle );
480        g_free( cbd );
481}
482
483static void imcb_ask_auth_cb_yes( void *data )
484{
485        struct imcb_ask_cb_data *cbd = data;
486       
487        cbd->ic->acc->prpl->auth_allow( cbd->ic, cbd->handle );
488       
489        g_free( cbd->handle );
490        g_free( cbd );
491}
492
493void imcb_ask_auth( struct im_connection *ic, const char *handle, const char *realname )
494{
495        struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 );
496        char *s, *realname_ = NULL;
497       
498        if( realname != NULL )
499                realname_ = g_strdup_printf( " (%s)", realname );
500       
501        s = g_strdup_printf( "The user %s%s wants to add you to his/her buddy list.",
502                             handle, realname_ ? realname_ : "" );
503       
504        g_free( realname_ );
505       
506        data->ic = ic;
507        data->handle = g_strdup( handle );
508        query_add( (irc_t *) ic->bee->ui_data, ic, s,
509                   imcb_ask_auth_cb_yes, imcb_ask_auth_cb_no, g_free, data );
510}
511
512
513static void imcb_ask_add_cb_no( void *data )
514{
515        g_free( ((struct imcb_ask_cb_data*)data)->handle );
516        g_free( data );
517}
518
519static void imcb_ask_add_cb_yes( void *data )
520{
521        struct imcb_ask_cb_data *cbd = data;
522       
523        cbd->ic->acc->prpl->add_buddy( cbd->ic, cbd->handle, NULL );
524       
525        imcb_ask_add_cb_no( data );
526}
527
528void imcb_ask_add( struct im_connection *ic, const char *handle, const char *realname )
529{
530        struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 );
531        char *s;
532       
533        /* TODO: Make a setting for this! */
534        if( bee_user_by_handle( ic->bee, ic, handle ) != NULL )
535                return;
536       
537        s = g_strdup_printf( "The user %s is not in your buddy list yet. Do you want to add him/her now?", handle );
538       
539        data->ic = ic;
540        data->handle = g_strdup( handle );
541        query_add( (irc_t *) ic->bee->ui_data, ic, s,
542                   imcb_ask_add_cb_yes, imcb_ask_add_cb_no, g_free, data );
543}
544
545struct bee_user *imcb_buddy_by_handle( struct im_connection *ic, const char *handle )
546{
547        return bee_user_by_handle( ic->bee, ic, handle );
548}
549
550/* The plan is to not allow straight calls to prpl functions anymore, but do
551   them all from some wrappers. We'll start to define some down here: */
552
553int imc_chat_msg( struct groupchat *c, char *msg, int flags )
554{
555        char *buf = NULL;
556       
557        if( ( c->ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
558        {
559                buf = escape_html( msg );
560                msg = buf;
561        }
562       
563        c->ic->acc->prpl->chat_msg( c, msg, flags );
564        g_free( buf );
565       
566        return 1;
567}
568
569static char *imc_away_state_find( GList *gcm, char *away, char **message );
570
571int imc_away_send_update( struct im_connection *ic )
572{
573        char *away, *msg = NULL;
574       
575        if( ic->acc->prpl->away_states == NULL ||
576            ic->acc->prpl->set_away == NULL )
577                return 0;
578       
579        away = set_getstr( &ic->acc->set, "away" ) ?
580             : set_getstr( &ic->bee->set, "away" );
581        if( away && *away )
582        {
583                GList *m = ic->acc->prpl->away_states( ic );
584                msg = ic->acc->flags & ACC_FLAG_AWAY_MESSAGE ? away : NULL;
585                away = imc_away_state_find( m, away, &msg ) ? : m->data;
586        }
587        else if( ic->acc->flags & ACC_FLAG_STATUS_MESSAGE )
588        {
589                away = NULL;
590                msg = set_getstr( &ic->acc->set, "status" ) ?
591                    : set_getstr( &ic->bee->set, "status" );
592        }
593       
594        ic->acc->prpl->set_away( ic, away, msg );
595       
596        return 1;
597}
598
599static char *imc_away_alias_list[8][5] =
600{
601        { "Away from computer", "Away", "Extended away", NULL },
602        { "NA", "N/A", "Not available", NULL },
603        { "Busy", "Do not disturb", "DND", "Occupied", NULL },
604        { "Be right back", "BRB", NULL },
605        { "On the phone", "Phone", "On phone", NULL },
606        { "Out to lunch", "Lunch", "Food", NULL },
607        { "Invisible", "Hidden" },
608        { NULL }
609};
610
611static char *imc_away_state_find( GList *gcm, char *away, char **message )
612{
613        GList *m;
614        int i, j;
615       
616        for( m = gcm; m; m = m->next )
617                if( g_strncasecmp( m->data, away, strlen( m->data ) ) == 0 )
618                {
619                        /* At least the Yahoo! module works better if message
620                           contains no data unless it adds something to what
621                           we have in state already. */
622                        if( strlen( m->data ) == strlen( away ) )
623                                *message = NULL;
624                       
625                        return m->data;
626                }
627       
628        for( i = 0; *imc_away_alias_list[i]; i ++ )
629        {
630                int keep_message;
631               
632                for( j = 0; imc_away_alias_list[i][j]; j ++ )
633                        if( g_strncasecmp( away, imc_away_alias_list[i][j], strlen( imc_away_alias_list[i][j] ) ) == 0 )
634                        {
635                                keep_message = strlen( away ) != strlen( imc_away_alias_list[i][j] );
636                                break;
637                        }
638               
639                if( !imc_away_alias_list[i][j] )        /* If we reach the end, this row */
640                        continue;                       /* is not what we want. Next!    */
641               
642                /* Now find an entry in this row which exists in gcm */
643                for( j = 0; imc_away_alias_list[i][j]; j ++ )
644                {
645                        for( m = gcm; m; m = m->next )
646                                if( g_strcasecmp( imc_away_alias_list[i][j], m->data ) == 0 )
647                                {
648                                        if( !keep_message )
649                                                *message = NULL;
650                                       
651                                        return imc_away_alias_list[i][j];
652                                }
653                }
654               
655                /* No need to look further, apparently this state doesn't
656                   have any good alias for this protocol. */
657                break;
658        }
659       
660        return NULL;
661}
662
663void imc_add_allow( struct im_connection *ic, char *handle )
664{
665        if( g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL )
666        {
667                ic->permit = g_slist_prepend( ic->permit, g_strdup( handle ) );
668        }
669       
670        ic->acc->prpl->add_permit( ic, handle );
671}
672
673void imc_rem_allow( struct im_connection *ic, char *handle )
674{
675        GSList *l;
676       
677        if( ( l = g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) )
678        {
679                g_free( l->data );
680                ic->permit = g_slist_delete_link( ic->permit, l );
681        }
682       
683        ic->acc->prpl->rem_permit( ic, handle );
684}
685
686void imc_add_block( struct im_connection *ic, char *handle )
687{
688        if( g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL )
689        {
690                ic->deny = g_slist_prepend( ic->deny, g_strdup( handle ) );
691        }
692       
693        ic->acc->prpl->add_deny( ic, handle );
694}
695
696void imc_rem_block( struct im_connection *ic, char *handle )
697{
698        GSList *l;
699       
700        if( ( l = g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) )
701        {
702                g_free( l->data );
703                ic->deny = g_slist_delete_link( ic->deny, l );
704        }
705       
706        ic->acc->prpl->rem_deny( ic, handle );
707}
708
709void imcb_clean_handle( struct im_connection *ic, char *handle )
710{
711        /* Accepts a handle and does whatever is necessary to make it
712           BitlBee-friendly. Currently this means removing everything
713           outside 33-127 (ASCII printable excl spaces), @ (only one
714           is allowed) and ! and : */
715        char out[strlen(handle)+1];
716        int s, d;
717       
718        s = d = 0;
719        while( handle[s] )
720        {
721                if( handle[s] > ' ' && handle[s] != '!' && handle[s] != ':' &&
722                    ( handle[s] & 0x80 ) == 0 )
723                {
724                        if( handle[s] == '@' )
725                        {
726                                /* See if we got an @ already? */
727                                out[d] = 0;
728                                if( strchr( out, '@' ) )
729                                        continue;
730                        }
731                       
732                        out[d++] = handle[s];
733                }
734                s ++;
735        }
736        out[d] = handle[s];
737       
738        strcpy( handle, out );
739}
Note: See TracBrowser for help on using the repository browser.