source: protocols/nogaim.c @ 20e830b

Last change on this file since 20e830b was 4cf80bb, checked in by Wilmer van der Gaast <wilmer@…>, at 2009-10-23T10:43:15Z

Shuffling a few more #includes. Much more shuffling will be needed.

  • Property mode set to 100644
File size: 28.3 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        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        /* Also necessary when we're not away, at least for some of the
271           protocols. */
272        imc_set_away( ic, u->away );
273       
274        /* Apparently we're connected successfully, so reset the
275           exponential backoff timer. */
276        ic->acc->auto_reconnect_delay = 0;
277       
278        for( c = irc->chatrooms; c; c = c->next )
279        {
280                if( c->acc != ic->acc )
281                        continue;
282               
283                if( set_getbool( &c->set, "auto_join" ) )
284                        chat_join( irc, c, NULL );
285        }
286}
287
288gboolean auto_reconnect( gpointer data, gint fd, b_input_condition cond )
289{
290        account_t *a = data;
291       
292        a->reconnect = 0;
293        account_on( a->irc, a );
294       
295        return( FALSE );        /* Only have to run the timeout once */
296}
297
298void cancel_auto_reconnect( account_t *a )
299{
300        b_event_remove( a->reconnect );
301        a->reconnect = 0;
302}
303
304void imc_logout( struct im_connection *ic, int allow_reconnect )
305{
306        irc_t *irc = ic->irc;
307        user_t *t, *u;
308        account_t *a;
309        int delay;
310       
311        /* Nested calls might happen sometimes, this is probably the best
312           place to catch them. */
313        if( ic->flags & OPT_LOGGING_OUT )
314                return;
315        else
316                ic->flags |= OPT_LOGGING_OUT;
317       
318        imcb_log( ic, "Signing off.." );
319       
320        b_event_remove( ic->keepalive );
321        ic->keepalive = 0;
322        ic->acc->prpl->logout( ic );
323        b_event_remove( ic->inpa );
324       
325        g_free( ic->away );
326        ic->away = NULL;
327       
328        u = irc->users;
329        while( u )
330        {
331                if( u->ic == ic )
332                {
333                        t = u->next;
334                        user_del( irc, u->nick );
335                        u = t;
336                }
337                else
338                        u = u->next;
339        }
340       
341        query_del_by_conn( ic->irc, ic );
342       
343        for( a = irc->accounts; a; a = a->next )
344                if( a->ic == ic )
345                        break;
346       
347        if( !a )
348        {
349                /* Uhm... This is very sick. */
350        }
351        else if( allow_reconnect && set_getbool( &irc->set, "auto_reconnect" ) &&
352                 set_getbool( &a->set, "auto_reconnect" ) &&
353                 ( delay = account_reconnect_delay( a ) ) > 0 )
354        {
355                imcb_log( ic, "Reconnecting in %d seconds..", delay );
356                a->reconnect = b_timeout_add( delay * 1000, auto_reconnect, a );
357        }
358       
359        imc_free( ic );
360}
361
362
363/* dialogs.c */
364
365void imcb_ask( struct im_connection *ic, char *msg, void *data,
366               query_callback doit, query_callback dont )
367{
368        query_add( ic->irc, ic, msg, doit, dont, data );
369}
370
371
372/* list.c */
373
374void imcb_add_buddy( struct im_connection *ic, char *handle, char *group )
375{
376        user_t *u;
377        char nick[MAX_NICK_LENGTH+1], *s;
378        irc_t *irc = ic->irc;
379       
380        if( user_findhandle( ic, handle ) )
381        {
382                if( set_getbool( &irc->set, "debug" ) )
383                        imcb_log( ic, "User already exists, ignoring add request: %s", handle );
384               
385                return;
386               
387                /* Buddy seems to exist already. Let's ignore this request then...
388                   Eventually subsequent calls to this function *should* be possible
389                   when a buddy is in multiple groups. But for now BitlBee doesn't
390                   even support groups so let's silently ignore this for now. */
391        }
392       
393        memset( nick, 0, MAX_NICK_LENGTH + 1 );
394        strcpy( nick, nick_get( ic->acc, handle ) );
395       
396        u = user_add( ic->irc, nick );
397       
398//      if( !realname || !*realname ) realname = nick;
399//      u->realname = g_strdup( realname );
400       
401        if( ( s = strchr( handle, '@' ) ) )
402        {
403                u->host = g_strdup( s + 1 );
404                u->user = g_strndup( handle, s - handle );
405        }
406        else if( ic->acc->server )
407        {
408                u->host = g_strdup( ic->acc->server );
409                u->user = g_strdup( handle );
410               
411                /* s/ /_/ ... important for AOL screennames */
412                for( s = u->user; *s; s ++ )
413                        if( *s == ' ' )
414                                *s = '_';
415        }
416        else
417        {
418                u->host = g_strdup( ic->acc->prpl->name );
419                u->user = g_strdup( handle );
420        }
421       
422        u->ic = ic;
423        u->handle = g_strdup( handle );
424        if( group ) u->group = g_strdup( group );
425        u->send_handler = buddy_send_handler;
426        u->last_typing_notice = 0;
427}
428
429struct buddy *imcb_find_buddy( struct im_connection *ic, char *handle )
430{
431        static struct buddy b[1];
432        user_t *u;
433       
434        u = user_findhandle( ic, handle );
435       
436        if( !u )
437                return( NULL );
438       
439        memset( b, 0, sizeof( b ) );
440        strncpy( b->name, handle, 80 );
441        strncpy( b->show, u->realname, BUDDY_ALIAS_MAXLEN );
442        b->present = u->online;
443        b->ic = u->ic;
444       
445        return( b );
446}
447
448void imcb_rename_buddy( struct im_connection *ic, char *handle, char *realname )
449{
450        user_t *u = user_findhandle( ic, handle );
451       
452        if( !u || !realname ) return;
453       
454        if( g_strcasecmp( u->realname, realname ) != 0 )
455        {
456                if( u->realname != u->nick ) g_free( u->realname );
457               
458                u->realname = g_strdup( realname );
459               
460                if( ( ic->flags & OPT_LOGGED_IN ) && set_getbool( &ic->irc->set, "display_namechanges" ) )
461                        imcb_log( ic, "User `%s' changed name to `%s'", u->nick, u->realname );
462        }
463}
464
465void imcb_remove_buddy( struct im_connection *ic, char *handle, char *group )
466{
467        user_t *u;
468       
469        if( ( u = user_findhandle( ic, handle ) ) )
470                user_del( ic->irc, u->nick );
471}
472
473/* Mainly meant for ICQ (and now also for Jabber conferences) to allow IM
474   modules to suggest a nickname for a handle. */
475void imcb_buddy_nick_hint( struct im_connection *ic, char *handle, char *nick )
476{
477        user_t *u = user_findhandle( ic, handle );
478        char newnick[MAX_NICK_LENGTH+1], *orig_nick;
479       
480        if( u && !u->online && !nick_saved( ic->acc, handle ) )
481        {
482                /* Only do this if the person isn't online yet (which should
483                   be the case if we just added it) and if the user hasn't
484                   assigned a nickname to this buddy already. */
485               
486                strncpy( newnick, nick, MAX_NICK_LENGTH );
487                newnick[MAX_NICK_LENGTH] = 0;
488               
489                /* Some processing to make sure this string is a valid IRC nickname. */
490                nick_strip( newnick );
491                if( set_getbool( &ic->irc->set, "lcnicks" ) )
492                        nick_lc( newnick );
493               
494                if( strcmp( u->nick, newnick ) != 0 )
495                {
496                        /* Only do this if newnick is different from the current one.
497                           If rejoining a channel, maybe we got this nick already
498                           (and dedupe would only add an underscore. */
499                        nick_dedupe( ic->acc, handle, newnick );
500                       
501                        /* u->nick will be freed halfway the process, so it can't be
502                           passed as an argument. */
503                        orig_nick = g_strdup( u->nick );
504                        user_rename( ic->irc, orig_nick, newnick );
505                        g_free( orig_nick );
506                }
507        }
508}
509
510
511struct imcb_ask_cb_data
512{
513        struct im_connection *ic;
514        char *handle;
515};
516
517static void imcb_ask_auth_cb_no( void *data )
518{
519        struct imcb_ask_cb_data *cbd = data;
520       
521        cbd->ic->acc->prpl->auth_deny( cbd->ic, cbd->handle );
522       
523        g_free( cbd->handle );
524        g_free( cbd );
525}
526
527static void imcb_ask_auth_cb_yes( void *data )
528{
529        struct imcb_ask_cb_data *cbd = data;
530       
531        cbd->ic->acc->prpl->auth_allow( cbd->ic, cbd->handle );
532       
533        g_free( cbd->handle );
534        g_free( cbd );
535}
536
537void imcb_ask_auth( struct im_connection *ic, const char *handle, const char *realname )
538{
539        struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 );
540        char *s, *realname_ = NULL;
541       
542        if( realname != NULL )
543                realname_ = g_strdup_printf( " (%s)", realname );
544       
545        s = g_strdup_printf( "The user %s%s wants to add you to his/her buddy list.",
546                             handle, realname_ ?: "" );
547       
548        g_free( realname_ );
549       
550        data->ic = ic;
551        data->handle = g_strdup( handle );
552        query_add( ic->irc, ic, s, imcb_ask_auth_cb_yes, imcb_ask_auth_cb_no, data );
553}
554
555
556static void imcb_ask_add_cb_no( void *data )
557{
558        g_free( ((struct imcb_ask_cb_data*)data)->handle );
559        g_free( data );
560}
561
562static void imcb_ask_add_cb_yes( void *data )
563{
564        struct imcb_ask_cb_data *cbd = data;
565       
566        cbd->ic->acc->prpl->add_buddy( cbd->ic, cbd->handle, NULL );
567       
568        return imcb_ask_add_cb_no( data );
569}
570
571void imcb_ask_add( struct im_connection *ic, const char *handle, const char *realname )
572{
573        struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 );
574        char *s;
575       
576        /* TODO: Make a setting for this! */
577        if( user_findhandle( ic, handle ) != NULL )
578                return;
579       
580        s = g_strdup_printf( "The user %s is not in your buddy list yet. Do you want to add him/her now?", handle );
581       
582        data->ic = ic;
583        data->handle = g_strdup( handle );
584        query_add( ic->irc, ic, s, imcb_ask_add_cb_yes, imcb_ask_add_cb_no, data );
585}
586
587
588/* server.c */                   
589
590void imcb_buddy_status( struct im_connection *ic, const char *handle, int flags, const char *state, const char *message )
591{
592        user_t *u;
593        int oa, oo;
594       
595        u = user_findhandle( ic, (char*) handle );
596       
597        if( !u )
598        {
599                if( g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "add" ) == 0 )
600                {
601                        imcb_add_buddy( ic, (char*) handle, NULL );
602                        u = user_findhandle( ic, (char*) handle );
603                }
604                else
605                {
606                        if( set_getbool( &ic->irc->set, "debug" ) || g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "ignore" ) != 0 )
607                        {
608                                imcb_log( ic, "imcb_buddy_status() for unknown handle %s:", handle );
609                                imcb_log( ic, "flags = %d, state = %s, message = %s", flags,
610                                          state ? state : "NULL", message ? message : "NULL" );
611                        }
612                       
613                        return;
614                }
615        }
616       
617        oa = u->away != NULL;
618        oo = u->online;
619       
620        if( u->away )
621        {
622                g_free( u->away );
623                u->away = NULL;
624        }
625       
626        if( ( flags & OPT_LOGGED_IN ) && !u->online )
627        {
628                irc_spawn( ic->irc, u );
629                u->online = 1;
630        }
631        else if( !( flags & OPT_LOGGED_IN ) && u->online )
632        {
633                struct groupchat *c;
634               
635                irc_kill( ic->irc, u );
636                u->online = 0;
637               
638                /* Remove him/her from the groupchats to prevent PART messages after he/she QUIT already */
639                for( c = ic->groupchats; c; c = c->next )
640                        remove_chat_buddy_silent( c, handle );
641        }
642       
643        if( flags & OPT_AWAY )
644        {
645                if( state && message )
646                {
647                        u->away = g_strdup_printf( "%s (%s)", state, message );
648                }
649                else if( state )
650                {
651                        u->away = g_strdup( state );
652                }
653                else if( message )
654                {
655                        u->away = g_strdup( message );
656                }
657                else
658                {
659                        u->away = g_strdup( "Away" );
660                }
661        }
662        /* else waste_any_state_information_for_now(); */
663       
664        /* LISPy... */
665        if( ( set_getbool( &ic->irc->set, "away_devoice" ) ) &&         /* Don't do a thing when user doesn't want it */
666            ( u->online ) &&                                            /* Don't touch offline people */
667            ( ( ( u->online != oo ) && !u->away ) ||                    /* Voice joining people */
668              ( ( u->online == oo ) && ( oa == !u->away ) ) ) )         /* (De)voice people changing state */
669        {
670                char *from;
671               
672                if( set_getbool( &ic->irc->set, "simulate_netsplit" ) )
673                {
674                        from = g_strdup( ic->irc->myhost );
675                }
676                else
677                {
678                        from = g_strdup_printf( "%s!%s@%s", ic->irc->mynick, ic->irc->mynick,
679                                                            ic->irc->myhost );
680                }
681                irc_write( ic->irc, ":%s MODE %s %cv %s", from, ic->irc->channel,
682                                                          u->away?'-':'+', u->nick );
683                g_free( from );
684        }
685}
686
687void imcb_buddy_msg( struct im_connection *ic, char *handle, char *msg, uint32_t flags, time_t sent_at )
688{
689        irc_t *irc = ic->irc;
690        char *wrapped;
691        user_t *u;
692       
693        u = user_findhandle( ic, handle );
694       
695        if( !u )
696        {
697                char *h = set_getstr( &irc->set, "handle_unknown" );
698               
699                if( g_strcasecmp( h, "ignore" ) == 0 )
700                {
701                        if( set_getbool( &irc->set, "debug" ) )
702                                imcb_log( ic, "Ignoring message from unknown handle %s", handle );
703                       
704                        return;
705                }
706                else if( g_strncasecmp( h, "add", 3 ) == 0 )
707                {
708                        int private = set_getbool( &irc->set, "private" );
709                       
710                        if( h[3] )
711                        {
712                                if( g_strcasecmp( h + 3, "_private" ) == 0 )
713                                        private = 1;
714                                else if( g_strcasecmp( h + 3, "_channel" ) == 0 )
715                                        private = 0;
716                        }
717                       
718                        imcb_add_buddy( ic, handle, NULL );
719                        u = user_findhandle( ic, handle );
720                        u->is_private = private;
721                }
722                else
723                {
724                        imcb_log( ic, "Message from unknown handle %s:", handle );
725                        u = user_find( irc, irc->mynick );
726                }
727        }
728       
729        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
730            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
731                strip_html( msg );
732
733        wrapped = word_wrap( msg, 425 );
734        irc_msgfrom( irc, u->nick, wrapped );
735        g_free( wrapped );
736}
737
738void imcb_buddy_typing( struct im_connection *ic, char *handle, uint32_t flags )
739{
740        user_t *u;
741       
742        if( !set_getbool( &ic->irc->set, "typing_notice" ) )
743                return;
744       
745        if( ( u = user_findhandle( ic, handle ) ) )
746        {
747                char buf[256]; 
748               
749                g_snprintf( buf, 256, "\1TYPING %d\1", ( flags >> 8 ) & 3 );
750                irc_privmsg( ic->irc, u, "PRIVMSG", ic->irc->nick, NULL, buf );
751        }
752}
753
754struct groupchat *imcb_chat_new( struct im_connection *ic, const char *handle )
755{
756        struct groupchat *c;
757       
758        /* This one just creates the conversation structure, user won't see anything yet */
759       
760        if( ic->groupchats )
761        {
762                for( c = ic->groupchats; c->next; c = c->next );
763                c = c->next = g_new0( struct groupchat, 1 );
764        }
765        else
766                ic->groupchats = c = g_new0( struct groupchat, 1 );
767       
768        c->ic = ic;
769        c->title = g_strdup( handle );
770        c->channel = g_strdup_printf( "&chat_%03d", ic->irc->c_id++ );
771        c->topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title );
772       
773        if( set_getbool( &ic->irc->set, "debug" ) )
774                imcb_log( ic, "Creating new conversation: (id=%p,handle=%s)", c, handle );
775       
776        return c;
777}
778
779void imcb_chat_free( struct groupchat *c )
780{
781        struct im_connection *ic = c->ic;
782        struct groupchat *l;
783        GList *ir;
784       
785        if( set_getbool( &ic->irc->set, "debug" ) )
786                imcb_log( ic, "You were removed from conversation %p", c );
787       
788        if( c )
789        {
790                if( c->joined )
791                {
792                        user_t *u, *r;
793                       
794                        r = user_find( ic->irc, ic->irc->mynick );
795                        irc_privmsg( ic->irc, r, "PRIVMSG", c->channel, "", "Cleaning up channel, bye!" );
796                       
797                        u = user_find( ic->irc, ic->irc->nick );
798                        irc_kick( ic->irc, u, c->channel, r );
799                        /* irc_part( ic->irc, u, c->channel ); */
800                }
801               
802                /* Find the previous chat in the linked list. */
803                for( l = ic->groupchats; l && l->next != c; l = l->next );
804               
805                if( l )
806                        l->next = c->next;
807                else
808                        ic->groupchats = c->next;
809               
810                for( ir = c->in_room; ir; ir = ir->next )
811                        g_free( ir->data );
812                g_list_free( c->in_room );
813                g_free( c->channel );
814                g_free( c->title );
815                g_free( c->topic );
816                g_free( c );
817        }
818}
819
820void imcb_chat_msg( struct groupchat *c, char *who, char *msg, uint32_t flags, time_t sent_at )
821{
822        struct im_connection *ic = c->ic;
823        char *wrapped;
824        user_t *u;
825       
826        /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */
827        if( g_strcasecmp( who, ic->acc->user ) == 0 )
828                return;
829       
830        u = user_findhandle( ic, who );
831       
832        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
833            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
834                strip_html( msg );
835       
836        wrapped = word_wrap( msg, 425 );
837        if( c && u )
838        {
839                irc_privmsg( ic->irc, u, "PRIVMSG", c->channel, "", wrapped );
840        }
841        else
842        {
843                imcb_log( ic, "Message from/to conversation %s@%p (unknown conv/user): %s", who, c, wrapped );
844        }
845        g_free( wrapped );
846}
847
848void imcb_chat_log( struct groupchat *c, char *format, ... )
849{
850        irc_t *irc = c->ic->irc;
851        va_list params;
852        char *text;
853        user_t *u;
854       
855        va_start( params, format );
856        text = g_strdup_vprintf( format, params );
857        va_end( params );
858       
859        u = user_find( irc, irc->mynick );
860       
861        irc_privmsg( irc, u, "PRIVMSG", c->channel, "System message: ", text );
862       
863        g_free( text );
864}
865
866void imcb_chat_topic( struct groupchat *c, char *who, char *topic, time_t set_at )
867{
868        struct im_connection *ic = c->ic;
869        user_t *u = NULL;
870       
871        if( who == NULL)
872                u = user_find( ic->irc, ic->irc->mynick );
873        else if( g_strcasecmp( who, ic->acc->user ) == 0 )
874                u = user_find( ic->irc, ic->irc->nick );
875        else
876                u = user_findhandle( ic, who );
877       
878        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
879            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
880                strip_html( topic );
881       
882        g_free( c->topic );
883        c->topic = g_strdup( topic );
884       
885        if( c->joined && u )
886                irc_write( ic->irc, ":%s!%s@%s TOPIC %s :%s", u->nick, u->user, u->host, c->channel, topic );
887}
888
889
890/* buddy_chat.c */
891
892void imcb_chat_add_buddy( struct groupchat *b, char *handle )
893{
894        user_t *u = user_findhandle( b->ic, handle );
895        int me = 0;
896       
897        if( set_getbool( &b->ic->irc->set, "debug" ) )
898                imcb_log( b->ic, "User %s added to conversation %p", handle, b );
899       
900        /* It might be yourself! */
901        if( b->ic->acc->prpl->handle_cmp( handle, b->ic->acc->user ) == 0 )
902        {
903                u = user_find( b->ic->irc, b->ic->irc->nick );
904                if( !b->joined )
905                        irc_join( b->ic->irc, u, b->channel );
906                b->joined = me = 1;
907        }
908       
909        /* Most protocols allow people to join, even when they're not in
910           your contact list. Try to handle that here */
911        if( !u )
912        {
913                imcb_add_buddy( b->ic, handle, NULL );
914                u = user_findhandle( b->ic, handle );
915        }
916       
917        /* Add the handle to the room userlist, if it's not 'me' */
918        if( !me )
919        {
920                if( b->joined )
921                        irc_join( b->ic->irc, u, b->channel );
922                b->in_room = g_list_append( b->in_room, g_strdup( handle ) );
923        }
924}
925
926/* This function is one BIG hack... :-( EREWRITE */
927void imcb_chat_remove_buddy( struct groupchat *b, char *handle, char *reason )
928{
929        user_t *u;
930        int me = 0;
931       
932        if( set_getbool( &b->ic->irc->set, "debug" ) )
933                imcb_log( b->ic, "User %s removed from conversation %p (%s)", handle, b, reason ? reason : "" );
934       
935        /* It might be yourself! */
936        if( g_strcasecmp( handle, b->ic->acc->user ) == 0 )
937        {
938                if( b->joined == 0 )
939                        return;
940               
941                u = user_find( b->ic->irc, b->ic->irc->nick );
942                b->joined = 0;
943                me = 1;
944        }
945        else
946        {
947                u = user_findhandle( b->ic, handle );
948        }
949       
950        if( me || ( remove_chat_buddy_silent( b, handle ) && b->joined && u ) )
951                irc_part( b->ic->irc, u, b->channel );
952}
953
954static int remove_chat_buddy_silent( struct groupchat *b, const char *handle )
955{
956        GList *i;
957       
958        /* Find the handle in the room userlist and shoot it */
959        i = b->in_room;
960        while( i )
961        {
962                if( g_strcasecmp( handle, i->data ) == 0 )
963                {
964                        g_free( i->data );
965                        b->in_room = g_list_remove( b->in_room, i->data );
966                        return( 1 );
967                }
968               
969                i = i->next;
970        }
971       
972        return( 0 );
973}
974
975
976/* Misc. BitlBee stuff which shouldn't really be here */
977
978char *set_eval_away_devoice( set_t *set, char *value )
979{
980        irc_t *irc = set->data;
981        int st;
982       
983        if( !is_bool( value ) )
984                return SET_INVALID;
985       
986        st = bool2int( value );
987       
988        /* Horror.... */
989       
990        if( st != set_getbool( &irc->set, "away_devoice" ) )
991        {
992                char list[80] = "";
993                user_t *u = irc->users;
994                int i = 0, count = 0;
995                char pm;
996                char v[80];
997               
998                if( st )
999                        pm = '+';
1000                else
1001                        pm = '-';
1002               
1003                while( u )
1004                {
1005                        if( u->ic && u->online && !u->away )
1006                        {
1007                                if( ( strlen( list ) + strlen( u->nick ) ) >= 79 )
1008                                {
1009                                        for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
1010                                        irc_write( irc, ":%s MODE %s %c%s%s",
1011                                                   irc->myhost,
1012                                                   irc->channel, pm, v, list );
1013                                       
1014                                        *list = 0;
1015                                        count = 0;
1016                                }
1017                               
1018                                sprintf( list + strlen( list ), " %s", u->nick );
1019                                count ++;
1020                        }
1021                        u = u->next;
1022                }
1023               
1024                /* $v = 'v' x $i */
1025                for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
1026                irc_write( irc, ":%s MODE %s %c%s%s", irc->myhost,
1027                                                            irc->channel, pm, v, list );
1028        }
1029       
1030        return value;
1031}
1032
1033
1034
1035
1036/* The plan is to not allow straight calls to prpl functions anymore, but do
1037   them all from some wrappers. We'll start to define some down here: */
1038
1039int imc_buddy_msg( struct im_connection *ic, char *handle, char *msg, int flags )
1040{
1041        char *buf = NULL;
1042        int st;
1043       
1044        if( ( ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
1045        {
1046                buf = escape_html( msg );
1047                msg = buf;
1048        }
1049       
1050        st = ic->acc->prpl->buddy_msg( ic, handle, msg, flags );
1051        g_free( buf );
1052       
1053        return st;
1054}
1055
1056int imc_chat_msg( struct groupchat *c, char *msg, int flags )
1057{
1058        char *buf = NULL;
1059       
1060        if( ( c->ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
1061        {
1062                buf = escape_html( msg );
1063                msg = buf;
1064        }
1065       
1066        c->ic->acc->prpl->chat_msg( c, msg, flags );
1067        g_free( buf );
1068       
1069        return 1;
1070}
1071
1072static char *imc_away_alias_find( GList *gcm, char *away );
1073
1074int imc_set_away( struct im_connection *ic, char *away )
1075{
1076        GList *m, *ms;
1077        char *s;
1078       
1079        if( !away ) away = "";
1080        ms = m = ic->acc->prpl->away_states( ic );
1081       
1082        while( m )
1083        {
1084                if( *away )
1085                {
1086                        if( g_strncasecmp( m->data, away, strlen( m->data ) ) == 0 )
1087                                break;
1088                }
1089                else
1090                {
1091                        if( g_strcasecmp( m->data, "Available" ) == 0 )
1092                                break;
1093                        if( g_strcasecmp( m->data, "Online" ) == 0 )
1094                                break;
1095                }
1096                m = m->next;
1097        }
1098       
1099        if( m )
1100        {
1101                ic->acc->prpl->set_away( ic, m->data, *away ? away : NULL );
1102        }
1103        else
1104        {
1105                s = imc_away_alias_find( ms, away );
1106                if( s )
1107                {
1108                        ic->acc->prpl->set_away( ic, s, away );
1109                        if( set_getbool( &ic->irc->set, "debug" ) )
1110                                imcb_log( ic, "Setting away state to %s", s );
1111                }
1112                else
1113                        ic->acc->prpl->set_away( ic, GAIM_AWAY_CUSTOM, away );
1114        }
1115       
1116        return( 1 );
1117}
1118
1119static char *imc_away_alias_list[8][5] =
1120{
1121        { "Away from computer", "Away", "Extended away", NULL },
1122        { "NA", "N/A", "Not available", NULL },
1123        { "Busy", "Do not disturb", "DND", "Occupied", NULL },
1124        { "Be right back", "BRB", NULL },
1125        { "On the phone", "Phone", "On phone", NULL },
1126        { "Out to lunch", "Lunch", "Food", NULL },
1127        { "Invisible", "Hidden" },
1128        { NULL }
1129};
1130
1131static char *imc_away_alias_find( GList *gcm, char *away )
1132{
1133        GList *m;
1134        int i, j;
1135       
1136        for( i = 0; *imc_away_alias_list[i]; i ++ )
1137        {
1138                for( j = 0; imc_away_alias_list[i][j]; j ++ )
1139                        if( g_strncasecmp( away, imc_away_alias_list[i][j], strlen( imc_away_alias_list[i][j] ) ) == 0 )
1140                                break;
1141               
1142                if( !imc_away_alias_list[i][j] )        /* If we reach the end, this row */
1143                        continue;                       /* is not what we want. Next!    */
1144               
1145                /* Now find an entry in this row which exists in gcm */
1146                for( j = 0; imc_away_alias_list[i][j]; j ++ )
1147                {
1148                        m = gcm;
1149                        while( m )
1150                        {
1151                                if( g_strcasecmp( imc_away_alias_list[i][j], m->data ) == 0 )
1152                                        return( imc_away_alias_list[i][j] );
1153                                m = m->next;
1154                        }
1155                }
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.