source: protocols/nogaim.c @ 85e9644

Last change on this file since 85e9644 was 85e9644, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-06-07T13:36:19Z

Merging Jelmer's storage tree (with LDAP support).

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