source: protocols/nogaim.c @ d6aa6dd

Last change on this file since d6aa6dd was 839189b, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-05-02T21:06:25Z

Applied show-offline patch from Florian E.J. Fruth, adapted for a few
changes that happened since 1.2.4.

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