source: protocols/nogaim.c @ fb62f81f

Last change on this file since fb62f81f was fb62f81f, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-06-03T19:51:16Z

Implemented netsplits for "account off".

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