source: protocols/nogaim.c @ dfbb056

Last change on this file since dfbb056 was 9143aeb, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-04-05T12:26:04Z

query.h now defines a callback function type, not using void* for it anymore.
Got rid of the bogus window handler pointer as the first argument to the
callback.

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