source: protocols/nogaim.c @ 3330468

Last change on this file since 3330468 was 3330468, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-03-14T23:15:05Z

Merging in head.

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