source: protocols/nogaim.c @ e08e53c

Last change on this file since e08e53c was cd741d8, checked in by Wilmer van der Gaast <wilmer@…>, at 2009-11-23T23:23:37Z

Fixed compatibility with non-libpurple version: oscar is now recognized
as a protocol name, and removed prpl- hack from nogaim.c.

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