source: protocols/nogaim.c @ 0e8b3e8

Last change on this file since 0e8b3e8 was 0e8b3e8, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-07T15:21:21Z

Changing away_devoice will change current voice statuses in all channels.

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