source: protocols/nogaim.c @ c0c43fb

Last change on this file since c0c43fb was c0c43fb, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-12-14T10:31:49Z

Fixed ic->away leaking memory. This var is only used by OSCAR and should
maybe be killed. Also fixed some completely broken indentation in those
functions.

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