source: protocols/nogaim.c @ cd741d8

Last change on this file since cd741d8 was cd741d8, checked in by Wilmer van der Gaast <wilmer@…>, at 2009-11-23T23:23:37Z

Fixed compatibility with non-libpurple version: oscar is now recognized
as a protocol name, and removed prpl- hack from nogaim.c.

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