source: protocols/nogaim.c @ 1be0d26

Last change on this file since 1be0d26 was 3e1ef92c, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-03-09T13:13:50Z

Fixed a compiler warning for an ugliness that fortunately shouldn't have
caused any issues except with broken protocol modules.

  • Property mode set to 100644
File size: 28.7 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2010 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 <ctype.h>
36
37#include "nogaim.h"
38#include "chat.h"
39
40static int remove_chat_buddy_silent( struct groupchat *b, const 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 (%s)\n", path, g_module_error());
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
103struct prpl *find_protocol(const char *name)
104{
105        GList *gl;
106        for (gl = protocols; gl; gl = gl->next) 
107        {
108                struct prpl *proto = gl->data;
109                if(!g_strcasecmp(proto->name, name)) 
110                        return proto;
111        }
112        return NULL;
113}
114
115/* nogaim.c */
116void nogaim_init()
117{
118        extern void msn_initmodule();
119        extern void oscar_initmodule();
120        extern void byahoo_initmodule();
121        extern void jabber_initmodule();
122
123#ifdef WITH_MSN
124        msn_initmodule();
125#endif
126
127#ifdef WITH_OSCAR
128        oscar_initmodule();
129#endif
130       
131#ifdef WITH_YAHOO
132        byahoo_initmodule();
133#endif
134       
135#ifdef WITH_JABBER
136        jabber_initmodule();
137#endif
138
139#ifdef WITH_PLUGINS
140        load_plugins();
141#endif
142}
143
144GSList *get_connections() { return connections; }
145
146/* multi.c */
147
148struct im_connection *imcb_new( account_t *acc )
149{
150        struct im_connection *ic;
151       
152        ic = g_new0( struct im_connection, 1 );
153       
154        ic->irc = acc->irc;
155        ic->acc = acc;
156        acc->ic = ic;
157       
158        connections = g_slist_append( connections, ic );
159       
160        return( ic );
161}
162
163void imc_free( struct im_connection *ic )
164{
165        account_t *a;
166       
167        /* Destroy the pointer to this connection from the account list */
168        for( a = ic->irc->accounts; a; a = a->next )
169                if( a->ic == ic )
170                {
171                        a->ic = NULL;
172                        break;
173                }
174       
175        connections = g_slist_remove( connections, ic );
176        g_free( ic );
177}
178
179static void serv_got_crap( struct im_connection *ic, char *format, ... )
180{
181        va_list params;
182        char *text;
183        account_t *a;
184       
185        va_start( params, format );
186        text = g_strdup_vprintf( format, params );
187        va_end( params );
188
189        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
190            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
191                strip_html( text );
192       
193        /* Try to find a different connection on the same protocol. */
194        for( a = ic->irc->accounts; a; a = a->next )
195                if( a->prpl == ic->acc->prpl && a->ic != ic )
196                        break;
197       
198        /* If we found one, include the screenname in the message. */
199        if( a )
200                irc_usermsg( ic->irc, "%s(%s) - %s", ic->acc->prpl->name, ic->acc->user, text );
201        else
202                irc_usermsg( ic->irc, "%s - %s", ic->acc->prpl->name, text );
203       
204        g_free( text );
205}
206
207void imcb_log( struct im_connection *ic, char *format, ... )
208{
209        va_list params;
210        char *text;
211       
212        va_start( params, format );
213        text = g_strdup_vprintf( format, params );
214        va_end( params );
215       
216        if( ic->flags & OPT_LOGGED_IN )
217                serv_got_crap( ic, "%s", text );
218        else
219                serv_got_crap( ic, "Logging in: %s", text );
220       
221        g_free( text );
222}
223
224void imcb_error( struct im_connection *ic, char *format, ... )
225{
226        va_list params;
227        char *text;
228       
229        va_start( params, format );
230        text = g_strdup_vprintf( format, params );
231        va_end( params );
232       
233        if( ic->flags & OPT_LOGGED_IN )
234                serv_got_crap( ic, "Error: %s", text );
235        else
236                serv_got_crap( ic, "Couldn't log in: %s", text );
237       
238        g_free( text );
239}
240
241static gboolean send_keepalive( gpointer d, gint fd, b_input_condition cond )
242{
243        struct im_connection *ic = d;
244       
245        if( ic->acc->prpl->keepalive )
246                ic->acc->prpl->keepalive( ic );
247       
248        return TRUE;
249}
250
251void imcb_connected( struct im_connection *ic )
252{
253        irc_t *irc = ic->irc;
254        struct chat *c;
255        user_t *u;
256       
257        /* MSN servers sometimes redirect you to a different server and do
258           the whole login sequence again, so these "late" calls to this
259           function should be handled correctly. (IOW, ignored) */
260        if( ic->flags & OPT_LOGGED_IN )
261                return;
262       
263        u = user_find( ic->irc, ic->irc->nick );
264       
265        imcb_log( ic, "Logged in" );
266       
267        ic->keepalive = b_timeout_add( 60000, send_keepalive, ic );
268        ic->flags |= OPT_LOGGED_IN;
269       
270        /* Necessary to send initial presence status, even if we're not away. */
271        imc_away_send_update( ic );
272       
273        /* Apparently we're connected successfully, so reset the
274           exponential backoff timer. */
275        ic->acc->auto_reconnect_delay = 0;
276       
277        for( c = irc->chatrooms; c; c = c->next )
278        {
279                if( c->acc != ic->acc )
280                        continue;
281               
282                if( set_getbool( &c->set, "auto_join" ) )
283                        chat_join( irc, c, NULL );
284        }
285}
286
287gboolean auto_reconnect( gpointer data, gint fd, b_input_condition cond )
288{
289        account_t *a = data;
290       
291        a->reconnect = 0;
292        account_on( a->irc, a );
293       
294        return( FALSE );        /* Only have to run the timeout once */
295}
296
297void cancel_auto_reconnect( account_t *a )
298{
299        b_event_remove( a->reconnect );
300        a->reconnect = 0;
301}
302
303void imc_logout( struct im_connection *ic, int allow_reconnect )
304{
305        irc_t *irc = ic->irc;
306        user_t *t, *u;
307        account_t *a;
308        int delay;
309       
310        /* Nested calls might happen sometimes, this is probably the best
311           place to catch them. */
312        if( ic->flags & OPT_LOGGING_OUT )
313                return;
314        else
315                ic->flags |= OPT_LOGGING_OUT;
316       
317        imcb_log( ic, "Signing off.." );
318       
319        b_event_remove( ic->keepalive );
320        ic->keepalive = 0;
321        ic->acc->prpl->logout( ic );
322        b_event_remove( ic->inpa );
323       
324        g_free( ic->away );
325        ic->away = NULL;
326       
327        u = irc->users;
328        while( u )
329        {
330                if( u->ic == ic )
331                {
332                        t = u->next;
333                        user_del( irc, u->nick );
334                        u = t;
335                }
336                else
337                        u = u->next;
338        }
339       
340        query_del_by_conn( ic->irc, ic );
341       
342        for( a = irc->accounts; a; a = a->next )
343                if( a->ic == ic )
344                        break;
345       
346        if( !a )
347        {
348                /* Uhm... This is very sick. */
349        }
350        else if( allow_reconnect && set_getbool( &irc->set, "auto_reconnect" ) &&
351                 set_getbool( &a->set, "auto_reconnect" ) &&
352                 ( delay = account_reconnect_delay( a ) ) > 0 )
353        {
354                imcb_log( ic, "Reconnecting in %d seconds..", delay );
355                a->reconnect = b_timeout_add( delay * 1000, auto_reconnect, a );
356        }
357       
358        imc_free( ic );
359}
360
361
362/* dialogs.c */
363
364void imcb_ask( struct im_connection *ic, char *msg, void *data,
365               query_callback doit, query_callback dont )
366{
367        query_add( ic->irc, ic, msg, doit, dont, data );
368}
369
370
371/* list.c */
372
373void imcb_add_buddy( struct im_connection *ic, char *handle, char *group )
374{
375        user_t *u;
376        char nick[MAX_NICK_LENGTH+1], *s;
377        irc_t *irc = ic->irc;
378       
379        if( user_findhandle( ic, handle ) )
380        {
381                if( set_getbool( &irc->set, "debug" ) )
382                        imcb_log( ic, "User already exists, ignoring add request: %s", handle );
383               
384                return;
385               
386                /* Buddy seems to exist already. Let's ignore this request then...
387                   Eventually subsequent calls to this function *should* be possible
388                   when a buddy is in multiple groups. But for now BitlBee doesn't
389                   even support groups so let's silently ignore this for now. */
390        }
391       
392        memset( nick, 0, MAX_NICK_LENGTH + 1 );
393        strcpy( nick, nick_get( ic->acc, handle ) );
394       
395        u = user_add( ic->irc, nick );
396       
397//      if( !realname || !*realname ) realname = nick;
398//      u->realname = g_strdup( realname );
399       
400        if( ( s = strchr( handle, '@' ) ) )
401        {
402                u->host = g_strdup( s + 1 );
403                u->user = g_strndup( handle, s - handle );
404        }
405        else if( ic->acc->server )
406        {
407                u->host = g_strdup( ic->acc->server );
408                u->user = g_strdup( handle );
409               
410                /* s/ /_/ ... important for AOL screennames */
411                for( s = u->user; *s; s ++ )
412                        if( *s == ' ' )
413                                *s = '_';
414        }
415        else
416        {
417                u->host = g_strdup( ic->acc->prpl->name );
418                u->user = g_strdup( handle );
419        }
420       
421        u->ic = ic;
422        u->handle = g_strdup( handle );
423        if( group ) u->group = g_strdup( group );
424        u->send_handler = buddy_send_handler;
425        u->last_typing_notice = 0;
426}
427
428struct buddy *imcb_find_buddy( struct im_connection *ic, char *handle )
429{
430        static struct buddy b[1];
431        user_t *u;
432       
433        u = user_findhandle( ic, handle );
434       
435        if( !u )
436                return( NULL );
437       
438        memset( b, 0, sizeof( b ) );
439        strncpy( b->name, handle, 80 );
440        strncpy( b->show, u->realname, BUDDY_ALIAS_MAXLEN );
441        b->present = u->online;
442        b->ic = u->ic;
443       
444        return( b );
445}
446
447void imcb_rename_buddy( struct im_connection *ic, char *handle, char *realname )
448{
449        user_t *u = user_findhandle( ic, handle );
450       
451        if( !u || !realname ) return;
452       
453        if( g_strcasecmp( u->realname, realname ) != 0 )
454        {
455                if( u->realname != u->nick ) g_free( u->realname );
456               
457                u->realname = g_strdup( realname );
458               
459                if( ( ic->flags & OPT_LOGGED_IN ) && set_getbool( &ic->irc->set, "display_namechanges" ) )
460                        imcb_log( ic, "User `%s' changed name to `%s'", u->nick, u->realname );
461        }
462}
463
464void imcb_remove_buddy( struct im_connection *ic, char *handle, char *group )
465{
466        user_t *u;
467       
468        if( ( u = user_findhandle( ic, handle ) ) )
469                user_del( ic->irc, u->nick );
470}
471
472/* Mainly meant for ICQ (and now also for Jabber conferences) to allow IM
473   modules to suggest a nickname for a handle. */
474void imcb_buddy_nick_hint( struct im_connection *ic, char *handle, char *nick )
475{
476        user_t *u = user_findhandle( ic, handle );
477        char newnick[MAX_NICK_LENGTH+1], *orig_nick;
478       
479        if( u && !u->online && !nick_saved( ic->acc, handle ) )
480        {
481                /* Only do this if the person isn't online yet (which should
482                   be the case if we just added it) and if the user hasn't
483                   assigned a nickname to this buddy already. */
484               
485                strncpy( newnick, nick, MAX_NICK_LENGTH );
486                newnick[MAX_NICK_LENGTH] = 0;
487               
488                /* Some processing to make sure this string is a valid IRC nickname. */
489                nick_strip( newnick );
490                if( set_getbool( &ic->irc->set, "lcnicks" ) )
491                        nick_lc( newnick );
492               
493                if( strcmp( u->nick, newnick ) != 0 )
494                {
495                        /* Only do this if newnick is different from the current one.
496                           If rejoining a channel, maybe we got this nick already
497                           (and dedupe would only add an underscore. */
498                        nick_dedupe( ic->acc, handle, newnick );
499                       
500                        /* u->nick will be freed halfway the process, so it can't be
501                           passed as an argument. */
502                        orig_nick = g_strdup( u->nick );
503                        user_rename( ic->irc, orig_nick, newnick );
504                        g_free( orig_nick );
505                }
506        }
507}
508
509
510struct imcb_ask_cb_data
511{
512        struct im_connection *ic;
513        char *handle;
514};
515
516static void imcb_ask_auth_cb_no( void *data )
517{
518        struct imcb_ask_cb_data *cbd = data;
519       
520        cbd->ic->acc->prpl->auth_deny( cbd->ic, cbd->handle );
521       
522        g_free( cbd->handle );
523        g_free( cbd );
524}
525
526static void imcb_ask_auth_cb_yes( void *data )
527{
528        struct imcb_ask_cb_data *cbd = data;
529       
530        cbd->ic->acc->prpl->auth_allow( cbd->ic, cbd->handle );
531       
532        g_free( cbd->handle );
533        g_free( cbd );
534}
535
536void imcb_ask_auth( struct im_connection *ic, const char *handle, const char *realname )
537{
538        struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 );
539        char *s, *realname_ = NULL;
540       
541        if( realname != NULL )
542                realname_ = g_strdup_printf( " (%s)", realname );
543       
544        s = g_strdup_printf( "The user %s%s wants to add you to his/her buddy list.",
545                             handle, realname_ ?: "" );
546       
547        g_free( realname_ );
548       
549        data->ic = ic;
550        data->handle = g_strdup( handle );
551        query_add( ic->irc, ic, s, imcb_ask_auth_cb_yes, imcb_ask_auth_cb_no, data );
552}
553
554
555static void imcb_ask_add_cb_no( void *data )
556{
557        g_free( ((struct imcb_ask_cb_data*)data)->handle );
558        g_free( data );
559}
560
561static void imcb_ask_add_cb_yes( void *data )
562{
563        struct imcb_ask_cb_data *cbd = data;
564       
565        cbd->ic->acc->prpl->add_buddy( cbd->ic, cbd->handle, NULL );
566       
567        return imcb_ask_add_cb_no( data );
568}
569
570void imcb_ask_add( struct im_connection *ic, const char *handle, const char *realname )
571{
572        struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 );
573        char *s;
574       
575        /* TODO: Make a setting for this! */
576        if( user_findhandle( ic, handle ) != NULL )
577                return;
578       
579        s = g_strdup_printf( "The user %s is not in your buddy list yet. Do you want to add him/her now?", handle );
580       
581        data->ic = ic;
582        data->handle = g_strdup( handle );
583        query_add( ic->irc, ic, s, imcb_ask_add_cb_yes, imcb_ask_add_cb_no, data );
584}
585
586
587/* server.c */                   
588
589void imcb_buddy_status( struct im_connection *ic, const char *handle, int flags, const char *state, const char *message )
590{
591        user_t *u;
592        int oa, oo;
593       
594        u = user_findhandle( ic, (char*) handle );
595       
596        if( !u )
597        {
598                if( g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "add" ) == 0 )
599                {
600                        imcb_add_buddy( ic, (char*) handle, NULL );
601                        u = user_findhandle( ic, (char*) handle );
602                }
603                else
604                {
605                        if( set_getbool( &ic->irc->set, "debug" ) || g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "ignore" ) != 0 )
606                        {
607                                imcb_log( ic, "imcb_buddy_status() for unknown handle %s:", handle );
608                                imcb_log( ic, "flags = %d, state = %s, message = %s", flags,
609                                          state ? state : "NULL", message ? message : "NULL" );
610                        }
611                       
612                        return;
613                }
614        }
615       
616        oa = u->away != NULL;
617        oo = u->online;
618       
619        if( u->away )
620        {
621                g_free( u->away );
622                u->away = NULL;
623        }
624       
625        if( ( flags & OPT_LOGGED_IN ) && !u->online )
626        {
627                irc_spawn( ic->irc, u );
628                u->online = 1;
629        }
630        else if( !( flags & OPT_LOGGED_IN ) && u->online )
631        {
632                struct groupchat *c;
633               
634                irc_kill( ic->irc, u );
635                u->online = 0;
636               
637                /* Remove him/her from the groupchats to prevent PART messages after he/she QUIT already */
638                for( c = ic->groupchats; c; c = c->next )
639                        remove_chat_buddy_silent( c, handle );
640        }
641       
642        if( flags & OPT_AWAY )
643        {
644                if( state && message )
645                {
646                        u->away = g_strdup_printf( "%s (%s)", state, message );
647                }
648                else if( state )
649                {
650                        u->away = g_strdup( state );
651                }
652                else if( message )
653                {
654                        u->away = g_strdup( message );
655                }
656                else
657                {
658                        u->away = g_strdup( "Away" );
659                }
660        }
661        /* else waste_any_state_information_for_now(); */
662       
663        /* LISPy... */
664        if( ( set_getbool( &ic->irc->set, "away_devoice" ) ) &&         /* Don't do a thing when user doesn't want it */
665            ( u->online ) &&                                            /* Don't touch offline people */
666            ( ( ( u->online != oo ) && !u->away ) ||                    /* Voice joining people */
667              ( ( u->online == oo ) && ( oa == !u->away ) ) ) )         /* (De)voice people changing state */
668        {
669                char *from;
670               
671                if( set_getbool( &ic->irc->set, "simulate_netsplit" ) )
672                {
673                        from = g_strdup( ic->irc->myhost );
674                }
675                else
676                {
677                        from = g_strdup_printf( "%s!%s@%s", ic->irc->mynick, ic->irc->mynick,
678                                                            ic->irc->myhost );
679                }
680                irc_write( ic->irc, ":%s MODE %s %cv %s", from, ic->irc->channel,
681                                                          u->away?'-':'+', u->nick );
682                g_free( from );
683        }
684}
685
686void imcb_buddy_msg( struct im_connection *ic, char *handle, char *msg, uint32_t flags, time_t sent_at )
687{
688        irc_t *irc = ic->irc;
689        char *wrapped;
690        user_t *u;
691       
692        u = user_findhandle( ic, handle );
693       
694        if( !u )
695        {
696                char *h = set_getstr( &irc->set, "handle_unknown" );
697               
698                if( g_strcasecmp( h, "ignore" ) == 0 )
699                {
700                        if( set_getbool( &irc->set, "debug" ) )
701                                imcb_log( ic, "Ignoring message from unknown handle %s", handle );
702                       
703                        return;
704                }
705                else if( g_strncasecmp( h, "add", 3 ) == 0 )
706                {
707                        int private = set_getbool( &irc->set, "private" );
708                       
709                        if( h[3] )
710                        {
711                                if( g_strcasecmp( h + 3, "_private" ) == 0 )
712                                        private = 1;
713                                else if( g_strcasecmp( h + 3, "_channel" ) == 0 )
714                                        private = 0;
715                        }
716                       
717                        imcb_add_buddy( ic, handle, NULL );
718                        u = user_findhandle( ic, handle );
719                        u->is_private = private;
720                }
721                else
722                {
723                        imcb_log( ic, "Message from unknown handle %s:", handle );
724                        u = user_find( irc, irc->mynick );
725                }
726        }
727       
728        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
729            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
730                strip_html( msg );
731
732        wrapped = word_wrap( msg, 425 );
733        irc_msgfrom( irc, u->nick, wrapped );
734        g_free( wrapped );
735}
736
737void imcb_buddy_typing( struct im_connection *ic, char *handle, uint32_t flags )
738{
739        user_t *u;
740       
741        if( !set_getbool( &ic->irc->set, "typing_notice" ) )
742                return;
743       
744        if( ( u = user_findhandle( ic, handle ) ) )
745        {
746                char buf[256]; 
747               
748                g_snprintf( buf, 256, "\1TYPING %d\1", ( flags >> 8 ) & 3 );
749                irc_privmsg( ic->irc, u, "PRIVMSG", ic->irc->nick, NULL, buf );
750        }
751}
752
753struct groupchat *imcb_chat_new( struct im_connection *ic, const char *handle )
754{
755        struct groupchat *c;
756       
757        /* This one just creates the conversation structure, user won't see anything yet */
758       
759        if( ic->groupchats )
760        {
761                for( c = ic->groupchats; c->next; c = c->next );
762                c = c->next = g_new0( struct groupchat, 1 );
763        }
764        else
765                ic->groupchats = c = g_new0( struct groupchat, 1 );
766       
767        c->ic = ic;
768        c->title = g_strdup( handle );
769        c->channel = g_strdup_printf( "&chat_%03d", ic->irc->c_id++ );
770        c->topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title );
771       
772        if( set_getbool( &ic->irc->set, "debug" ) )
773                imcb_log( ic, "Creating new conversation: (id=%p,handle=%s)", c, handle );
774       
775        return c;
776}
777
778void imcb_chat_free( struct groupchat *c )
779{
780        struct im_connection *ic = c->ic;
781        struct groupchat *l;
782        GList *ir;
783       
784        if( set_getbool( &ic->irc->set, "debug" ) )
785                imcb_log( ic, "You were removed from conversation %p", c );
786       
787        if( c )
788        {
789                if( c->joined )
790                {
791                        user_t *u, *r;
792                       
793                        r = user_find( ic->irc, ic->irc->mynick );
794                        irc_privmsg( ic->irc, r, "PRIVMSG", c->channel, "", "Cleaning up channel, bye!" );
795                       
796                        u = user_find( ic->irc, ic->irc->nick );
797                        irc_kick( ic->irc, u, c->channel, r );
798                        /* irc_part( ic->irc, u, c->channel ); */
799                }
800               
801                /* Find the previous chat in the linked list. */
802                for( l = ic->groupchats; l && l->next != c; l = l->next );
803               
804                if( l )
805                        l->next = c->next;
806                else
807                        ic->groupchats = c->next;
808               
809                for( ir = c->in_room; ir; ir = ir->next )
810                        g_free( ir->data );
811                g_list_free( c->in_room );
812                g_free( c->channel );
813                g_free( c->title );
814                g_free( c->topic );
815                g_free( c );
816        }
817}
818
819void imcb_chat_msg( struct groupchat *c, char *who, char *msg, uint32_t flags, time_t sent_at )
820{
821        struct im_connection *ic = c->ic;
822        char *wrapped;
823        user_t *u;
824       
825        /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */
826        if( g_strcasecmp( who, ic->acc->user ) == 0 )
827                return;
828       
829        u = user_findhandle( ic, who );
830       
831        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
832            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
833                strip_html( msg );
834       
835        wrapped = word_wrap( msg, 425 );
836        if( c && u )
837        {
838                irc_privmsg( ic->irc, u, "PRIVMSG", c->channel, "", wrapped );
839        }
840        else
841        {
842                imcb_log( ic, "Message from/to conversation %s@%p (unknown conv/user): %s", who, c, wrapped );
843        }
844        g_free( wrapped );
845}
846
847void imcb_chat_log( struct groupchat *c, char *format, ... )
848{
849        irc_t *irc = c->ic->irc;
850        va_list params;
851        char *text;
852        user_t *u;
853       
854        va_start( params, format );
855        text = g_strdup_vprintf( format, params );
856        va_end( params );
857       
858        u = user_find( irc, irc->mynick );
859       
860        irc_privmsg( irc, u, "PRIVMSG", c->channel, "System message: ", text );
861       
862        g_free( text );
863}
864
865void imcb_chat_topic( struct groupchat *c, char *who, char *topic, time_t set_at )
866{
867        struct im_connection *ic = c->ic;
868        user_t *u = NULL;
869       
870        if( who == NULL)
871                u = user_find( ic->irc, ic->irc->mynick );
872        else if( g_strcasecmp( who, ic->acc->user ) == 0 )
873                u = user_find( ic->irc, ic->irc->nick );
874        else
875                u = user_findhandle( ic, who );
876       
877        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
878            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
879                strip_html( topic );
880       
881        g_free( c->topic );
882        c->topic = g_strdup( topic );
883       
884        if( c->joined && u )
885                irc_write( ic->irc, ":%s!%s@%s TOPIC %s :%s", u->nick, u->user, u->host, c->channel, topic );
886}
887
888
889/* buddy_chat.c */
890
891void imcb_chat_add_buddy( struct groupchat *b, char *handle )
892{
893        user_t *u = user_findhandle( b->ic, handle );
894        int me = 0;
895       
896        if( set_getbool( &b->ic->irc->set, "debug" ) )
897                imcb_log( b->ic, "User %s added to conversation %p", handle, b );
898       
899        /* It might be yourself! */
900        if( b->ic->acc->prpl->handle_cmp( handle, b->ic->acc->user ) == 0 )
901        {
902                u = user_find( b->ic->irc, b->ic->irc->nick );
903                if( !b->joined )
904                        irc_join( b->ic->irc, u, b->channel );
905                b->joined = me = 1;
906        }
907       
908        /* Most protocols allow people to join, even when they're not in
909           your contact list. Try to handle that here */
910        if( !u )
911        {
912                imcb_add_buddy( b->ic, handle, NULL );
913                u = user_findhandle( b->ic, handle );
914        }
915       
916        /* Add the handle to the room userlist, if it's not 'me' */
917        if( !me )
918        {
919                if( b->joined )
920                        irc_join( b->ic->irc, u, b->channel );
921                b->in_room = g_list_append( b->in_room, g_strdup( handle ) );
922        }
923}
924
925/* This function is one BIG hack... :-( EREWRITE */
926void imcb_chat_remove_buddy( struct groupchat *b, char *handle, char *reason )
927{
928        user_t *u;
929        int me = 0;
930       
931        if( set_getbool( &b->ic->irc->set, "debug" ) )
932                imcb_log( b->ic, "User %s removed from conversation %p (%s)", handle, b, reason ? reason : "" );
933       
934        /* It might be yourself! */
935        if( g_strcasecmp( handle, b->ic->acc->user ) == 0 )
936        {
937                if( b->joined == 0 )
938                        return;
939               
940                u = user_find( b->ic->irc, b->ic->irc->nick );
941                b->joined = 0;
942                me = 1;
943        }
944        else
945        {
946                u = user_findhandle( b->ic, handle );
947        }
948       
949        if( me || ( remove_chat_buddy_silent( b, handle ) && b->joined && u ) )
950                irc_part( b->ic->irc, u, b->channel );
951}
952
953static int remove_chat_buddy_silent( struct groupchat *b, const char *handle )
954{
955        GList *i;
956       
957        /* Find the handle in the room userlist and shoot it */
958        i = b->in_room;
959        while( i )
960        {
961                if( g_strcasecmp( handle, i->data ) == 0 )
962                {
963                        g_free( i->data );
964                        b->in_room = g_list_remove( b->in_room, i->data );
965                        return( 1 );
966                }
967               
968                i = i->next;
969        }
970       
971        return( 0 );
972}
973
974
975/* Misc. BitlBee stuff which shouldn't really be here */
976
977char *set_eval_away_devoice( set_t *set, char *value )
978{
979        irc_t *irc = set->data;
980        int st;
981       
982        if( !is_bool( value ) )
983                return SET_INVALID;
984       
985        st = bool2int( value );
986       
987        /* Horror.... */
988       
989        if( st != set_getbool( &irc->set, "away_devoice" ) )
990        {
991                char list[80] = "";
992                user_t *u = irc->users;
993                int i = 0, count = 0;
994                char pm;
995                char v[80];
996               
997                if( st )
998                        pm = '+';
999                else
1000                        pm = '-';
1001               
1002                while( u )
1003                {
1004                        if( u->ic && u->online && !u->away )
1005                        {
1006                                if( ( strlen( list ) + strlen( u->nick ) ) >= 79 )
1007                                {
1008                                        for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
1009                                        irc_write( irc, ":%s MODE %s %c%s%s",
1010                                                   irc->myhost,
1011                                                   irc->channel, pm, v, list );
1012                                       
1013                                        *list = 0;
1014                                        count = 0;
1015                                }
1016                               
1017                                sprintf( list + strlen( list ), " %s", u->nick );
1018                                count ++;
1019                        }
1020                        u = u->next;
1021                }
1022               
1023                /* $v = 'v' x $i */
1024                for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
1025                irc_write( irc, ":%s MODE %s %c%s%s", irc->myhost,
1026                                                            irc->channel, pm, v, list );
1027        }
1028       
1029        return value;
1030}
1031
1032
1033
1034
1035/* The plan is to not allow straight calls to prpl functions anymore, but do
1036   them all from some wrappers. We'll start to define some down here: */
1037
1038int imc_buddy_msg( struct im_connection *ic, char *handle, char *msg, int flags )
1039{
1040        char *buf = NULL;
1041        int st;
1042       
1043        if( ( ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
1044        {
1045                buf = escape_html( msg );
1046                msg = buf;
1047        }
1048       
1049        st = ic->acc->prpl->buddy_msg( ic, handle, msg, flags );
1050        g_free( buf );
1051       
1052        return st;
1053}
1054
1055int imc_chat_msg( struct groupchat *c, char *msg, int flags )
1056{
1057        char *buf = NULL;
1058       
1059        if( ( c->ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
1060        {
1061                buf = escape_html( msg );
1062                msg = buf;
1063        }
1064       
1065        c->ic->acc->prpl->chat_msg( c, msg, flags );
1066        g_free( buf );
1067       
1068        return 1;
1069}
1070
1071static char *imc_away_state_find( GList *gcm, char *away, char **message );
1072
1073int imc_away_send_update( struct im_connection *ic )
1074{
1075        char *away, *msg = NULL;
1076       
1077        away = set_getstr( &ic->acc->set, "away" ) ?
1078             : set_getstr( &ic->irc->set, "away" );
1079        if( away && *away )
1080        {
1081                GList *m = ic->acc->prpl->away_states( ic );
1082                msg = ic->acc->flags & ACC_FLAG_AWAY_MESSAGE ? away : NULL;
1083                away = imc_away_state_find( m, away, &msg ) ? : m->data;
1084        }
1085        else if( ic->acc->flags & ACC_FLAG_STATUS_MESSAGE )
1086        {
1087                away = NULL;
1088                msg = set_getstr( &ic->acc->set, "status" ) ?
1089                    : set_getstr( &ic->irc->set, "status" );
1090        }
1091       
1092        ic->acc->prpl->set_away( ic, away, msg );
1093       
1094        return 1;
1095}
1096
1097static char *imc_away_alias_list[8][5] =
1098{
1099        { "Away from computer", "Away", "Extended away", NULL },
1100        { "NA", "N/A", "Not available", NULL },
1101        { "Busy", "Do not disturb", "DND", "Occupied", NULL },
1102        { "Be right back", "BRB", NULL },
1103        { "On the phone", "Phone", "On phone", NULL },
1104        { "Out to lunch", "Lunch", "Food", NULL },
1105        { "Invisible", "Hidden" },
1106        { NULL }
1107};
1108
1109static char *imc_away_state_find( GList *gcm, char *away, char **message )
1110{
1111        GList *m;
1112        int i, j;
1113       
1114        for( m = gcm; m; m = m->next )
1115                if( g_strncasecmp( m->data, away, strlen( m->data ) ) == 0 )
1116                {
1117                        /* At least the Yahoo! module works better if message
1118                           contains no data unless it adds something to what
1119                           we have in state already. */
1120                        if( strlen( m->data ) == strlen( away ) )
1121                                *message = NULL;
1122                       
1123                        return m->data;
1124                }
1125       
1126        for( i = 0; *imc_away_alias_list[i]; i ++ )
1127        {
1128                int keep_message;
1129               
1130                for( j = 0; imc_away_alias_list[i][j]; j ++ )
1131                        if( g_strncasecmp( away, imc_away_alias_list[i][j], strlen( imc_away_alias_list[i][j] ) ) == 0 )
1132                        {
1133                                keep_message = strlen( away ) != strlen( imc_away_alias_list[i][j] );
1134                                break;
1135                        }
1136               
1137                if( !imc_away_alias_list[i][j] )        /* If we reach the end, this row */
1138                        continue;                       /* is not what we want. Next!    */
1139               
1140                /* Now find an entry in this row which exists in gcm */
1141                for( j = 0; imc_away_alias_list[i][j]; j ++ )
1142                {
1143                        for( m = gcm; m; m = m->next )
1144                                if( g_strcasecmp( imc_away_alias_list[i][j], m->data ) == 0 )
1145                                {
1146                                        if( !keep_message )
1147                                                *message = NULL;
1148                                       
1149                                        return imc_away_alias_list[i][j];
1150                                }
1151                }
1152               
1153                /* No need to look further, apparently this state doesn't
1154                   have any good alias for this protocol. */
1155                break;
1156        }
1157       
1158        return NULL;
1159}
1160
1161void imc_add_allow( struct im_connection *ic, char *handle )
1162{
1163        if( g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL )
1164        {
1165                ic->permit = g_slist_prepend( ic->permit, g_strdup( handle ) );
1166        }
1167       
1168        ic->acc->prpl->add_permit( ic, handle );
1169}
1170
1171void imc_rem_allow( struct im_connection *ic, char *handle )
1172{
1173        GSList *l;
1174       
1175        if( ( l = g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) )
1176        {
1177                g_free( l->data );
1178                ic->permit = g_slist_delete_link( ic->permit, l );
1179        }
1180       
1181        ic->acc->prpl->rem_permit( ic, handle );
1182}
1183
1184void imc_add_block( struct im_connection *ic, char *handle )
1185{
1186        if( g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL )
1187        {
1188                ic->deny = g_slist_prepend( ic->deny, g_strdup( handle ) );
1189        }
1190       
1191        ic->acc->prpl->add_deny( ic, handle );
1192}
1193
1194void imc_rem_block( struct im_connection *ic, char *handle )
1195{
1196        GSList *l;
1197       
1198        if( ( l = g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) )
1199        {
1200                g_free( l->data );
1201                ic->deny = g_slist_delete_link( ic->deny, l );
1202        }
1203       
1204        ic->acc->prpl->rem_deny( ic, handle );
1205}
1206
1207void imcb_clean_handle( struct im_connection *ic, char *handle )
1208{
1209        /* Accepts a handle and does whatever is necessary to make it
1210           BitlBee-friendly. Currently this means removing everything
1211           outside 33-127 (ASCII printable excl spaces), @ (only one
1212           is allowed) and ! and : */
1213        char out[strlen(handle)+1];
1214        int s, d;
1215       
1216        s = d = 0;
1217        while( handle[s] )
1218        {
1219                if( handle[s] > ' ' && handle[s] != '!' && handle[s] != ':' &&
1220                    ( handle[s] & 0x80 ) == 0 )
1221                {
1222                        if( handle[s] == '@' )
1223                        {
1224                                /* See if we got an @ already? */
1225                                out[d] = 0;
1226                                if( strchr( out, '@' ) )
1227                                        continue;
1228                        }
1229                       
1230                        out[d++] = handle[s];
1231                }
1232                s ++;
1233        }
1234        out[d] = handle[s];
1235       
1236        strcpy( handle, out );
1237}
Note: See TracBrowser for help on using the repository browser.