source: protocols/nogaim.c @ d323394c

Last change on this file since d323394c was d323394c, checked in by Wilmer van der Gaast <wilmer@…>, at 2007-04-20T04:49:30Z

New code to use nicknames stored in ICQ contact lists.

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