source: protocols/nogaim.c @ fb020ac

Last change on this file since fb020ac was fb020ac, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-03-07T18:43:23Z

Merging in mainline, including improved away/status stuff.

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