source: protocols/nogaim.c @ ceebeb1

Last change on this file since ceebeb1 was fb00989, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-03-14T17:55:27Z

Adding a few consts and other misc fixes from bug #431. Doing this via a
merge because bzr can probably deal with the conflicts better than patch.

  • Property mode set to 100644
File size: 29.1 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, const char *handle, const 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, const char *handle, const char *realname )
448{
449        user_t *u = user_findhandle( ic, handle );
450        char *set;
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        set = set_getstr( &ic->acc->set, "nick_source" );
465        if( strcmp( set, "handle" ) != 0 )
466        {
467                char *name = g_strdup( realname );
468               
469                if( strcmp( set, "first_name" ) == 0 )
470                {
471                        int i;
472                        for( i = 0; name[i] && !isspace( name[i] ); i ++ ) {}
473                        name[i] = '\0';
474                }
475               
476                imcb_buddy_nick_hint( ic, handle, name );
477               
478                g_free( name );
479        }
480}
481
482void imcb_remove_buddy( struct im_connection *ic, const char *handle, char *group )
483{
484        user_t *u;
485       
486        if( ( u = user_findhandle( ic, handle ) ) )
487                user_del( ic->irc, u->nick );
488}
489
490/* Mainly meant for ICQ (and now also for Jabber conferences) to allow IM
491   modules to suggest a nickname for a handle. */
492void imcb_buddy_nick_hint( struct im_connection *ic, const char *handle, const char *nick )
493{
494        user_t *u = user_findhandle( ic, handle );
495        char newnick[MAX_NICK_LENGTH+1], *orig_nick;
496       
497        if( u && !u->online && !nick_saved( ic->acc, handle ) )
498        {
499                /* Only do this if the person isn't online yet (which should
500                   be the case if we just added it) and if the user hasn't
501                   assigned a nickname to this buddy already. */
502               
503                strncpy( newnick, nick, MAX_NICK_LENGTH );
504                newnick[MAX_NICK_LENGTH] = 0;
505               
506                /* Some processing to make sure this string is a valid IRC nickname. */
507                nick_strip( newnick );
508                if( set_getbool( &ic->irc->set, "lcnicks" ) )
509                        nick_lc( newnick );
510               
511                if( strcmp( u->nick, newnick ) != 0 )
512                {
513                        /* Only do this if newnick is different from the current one.
514                           If rejoining a channel, maybe we got this nick already
515                           (and dedupe would only add an underscore. */
516                        nick_dedupe( ic->acc, handle, newnick );
517                       
518                        /* u->nick will be freed halfway the process, so it can't be
519                           passed as an argument. */
520                        orig_nick = g_strdup( u->nick );
521                        user_rename( ic->irc, orig_nick, newnick );
522                        g_free( orig_nick );
523                }
524        }
525}
526
527
528struct imcb_ask_cb_data
529{
530        struct im_connection *ic;
531        char *handle;
532};
533
534static void imcb_ask_auth_cb_no( void *data )
535{
536        struct imcb_ask_cb_data *cbd = data;
537       
538        cbd->ic->acc->prpl->auth_deny( cbd->ic, cbd->handle );
539       
540        g_free( cbd->handle );
541        g_free( cbd );
542}
543
544static void imcb_ask_auth_cb_yes( void *data )
545{
546        struct imcb_ask_cb_data *cbd = data;
547       
548        cbd->ic->acc->prpl->auth_allow( cbd->ic, cbd->handle );
549       
550        g_free( cbd->handle );
551        g_free( cbd );
552}
553
554void imcb_ask_auth( struct im_connection *ic, const char *handle, const char *realname )
555{
556        struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 );
557        char *s, *realname_ = NULL;
558       
559        if( realname != NULL )
560                realname_ = g_strdup_printf( " (%s)", realname );
561       
562        s = g_strdup_printf( "The user %s%s wants to add you to his/her buddy list.",
563                             handle, realname_ ?: "" );
564       
565        g_free( realname_ );
566       
567        data->ic = ic;
568        data->handle = g_strdup( handle );
569        query_add( ic->irc, ic, s, imcb_ask_auth_cb_yes, imcb_ask_auth_cb_no, data );
570}
571
572
573static void imcb_ask_add_cb_no( void *data )
574{
575        g_free( ((struct imcb_ask_cb_data*)data)->handle );
576        g_free( data );
577}
578
579static void imcb_ask_add_cb_yes( void *data )
580{
581        struct imcb_ask_cb_data *cbd = data;
582       
583        cbd->ic->acc->prpl->add_buddy( cbd->ic, cbd->handle, NULL );
584       
585        return imcb_ask_add_cb_no( data );
586}
587
588void imcb_ask_add( struct im_connection *ic, const char *handle, const char *realname )
589{
590        struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 );
591        char *s;
592       
593        /* TODO: Make a setting for this! */
594        if( user_findhandle( ic, handle ) != NULL )
595                return;
596       
597        s = g_strdup_printf( "The user %s is not in your buddy list yet. Do you want to add him/her now?", handle );
598       
599        data->ic = ic;
600        data->handle = g_strdup( handle );
601        query_add( ic->irc, ic, s, imcb_ask_add_cb_yes, imcb_ask_add_cb_no, data );
602}
603
604
605/* server.c */                   
606
607void imcb_buddy_status( struct im_connection *ic, const char *handle, int flags, const char *state, const char *message )
608{
609        user_t *u;
610        int oa, oo;
611       
612        u = user_findhandle( ic, (char*) handle );
613       
614        if( !u )
615        {
616                if( g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "add" ) == 0 )
617                {
618                        imcb_add_buddy( ic, (char*) handle, NULL );
619                        u = user_findhandle( ic, (char*) handle );
620                }
621                else
622                {
623                        if( set_getbool( &ic->irc->set, "debug" ) || g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "ignore" ) != 0 )
624                        {
625                                imcb_log( ic, "imcb_buddy_status() for unknown handle %s:", handle );
626                                imcb_log( ic, "flags = %d, state = %s, message = %s", flags,
627                                          state ? state : "NULL", message ? message : "NULL" );
628                        }
629                       
630                        return;
631                }
632        }
633       
634        oa = u->away != NULL;
635        oo = u->online;
636       
637        if( u->away )
638        {
639                g_free( u->away );
640                u->away = NULL;
641        }
642       
643        if( ( flags & OPT_LOGGED_IN ) && !u->online )
644        {
645                irc_spawn( ic->irc, u );
646                u->online = 1;
647        }
648        else if( !( flags & OPT_LOGGED_IN ) && u->online )
649        {
650                struct groupchat *c;
651               
652                irc_kill( ic->irc, u );
653                u->online = 0;
654               
655                /* Remove him/her from the groupchats to prevent PART messages after he/she QUIT already */
656                for( c = ic->groupchats; c; c = c->next )
657                        remove_chat_buddy_silent( c, handle );
658        }
659       
660        if( flags & OPT_AWAY )
661        {
662                if( state && message )
663                {
664                        u->away = g_strdup_printf( "%s (%s)", state, message );
665                }
666                else if( state )
667                {
668                        u->away = g_strdup( state );
669                }
670                else if( message )
671                {
672                        u->away = g_strdup( message );
673                }
674                else
675                {
676                        u->away = g_strdup( "Away" );
677                }
678        }
679        /* else waste_any_state_information_for_now(); */
680       
681        /* LISPy... */
682        if( ( set_getbool( &ic->irc->set, "away_devoice" ) ) &&         /* Don't do a thing when user doesn't want it */
683            ( u->online ) &&                                            /* Don't touch offline people */
684            ( ( ( u->online != oo ) && !u->away ) ||                    /* Voice joining people */
685              ( ( u->online == oo ) && ( oa == !u->away ) ) ) )         /* (De)voice people changing state */
686        {
687                char *from;
688               
689                if( set_getbool( &ic->irc->set, "simulate_netsplit" ) )
690                {
691                        from = g_strdup( ic->irc->myhost );
692                }
693                else
694                {
695                        from = g_strdup_printf( "%s!%s@%s", ic->irc->mynick, ic->irc->mynick,
696                                                            ic->irc->myhost );
697                }
698                irc_write( ic->irc, ":%s MODE %s %cv %s", from, ic->irc->channel,
699                                                          u->away?'-':'+', u->nick );
700                g_free( from );
701        }
702}
703
704void imcb_buddy_msg( struct im_connection *ic, const char *handle, char *msg, uint32_t flags, time_t sent_at )
705{
706        irc_t *irc = ic->irc;
707        char *wrapped;
708        user_t *u;
709       
710        u = user_findhandle( ic, handle );
711       
712        if( !u )
713        {
714                char *h = set_getstr( &irc->set, "handle_unknown" );
715               
716                if( g_strcasecmp( h, "ignore" ) == 0 )
717                {
718                        if( set_getbool( &irc->set, "debug" ) )
719                                imcb_log( ic, "Ignoring message from unknown handle %s", handle );
720                       
721                        return;
722                }
723                else if( g_strncasecmp( h, "add", 3 ) == 0 )
724                {
725                        int private = set_getbool( &irc->set, "private" );
726                       
727                        if( h[3] )
728                        {
729                                if( g_strcasecmp( h + 3, "_private" ) == 0 )
730                                        private = 1;
731                                else if( g_strcasecmp( h + 3, "_channel" ) == 0 )
732                                        private = 0;
733                        }
734                       
735                        imcb_add_buddy( ic, handle, NULL );
736                        u = user_findhandle( ic, handle );
737                        u->is_private = private;
738                }
739                else
740                {
741                        imcb_log( ic, "Message from unknown handle %s:", handle );
742                        u = user_find( irc, irc->mynick );
743                }
744        }
745       
746        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
747            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
748                strip_html( msg );
749
750        wrapped = word_wrap( msg, 425 );
751        irc_msgfrom( irc, u->nick, wrapped );
752        g_free( wrapped );
753}
754
755void imcb_buddy_typing( struct im_connection *ic, char *handle, uint32_t flags )
756{
757        user_t *u;
758       
759        if( !set_getbool( &ic->irc->set, "typing_notice" ) )
760                return;
761       
762        if( ( u = user_findhandle( ic, handle ) ) )
763        {
764                char buf[256]; 
765               
766                g_snprintf( buf, 256, "\1TYPING %d\1", ( flags >> 8 ) & 3 );
767                irc_privmsg( ic->irc, u, "PRIVMSG", ic->irc->nick, NULL, buf );
768        }
769}
770
771struct groupchat *imcb_chat_new( struct im_connection *ic, const char *handle )
772{
773        struct groupchat *c;
774       
775        /* This one just creates the conversation structure, user won't see anything yet */
776       
777        if( ic->groupchats )
778        {
779                for( c = ic->groupchats; c->next; c = c->next );
780                c = c->next = g_new0( struct groupchat, 1 );
781        }
782        else
783                ic->groupchats = c = g_new0( struct groupchat, 1 );
784       
785        c->ic = ic;
786        c->title = g_strdup( handle );
787        c->channel = g_strdup_printf( "&chat_%03d", ic->irc->c_id++ );
788        c->topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title );
789       
790        if( set_getbool( &ic->irc->set, "debug" ) )
791                imcb_log( ic, "Creating new conversation: (id=%p,handle=%s)", c, handle );
792       
793        return c;
794}
795
796void imcb_chat_free( struct groupchat *c )
797{
798        struct im_connection *ic = c->ic;
799        struct groupchat *l;
800        GList *ir;
801       
802        if( set_getbool( &ic->irc->set, "debug" ) )
803                imcb_log( ic, "You were removed from conversation %p", c );
804       
805        if( c )
806        {
807                if( c->joined )
808                {
809                        user_t *u, *r;
810                       
811                        r = user_find( ic->irc, ic->irc->mynick );
812                        irc_privmsg( ic->irc, r, "PRIVMSG", c->channel, "", "Cleaning up channel, bye!" );
813                       
814                        u = user_find( ic->irc, ic->irc->nick );
815                        irc_kick( ic->irc, u, c->channel, r );
816                        /* irc_part( ic->irc, u, c->channel ); */
817                }
818               
819                /* Find the previous chat in the linked list. */
820                for( l = ic->groupchats; l && l->next != c; l = l->next );
821               
822                if( l )
823                        l->next = c->next;
824                else
825                        ic->groupchats = c->next;
826               
827                for( ir = c->in_room; ir; ir = ir->next )
828                        g_free( ir->data );
829                g_list_free( c->in_room );
830                g_free( c->channel );
831                g_free( c->title );
832                g_free( c->topic );
833                g_free( c );
834        }
835}
836
837void imcb_chat_msg( struct groupchat *c, const char *who, char *msg, uint32_t flags, time_t sent_at )
838{
839        struct im_connection *ic = c->ic;
840        char *wrapped;
841        user_t *u;
842       
843        /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */
844        if( g_strcasecmp( who, ic->acc->user ) == 0 )
845                return;
846       
847        u = user_findhandle( ic, who );
848       
849        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
850            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
851                strip_html( msg );
852       
853        wrapped = word_wrap( msg, 425 );
854        if( c && u )
855        {
856                irc_privmsg( ic->irc, u, "PRIVMSG", c->channel, "", wrapped );
857        }
858        else
859        {
860                imcb_log( ic, "Message from/to conversation %s@%p (unknown conv/user): %s", who, c, wrapped );
861        }
862        g_free( wrapped );
863}
864
865void imcb_chat_log( struct groupchat *c, char *format, ... )
866{
867        irc_t *irc = c->ic->irc;
868        va_list params;
869        char *text;
870        user_t *u;
871       
872        va_start( params, format );
873        text = g_strdup_vprintf( format, params );
874        va_end( params );
875       
876        u = user_find( irc, irc->mynick );
877       
878        irc_privmsg( irc, u, "PRIVMSG", c->channel, "System message: ", text );
879       
880        g_free( text );
881}
882
883void imcb_chat_topic( struct groupchat *c, char *who, char *topic, time_t set_at )
884{
885        struct im_connection *ic = c->ic;
886        user_t *u = NULL;
887       
888        if( who == NULL)
889                u = user_find( ic->irc, ic->irc->mynick );
890        else if( g_strcasecmp( who, ic->acc->user ) == 0 )
891                u = user_find( ic->irc, ic->irc->nick );
892        else
893                u = user_findhandle( ic, who );
894       
895        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
896            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
897                strip_html( topic );
898       
899        g_free( c->topic );
900        c->topic = g_strdup( topic );
901       
902        if( c->joined && u )
903                irc_write( ic->irc, ":%s!%s@%s TOPIC %s :%s", u->nick, u->user, u->host, c->channel, topic );
904}
905
906
907/* buddy_chat.c */
908
909void imcb_chat_add_buddy( struct groupchat *b, const char *handle )
910{
911        user_t *u = user_findhandle( b->ic, handle );
912        int me = 0;
913       
914        if( set_getbool( &b->ic->irc->set, "debug" ) )
915                imcb_log( b->ic, "User %s added to conversation %p", handle, b );
916       
917        /* It might be yourself! */
918        if( b->ic->acc->prpl->handle_cmp( handle, b->ic->acc->user ) == 0 )
919        {
920                u = user_find( b->ic->irc, b->ic->irc->nick );
921                if( !b->joined )
922                        irc_join( b->ic->irc, u, b->channel );
923                b->joined = me = 1;
924        }
925       
926        /* Most protocols allow people to join, even when they're not in
927           your contact list. Try to handle that here */
928        if( !u )
929        {
930                imcb_add_buddy( b->ic, handle, NULL );
931                u = user_findhandle( b->ic, handle );
932        }
933       
934        /* Add the handle to the room userlist, if it's not 'me' */
935        if( !me )
936        {
937                if( b->joined )
938                        irc_join( b->ic->irc, u, b->channel );
939                b->in_room = g_list_append( b->in_room, g_strdup( handle ) );
940        }
941}
942
943/* This function is one BIG hack... :-( EREWRITE */
944void imcb_chat_remove_buddy( struct groupchat *b, const char *handle, const char *reason )
945{
946        user_t *u;
947        int me = 0;
948       
949        if( set_getbool( &b->ic->irc->set, "debug" ) )
950                imcb_log( b->ic, "User %s removed from conversation %p (%s)", handle, b, reason ? reason : "" );
951       
952        /* It might be yourself! */
953        if( g_strcasecmp( handle, b->ic->acc->user ) == 0 )
954        {
955                if( b->joined == 0 )
956                        return;
957               
958                u = user_find( b->ic->irc, b->ic->irc->nick );
959                b->joined = 0;
960                me = 1;
961        }
962        else
963        {
964                u = user_findhandle( b->ic, handle );
965        }
966       
967        if( me || ( remove_chat_buddy_silent( b, handle ) && b->joined && u ) )
968                irc_part( b->ic->irc, u, b->channel );
969}
970
971static int remove_chat_buddy_silent( struct groupchat *b, const char *handle )
972{
973        GList *i;
974       
975        /* Find the handle in the room userlist and shoot it */
976        i = b->in_room;
977        while( i )
978        {
979                if( g_strcasecmp( handle, i->data ) == 0 )
980                {
981                        g_free( i->data );
982                        b->in_room = g_list_remove( b->in_room, i->data );
983                        return( 1 );
984                }
985               
986                i = i->next;
987        }
988       
989        return( 0 );
990}
991
992
993/* Misc. BitlBee stuff which shouldn't really be here */
994
995char *set_eval_away_devoice( set_t *set, char *value )
996{
997        irc_t *irc = set->data;
998        int st;
999       
1000        if( !is_bool( value ) )
1001                return SET_INVALID;
1002       
1003        st = bool2int( value );
1004       
1005        /* Horror.... */
1006       
1007        if( st != set_getbool( &irc->set, "away_devoice" ) )
1008        {
1009                char list[80] = "";
1010                user_t *u = irc->users;
1011                int i = 0, count = 0;
1012                char pm;
1013                char v[80];
1014               
1015                if( st )
1016                        pm = '+';
1017                else
1018                        pm = '-';
1019               
1020                while( u )
1021                {
1022                        if( u->ic && u->online && !u->away )
1023                        {
1024                                if( ( strlen( list ) + strlen( u->nick ) ) >= 79 )
1025                                {
1026                                        for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
1027                                        irc_write( irc, ":%s MODE %s %c%s%s",
1028                                                   irc->myhost,
1029                                                   irc->channel, pm, v, list );
1030                                       
1031                                        *list = 0;
1032                                        count = 0;
1033                                }
1034                               
1035                                sprintf( list + strlen( list ), " %s", u->nick );
1036                                count ++;
1037                        }
1038                        u = u->next;
1039                }
1040               
1041                /* $v = 'v' x $i */
1042                for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
1043                irc_write( irc, ":%s MODE %s %c%s%s", irc->myhost,
1044                                                            irc->channel, pm, v, list );
1045        }
1046       
1047        return value;
1048}
1049
1050
1051
1052
1053/* The plan is to not allow straight calls to prpl functions anymore, but do
1054   them all from some wrappers. We'll start to define some down here: */
1055
1056int imc_buddy_msg( struct im_connection *ic, char *handle, char *msg, int flags )
1057{
1058        char *buf = NULL;
1059        int st;
1060       
1061        if( ( ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
1062        {
1063                buf = escape_html( msg );
1064                msg = buf;
1065        }
1066       
1067        st = ic->acc->prpl->buddy_msg( ic, handle, msg, flags );
1068        g_free( buf );
1069       
1070        return st;
1071}
1072
1073int imc_chat_msg( struct groupchat *c, char *msg, int flags )
1074{
1075        char *buf = NULL;
1076       
1077        if( ( c->ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
1078        {
1079                buf = escape_html( msg );
1080                msg = buf;
1081        }
1082       
1083        c->ic->acc->prpl->chat_msg( c, msg, flags );
1084        g_free( buf );
1085       
1086        return 1;
1087}
1088
1089static char *imc_away_state_find( GList *gcm, char *away, char **message );
1090
1091int imc_away_send_update( struct im_connection *ic )
1092{
1093        char *away, *msg = NULL;
1094       
1095        away = set_getstr( &ic->acc->set, "away" ) ?
1096             : set_getstr( &ic->irc->set, "away" );
1097        if( away && *away )
1098        {
1099                GList *m = ic->acc->prpl->away_states( ic );
1100                msg = ic->acc->flags & ACC_FLAG_AWAY_MESSAGE ? away : NULL;
1101                away = imc_away_state_find( m, away, &msg ) ? : m->data;
1102        }
1103        else if( ic->acc->flags & ACC_FLAG_STATUS_MESSAGE )
1104        {
1105                away = NULL;
1106                msg = set_getstr( &ic->acc->set, "status" ) ?
1107                    : set_getstr( &ic->irc->set, "status" );
1108        }
1109       
1110        ic->acc->prpl->set_away( ic, away, msg );
1111       
1112        return 1;
1113}
1114
1115static char *imc_away_alias_list[8][5] =
1116{
1117        { "Away from computer", "Away", "Extended away", NULL },
1118        { "NA", "N/A", "Not available", NULL },
1119        { "Busy", "Do not disturb", "DND", "Occupied", NULL },
1120        { "Be right back", "BRB", NULL },
1121        { "On the phone", "Phone", "On phone", NULL },
1122        { "Out to lunch", "Lunch", "Food", NULL },
1123        { "Invisible", "Hidden" },
1124        { NULL }
1125};
1126
1127static char *imc_away_state_find( GList *gcm, char *away, char **message )
1128{
1129        GList *m;
1130        int i, j;
1131       
1132        for( m = gcm; m; m = m->next )
1133                if( g_strncasecmp( m->data, away, strlen( m->data ) ) == 0 )
1134                {
1135                        /* At least the Yahoo! module works better if message
1136                           contains no data unless it adds something to what
1137                           we have in state already. */
1138                        if( strlen( m->data ) == strlen( away ) )
1139                                *message = NULL;
1140                       
1141                        return m->data;
1142                }
1143       
1144        for( i = 0; *imc_away_alias_list[i]; i ++ )
1145        {
1146                int keep_message;
1147               
1148                for( j = 0; imc_away_alias_list[i][j]; j ++ )
1149                        if( g_strncasecmp( away, imc_away_alias_list[i][j], strlen( imc_away_alias_list[i][j] ) ) == 0 )
1150                        {
1151                                keep_message = strlen( away ) != strlen( imc_away_alias_list[i][j] );
1152                                break;
1153                        }
1154               
1155                if( !imc_away_alias_list[i][j] )        /* If we reach the end, this row */
1156                        continue;                       /* is not what we want. Next!    */
1157               
1158                /* Now find an entry in this row which exists in gcm */
1159                for( j = 0; imc_away_alias_list[i][j]; j ++ )
1160                {
1161                        for( m = gcm; m; m = m->next )
1162                                if( g_strcasecmp( imc_away_alias_list[i][j], m->data ) == 0 )
1163                                {
1164                                        if( !keep_message )
1165                                                *message = NULL;
1166                                       
1167                                        return imc_away_alias_list[i][j];
1168                                }
1169                }
1170               
1171                /* No need to look further, apparently this state doesn't
1172                   have any good alias for this protocol. */
1173                break;
1174        }
1175       
1176        return NULL;
1177}
1178
1179void imc_add_allow( struct im_connection *ic, char *handle )
1180{
1181        if( g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL )
1182        {
1183                ic->permit = g_slist_prepend( ic->permit, g_strdup( handle ) );
1184        }
1185       
1186        ic->acc->prpl->add_permit( ic, handle );
1187}
1188
1189void imc_rem_allow( struct im_connection *ic, char *handle )
1190{
1191        GSList *l;
1192       
1193        if( ( l = g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) )
1194        {
1195                g_free( l->data );
1196                ic->permit = g_slist_delete_link( ic->permit, l );
1197        }
1198       
1199        ic->acc->prpl->rem_permit( ic, handle );
1200}
1201
1202void imc_add_block( struct im_connection *ic, char *handle )
1203{
1204        if( g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL )
1205        {
1206                ic->deny = g_slist_prepend( ic->deny, g_strdup( handle ) );
1207        }
1208       
1209        ic->acc->prpl->add_deny( ic, handle );
1210}
1211
1212void imc_rem_block( struct im_connection *ic, char *handle )
1213{
1214        GSList *l;
1215       
1216        if( ( l = g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) )
1217        {
1218                g_free( l->data );
1219                ic->deny = g_slist_delete_link( ic->deny, l );
1220        }
1221       
1222        ic->acc->prpl->rem_deny( ic, handle );
1223}
1224
1225void imcb_clean_handle( struct im_connection *ic, char *handle )
1226{
1227        /* Accepts a handle and does whatever is necessary to make it
1228           BitlBee-friendly. Currently this means removing everything
1229           outside 33-127 (ASCII printable excl spaces), @ (only one
1230           is allowed) and ! and : */
1231        char out[strlen(handle)+1];
1232        int s, d;
1233       
1234        s = d = 0;
1235        while( handle[s] )
1236        {
1237                if( handle[s] > ' ' && handle[s] != '!' && handle[s] != ':' &&
1238                    ( handle[s] & 0x80 ) == 0 )
1239                {
1240                        if( handle[s] == '@' )
1241                        {
1242                                /* See if we got an @ already? */
1243                                out[d] = 0;
1244                                if( strchr( out, '@' ) )
1245                                        continue;
1246                        }
1247                       
1248                        out[d++] = handle[s];
1249                }
1250                s ++;
1251        }
1252        out[d] = handle[s];
1253       
1254        strcpy( handle, out );
1255}
Note: See TracBrowser for help on using the repository browser.