source: protocols/nogaim.c @ 0da65d5

Last change on this file since 0da65d5 was 0da65d5, checked in by Wilmer van der Gaast <wilmer@…>, at 2007-03-31T05:40:45Z

s/gaim_connection/im_connection/ and some other minor API changes. The rest
will come tomorrow. It compiles, I'll leave the real testing up to someone
else. ;-)

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