source: protocols/nogaim.c @ 34fbbf9

Last change on this file since 34fbbf9 was 34fbbf9, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-03-06T18:22:20Z

Adapted nogaim.c. Pretty nice cleanup of away state alias handling, mostly.

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