source: protocols/nogaim.c @ 00a5270

Last change on this file since 00a5270 was 00a5270, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-07-14T18:24:59Z

Added a per-connection auto_reconnect setting.

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