source: protocols/nogaim.c @ c53911e

Last change on this file since c53911e was c53911e, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-05-20T11:48:08Z

Fixed a string handling mistake and better stripping of servernames used
in user_t structures.

  • Property mode set to 100644
File size: 24.3 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
364gboolean auto_reconnect( gpointer data )
365{
366        account_t *a = data;
367       
368        a->reconnect = 0;
369        account_on( a->irc, a );
370       
371        return( FALSE );        /* Only have to run the timeout once */
372}
373
374void cancel_auto_reconnect( account_t *a )
375{
376        while( g_source_remove_by_user_data( (gpointer) a ) );
377        a->reconnect = 0;
378}
379
380void signoff( struct gaim_connection *gc )
381{
382        irc_t *irc = gc->irc;
383        user_t *t, *u = irc->users;
384        account_t *a;
385       
386        serv_got_crap( gc, "Signing off.." );
387
388        gaim_input_remove( gc->keepalive );
389        gc->keepalive = 0;
390        gc->prpl->close( gc );
391        gaim_input_remove( gc->inpa );
392       
393        while( u )
394        {
395                if( u->gc == gc )
396                {
397                        t = u->next;
398                        user_del( irc, u->nick );
399                        u = t;
400                }
401                else
402                        u = u->next;
403        }
404       
405        query_del_by_gc( gc->irc, gc );
406       
407        for( a = irc->accounts; a; a = a->next )
408                if( a->gc == gc )
409                        break;
410       
411        if( !a )
412        {
413                /* Uhm... This is very sick. */
414        }
415        else if( !gc->wants_to_die && set_getint( irc, "auto_reconnect" ) )
416        {
417                int delay = set_getint( irc, "auto_reconnect_delay" );
418                serv_got_crap( gc, "Reconnecting in %d seconds..", delay );
419               
420                a->reconnect = 1;
421                g_timeout_add( delay * 1000, auto_reconnect, a );
422        }
423       
424        destroy_gaim_conn( gc );
425}
426
427
428/* dialogs.c */
429
430void do_error_dialog( struct gaim_connection *gc, char *msg, char *title )
431{
432        if( msg && title )
433                serv_got_crap( gc, "Error: %s: %s", title, msg );
434        else if( msg )
435                serv_got_crap( gc, "Error: %s", msg );
436        else if( title )
437                serv_got_crap( gc, "Error: %s", title );
438        else
439                serv_got_crap( gc, "Error" );
440}
441
442void do_ask_dialog( struct gaim_connection *gc, char *msg, void *data, void *doit, void *dont )
443{
444        query_add( gc->irc, gc, msg, doit, dont, data );
445}
446
447
448/* list.c */
449
450void add_buddy( struct gaim_connection *gc, char *group, char *handle, char *realname )
451{
452        user_t *u;
453        char nick[MAX_NICK_LENGTH+1];
454        char *s;
455        irc_t *irc = gc->irc;
456       
457        if( set_getint( irc, "debug" ) && 0 ) /* This message is too useless */
458                serv_got_crap( gc, "Receiving user add from handle: %s", handle );
459       
460        if( user_findhandle( gc, handle ) )
461        {
462                if( set_getint( irc, "debug" ) )
463                        serv_got_crap( gc, "User already exists, ignoring add request: %s", handle );
464               
465                return;
466               
467                /* Buddy seems to exist already. Let's ignore this request then... */
468        }
469       
470        memset( nick, 0, MAX_NICK_LENGTH + 1 );
471        strcpy( nick, nick_get( gc->irc, handle, gc->prpl, realname ) );
472       
473        u = user_add( gc->irc, nick );
474       
475        if( !realname || !*realname ) realname = nick;
476        u->realname = g_strdup( realname );
477       
478        if( ( s = strchr( handle, '@' ) ) )
479        {
480                u->host = g_strdup( s + 1 );
481                u->user = g_strndup( handle, s - handle );
482        }
483        else if( gc->user->proto_opt[0] && *gc->user->proto_opt[0] )
484        {
485                char *colon;
486               
487                if( ( colon = strchr( gc->user->proto_opt[0], ':' ) ) )
488                        u->host = g_strndup( gc->user->proto_opt[0],
489                                             colon - gc->user->proto_opt[0] );
490                else
491                        u->host = g_strdup( gc->user->proto_opt[0] );
492               
493                u->user = g_strdup( handle );
494               
495                /* s/ /_/ ... important for AOL screennames */
496                for( s = u->user; *s; s ++ )
497                        if( *s == ' ' )
498                                *s = '_';
499        }
500        else
501        {
502                u->host = g_strdup( gc->user->prpl->name );
503                u->user = g_strdup( handle );
504        }
505       
506        u->gc = gc;
507        u->handle = g_strdup( handle );
508        if( group ) u->group = g_strdup( group );
509        u->send_handler = buddy_send_handler;
510        u->last_typing_notice = 0;
511}
512
513struct buddy *find_buddy( struct gaim_connection *gc, char *handle )
514{
515        static struct buddy b[1];
516        user_t *u;
517       
518        u = user_findhandle( gc, handle );
519       
520        if( !u )
521                return( NULL );
522
523        memset( b, 0, sizeof( b ) );
524        strncpy( b->name, handle, 80 );
525        strncpy( b->show, u->realname, BUDDY_ALIAS_MAXLEN );
526        b->present = u->online;
527        b->gc = u->gc;
528       
529        return( b );
530}
531
532void signoff_blocked( struct gaim_connection *gc )
533{
534        return; /* Make all blocked users look invisible (TODO?) */
535}
536
537
538void serv_buddy_rename( struct gaim_connection *gc, char *handle, char *realname )
539{
540        user_t *u = user_findhandle( gc, handle );
541       
542        if( !u ) return;
543       
544        if( g_strcasecmp( u->realname, realname ) != 0 )
545        {
546                if( u->realname != u->nick ) g_free( u->realname );
547               
548                u->realname = g_strdup( realname );
549               
550                if( ( gc->flags & OPT_LOGGED_IN ) && set_getint( gc->irc, "display_namechanges" ) )
551                        serv_got_crap( gc, "User `%s' changed name to `%s'", u->nick, u->realname );
552        }
553}
554
555
556/* prpl.c */
557
558struct show_got_added_data
559{
560        struct gaim_connection *gc;
561        char *handle;
562};
563
564void show_got_added_no( gpointer w, struct show_got_added_data *data )
565{
566        g_free( data->handle );
567        g_free( data );
568}
569
570void show_got_added_yes( gpointer w, struct show_got_added_data *data )
571{
572        data->gc->prpl->add_buddy( data->gc, data->handle );
573        add_buddy( data->gc, NULL, data->handle, data->handle );
574       
575        return show_got_added_no( w, data );
576}
577
578void show_got_added( struct gaim_connection *gc, char *handle, const char *realname )
579{
580        struct show_got_added_data *data = g_new0( struct show_got_added_data, 1 );
581        char *s;
582       
583        /* TODO: Make a setting for this! */
584        if( user_findhandle( gc, handle ) != NULL )
585                return;
586       
587        s = g_strdup_printf( "The user %s is not in your buddy list yet. Do you want to add him/her now?", handle );
588       
589        data->gc = gc;
590        data->handle = g_strdup( handle );
591        query_add( gc->irc, gc, s, show_got_added_yes, show_got_added_no, data );
592}
593
594
595/* server.c */                   
596
597void serv_got_update( struct gaim_connection *gc, char *handle, int loggedin, int evil, time_t signon, time_t idle, int type, guint caps )
598{
599        user_t *u;
600        int oa, oo;
601       
602        u = user_findhandle( gc, handle );
603       
604        if( !u )
605        {
606                if( g_strcasecmp( set_getstr( gc->irc, "handle_unknown" ), "add" ) == 0 )
607                {
608                        add_buddy( gc, NULL, handle, NULL );
609                        u = user_findhandle( gc, handle );
610                }
611                else
612                {
613                        if( set_getint( gc->irc, "debug" ) || g_strcasecmp( set_getstr( gc->irc, "handle_unknown" ), "ignore" ) != 0 )
614                        {
615                                serv_got_crap( gc, "serv_got_update() for handle %s:", handle );
616                                serv_got_crap( gc, "loggedin = %d, type = %d", loggedin, type );
617                        }
618                       
619                        return;
620                }
621                /* Why did we have this here....
622                return; */
623        }
624       
625        oa = u->away != NULL;
626        oo = u->online;
627       
628        if( u->away )
629        {
630                g_free( u->away );
631                u->away = NULL;
632        }
633       
634        if( loggedin && !u->online )
635        {
636                irc_spawn( gc->irc, u );
637                u->online = 1;
638        }
639        else if( !loggedin && u->online )
640        {
641                struct conversation *c;
642               
643                irc_kill( gc->irc, u );
644                u->online = 0;
645               
646                /* Remove him/her from the conversations to prevent PART messages after he/she QUIT already */
647                for( c = gc->conversations; c; c = c->next )
648                        remove_chat_buddy_silent( c, handle );
649        }
650       
651        if( ( type & UC_UNAVAILABLE ) && ( !strcmp(gc->prpl->name, "oscar") || !strcmp(gc->prpl->name, "icq")) )
652        {
653                u->away = g_strdup( "Away" );
654        }
655        else if( ( type & UC_UNAVAILABLE ) && ( !strcmp(gc->prpl->name, "jabber") ) )
656        {
657                if( type & UC_DND )
658                        u->away = g_strdup( "Do Not Disturb" );
659                else if( type & UC_XA )
660                        u->away = g_strdup( "Extended Away" );
661                else // if( type & UC_AWAY )
662                        u->away = g_strdup( "Away" );
663        }
664        else if( ( type & UC_UNAVAILABLE ) && gc->prpl->get_status_string )
665        {
666                u->away = g_strdup( gc->prpl->get_status_string( gc, type ) );
667        }
668        else
669                u->away = NULL;
670       
671        /* LISPy... */
672        if( ( set_getint( gc->irc, "away_devoice" ) ) &&                /* Don't do a thing when user doesn't want it */
673            ( u->online ) &&                                            /* Don't touch offline people */
674            ( ( ( u->online != oo ) && !u->away ) ||                    /* Voice joining people */
675              ( ( u->online == oo ) && ( oa == !u->away ) ) ) )         /* (De)voice people changing state */
676        {
677                irc_write( gc->irc, ":%s!%s@%s MODE %s %cv %s", gc->irc->mynick, gc->irc->mynick, gc->irc->myhost,
678                                                                gc->irc->channel, u->away?'-':'+', u->nick );
679        }
680}
681
682void serv_got_im( struct gaim_connection *gc, char *handle, char *msg, guint32 flags, time_t mtime, gint len )
683{
684        irc_t *irc = gc->irc;
685        user_t *u;
686       
687        u = user_findhandle( gc, handle );
688       
689        if( !u )
690        {
691                char *h = set_getstr( irc, "handle_unknown" );
692               
693                if( g_strcasecmp( h, "ignore" ) == 0 )
694                {
695                        if( set_getint( irc, "debug" ) )
696                                serv_got_crap( gc, "Ignoring message from unknown handle %s", handle );
697                       
698                        return;
699                }
700                else if( g_strncasecmp( h, "add", 3 ) == 0 )
701                {
702                        int private = set_getint( irc, "private" );
703                       
704                        if( h[3] )
705                        {
706                                if( g_strcasecmp( h + 3, "_private" ) == 0 )
707                                        private = 1;
708                                else if( g_strcasecmp( h + 3, "_channel" ) == 0 )
709                                        private = 0;
710                        }
711                       
712                        add_buddy( gc, NULL, handle, NULL );
713                        u = user_findhandle( gc, handle );
714                        u->is_private = private;
715                }
716                else
717                {
718                        serv_got_crap( gc, "Message from unknown handle %s:", handle );
719                        u = user_find( irc, irc->mynick );
720                }
721        }
722       
723        if( ( g_strcasecmp( set_getstr( gc->irc, "strip_html" ), "always" ) == 0 ) ||
724            ( ( gc->flags & OPT_CONN_HTML ) && set_getint( gc->irc, "strip_html" ) ) )
725                strip_html( msg );
726
727        while( strlen( msg ) > 425 )
728        {
729                char tmp, *nl;
730               
731                tmp = msg[425];
732                msg[425] = 0;
733               
734                /* If there's a newline/space in this string, split up there,
735                   looks a bit prettier. */
736                if( ( nl = strrchr( msg, '\n' ) ) || ( nl = strrchr( msg, ' ' ) ) )
737                {
738                        msg[425] = tmp;
739                        tmp = *nl;
740                        *nl = 0;
741                }
742               
743                irc_msgfrom( irc, u->nick, msg );
744               
745                /* Move on. */
746                if( nl )
747                {
748                        *nl = tmp;
749                        msg = nl + 1;
750                }
751                else
752                {
753                        msg[425] = tmp;
754                        msg += 425;
755                }
756        }
757        irc_msgfrom( irc, u->nick, msg );
758}
759
760void serv_got_typing( struct gaim_connection *gc, char *handle, int timeout, int type )
761{
762        user_t *u;
763       
764        if( !set_getint( gc->irc, "typing_notice" ) )
765                return;
766       
767        if( ( u = user_findhandle( gc, handle ) ) ) {
768                /* If type is:
769                 * 0: user has stopped typing
770                 * 1: user is actively typing
771                 * 2: user has entered text, but is not actively typing
772                 */
773                if (type == 0 || type == 1 || type == 2) {
774                        char buf[256]; 
775                        g_snprintf(buf, 256, "\1TYPING %d\1", type); 
776                        irc_privmsg( gc->irc, u, "PRIVMSG", gc->irc->nick, NULL, buf );
777                }
778        }
779}
780
781void serv_got_chat_left( struct gaim_connection *gc, int id )
782{
783        struct conversation *c, *l = NULL;
784        GList *ir;
785       
786        if( set_getint( gc->irc, "debug" ) )
787                serv_got_crap( gc, "You were removed from conversation %d", (int) id );
788       
789        for( c = gc->conversations; c && c->id != id; c = (l=c)->next );
790       
791        if( c )
792        {
793                if( c->joined )
794                {
795                        user_t *u, *r;
796                       
797                        r = user_find( gc->irc, gc->irc->mynick );
798                        irc_privmsg( gc->irc, r, "PRIVMSG", c->channel, "", "Cleaning up channel, bye!" );
799                       
800                        u = user_find( gc->irc, gc->irc->nick );
801                        irc_kick( gc->irc, u, c->channel, r );
802                        /* irc_part( gc->irc, u, c->channel ); */
803                }
804               
805                if( l )
806                        l->next = c->next;
807                else
808                        gc->conversations = c->next;
809               
810                for( ir = c->in_room; ir; ir = ir->next )
811                        g_free( ir->data );
812                g_list_free( c->in_room );
813                g_free( c->channel );
814                g_free( c->title );
815                g_free( c );
816        }
817}
818
819void serv_got_chat_in( struct gaim_connection *gc, int id, char *who, int whisper, char *msg, time_t mtime )
820{
821        struct conversation *c;
822        user_t *u;
823       
824        /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */
825        if( g_strcasecmp( who, gc->user->username ) == 0 )
826                return;
827       
828        u = user_findhandle( gc, who );
829        for( c = gc->conversations; c && c->id != id; c = c->next );
830       
831        if( ( g_strcasecmp( set_getstr( gc->irc, "strip_html" ), "always" ) == 0 ) ||
832            ( ( gc->flags & OPT_CONN_HTML ) && set_getint( gc->irc, "strip_html" ) ) )
833                strip_html( msg );
834       
835        if( c && u )
836                irc_privmsg( gc->irc, u, "PRIVMSG", c->channel, "", msg );
837        else
838                serv_got_crap( gc, "Message from/to conversation %s@%d (unknown conv/user): %s", who, id, msg );
839}
840
841struct conversation *serv_got_joined_chat( struct gaim_connection *gc, int id, char *handle )
842{
843        struct conversation *c;
844        char *s;
845       
846        /* This one just creates the conversation structure, user won't see anything yet */
847       
848        if( gc->conversations )
849        {
850                for( c = gc->conversations; c->next; c = c->next );
851                c = c->next = g_new0( struct conversation, 1 );
852        }
853        else
854                gc->conversations = c = g_new0( struct conversation, 1);
855       
856        c->id = id;
857        c->gc = gc;
858        c->title = g_strdup( handle );
859       
860        s = g_new( char, 16 );
861        sprintf( s, "&chat_%03d", gc->irc->c_id++ );
862        c->channel = g_strdup( s );
863        g_free( s );
864       
865        if( set_getint( gc->irc, "debug" ) )
866                serv_got_crap( gc, "Creating new conversation: (id=%d,handle=%s)", id, handle );
867       
868        return( c );
869}
870
871
872/* buddy_chat.c */
873
874void add_chat_buddy( struct conversation *b, char *handle )
875{
876        user_t *u = user_findhandle( b->gc, handle );
877        int me = 0;
878       
879        if( set_getint( b->gc->irc, "debug" ) )
880                serv_got_crap( b->gc, "User %s added to conversation %d", handle, b->id );
881       
882        /* It might be yourself! */
883        if( b->gc->prpl->cmp_buddynames( handle, b->gc->user->username ) == 0 )
884        {
885                u = user_find( b->gc->irc, b->gc->irc->nick );
886                if( !b->joined )
887                        irc_join( b->gc->irc, u, b->channel );
888                b->joined = me = 1;
889        }
890       
891        /* Most protocols allow people to join, even when they're not in
892           your contact list. Try to handle that here */
893        if( !u )
894        {
895                add_buddy( b->gc, NULL, handle, NULL );
896                u = user_findhandle( b->gc, handle );
897        }
898       
899        /* Add the handle to the room userlist, if it's not 'me' */
900        if( !me )
901        {
902                if( b->joined )
903                        irc_join( b->gc->irc, u, b->channel );
904                b->in_room = g_list_append( b->in_room, g_strdup( handle ) );
905        }
906}
907
908void remove_chat_buddy( struct conversation *b, char *handle, char *reason )
909{
910        user_t *u;
911        int me = 0;
912       
913        if( set_getint( b->gc->irc, "debug" ) )
914                serv_got_crap( b->gc, "User %s removed from conversation %d (%s)", handle, b->id, reason ? reason : "" );
915       
916        /* It might be yourself! */
917        if( g_strcasecmp( handle, b->gc->user->username ) == 0 )
918        {
919                u = user_find( b->gc->irc, b->gc->irc->nick );
920                b->joined = 0;
921                me = 1;
922        }
923        else
924        {
925                u = user_findhandle( b->gc, handle );
926        }
927       
928        if( remove_chat_buddy_silent( b, handle ) )
929                if( ( b->joined || me ) && u )
930                        irc_part( b->gc->irc, u, b->channel );
931}
932
933static int remove_chat_buddy_silent( struct conversation *b, char *handle )
934{
935        GList *i;
936       
937        /* Find the handle in the room userlist and shoot it */
938        i = b->in_room;
939        while( i )
940        {
941                if( g_strcasecmp( handle, i->data ) == 0 )
942                {
943                        g_free( i->data );
944                        b->in_room = g_list_remove( b->in_room, i->data );
945                        return( 1 );
946                }
947               
948                i = i->next;
949        }
950       
951        return( 0 );
952}
953
954
955/* Misc. BitlBee stuff which shouldn't really be here */
956
957struct conversation *conv_findchannel( char *channel )
958{
959        struct gaim_connection *gc;
960        struct conversation *c;
961        GSList *l;
962       
963        /* This finds the connection which has a conversation which belongs to this channel */
964        for( l = connections; l; l = l->next )
965        {
966                gc = l->data;
967                for( c = gc->conversations; c && g_strcasecmp( c->channel, channel ) != 0; c = c->next );
968                if( c )
969                        return( c );
970        }
971       
972        return( NULL );
973}
974
975char *set_eval_away_devoice( irc_t *irc, set_t *set, char *value )
976{
977        int st;
978       
979        if( ( g_strcasecmp( value, "true" ) == 0 ) || ( g_strcasecmp( value, "yes" ) == 0 ) || ( g_strcasecmp( value, "on" ) == 0 ) )
980                st = 1;
981        else if( ( g_strcasecmp( value, "false" ) == 0 ) || ( g_strcasecmp( value, "no" ) == 0 ) || ( g_strcasecmp( value, "off" ) == 0 ) )
982                st = 0;
983        else if( sscanf( value, "%d", &st ) != 1 )
984                return( NULL );
985       
986        st = st != 0;
987       
988        /* Horror.... */
989       
990        if( st != set_getint( irc, "away_devoice" ) )
991        {
992                char list[80] = "";
993                user_t *u = irc->users;
994                int i = 0, count = 0;
995                char pm;
996                char v[80];
997               
998                if( st )
999                        pm = '+';
1000                else
1001                        pm = '-';
1002               
1003                while( u )
1004                {
1005                        if( u->gc && u->online && !u->away )
1006                        {
1007                                if( ( strlen( list ) + strlen( u->nick ) ) >= 79 )
1008                                {
1009                                        for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
1010                                        irc_write( irc, ":%s!%s@%s MODE %s %c%s%s",
1011                                                   irc->mynick, irc->mynick, irc->myhost,
1012                                                   irc->channel, pm, v, list );
1013                                       
1014                                        *list = 0;
1015                                        count = 0;
1016                                }
1017                               
1018                                sprintf( list + strlen( list ), " %s", u->nick );
1019                                count ++;
1020                        }
1021                        u = u->next;
1022                }
1023               
1024                /* $v = 'v' x $i */
1025                for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
1026                irc_write( irc, ":%s!%s@%s MODE %s %c%s%s", irc->mynick, irc->mynick, irc->myhost,
1027                                                            irc->channel, pm, v, list );
1028        }
1029       
1030        return( set_eval_bool( irc, set, value ) );
1031}
1032
1033int serv_send_im( irc_t *irc, user_t *u, char *msg, int flags )
1034{
1035        char *buf = NULL;
1036        int st;
1037       
1038        if( ( u->gc->flags & OPT_CONN_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
1039        {
1040                buf = escape_html( msg );
1041                msg = buf;
1042        }
1043       
1044        st = ((struct gaim_connection *)u->gc)->prpl->send_im( u->gc, u->handle, msg, strlen( msg ), flags );
1045        g_free( buf );
1046       
1047        return st;
1048}
1049
1050int serv_send_chat( irc_t *irc, struct gaim_connection *gc, int id, char *msg )
1051{
1052        char *buf = NULL;
1053        int st;
1054       
1055        if( ( gc->flags & OPT_CONN_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
1056        {
1057                buf = escape_html( msg );
1058                msg = buf;
1059        }
1060       
1061        st = gc->prpl->chat_send( gc, id, msg );
1062        g_free( buf );
1063       
1064        return st;
1065}
Note: See TracBrowser for help on using the repository browser.