source: protocols/nogaim.c @ 5b52a48

Last change on this file since 5b52a48 was 5b52a48, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-07-03T21:22:45Z

Implemented per-account nick lists instead of per-protocol nick lists.
nick_t is dead, instead nicks are just saves in a per-account_t GLib
hash table. While doing this, the import_buddies command finally died
and text_save() disappeared, because the old file format can't handle
most of the new features in this branch anyway.

Still have to implement support for the new nick lists in text_load()!

  • Property mode set to 100644
File size: 25.4 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_getint( &irc->set, "auto_reconnect" ) )
314        {
315                int delay = set_getint( &irc->set, "auto_reconnect_delay" );
316               
317                serv_got_crap( gc, "Reconnecting in %d seconds..", delay );
318                a->reconnect = b_timeout_add( delay * 1000, auto_reconnect, a );
319        }
320       
321        destroy_gaim_conn( gc );
322}
323
324
325/* dialogs.c */
326
327void do_error_dialog( struct gaim_connection *gc, char *msg, char *title )
328{
329        if( msg && title )
330                serv_got_crap( gc, "Error: %s: %s", title, msg );
331        else if( msg )
332                serv_got_crap( gc, "Error: %s", msg );
333        else if( title )
334                serv_got_crap( gc, "Error: %s", title );
335        else
336                serv_got_crap( gc, "Error" );
337}
338
339void do_ask_dialog( struct gaim_connection *gc, char *msg, void *data, void *doit, void *dont )
340{
341        query_add( gc->irc, gc, msg, doit, dont, data );
342}
343
344
345/* list.c */
346
347void add_buddy( struct gaim_connection *gc, char *group, char *handle, char *realname )
348{
349        user_t *u;
350        char nick[MAX_NICK_LENGTH+1];
351        char *s;
352        irc_t *irc = gc->irc;
353       
354        if( set_getint( &irc->set, "debug" ) && 0 ) /* This message is too useless */
355                serv_got_crap( gc, "Receiving user add from handle: %s", handle );
356       
357        if( user_findhandle( gc, handle ) )
358        {
359                if( set_getint( &irc->set, "debug" ) )
360                        serv_got_crap( gc, "User already exists, ignoring add request: %s", handle );
361               
362                return;
363               
364                /* Buddy seems to exist already. Let's ignore this request then... */
365        }
366       
367        memset( nick, 0, MAX_NICK_LENGTH + 1 );
368        strcpy( nick, nick_get( gc->acc, handle, realname ) );
369       
370        u = user_add( gc->irc, nick );
371       
372        if( !realname || !*realname ) realname = nick;
373        u->realname = g_strdup( realname );
374       
375        if( ( s = strchr( handle, '@' ) ) )
376        {
377                u->host = g_strdup( s + 1 );
378                u->user = g_strndup( handle, s - handle );
379        }
380        else if( gc->acc->server )
381        {
382                char *colon;
383               
384                if( ( colon = strchr( gc->acc->server, ':' ) ) )
385                        u->host = g_strndup( gc->acc->server,
386                                             colon - gc->acc->server );
387                else
388                        u->host = g_strdup( gc->acc->server );
389               
390                u->user = g_strdup( handle );
391               
392                /* s/ /_/ ... important for AOL screennames */
393                for( s = u->user; *s; s ++ )
394                        if( *s == ' ' )
395                                *s = '_';
396        }
397        else
398        {
399                u->host = g_strdup( gc->acc->prpl->name );
400                u->user = g_strdup( handle );
401        }
402       
403        u->gc = gc;
404        u->handle = g_strdup( handle );
405        if( group ) u->group = g_strdup( group );
406        u->send_handler = buddy_send_handler;
407        u->last_typing_notice = 0;
408}
409
410struct buddy *find_buddy( struct gaim_connection *gc, char *handle )
411{
412        static struct buddy b[1];
413        user_t *u;
414       
415        u = user_findhandle( gc, handle );
416       
417        if( !u )
418                return( NULL );
419
420        memset( b, 0, sizeof( b ) );
421        strncpy( b->name, handle, 80 );
422        strncpy( b->show, u->realname, BUDDY_ALIAS_MAXLEN );
423        b->present = u->online;
424        b->gc = u->gc;
425       
426        return( b );
427}
428
429void signoff_blocked( struct gaim_connection *gc )
430{
431        return; /* Make all blocked users look invisible (TODO?) */
432}
433
434
435void serv_buddy_rename( struct gaim_connection *gc, char *handle, char *realname )
436{
437        user_t *u = user_findhandle( gc, handle );
438       
439        if( !u ) return;
440       
441        if( g_strcasecmp( u->realname, realname ) != 0 )
442        {
443                if( u->realname != u->nick ) g_free( u->realname );
444               
445                u->realname = g_strdup( realname );
446               
447                if( ( gc->flags & OPT_LOGGED_IN ) && set_getint( &gc->irc->set, "display_namechanges" ) )
448                        serv_got_crap( gc, "User `%s' changed name to `%s'", u->nick, u->realname );
449        }
450}
451
452
453/* prpl.c */
454
455struct show_got_added_data
456{
457        struct gaim_connection *gc;
458        char *handle;
459};
460
461void show_got_added_no( gpointer w, struct show_got_added_data *data )
462{
463        g_free( data->handle );
464        g_free( data );
465}
466
467void show_got_added_yes( gpointer w, struct show_got_added_data *data )
468{
469        data->gc->acc->prpl->add_buddy( data->gc, data->handle );
470        add_buddy( data->gc, NULL, data->handle, data->handle );
471       
472        return show_got_added_no( w, data );
473}
474
475void show_got_added( struct gaim_connection *gc, char *handle, const char *realname )
476{
477        struct show_got_added_data *data = g_new0( struct show_got_added_data, 1 );
478        char *s;
479       
480        /* TODO: Make a setting for this! */
481        if( user_findhandle( gc, handle ) != NULL )
482                return;
483       
484        s = g_strdup_printf( "The user %s is not in your buddy list yet. Do you want to add him/her now?", handle );
485       
486        data->gc = gc;
487        data->handle = g_strdup( handle );
488        query_add( gc->irc, gc, s, show_got_added_yes, show_got_added_no, data );
489}
490
491
492/* server.c */                   
493
494void serv_got_update( struct gaim_connection *gc, char *handle, int loggedin, int evil, time_t signon, time_t idle, int type, guint caps )
495{
496        user_t *u;
497        int oa, oo;
498       
499        u = user_findhandle( gc, handle );
500       
501        if( !u )
502        {
503                if( g_strcasecmp( set_getstr( &gc->irc->set, "handle_unknown" ), "add" ) == 0 )
504                {
505                        add_buddy( gc, NULL, handle, NULL );
506                        u = user_findhandle( gc, handle );
507                }
508                else
509                {
510                        if( set_getint( &gc->irc->set, "debug" ) || g_strcasecmp( set_getstr( &gc->irc->set, "handle_unknown" ), "ignore" ) != 0 )
511                        {
512                                serv_got_crap( gc, "serv_got_update() for handle %s:", handle );
513                                serv_got_crap( gc, "loggedin = %d, type = %d", loggedin, type );
514                        }
515                       
516                        return;
517                }
518                /* Why did we have this here....
519                return; */
520        }
521       
522        oa = u->away != NULL;
523        oo = u->online;
524       
525        if( u->away )
526        {
527                g_free( u->away );
528                u->away = NULL;
529        }
530       
531        if( loggedin && !u->online )
532        {
533                irc_spawn( gc->irc, u );
534                u->online = 1;
535        }
536        else if( !loggedin && u->online )
537        {
538                struct conversation *c;
539               
540                irc_kill( gc->irc, u );
541                u->online = 0;
542               
543                /* Remove him/her from the conversations to prevent PART messages after he/she QUIT already */
544                for( c = gc->conversations; c; c = c->next )
545                        remove_chat_buddy_silent( c, handle );
546        }
547       
548        if( ( type & UC_UNAVAILABLE ) && ( strcmp( gc->acc->prpl->name, "oscar" ) == 0 || strcmp( gc->acc->prpl->name, "icq" ) == 0 ) )
549        {
550                u->away = g_strdup( "Away" );
551        }
552        else if( ( type & UC_UNAVAILABLE ) && ( strcmp( gc->acc->prpl->name, "jabber" ) == 0 ) )
553        {
554                if( type & UC_DND )
555                        u->away = g_strdup( "Do Not Disturb" );
556                else if( type & UC_XA )
557                        u->away = g_strdup( "Extended Away" );
558                else // if( type & UC_AWAY )
559                        u->away = g_strdup( "Away" );
560        }
561        else if( ( type & UC_UNAVAILABLE ) && gc->acc->prpl->get_status_string )
562        {
563                u->away = g_strdup( gc->acc->prpl->get_status_string( gc, type ) );
564        }
565        else
566                u->away = NULL;
567       
568        /* LISPy... */
569        if( ( set_getint( &gc->irc->set, "away_devoice" ) ) &&          /* Don't do a thing when user doesn't want it */
570            ( u->online ) &&                                            /* Don't touch offline people */
571            ( ( ( u->online != oo ) && !u->away ) ||                    /* Voice joining people */
572              ( ( u->online == oo ) && ( oa == !u->away ) ) ) )         /* (De)voice people changing state */
573        {
574                irc_write( gc->irc, ":%s!%s@%s MODE %s %cv %s", gc->irc->mynick, gc->irc->mynick, gc->irc->myhost,
575                                                                gc->irc->channel, u->away?'-':'+', u->nick );
576        }
577}
578
579void serv_got_im( struct gaim_connection *gc, char *handle, char *msg, guint32 flags, time_t mtime, gint len )
580{
581        irc_t *irc = gc->irc;
582        user_t *u;
583       
584        u = user_findhandle( gc, handle );
585       
586        if( !u )
587        {
588                char *h = set_getstr( &irc->set, "handle_unknown" );
589               
590                if( g_strcasecmp( h, "ignore" ) == 0 )
591                {
592                        if( set_getint( &irc->set, "debug" ) )
593                                serv_got_crap( gc, "Ignoring message from unknown handle %s", handle );
594                       
595                        return;
596                }
597                else if( g_strncasecmp( h, "add", 3 ) == 0 )
598                {
599                        int private = set_getint( &irc->set, "private" );
600                       
601                        if( h[3] )
602                        {
603                                if( g_strcasecmp( h + 3, "_private" ) == 0 )
604                                        private = 1;
605                                else if( g_strcasecmp( h + 3, "_channel" ) == 0 )
606                                        private = 0;
607                        }
608                       
609                        add_buddy( gc, NULL, handle, NULL );
610                        u = user_findhandle( gc, handle );
611                        u->is_private = private;
612                }
613                else
614                {
615                        serv_got_crap( gc, "Message from unknown handle %s:", handle );
616                        u = user_find( irc, irc->mynick );
617                }
618        }
619       
620        if( ( g_strcasecmp( set_getstr( &gc->irc->set, "strip_html" ), "always" ) == 0 ) ||
621            ( ( gc->flags & OPT_CONN_HTML ) && set_getint( &gc->irc->set, "strip_html" ) ) )
622                strip_html( msg );
623
624        while( strlen( msg ) > 425 )
625        {
626                char tmp, *nl;
627               
628                tmp = msg[425];
629                msg[425] = 0;
630               
631                /* If there's a newline/space in this string, split up there,
632                   looks a bit prettier. */
633                if( ( nl = strrchr( msg, '\n' ) ) || ( nl = strrchr( msg, ' ' ) ) )
634                {
635                        msg[425] = tmp;
636                        tmp = *nl;
637                        *nl = 0;
638                }
639               
640                irc_msgfrom( irc, u->nick, msg );
641               
642                /* Move on. */
643                if( nl )
644                {
645                        *nl = tmp;
646                        msg = nl + 1;
647                }
648                else
649                {
650                        msg[425] = tmp;
651                        msg += 425;
652                }
653        }
654        irc_msgfrom( irc, u->nick, msg );
655}
656
657void serv_got_typing( struct gaim_connection *gc, char *handle, int timeout, int type )
658{
659        user_t *u;
660       
661        if( !set_getint( &gc->irc->set, "typing_notice" ) )
662                return;
663       
664        if( ( u = user_findhandle( gc, handle ) ) ) {
665                /* If type is:
666                 * 0: user has stopped typing
667                 * 1: user is actively typing
668                 * 2: user has entered text, but is not actively typing
669                 */
670                if (type == 0 || type == 1 || type == 2) {
671                        char buf[256]; 
672                        g_snprintf(buf, 256, "\1TYPING %d\1", type); 
673                        irc_privmsg( gc->irc, u, "PRIVMSG", gc->irc->nick, NULL, buf );
674                }
675        }
676}
677
678void serv_got_chat_left( struct gaim_connection *gc, int id )
679{
680        struct conversation *c, *l = NULL;
681        GList *ir;
682       
683        if( set_getint( &gc->irc->set, "debug" ) )
684                serv_got_crap( gc, "You were removed from conversation %d", (int) id );
685       
686        for( c = gc->conversations; c && c->id != id; c = (l=c)->next );
687       
688        if( c )
689        {
690                if( c->joined )
691                {
692                        user_t *u, *r;
693                       
694                        r = user_find( gc->irc, gc->irc->mynick );
695                        irc_privmsg( gc->irc, r, "PRIVMSG", c->channel, "", "Cleaning up channel, bye!" );
696                       
697                        u = user_find( gc->irc, gc->irc->nick );
698                        irc_kick( gc->irc, u, c->channel, r );
699                        /* irc_part( gc->irc, u, c->channel ); */
700                }
701               
702                if( l )
703                        l->next = c->next;
704                else
705                        gc->conversations = c->next;
706               
707                for( ir = c->in_room; ir; ir = ir->next )
708                        g_free( ir->data );
709                g_list_free( c->in_room );
710                g_free( c->channel );
711                g_free( c->title );
712                g_free( c );
713        }
714}
715
716void serv_got_chat_in( struct gaim_connection *gc, int id, char *who, int whisper, char *msg, time_t mtime )
717{
718        struct conversation *c;
719        user_t *u;
720       
721        /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */
722        if( g_strcasecmp( who, gc->username ) == 0 )
723                return;
724       
725        u = user_findhandle( gc, who );
726        for( c = gc->conversations; c && c->id != id; c = c->next );
727       
728        if( ( g_strcasecmp( set_getstr( &gc->irc->set, "strip_html" ), "always" ) == 0 ) ||
729            ( ( gc->flags & OPT_CONN_HTML ) && set_getint( &gc->irc->set, "strip_html" ) ) )
730                strip_html( msg );
731       
732        if( c && u )
733                irc_privmsg( gc->irc, u, "PRIVMSG", c->channel, "", msg );
734        else
735                serv_got_crap( gc, "Message from/to conversation %s@%d (unknown conv/user): %s", who, id, msg );
736}
737
738struct conversation *serv_got_joined_chat( struct gaim_connection *gc, int id, char *handle )
739{
740        struct conversation *c;
741        char *s;
742       
743        /* This one just creates the conversation structure, user won't see anything yet */
744       
745        if( gc->conversations )
746        {
747                for( c = gc->conversations; c->next; c = c->next );
748                c = c->next = g_new0( struct conversation, 1 );
749        }
750        else
751                gc->conversations = c = g_new0( struct conversation, 1);
752       
753        c->id = id;
754        c->gc = gc;
755        c->title = g_strdup( handle );
756       
757        s = g_new( char, 16 );
758        sprintf( s, "&chat_%03d", gc->irc->c_id++ );
759        c->channel = g_strdup( s );
760        g_free( s );
761       
762        if( set_getint( &gc->irc->set, "debug" ) )
763                serv_got_crap( gc, "Creating new conversation: (id=%d,handle=%s)", id, handle );
764       
765        return( c );
766}
767
768
769/* buddy_chat.c */
770
771void add_chat_buddy( struct conversation *b, char *handle )
772{
773        user_t *u = user_findhandle( b->gc, handle );
774        int me = 0;
775       
776        if( set_getint( &b->gc->irc->set, "debug" ) )
777                serv_got_crap( b->gc, "User %s added to conversation %d", handle, b->id );
778       
779        /* It might be yourself! */
780        if( b->gc->acc->prpl->handle_cmp( handle, b->gc->username ) == 0 )
781        {
782                u = user_find( b->gc->irc, b->gc->irc->nick );
783                if( !b->joined )
784                        irc_join( b->gc->irc, u, b->channel );
785                b->joined = me = 1;
786        }
787       
788        /* Most protocols allow people to join, even when they're not in
789           your contact list. Try to handle that here */
790        if( !u )
791        {
792                add_buddy( b->gc, NULL, handle, NULL );
793                u = user_findhandle( b->gc, handle );
794        }
795       
796        /* Add the handle to the room userlist, if it's not 'me' */
797        if( !me )
798        {
799                if( b->joined )
800                        irc_join( b->gc->irc, u, b->channel );
801                b->in_room = g_list_append( b->in_room, g_strdup( handle ) );
802        }
803}
804
805void remove_chat_buddy( struct conversation *b, char *handle, char *reason )
806{
807        user_t *u;
808        int me = 0;
809       
810        if( set_getint( &b->gc->irc->set, "debug" ) )
811                serv_got_crap( b->gc, "User %s removed from conversation %d (%s)", handle, b->id, reason ? reason : "" );
812       
813        /* It might be yourself! */
814        if( g_strcasecmp( handle, b->gc->username ) == 0 )
815        {
816                u = user_find( b->gc->irc, b->gc->irc->nick );
817                b->joined = 0;
818                me = 1;
819        }
820        else
821        {
822                u = user_findhandle( b->gc, handle );
823        }
824       
825        if( remove_chat_buddy_silent( b, handle ) )
826                if( ( b->joined || me ) && u )
827                        irc_part( b->gc->irc, u, b->channel );
828}
829
830static int remove_chat_buddy_silent( struct conversation *b, char *handle )
831{
832        GList *i;
833       
834        /* Find the handle in the room userlist and shoot it */
835        i = b->in_room;
836        while( i )
837        {
838                if( g_strcasecmp( handle, i->data ) == 0 )
839                {
840                        g_free( i->data );
841                        b->in_room = g_list_remove( b->in_room, i->data );
842                        return( 1 );
843                }
844               
845                i = i->next;
846        }
847       
848        return( 0 );
849}
850
851
852/* Misc. BitlBee stuff which shouldn't really be here */
853
854struct conversation *conv_findchannel( char *channel )
855{
856        struct gaim_connection *gc;
857        struct conversation *c;
858        GSList *l;
859       
860        /* This finds the connection which has a conversation which belongs to this channel */
861        for( l = connections; l; l = l->next )
862        {
863                gc = l->data;
864                for( c = gc->conversations; c && g_strcasecmp( c->channel, channel ) != 0; c = c->next );
865                if( c )
866                        return( c );
867        }
868       
869        return( NULL );
870}
871
872char *set_eval_away_devoice( set_t *set, char *value )
873{
874        irc_t *irc = set->data;
875        int st;
876       
877        if( ( g_strcasecmp( value, "true" ) == 0 ) || ( g_strcasecmp( value, "yes" ) == 0 ) || ( g_strcasecmp( value, "on" ) == 0 ) )
878                st = 1;
879        else if( ( g_strcasecmp( value, "false" ) == 0 ) || ( g_strcasecmp( value, "no" ) == 0 ) || ( g_strcasecmp( value, "off" ) == 0 ) )
880                st = 0;
881        else if( sscanf( value, "%d", &st ) != 1 )
882                return( NULL );
883       
884        st = st != 0;
885       
886        /* Horror.... */
887       
888        if( st != set_getint( &irc->set, "away_devoice" ) )
889        {
890                char list[80] = "";
891                user_t *u = irc->users;
892                int i = 0, count = 0;
893                char pm;
894                char v[80];
895               
896                if( st )
897                        pm = '+';
898                else
899                        pm = '-';
900               
901                while( u )
902                {
903                        if( u->gc && u->online && !u->away )
904                        {
905                                if( ( strlen( list ) + strlen( u->nick ) ) >= 79 )
906                                {
907                                        for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
908                                        irc_write( irc, ":%s!%s@%s MODE %s %c%s%s",
909                                                   irc->mynick, irc->mynick, irc->myhost,
910                                                   irc->channel, pm, v, list );
911                                       
912                                        *list = 0;
913                                        count = 0;
914                                }
915                               
916                                sprintf( list + strlen( list ), " %s", u->nick );
917                                count ++;
918                        }
919                        u = u->next;
920                }
921               
922                /* $v = 'v' x $i */
923                for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
924                irc_write( irc, ":%s!%s@%s MODE %s %c%s%s", irc->mynick, irc->mynick, irc->myhost,
925                                                            irc->channel, pm, v, list );
926        }
927       
928        return( set_eval_bool( set, value ) );
929}
930
931
932
933
934/* The plan is to not allow straight calls to prpl functions anymore, but do
935   them all from some wrappers. We'll start to define some down here: */
936
937int bim_buddy_msg( struct gaim_connection *gc, char *handle, char *msg, int flags )
938{
939        char *buf = NULL;
940        int st;
941       
942        if( ( gc->flags & OPT_CONN_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
943        {
944                buf = escape_html( msg );
945                msg = buf;
946        }
947       
948        st = gc->acc->prpl->send_im( gc, handle, msg, strlen( msg ), flags );
949        g_free( buf );
950       
951        return st;
952}
953
954int bim_chat_msg( struct gaim_connection *gc, int id, char *msg )
955{
956        char *buf = NULL;
957        int st;
958       
959        if( ( gc->flags & OPT_CONN_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
960        {
961                buf = escape_html( msg );
962                msg = buf;
963        }
964       
965        st = gc->acc->prpl->chat_send( gc, id, msg );
966        g_free( buf );
967       
968        return st;
969}
970
971static char *bim_away_alias_find( GList *gcm, char *away );
972
973int bim_set_away( struct gaim_connection *gc, char *away )
974{
975        GList *m, *ms;
976        char *s;
977       
978        if( !away ) away = "";
979        ms = m = gc->acc->prpl->away_states( gc );
980       
981        while( m )
982        {
983                if( *away )
984                {
985                        if( g_strncasecmp( m->data, away, strlen( m->data ) ) == 0 )
986                                break;
987                }
988                else
989                {
990                        if( g_strcasecmp( m->data, "Available" ) == 0 )
991                                break;
992                        if( g_strcasecmp( m->data, "Online" ) == 0 )
993                                break;
994                }
995                m = m->next;
996        }
997       
998        if( m )
999        {
1000                gc->acc->prpl->set_away( gc, m->data, *away ? away : NULL );
1001        }
1002        else
1003        {
1004                s = bim_away_alias_find( ms, away );
1005                if( s )
1006                {
1007                        gc->acc->prpl->set_away( gc, s, away );
1008                        if( set_getint( &gc->irc->set, "debug" ) )
1009                                serv_got_crap( gc, "Setting away state to %s", s );
1010                }
1011                else
1012                        gc->acc->prpl->set_away( gc, GAIM_AWAY_CUSTOM, away );
1013        }
1014       
1015        g_list_free( ms );
1016       
1017        return( 1 );
1018}
1019
1020static char *bim_away_alias_list[8][5] =
1021{
1022        { "Away from computer", "Away", "Extended away", NULL },
1023        { "NA", "N/A", "Not available", NULL },
1024        { "Busy", "Do not disturb", "DND", "Occupied", NULL },
1025        { "Be right back", "BRB", NULL },
1026        { "On the phone", "Phone", "On phone", NULL },
1027        { "Out to lunch", "Lunch", "Food", NULL },
1028        { "Invisible", "Hidden" },
1029        { NULL }
1030};
1031
1032static char *bim_away_alias_find( GList *gcm, char *away )
1033{
1034        GList *m;
1035        int i, j;
1036       
1037        for( i = 0; *bim_away_alias_list[i]; i ++ )
1038        {
1039                for( j = 0; bim_away_alias_list[i][j]; j ++ )
1040                        if( g_strncasecmp( away, bim_away_alias_list[i][j], strlen( bim_away_alias_list[i][j] ) ) == 0 )
1041                                break;
1042               
1043                if( !bim_away_alias_list[i][j] )        /* If we reach the end, this row */
1044                        continue;                       /* is not what we want. Next!    */
1045               
1046                /* Now find an entry in this row which exists in gcm */
1047                for( j = 0; bim_away_alias_list[i][j]; j ++ )
1048                {
1049                        m = gcm;
1050                        while( m )
1051                        {
1052                                if( g_strcasecmp( bim_away_alias_list[i][j], m->data ) == 0 )
1053                                        return( bim_away_alias_list[i][j] );
1054                                m = m->next;
1055                        }
1056                }
1057        }
1058       
1059        return( NULL );
1060}
1061
1062void bim_add_allow( struct gaim_connection *gc, char *handle )
1063{
1064        if( g_slist_find_custom( gc->permit, handle, (GCompareFunc) gc->acc->prpl->handle_cmp ) == NULL )
1065        {
1066                gc->permit = g_slist_prepend( gc->permit, g_strdup( handle ) );
1067        }
1068       
1069        gc->acc->prpl->add_permit( gc, handle );
1070}
1071
1072void bim_rem_allow( struct gaim_connection *gc, char *handle )
1073{
1074        GSList *l;
1075       
1076        if( ( l = g_slist_find_custom( gc->permit, handle, (GCompareFunc) gc->acc->prpl->handle_cmp ) ) )
1077        {
1078                g_free( l->data );
1079                gc->permit = g_slist_delete_link( gc->permit, l );
1080        }
1081       
1082        gc->acc->prpl->rem_permit( gc, handle );
1083}
1084
1085void bim_add_block( struct gaim_connection *gc, char *handle )
1086{
1087        if( g_slist_find_custom( gc->deny, handle, (GCompareFunc) gc->acc->prpl->handle_cmp ) == NULL )
1088        {
1089                gc->deny = g_slist_prepend( gc->deny, g_strdup( handle ) );
1090        }
1091       
1092        gc->acc->prpl->add_deny( gc, handle );
1093}
1094
1095void bim_rem_block( struct gaim_connection *gc, char *handle )
1096{
1097        GSList *l;
1098       
1099        if( ( l = g_slist_find_custom( gc->deny, handle, (GCompareFunc) gc->acc->prpl->handle_cmp ) ) )
1100        {
1101                g_free( l->data );
1102                gc->deny = g_slist_delete_link( gc->deny, l );
1103        }
1104       
1105        gc->acc->prpl->rem_deny( gc, handle );
1106}
Note: See TracBrowser for help on using the repository browser.