source: protocols/nogaim.c @ 58adb7e

Last change on this file since 58adb7e was 58adb7e, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-03-07T00:22:33Z

Added global and per-account settings "away" and "status" so the user can
set these things individually.

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