source: protocols/nogaim.c @ b135438

Last change on this file since b135438 was b135438, checked in by Jelmer Vernooij <jelmer@…>, at 2005-11-15T13:20:27Z

Merge changes from Wilmer

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