source: protocols/nogaim.c @ 7bfd574

Last change on this file since 7bfd574 was da3b536, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-05-23T08:09:16Z

Added bim_ functions for block/allow list management to keep gc->permit/deny
up-to-date at run-time.

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