source: protocols/nogaim.c @ 81e04e1

Last change on this file since 81e04e1 was 81e04e1, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-04-01T02:32:25Z

nogaim.c is close to doing something useful again without speaking any IRC
itself.

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