source: protocols/nogaim.c @ 231b08b

Last change on this file since 231b08b was fb117aee, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-04-02T02:29:45Z

Cleaned lots of compiler warnings so I can get some signal again.

  • Property mode set to 100644
File size: 24.5 KB
RevLine 
[b7d3cc34]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
[58adb7e]4  * Copyright 2002-2010 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 <ctype.h>
36
[4cf80bb]37#include "nogaim.h"
38#include "chat.h"
39
[b7d3cc34]40GSList *connections;
41
[65e2ce1]42#ifdef WITH_PLUGINS
[7b23afd]43gboolean load_plugin(char *path)
44{
45        void (*init_function) (void);
46       
47        GModule *mod = g_module_open(path, G_MODULE_BIND_LAZY);
48
49        if(!mod) {
[8ad90fb]50                log_message(LOGLVL_ERROR, "Can't find `%s', not loading (%s)\n", path, g_module_error());
[7b23afd]51                return FALSE;
52        }
53
54        if(!g_module_symbol(mod,"init_plugin",(gpointer *) &init_function)) {
55                log_message(LOGLVL_WARNING, "Can't find function `init_plugin' in `%s'\n", path);
56                return FALSE;
57        }
58
59        init_function();
60
61        return TRUE;
62}
[b7d3cc34]63
[65e2ce1]64void load_plugins(void)
65{
66        GDir *dir;
67        GError *error = NULL;
68
[4bfca70]69        dir = g_dir_open(global.conf->plugindir, 0, &error);
[65e2ce1]70
71        if (dir) {
72                const gchar *entry;
73                char *path;
74
75                while ((entry = g_dir_read_name(dir))) {
[4bfca70]76                        path = g_build_filename(global.conf->plugindir, entry, NULL);
[65e2ce1]77                        if(!path) {
78                                log_message(LOGLVL_WARNING, "Can't build path for %s\n", entry);
79                                continue;
80                        }
81
82                        load_plugin(path);
83
84                        g_free(path);
85                }
86
87                g_dir_close(dir);
88        }
89}
90#endif
[b7d3cc34]91
[7b23afd]92GList *protocols = NULL;
93 
94void register_protocol (struct prpl *p)
95{
[90cd6c4]96        int i;
97        gboolean refused = global.conf->protocols != NULL;
98 
99        for (i = 0; global.conf->protocols && global.conf->protocols[i]; i++)
100        {
101                if (g_strcasecmp(p->name, global.conf->protocols[i]) == 0)
102                        refused = FALSE;
103        }
104
105        if (refused)
106                log_message(LOGLVL_WARNING, "Protocol %s disabled\n", p->name);
107        else
108                protocols = g_list_append(protocols, p);
[7b23afd]109}
110
111struct prpl *find_protocol(const char *name)
112{
113        GList *gl;
114        for (gl = protocols; gl; gl = gl->next) 
115        {
116                struct prpl *proto = gl->data;
117                if(!g_strcasecmp(proto->name, name)) 
118                        return proto;
119        }
120        return NULL;
121}
122
[b7d3cc34]123void nogaim_init()
124{
[0da65d5]125        extern void msn_initmodule();
126        extern void oscar_initmodule();
127        extern void byahoo_initmodule();
128        extern void jabber_initmodule();
[7b23afd]129
[b7d3cc34]130#ifdef WITH_MSN
[0da65d5]131        msn_initmodule();
[b7d3cc34]132#endif
133
134#ifdef WITH_OSCAR
[0da65d5]135        oscar_initmodule();
[b7d3cc34]136#endif
137       
138#ifdef WITH_YAHOO
[0da65d5]139        byahoo_initmodule();
[b7d3cc34]140#endif
141       
142#ifdef WITH_JABBER
[0da65d5]143        jabber_initmodule();
[b7d3cc34]144#endif
[7b23afd]145
[65e2ce1]146#ifdef WITH_PLUGINS
147        load_plugins();
[b7d3cc34]148#endif
149}
150
151GSList *get_connections() { return connections; }
152
[84b045d]153struct im_connection *imcb_new( account_t *acc )
[b7d3cc34]154{
[0da65d5]155        struct im_connection *ic;
[b7d3cc34]156       
[0da65d5]157        ic = g_new0( struct im_connection, 1 );
[b7d3cc34]158       
[81e04e1]159        ic->bee = acc->bee;
[0da65d5]160        ic->acc = acc;
161        acc->ic = ic;
[b7d3cc34]162       
[0da65d5]163        connections = g_slist_append( connections, ic );
[b7d3cc34]164       
[0da65d5]165        return( ic );
[b7d3cc34]166}
167
[aef4828]168void imc_free( struct im_connection *ic )
[b7d3cc34]169{
170        account_t *a;
171       
172        /* Destroy the pointer to this connection from the account list */
[81e04e1]173        for( a = ic->bee->accounts; a; a = a->next )
[0da65d5]174                if( a->ic == ic )
[b7d3cc34]175                {
[0da65d5]176                        a->ic = NULL;
[b7d3cc34]177                        break;
178                }
179       
[0da65d5]180        connections = g_slist_remove( connections, ic );
181        g_free( ic );
[b7d3cc34]182}
183
[aef4828]184static void serv_got_crap( struct im_connection *ic, char *format, ... )
[b7d3cc34]185{
186        va_list params;
[e27661d]187        char *text;
[dfde8e0]188        account_t *a;
[b7d3cc34]189       
190        va_start( params, format );
[e27661d]191        text = g_strdup_vprintf( format, params );
[b7d3cc34]192        va_end( params );
193
[81e04e1]194        if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) ||
195            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) )
[e27661d]196                strip_html( text );
[b7d3cc34]197       
[dfde8e0]198        /* Try to find a different connection on the same protocol. */
[81e04e1]199        for( a = ic->bee->accounts; a; a = a->next )
[0da65d5]200                if( a->prpl == ic->acc->prpl && a->ic != ic )
[dfde8e0]201                        break;
202       
[e27661d]203        /* If we found one, include the screenname in the message. */
[dfde8e0]204        if( a )
[81e04e1]205                /* FIXME(wilmer): ui_log callback or so */
206                irc_usermsg( ic->bee->ui_data, "%s(%s) - %s", ic->acc->prpl->name, ic->acc->user, text );
[dfde8e0]207        else
[81e04e1]208                irc_usermsg( ic->bee->ui_data, "%s - %s", ic->acc->prpl->name, text );
[7b07dc6]209       
[e27661d]210        g_free( text );
[b7d3cc34]211}
212
[84b045d]213void imcb_log( struct im_connection *ic, char *format, ... )
[aef4828]214{
215        va_list params;
216        char *text;
217       
218        va_start( params, format );
219        text = g_strdup_vprintf( format, params );
220        va_end( params );
221       
222        if( ic->flags & OPT_LOGGED_IN )
223                serv_got_crap( ic, "%s", text );
224        else
225                serv_got_crap( ic, "Logging in: %s", text );
226       
227        g_free( text );
228}
229
[84b045d]230void imcb_error( struct im_connection *ic, char *format, ... )
[aef4828]231{
232        va_list params;
233        char *text;
234       
235        va_start( params, format );
236        text = g_strdup_vprintf( format, params );
237        va_end( params );
238       
239        if( ic->flags & OPT_LOGGED_IN )
240                serv_got_crap( ic, "Error: %s", text );
241        else
242                serv_got_crap( ic, "Couldn't log in: %s", text );
243       
244        g_free( text );
245}
246
[ba9edaa]247static gboolean send_keepalive( gpointer d, gint fd, b_input_condition cond )
[b7d3cc34]248{
[0da65d5]249        struct im_connection *ic = d;
[b7d3cc34]250       
[0da65d5]251        if( ic->acc->prpl->keepalive )
252                ic->acc->prpl->keepalive( ic );
[b7d3cc34]253       
254        return TRUE;
255}
256
[84b045d]257void imcb_connected( struct im_connection *ic )
[b7d3cc34]258{
259        /* MSN servers sometimes redirect you to a different server and do
[84c1a0a]260           the whole login sequence again, so these "late" calls to this
[b7d3cc34]261           function should be handled correctly. (IOW, ignored) */
[0da65d5]262        if( ic->flags & OPT_LOGGED_IN )
[b7d3cc34]263                return;
264       
[84b045d]265        imcb_log( ic, "Logged in" );
[b7d3cc34]266       
[0da65d5]267        ic->keepalive = b_timeout_add( 60000, send_keepalive, ic );
268        ic->flags |= OPT_LOGGED_IN;
[b7d3cc34]269       
[58adb7e]270        /* Necessary to send initial presence status, even if we're not away. */
271        imc_away_send_update( ic );
[280e655]272       
273        /* Apparently we're connected successfully, so reset the
274           exponential backoff timer. */
275        ic->acc->auto_reconnect_delay = 0;
[3611717]276       
[10a96f4]277        /*
[3611717]278        for( c = irc->chatrooms; c; c = c->next )
279        {
280                if( c->acc != ic->acc )
281                        continue;
282               
283                if( set_getbool( &c->set, "auto_join" ) )
[94acdd0]284                        chat_join( irc, c, NULL );
[3611717]285        }
[10a96f4]286        */
[b7d3cc34]287}
288
[ba9edaa]289gboolean auto_reconnect( gpointer data, gint fd, b_input_condition cond )
[b7d3cc34]290{
291        account_t *a = data;
292       
293        a->reconnect = 0;
[81e04e1]294        account_on( a->bee, a );
[b7d3cc34]295       
296        return( FALSE );        /* Only have to run the timeout once */
297}
298
299void cancel_auto_reconnect( account_t *a )
300{
[c98be00]301        b_event_remove( a->reconnect );
[b7d3cc34]302        a->reconnect = 0;
303}
304
[c2fb3809]305void imc_logout( struct im_connection *ic, int allow_reconnect )
[b7d3cc34]306{
[81e04e1]307        bee_t *bee = ic->bee;
[b7d3cc34]308        account_t *a;
[81e04e1]309        GSList *l;
[4230221]310        int delay;
[b7d3cc34]311       
[8d74291]312        /* Nested calls might happen sometimes, this is probably the best
313           place to catch them. */
[0da65d5]314        if( ic->flags & OPT_LOGGING_OUT )
[8d74291]315                return;
[66f783f]316        else
[0da65d5]317                ic->flags |= OPT_LOGGING_OUT;
[8d74291]318       
[84b045d]319        imcb_log( ic, "Signing off.." );
[fb62f81f]320       
[0da65d5]321        b_event_remove( ic->keepalive );
322        ic->keepalive = 0;
323        ic->acc->prpl->logout( ic );
324        b_event_remove( ic->inpa );
[b7d3cc34]325       
[c0c43fb]326        g_free( ic->away );
327        ic->away = NULL;
328       
[81e04e1]329        for( l = bee->users; l; l = l->next )
[b7d3cc34]330        {
[81e04e1]331                bee_user_t *bu = l->data;
332               
333                if( bu->ic == ic )
334                        bee_user_free( bee, ic, bu->handle );
[b7d3cc34]335        }
336       
[81e04e1]337        //query_del_by_conn( ic->irc, ic );
[b7d3cc34]338       
[81e04e1]339        for( a = bee->accounts; a; a = a->next )
[0da65d5]340                if( a->ic == ic )
[b7d3cc34]341                        break;
342       
343        if( !a )
344        {
345                /* Uhm... This is very sick. */
346        }
[81e04e1]347        else if( allow_reconnect && set_getbool( &bee->set, "auto_reconnect" ) &&
[4230221]348                 set_getbool( &a->set, "auto_reconnect" ) &&
349                 ( delay = account_reconnect_delay( a ) ) > 0 )
[b7d3cc34]350        {
[84b045d]351                imcb_log( ic, "Reconnecting in %d seconds..", delay );
[c98be00]352                a->reconnect = b_timeout_add( delay * 1000, auto_reconnect, a );
[b7d3cc34]353        }
354       
[aef4828]355        imc_free( ic );
[b7d3cc34]356}
357
[9143aeb]358void imcb_ask( struct im_connection *ic, char *msg, void *data,
359               query_callback doit, query_callback dont )
[b7d3cc34]360{
[81e04e1]361        //query_add( ic->irc, ic, msg, doit, dont, data );
[b7d3cc34]362}
363
[c6ca3ee]364void imcb_add_buddy( struct im_connection *ic, const char *handle, const char *group )
[b7d3cc34]365{
[81e04e1]366        bee_user_t *bu;
367        //char nick[MAX_NICK_LENGTH+1], *s;
368        bee_t *bee = ic->bee;
[b7d3cc34]369       
[81e04e1]370        if( bee_user_by_handle( bee, ic, handle ) )
[b7d3cc34]371        {
[81e04e1]372                if( set_getbool( &bee->set, "debug" ) )
[84b045d]373                        imcb_log( ic, "User already exists, ignoring add request: %s", handle );
[b7d3cc34]374               
375                return;
376               
[f0cb961]377                /* Buddy seems to exist already. Let's ignore this request then...
378                   Eventually subsequent calls to this function *should* be possible
379                   when a buddy is in multiple groups. But for now BitlBee doesn't
380                   even support groups so let's silently ignore this for now. */
[b7d3cc34]381        }
382       
[81e04e1]383        bu = bee_user_new( bee, ic, handle );
384        bu->group = g_strdup( group );
[b7d3cc34]385}
386
[c6ca3ee]387void imcb_rename_buddy( struct im_connection *ic, const char *handle, const char *realname )
[b7d3cc34]388{
[81e04e1]389#if 0
[0da65d5]390        user_t *u = user_findhandle( ic, handle );
[286b28e]391        char *set;
[b7d3cc34]392       
[f0cb961]393        if( !u || !realname ) return;
[b7d3cc34]394       
[e27661d]395        if( g_strcasecmp( u->realname, realname ) != 0 )
[b7d3cc34]396        {
397                if( u->realname != u->nick ) g_free( u->realname );
398               
[e27661d]399                u->realname = g_strdup( realname );
[b7d3cc34]400               
[81e04e1]401                if( ( ic->flags & OPT_LOGGED_IN ) && set_getbool( &ic->bee->set, "display_namechanges" ) )
[84b045d]402                        imcb_log( ic, "User `%s' changed name to `%s'", u->nick, u->realname );
[b7d3cc34]403        }
[286b28e]404       
405        set = set_getstr( &ic->acc->set, "nick_source" );
406        if( strcmp( set, "handle" ) != 0 )
407        {
408                char *name = g_strdup( realname );
409               
410                if( strcmp( set, "first_name" ) == 0 )
411                {
412                        int i;
413                        for( i = 0; name[i] && !isspace( name[i] ); i ++ ) {}
414                        name[i] = '\0';
415                }
416               
417                imcb_buddy_nick_hint( ic, handle, name );
418               
419                g_free( name );
420        }
[81e04e1]421#endif
[b7d3cc34]422}
423
[c6ca3ee]424void imcb_remove_buddy( struct im_connection *ic, const char *handle, char *group )
[998b103]425{
[81e04e1]426        bee_user_free( ic->bee, ic, handle );
[998b103]427}
428
[d06eabf]429/* Mainly meant for ICQ (and now also for Jabber conferences) to allow IM
430   modules to suggest a nickname for a handle. */
[fb00989]431void imcb_buddy_nick_hint( struct im_connection *ic, const char *handle, const char *nick )
[d06eabf]432{
[81e04e1]433#if 0
[d06eabf]434        user_t *u = user_findhandle( ic, handle );
[43d8cc5]435        char newnick[MAX_NICK_LENGTH+1], *orig_nick;
[d06eabf]436       
[e0e2a71]437        if( u && !u->online && !nick_saved( ic->acc, handle ) )
[d06eabf]438        {
439                /* Only do this if the person isn't online yet (which should
440                   be the case if we just added it) and if the user hasn't
441                   assigned a nickname to this buddy already. */
442               
[e0e2a71]443                strncpy( newnick, nick, MAX_NICK_LENGTH );
444                newnick[MAX_NICK_LENGTH] = 0;
[d06eabf]445               
446                /* Some processing to make sure this string is a valid IRC nickname. */
447                nick_strip( newnick );
[81e04e1]448                if( set_getbool( &ic->bee->set, "lcnicks" ) )
[d06eabf]449                        nick_lc( newnick );
450               
[1962ac1]451                if( strcmp( u->nick, newnick ) != 0 )
452                {
453                        /* Only do this if newnick is different from the current one.
454                           If rejoining a channel, maybe we got this nick already
455                           (and dedupe would only add an underscore. */
456                        nick_dedupe( ic->acc, handle, newnick );
457                       
458                        /* u->nick will be freed halfway the process, so it can't be
459                           passed as an argument. */
460                        orig_nick = g_strdup( u->nick );
461                        user_rename( ic->irc, orig_nick, newnick );
462                        g_free( orig_nick );
463                }
[d06eabf]464        }
[81e04e1]465#endif
[d06eabf]466}
[b7d3cc34]467
468
[fa295e36]469struct imcb_ask_cb_data
[7bf0f5f0]470{
[0da65d5]471        struct im_connection *ic;
[7bf0f5f0]472        char *handle;
473};
474
[fb117aee]475#if 0
[fa295e36]476static void imcb_ask_auth_cb_no( void *data )
[7bf0f5f0]477{
[fa295e36]478        struct imcb_ask_cb_data *cbd = data;
479       
480        cbd->ic->acc->prpl->auth_deny( cbd->ic, cbd->handle );
481       
482        g_free( cbd->handle );
483        g_free( cbd );
484}
485
486static void imcb_ask_auth_cb_yes( void *data )
487{
488        struct imcb_ask_cb_data *cbd = data;
489       
490        cbd->ic->acc->prpl->auth_allow( cbd->ic, cbd->handle );
491       
492        g_free( cbd->handle );
493        g_free( cbd );
494}
[fb117aee]495#endif
[fa295e36]496
497void imcb_ask_auth( struct im_connection *ic, const char *handle, const char *realname )
498{
[81e04e1]499#if 0
[fa295e36]500        struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 );
501        char *s, *realname_ = NULL;
502       
503        if( realname != NULL )
504                realname_ = g_strdup_printf( " (%s)", realname );
505       
506        s = g_strdup_printf( "The user %s%s wants to add you to his/her buddy list.",
507                             handle, realname_ ?: "" );
508       
509        g_free( realname_ );
510       
511        data->ic = ic;
512        data->handle = g_strdup( handle );
513        query_add( ic->irc, ic, s, imcb_ask_auth_cb_yes, imcb_ask_auth_cb_no, data );
[81e04e1]514#endif
[fa295e36]515}
516
517
[fb117aee]518#if 0
[fa295e36]519static void imcb_ask_add_cb_no( void *data )
520{
521        g_free( ((struct imcb_ask_cb_data*)data)->handle );
[7bf0f5f0]522        g_free( data );
523}
524
[fa295e36]525static void imcb_ask_add_cb_yes( void *data )
[7bf0f5f0]526{
[fa295e36]527        struct imcb_ask_cb_data *cbd = data;
[7bf0f5f0]528       
[fa295e36]529        cbd->ic->acc->prpl->add_buddy( cbd->ic, cbd->handle, NULL );
[9143aeb]530       
[fa295e36]531        return imcb_ask_add_cb_no( data );
[7bf0f5f0]532}
[fb117aee]533#endif
[7bf0f5f0]534
[fa295e36]535void imcb_ask_add( struct im_connection *ic, const char *handle, const char *realname )
[b7d3cc34]536{
[81e04e1]537#if 0
[fa295e36]538        struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 );
[7bf0f5f0]539        char *s;
540       
541        /* TODO: Make a setting for this! */
[0da65d5]542        if( user_findhandle( ic, handle ) != NULL )
[7bf0f5f0]543                return;
544       
545        s = g_strdup_printf( "The user %s is not in your buddy list yet. Do you want to add him/her now?", handle );
546       
[0da65d5]547        data->ic = ic;
[7bf0f5f0]548        data->handle = g_strdup( handle );
[fa295e36]549        query_add( ic->irc, ic, s, imcb_ask_add_cb_yes, imcb_ask_add_cb_no, data );
[81e04e1]550#endif
[b7d3cc34]551}
552
[52744f8]553void imcb_buddy_typing( struct im_connection *ic, char *handle, uint32_t flags )
[b7d3cc34]554{
[81e04e1]555#if 0
[b7d3cc34]556        user_t *u;
557       
[81e04e1]558        if( !set_getbool( &ic->bee->set, "typing_notice" ) )
[b7d3cc34]559                return;
560       
[9624fdf]561        if( ( u = user_findhandle( ic, handle ) ) )
562        {
563                char buf[256];
564               
565                g_snprintf( buf, 256, "\1TYPING %d\1", ( flags >> 8 ) & 3 );
566                irc_privmsg( ic->irc, u, "PRIVMSG", ic->irc->nick, NULL, buf );
[e7f46c5]567        }
[81e04e1]568#endif
569}
570
571struct bee_user *imcb_buddy_by_handle( struct im_connection *ic, const char *handle )
572{
573        return bee_user_by_handle( ic->bee, ic, handle );
[b7d3cc34]574}
575
[94acdd0]576struct groupchat *imcb_chat_new( struct im_connection *ic, const char *handle )
[83ba3e5]577{
[81e04e1]578#if 0
[83ba3e5]579        struct groupchat *c;
580       
581        /* This one just creates the conversation structure, user won't see anything yet */
582       
583        if( ic->groupchats )
584        {
585                for( c = ic->groupchats; c->next; c = c->next );
586                c = c->next = g_new0( struct groupchat, 1 );
587        }
588        else
589                ic->groupchats = c = g_new0( struct groupchat, 1 );
590       
591        c->ic = ic;
592        c->title = g_strdup( handle );
593        c->channel = g_strdup_printf( "&chat_%03d", ic->irc->c_id++ );
594        c->topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title );
595       
[81e04e1]596        if( set_getbool( &ic->bee->set, "debug" ) )
[83ba3e5]597                imcb_log( ic, "Creating new conversation: (id=%p,handle=%s)", c, handle );
598       
599        return c;
[81e04e1]600#endif
[fb117aee]601        return NULL;
[83ba3e5]602}
603
[e35d1a1]604void imcb_chat_free( struct groupchat *c )
[b7d3cc34]605{
[81e04e1]606#if 0
[0da65d5]607        struct im_connection *ic = c->ic;
[e35d1a1]608        struct groupchat *l;
[b7d3cc34]609        GList *ir;
610       
[81e04e1]611        if( set_getbool( &ic->bee->set, "debug" ) )
[56f260a]612                imcb_log( ic, "You were removed from conversation %p", c );
[b7d3cc34]613       
614        if( c )
615        {
616                if( c->joined )
617                {
618                        user_t *u, *r;
619                       
[0da65d5]620                        r = user_find( ic->irc, ic->irc->mynick );
621                        irc_privmsg( ic->irc, r, "PRIVMSG", c->channel, "", "Cleaning up channel, bye!" );
[b7d3cc34]622                       
[0da65d5]623                        u = user_find( ic->irc, ic->irc->nick );
624                        irc_kick( ic->irc, u, c->channel, r );
625                        /* irc_part( ic->irc, u, c->channel ); */
[b7d3cc34]626                }
627               
[e35d1a1]628                /* Find the previous chat in the linked list. */
629                for( l = ic->groupchats; l && l->next != c; l = l->next );
630               
[b7d3cc34]631                if( l )
632                        l->next = c->next;
633                else
[e35d1a1]634                        ic->groupchats = c->next;
[b7d3cc34]635               
636                for( ir = c->in_room; ir; ir = ir->next )
637                        g_free( ir->data );
638                g_list_free( c->in_room );
639                g_free( c->channel );
640                g_free( c->title );
[83ba3e5]641                g_free( c->topic );
[b7d3cc34]642                g_free( c );
643        }
[81e04e1]644#endif
[b7d3cc34]645}
646
[c6ca3ee]647void imcb_chat_msg( struct groupchat *c, const char *who, char *msg, uint32_t flags, time_t sent_at )
[b7d3cc34]648{
[81e04e1]649#if 0
[0da65d5]650        struct im_connection *ic = c->ic;
[d444c09]651        char *wrapped;
[b7d3cc34]652        user_t *u;
653       
654        /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */
[c2fb3809]655        if( g_strcasecmp( who, ic->acc->user ) == 0 )
[b7d3cc34]656                return;
657       
[0da65d5]658        u = user_findhandle( ic, who );
[b7d3cc34]659       
[81e04e1]660        if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) ||
661            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) )
[b7d3cc34]662                strip_html( msg );
663       
[d444c09]664        wrapped = word_wrap( msg, 425 );
[b7d3cc34]665        if( c && u )
[d444c09]666        {
667                irc_privmsg( ic->irc, u, "PRIVMSG", c->channel, "", wrapped );
668        }
[b7d3cc34]669        else
[d444c09]670        {
[56f260a]671                imcb_log( ic, "Message from/to conversation %s@%p (unknown conv/user): %s", who, c, wrapped );
[d444c09]672        }
673        g_free( wrapped );
[81e04e1]674#endif
[b7d3cc34]675}
676
[31e5846]677void imcb_chat_log( struct groupchat *c, char *format, ... )
678{
[81e04e1]679#if 0
[31e5846]680        irc_t *irc = c->ic->irc;
681        va_list params;
682        char *text;
683        user_t *u;
684       
685        va_start( params, format );
686        text = g_strdup_vprintf( format, params );
687        va_end( params );
688       
689        u = user_find( irc, irc->mynick );
690       
691        irc_privmsg( irc, u, "PRIVMSG", c->channel, "System message: ", text );
692       
693        g_free( text );
[81e04e1]694#endif
[31e5846]695}
696
[ef5c185]697void imcb_chat_topic( struct groupchat *c, char *who, char *topic, time_t set_at )
[50e1776]698{
[81e04e1]699#if 0
[50e1776]700        struct im_connection *ic = c->ic;
701        user_t *u = NULL;
702       
703        if( who == NULL)
[ef5c185]704                u = user_find( ic->irc, ic->irc->mynick );
[50e1776]705        else if( g_strcasecmp( who, ic->acc->user ) == 0 )
[ef5c185]706                u = user_find( ic->irc, ic->irc->nick );
[50e1776]707        else
708                u = user_findhandle( ic, who );
709       
[81e04e1]710        if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) ||
711            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) )
[50e1776]712                strip_html( topic );
713       
714        g_free( c->topic );
715        c->topic = g_strdup( topic );
716       
717        if( c->joined && u )
718                irc_write( ic->irc, ":%s!%s@%s TOPIC %s :%s", u->nick, u->user, u->host, c->channel, topic );
[81e04e1]719#endif
[50e1776]720}
721
[c6ca3ee]722void imcb_chat_add_buddy( struct groupchat *b, const char *handle )
[b7d3cc34]723{
[81e04e1]724#if 0
[0da65d5]725        user_t *u = user_findhandle( b->ic, handle );
[b7d3cc34]726        int me = 0;
727       
[81e04e1]728        if( set_getbool( &b->ic->bee->set, "debug" ) )
[56f260a]729                imcb_log( b->ic, "User %s added to conversation %p", handle, b );
[b7d3cc34]730       
731        /* It might be yourself! */
[c2fb3809]732        if( b->ic->acc->prpl->handle_cmp( handle, b->ic->acc->user ) == 0 )
[b7d3cc34]733        {
[0da65d5]734                u = user_find( b->ic->irc, b->ic->irc->nick );
[b7d3cc34]735                if( !b->joined )
[0da65d5]736                        irc_join( b->ic->irc, u, b->channel );
[b7d3cc34]737                b->joined = me = 1;
738        }
739       
740        /* Most protocols allow people to join, even when they're not in
741           your contact list. Try to handle that here */
742        if( !u )
743        {
[f0cb961]744                imcb_add_buddy( b->ic, handle, NULL );
[0da65d5]745                u = user_findhandle( b->ic, handle );
[b7d3cc34]746        }
747       
748        /* Add the handle to the room userlist, if it's not 'me' */
749        if( !me )
750        {
751                if( b->joined )
[0da65d5]752                        irc_join( b->ic->irc, u, b->channel );
[b7d3cc34]753                b->in_room = g_list_append( b->in_room, g_strdup( handle ) );
754        }
[81e04e1]755#endif
[b7d3cc34]756}
757
[2d317bb]758/* This function is one BIG hack... :-( EREWRITE */
[c6ca3ee]759void imcb_chat_remove_buddy( struct groupchat *b, const char *handle, const char *reason )
[b7d3cc34]760{
[81e04e1]761#if 0
[b7d3cc34]762        user_t *u;
763        int me = 0;
764       
[81e04e1]765        if( set_getbool( &b->ic->bee->set, "debug" ) )
[56f260a]766                imcb_log( b->ic, "User %s removed from conversation %p (%s)", handle, b, reason ? reason : "" );
[b7d3cc34]767       
768        /* It might be yourself! */
[c2fb3809]769        if( g_strcasecmp( handle, b->ic->acc->user ) == 0 )
[b7d3cc34]770        {
[2d317bb]771                if( b->joined == 0 )
772                        return;
773               
[0da65d5]774                u = user_find( b->ic->irc, b->ic->irc->nick );
[b7d3cc34]775                b->joined = 0;
776                me = 1;
777        }
778        else
779        {
[0da65d5]780                u = user_findhandle( b->ic, handle );
[b7d3cc34]781        }
782       
[2d317bb]783        if( me || ( remove_chat_buddy_silent( b, handle ) && b->joined && u ) )
784                irc_part( b->ic->irc, u, b->channel );
[81e04e1]785#endif
[b7d3cc34]786}
787
[fb117aee]788#if 0
[764b163d]789static int remove_chat_buddy_silent( struct groupchat *b, const char *handle )
[b7d3cc34]790{
791        GList *i;
792       
793        /* Find the handle in the room userlist and shoot it */
794        i = b->in_room;
795        while( i )
796        {
797                if( g_strcasecmp( handle, i->data ) == 0 )
798                {
799                        g_free( i->data );
800                        b->in_room = g_list_remove( b->in_room, i->data );
801                        return( 1 );
802                }
803               
804                i = i->next;
805        }
806       
[81e04e1]807        return 0;
[b7d3cc34]808}
[fb117aee]809#endif
[b7d3cc34]810
811
812/* Misc. BitlBee stuff which shouldn't really be here */
[81e04e1]813#if 0
[5c9512f]814char *set_eval_away_devoice( set_t *set, char *value )
[b7d3cc34]815{
[5c9512f]816        irc_t *irc = set->data;
[b7d3cc34]817        int st;
818       
[7125cb3]819        if( !is_bool( value ) )
820                return SET_INVALID;
[b7d3cc34]821       
[7125cb3]822        st = bool2int( value );
[b7d3cc34]823       
824        /* Horror.... */
825       
[10a96f4]826        if( st != set_getbool( &irc->b->set, "away_devoice" ) )
[b7d3cc34]827        {
828                char list[80] = "";
829                user_t *u = irc->users;
830                int i = 0, count = 0;
831                char pm;
832                char v[80];
833               
834                if( st )
835                        pm = '+';
836                else
837                        pm = '-';
838               
839                while( u )
840                {
[0da65d5]841                        if( u->ic && u->online && !u->away )
[b7d3cc34]842                        {
843                                if( ( strlen( list ) + strlen( u->nick ) ) >= 79 )
844                                {
845                                        for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
[2087159]846                                        irc_write( irc, ":%s MODE %s %c%s%s",
847                                                   irc->myhost,
[b7d3cc34]848                                                   irc->channel, pm, v, list );
849                                       
850                                        *list = 0;
851                                        count = 0;
852                                }
853                               
854                                sprintf( list + strlen( list ), " %s", u->nick );
855                                count ++;
856                        }
857                        u = u->next;
858                }
859               
860                /* $v = 'v' x $i */
861                for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
[2087159]862                irc_write( irc, ":%s MODE %s %c%s%s", irc->myhost,
[b7d3cc34]863                                                            irc->channel, pm, v, list );
864        }
865       
[7125cb3]866        return value;
[b7d3cc34]867}
[81e04e1]868#endif
[226fce1]869
870
871
872/* The plan is to not allow straight calls to prpl functions anymore, but do
873   them all from some wrappers. We'll start to define some down here: */
874
[84b045d]875int imc_chat_msg( struct groupchat *c, char *msg, int flags )
[b7d3cc34]876{
[e27661d]877        char *buf = NULL;
[b7d3cc34]878       
[6bbb939]879        if( ( c->ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
[e27661d]880        {
881                buf = escape_html( msg );
[b7d3cc34]882                msg = buf;
883        }
884       
[f6c963b]885        c->ic->acc->prpl->chat_msg( c, msg, flags );
[e27661d]886        g_free( buf );
887       
[0da65d5]888        return 1;
[b7d3cc34]889}
[226fce1]890
[34fbbf9]891static char *imc_away_state_find( GList *gcm, char *away, char **message );
[226fce1]892
[58adb7e]893int imc_away_send_update( struct im_connection *ic )
[226fce1]894{
[3e1ef92c]895        char *away, *msg = NULL;
[226fce1]896       
[58adb7e]897        away = set_getstr( &ic->acc->set, "away" ) ?
[81e04e1]898             : set_getstr( &ic->bee->set, "away" );
[34fbbf9]899        if( away && *away )
[226fce1]900        {
[34fbbf9]901                GList *m = ic->acc->prpl->away_states( ic );
[58adb7e]902                msg = ic->acc->flags & ACC_FLAG_AWAY_MESSAGE ? away : NULL;
903                away = imc_away_state_find( m, away, &msg ) ? : m->data;
904        }
905        else if( ic->acc->flags & ACC_FLAG_STATUS_MESSAGE )
906        {
907                away = NULL;
908                msg = set_getstr( &ic->acc->set, "status" ) ?
[81e04e1]909                    : set_getstr( &ic->bee->set, "status" );
[226fce1]910        }
911       
[58adb7e]912        ic->acc->prpl->set_away( ic, away, msg );
[226fce1]913       
[34fbbf9]914        return 1;
[226fce1]915}
916
[84b045d]917static char *imc_away_alias_list[8][5] =
[226fce1]918{
919        { "Away from computer", "Away", "Extended away", NULL },
920        { "NA", "N/A", "Not available", NULL },
921        { "Busy", "Do not disturb", "DND", "Occupied", NULL },
922        { "Be right back", "BRB", NULL },
923        { "On the phone", "Phone", "On phone", NULL },
924        { "Out to lunch", "Lunch", "Food", NULL },
925        { "Invisible", "Hidden" },
926        { NULL }
927};
928
[34fbbf9]929static char *imc_away_state_find( GList *gcm, char *away, char **message )
[226fce1]930{
931        GList *m;
932        int i, j;
933       
[34fbbf9]934        for( m = gcm; m; m = m->next )
935                if( g_strncasecmp( m->data, away, strlen( m->data ) ) == 0 )
936                {
937                        /* At least the Yahoo! module works better if message
938                           contains no data unless it adds something to what
939                           we have in state already. */
940                        if( strlen( m->data ) == strlen( away ) )
941                                *message = NULL;
942                       
943                        return m->data;
944                }
945       
[84b045d]946        for( i = 0; *imc_away_alias_list[i]; i ++ )
[226fce1]947        {
[34fbbf9]948                int keep_message;
949               
[84b045d]950                for( j = 0; imc_away_alias_list[i][j]; j ++ )
951                        if( g_strncasecmp( away, imc_away_alias_list[i][j], strlen( imc_away_alias_list[i][j] ) ) == 0 )
[34fbbf9]952                        {
953                                keep_message = strlen( away ) != strlen( imc_away_alias_list[i][j] );
[226fce1]954                                break;
[34fbbf9]955                        }
[226fce1]956               
[84b045d]957                if( !imc_away_alias_list[i][j] )        /* If we reach the end, this row */
[226fce1]958                        continue;                       /* is not what we want. Next!    */
959               
960                /* Now find an entry in this row which exists in gcm */
[84b045d]961                for( j = 0; imc_away_alias_list[i][j]; j ++ )
[226fce1]962                {
[34fbbf9]963                        for( m = gcm; m; m = m->next )
[84b045d]964                                if( g_strcasecmp( imc_away_alias_list[i][j], m->data ) == 0 )
[34fbbf9]965                                {
966                                        if( !keep_message )
967                                                *message = NULL;
968                                       
969                                        return imc_away_alias_list[i][j];
970                                }
[226fce1]971                }
[34fbbf9]972               
973                /* No need to look further, apparently this state doesn't
974                   have any good alias for this protocol. */
975                break;
[226fce1]976        }
977       
[34fbbf9]978        return NULL;
[226fce1]979}
[da3b536]980
[84b045d]981void imc_add_allow( struct im_connection *ic, char *handle )
[da3b536]982{
[0da65d5]983        if( g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL )
[da3b536]984        {
[0da65d5]985                ic->permit = g_slist_prepend( ic->permit, g_strdup( handle ) );
[da3b536]986        }
987       
[0da65d5]988        ic->acc->prpl->add_permit( ic, handle );
[da3b536]989}
990
[84b045d]991void imc_rem_allow( struct im_connection *ic, char *handle )
[da3b536]992{
993        GSList *l;
994       
[0da65d5]995        if( ( l = g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) )
[da3b536]996        {
997                g_free( l->data );
[0da65d5]998                ic->permit = g_slist_delete_link( ic->permit, l );
[da3b536]999        }
1000       
[0da65d5]1001        ic->acc->prpl->rem_permit( ic, handle );
[da3b536]1002}
1003
[84b045d]1004void imc_add_block( struct im_connection *ic, char *handle )
[da3b536]1005{
[0da65d5]1006        if( g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL )
[da3b536]1007        {
[0da65d5]1008                ic->deny = g_slist_prepend( ic->deny, g_strdup( handle ) );
[da3b536]1009        }
1010       
[0da65d5]1011        ic->acc->prpl->add_deny( ic, handle );
[da3b536]1012}
1013
[84b045d]1014void imc_rem_block( struct im_connection *ic, char *handle )
[da3b536]1015{
1016        GSList *l;
1017       
[0da65d5]1018        if( ( l = g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) )
[da3b536]1019        {
1020                g_free( l->data );
[0da65d5]1021                ic->deny = g_slist_delete_link( ic->deny, l );
[da3b536]1022        }
1023       
[0da65d5]1024        ic->acc->prpl->rem_deny( ic, handle );
[da3b536]1025}
[85023c6]1026
1027void imcb_clean_handle( struct im_connection *ic, char *handle )
1028{
1029        /* Accepts a handle and does whatever is necessary to make it
1030           BitlBee-friendly. Currently this means removing everything
1031           outside 33-127 (ASCII printable excl spaces), @ (only one
1032           is allowed) and ! and : */
1033        char out[strlen(handle)+1];
1034        int s, d;
1035       
1036        s = d = 0;
1037        while( handle[s] )
1038        {
1039                if( handle[s] > ' ' && handle[s] != '!' && handle[s] != ':' &&
1040                    ( handle[s] & 0x80 ) == 0 )
1041                {
1042                        if( handle[s] == '@' )
1043                        {
1044                                /* See if we got an @ already? */
1045                                out[d] = 0;
1046                                if( strchr( out, '@' ) )
1047                                        continue;
1048                        }
1049                       
1050                        out[d++] = handle[s];
1051                }
1052                s ++;
1053        }
1054        out[d] = handle[s];
1055       
1056        strcpy( handle, out );
1057}
Note: See TracBrowser for help on using the repository browser.