source: protocols/nogaim.c @ aef4828

Last change on this file since aef4828 was aef4828, checked in by Wilmer van der Gaast <wilmer@…>, at 2007-04-06T05:20:31Z

More cleanups, mainly in the callbacks. Replaced things like
do_error_dialog() and (set|hide)_login_progress(_error)?() with things
that hopefully make more sense.

Although it's still not really great...

  • Property mode set to 100644
File size: 25.1 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2006 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 "nogaim.h"
36#include <ctype.h>
37
38static int remove_chat_buddy_silent( struct groupchat *b, char *handle );
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", path);
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
92/* nogaim.c */
93
94GList *protocols = NULL;
95 
96void register_protocol (struct prpl *p)
97{
98        protocols = g_list_append(protocols, p);
99}
100
101 
102struct prpl *find_protocol(const char *name)
103{
104        GList *gl;
105        for (gl = protocols; gl; gl = gl->next) 
106        {
107                struct prpl *proto = gl->data;
108                if(!g_strcasecmp(proto->name, name)) 
109                        return proto;
110        }
111        return NULL;
112}
113
114/* nogaim.c */
115void nogaim_init()
116{
117        extern void msn_initmodule();
118        extern void oscar_initmodule();
119        extern void byahoo_initmodule();
120        extern void jabber_initmodule();
121
122#ifdef WITH_MSN
123        msn_initmodule();
124#endif
125
126#ifdef WITH_OSCAR
127        oscar_initmodule();
128#endif
129       
130#ifdef WITH_YAHOO
131        byahoo_initmodule();
132#endif
133       
134#ifdef WITH_JABBER
135        jabber_initmodule();
136#endif
137
138#ifdef WITH_PLUGINS
139        load_plugins();
140#endif
141}
142
143GSList *get_connections() { return connections; }
144
145/* multi.c */
146
147struct im_connection *imc_new( account_t *acc )
148{
149        struct im_connection *ic;
150       
151        ic = g_new0( struct im_connection, 1 );
152       
153        /* Maybe we should get rid of this memory waste later. ;-) */
154        g_snprintf( ic->username, sizeof( ic->username ), "%s", acc->user );
155        g_snprintf( ic->password, sizeof( ic->password ), "%s", acc->pass );
156       
157        ic->irc = acc->irc;
158        ic->acc = acc;
159        acc->ic = ic;
160       
161        connections = g_slist_append( connections, ic );
162       
163        return( ic );
164}
165
166void imc_free( struct im_connection *ic )
167{
168        account_t *a;
169       
170        /* Destroy the pointer to this connection from the account list */
171        for( a = ic->irc->accounts; a; a = a->next )
172                if( a->ic == ic )
173                {
174                        a->ic = NULL;
175                        break;
176                }
177       
178        connections = g_slist_remove( connections, ic );
179        g_free( ic );
180}
181
182static void serv_got_crap( struct im_connection *ic, char *format, ... )
183{
184        va_list params;
185        char *text;
186        account_t *a;
187       
188        va_start( params, format );
189        text = g_strdup_vprintf( format, params );
190        va_end( params );
191
192        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
193            ( ( ic->flags & OPT_CONN_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
194                strip_html( text );
195       
196        /* Try to find a different connection on the same protocol. */
197        for( a = ic->irc->accounts; a; a = a->next )
198                if( a->prpl == ic->acc->prpl && a->ic != ic )
199                        break;
200       
201        /* If we found one, include the screenname in the message. */
202        if( a )
203                irc_usermsg( ic->irc, "%s(%s) - %s", ic->acc->prpl->name, ic->username, text );
204        else
205                irc_usermsg( ic->irc, "%s - %s", ic->acc->prpl->name, text );
206       
207        g_free( text );
208}
209
210void imc_log( struct im_connection *ic, char *format, ... )
211{
212        va_list params;
213        char *text;
214       
215        va_start( params, format );
216        text = g_strdup_vprintf( format, params );
217        va_end( params );
218       
219        if( ic->flags & OPT_LOGGED_IN )
220                serv_got_crap( ic, "%s", text );
221        else
222                serv_got_crap( ic, "Logging in: %s", text );
223       
224        g_free( text );
225}
226
227void imc_error( struct im_connection *ic, char *format, ... )
228{
229        va_list params;
230        char *text;
231       
232        va_start( params, format );
233        text = g_strdup_vprintf( format, params );
234        va_end( params );
235       
236        if( ic->flags & OPT_LOGGED_IN )
237                serv_got_crap( ic, "Error: %s", text );
238        else
239                serv_got_crap( ic, "Couldn't log in: %s", text );
240       
241        g_free( text );
242}
243
244static gboolean send_keepalive( gpointer d, gint fd, b_input_condition cond )
245{
246        struct im_connection *ic = d;
247       
248        if( ic->acc->prpl->keepalive )
249                ic->acc->prpl->keepalive( ic );
250       
251        return TRUE;
252}
253
254void imc_connected( struct im_connection *ic )
255{
256        user_t *u;
257       
258        /* MSN servers sometimes redirect you to a different server and do
259           the whole login sequence again, so these "late" calls to this
260           function should be handled correctly. (IOW, ignored) */
261        if( ic->flags & OPT_LOGGED_IN )
262                return;
263       
264        u = user_find( ic->irc, ic->irc->nick );
265       
266        imc_log( ic, "Logged in" );
267       
268        ic->keepalive = b_timeout_add( 60000, send_keepalive, ic );
269        ic->flags |= OPT_LOGGED_IN;
270       
271        /* Also necessary when we're not away, at least for some of the
272           protocols. */
273        bim_set_away( ic, u->away );
274}
275
276gboolean auto_reconnect( gpointer data, gint fd, b_input_condition cond )
277{
278        account_t *a = data;
279       
280        a->reconnect = 0;
281        account_on( a->irc, a );
282       
283        return( FALSE );        /* Only have to run the timeout once */
284}
285
286void cancel_auto_reconnect( account_t *a )
287{
288        /* while( b_event_remove_by_data( (gpointer) a ) ); */
289        b_event_remove( a->reconnect );
290        a->reconnect = 0;
291}
292
293void imc_logout( struct im_connection *ic )
294{
295        irc_t *irc = ic->irc;
296        user_t *t, *u = irc->users;
297        account_t *a;
298       
299        /* Nested calls might happen sometimes, this is probably the best
300           place to catch them. */
301        if( ic->flags & OPT_LOGGING_OUT )
302                return;
303        else
304                ic->flags |= OPT_LOGGING_OUT;
305       
306        imc_log( ic, "Signing off.." );
307       
308        b_event_remove( ic->keepalive );
309        ic->keepalive = 0;
310        ic->acc->prpl->logout( ic );
311        b_event_remove( ic->inpa );
312       
313        while( u )
314        {
315                if( u->ic == ic )
316                {
317                        t = u->next;
318                        user_del( irc, u->nick );
319                        u = t;
320                }
321                else
322                        u = u->next;
323        }
324       
325        query_del_by_conn( ic->irc, ic );
326       
327        for( a = irc->accounts; a; a = a->next )
328                if( a->ic == ic )
329                        break;
330       
331        if( !a )
332        {
333                /* Uhm... This is very sick. */
334        }
335        else if( !ic->wants_to_die && set_getbool( &irc->set, "auto_reconnect" ) &&
336                 set_getbool( &a->set, "auto_reconnect" ) )
337        {
338                int delay = set_getint( &irc->set, "auto_reconnect_delay" );
339               
340                imc_log( ic, "Reconnecting in %d seconds..", delay );
341                a->reconnect = b_timeout_add( delay * 1000, auto_reconnect, a );
342        }
343       
344        imc_free( ic );
345}
346
347
348/* dialogs.c */
349
350void do_ask_dialog( struct im_connection *ic, char *msg, void *data, void *doit, void *dont )
351{
352        query_add( ic->irc, ic, msg, doit, dont, data );
353}
354
355
356/* list.c */
357
358void add_buddy( struct im_connection *ic, char *group, char *handle, char *realname )
359{
360        user_t *u;
361        char nick[MAX_NICK_LENGTH+1];
362        char *s;
363        irc_t *irc = ic->irc;
364       
365        if( set_getbool( &irc->set, "debug" ) && 0 ) /* This message is too useless */
366                imc_log( ic, "Receiving user add from handle: %s", handle );
367       
368        if( user_findhandle( ic, handle ) )
369        {
370                if( set_getbool( &irc->set, "debug" ) )
371                        imc_log( ic, "User already exists, ignoring add request: %s", handle );
372               
373                return;
374               
375                /* Buddy seems to exist already. Let's ignore this request then... */
376        }
377       
378        memset( nick, 0, MAX_NICK_LENGTH + 1 );
379        strcpy( nick, nick_get( ic->acc, handle, realname ) );
380       
381        u = user_add( ic->irc, nick );
382       
383        if( !realname || !*realname ) realname = nick;
384        u->realname = g_strdup( realname );
385       
386        if( ( s = strchr( handle, '@' ) ) )
387        {
388                u->host = g_strdup( s + 1 );
389                u->user = g_strndup( handle, s - handle );
390        }
391        else if( ic->acc->server )
392        {
393                char *colon;
394               
395                if( ( colon = strchr( ic->acc->server, ':' ) ) )
396                        u->host = g_strndup( ic->acc->server,
397                                             colon - ic->acc->server );
398                else
399                        u->host = g_strdup( ic->acc->server );
400               
401                u->user = g_strdup( handle );
402               
403                /* s/ /_/ ... important for AOL screennames */
404                for( s = u->user; *s; s ++ )
405                        if( *s == ' ' )
406                                *s = '_';
407        }
408        else
409        {
410                u->host = g_strdup( ic->acc->prpl->name );
411                u->user = g_strdup( handle );
412        }
413       
414        u->ic = ic;
415        u->handle = g_strdup( handle );
416        if( group ) u->group = g_strdup( group );
417        u->send_handler = buddy_send_handler;
418        u->last_typing_notice = 0;
419}
420
421struct buddy *find_buddy( struct im_connection *ic, char *handle )
422{
423        static struct buddy b[1];
424        user_t *u;
425       
426        u = user_findhandle( ic, handle );
427       
428        if( !u )
429                return( NULL );
430
431        memset( b, 0, sizeof( b ) );
432        strncpy( b->name, handle, 80 );
433        strncpy( b->show, u->realname, BUDDY_ALIAS_MAXLEN );
434        b->present = u->online;
435        b->ic = u->ic;
436       
437        return( b );
438}
439
440void signoff_blocked( struct im_connection *ic )
441{
442        return; /* Make all blocked users look invisible (TODO?) */
443}
444
445
446void serv_buddy_rename( struct im_connection *ic, char *handle, char *realname )
447{
448        user_t *u = user_findhandle( ic, handle );
449       
450        if( !u ) return;
451       
452        if( g_strcasecmp( u->realname, realname ) != 0 )
453        {
454                if( u->realname != u->nick ) g_free( u->realname );
455               
456                u->realname = g_strdup( realname );
457               
458                if( ( ic->flags & OPT_LOGGED_IN ) && set_getbool( &ic->irc->set, "display_namechanges" ) )
459                        imc_log( ic, "User `%s' changed name to `%s'", u->nick, u->realname );
460        }
461}
462
463
464/* prpl.c */
465
466struct show_got_added_data
467{
468        struct im_connection *ic;
469        char *handle;
470};
471
472void show_got_added_no( gpointer w, struct show_got_added_data *data )
473{
474        g_free( data->handle );
475        g_free( data );
476}
477
478void show_got_added_yes( gpointer w, struct show_got_added_data *data )
479{
480        data->ic->acc->prpl->add_buddy( data->ic, data->handle, NULL );
481        add_buddy( data->ic, NULL, data->handle, data->handle );
482       
483        return show_got_added_no( w, data );
484}
485
486void show_got_added( struct im_connection *ic, char *handle, const char *realname )
487{
488        struct show_got_added_data *data = g_new0( struct show_got_added_data, 1 );
489        char *s;
490       
491        /* TODO: Make a setting for this! */
492        if( user_findhandle( ic, handle ) != NULL )
493                return;
494       
495        s = g_strdup_printf( "The user %s is not in your buddy list yet. Do you want to add him/her now?", handle );
496       
497        data->ic = ic;
498        data->handle = g_strdup( handle );
499        query_add( ic->irc, ic, s, show_got_added_yes, show_got_added_no, data );
500}
501
502
503/* server.c */                   
504
505void serv_got_update( struct im_connection *ic, char *handle, int loggedin, int evil, time_t signon, time_t idle, int type, guint caps )
506{
507        user_t *u;
508        int oa, oo;
509       
510        u = user_findhandle( ic, handle );
511       
512        if( !u )
513        {
514                if( g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "add" ) == 0 )
515                {
516                        add_buddy( ic, NULL, handle, NULL );
517                        u = user_findhandle( ic, handle );
518                }
519                else
520                {
521                        if( set_getbool( &ic->irc->set, "debug" ) || g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "ignore" ) != 0 )
522                        {
523                                imc_log( ic, "serv_got_update() for handle %s:", handle );
524                                imc_log( ic, "loggedin = %d, type = %d", loggedin, type );
525                        }
526                       
527                        return;
528                }
529                /* Why did we have this here....
530                return; */
531        }
532       
533        oa = u->away != NULL;
534        oo = u->online;
535       
536        if( u->away )
537        {
538                g_free( u->away );
539                u->away = NULL;
540        }
541       
542        if( loggedin && !u->online )
543        {
544                irc_spawn( ic->irc, u );
545                u->online = 1;
546        }
547        else if( !loggedin && u->online )
548        {
549                struct groupchat *c;
550               
551                irc_kill( ic->irc, u );
552                u->online = 0;
553               
554                /* Remove him/her from the conversations to prevent PART messages after he/she QUIT already */
555                for( c = ic->conversations; c; c = c->next )
556                        remove_chat_buddy_silent( c, handle );
557        }
558       
559        if( ( type & UC_UNAVAILABLE ) && ( strcmp( ic->acc->prpl->name, "oscar" ) == 0 || strcmp( ic->acc->prpl->name, "icq" ) == 0 ) )
560        {
561                u->away = g_strdup( "Away" );
562        }
563        else if( ( type & UC_UNAVAILABLE ) && ( strcmp( ic->acc->prpl->name, "jabber" ) == 0 ) )
564        {
565                if( type & UC_DND )
566                        u->away = g_strdup( "Do Not Disturb" );
567                else if( type & UC_XA )
568                        u->away = g_strdup( "Extended Away" );
569                else // if( type & UC_AWAY )
570                        u->away = g_strdup( "Away" );
571        }
572        else if( ( type & UC_UNAVAILABLE ) && ic->acc->prpl->get_status_string )
573        {
574                u->away = g_strdup( ic->acc->prpl->get_status_string( ic, type ) );
575        }
576        else
577                u->away = NULL;
578       
579        /* LISPy... */
580        if( ( set_getbool( &ic->irc->set, "away_devoice" ) ) &&         /* Don't do a thing when user doesn't want it */
581            ( u->online ) &&                                            /* Don't touch offline people */
582            ( ( ( u->online != oo ) && !u->away ) ||                    /* Voice joining people */
583              ( ( u->online == oo ) && ( oa == !u->away ) ) ) )         /* (De)voice people changing state */
584        {
585                irc_write( ic->irc, ":%s MODE %s %cv %s", ic->irc->myhost,
586                                                                ic->irc->channel, u->away?'-':'+', u->nick );
587        }
588}
589
590void serv_got_im( struct im_connection *ic, char *handle, char *msg, guint32 flags, time_t mtime, gint len )
591{
592        irc_t *irc = ic->irc;
593        user_t *u;
594       
595        u = user_findhandle( ic, handle );
596       
597        if( !u )
598        {
599                char *h = set_getstr( &irc->set, "handle_unknown" );
600               
601                if( g_strcasecmp( h, "ignore" ) == 0 )
602                {
603                        if( set_getbool( &irc->set, "debug" ) )
604                                imc_log( ic, "Ignoring message from unknown handle %s", handle );
605                       
606                        return;
607                }
608                else if( g_strncasecmp( h, "add", 3 ) == 0 )
609                {
610                        int private = set_getbool( &irc->set, "private" );
611                       
612                        if( h[3] )
613                        {
614                                if( g_strcasecmp( h + 3, "_private" ) == 0 )
615                                        private = 1;
616                                else if( g_strcasecmp( h + 3, "_channel" ) == 0 )
617                                        private = 0;
618                        }
619                       
620                        add_buddy( ic, NULL, handle, NULL );
621                        u = user_findhandle( ic, handle );
622                        u->is_private = private;
623                }
624                else
625                {
626                        imc_log( ic, "Message from unknown handle %s:", handle );
627                        u = user_find( irc, irc->mynick );
628                }
629        }
630       
631        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
632            ( ( ic->flags & OPT_CONN_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
633                strip_html( msg );
634
635        while( strlen( msg ) > 425 )
636        {
637                char tmp, *nl;
638               
639                tmp = msg[425];
640                msg[425] = 0;
641               
642                /* If there's a newline/space in this string, split up there,
643                   looks a bit prettier. */
644                if( ( nl = strrchr( msg, '\n' ) ) || ( nl = strrchr( msg, ' ' ) ) )
645                {
646                        msg[425] = tmp;
647                        tmp = *nl;
648                        *nl = 0;
649                }
650               
651                irc_msgfrom( irc, u->nick, msg );
652               
653                /* Move on. */
654                if( nl )
655                {
656                        *nl = tmp;
657                        msg = nl + 1;
658                }
659                else
660                {
661                        msg[425] = tmp;
662                        msg += 425;
663                }
664        }
665        irc_msgfrom( irc, u->nick, msg );
666}
667
668void serv_got_typing( struct im_connection *ic, char *handle, int timeout, int type )
669{
670        user_t *u;
671       
672        if( !set_getbool( &ic->irc->set, "typing_notice" ) )
673                return;
674       
675        if( ( u = user_findhandle( ic, handle ) ) ) {
676                /* If type is:
677                 * 0: user has stopped typing
678                 * 1: user is actively typing
679                 * 2: user has entered text, but is not actively typing
680                 */
681                if (type == 0 || type == 1 || type == 2) {
682                        char buf[256]; 
683                        g_snprintf(buf, 256, "\1TYPING %d\1", type); 
684                        irc_privmsg( ic->irc, u, "PRIVMSG", ic->irc->nick, NULL, buf );
685                }
686        }
687}
688
689void serv_got_chat_left( struct groupchat *c )
690{
691        struct im_connection *ic = c->ic;
692        struct groupchat *l = NULL;
693        GList *ir;
694       
695        if( set_getbool( &ic->irc->set, "debug" ) )
696                imc_log( ic, "You were removed from conversation 0x%x", (int) c );
697       
698        if( c )
699        {
700                if( c->joined )
701                {
702                        user_t *u, *r;
703                       
704                        r = user_find( ic->irc, ic->irc->mynick );
705                        irc_privmsg( ic->irc, r, "PRIVMSG", c->channel, "", "Cleaning up channel, bye!" );
706                       
707                        u = user_find( ic->irc, ic->irc->nick );
708                        irc_kick( ic->irc, u, c->channel, r );
709                        /* irc_part( ic->irc, u, c->channel ); */
710                }
711               
712                if( l )
713                        l->next = c->next;
714                else
715                        ic->conversations = c->next;
716               
717                for( ir = c->in_room; ir; ir = ir->next )
718                        g_free( ir->data );
719                g_list_free( c->in_room );
720                g_free( c->channel );
721                g_free( c->title );
722                g_free( c );
723        }
724}
725
726void serv_got_chat_in( struct groupchat *c, char *who, int whisper, char *msg, time_t mtime )
727{
728        struct im_connection *ic = c->ic;
729        user_t *u;
730       
731        /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */
732        if( g_strcasecmp( who, ic->username ) == 0 )
733                return;
734       
735        u = user_findhandle( ic, who );
736       
737        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
738            ( ( ic->flags & OPT_CONN_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
739                strip_html( msg );
740       
741        if( c && u )
742                irc_privmsg( ic->irc, u, "PRIVMSG", c->channel, "", msg );
743        else
744                imc_log( ic, "Message from/to conversation %s@0x%x (unknown conv/user): %s", who, (int) c, msg );
745}
746
747struct groupchat *serv_got_joined_chat( struct im_connection *ic, char *handle )
748{
749        struct groupchat *c;
750       
751        /* This one just creates the conversation structure, user won't see anything yet */
752       
753        if( ic->conversations )
754        {
755                for( c = ic->conversations; c->next; c = c->next );
756                c = c->next = g_new0( struct groupchat, 1 );
757        }
758        else
759                ic->conversations = c = g_new0( struct groupchat, 1 );
760       
761        c->ic = ic;
762        c->title = g_strdup( handle );
763        c->channel = g_strdup_printf( "&chat_%03d", ic->irc->c_id++ );
764       
765        if( set_getbool( &ic->irc->set, "debug" ) )
766                imc_log( ic, "Creating new conversation: (id=0x%x,handle=%s)", (int) c, handle );
767       
768        return c;
769}
770
771
772/* buddy_chat.c */
773
774void add_chat_buddy( struct groupchat *b, char *handle )
775{
776        user_t *u = user_findhandle( b->ic, handle );
777        int me = 0;
778       
779        if( set_getbool( &b->ic->irc->set, "debug" ) )
780                imc_log( b->ic, "User %s added to conversation 0x%x", handle, (int) b );
781       
782        /* It might be yourself! */
783        if( b->ic->acc->prpl->handle_cmp( handle, b->ic->username ) == 0 )
784        {
785                u = user_find( b->ic->irc, b->ic->irc->nick );
786                if( !b->joined )
787                        irc_join( b->ic->irc, u, b->channel );
788                b->joined = me = 1;
789        }
790       
791        /* Most protocols allow people to join, even when they're not in
792           your contact list. Try to handle that here */
793        if( !u )
794        {
795                add_buddy( b->ic, NULL, handle, NULL );
796                u = user_findhandle( b->ic, handle );
797        }
798       
799        /* Add the handle to the room userlist, if it's not 'me' */
800        if( !me )
801        {
802                if( b->joined )
803                        irc_join( b->ic->irc, u, b->channel );
804                b->in_room = g_list_append( b->in_room, g_strdup( handle ) );
805        }
806}
807
808void remove_chat_buddy( struct groupchat *b, char *handle, char *reason )
809{
810        user_t *u;
811        int me = 0;
812       
813        if( set_getbool( &b->ic->irc->set, "debug" ) )
814                imc_log( b->ic, "User %s removed from conversation 0x%x (%s)", handle, (int) b, reason ? reason : "" );
815       
816        /* It might be yourself! */
817        if( g_strcasecmp( handle, b->ic->username ) == 0 )
818        {
819                u = user_find( b->ic->irc, b->ic->irc->nick );
820                b->joined = 0;
821                me = 1;
822        }
823        else
824        {
825                u = user_findhandle( b->ic, handle );
826        }
827       
828        if( remove_chat_buddy_silent( b, handle ) )
829                if( ( b->joined || me ) && u )
830                        irc_part( b->ic->irc, u, b->channel );
831}
832
833static int remove_chat_buddy_silent( struct groupchat *b, char *handle )
834{
835        GList *i;
836       
837        /* Find the handle in the room userlist and shoot it */
838        i = b->in_room;
839        while( i )
840        {
841                if( g_strcasecmp( handle, i->data ) == 0 )
842                {
843                        g_free( i->data );
844                        b->in_room = g_list_remove( b->in_room, i->data );
845                        return( 1 );
846                }
847               
848                i = i->next;
849        }
850       
851        return( 0 );
852}
853
854
855/* Misc. BitlBee stuff which shouldn't really be here */
856
857struct groupchat *chat_by_channel( char *channel )
858{
859        struct im_connection *ic;
860        struct groupchat *c;
861        GSList *l;
862       
863        /* This finds the connection which has a conversation which belongs to this channel */
864        for( l = connections; l; l = l->next )
865        {
866                ic = l->data;
867                for( c = ic->conversations; c && g_strcasecmp( c->channel, channel ) != 0; c = c->next );
868                if( c )
869                        return c;
870        }
871       
872        return NULL;
873}
874
875char *set_eval_away_devoice( set_t *set, char *value )
876{
877        irc_t *irc = set->data;
878        int st;
879       
880        if( ( g_strcasecmp( value, "true" ) == 0 ) || ( g_strcasecmp( value, "yes" ) == 0 ) || ( g_strcasecmp( value, "on" ) == 0 ) )
881                st = 1;
882        else if( ( g_strcasecmp( value, "false" ) == 0 ) || ( g_strcasecmp( value, "no" ) == 0 ) || ( g_strcasecmp( value, "off" ) == 0 ) )
883                st = 0;
884        else if( sscanf( value, "%d", &st ) != 1 )
885                return( NULL );
886       
887        st = st != 0;
888       
889        /* Horror.... */
890       
891        if( st != set_getbool( &irc->set, "away_devoice" ) )
892        {
893                char list[80] = "";
894                user_t *u = irc->users;
895                int i = 0, count = 0;
896                char pm;
897                char v[80];
898               
899                if( st )
900                        pm = '+';
901                else
902                        pm = '-';
903               
904                while( u )
905                {
906                        if( u->ic && u->online && !u->away )
907                        {
908                                if( ( strlen( list ) + strlen( u->nick ) ) >= 79 )
909                                {
910                                        for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
911                                        irc_write( irc, ":%s MODE %s %c%s%s",
912                                                   irc->myhost,
913                                                   irc->channel, pm, v, list );
914                                       
915                                        *list = 0;
916                                        count = 0;
917                                }
918                               
919                                sprintf( list + strlen( list ), " %s", u->nick );
920                                count ++;
921                        }
922                        u = u->next;
923                }
924               
925                /* $v = 'v' x $i */
926                for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
927                irc_write( irc, ":%s MODE %s %c%s%s", irc->myhost,
928                                                            irc->channel, pm, v, list );
929        }
930       
931        return( set_eval_bool( set, value ) );
932}
933
934
935
936
937/* The plan is to not allow straight calls to prpl functions anymore, but do
938   them all from some wrappers. We'll start to define some down here: */
939
940int bim_buddy_msg( struct im_connection *ic, char *handle, char *msg, int flags )
941{
942        char *buf = NULL;
943        int st;
944       
945        if( ( ic->flags & OPT_CONN_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
946        {
947                buf = escape_html( msg );
948                msg = buf;
949        }
950       
951        st = ic->acc->prpl->send_im( ic, handle, msg, flags );
952        g_free( buf );
953       
954        return st;
955}
956
957int bim_chat_msg( struct groupchat *c, char *msg, int flags )
958{
959        char *buf = NULL;
960        int st;
961       
962        if( ( c->ic->flags & OPT_CONN_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
963        {
964                buf = escape_html( msg );
965                msg = buf;
966        }
967       
968        c->ic->acc->prpl->chat_send( c, msg, flags );
969        g_free( buf );
970       
971        return 1;
972}
973
974static char *bim_away_alias_find( GList *gcm, char *away );
975
976int bim_set_away( struct im_connection *ic, char *away )
977{
978        GList *m, *ms;
979        char *s;
980       
981        if( !away ) away = "";
982        ms = m = ic->acc->prpl->away_states( ic );
983       
984        while( m )
985        {
986                if( *away )
987                {
988                        if( g_strncasecmp( m->data, away, strlen( m->data ) ) == 0 )
989                                break;
990                }
991                else
992                {
993                        if( g_strcasecmp( m->data, "Available" ) == 0 )
994                                break;
995                        if( g_strcasecmp( m->data, "Online" ) == 0 )
996                                break;
997                }
998                m = m->next;
999        }
1000       
1001        if( m )
1002        {
1003                ic->acc->prpl->set_away( ic, m->data, *away ? away : NULL );
1004        }
1005        else
1006        {
1007                s = bim_away_alias_find( ms, away );
1008                if( s )
1009                {
1010                        ic->acc->prpl->set_away( ic, s, away );
1011                        if( set_getbool( &ic->irc->set, "debug" ) )
1012                                imc_log( ic, "Setting away state to %s", s );
1013                }
1014                else
1015                        ic->acc->prpl->set_away( ic, GAIM_AWAY_CUSTOM, away );
1016        }
1017       
1018        return( 1 );
1019}
1020
1021static char *bim_away_alias_list[8][5] =
1022{
1023        { "Away from computer", "Away", "Extended away", NULL },
1024        { "NA", "N/A", "Not available", NULL },
1025        { "Busy", "Do not disturb", "DND", "Occupied", NULL },
1026        { "Be right back", "BRB", NULL },
1027        { "On the phone", "Phone", "On phone", NULL },
1028        { "Out to lunch", "Lunch", "Food", NULL },
1029        { "Invisible", "Hidden" },
1030        { NULL }
1031};
1032
1033static char *bim_away_alias_find( GList *gcm, char *away )
1034{
1035        GList *m;
1036        int i, j;
1037       
1038        for( i = 0; *bim_away_alias_list[i]; i ++ )
1039        {
1040                for( j = 0; bim_away_alias_list[i][j]; j ++ )
1041                        if( g_strncasecmp( away, bim_away_alias_list[i][j], strlen( bim_away_alias_list[i][j] ) ) == 0 )
1042                                break;
1043               
1044                if( !bim_away_alias_list[i][j] )        /* If we reach the end, this row */
1045                        continue;                       /* is not what we want. Next!    */
1046               
1047                /* Now find an entry in this row which exists in gcm */
1048                for( j = 0; bim_away_alias_list[i][j]; j ++ )
1049                {
1050                        m = gcm;
1051                        while( m )
1052                        {
1053                                if( g_strcasecmp( bim_away_alias_list[i][j], m->data ) == 0 )
1054                                        return( bim_away_alias_list[i][j] );
1055                                m = m->next;
1056                        }
1057                }
1058        }
1059       
1060        return( NULL );
1061}
1062
1063void bim_add_allow( struct im_connection *ic, char *handle )
1064{
1065        if( g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL )
1066        {
1067                ic->permit = g_slist_prepend( ic->permit, g_strdup( handle ) );
1068        }
1069       
1070        ic->acc->prpl->add_permit( ic, handle );
1071}
1072
1073void bim_rem_allow( struct im_connection *ic, char *handle )
1074{
1075        GSList *l;
1076       
1077        if( ( l = g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) )
1078        {
1079                g_free( l->data );
1080                ic->permit = g_slist_delete_link( ic->permit, l );
1081        }
1082       
1083        ic->acc->prpl->rem_permit( ic, handle );
1084}
1085
1086void bim_add_block( struct im_connection *ic, char *handle )
1087{
1088        if( g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL )
1089        {
1090                ic->deny = g_slist_prepend( ic->deny, g_strdup( handle ) );
1091        }
1092       
1093        ic->acc->prpl->add_deny( ic, handle );
1094}
1095
1096void bim_rem_block( struct im_connection *ic, char *handle )
1097{
1098        GSList *l;
1099       
1100        if( ( l = g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) )
1101        {
1102                g_free( l->data );
1103                ic->deny = g_slist_delete_link( ic->deny, l );
1104        }
1105       
1106        ic->acc->prpl->rem_deny( ic, handle );
1107}
Note: See TracBrowser for help on using the repository browser.