source: protocols/nogaim.c @ 0d9d53e

Last change on this file since 0d9d53e was 7e83e8e4, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-05T00:35:17Z

Inform the UI about group changes. This is important if the user has
group channels.

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