source: protocols/nogaim.c @ a014331

Last change on this file since a014331 was 9b8a38b, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-03-22T18:22:41Z

Buddy group data is now stored in the user_t structure.

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