source: protocols/nogaim.c @ aef4828

Last change on this file since aef4828 was aef4828, checked in by Wilmer van der Gaast <wilmer@…>, at 2007-04-06T05:20:31Z

More cleanups, mainly in the callbacks. Replaced things like
do_error_dialog() and (set|hide)_login_progress(_error)?() with things
that hopefully make more sense.

Although it's still not really great...

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