source: protocols/nogaim.c @ 85693e6

Last change on this file since 85693e6 was 81ee561, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-03-20T18:03:18Z

Merging head. Most changes are not so relevant because they're to IM
modules.

  • 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        g_free( u->away );
658        g_free( u->status_msg );
659        u->away = u->status_msg = NULL;
660       
661        if( ( flags & OPT_LOGGED_IN ) && !u->online )
662        {
663                irc_spawn( ic->irc, u );
664                u->online = 1;
665        }
666        else if( !( flags & OPT_LOGGED_IN ) && u->online )
667        {
668                struct groupchat *c;
669               
670                irc_kill( ic->irc, u );
671                u->online = 0;
672               
673                /* Remove him/her from the groupchats to prevent PART messages after he/she QUIT already */
674                for( c = ic->groupchats; c; c = c->next )
675                        remove_chat_buddy_silent( c, handle );
676        }
677       
678        if( flags & OPT_AWAY )
679        {
680                if( state && message )
681                {
682                        u->away = g_strdup_printf( "%s (%s)", state, message );
683                }
684                else if( state )
685                {
686                        u->away = g_strdup( state );
687                }
688                else if( message )
689                {
690                        u->away = g_strdup( message );
691                }
692                else
693                {
694                        u->away = g_strdup( "Away" );
695                }
696        }
697        else
698        {
699                u->status_msg = g_strdup( message );
700        }
701       
702        /* LISPy... */
703        if( ( set_getbool( &ic->irc->set, "away_devoice" ) ) &&         /* Don't do a thing when user doesn't want it */
704            ( u->online ) &&                                            /* Don't touch offline people */
705            ( ( ( u->online != oo ) && !u->away ) ||                    /* Voice joining people */
706              ( ( u->online == oo ) && ( oa == !u->away ) ) ) )         /* (De)voice people changing state */
707        {
708                char *from;
709               
710                if( set_getbool( &ic->irc->set, "simulate_netsplit" ) )
711                {
712                        from = g_strdup( ic->irc->myhost );
713                }
714                else
715                {
716                        from = g_strdup_printf( "%s!%s@%s", ic->irc->mynick, ic->irc->mynick,
717                                                            ic->irc->myhost );
718                }
719                irc_write( ic->irc, ":%s MODE %s %cv %s", from, ic->irc->channel,
720                                                          u->away?'-':'+', u->nick );
721                g_free( from );
722        }
723}
724
725void imcb_buddy_msg( struct im_connection *ic, const char *handle, char *msg, uint32_t flags, time_t sent_at )
726{
727        irc_t *irc = ic->irc;
728        char *wrapped;
729        user_t *u;
730       
731        u = user_findhandle( ic, handle );
732       
733        if( !u )
734        {
735                char *h = set_getstr( &irc->set, "handle_unknown" );
736               
737                if( g_strcasecmp( h, "ignore" ) == 0 )
738                {
739                        if( set_getbool( &irc->set, "debug" ) )
740                                imcb_log( ic, "Ignoring message from unknown handle %s", handle );
741                       
742                        return;
743                }
744                else if( g_strncasecmp( h, "add", 3 ) == 0 )
745                {
746                        int private = set_getbool( &irc->set, "private" );
747                       
748                        if( h[3] )
749                        {
750                                if( g_strcasecmp( h + 3, "_private" ) == 0 )
751                                        private = 1;
752                                else if( g_strcasecmp( h + 3, "_channel" ) == 0 )
753                                        private = 0;
754                        }
755                       
756                        imcb_add_buddy( ic, handle, NULL );
757                        u = user_findhandle( ic, handle );
758                        u->is_private = private;
759                }
760                else
761                {
762                        imcb_log( ic, "Message from unknown handle %s:", handle );
763                        u = user_find( irc, irc->mynick );
764                }
765        }
766       
767        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
768            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
769                strip_html( msg );
770
771        wrapped = word_wrap( msg, 425 );
772        irc_msgfrom( irc, u->nick, wrapped );
773        g_free( wrapped );
774}
775
776void imcb_buddy_typing( struct im_connection *ic, char *handle, uint32_t flags )
777{
778        user_t *u;
779       
780        if( !set_getbool( &ic->irc->set, "typing_notice" ) )
781                return;
782       
783        if( ( u = user_findhandle( ic, handle ) ) )
784        {
785                char buf[256]; 
786               
787                g_snprintf( buf, 256, "\1TYPING %d\1", ( flags >> 8 ) & 3 );
788                irc_privmsg( ic->irc, u, "PRIVMSG", ic->irc->nick, NULL, buf );
789        }
790}
791
792struct groupchat *imcb_chat_new( struct im_connection *ic, const char *handle )
793{
794        struct groupchat *c;
795       
796        /* This one just creates the conversation structure, user won't see anything yet */
797       
798        if( ic->groupchats )
799        {
800                for( c = ic->groupchats; c->next; c = c->next );
801                c = c->next = g_new0( struct groupchat, 1 );
802        }
803        else
804                ic->groupchats = c = g_new0( struct groupchat, 1 );
805       
806        c->ic = ic;
807        c->title = g_strdup( handle );
808        c->channel = g_strdup_printf( "&chat_%03d", ic->irc->c_id++ );
809        c->topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title );
810       
811        if( set_getbool( &ic->irc->set, "debug" ) )
812                imcb_log( ic, "Creating new conversation: (id=%p,handle=%s)", c, handle );
813       
814        return c;
815}
816
817void imcb_chat_free( struct groupchat *c )
818{
819        struct im_connection *ic = c->ic;
820        struct groupchat *l;
821        GList *ir;
822       
823        if( set_getbool( &ic->irc->set, "debug" ) )
824                imcb_log( ic, "You were removed from conversation %p", c );
825       
826        if( c )
827        {
828                if( c->joined )
829                {
830                        user_t *u, *r;
831                       
832                        r = user_find( ic->irc, ic->irc->mynick );
833                        irc_privmsg( ic->irc, r, "PRIVMSG", c->channel, "", "Cleaning up channel, bye!" );
834                       
835                        u = user_find( ic->irc, ic->irc->nick );
836                        irc_kick( ic->irc, u, c->channel, r );
837                        /* irc_part( ic->irc, u, c->channel ); */
838                }
839               
840                /* Find the previous chat in the linked list. */
841                for( l = ic->groupchats; l && l->next != c; l = l->next );
842               
843                if( l )
844                        l->next = c->next;
845                else
846                        ic->groupchats = c->next;
847               
848                for( ir = c->in_room; ir; ir = ir->next )
849                        g_free( ir->data );
850                g_list_free( c->in_room );
851                g_free( c->channel );
852                g_free( c->title );
853                g_free( c->topic );
854                g_free( c );
855        }
856}
857
858void imcb_chat_msg( struct groupchat *c, const char *who, char *msg, uint32_t flags, time_t sent_at )
859{
860        struct im_connection *ic = c->ic;
861        char *wrapped;
862        user_t *u;
863       
864        /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */
865        if( g_strcasecmp( who, ic->acc->user ) == 0 )
866                return;
867       
868        u = user_findhandle( ic, who );
869       
870        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
871            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
872                strip_html( msg );
873       
874        wrapped = word_wrap( msg, 425 );
875        if( c && u )
876        {
877                irc_privmsg( ic->irc, u, "PRIVMSG", c->channel, "", wrapped );
878        }
879        else
880        {
881                imcb_log( ic, "Message from/to conversation %s@%p (unknown conv/user): %s", who, c, wrapped );
882        }
883        g_free( wrapped );
884}
885
886void imcb_chat_log( struct groupchat *c, char *format, ... )
887{
888        irc_t *irc = c->ic->irc;
889        va_list params;
890        char *text;
891        user_t *u;
892       
893        va_start( params, format );
894        text = g_strdup_vprintf( format, params );
895        va_end( params );
896       
897        u = user_find( irc, irc->mynick );
898       
899        irc_privmsg( irc, u, "PRIVMSG", c->channel, "System message: ", text );
900       
901        g_free( text );
902}
903
904void imcb_chat_topic( struct groupchat *c, char *who, char *topic, time_t set_at )
905{
906        struct im_connection *ic = c->ic;
907        user_t *u = NULL;
908       
909        if( who == NULL)
910                u = user_find( ic->irc, ic->irc->mynick );
911        else if( g_strcasecmp( who, ic->acc->user ) == 0 )
912                u = user_find( ic->irc, ic->irc->nick );
913        else
914                u = user_findhandle( ic, who );
915       
916        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
917            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
918                strip_html( topic );
919       
920        g_free( c->topic );
921        c->topic = g_strdup( topic );
922       
923        if( c->joined && u )
924                irc_write( ic->irc, ":%s!%s@%s TOPIC %s :%s", u->nick, u->user, u->host, c->channel, topic );
925}
926
927
928/* buddy_chat.c */
929
930void imcb_chat_add_buddy( struct groupchat *b, const char *handle )
931{
932        user_t *u = user_findhandle( b->ic, handle );
933        int me = 0;
934       
935        if( set_getbool( &b->ic->irc->set, "debug" ) )
936                imcb_log( b->ic, "User %s added to conversation %p", handle, b );
937       
938        /* It might be yourself! */
939        if( b->ic->acc->prpl->handle_cmp( handle, b->ic->acc->user ) == 0 )
940        {
941                u = user_find( b->ic->irc, b->ic->irc->nick );
942                if( !b->joined )
943                        irc_join( b->ic->irc, u, b->channel );
944                b->joined = me = 1;
945        }
946       
947        /* Most protocols allow people to join, even when they're not in
948           your contact list. Try to handle that here */
949        if( !u )
950        {
951                imcb_add_buddy( b->ic, handle, NULL );
952                u = user_findhandle( b->ic, handle );
953        }
954       
955        /* Add the handle to the room userlist, if it's not 'me' */
956        if( !me )
957        {
958                if( b->joined )
959                        irc_join( b->ic->irc, u, b->channel );
960                b->in_room = g_list_append( b->in_room, g_strdup( handle ) );
961        }
962}
963
964/* This function is one BIG hack... :-( EREWRITE */
965void imcb_chat_remove_buddy( struct groupchat *b, const char *handle, const char *reason )
966{
967        user_t *u;
968        int me = 0;
969       
970        if( set_getbool( &b->ic->irc->set, "debug" ) )
971                imcb_log( b->ic, "User %s removed from conversation %p (%s)", handle, b, reason ? reason : "" );
972       
973        /* It might be yourself! */
974        if( g_strcasecmp( handle, b->ic->acc->user ) == 0 )
975        {
976                if( b->joined == 0 )
977                        return;
978               
979                u = user_find( b->ic->irc, b->ic->irc->nick );
980                b->joined = 0;
981                me = 1;
982        }
983        else
984        {
985                u = user_findhandle( b->ic, handle );
986        }
987       
988        if( me || ( remove_chat_buddy_silent( b, handle ) && b->joined && u ) )
989                irc_part( b->ic->irc, u, b->channel );
990}
991
992static int remove_chat_buddy_silent( struct groupchat *b, const char *handle )
993{
994        GList *i;
995       
996        /* Find the handle in the room userlist and shoot it */
997        i = b->in_room;
998        while( i )
999        {
1000                if( g_strcasecmp( handle, i->data ) == 0 )
1001                {
1002                        g_free( i->data );
1003                        b->in_room = g_list_remove( b->in_room, i->data );
1004                        return( 1 );
1005                }
1006               
1007                i = i->next;
1008        }
1009       
1010        return( 0 );
1011}
1012
1013
1014/* Misc. BitlBee stuff which shouldn't really be here */
1015
1016char *set_eval_away_devoice( set_t *set, char *value )
1017{
1018        irc_t *irc = set->data;
1019        int st;
1020       
1021        if( !is_bool( value ) )
1022                return SET_INVALID;
1023       
1024        st = bool2int( value );
1025       
1026        /* Horror.... */
1027       
1028        if( st != set_getbool( &irc->set, "away_devoice" ) )
1029        {
1030                char list[80] = "";
1031                user_t *u = irc->users;
1032                int i = 0, count = 0;
1033                char pm;
1034                char v[80];
1035               
1036                if( st )
1037                        pm = '+';
1038                else
1039                        pm = '-';
1040               
1041                while( u )
1042                {
1043                        if( u->ic && u->online && !u->away )
1044                        {
1045                                if( ( strlen( list ) + strlen( u->nick ) ) >= 79 )
1046                                {
1047                                        for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
1048                                        irc_write( irc, ":%s MODE %s %c%s%s",
1049                                                   irc->myhost,
1050                                                   irc->channel, pm, v, list );
1051                                       
1052                                        *list = 0;
1053                                        count = 0;
1054                                }
1055                               
1056                                sprintf( list + strlen( list ), " %s", u->nick );
1057                                count ++;
1058                        }
1059                        u = u->next;
1060                }
1061               
1062                /* $v = 'v' x $i */
1063                for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
1064                irc_write( irc, ":%s MODE %s %c%s%s", irc->myhost,
1065                                                            irc->channel, pm, v, list );
1066        }
1067       
1068        return value;
1069}
1070
1071
1072
1073
1074/* The plan is to not allow straight calls to prpl functions anymore, but do
1075   them all from some wrappers. We'll start to define some down here: */
1076
1077int imc_buddy_msg( struct im_connection *ic, char *handle, char *msg, int flags )
1078{
1079        char *buf = NULL;
1080        int st;
1081       
1082        if( ( ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
1083        {
1084                buf = escape_html( msg );
1085                msg = buf;
1086        }
1087       
1088        st = ic->acc->prpl->buddy_msg( ic, handle, msg, flags );
1089        g_free( buf );
1090       
1091        return st;
1092}
1093
1094int imc_chat_msg( struct groupchat *c, char *msg, int flags )
1095{
1096        char *buf = NULL;
1097       
1098        if( ( c->ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
1099        {
1100                buf = escape_html( msg );
1101                msg = buf;
1102        }
1103       
1104        c->ic->acc->prpl->chat_msg( c, msg, flags );
1105        g_free( buf );
1106       
1107        return 1;
1108}
1109
1110static char *imc_away_state_find( GList *gcm, char *away, char **message );
1111
1112int imc_away_send_update( struct im_connection *ic )
1113{
1114        char *away, *msg = NULL;
1115       
1116        away = set_getstr( &ic->acc->set, "away" ) ?
1117             : set_getstr( &ic->irc->set, "away" );
1118        if( away && *away )
1119        {
1120                GList *m = ic->acc->prpl->away_states( ic );
1121                msg = ic->acc->flags & ACC_FLAG_AWAY_MESSAGE ? away : NULL;
1122                away = imc_away_state_find( m, away, &msg ) ? : m->data;
1123        }
1124        else if( ic->acc->flags & ACC_FLAG_STATUS_MESSAGE )
1125        {
1126                away = NULL;
1127                msg = set_getstr( &ic->acc->set, "status" ) ?
1128                    : set_getstr( &ic->irc->set, "status" );
1129        }
1130       
1131        ic->acc->prpl->set_away( ic, away, msg );
1132       
1133        return 1;
1134}
1135
1136static char *imc_away_alias_list[8][5] =
1137{
1138        { "Away from computer", "Away", "Extended away", NULL },
1139        { "NA", "N/A", "Not available", NULL },
1140        { "Busy", "Do not disturb", "DND", "Occupied", NULL },
1141        { "Be right back", "BRB", NULL },
1142        { "On the phone", "Phone", "On phone", NULL },
1143        { "Out to lunch", "Lunch", "Food", NULL },
1144        { "Invisible", "Hidden" },
1145        { NULL }
1146};
1147
1148static char *imc_away_state_find( GList *gcm, char *away, char **message )
1149{
1150        GList *m;
1151        int i, j;
1152       
1153        for( m = gcm; m; m = m->next )
1154                if( g_strncasecmp( m->data, away, strlen( m->data ) ) == 0 )
1155                {
1156                        /* At least the Yahoo! module works better if message
1157                           contains no data unless it adds something to what
1158                           we have in state already. */
1159                        if( strlen( m->data ) == strlen( away ) )
1160                                *message = NULL;
1161                       
1162                        return m->data;
1163                }
1164       
1165        for( i = 0; *imc_away_alias_list[i]; i ++ )
1166        {
1167                int keep_message;
1168               
1169                for( j = 0; imc_away_alias_list[i][j]; j ++ )
1170                        if( g_strncasecmp( away, imc_away_alias_list[i][j], strlen( imc_away_alias_list[i][j] ) ) == 0 )
1171                        {
1172                                keep_message = strlen( away ) != strlen( imc_away_alias_list[i][j] );
1173                                break;
1174                        }
1175               
1176                if( !imc_away_alias_list[i][j] )        /* If we reach the end, this row */
1177                        continue;                       /* is not what we want. Next!    */
1178               
1179                /* Now find an entry in this row which exists in gcm */
1180                for( j = 0; imc_away_alias_list[i][j]; j ++ )
1181                {
1182                        for( m = gcm; m; m = m->next )
1183                                if( g_strcasecmp( imc_away_alias_list[i][j], m->data ) == 0 )
1184                                {
1185                                        if( !keep_message )
1186                                                *message = NULL;
1187                                       
1188                                        return imc_away_alias_list[i][j];
1189                                }
1190                }
1191               
1192                /* No need to look further, apparently this state doesn't
1193                   have any good alias for this protocol. */
1194                break;
1195        }
1196       
1197        return NULL;
1198}
1199
1200void imc_add_allow( struct im_connection *ic, char *handle )
1201{
1202        if( g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL )
1203        {
1204                ic->permit = g_slist_prepend( ic->permit, g_strdup( handle ) );
1205        }
1206       
1207        ic->acc->prpl->add_permit( ic, handle );
1208}
1209
1210void imc_rem_allow( struct im_connection *ic, char *handle )
1211{
1212        GSList *l;
1213       
1214        if( ( l = g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) )
1215        {
1216                g_free( l->data );
1217                ic->permit = g_slist_delete_link( ic->permit, l );
1218        }
1219       
1220        ic->acc->prpl->rem_permit( ic, handle );
1221}
1222
1223void imc_add_block( struct im_connection *ic, char *handle )
1224{
1225        if( g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL )
1226        {
1227                ic->deny = g_slist_prepend( ic->deny, g_strdup( handle ) );
1228        }
1229       
1230        ic->acc->prpl->add_deny( ic, handle );
1231}
1232
1233void imc_rem_block( struct im_connection *ic, char *handle )
1234{
1235        GSList *l;
1236       
1237        if( ( l = g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) )
1238        {
1239                g_free( l->data );
1240                ic->deny = g_slist_delete_link( ic->deny, l );
1241        }
1242       
1243        ic->acc->prpl->rem_deny( ic, handle );
1244}
1245
1246void imcb_clean_handle( struct im_connection *ic, char *handle )
1247{
1248        /* Accepts a handle and does whatever is necessary to make it
1249           BitlBee-friendly. Currently this means removing everything
1250           outside 33-127 (ASCII printable excl spaces), @ (only one
1251           is allowed) and ! and : */
1252        char out[strlen(handle)+1];
1253        int s, d;
1254       
1255        s = d = 0;
1256        while( handle[s] )
1257        {
1258                if( handle[s] > ' ' && handle[s] != '!' && handle[s] != ':' &&
1259                    ( handle[s] & 0x80 ) == 0 )
1260                {
1261                        if( handle[s] == '@' )
1262                        {
1263                                /* See if we got an @ already? */
1264                                out[d] = 0;
1265                                if( strchr( out, '@' ) )
1266                                        continue;
1267                        }
1268                       
1269                        out[d++] = handle[s];
1270                }
1271                s ++;
1272        }
1273        out[d] = handle[s];
1274       
1275        strcpy( handle, out );
1276}
Note: See TracBrowser for help on using the repository browser.