source: protocols/nogaim.c @ f0cb961

Last change on this file since f0cb961 was f0cb961, checked in by Wilmer van der Gaast <wilmer@…>, at 2007-04-19T06:03:43Z

More API changes: buddy list management. imcb_add_buddy() is now a *real*
callback, it's only called from inside IM-modules. This makes sure a buddy
only gets added to the BitlBee structures if the add was successful. This
gets rid of the weirdness described in #55. Unfortunately for now this
change breaks A) automatic renaming of ICQ contacts (if there are names
stored in the contact list) B) add -tmp.

  • Property mode set to 100644
File size: 24.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
[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
[84b045d]147struct im_connection *imcb_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 ) ||
[6bbb939]189            ( ( ic->flags & OPT_DOES_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
[84b045d]206void imcb_log( struct im_connection *ic, char *format, ... )
[aef4828]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
[84b045d]223void imcb_error( struct im_connection *ic, char *format, ... )
[aef4828]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
[84b045d]250void imcb_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       
[84b045d]262        imcb_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. */
[84b045d]269        imc_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       
[84b045d]302        imcb_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               
[84b045d]336                imcb_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
[84b045d]346void imcb_ask( 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
[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 );
[f0cb961]374        strcpy( nick, nick_get( ic->acc, handle, NULL ) );
[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
428
[f0cb961]429void imcb_rename_buddy( struct im_connection *ic, char *handle, char *realname )
[b7d3cc34]430{
[0da65d5]431        user_t *u = user_findhandle( ic, handle );
[b7d3cc34]432       
[f0cb961]433        if( !u || !realname ) return;
[b7d3cc34]434       
[e27661d]435        if( g_strcasecmp( u->realname, realname ) != 0 )
[b7d3cc34]436        {
437                if( u->realname != u->nick ) g_free( u->realname );
438               
[e27661d]439                u->realname = g_strdup( realname );
[b7d3cc34]440               
[0da65d5]441                if( ( ic->flags & OPT_LOGGED_IN ) && set_getbool( &ic->irc->set, "display_namechanges" ) )
[84b045d]442                        imcb_log( ic, "User `%s' changed name to `%s'", u->nick, u->realname );
[b7d3cc34]443        }
444}
445
446
447/* prpl.c */
448
[7bf0f5f0]449struct show_got_added_data
450{
[0da65d5]451        struct im_connection *ic;
[7bf0f5f0]452        char *handle;
453};
454
455void show_got_added_no( gpointer w, struct show_got_added_data *data )
456{
457        g_free( data->handle );
458        g_free( data );
459}
460
461void show_got_added_yes( gpointer w, struct show_got_added_data *data )
462{
[0da65d5]463        data->ic->acc->prpl->add_buddy( data->ic, data->handle, NULL );
[f0cb961]464        /* imcb_add_buddy( data->ic, NULL, data->handle, data->handle ); */
[7bf0f5f0]465       
466        return show_got_added_no( w, data );
467}
468
[84b045d]469void imcb_ask_add( struct im_connection *ic, char *handle, const char *realname )
[b7d3cc34]470{
[7bf0f5f0]471        struct show_got_added_data *data = g_new0( struct show_got_added_data, 1 );
472        char *s;
473       
474        /* TODO: Make a setting for this! */
[0da65d5]475        if( user_findhandle( ic, handle ) != NULL )
[7bf0f5f0]476                return;
477       
478        s = g_strdup_printf( "The user %s is not in your buddy list yet. Do you want to add him/her now?", handle );
479       
[0da65d5]480        data->ic = ic;
[7bf0f5f0]481        data->handle = g_strdup( handle );
[0da65d5]482        query_add( ic->irc, ic, s, show_got_added_yes, show_got_added_no, data );
[b7d3cc34]483}
484
485
486/* server.c */                   
487
[6bbb939]488void imcb_buddy_status( struct im_connection *ic, const char *handle, int flags, const char *state, const char *message )
[b7d3cc34]489{
490        user_t *u;
491        int oa, oo;
492       
[6bbb939]493        u = user_findhandle( ic, (char*) handle );
[b7d3cc34]494       
495        if( !u )
496        {
[0da65d5]497                if( g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "add" ) == 0 )
[b7d3cc34]498                {
[f0cb961]499                        imcb_add_buddy( ic, (char*) handle, NULL );
[6bbb939]500                        u = user_findhandle( ic, (char*) handle );
[b7d3cc34]501                }
502                else
503                {
[0da65d5]504                        if( set_getbool( &ic->irc->set, "debug" ) || g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "ignore" ) != 0 )
[b7d3cc34]505                        {
[6bbb939]506                                imcb_log( ic, "imcb_buddy_status() for unknown handle %s:", handle );
507                                imcb_log( ic, "flags = %d, state = %s, message = %s", flags,
508                                          state ? state : "NULL", message ? message : "NULL" );
[b7d3cc34]509                        }
510                       
511                        return;
512                }
513        }
514       
515        oa = u->away != NULL;
516        oo = u->online;
517       
518        if( u->away )
519        {
520                g_free( u->away );
521                u->away = NULL;
522        }
523       
[6bbb939]524        if( ( flags & OPT_LOGGED_IN ) && !u->online )
[b7d3cc34]525        {
[0da65d5]526                irc_spawn( ic->irc, u );
[b7d3cc34]527                u->online = 1;
528        }
[6bbb939]529        else if( !( flags & OPT_LOGGED_IN ) && u->online )
[b7d3cc34]530        {
[0da65d5]531                struct groupchat *c;
[b7d3cc34]532               
[0da65d5]533                irc_kill( ic->irc, u );
[b7d3cc34]534                u->online = 0;
535               
536                /* Remove him/her from the conversations to prevent PART messages after he/she QUIT already */
[0da65d5]537                for( c = ic->conversations; c; c = c->next )
[6bbb939]538                        remove_chat_buddy_silent( c, (char*) handle );
[b7d3cc34]539        }
540       
[6bbb939]541        if( flags & OPT_AWAY )
[b7d3cc34]542        {
[6bbb939]543                if( state && message )
544                {
545                        u->away = g_strdup_printf( "%s (%s)", state, message );
546                }
547                else if( state )
548                {
549                        u->away = g_strdup( state );
550                }
551                else if( message )
552                {
553                        u->away = g_strdup( message );
554                }
555                else
556                {
557                        u->away = g_strdup( "Away" );
558                }
[b7d3cc34]559        }
[6bbb939]560        /* else waste_any_state_information_for_now(); */
[b7d3cc34]561       
562        /* LISPy... */
[0da65d5]563        if( ( set_getbool( &ic->irc->set, "away_devoice" ) ) &&         /* Don't do a thing when user doesn't want it */
[b7d3cc34]564            ( u->online ) &&                                            /* Don't touch offline people */
565            ( ( ( u->online != oo ) && !u->away ) ||                    /* Voice joining people */
566              ( ( u->online == oo ) && ( oa == !u->away ) ) ) )         /* (De)voice people changing state */
567        {
[0da65d5]568                irc_write( ic->irc, ":%s MODE %s %cv %s", ic->irc->myhost,
[9624fdf]569                                                          ic->irc->channel, u->away?'-':'+', u->nick );
[b7d3cc34]570        }
571}
572
[9624fdf]573void imcb_buddy_msg( struct im_connection *ic, char *handle, char *msg, u_int32_t flags, time_t sent_at )
[b7d3cc34]574{
[0da65d5]575        irc_t *irc = ic->irc;
[b7d3cc34]576        user_t *u;
577       
[0da65d5]578        u = user_findhandle( ic, handle );
[b7d3cc34]579       
580        if( !u )
581        {
[5c9512f]582                char *h = set_getstr( &irc->set, "handle_unknown" );
[b7d3cc34]583               
584                if( g_strcasecmp( h, "ignore" ) == 0 )
585                {
[d5ccd83]586                        if( set_getbool( &irc->set, "debug" ) )
[84b045d]587                                imcb_log( ic, "Ignoring message from unknown handle %s", handle );
[b7d3cc34]588                       
589                        return;
590                }
591                else if( g_strncasecmp( h, "add", 3 ) == 0 )
592                {
[d5ccd83]593                        int private = set_getbool( &irc->set, "private" );
[b7d3cc34]594                       
595                        if( h[3] )
596                        {
597                                if( g_strcasecmp( h + 3, "_private" ) == 0 )
598                                        private = 1;
599                                else if( g_strcasecmp( h + 3, "_channel" ) == 0 )
600                                        private = 0;
601                        }
602                       
[f0cb961]603                        imcb_add_buddy( ic, handle, NULL );
[0da65d5]604                        u = user_findhandle( ic, handle );
[b7d3cc34]605                        u->is_private = private;
606                }
607                else
608                {
[84b045d]609                        imcb_log( ic, "Message from unknown handle %s:", handle );
[b7d3cc34]610                        u = user_find( irc, irc->mynick );
611                }
612        }
613       
[0da65d5]614        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
[6bbb939]615            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
[b7d3cc34]616                strip_html( msg );
617
[30f248a]618        while( strlen( msg ) > 425 )
[b7d3cc34]619        {
620                char tmp, *nl;
621               
[30f248a]622                tmp = msg[425];
623                msg[425] = 0;
[b7d3cc34]624               
[30f248a]625                /* If there's a newline/space in this string, split up there,
626                   looks a bit prettier. */
[f1df064]627                if( ( nl = strrchr( msg, '\n' ) ) || ( nl = strrchr( msg, ' ' ) ) )
[30f248a]628                {
629                        msg[425] = tmp;
630                        tmp = *nl;
[b7d3cc34]631                        *nl = 0;
[30f248a]632                }
[b7d3cc34]633               
634                irc_msgfrom( irc, u->nick, msg );
635               
636                /* Move on. */
637                if( nl )
638                {
[30f248a]639                        *nl = tmp;
[b7d3cc34]640                        msg = nl + 1;
641                }
642                else
643                {
[30f248a]644                        msg[425] = tmp;
645                        msg += 425;
[b7d3cc34]646                }
647        }
648        irc_msgfrom( irc, u->nick, msg );
649}
650
[9624fdf]651void imcb_buddy_typing( struct im_connection *ic, char *handle, u_int32_t flags )
[b7d3cc34]652{
653        user_t *u;
654       
[0da65d5]655        if( !set_getbool( &ic->irc->set, "typing_notice" ) )
[b7d3cc34]656                return;
657       
[9624fdf]658        if( ( u = user_findhandle( ic, handle ) ) )
659        {
660                char buf[256]; 
661               
662                g_snprintf( buf, 256, "\1TYPING %d\1", ( flags >> 8 ) & 3 );
663                irc_privmsg( ic->irc, u, "PRIVMSG", ic->irc->nick, NULL, buf );
[e7f46c5]664        }
[b7d3cc34]665}
666
[0da65d5]667void serv_got_chat_left( struct groupchat *c )
[b7d3cc34]668{
[0da65d5]669        struct im_connection *ic = c->ic;
670        struct groupchat *l = NULL;
[b7d3cc34]671        GList *ir;
672       
[0da65d5]673        if( set_getbool( &ic->irc->set, "debug" ) )
[84b045d]674                imcb_log( ic, "You were removed from conversation 0x%x", (int) c );
[b7d3cc34]675       
676        if( c )
677        {
678                if( c->joined )
679                {
680                        user_t *u, *r;
681                       
[0da65d5]682                        r = user_find( ic->irc, ic->irc->mynick );
683                        irc_privmsg( ic->irc, r, "PRIVMSG", c->channel, "", "Cleaning up channel, bye!" );
[b7d3cc34]684                       
[0da65d5]685                        u = user_find( ic->irc, ic->irc->nick );
686                        irc_kick( ic->irc, u, c->channel, r );
687                        /* irc_part( ic->irc, u, c->channel ); */
[b7d3cc34]688                }
689               
690                if( l )
691                        l->next = c->next;
692                else
[0da65d5]693                        ic->conversations = c->next;
[b7d3cc34]694               
695                for( ir = c->in_room; ir; ir = ir->next )
696                        g_free( ir->data );
697                g_list_free( c->in_room );
698                g_free( c->channel );
699                g_free( c->title );
700                g_free( c );
701        }
702}
703
[0da65d5]704void serv_got_chat_in( struct groupchat *c, char *who, int whisper, char *msg, time_t mtime )
[b7d3cc34]705{
[0da65d5]706        struct im_connection *ic = c->ic;
[b7d3cc34]707        user_t *u;
708       
709        /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */
[c2fb3809]710        if( g_strcasecmp( who, ic->acc->user ) == 0 )
[b7d3cc34]711                return;
712       
[0da65d5]713        u = user_findhandle( ic, who );
[b7d3cc34]714       
[0da65d5]715        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
[6bbb939]716            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
[b7d3cc34]717                strip_html( msg );
718       
719        if( c && u )
[0da65d5]720                irc_privmsg( ic->irc, u, "PRIVMSG", c->channel, "", msg );
[b7d3cc34]721        else
[84b045d]722                imcb_log( ic, "Message from/to conversation %s@0x%x (unknown conv/user): %s", who, (int) c, msg );
[b7d3cc34]723}
724
[0da65d5]725struct groupchat *serv_got_joined_chat( struct im_connection *ic, char *handle )
[b7d3cc34]726{
[0da65d5]727        struct groupchat *c;
[b7d3cc34]728       
729        /* This one just creates the conversation structure, user won't see anything yet */
730       
[0da65d5]731        if( ic->conversations )
[b7d3cc34]732        {
[0da65d5]733                for( c = ic->conversations; c->next; c = c->next );
734                c = c->next = g_new0( struct groupchat, 1 );
[b7d3cc34]735        }
736        else
[0da65d5]737                ic->conversations = c = g_new0( struct groupchat, 1 );
[b7d3cc34]738       
[0da65d5]739        c->ic = ic;
[b7d3cc34]740        c->title = g_strdup( handle );
[0da65d5]741        c->channel = g_strdup_printf( "&chat_%03d", ic->irc->c_id++ );
[b7d3cc34]742       
[0da65d5]743        if( set_getbool( &ic->irc->set, "debug" ) )
[84b045d]744                imcb_log( ic, "Creating new conversation: (id=0x%x,handle=%s)", (int) c, handle );
[b7d3cc34]745       
[fa29d093]746        return c;
[b7d3cc34]747}
748
749
750/* buddy_chat.c */
751
[0da65d5]752void add_chat_buddy( struct groupchat *b, char *handle )
[b7d3cc34]753{
[0da65d5]754        user_t *u = user_findhandle( b->ic, handle );
[b7d3cc34]755        int me = 0;
756       
[0da65d5]757        if( set_getbool( &b->ic->irc->set, "debug" ) )
[84b045d]758                imcb_log( b->ic, "User %s added to conversation 0x%x", handle, (int) b );
[b7d3cc34]759       
760        /* It might be yourself! */
[c2fb3809]761        if( b->ic->acc->prpl->handle_cmp( handle, b->ic->acc->user ) == 0 )
[b7d3cc34]762        {
[0da65d5]763                u = user_find( b->ic->irc, b->ic->irc->nick );
[b7d3cc34]764                if( !b->joined )
[0da65d5]765                        irc_join( b->ic->irc, u, b->channel );
[b7d3cc34]766                b->joined = me = 1;
767        }
768       
769        /* Most protocols allow people to join, even when they're not in
770           your contact list. Try to handle that here */
771        if( !u )
772        {
[f0cb961]773                imcb_add_buddy( b->ic, handle, NULL );
[0da65d5]774                u = user_findhandle( b->ic, handle );
[b7d3cc34]775        }
776       
777        /* Add the handle to the room userlist, if it's not 'me' */
778        if( !me )
779        {
780                if( b->joined )
[0da65d5]781                        irc_join( b->ic->irc, u, b->channel );
[b7d3cc34]782                b->in_room = g_list_append( b->in_room, g_strdup( handle ) );
783        }
784}
785
[0da65d5]786void remove_chat_buddy( struct groupchat *b, char *handle, char *reason )
[b7d3cc34]787{
788        user_t *u;
789        int me = 0;
790       
[0da65d5]791        if( set_getbool( &b->ic->irc->set, "debug" ) )
[84b045d]792                imcb_log( b->ic, "User %s removed from conversation 0x%x (%s)", handle, (int) b, reason ? reason : "" );
[b7d3cc34]793       
794        /* It might be yourself! */
[c2fb3809]795        if( g_strcasecmp( handle, b->ic->acc->user ) == 0 )
[b7d3cc34]796        {
[0da65d5]797                u = user_find( b->ic->irc, b->ic->irc->nick );
[b7d3cc34]798                b->joined = 0;
799                me = 1;
800        }
801        else
802        {
[0da65d5]803                u = user_findhandle( b->ic, handle );
[b7d3cc34]804        }
805       
806        if( remove_chat_buddy_silent( b, handle ) )
807                if( ( b->joined || me ) && u )
[0da65d5]808                        irc_part( b->ic->irc, u, b->channel );
[b7d3cc34]809}
810
[0da65d5]811static int remove_chat_buddy_silent( struct groupchat *b, char *handle )
[b7d3cc34]812{
813        GList *i;
814       
815        /* Find the handle in the room userlist and shoot it */
816        i = b->in_room;
817        while( i )
818        {
819                if( g_strcasecmp( handle, i->data ) == 0 )
820                {
821                        g_free( i->data );
822                        b->in_room = g_list_remove( b->in_room, i->data );
823                        return( 1 );
824                }
825               
826                i = i->next;
827        }
828       
829        return( 0 );
830}
831
832
833/* Misc. BitlBee stuff which shouldn't really be here */
834
[0da65d5]835struct groupchat *chat_by_channel( char *channel )
[b7d3cc34]836{
[0da65d5]837        struct im_connection *ic;
838        struct groupchat *c;
[b7d3cc34]839        GSList *l;
840       
841        /* This finds the connection which has a conversation which belongs to this channel */
842        for( l = connections; l; l = l->next )
843        {
[0da65d5]844                ic = l->data;
845                for( c = ic->conversations; c && g_strcasecmp( c->channel, channel ) != 0; c = c->next );
[b7d3cc34]846                if( c )
[fa29d093]847                        return c;
[b7d3cc34]848        }
849       
[fa29d093]850        return NULL;
[b7d3cc34]851}
852
[5c9512f]853char *set_eval_away_devoice( set_t *set, char *value )
[b7d3cc34]854{
[5c9512f]855        irc_t *irc = set->data;
[b7d3cc34]856        int st;
857       
858        if( ( g_strcasecmp( value, "true" ) == 0 ) || ( g_strcasecmp( value, "yes" ) == 0 ) || ( g_strcasecmp( value, "on" ) == 0 ) )
859                st = 1;
860        else if( ( g_strcasecmp( value, "false" ) == 0 ) || ( g_strcasecmp( value, "no" ) == 0 ) || ( g_strcasecmp( value, "off" ) == 0 ) )
861                st = 0;
862        else if( sscanf( value, "%d", &st ) != 1 )
863                return( NULL );
864       
865        st = st != 0;
866       
867        /* Horror.... */
868       
[d5ccd83]869        if( st != set_getbool( &irc->set, "away_devoice" ) )
[b7d3cc34]870        {
871                char list[80] = "";
872                user_t *u = irc->users;
873                int i = 0, count = 0;
874                char pm;
875                char v[80];
876               
877                if( st )
878                        pm = '+';
879                else
880                        pm = '-';
881               
882                while( u )
883                {
[0da65d5]884                        if( u->ic && u->online && !u->away )
[b7d3cc34]885                        {
886                                if( ( strlen( list ) + strlen( u->nick ) ) >= 79 )
887                                {
888                                        for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
[2087159]889                                        irc_write( irc, ":%s MODE %s %c%s%s",
890                                                   irc->myhost,
[b7d3cc34]891                                                   irc->channel, pm, v, list );
892                                       
893                                        *list = 0;
894                                        count = 0;
895                                }
896                               
897                                sprintf( list + strlen( list ), " %s", u->nick );
898                                count ++;
899                        }
900                        u = u->next;
901                }
902               
903                /* $v = 'v' x $i */
904                for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
[2087159]905                irc_write( irc, ":%s MODE %s %c%s%s", irc->myhost,
[b7d3cc34]906                                                            irc->channel, pm, v, list );
907        }
908       
[5c9512f]909        return( set_eval_bool( set, value ) );
[b7d3cc34]910}
911
[226fce1]912
913
914
915/* The plan is to not allow straight calls to prpl functions anymore, but do
916   them all from some wrappers. We'll start to define some down here: */
917
[84b045d]918int imc_buddy_msg( struct im_connection *ic, char *handle, char *msg, int flags )
[b7d3cc34]919{
[e27661d]920        char *buf = NULL;
921        int st;
[b7d3cc34]922       
[6bbb939]923        if( ( ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
[c572dd6]924        {
[e27661d]925                buf = escape_html( msg );
[c572dd6]926                msg = buf;
[b7d3cc34]927        }
928       
[0da65d5]929        st = ic->acc->prpl->send_im( ic, handle, msg, flags );
[e27661d]930        g_free( buf );
931       
932        return st;
[b7d3cc34]933}
934
[84b045d]935int imc_chat_msg( struct groupchat *c, char *msg, int flags )
[b7d3cc34]936{
[e27661d]937        char *buf = NULL;
[b7d3cc34]938       
[6bbb939]939        if( ( c->ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
[e27661d]940        {
941                buf = escape_html( msg );
[b7d3cc34]942                msg = buf;
943        }
944       
[0da65d5]945        c->ic->acc->prpl->chat_send( c, msg, flags );
[e27661d]946        g_free( buf );
947       
[0da65d5]948        return 1;
[b7d3cc34]949}
[226fce1]950
[84b045d]951static char *imc_away_alias_find( GList *gcm, char *away );
[226fce1]952
[84b045d]953int imc_set_away( struct im_connection *ic, char *away )
[226fce1]954{
955        GList *m, *ms;
956        char *s;
957       
958        if( !away ) away = "";
[0da65d5]959        ms = m = ic->acc->prpl->away_states( ic );
[226fce1]960       
961        while( m )
962        {
963                if( *away )
964                {
965                        if( g_strncasecmp( m->data, away, strlen( m->data ) ) == 0 )
966                                break;
967                }
968                else
969                {
970                        if( g_strcasecmp( m->data, "Available" ) == 0 )
971                                break;
972                        if( g_strcasecmp( m->data, "Online" ) == 0 )
973                                break;
974                }
975                m = m->next;
976        }
977       
978        if( m )
979        {
[0da65d5]980                ic->acc->prpl->set_away( ic, m->data, *away ? away : NULL );
[226fce1]981        }
982        else
983        {
[84b045d]984                s = imc_away_alias_find( ms, away );
[226fce1]985                if( s )
986                {
[0da65d5]987                        ic->acc->prpl->set_away( ic, s, away );
988                        if( set_getbool( &ic->irc->set, "debug" ) )
[84b045d]989                                imcb_log( ic, "Setting away state to %s", s );
[226fce1]990                }
991                else
[0da65d5]992                        ic->acc->prpl->set_away( ic, GAIM_AWAY_CUSTOM, away );
[226fce1]993        }
994       
995        return( 1 );
996}
997
[84b045d]998static char *imc_away_alias_list[8][5] =
[226fce1]999{
1000        { "Away from computer", "Away", "Extended away", NULL },
1001        { "NA", "N/A", "Not available", NULL },
1002        { "Busy", "Do not disturb", "DND", "Occupied", NULL },
1003        { "Be right back", "BRB", NULL },
1004        { "On the phone", "Phone", "On phone", NULL },
1005        { "Out to lunch", "Lunch", "Food", NULL },
1006        { "Invisible", "Hidden" },
1007        { NULL }
1008};
1009
[84b045d]1010static char *imc_away_alias_find( GList *gcm, char *away )
[226fce1]1011{
1012        GList *m;
1013        int i, j;
1014       
[84b045d]1015        for( i = 0; *imc_away_alias_list[i]; i ++ )
[226fce1]1016        {
[84b045d]1017                for( j = 0; imc_away_alias_list[i][j]; j ++ )
1018                        if( g_strncasecmp( away, imc_away_alias_list[i][j], strlen( imc_away_alias_list[i][j] ) ) == 0 )
[226fce1]1019                                break;
1020               
[84b045d]1021                if( !imc_away_alias_list[i][j] )        /* If we reach the end, this row */
[226fce1]1022                        continue;                       /* is not what we want. Next!    */
1023               
1024                /* Now find an entry in this row which exists in gcm */
[84b045d]1025                for( j = 0; imc_away_alias_list[i][j]; j ++ )
[226fce1]1026                {
1027                        m = gcm;
1028                        while( m )
1029                        {
[84b045d]1030                                if( g_strcasecmp( imc_away_alias_list[i][j], m->data ) == 0 )
1031                                        return( imc_away_alias_list[i][j] );
[226fce1]1032                                m = m->next;
1033                        }
1034                }
1035        }
1036       
1037        return( NULL );
1038}
[da3b536]1039
[84b045d]1040void imc_add_allow( struct im_connection *ic, char *handle )
[da3b536]1041{
[0da65d5]1042        if( g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL )
[da3b536]1043        {
[0da65d5]1044                ic->permit = g_slist_prepend( ic->permit, g_strdup( handle ) );
[da3b536]1045        }
1046       
[0da65d5]1047        ic->acc->prpl->add_permit( ic, handle );
[da3b536]1048}
1049
[84b045d]1050void imc_rem_allow( struct im_connection *ic, char *handle )
[da3b536]1051{
1052        GSList *l;
1053       
[0da65d5]1054        if( ( l = g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) )
[da3b536]1055        {
1056                g_free( l->data );
[0da65d5]1057                ic->permit = g_slist_delete_link( ic->permit, l );
[da3b536]1058        }
1059       
[0da65d5]1060        ic->acc->prpl->rem_permit( ic, handle );
[da3b536]1061}
1062
[84b045d]1063void imc_add_block( struct im_connection *ic, char *handle )
[da3b536]1064{
[0da65d5]1065        if( g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL )
[da3b536]1066        {
[0da65d5]1067                ic->deny = g_slist_prepend( ic->deny, g_strdup( handle ) );
[da3b536]1068        }
1069       
[0da65d5]1070        ic->acc->prpl->add_deny( ic, handle );
[da3b536]1071}
1072
[84b045d]1073void imc_rem_block( struct im_connection *ic, char *handle )
[da3b536]1074{
1075        GSList *l;
1076       
[0da65d5]1077        if( ( l = g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) )
[da3b536]1078        {
1079                g_free( l->data );
[0da65d5]1080                ic->deny = g_slist_delete_link( ic->deny, l );
[da3b536]1081        }
1082       
[0da65d5]1083        ic->acc->prpl->rem_deny( ic, handle );
[da3b536]1084}
Note: See TracBrowser for help on using the repository browser.