source: protocols/nogaim.c @ c2fb3809

Last change on this file since c2fb3809 was c2fb3809, checked in by Wilmer van der Gaast <wilmer@…>, at 2007-04-15T22:39:35Z

Cleaned up struct im_connection. No more username/password stuff since
it's in acc too. wants_to_die is now an argument to imc_logout().

  • Property mode set to 100644
File size: 24.8 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
[0da65d5]38static int remove_chat_buddy_silent( struct groupchat *b, 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) {
50                log_message(LOGLVL_ERROR, "Can't find `%s', not loading", path);
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
101 
102struct prpl *find_protocol(const char *name)
103{
104        GList *gl;
105        for (gl = protocols; gl; gl = gl->next) 
106        {
107                struct prpl *proto = gl->data;
108                if(!g_strcasecmp(proto->name, name)) 
109                        return proto;
110        }
111        return NULL;
112}
113
114/* nogaim.c */
[b7d3cc34]115void nogaim_init()
116{
[0da65d5]117        extern void msn_initmodule();
118        extern void oscar_initmodule();
119        extern void byahoo_initmodule();
120        extern void jabber_initmodule();
[7b23afd]121
[b7d3cc34]122#ifdef WITH_MSN
[0da65d5]123        msn_initmodule();
[b7d3cc34]124#endif
125
126#ifdef WITH_OSCAR
[0da65d5]127        oscar_initmodule();
[b7d3cc34]128#endif
129       
130#ifdef WITH_YAHOO
[0da65d5]131        byahoo_initmodule();
[b7d3cc34]132#endif
133       
134#ifdef WITH_JABBER
[0da65d5]135        jabber_initmodule();
[b7d3cc34]136#endif
[7b23afd]137
[65e2ce1]138#ifdef WITH_PLUGINS
139        load_plugins();
[b7d3cc34]140#endif
141}
142
143GSList *get_connections() { return connections; }
144
145/* multi.c */
146
[aef4828]147struct im_connection *imc_new( account_t *acc )
[b7d3cc34]148{
[0da65d5]149        struct im_connection *ic;
[b7d3cc34]150       
[0da65d5]151        ic = g_new0( struct im_connection, 1 );
[b7d3cc34]152       
[0da65d5]153        ic->irc = acc->irc;
154        ic->acc = acc;
155        acc->ic = ic;
[b7d3cc34]156       
[0da65d5]157        connections = g_slist_append( connections, ic );
[b7d3cc34]158       
[0da65d5]159        return( ic );
[b7d3cc34]160}
161
[aef4828]162void imc_free( struct im_connection *ic )
[b7d3cc34]163{
164        account_t *a;
165       
166        /* Destroy the pointer to this connection from the account list */
[0da65d5]167        for( a = ic->irc->accounts; a; a = a->next )
168                if( a->ic == ic )
[b7d3cc34]169                {
[0da65d5]170                        a->ic = NULL;
[b7d3cc34]171                        break;
172                }
173       
[0da65d5]174        connections = g_slist_remove( connections, ic );
175        g_free( ic );
[b7d3cc34]176}
177
[aef4828]178static void serv_got_crap( struct im_connection *ic, char *format, ... )
[b7d3cc34]179{
180        va_list params;
[e27661d]181        char *text;
[dfde8e0]182        account_t *a;
[b7d3cc34]183       
184        va_start( params, format );
[e27661d]185        text = g_strdup_vprintf( format, params );
[b7d3cc34]186        va_end( params );
187
[0da65d5]188        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
189            ( ( ic->flags & OPT_CONN_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
[e27661d]190                strip_html( text );
[b7d3cc34]191       
[dfde8e0]192        /* Try to find a different connection on the same protocol. */
[0da65d5]193        for( a = ic->irc->accounts; a; a = a->next )
194                if( a->prpl == ic->acc->prpl && a->ic != ic )
[dfde8e0]195                        break;
196       
[e27661d]197        /* If we found one, include the screenname in the message. */
[dfde8e0]198        if( a )
[c2fb3809]199                irc_usermsg( ic->irc, "%s(%s) - %s", ic->acc->prpl->name, ic->acc->user, text );
[dfde8e0]200        else
[0da65d5]201                irc_usermsg( ic->irc, "%s - %s", ic->acc->prpl->name, text );
[7b07dc6]202       
[e27661d]203        g_free( text );
[b7d3cc34]204}
205
[aef4828]206void imc_log( struct im_connection *ic, char *format, ... )
207{
208        va_list params;
209        char *text;
210       
211        va_start( params, format );
212        text = g_strdup_vprintf( format, params );
213        va_end( params );
214       
215        if( ic->flags & OPT_LOGGED_IN )
216                serv_got_crap( ic, "%s", text );
217        else
218                serv_got_crap( ic, "Logging in: %s", text );
219       
220        g_free( text );
221}
222
223void imc_error( struct im_connection *ic, char *format, ... )
224{
225        va_list params;
226        char *text;
227       
228        va_start( params, format );
229        text = g_strdup_vprintf( format, params );
230        va_end( params );
231       
232        if( ic->flags & OPT_LOGGED_IN )
233                serv_got_crap( ic, "Error: %s", text );
234        else
235                serv_got_crap( ic, "Couldn't log in: %s", text );
236       
237        g_free( text );
238}
239
[ba9edaa]240static gboolean send_keepalive( gpointer d, gint fd, b_input_condition cond )
[b7d3cc34]241{
[0da65d5]242        struct im_connection *ic = d;
[b7d3cc34]243       
[0da65d5]244        if( ic->acc->prpl->keepalive )
245                ic->acc->prpl->keepalive( ic );
[b7d3cc34]246       
247        return TRUE;
248}
249
[aef4828]250void imc_connected( struct im_connection *ic )
[b7d3cc34]251{
252        user_t *u;
253       
254        /* MSN servers sometimes redirect you to a different server and do
[84c1a0a]255           the whole login sequence again, so these "late" calls to this
[b7d3cc34]256           function should be handled correctly. (IOW, ignored) */
[0da65d5]257        if( ic->flags & OPT_LOGGED_IN )
[b7d3cc34]258                return;
259       
[0da65d5]260        u = user_find( ic->irc, ic->irc->nick );
[b7d3cc34]261       
[aef4828]262        imc_log( ic, "Logged in" );
[b7d3cc34]263       
[0da65d5]264        ic->keepalive = b_timeout_add( 60000, send_keepalive, ic );
265        ic->flags |= OPT_LOGGED_IN;
[b7d3cc34]266       
[61dddd0]267        /* Also necessary when we're not away, at least for some of the
268           protocols. */
[0da65d5]269        bim_set_away( ic, u->away );
[b7d3cc34]270}
271
[ba9edaa]272gboolean auto_reconnect( gpointer data, gint fd, b_input_condition cond )
[b7d3cc34]273{
274        account_t *a = data;
275       
276        a->reconnect = 0;
277        account_on( a->irc, a );
278       
279        return( FALSE );        /* Only have to run the timeout once */
280}
281
282void cancel_auto_reconnect( account_t *a )
283{
[c98be00]284        /* while( b_event_remove_by_data( (gpointer) a ) ); */
285        b_event_remove( a->reconnect );
[b7d3cc34]286        a->reconnect = 0;
287}
288
[c2fb3809]289void imc_logout( struct im_connection *ic, int allow_reconnect )
[b7d3cc34]290{
[0da65d5]291        irc_t *irc = ic->irc;
[b7d3cc34]292        user_t *t, *u = irc->users;
293        account_t *a;
294       
[8d74291]295        /* Nested calls might happen sometimes, this is probably the best
296           place to catch them. */
[0da65d5]297        if( ic->flags & OPT_LOGGING_OUT )
[8d74291]298                return;
[66f783f]299        else
[0da65d5]300                ic->flags |= OPT_LOGGING_OUT;
[8d74291]301       
[aef4828]302        imc_log( ic, "Signing off.." );
[fb62f81f]303       
[0da65d5]304        b_event_remove( ic->keepalive );
305        ic->keepalive = 0;
306        ic->acc->prpl->logout( ic );
307        b_event_remove( ic->inpa );
[b7d3cc34]308       
309        while( u )
310        {
[0da65d5]311                if( u->ic == ic )
[b7d3cc34]312                {
313                        t = u->next;
314                        user_del( irc, u->nick );
315                        u = t;
316                }
317                else
318                        u = u->next;
319        }
320       
[0da65d5]321        query_del_by_conn( ic->irc, ic );
[b7d3cc34]322       
323        for( a = irc->accounts; a; a = a->next )
[0da65d5]324                if( a->ic == ic )
[b7d3cc34]325                        break;
326       
327        if( !a )
328        {
329                /* Uhm... This is very sick. */
330        }
[c2fb3809]331        else if( allow_reconnect && set_getbool( &irc->set, "auto_reconnect" ) &&
[00a5270]332                 set_getbool( &a->set, "auto_reconnect" ) )
[b7d3cc34]333        {
[5c9512f]334                int delay = set_getint( &irc->set, "auto_reconnect_delay" );
[b7d3cc34]335               
[aef4828]336                imc_log( ic, "Reconnecting in %d seconds..", delay );
[c98be00]337                a->reconnect = b_timeout_add( delay * 1000, auto_reconnect, a );
[b7d3cc34]338        }
339       
[aef4828]340        imc_free( ic );
[b7d3cc34]341}
342
343
344/* dialogs.c */
345
[0da65d5]346void do_ask_dialog( struct im_connection *ic, char *msg, void *data, void *doit, void *dont )
[b7d3cc34]347{
[0da65d5]348        query_add( ic->irc, ic, msg, doit, dont, data );
[b7d3cc34]349}
350
351
352/* list.c */
353
[0da65d5]354void add_buddy( struct im_connection *ic, char *group, char *handle, char *realname )
[b7d3cc34]355{
356        user_t *u;
357        char nick[MAX_NICK_LENGTH+1];
358        char *s;
[0da65d5]359        irc_t *irc = ic->irc;
[b7d3cc34]360       
[d5ccd83]361        if( set_getbool( &irc->set, "debug" ) && 0 ) /* This message is too useless */
[aef4828]362                imc_log( ic, "Receiving user add from handle: %s", handle );
[b7d3cc34]363       
[0da65d5]364        if( user_findhandle( ic, handle ) )
[b7d3cc34]365        {
[d5ccd83]366                if( set_getbool( &irc->set, "debug" ) )
[aef4828]367                        imc_log( ic, "User already exists, ignoring add request: %s", handle );
[b7d3cc34]368               
369                return;
370               
371                /* Buddy seems to exist already. Let's ignore this request then... */
372        }
373       
374        memset( nick, 0, MAX_NICK_LENGTH + 1 );
[0da65d5]375        strcpy( nick, nick_get( ic->acc, handle, realname ) );
[b7d3cc34]376       
[0da65d5]377        u = user_add( ic->irc, nick );
[b7d3cc34]378       
379        if( !realname || !*realname ) realname = nick;
380        u->realname = g_strdup( realname );
381       
382        if( ( s = strchr( handle, '@' ) ) )
383        {
384                u->host = g_strdup( s + 1 );
385                u->user = g_strndup( handle, s - handle );
386        }
[0da65d5]387        else if( ic->acc->server )
[b7d3cc34]388        {
[c53911e]389                char *colon;
390               
[0da65d5]391                if( ( colon = strchr( ic->acc->server, ':' ) ) )
392                        u->host = g_strndup( ic->acc->server,
393                                             colon - ic->acc->server );
[c53911e]394                else
[0da65d5]395                        u->host = g_strdup( ic->acc->server );
[c53911e]396               
[b7d3cc34]397                u->user = g_strdup( handle );
398               
399                /* s/ /_/ ... important for AOL screennames */
400                for( s = u->user; *s; s ++ )
401                        if( *s == ' ' )
402                                *s = '_';
403        }
404        else
405        {
[0da65d5]406                u->host = g_strdup( ic->acc->prpl->name );
[b7d3cc34]407                u->user = g_strdup( handle );
408        }
409       
[0da65d5]410        u->ic = ic;
[b7d3cc34]411        u->handle = g_strdup( handle );
[9b8a38b]412        if( group ) u->group = g_strdup( group );
[b7d3cc34]413        u->send_handler = buddy_send_handler;
414        u->last_typing_notice = 0;
415}
416
[0da65d5]417struct buddy *find_buddy( struct im_connection *ic, char *handle )
[b7d3cc34]418{
419        static struct buddy b[1];
420        user_t *u;
421       
[0da65d5]422        u = user_findhandle( ic, handle );
[b7d3cc34]423       
424        if( !u )
425                return( NULL );
426
427        memset( b, 0, sizeof( b ) );
428        strncpy( b->name, handle, 80 );
429        strncpy( b->show, u->realname, BUDDY_ALIAS_MAXLEN );
430        b->present = u->online;
[0da65d5]431        b->ic = u->ic;
[b7d3cc34]432       
433        return( b );
434}
435
436
[0da65d5]437void serv_buddy_rename( struct im_connection *ic, char *handle, char *realname )
[b7d3cc34]438{
[0da65d5]439        user_t *u = user_findhandle( ic, handle );
[b7d3cc34]440       
441        if( !u ) return;
442       
[e27661d]443        if( g_strcasecmp( u->realname, realname ) != 0 )
[b7d3cc34]444        {
445                if( u->realname != u->nick ) g_free( u->realname );
446               
[e27661d]447                u->realname = g_strdup( realname );
[b7d3cc34]448               
[0da65d5]449                if( ( ic->flags & OPT_LOGGED_IN ) && set_getbool( &ic->irc->set, "display_namechanges" ) )
[aef4828]450                        imc_log( ic, "User `%s' changed name to `%s'", u->nick, u->realname );
[b7d3cc34]451        }
452}
453
454
455/* prpl.c */
456
[7bf0f5f0]457struct show_got_added_data
458{
[0da65d5]459        struct im_connection *ic;
[7bf0f5f0]460        char *handle;
461};
462
463void show_got_added_no( gpointer w, struct show_got_added_data *data )
464{
465        g_free( data->handle );
466        g_free( data );
467}
468
469void show_got_added_yes( gpointer w, struct show_got_added_data *data )
470{
[0da65d5]471        data->ic->acc->prpl->add_buddy( data->ic, data->handle, NULL );
472        add_buddy( data->ic, NULL, data->handle, data->handle );
[7bf0f5f0]473       
474        return show_got_added_no( w, data );
475}
476
[0da65d5]477void show_got_added( struct im_connection *ic, char *handle, const char *realname )
[b7d3cc34]478{
[7bf0f5f0]479        struct show_got_added_data *data = g_new0( struct show_got_added_data, 1 );
480        char *s;
481       
482        /* TODO: Make a setting for this! */
[0da65d5]483        if( user_findhandle( ic, handle ) != NULL )
[7bf0f5f0]484                return;
485       
486        s = g_strdup_printf( "The user %s is not in your buddy list yet. Do you want to add him/her now?", handle );
487       
[0da65d5]488        data->ic = ic;
[7bf0f5f0]489        data->handle = g_strdup( handle );
[0da65d5]490        query_add( ic->irc, ic, s, show_got_added_yes, show_got_added_no, data );
[b7d3cc34]491}
492
493
494/* server.c */                   
495
[0da65d5]496void serv_got_update( struct im_connection *ic, char *handle, int loggedin, int evil, time_t signon, time_t idle, int type, guint caps )
[b7d3cc34]497{
498        user_t *u;
499        int oa, oo;
500       
[0da65d5]501        u = user_findhandle( ic, handle );
[b7d3cc34]502       
503        if( !u )
504        {
[0da65d5]505                if( g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "add" ) == 0 )
[b7d3cc34]506                {
[0da65d5]507                        add_buddy( ic, NULL, handle, NULL );
508                        u = user_findhandle( ic, handle );
[b7d3cc34]509                }
510                else
511                {
[0da65d5]512                        if( set_getbool( &ic->irc->set, "debug" ) || g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "ignore" ) != 0 )
[b7d3cc34]513                        {
[aef4828]514                                imc_log( ic, "serv_got_update() for handle %s:", handle );
515                                imc_log( ic, "loggedin = %d, type = %d", loggedin, type );
[b7d3cc34]516                        }
517                       
518                        return;
519                }
[dd89a55]520                /* Why did we have this here....
521                return; */
[b7d3cc34]522        }
523       
524        oa = u->away != NULL;
525        oo = u->online;
526       
527        if( u->away )
528        {
529                g_free( u->away );
530                u->away = NULL;
531        }
532       
533        if( loggedin && !u->online )
534        {
[0da65d5]535                irc_spawn( ic->irc, u );
[b7d3cc34]536                u->online = 1;
537        }
538        else if( !loggedin && u->online )
539        {
[0da65d5]540                struct groupchat *c;
[b7d3cc34]541               
[0da65d5]542                irc_kill( ic->irc, u );
[b7d3cc34]543                u->online = 0;
544               
545                /* Remove him/her from the conversations to prevent PART messages after he/she QUIT already */
[0da65d5]546                for( c = ic->conversations; c; c = c->next )
[b7d3cc34]547                        remove_chat_buddy_silent( c, handle );
548        }
549       
[0da65d5]550        if( ( type & UC_UNAVAILABLE ) && ( strcmp( ic->acc->prpl->name, "oscar" ) == 0 || strcmp( ic->acc->prpl->name, "icq" ) == 0 ) )
[b7d3cc34]551        {
552                u->away = g_strdup( "Away" );
553        }
[0da65d5]554        else if( ( type & UC_UNAVAILABLE ) && ( strcmp( ic->acc->prpl->name, "jabber" ) == 0 ) )
[b7d3cc34]555        {
556                if( type & UC_DND )
557                        u->away = g_strdup( "Do Not Disturb" );
558                else if( type & UC_XA )
559                        u->away = g_strdup( "Extended Away" );
560                else // if( type & UC_AWAY )
561                        u->away = g_strdup( "Away" );
562        }
[0da65d5]563        else if( ( type & UC_UNAVAILABLE ) && ic->acc->prpl->get_status_string )
[b7d3cc34]564        {
[0da65d5]565                u->away = g_strdup( ic->acc->prpl->get_status_string( ic, type ) );
[b7d3cc34]566        }
567        else
568                u->away = NULL;
569       
570        /* LISPy... */
[0da65d5]571        if( ( set_getbool( &ic->irc->set, "away_devoice" ) ) &&         /* Don't do a thing when user doesn't want it */
[b7d3cc34]572            ( u->online ) &&                                            /* Don't touch offline people */
573            ( ( ( u->online != oo ) && !u->away ) ||                    /* Voice joining people */
574              ( ( u->online == oo ) && ( oa == !u->away ) ) ) )         /* (De)voice people changing state */
575        {
[0da65d5]576                irc_write( ic->irc, ":%s MODE %s %cv %s", ic->irc->myhost,
577                                                                ic->irc->channel, u->away?'-':'+', u->nick );
[b7d3cc34]578        }
579}
580
[0da65d5]581void serv_got_im( struct im_connection *ic, char *handle, char *msg, guint32 flags, time_t mtime, gint len )
[b7d3cc34]582{
[0da65d5]583        irc_t *irc = ic->irc;
[b7d3cc34]584        user_t *u;
585       
[0da65d5]586        u = user_findhandle( ic, handle );
[b7d3cc34]587       
588        if( !u )
589        {
[5c9512f]590                char *h = set_getstr( &irc->set, "handle_unknown" );
[b7d3cc34]591               
592                if( g_strcasecmp( h, "ignore" ) == 0 )
593                {
[d5ccd83]594                        if( set_getbool( &irc->set, "debug" ) )
[aef4828]595                                imc_log( ic, "Ignoring message from unknown handle %s", handle );
[b7d3cc34]596                       
597                        return;
598                }
599                else if( g_strncasecmp( h, "add", 3 ) == 0 )
600                {
[d5ccd83]601                        int private = set_getbool( &irc->set, "private" );
[b7d3cc34]602                       
603                        if( h[3] )
604                        {
605                                if( g_strcasecmp( h + 3, "_private" ) == 0 )
606                                        private = 1;
607                                else if( g_strcasecmp( h + 3, "_channel" ) == 0 )
608                                        private = 0;
609                        }
610                       
[0da65d5]611                        add_buddy( ic, NULL, handle, NULL );
612                        u = user_findhandle( ic, handle );
[b7d3cc34]613                        u->is_private = private;
614                }
615                else
616                {
[aef4828]617                        imc_log( ic, "Message from unknown handle %s:", handle );
[b7d3cc34]618                        u = user_find( irc, irc->mynick );
619                }
620        }
621       
[0da65d5]622        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
623            ( ( ic->flags & OPT_CONN_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
[b7d3cc34]624                strip_html( msg );
625
[30f248a]626        while( strlen( msg ) > 425 )
[b7d3cc34]627        {
628                char tmp, *nl;
629               
[30f248a]630                tmp = msg[425];
631                msg[425] = 0;
[b7d3cc34]632               
[30f248a]633                /* If there's a newline/space in this string, split up there,
634                   looks a bit prettier. */
[f1df064]635                if( ( nl = strrchr( msg, '\n' ) ) || ( nl = strrchr( msg, ' ' ) ) )
[30f248a]636                {
637                        msg[425] = tmp;
638                        tmp = *nl;
[b7d3cc34]639                        *nl = 0;
[30f248a]640                }
[b7d3cc34]641               
642                irc_msgfrom( irc, u->nick, msg );
643               
644                /* Move on. */
645                if( nl )
646                {
[30f248a]647                        *nl = tmp;
[b7d3cc34]648                        msg = nl + 1;
649                }
650                else
651                {
[30f248a]652                        msg[425] = tmp;
653                        msg += 425;
[b7d3cc34]654                }
655        }
656        irc_msgfrom( irc, u->nick, msg );
657}
658
[0da65d5]659void serv_got_typing( struct im_connection *ic, char *handle, int timeout, int type )
[b7d3cc34]660{
661        user_t *u;
662       
[0da65d5]663        if( !set_getbool( &ic->irc->set, "typing_notice" ) )
[b7d3cc34]664                return;
665       
[0da65d5]666        if( ( u = user_findhandle( ic, handle ) ) ) {
[e7f46c5]667                /* If type is:
668                 * 0: user has stopped typing
669                 * 1: user is actively typing
670                 * 2: user has entered text, but is not actively typing
671                 */
672                if (type == 0 || type == 1 || type == 2) {
673                        char buf[256]; 
674                        g_snprintf(buf, 256, "\1TYPING %d\1", type); 
[0da65d5]675                        irc_privmsg( ic->irc, u, "PRIVMSG", ic->irc->nick, NULL, buf );
[e7f46c5]676                }
677        }
[b7d3cc34]678}
679
[0da65d5]680void serv_got_chat_left( struct groupchat *c )
[b7d3cc34]681{
[0da65d5]682        struct im_connection *ic = c->ic;
683        struct groupchat *l = NULL;
[b7d3cc34]684        GList *ir;
685       
[0da65d5]686        if( set_getbool( &ic->irc->set, "debug" ) )
[aef4828]687                imc_log( ic, "You were removed from conversation 0x%x", (int) c );
[b7d3cc34]688       
689        if( c )
690        {
691                if( c->joined )
692                {
693                        user_t *u, *r;
694                       
[0da65d5]695                        r = user_find( ic->irc, ic->irc->mynick );
696                        irc_privmsg( ic->irc, r, "PRIVMSG", c->channel, "", "Cleaning up channel, bye!" );
[b7d3cc34]697                       
[0da65d5]698                        u = user_find( ic->irc, ic->irc->nick );
699                        irc_kick( ic->irc, u, c->channel, r );
700                        /* irc_part( ic->irc, u, c->channel ); */
[b7d3cc34]701                }
702               
703                if( l )
704                        l->next = c->next;
705                else
[0da65d5]706                        ic->conversations = c->next;
[b7d3cc34]707               
708                for( ir = c->in_room; ir; ir = ir->next )
709                        g_free( ir->data );
710                g_list_free( c->in_room );
711                g_free( c->channel );
712                g_free( c->title );
713                g_free( c );
714        }
715}
716
[0da65d5]717void serv_got_chat_in( struct groupchat *c, char *who, int whisper, char *msg, time_t mtime )
[b7d3cc34]718{
[0da65d5]719        struct im_connection *ic = c->ic;
[b7d3cc34]720        user_t *u;
721       
722        /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */
[c2fb3809]723        if( g_strcasecmp( who, ic->acc->user ) == 0 )
[b7d3cc34]724                return;
725       
[0da65d5]726        u = user_findhandle( ic, who );
[b7d3cc34]727       
[0da65d5]728        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
729            ( ( ic->flags & OPT_CONN_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
[b7d3cc34]730                strip_html( msg );
731       
732        if( c && u )
[0da65d5]733                irc_privmsg( ic->irc, u, "PRIVMSG", c->channel, "", msg );
[b7d3cc34]734        else
[aef4828]735                imc_log( ic, "Message from/to conversation %s@0x%x (unknown conv/user): %s", who, (int) c, msg );
[b7d3cc34]736}
737
[0da65d5]738struct groupchat *serv_got_joined_chat( struct im_connection *ic, char *handle )
[b7d3cc34]739{
[0da65d5]740        struct groupchat *c;
[b7d3cc34]741       
742        /* This one just creates the conversation structure, user won't see anything yet */
743       
[0da65d5]744        if( ic->conversations )
[b7d3cc34]745        {
[0da65d5]746                for( c = ic->conversations; c->next; c = c->next );
747                c = c->next = g_new0( struct groupchat, 1 );
[b7d3cc34]748        }
749        else
[0da65d5]750                ic->conversations = c = g_new0( struct groupchat, 1 );
[b7d3cc34]751       
[0da65d5]752        c->ic = ic;
[b7d3cc34]753        c->title = g_strdup( handle );
[0da65d5]754        c->channel = g_strdup_printf( "&chat_%03d", ic->irc->c_id++ );
[b7d3cc34]755       
[0da65d5]756        if( set_getbool( &ic->irc->set, "debug" ) )
[aef4828]757                imc_log( ic, "Creating new conversation: (id=0x%x,handle=%s)", (int) c, handle );
[b7d3cc34]758       
[fa29d093]759        return c;
[b7d3cc34]760}
761
762
763/* buddy_chat.c */
764
[0da65d5]765void add_chat_buddy( struct groupchat *b, char *handle )
[b7d3cc34]766{
[0da65d5]767        user_t *u = user_findhandle( b->ic, handle );
[b7d3cc34]768        int me = 0;
769       
[0da65d5]770        if( set_getbool( &b->ic->irc->set, "debug" ) )
[aef4828]771                imc_log( b->ic, "User %s added to conversation 0x%x", handle, (int) b );
[b7d3cc34]772       
773        /* It might be yourself! */
[c2fb3809]774        if( b->ic->acc->prpl->handle_cmp( handle, b->ic->acc->user ) == 0 )
[b7d3cc34]775        {
[0da65d5]776                u = user_find( b->ic->irc, b->ic->irc->nick );
[b7d3cc34]777                if( !b->joined )
[0da65d5]778                        irc_join( b->ic->irc, u, b->channel );
[b7d3cc34]779                b->joined = me = 1;
780        }
781       
782        /* Most protocols allow people to join, even when they're not in
783           your contact list. Try to handle that here */
784        if( !u )
785        {
[0da65d5]786                add_buddy( b->ic, NULL, handle, NULL );
787                u = user_findhandle( b->ic, handle );
[b7d3cc34]788        }
789       
790        /* Add the handle to the room userlist, if it's not 'me' */
791        if( !me )
792        {
793                if( b->joined )
[0da65d5]794                        irc_join( b->ic->irc, u, b->channel );
[b7d3cc34]795                b->in_room = g_list_append( b->in_room, g_strdup( handle ) );
796        }
797}
798
[0da65d5]799void remove_chat_buddy( struct groupchat *b, char *handle, char *reason )
[b7d3cc34]800{
801        user_t *u;
802        int me = 0;
803       
[0da65d5]804        if( set_getbool( &b->ic->irc->set, "debug" ) )
[aef4828]805                imc_log( b->ic, "User %s removed from conversation 0x%x (%s)", handle, (int) b, reason ? reason : "" );
[b7d3cc34]806       
807        /* It might be yourself! */
[c2fb3809]808        if( g_strcasecmp( handle, b->ic->acc->user ) == 0 )
[b7d3cc34]809        {
[0da65d5]810                u = user_find( b->ic->irc, b->ic->irc->nick );
[b7d3cc34]811                b->joined = 0;
812                me = 1;
813        }
814        else
815        {
[0da65d5]816                u = user_findhandle( b->ic, handle );
[b7d3cc34]817        }
818       
819        if( remove_chat_buddy_silent( b, handle ) )
820                if( ( b->joined || me ) && u )
[0da65d5]821                        irc_part( b->ic->irc, u, b->channel );
[b7d3cc34]822}
823
[0da65d5]824static int remove_chat_buddy_silent( struct groupchat *b, char *handle )
[b7d3cc34]825{
826        GList *i;
827       
828        /* Find the handle in the room userlist and shoot it */
829        i = b->in_room;
830        while( i )
831        {
832                if( g_strcasecmp( handle, i->data ) == 0 )
833                {
834                        g_free( i->data );
835                        b->in_room = g_list_remove( b->in_room, i->data );
836                        return( 1 );
837                }
838               
839                i = i->next;
840        }
841       
842        return( 0 );
843}
844
845
846/* Misc. BitlBee stuff which shouldn't really be here */
847
[0da65d5]848struct groupchat *chat_by_channel( char *channel )
[b7d3cc34]849{
[0da65d5]850        struct im_connection *ic;
851        struct groupchat *c;
[b7d3cc34]852        GSList *l;
853       
854        /* This finds the connection which has a conversation which belongs to this channel */
855        for( l = connections; l; l = l->next )
856        {
[0da65d5]857                ic = l->data;
858                for( c = ic->conversations; c && g_strcasecmp( c->channel, channel ) != 0; c = c->next );
[b7d3cc34]859                if( c )
[fa29d093]860                        return c;
[b7d3cc34]861        }
862       
[fa29d093]863        return NULL;
[b7d3cc34]864}
865
[5c9512f]866char *set_eval_away_devoice( set_t *set, char *value )
[b7d3cc34]867{
[5c9512f]868        irc_t *irc = set->data;
[b7d3cc34]869        int st;
870       
871        if( ( g_strcasecmp( value, "true" ) == 0 ) || ( g_strcasecmp( value, "yes" ) == 0 ) || ( g_strcasecmp( value, "on" ) == 0 ) )
872                st = 1;
873        else if( ( g_strcasecmp( value, "false" ) == 0 ) || ( g_strcasecmp( value, "no" ) == 0 ) || ( g_strcasecmp( value, "off" ) == 0 ) )
874                st = 0;
875        else if( sscanf( value, "%d", &st ) != 1 )
876                return( NULL );
877       
878        st = st != 0;
879       
880        /* Horror.... */
881       
[d5ccd83]882        if( st != set_getbool( &irc->set, "away_devoice" ) )
[b7d3cc34]883        {
884                char list[80] = "";
885                user_t *u = irc->users;
886                int i = 0, count = 0;
887                char pm;
888                char v[80];
889               
890                if( st )
891                        pm = '+';
892                else
893                        pm = '-';
894               
895                while( u )
896                {
[0da65d5]897                        if( u->ic && u->online && !u->away )
[b7d3cc34]898                        {
899                                if( ( strlen( list ) + strlen( u->nick ) ) >= 79 )
900                                {
901                                        for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
[2087159]902                                        irc_write( irc, ":%s MODE %s %c%s%s",
903                                                   irc->myhost,
[b7d3cc34]904                                                   irc->channel, pm, v, list );
905                                       
906                                        *list = 0;
907                                        count = 0;
908                                }
909                               
910                                sprintf( list + strlen( list ), " %s", u->nick );
911                                count ++;
912                        }
913                        u = u->next;
914                }
915               
916                /* $v = 'v' x $i */
917                for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
[2087159]918                irc_write( irc, ":%s MODE %s %c%s%s", irc->myhost,
[b7d3cc34]919                                                            irc->channel, pm, v, list );
920        }
921       
[5c9512f]922        return( set_eval_bool( set, value ) );
[b7d3cc34]923}
924
[226fce1]925
926
927
928/* The plan is to not allow straight calls to prpl functions anymore, but do
929   them all from some wrappers. We'll start to define some down here: */
930
[0da65d5]931int bim_buddy_msg( struct im_connection *ic, char *handle, char *msg, int flags )
[b7d3cc34]932{
[e27661d]933        char *buf = NULL;
934        int st;
[b7d3cc34]935       
[0da65d5]936        if( ( ic->flags & OPT_CONN_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
[c572dd6]937        {
[e27661d]938                buf = escape_html( msg );
[c572dd6]939                msg = buf;
[b7d3cc34]940        }
941       
[0da65d5]942        st = ic->acc->prpl->send_im( ic, handle, msg, flags );
[e27661d]943        g_free( buf );
944       
945        return st;
[b7d3cc34]946}
947
[0da65d5]948int bim_chat_msg( struct groupchat *c, char *msg, int flags )
[b7d3cc34]949{
[e27661d]950        char *buf = NULL;
[b7d3cc34]951       
[0da65d5]952        if( ( c->ic->flags & OPT_CONN_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
[e27661d]953        {
954                buf = escape_html( msg );
[b7d3cc34]955                msg = buf;
956        }
957       
[0da65d5]958        c->ic->acc->prpl->chat_send( c, msg, flags );
[e27661d]959        g_free( buf );
960       
[0da65d5]961        return 1;
[b7d3cc34]962}
[226fce1]963
964static char *bim_away_alias_find( GList *gcm, char *away );
965
[0da65d5]966int bim_set_away( struct im_connection *ic, char *away )
[226fce1]967{
968        GList *m, *ms;
969        char *s;
970       
971        if( !away ) away = "";
[0da65d5]972        ms = m = ic->acc->prpl->away_states( ic );
[226fce1]973       
974        while( m )
975        {
976                if( *away )
977                {
978                        if( g_strncasecmp( m->data, away, strlen( m->data ) ) == 0 )
979                                break;
980                }
981                else
982                {
983                        if( g_strcasecmp( m->data, "Available" ) == 0 )
984                                break;
985                        if( g_strcasecmp( m->data, "Online" ) == 0 )
986                                break;
987                }
988                m = m->next;
989        }
990       
991        if( m )
992        {
[0da65d5]993                ic->acc->prpl->set_away( ic, m->data, *away ? away : NULL );
[226fce1]994        }
995        else
996        {
997                s = bim_away_alias_find( ms, away );
998                if( s )
999                {
[0da65d5]1000                        ic->acc->prpl->set_away( ic, s, away );
1001                        if( set_getbool( &ic->irc->set, "debug" ) )
[aef4828]1002                                imc_log( ic, "Setting away state to %s", s );
[226fce1]1003                }
1004                else
[0da65d5]1005                        ic->acc->prpl->set_away( ic, GAIM_AWAY_CUSTOM, away );
[226fce1]1006        }
1007       
1008        return( 1 );
1009}
1010
1011static char *bim_away_alias_list[8][5] =
1012{
1013        { "Away from computer", "Away", "Extended away", NULL },
1014        { "NA", "N/A", "Not available", NULL },
1015        { "Busy", "Do not disturb", "DND", "Occupied", NULL },
1016        { "Be right back", "BRB", NULL },
1017        { "On the phone", "Phone", "On phone", NULL },
1018        { "Out to lunch", "Lunch", "Food", NULL },
1019        { "Invisible", "Hidden" },
1020        { NULL }
1021};
1022
1023static char *bim_away_alias_find( GList *gcm, char *away )
1024{
1025        GList *m;
1026        int i, j;
1027       
1028        for( i = 0; *bim_away_alias_list[i]; i ++ )
1029        {
1030                for( j = 0; bim_away_alias_list[i][j]; j ++ )
1031                        if( g_strncasecmp( away, bim_away_alias_list[i][j], strlen( bim_away_alias_list[i][j] ) ) == 0 )
1032                                break;
1033               
1034                if( !bim_away_alias_list[i][j] )        /* If we reach the end, this row */
1035                        continue;                       /* is not what we want. Next!    */
1036               
1037                /* Now find an entry in this row which exists in gcm */
1038                for( j = 0; bim_away_alias_list[i][j]; j ++ )
1039                {
1040                        m = gcm;
1041                        while( m )
1042                        {
1043                                if( g_strcasecmp( bim_away_alias_list[i][j], m->data ) == 0 )
1044                                        return( bim_away_alias_list[i][j] );
1045                                m = m->next;
1046                        }
1047                }
1048        }
1049       
1050        return( NULL );
1051}
[da3b536]1052
[0da65d5]1053void bim_add_allow( struct im_connection *ic, char *handle )
[da3b536]1054{
[0da65d5]1055        if( g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL )
[da3b536]1056        {
[0da65d5]1057                ic->permit = g_slist_prepend( ic->permit, g_strdup( handle ) );
[da3b536]1058        }
1059       
[0da65d5]1060        ic->acc->prpl->add_permit( ic, handle );
[da3b536]1061}
1062
[0da65d5]1063void bim_rem_allow( struct im_connection *ic, char *handle )
[da3b536]1064{
1065        GSList *l;
1066       
[0da65d5]1067        if( ( l = g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) )
[da3b536]1068        {
1069                g_free( l->data );
[0da65d5]1070                ic->permit = g_slist_delete_link( ic->permit, l );
[da3b536]1071        }
1072       
[0da65d5]1073        ic->acc->prpl->rem_permit( ic, handle );
[da3b536]1074}
1075
[0da65d5]1076void bim_add_block( struct im_connection *ic, char *handle )
[da3b536]1077{
[0da65d5]1078        if( g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL )
[da3b536]1079        {
[0da65d5]1080                ic->deny = g_slist_prepend( ic->deny, g_strdup( handle ) );
[da3b536]1081        }
1082       
[0da65d5]1083        ic->acc->prpl->add_deny( ic, handle );
[da3b536]1084}
1085
[0da65d5]1086void bim_rem_block( struct im_connection *ic, char *handle )
[da3b536]1087{
1088        GSList *l;
1089       
[0da65d5]1090        if( ( l = g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) )
[da3b536]1091        {
1092                g_free( l->data );
[0da65d5]1093                ic->deny = g_slist_delete_link( ic->deny, l );
[da3b536]1094        }
1095       
[0da65d5]1096        ic->acc->prpl->rem_deny( ic, handle );
[da3b536]1097}
Note: See TracBrowser for help on using the repository browser.