source: protocols/nogaim.c @ 46cce70

Last change on this file since 46cce70 was f1df064, checked in by Wilmer van der Gaast <wilmer@…>, at 2005-12-02T23:41:57Z

Oops...
A
very
small
typo
caused very weird problems in the line splitting code.

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