source: protocols/nogaim.c @ c99af3a

Last change on this file since c99af3a was c99af3a, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-04-13T07:30:11Z

Removed account_offline(), we have signoff() already.

  • Property mode set to 100644
File size: 24.4 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2004 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 * Copyright 2002-2006 Wilmer van der Gaast <wilmer@gaast.net> and others
17 */
18
19/*
20  This program is free software; you can redistribute it and/or modify
21  it under the terms of the GNU General Public License as published by
22  the Free Software Foundation; either version 2 of the License, or
23  (at your option) any later version.
24
25  This program is distributed in the hope that it will be useful,
26  but WITHOUT ANY WARRANTY; without even the implied warranty of
27  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  GNU General Public License for more details.
29
30  You should have received a copy of the GNU General Public License with
31  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
32  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
33  Suite 330, Boston, MA  02111-1307  USA
34*/
35
36#define BITLBEE_CORE
37#include "nogaim.h"
38#include <ctype.h>
39
40static char *proto_away_alias[8][5] =
41{
42        { "Away from computer", "Away", "Extended away", NULL },
43        { "NA", "N/A", "Not available", NULL },
44        { "Busy", "Do not disturb", "DND", "Occupied", NULL },
45        { "Be right back", "BRB", NULL },
46        { "On the phone", "Phone", "On phone", NULL },
47        { "Out to lunch", "Lunch", "Food", NULL },
48        { "Invisible", "Hidden" },
49        { NULL }
50};
51static char *proto_away_alias_find( GList *gcm, char *away );
52
53static int remove_chat_buddy_silent( struct conversation *b, char *handle );
54
55GSList *connections;
56
57#ifdef WITH_PLUGINS
58gboolean load_plugin(char *path)
59{
60        void (*init_function) (void);
61       
62        GModule *mod = g_module_open(path, G_MODULE_BIND_LAZY);
63
64        if(!mod) {
65                log_message(LOGLVL_ERROR, "Can't find `%s', not loading", path);
66                return FALSE;
67        }
68
69        if(!g_module_symbol(mod,"init_plugin",(gpointer *) &init_function)) {
70                log_message(LOGLVL_WARNING, "Can't find function `init_plugin' in `%s'\n", path);
71                return FALSE;
72        }
73
74        init_function();
75
76        return TRUE;
77}
78
79void load_plugins(void)
80{
81        GDir *dir;
82        GError *error = NULL;
83
84        dir = g_dir_open(global.conf->plugindir, 0, &error);
85
86        if (dir) {
87                const gchar *entry;
88                char *path;
89
90                while ((entry = g_dir_read_name(dir))) {
91                        path = g_build_filename(global.conf->plugindir, entry, NULL);
92                        if(!path) {
93                                log_message(LOGLVL_WARNING, "Can't build path for %s\n", entry);
94                                continue;
95                        }
96
97                        load_plugin(path);
98
99                        g_free(path);
100                }
101
102                g_dir_close(dir);
103        }
104}
105#endif
106
107/* nogaim.c */
108
109GList *protocols = NULL;
110 
111void register_protocol (struct prpl *p)
112{
113        protocols = g_list_append(protocols, p);
114}
115
116 
117struct prpl *find_protocol(const char *name)
118{
119        GList *gl;
120        for (gl = protocols; gl; gl = gl->next) 
121        {
122                struct prpl *proto = gl->data;
123                if(!g_strcasecmp(proto->name, name)) 
124                        return proto;
125        }
126        return NULL;
127}
128
129/* nogaim.c */
130void nogaim_init()
131{
132        extern void msn_init();
133        extern void oscar_init();
134        extern void byahoo_init();
135        extern void jabber_init();
136
137#ifdef WITH_MSN
138        msn_init();
139#endif
140
141#ifdef WITH_OSCAR
142        oscar_init();
143#endif
144       
145#ifdef WITH_YAHOO
146        byahoo_init();
147#endif
148       
149#ifdef WITH_JABBER
150        jabber_init();
151#endif
152
153#ifdef WITH_PLUGINS
154        load_plugins();
155#endif
156}
157
158GSList *get_connections() { return connections; }
159
160int proto_away( struct gaim_connection *gc, char *away )
161{
162        GList *m, *ms;
163        char *s;
164       
165        if( !away ) away = "";
166        ms = m = gc->prpl->away_states( gc );
167       
168        while( m )
169        {
170                if( *away )
171                {
172                        if( g_strncasecmp( m->data, away, strlen( m->data ) ) == 0 )
173                                break;
174                }
175                else
176                {
177                        if( g_strcasecmp( m->data, "Available" ) == 0 )
178                                break;
179                        if( g_strcasecmp( m->data, "Online" ) == 0 )
180                                break;
181                }
182                m = m->next;
183        }
184       
185        if( m )
186        {
187                gc->prpl->set_away( gc, m->data, *away ? away : NULL );
188        }
189        else
190        {
191                s = proto_away_alias_find( ms, away );
192                if( s )
193                {
194                        gc->prpl->set_away( gc, s, away );
195                        if( set_getint( gc->irc, "debug" ) )
196                                serv_got_crap( gc, "Setting away state to %s", s );
197                }
198                else
199                        gc->prpl->set_away( gc, GAIM_AWAY_CUSTOM, away );
200        }
201       
202        g_list_free( ms );
203       
204        return( 1 );
205}
206
207static char *proto_away_alias_find( GList *gcm, char *away )
208{
209        GList *m;
210        int i, j;
211       
212        for( i = 0; *proto_away_alias[i]; i ++ )
213        {
214                for( j = 0; proto_away_alias[i][j]; j ++ )
215                        if( g_strncasecmp( away, proto_away_alias[i][j], strlen( proto_away_alias[i][j] ) ) == 0 )
216                                break;
217               
218                if( !proto_away_alias[i][j] )   /* If we reach the end, this row */
219                        continue;               /* is not what we want. Next!    */
220               
221                /* Now find an entry in this row which exists in gcm */
222                for( j = 0; proto_away_alias[i][j]; j ++ )
223                {
224                        m = gcm;
225                        while( m )
226                        {
227                                if( g_strcasecmp( proto_away_alias[i][j], m->data ) == 0 )
228                                        return( proto_away_alias[i][j] );
229                                m = m->next;
230                        }
231                }
232        }
233       
234        return( NULL );
235}
236
237/* multi.c */
238
239struct gaim_connection *new_gaim_conn( struct aim_user *user )
240{
241        struct gaim_connection *gc;
242        account_t *a;
243       
244        gc = g_new0( struct gaim_connection, 1 );
245       
246        gc->prpl = user->prpl;
247        g_snprintf( gc->username, sizeof( gc->username ), "%s", user->username );
248        g_snprintf( gc->password, sizeof( gc->password ), "%s", user->password );
249        /* [MD] BUGFIX: don't set gc->irc to the global IRC, but use the one from the struct aim_user.
250         * This fixes daemon mode breakage where IRC doesn't point to the currently active connection.
251         */
252        gc->irc=user->irc;
253       
254        connections = g_slist_append( connections, gc );
255       
256        user->gc = gc;
257        gc->user = user;
258       
259        // Find the account_t so we can set its gc pointer
260        for( a = gc->irc->accounts; a; a = a->next )
261                if( ( struct aim_user * ) a->gc == user )
262                {
263                        a->gc = gc;
264                        break;
265                }
266       
267        return( gc );
268}
269
270void destroy_gaim_conn( struct gaim_connection *gc )
271{
272        account_t *a;
273       
274        /* Destroy the pointer to this connection from the account list */
275        for( a = gc->irc->accounts; a; a = a->next )
276                if( a->gc == gc )
277                {
278                        a->gc = NULL;
279                        break;
280                }
281       
282        connections = g_slist_remove( connections, gc );
283        g_free( gc->user );
284        g_free( gc );
285}
286
287void set_login_progress( struct gaim_connection *gc, int step, char *msg )
288{
289        serv_got_crap( gc, "Logging in: %s", msg );
290}
291
292/* Errors *while* logging in */
293void hide_login_progress( struct gaim_connection *gc, char *msg )
294{
295        serv_got_crap( gc, "Login error: %s", msg );
296}
297
298/* Errors *after* logging in */
299void hide_login_progress_error( struct gaim_connection *gc, char *msg )
300{
301        serv_got_crap( gc, "Logged out: %s", msg );
302}
303
304void serv_got_crap( struct gaim_connection *gc, char *format, ... )
305{
306        va_list params;
307        char *text;
308        account_t *a;
309       
310        va_start( params, format );
311        text = g_strdup_vprintf( format, params );
312        va_end( params );
313
314        if( ( g_strcasecmp( set_getstr( gc->irc, "strip_html" ), "always" ) == 0 ) ||
315            ( ( gc->flags & OPT_CONN_HTML ) && set_getint( gc->irc, "strip_html" ) ) )
316                strip_html( text );
317       
318        /* Try to find a different connection on the same protocol. */
319        for( a = gc->irc->accounts; a; a = a->next )
320                if( a->prpl == gc->prpl && a->gc != gc )
321                        break;
322       
323        /* If we found one, include the screenname in the message. */
324        if( a )
325                irc_usermsg( gc->irc, "%s(%s) - %s", gc->prpl->name, gc->username, text );
326        else
327                irc_usermsg( gc->irc, "%s - %s", gc->prpl->name, text );
328       
329        g_free( text );
330}
331
332static gboolean send_keepalive( gpointer d )
333{
334        struct gaim_connection *gc = d;
335       
336        if( gc->prpl && gc->prpl->keepalive )
337                gc->prpl->keepalive( gc );
338       
339        return TRUE;
340}
341
342void account_online( struct gaim_connection *gc )
343{
344        user_t *u;
345       
346        /* MSN servers sometimes redirect you to a different server and do
347           the whole login sequence again, so these "late" calls to this
348           function should be handled correctly. (IOW, ignored) */
349        if( gc->flags & OPT_LOGGED_IN )
350                return;
351       
352        u = user_find( gc->irc, gc->irc->nick );
353       
354        serv_got_crap( gc, "Logged in" );
355       
356        gc->keepalive = g_timeout_add( 60000, send_keepalive, gc );
357        gc->flags |= OPT_LOGGED_IN;
358       
359        /* Also necessary when we're not away, at least for some of the
360           protocols. */
361        proto_away( gc, u->away );
362       
363        if( strcmp( gc->prpl->name, "ICQ" ) == 0 )
364        {
365                for( u = gc->irc->users; u; u = u->next )
366                        if( u->gc == gc )
367                                break;
368               
369                if( u == NULL )
370                        serv_got_crap( gc, "\x02""***\x02"" BitlBee now supports ICQ server-side contact lists. "
371                                              "See \x02""help import_buddies\x02"" for more information." );
372        }
373}
374
375gboolean auto_reconnect( gpointer data )
376{
377        account_t *a = data;
378       
379        a->reconnect = 0;
380        account_on( a->irc, a );
381       
382        return( FALSE );        /* Only have to run the timeout once */
383}
384
385void cancel_auto_reconnect( account_t *a )
386{
387        while( g_source_remove_by_user_data( (gpointer) a ) );
388        a->reconnect = 0;
389}
390
391void signoff( struct gaim_connection *gc )
392{
393        irc_t *irc = gc->irc;
394        user_t *t, *u = irc->users;
395        account_t *a;
396       
397        serv_got_crap( gc, "Signing off.." );
398
399        gaim_input_remove( gc->keepalive );
400        gc->keepalive = 0;
401        gc->prpl->close( gc );
402        gaim_input_remove( gc->inpa );
403       
404        while( u )
405        {
406                if( u->gc == gc )
407                {
408                        t = u->next;
409                        user_del( irc, u->nick );
410                        u = t;
411                }
412                else
413                        u = u->next;
414        }
415       
416        query_del_by_gc( gc->irc, gc );
417       
418        for( a = irc->accounts; a; a = a->next )
419                if( a->gc == gc )
420                        break;
421       
422        if( !a )
423        {
424                /* Uhm... This is very sick. */
425        }
426        else if( !gc->wants_to_die && set_getint( irc, "auto_reconnect" ) )
427        {
428                int delay = set_getint( irc, "auto_reconnect_delay" );
429                serv_got_crap( gc, "Reconnecting in %d seconds..", delay );
430               
431                a->reconnect = 1;
432                g_timeout_add( delay * 1000, auto_reconnect, a );
433        }
434       
435        destroy_gaim_conn( gc );
436}
437
438
439/* dialogs.c */
440
441void do_error_dialog( struct gaim_connection *gc, char *msg, char *title )
442{
443        if( msg && title )
444                serv_got_crap( gc, "Error: %s: %s", title, msg );
445        else if( msg )
446                serv_got_crap( gc, "Error: %s", msg );
447        else if( title )
448                serv_got_crap( gc, "Error: %s", title );
449        else
450                serv_got_crap( gc, "Error" );
451}
452
453void do_ask_dialog( struct gaim_connection *gc, char *msg, void *data, void *doit, void *dont )
454{
455        query_add( gc->irc, gc, msg, doit, dont, data );
456}
457
458
459/* list.c */
460
461void add_buddy( struct gaim_connection *gc, char *group, char *handle, char *realname )
462{
463        user_t *u;
464        char nick[MAX_NICK_LENGTH+1];
465        char *s;
466        irc_t *irc = gc->irc;
467       
468        if( set_getint( irc, "debug" ) && 0 ) /* This message is too useless */
469                serv_got_crap( gc, "Receiving user add from handle: %s", handle );
470       
471        if( user_findhandle( gc, handle ) )
472        {
473                if( set_getint( irc, "debug" ) )
474                        serv_got_crap( gc, "User already exists, ignoring add request: %s", handle );
475               
476                return;
477               
478                /* Buddy seems to exist already. Let's ignore this request then... */
479        }
480       
481        memset( nick, 0, MAX_NICK_LENGTH + 1 );
482        strcpy( nick, nick_get( gc->irc, handle, gc->prpl, realname ) );
483       
484        u = user_add( gc->irc, nick );
485       
486        if( !realname || !*realname ) realname = nick;
487        u->realname = g_strdup( realname );
488       
489        if( ( s = strchr( handle, '@' ) ) )
490        {
491                u->host = g_strdup( s + 1 );
492                u->user = g_strndup( handle, s - handle );
493        }
494        else if( gc->user->proto_opt[0] && *gc->user->proto_opt[0] )
495        {
496                u->host = g_strdup( gc->user->proto_opt[0] );
497                u->user = g_strdup( handle );
498               
499                /* s/ /_/ ... important for AOL screennames */
500                for( s = u->user; *s; s ++ )
501                        if( *s == ' ' )
502                                *s = '_';
503        }
504        else
505        {
506                u->host = g_strdup( gc->user->prpl->name );
507                u->user = g_strdup( handle );
508        }
509       
510        u->gc = gc;
511        u->handle = g_strdup( handle );
512        if( group ) u->group = g_strdup( group );
513        u->send_handler = buddy_send_handler;
514        u->last_typing_notice = 0;
515}
516
517struct buddy *find_buddy( struct gaim_connection *gc, char *handle )
518{
519        static struct buddy b[1];
520        user_t *u;
521       
522        u = user_findhandle( gc, handle );
523       
524        if( !u )
525                return( NULL );
526
527        memset( b, 0, sizeof( b ) );
528        strncpy( b->name, handle, 80 );
529        strncpy( b->show, u->realname, BUDDY_ALIAS_MAXLEN );
530        b->present = u->online;
531        b->gc = u->gc;
532       
533        return( b );
534}
535
536void signoff_blocked( struct gaim_connection *gc )
537{
538        return; /* Make all blocked users look invisible (TODO?) */
539}
540
541
542void serv_buddy_rename( struct gaim_connection *gc, char *handle, char *realname )
543{
544        user_t *u = user_findhandle( gc, handle );
545       
546        if( !u ) return;
547       
548        if( g_strcasecmp( u->realname, realname ) != 0 )
549        {
550                if( u->realname != u->nick ) g_free( u->realname );
551               
552                u->realname = g_strdup( realname );
553               
554                if( ( gc->flags & OPT_LOGGED_IN ) && set_getint( gc->irc, "display_namechanges" ) )
555                        serv_got_crap( gc, "User `%s' changed name to `%s'", u->nick, u->realname );
556        }
557}
558
559
560/* prpl.c */
561
562struct show_got_added_data
563{
564        struct gaim_connection *gc;
565        char *handle;
566};
567
568void show_got_added_no( gpointer w, struct show_got_added_data *data )
569{
570        g_free( data->handle );
571        g_free( data );
572}
573
574void show_got_added_yes( gpointer w, struct show_got_added_data *data )
575{
576        data->gc->prpl->add_buddy( data->gc, data->handle );
577        add_buddy( data->gc, NULL, data->handle, data->handle );
578       
579        return show_got_added_no( w, data );
580}
581
582void show_got_added( struct gaim_connection *gc, char *handle, const char *realname )
583{
584        struct show_got_added_data *data = g_new0( struct show_got_added_data, 1 );
585        char *s;
586       
587        /* TODO: Make a setting for this! */
588        if( user_findhandle( gc, handle ) != NULL )
589                return;
590       
591        s = g_strdup_printf( "The user %s is not in your buddy list yet. Do you want to add him/her now?", handle );
592       
593        data->gc = gc;
594        data->handle = g_strdup( handle );
595        query_add( gc->irc, gc, s, show_got_added_yes, show_got_added_no, data );
596}
597
598
599/* server.c */                   
600
601void serv_got_update( struct gaim_connection *gc, char *handle, int loggedin, int evil, time_t signon, time_t idle, int type, guint caps )
602{
603        user_t *u;
604        int oa, oo;
605       
606        u = user_findhandle( gc, handle );
607       
608        if( !u )
609        {
610                if( g_strcasecmp( set_getstr( gc->irc, "handle_unknown" ), "add" ) == 0 )
611                {
612                        add_buddy( gc, NULL, handle, NULL );
613                        u = user_findhandle( gc, handle );
614                }
615                else
616                {
617                        if( set_getint( gc->irc, "debug" ) || g_strcasecmp( set_getstr( gc->irc, "handle_unknown" ), "ignore" ) != 0 )
618                        {
619                                serv_got_crap( gc, "serv_got_update() for handle %s:", handle );
620                                serv_got_crap( gc, "loggedin = %d, type = %d", loggedin, type );
621                        }
622                       
623                        return;
624                }
625                return;
626        }
627       
628        oa = u->away != NULL;
629        oo = u->online;
630       
631        if( u->away )
632        {
633                g_free( u->away );
634                u->away = NULL;
635        }
636       
637        if( loggedin && !u->online )
638        {
639                irc_spawn( gc->irc, u );
640                u->online = 1;
641        }
642        else if( !loggedin && u->online )
643        {
644                struct conversation *c;
645               
646                irc_kill( gc->irc, u );
647                u->online = 0;
648               
649                /* Remove him/her from the conversations to prevent PART messages after he/she QUIT already */
650                for( c = gc->conversations; c; c = c->next )
651                        remove_chat_buddy_silent( c, handle );
652        }
653       
654        if( ( type & UC_UNAVAILABLE ) && ( !strcmp(gc->prpl->name, "oscar") || !strcmp(gc->prpl->name, "icq")) )
655        {
656                u->away = g_strdup( "Away" );
657        }
658        else if( ( type & UC_UNAVAILABLE ) && ( !strcmp(gc->prpl->name, "jabber") ) )
659        {
660                if( type & UC_DND )
661                        u->away = g_strdup( "Do Not Disturb" );
662                else if( type & UC_XA )
663                        u->away = g_strdup( "Extended Away" );
664                else // if( type & UC_AWAY )
665                        u->away = g_strdup( "Away" );
666        }
667        else if( ( type & UC_UNAVAILABLE ) && gc->prpl->get_status_string )
668        {
669                u->away = g_strdup( gc->prpl->get_status_string( gc, type ) );
670        }
671        else
672                u->away = NULL;
673       
674        /* LISPy... */
675        if( ( set_getint( gc->irc, "away_devoice" ) ) &&                /* Don't do a thing when user doesn't want it */
676            ( u->online ) &&                                            /* Don't touch offline people */
677            ( ( ( u->online != oo ) && !u->away ) ||                    /* Voice joining people */
678              ( ( u->online == oo ) && ( oa == !u->away ) ) ) )         /* (De)voice people changing state */
679        {
680                irc_write( gc->irc, ":%s!%s@%s MODE %s %cv %s", gc->irc->mynick, gc->irc->mynick, gc->irc->myhost,
681                                                                gc->irc->channel, u->away?'-':'+', u->nick );
682        }
683}
684
685void serv_got_im( struct gaim_connection *gc, char *handle, char *msg, guint32 flags, time_t mtime, gint len )
686{
687        irc_t *irc = gc->irc;
688        user_t *u;
689       
690        u = user_findhandle( gc, handle );
691       
692        if( !u )
693        {
694                char *h = set_getstr( irc, "handle_unknown" );
695               
696                if( g_strcasecmp( h, "ignore" ) == 0 )
697                {
698                        if( set_getint( irc, "debug" ) )
699                                serv_got_crap( gc, "Ignoring message from unknown handle %s", handle );
700                       
701                        return;
702                }
703                else if( g_strncasecmp( h, "add", 3 ) == 0 )
704                {
705                        int private = set_getint( irc, "private" );
706                       
707                        if( h[3] )
708                        {
709                                if( g_strcasecmp( h + 3, "_private" ) == 0 )
710                                        private = 1;
711                                else if( g_strcasecmp( h + 3, "_channel" ) == 0 )
712                                        private = 0;
713                        }
714                       
715                        add_buddy( gc, NULL, handle, NULL );
716                        u = user_findhandle( gc, handle );
717                        u->is_private = private;
718                }
719                else
720                {
721                        serv_got_crap( gc, "Message from unknown handle %s:", handle );
722                        u = user_find( irc, irc->mynick );
723                }
724        }
725       
726        if( ( g_strcasecmp( set_getstr( gc->irc, "strip_html" ), "always" ) == 0 ) ||
727            ( ( gc->flags & OPT_CONN_HTML ) && set_getint( gc->irc, "strip_html" ) ) )
728                strip_html( msg );
729
730        while( strlen( msg ) > 425 )
731        {
732                char tmp, *nl;
733               
734                tmp = msg[425];
735                msg[425] = 0;
736               
737                /* If there's a newline/space in this string, split up there,
738                   looks a bit prettier. */
739                if( ( nl = strrchr( msg, '\n' ) ) || ( nl = strrchr( msg, ' ' ) ) )
740                {
741                        msg[425] = tmp;
742                        tmp = *nl;
743                        *nl = 0;
744                }
745               
746                irc_msgfrom( irc, u->nick, msg );
747               
748                /* Move on. */
749                if( nl )
750                {
751                        *nl = tmp;
752                        msg = nl + 1;
753                }
754                else
755                {
756                        msg[425] = tmp;
757                        msg += 425;
758                }
759        }
760        irc_msgfrom( irc, u->nick, msg );
761}
762
763void serv_got_typing( struct gaim_connection *gc, char *handle, int timeout, int type )
764{
765        user_t *u;
766       
767        if( !set_getint( gc->irc, "typing_notice" ) )
768                return;
769       
770        if( ( u = user_findhandle( gc, handle ) ) ) {
771                /* If type is:
772                 * 0: user has stopped typing
773                 * 1: user is actively typing
774                 * 2: user has entered text, but is not actively typing
775                 */
776                if (type == 0 || type == 1 || type == 2) {
777                        char buf[256]; 
778                        g_snprintf(buf, 256, "\1TYPING %d\1", type); 
779                        irc_privmsg( gc->irc, u, "PRIVMSG", gc->irc->nick, NULL, buf );
780                }
781        }
782}
783
784void serv_got_chat_left( struct gaim_connection *gc, int id )
785{
786        struct conversation *c, *l = NULL;
787        GList *ir;
788       
789        if( set_getint( gc->irc, "debug" ) )
790                serv_got_crap( gc, "You were removed from conversation %d", (int) id );
791       
792        for( c = gc->conversations; c && c->id != id; c = (l=c)->next );
793       
794        if( c )
795        {
796                if( c->joined )
797                {
798                        user_t *u, *r;
799                       
800                        r = user_find( gc->irc, gc->irc->mynick );
801                        irc_privmsg( gc->irc, r, "PRIVMSG", c->channel, "", "Cleaning up channel, bye!" );
802                       
803                        u = user_find( gc->irc, gc->irc->nick );
804                        irc_kick( gc->irc, u, c->channel, r );
805                        /* irc_part( gc->irc, u, c->channel ); */
806                }
807               
808                if( l )
809                        l->next = c->next;
810                else
811                        gc->conversations = c->next;
812               
813                for( ir = c->in_room; ir; ir = ir->next )
814                        g_free( ir->data );
815                g_list_free( c->in_room );
816                g_free( c->channel );
817                g_free( c->title );
818                g_free( c );
819        }
820}
821
822void serv_got_chat_in( struct gaim_connection *gc, int id, char *who, int whisper, char *msg, time_t mtime )
823{
824        struct conversation *c;
825        user_t *u;
826       
827        /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */
828        if( g_strcasecmp( who, gc->user->username ) == 0 )
829                return;
830       
831        u = user_findhandle( gc, who );
832        for( c = gc->conversations; c && c->id != id; c = c->next );
833       
834        if( ( g_strcasecmp( set_getstr( gc->irc, "strip_html" ), "always" ) == 0 ) ||
835            ( ( gc->flags & OPT_CONN_HTML ) && set_getint( gc->irc, "strip_html" ) ) )
836                strip_html( msg );
837       
838        if( c && u )
839                irc_privmsg( gc->irc, u, "PRIVMSG", c->channel, "", msg );
840        else
841                serv_got_crap( gc, "Message from/to conversation %s@%d (unknown conv/user): %s", who, id, msg );
842}
843
844struct conversation *serv_got_joined_chat( struct gaim_connection *gc, int id, char *handle )
845{
846        struct conversation *c;
847        char *s;
848       
849        /* This one just creates the conversation structure, user won't see anything yet */
850       
851        if( gc->conversations )
852        {
853                for( c = gc->conversations; c->next; c = c->next );
854                c = c->next = g_new0( struct conversation, 1 );
855        }
856        else
857                gc->conversations = c = g_new0( struct conversation, 1);
858       
859        c->id = id;
860        c->gc = gc;
861        c->title = g_strdup( handle );
862       
863        s = g_new( char, 16 );
864        sprintf( s, "&chat_%03d", gc->irc->c_id++ );
865        c->channel = g_strdup( s );
866        g_free( s );
867       
868        if( set_getint( gc->irc, "debug" ) )
869                serv_got_crap( gc, "Creating new conversation: (id=%d,handle=%s)", id, handle );
870       
871        return( c );
872}
873
874
875/* buddy_chat.c */
876
877void add_chat_buddy( struct conversation *b, char *handle )
878{
879        user_t *u = user_findhandle( b->gc, handle );
880        int me = 0;
881       
882        if( set_getint( b->gc->irc, "debug" ) )
883                serv_got_crap( b->gc, "User %s added to conversation %d", handle, b->id );
884       
885        /* It might be yourself! */
886        if( b->gc->prpl->cmp_buddynames( handle, b->gc->user->username ) == 0 )
887        {
888                u = user_find( b->gc->irc, b->gc->irc->nick );
889                if( !b->joined )
890                        irc_join( b->gc->irc, u, b->channel );
891                b->joined = me = 1;
892        }
893       
894        /* Most protocols allow people to join, even when they're not in
895           your contact list. Try to handle that here */
896        if( !u )
897        {
898                add_buddy( b->gc, NULL, handle, NULL );
899                u = user_findhandle( b->gc, handle );
900        }
901       
902        /* Add the handle to the room userlist, if it's not 'me' */
903        if( !me )
904        {
905                if( b->joined )
906                        irc_join( b->gc->irc, u, b->channel );
907                b->in_room = g_list_append( b->in_room, g_strdup( handle ) );
908        }
909}
910
911void remove_chat_buddy( struct conversation *b, char *handle, char *reason )
912{
913        user_t *u;
914        int me = 0;
915       
916        if( set_getint( b->gc->irc, "debug" ) )
917                serv_got_crap( b->gc, "User %s removed from conversation %d (%s)", handle, b->id, reason ? reason : "" );
918       
919        /* It might be yourself! */
920        if( g_strcasecmp( handle, b->gc->user->username ) == 0 )
921        {
922                u = user_find( b->gc->irc, b->gc->irc->nick );
923                b->joined = 0;
924                me = 1;
925        }
926        else
927        {
928                u = user_findhandle( b->gc, handle );
929        }
930       
931        if( remove_chat_buddy_silent( b, handle ) )
932                if( ( b->joined || me ) && u )
933                        irc_part( b->gc->irc, u, b->channel );
934}
935
936static int remove_chat_buddy_silent( struct conversation *b, char *handle )
937{
938        GList *i;
939       
940        /* Find the handle in the room userlist and shoot it */
941        i = b->in_room;
942        while( i )
943        {
944                if( g_strcasecmp( handle, i->data ) == 0 )
945                {
946                        g_free( i->data );
947                        b->in_room = g_list_remove( b->in_room, i->data );
948                        return( 1 );
949                }
950               
951                i = i->next;
952        }
953       
954        return( 0 );
955}
956
957
958/* Misc. BitlBee stuff which shouldn't really be here */
959
960struct conversation *conv_findchannel( char *channel )
961{
962        struct gaim_connection *gc;
963        struct conversation *c;
964        GSList *l;
965       
966        /* This finds the connection which has a conversation which belongs to this channel */
967        for( l = connections; l; l = l->next )
968        {
969                gc = l->data;
970                for( c = gc->conversations; c && g_strcasecmp( c->channel, channel ) != 0; c = c->next );
971                if( c )
972                        return( c );
973        }
974       
975        return( NULL );
976}
977
978char *set_eval_away_devoice( irc_t *irc, set_t *set, char *value )
979{
980        int st;
981       
982        if( ( g_strcasecmp( value, "true" ) == 0 ) || ( g_strcasecmp( value, "yes" ) == 0 ) || ( g_strcasecmp( value, "on" ) == 0 ) )
983                st = 1;
984        else if( ( g_strcasecmp( value, "false" ) == 0 ) || ( g_strcasecmp( value, "no" ) == 0 ) || ( g_strcasecmp( value, "off" ) == 0 ) )
985                st = 0;
986        else if( sscanf( value, "%d", &st ) != 1 )
987                return( NULL );
988       
989        st = st != 0;
990       
991        /* Horror.... */
992       
993        if( st != set_getint( irc, "away_devoice" ) )
994        {
995                char list[80] = "";
996                user_t *u = irc->users;
997                int i = 0, count = 0;
998                char pm;
999                char v[80];
1000               
1001                if( st )
1002                        pm = '+';
1003                else
1004                        pm = '-';
1005               
1006                while( u )
1007                {
1008                        if( u->gc && u->online && !u->away )
1009                        {
1010                                if( ( strlen( list ) + strlen( u->nick ) ) >= 79 )
1011                                {
1012                                        for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
1013                                        irc_write( irc, ":%s!%s@%s MODE %s %c%s%s",
1014                                                   irc->mynick, irc->mynick, irc->myhost,
1015                                                   irc->channel, pm, v, list );
1016                                       
1017                                        *list = 0;
1018                                        count = 0;
1019                                }
1020                               
1021                                sprintf( list + strlen( list ), " %s", u->nick );
1022                                count ++;
1023                        }
1024                        u = u->next;
1025                }
1026               
1027                /* $v = 'v' x $i */
1028                for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
1029                irc_write( irc, ":%s!%s@%s MODE %s %c%s%s", irc->mynick, irc->mynick, irc->myhost,
1030                                                            irc->channel, pm, v, list );
1031        }
1032       
1033        return( set_eval_bool( irc, set, value ) );
1034}
1035
1036int serv_send_im( irc_t *irc, user_t *u, char *msg, int flags )
1037{
1038        char *buf = NULL;
1039        int st;
1040       
1041        if( ( u->gc->flags & OPT_CONN_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
1042        {
1043                buf = escape_html( msg );
1044                msg = buf;
1045        }
1046       
1047        st = ((struct gaim_connection *)u->gc)->prpl->send_im( u->gc, u->handle, msg, strlen( msg ), flags );
1048        g_free( buf );
1049       
1050        return st;
1051}
1052
1053int serv_send_chat( irc_t *irc, struct gaim_connection *gc, int id, char *msg )
1054{
1055        char *buf = NULL;
1056        int st;
1057       
1058        if( ( gc->flags & OPT_CONN_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
1059        {
1060                buf = escape_html( msg );
1061                msg = buf;
1062        }
1063       
1064        st = gc->prpl->chat_send( gc, id, msg );
1065        g_free( buf );
1066       
1067        return st;
1068}
Note: See TracBrowser for help on using the repository browser.