source: protocols/nogaim.c @ 43d8cc5

Last change on this file since 43d8cc5 was 43d8cc5, checked in by Wilmer van der Gaast <wilmer@…>, at 2007-06-13T23:31:18Z

Fixed a memory management problem that caused some strange nickname issues.

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