source: protocols/nogaim.c @ 87b6a3e

Last change on this file since 87b6a3e was 19a6c75, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-03-21T08:19:01Z

Added Hidden and Invisible to the away state aliases list.

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