source: protocols/nogaim.c @ 796da03

Last change on this file since 796da03 was 796da03, checked in by Wilmer van der Gaast <wilmer@…>, at 2009-10-04T23:28:11Z

Something that compiles and runs, but otherwise utterly useless. Added a
protocols/purple/ module and included it in the build system. Already picks
up all the supported protocols and adds them individually.

  • Property mode set to 100644
File size: 27.5 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2006 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
17/*
18  This program is free software; you can redistribute it and/or modify
19  it under the terms of the GNU General Public License as published by
20  the Free Software Foundation; either version 2 of the License, or
21  (at your option) any later version.
22
23  This program is distributed in the hope that it will be useful,
24  but WITHOUT ANY WARRANTY; without even the implied warranty of
25  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  GNU General Public License for more details.
27
28  You should have received a copy of the GNU General Public License with
29  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
30  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
31  Suite 330, Boston, MA  02111-1307  USA
32*/
33
34#define BITLBEE_CORE
35#include "nogaim.h"
36#include <ctype.h>
37
38static int remove_chat_buddy_silent( struct groupchat *b, const char *handle );
39
40GSList *connections;
41
42#ifdef WITH_PLUGINS
43gboolean load_plugin(char *path)
44{
45        void (*init_function) (void);
46       
47        GModule *mod = g_module_open(path, G_MODULE_BIND_LAZY);
48
49        if(!mod) {
50                log_message(LOGLVL_ERROR, "Can't find `%s', not loading (%s)\n", path, g_module_error());
51                return FALSE;
52        }
53
54        if(!g_module_symbol(mod,"init_plugin",(gpointer *) &init_function)) {
55                log_message(LOGLVL_WARNING, "Can't find function `init_plugin' in `%s'\n", path);
56                return FALSE;
57        }
58
59        init_function();
60
61        return TRUE;
62}
63
64void load_plugins(void)
65{
66        GDir *dir;
67        GError *error = NULL;
68
69        dir = g_dir_open(global.conf->plugindir, 0, &error);
70
71        if (dir) {
72                const gchar *entry;
73                char *path;
74
75                while ((entry = g_dir_read_name(dir))) {
76                        path = g_build_filename(global.conf->plugindir, entry, NULL);
77                        if(!path) {
78                                log_message(LOGLVL_WARNING, "Can't build path for %s\n", entry);
79                                continue;
80                        }
81
82                        load_plugin(path);
83
84                        g_free(path);
85                }
86
87                g_dir_close(dir);
88        }
89}
90#endif
91
92/* nogaim.c */
93
94GList *protocols = NULL;
95 
96void register_protocol (struct prpl *p)
97{
98        protocols = g_list_append(protocols, p);
99}
100
101struct prpl *find_protocol(const char *name)
102{
103        GList *gl;
104        for (gl = protocols; gl; gl = gl->next) 
105        {
106                struct prpl *proto = gl->data;
107                if(!g_strcasecmp(proto->name, name)) 
108                        return proto;
109        }
110        return NULL;
111}
112
113/* nogaim.c */
114void nogaim_init()
115{
116        extern void msn_initmodule();
117        extern void oscar_initmodule();
118        extern void byahoo_initmodule();
119        extern void jabber_initmodule();
120        extern void purple_initmodule();
121
122#ifdef WITH_MSN
123        msn_initmodule();
124#endif
125
126#ifdef WITH_OSCAR
127        oscar_initmodule();
128#endif
129       
130#ifdef WITH_YAHOO
131        byahoo_initmodule();
132#endif
133       
134#ifdef WITH_JABBER
135        jabber_initmodule();
136#endif
137       
138#ifdef WITH_PURPLE
139        purple_initmodule();
140#endif
141
142#ifdef WITH_PLUGINS
143        load_plugins();
144#endif
145}
146
147GSList *get_connections() { return connections; }
148
149/* multi.c */
150
151struct im_connection *imcb_new( account_t *acc )
152{
153        struct im_connection *ic;
154       
155        ic = g_new0( struct im_connection, 1 );
156       
157        ic->irc = acc->irc;
158        ic->acc = acc;
159        acc->ic = ic;
160       
161        connections = g_slist_append( connections, ic );
162       
163        return( ic );
164}
165
166void imc_free( struct im_connection *ic )
167{
168        account_t *a;
169       
170        /* Destroy the pointer to this connection from the account list */
171        for( a = ic->irc->accounts; a; a = a->next )
172                if( a->ic == ic )
173                {
174                        a->ic = NULL;
175                        break;
176                }
177       
178        connections = g_slist_remove( connections, ic );
179        g_free( ic );
180}
181
182static void serv_got_crap( struct im_connection *ic, char *format, ... )
183{
184        va_list params;
185        char *text;
186        account_t *a;
187       
188        va_start( params, format );
189        text = g_strdup_vprintf( format, params );
190        va_end( params );
191
192        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
193            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
194                strip_html( text );
195       
196        /* Try to find a different connection on the same protocol. */
197        for( a = ic->irc->accounts; a; a = a->next )
198                if( a->prpl == ic->acc->prpl && a->ic != ic )
199                        break;
200       
201        /* If we found one, include the screenname in the message. */
202        if( a )
203                irc_usermsg( ic->irc, "%s(%s) - %s", ic->acc->prpl->name, ic->acc->user, text );
204        else
205                irc_usermsg( ic->irc, "%s - %s", ic->acc->prpl->name, text );
206       
207        g_free( text );
208}
209
210void imcb_log( struct im_connection *ic, char *format, ... )
211{
212        va_list params;
213        char *text;
214       
215        va_start( params, format );
216        text = g_strdup_vprintf( format, params );
217        va_end( params );
218       
219        if( ic->flags & OPT_LOGGED_IN )
220                serv_got_crap( ic, "%s", text );
221        else
222                serv_got_crap( ic, "Logging in: %s", text );
223       
224        g_free( text );
225}
226
227void imcb_error( struct im_connection *ic, char *format, ... )
228{
229        va_list params;
230        char *text;
231       
232        va_start( params, format );
233        text = g_strdup_vprintf( format, params );
234        va_end( params );
235       
236        if( ic->flags & OPT_LOGGED_IN )
237                serv_got_crap( ic, "Error: %s", text );
238        else
239                serv_got_crap( ic, "Couldn't log in: %s", text );
240       
241        g_free( text );
242}
243
244static gboolean send_keepalive( gpointer d, gint fd, b_input_condition cond )
245{
246        struct im_connection *ic = d;
247       
248        if( ic->acc->prpl->keepalive )
249                ic->acc->prpl->keepalive( ic );
250       
251        return TRUE;
252}
253
254void imcb_connected( struct im_connection *ic )
255{
256        irc_t *irc = ic->irc;
257        struct chat *c;
258        user_t *u;
259       
260        /* MSN servers sometimes redirect you to a different server and do
261           the whole login sequence again, so these "late" calls to this
262           function should be handled correctly. (IOW, ignored) */
263        if( ic->flags & OPT_LOGGED_IN )
264                return;
265       
266        u = user_find( ic->irc, ic->irc->nick );
267       
268        imcb_log( ic, "Logged in" );
269       
270        ic->keepalive = b_timeout_add( 60000, send_keepalive, ic );
271        ic->flags |= OPT_LOGGED_IN;
272       
273        /* Also necessary when we're not away, at least for some of the
274           protocols. */
275        imc_set_away( ic, u->away );
276       
277        /* Apparently we're connected successfully, so reset the
278           exponential backoff timer. */
279        ic->acc->auto_reconnect_delay = 0;
280       
281        for( c = irc->chatrooms; c; c = c->next )
282        {
283                if( c->acc != ic->acc )
284                        continue;
285               
286                if( set_getbool( &c->set, "auto_join" ) )
287                        chat_join( irc, c, NULL );
288        }
289}
290
291gboolean auto_reconnect( gpointer data, gint fd, b_input_condition cond )
292{
293        account_t *a = data;
294       
295        a->reconnect = 0;
296        account_on( a->irc, a );
297       
298        return( FALSE );        /* Only have to run the timeout once */
299}
300
301void cancel_auto_reconnect( account_t *a )
302{
303        b_event_remove( a->reconnect );
304        a->reconnect = 0;
305}
306
307void imc_logout( struct im_connection *ic, int allow_reconnect )
308{
309        irc_t *irc = ic->irc;
310        user_t *t, *u;
311        account_t *a;
312        int delay;
313       
314        /* Nested calls might happen sometimes, this is probably the best
315           place to catch them. */
316        if( ic->flags & OPT_LOGGING_OUT )
317                return;
318        else
319                ic->flags |= OPT_LOGGING_OUT;
320       
321        imcb_log( ic, "Signing off.." );
322       
323        b_event_remove( ic->keepalive );
324        ic->keepalive = 0;
325        ic->acc->prpl->logout( ic );
326        b_event_remove( ic->inpa );
327       
328        g_free( ic->away );
329        ic->away = NULL;
330       
331        u = irc->users;
332        while( u )
333        {
334                if( u->ic == ic )
335                {
336                        t = u->next;
337                        user_del( irc, u->nick );
338                        u = t;
339                }
340                else
341                        u = u->next;
342        }
343       
344        query_del_by_conn( ic->irc, ic );
345       
346        for( a = irc->accounts; a; a = a->next )
347                if( a->ic == ic )
348                        break;
349       
350        if( !a )
351        {
352                /* Uhm... This is very sick. */
353        }
354        else if( allow_reconnect && set_getbool( &irc->set, "auto_reconnect" ) &&
355                 set_getbool( &a->set, "auto_reconnect" ) &&
356                 ( delay = account_reconnect_delay( a ) ) > 0 )
357        {
358                imcb_log( ic, "Reconnecting in %d seconds..", delay );
359                a->reconnect = b_timeout_add( delay * 1000, auto_reconnect, a );
360        }
361       
362        imc_free( ic );
363}
364
365
366/* dialogs.c */
367
368void imcb_ask( struct im_connection *ic, char *msg, void *data,
369               query_callback doit, query_callback dont )
370{
371        query_add( ic->irc, ic, msg, doit, dont, data );
372}
373
374
375/* list.c */
376
377void imcb_add_buddy( struct im_connection *ic, char *handle, char *group )
378{
379        user_t *u;
380        char nick[MAX_NICK_LENGTH+1], *s;
381        irc_t *irc = ic->irc;
382       
383        if( user_findhandle( ic, handle ) )
384        {
385                if( set_getbool( &irc->set, "debug" ) )
386                        imcb_log( ic, "User already exists, ignoring add request: %s", handle );
387               
388                return;
389               
390                /* Buddy seems to exist already. Let's ignore this request then...
391                   Eventually subsequent calls to this function *should* be possible
392                   when a buddy is in multiple groups. But for now BitlBee doesn't
393                   even support groups so let's silently ignore this for now. */
394        }
395       
396        memset( nick, 0, MAX_NICK_LENGTH + 1 );
397        strcpy( nick, nick_get( ic->acc, handle ) );
398       
399        u = user_add( ic->irc, nick );
400       
401//      if( !realname || !*realname ) realname = nick;
402//      u->realname = g_strdup( realname );
403       
404        if( ( s = strchr( handle, '@' ) ) )
405        {
406                u->host = g_strdup( s + 1 );
407                u->user = g_strndup( handle, s - handle );
408        }
409        else if( ic->acc->server )
410        {
411                u->host = g_strdup( ic->acc->server );
412                u->user = g_strdup( handle );
413               
414                /* s/ /_/ ... important for AOL screennames */
415                for( s = u->user; *s; s ++ )
416                        if( *s == ' ' )
417                                *s = '_';
418        }
419        else
420        {
421                u->host = g_strdup( ic->acc->prpl->name );
422                u->user = g_strdup( handle );
423        }
424       
425        u->ic = ic;
426        u->handle = g_strdup( handle );
427        if( group ) u->group = g_strdup( group );
428        u->send_handler = buddy_send_handler;
429        u->last_typing_notice = 0;
430}
431
432struct buddy *imcb_find_buddy( struct im_connection *ic, char *handle )
433{
434        static struct buddy b[1];
435        user_t *u;
436       
437        u = user_findhandle( ic, handle );
438       
439        if( !u )
440                return( NULL );
441       
442        memset( b, 0, sizeof( b ) );
443        strncpy( b->name, handle, 80 );
444        strncpy( b->show, u->realname, BUDDY_ALIAS_MAXLEN );
445        b->present = u->online;
446        b->ic = u->ic;
447       
448        return( b );
449}
450
451void imcb_rename_buddy( struct im_connection *ic, char *handle, char *realname )
452{
453        user_t *u = user_findhandle( ic, handle );
454       
455        if( !u || !realname ) return;
456       
457        if( g_strcasecmp( u->realname, realname ) != 0 )
458        {
459                if( u->realname != u->nick ) g_free( u->realname );
460               
461                u->realname = g_strdup( realname );
462               
463                if( ( ic->flags & OPT_LOGGED_IN ) && set_getbool( &ic->irc->set, "display_namechanges" ) )
464                        imcb_log( ic, "User `%s' changed name to `%s'", u->nick, u->realname );
465        }
466}
467
468void imcb_remove_buddy( struct im_connection *ic, char *handle, char *group )
469{
470        user_t *u;
471       
472        if( ( u = user_findhandle( ic, handle ) ) )
473                user_del( ic->irc, u->nick );
474}
475
476/* Mainly meant for ICQ (and now also for Jabber conferences) to allow IM
477   modules to suggest a nickname for a handle. */
478void imcb_buddy_nick_hint( struct im_connection *ic, char *handle, char *nick )
479{
480        user_t *u = user_findhandle( ic, handle );
481        char newnick[MAX_NICK_LENGTH+1], *orig_nick;
482       
483        if( u && !u->online && !nick_saved( ic->acc, handle ) )
484        {
485                /* Only do this if the person isn't online yet (which should
486                   be the case if we just added it) and if the user hasn't
487                   assigned a nickname to this buddy already. */
488               
489                strncpy( newnick, nick, MAX_NICK_LENGTH );
490                newnick[MAX_NICK_LENGTH] = 0;
491               
492                /* Some processing to make sure this string is a valid IRC nickname. */
493                nick_strip( newnick );
494                if( set_getbool( &ic->irc->set, "lcnicks" ) )
495                        nick_lc( newnick );
496               
497                if( strcmp( u->nick, newnick ) != 0 )
498                {
499                        /* Only do this if newnick is different from the current one.
500                           If rejoining a channel, maybe we got this nick already
501                           (and dedupe would only add an underscore. */
502                        nick_dedupe( ic->acc, handle, newnick );
503                       
504                        /* u->nick will be freed halfway the process, so it can't be
505                           passed as an argument. */
506                        orig_nick = g_strdup( u->nick );
507                        user_rename( ic->irc, orig_nick, newnick );
508                        g_free( orig_nick );
509                }
510        }
511}
512
513/* prpl.c */
514
515struct show_got_added_data
516{
517        struct im_connection *ic;
518        char *handle;
519};
520
521void show_got_added_no( void *data )
522{
523        g_free( ((struct show_got_added_data*)data)->handle );
524        g_free( data );
525}
526
527void show_got_added_yes( void *data )
528{
529        struct show_got_added_data *sga = data;
530       
531        sga->ic->acc->prpl->add_buddy( sga->ic, sga->handle, NULL );
532        /* imcb_add_buddy( sga->ic, NULL, sga->handle, sga->handle ); */
533       
534        return show_got_added_no( data );
535}
536
537void imcb_ask_add( struct im_connection *ic, char *handle, const char *realname )
538{
539        struct show_got_added_data *data = g_new0( struct show_got_added_data, 1 );
540        char *s;
541       
542        /* TODO: Make a setting for this! */
543        if( user_findhandle( ic, handle ) != NULL )
544                return;
545       
546        s = g_strdup_printf( "The user %s is not in your buddy list yet. Do you want to add him/her now?", handle );
547       
548        data->ic = ic;
549        data->handle = g_strdup( handle );
550        query_add( ic->irc, ic, s, show_got_added_yes, show_got_added_no, data );
551}
552
553
554/* server.c */                   
555
556void imcb_buddy_status( struct im_connection *ic, const char *handle, int flags, const char *state, const char *message )
557{
558        user_t *u;
559        int oa, oo;
560       
561        u = user_findhandle( ic, (char*) handle );
562       
563        if( !u )
564        {
565                if( g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "add" ) == 0 )
566                {
567                        imcb_add_buddy( ic, (char*) handle, NULL );
568                        u = user_findhandle( ic, (char*) handle );
569                }
570                else
571                {
572                        if( set_getbool( &ic->irc->set, "debug" ) || g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "ignore" ) != 0 )
573                        {
574                                imcb_log( ic, "imcb_buddy_status() for unknown handle %s:", handle );
575                                imcb_log( ic, "flags = %d, state = %s, message = %s", flags,
576                                          state ? state : "NULL", message ? message : "NULL" );
577                        }
578                       
579                        return;
580                }
581        }
582       
583        oa = u->away != NULL;
584        oo = u->online;
585       
586        if( u->away )
587        {
588                g_free( u->away );
589                u->away = NULL;
590        }
591       
592        if( ( flags & OPT_LOGGED_IN ) && !u->online )
593        {
594                irc_spawn( ic->irc, u );
595                u->online = 1;
596        }
597        else if( !( flags & OPT_LOGGED_IN ) && u->online )
598        {
599                struct groupchat *c;
600               
601                irc_kill( ic->irc, u );
602                u->online = 0;
603               
604                /* Remove him/her from the groupchats to prevent PART messages after he/she QUIT already */
605                for( c = ic->groupchats; c; c = c->next )
606                        remove_chat_buddy_silent( c, handle );
607        }
608       
609        if( flags & OPT_AWAY )
610        {
611                if( state && message )
612                {
613                        u->away = g_strdup_printf( "%s (%s)", state, message );
614                }
615                else if( state )
616                {
617                        u->away = g_strdup( state );
618                }
619                else if( message )
620                {
621                        u->away = g_strdup( message );
622                }
623                else
624                {
625                        u->away = g_strdup( "Away" );
626                }
627        }
628        /* else waste_any_state_information_for_now(); */
629       
630        /* LISPy... */
631        if( ( set_getbool( &ic->irc->set, "away_devoice" ) ) &&         /* Don't do a thing when user doesn't want it */
632            ( u->online ) &&                                            /* Don't touch offline people */
633            ( ( ( u->online != oo ) && !u->away ) ||                    /* Voice joining people */
634              ( ( u->online == oo ) && ( oa == !u->away ) ) ) )         /* (De)voice people changing state */
635        {
636                char *from;
637               
638                if( set_getbool( &ic->irc->set, "simulate_netsplit" ) )
639                {
640                        from = g_strdup( ic->irc->myhost );
641                }
642                else
643                {
644                        from = g_strdup_printf( "%s!%s@%s", ic->irc->mynick, ic->irc->mynick,
645                                                            ic->irc->myhost );
646                }
647                irc_write( ic->irc, ":%s MODE %s %cv %s", from, ic->irc->channel,
648                                                          u->away?'-':'+', u->nick );
649                g_free( from );
650        }
651}
652
653void imcb_buddy_msg( struct im_connection *ic, char *handle, char *msg, uint32_t flags, time_t sent_at )
654{
655        irc_t *irc = ic->irc;
656        char *wrapped;
657        user_t *u;
658       
659        u = user_findhandle( ic, handle );
660       
661        if( !u )
662        {
663                char *h = set_getstr( &irc->set, "handle_unknown" );
664               
665                if( g_strcasecmp( h, "ignore" ) == 0 )
666                {
667                        if( set_getbool( &irc->set, "debug" ) )
668                                imcb_log( ic, "Ignoring message from unknown handle %s", handle );
669                       
670                        return;
671                }
672                else if( g_strncasecmp( h, "add", 3 ) == 0 )
673                {
674                        int private = set_getbool( &irc->set, "private" );
675                       
676                        if( h[3] )
677                        {
678                                if( g_strcasecmp( h + 3, "_private" ) == 0 )
679                                        private = 1;
680                                else if( g_strcasecmp( h + 3, "_channel" ) == 0 )
681                                        private = 0;
682                        }
683                       
684                        imcb_add_buddy( ic, handle, NULL );
685                        u = user_findhandle( ic, handle );
686                        u->is_private = private;
687                }
688                else
689                {
690                        imcb_log( ic, "Message from unknown handle %s:", handle );
691                        u = user_find( irc, irc->mynick );
692                }
693        }
694       
695        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
696            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
697                strip_html( msg );
698
699        wrapped = word_wrap( msg, 425 );
700        irc_msgfrom( irc, u->nick, wrapped );
701        g_free( wrapped );
702}
703
704void imcb_buddy_typing( struct im_connection *ic, char *handle, uint32_t flags )
705{
706        user_t *u;
707       
708        if( !set_getbool( &ic->irc->set, "typing_notice" ) )
709                return;
710       
711        if( ( u = user_findhandle( ic, handle ) ) )
712        {
713                char buf[256]; 
714               
715                g_snprintf( buf, 256, "\1TYPING %d\1", ( flags >> 8 ) & 3 );
716                irc_privmsg( ic->irc, u, "PRIVMSG", ic->irc->nick, NULL, buf );
717        }
718}
719
720struct groupchat *imcb_chat_new( struct im_connection *ic, const char *handle )
721{
722        struct groupchat *c;
723       
724        /* This one just creates the conversation structure, user won't see anything yet */
725       
726        if( ic->groupchats )
727        {
728                for( c = ic->groupchats; c->next; c = c->next );
729                c = c->next = g_new0( struct groupchat, 1 );
730        }
731        else
732                ic->groupchats = c = g_new0( struct groupchat, 1 );
733       
734        c->ic = ic;
735        c->title = g_strdup( handle );
736        c->channel = g_strdup_printf( "&chat_%03d", ic->irc->c_id++ );
737        c->topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title );
738       
739        if( set_getbool( &ic->irc->set, "debug" ) )
740                imcb_log( ic, "Creating new conversation: (id=%p,handle=%s)", c, handle );
741       
742        return c;
743}
744
745void imcb_chat_free( struct groupchat *c )
746{
747        struct im_connection *ic = c->ic;
748        struct groupchat *l;
749        GList *ir;
750       
751        if( set_getbool( &ic->irc->set, "debug" ) )
752                imcb_log( ic, "You were removed from conversation %p", c );
753       
754        if( c )
755        {
756                if( c->joined )
757                {
758                        user_t *u, *r;
759                       
760                        r = user_find( ic->irc, ic->irc->mynick );
761                        irc_privmsg( ic->irc, r, "PRIVMSG", c->channel, "", "Cleaning up channel, bye!" );
762                       
763                        u = user_find( ic->irc, ic->irc->nick );
764                        irc_kick( ic->irc, u, c->channel, r );
765                        /* irc_part( ic->irc, u, c->channel ); */
766                }
767               
768                /* Find the previous chat in the linked list. */
769                for( l = ic->groupchats; l && l->next != c; l = l->next );
770               
771                if( l )
772                        l->next = c->next;
773                else
774                        ic->groupchats = c->next;
775               
776                for( ir = c->in_room; ir; ir = ir->next )
777                        g_free( ir->data );
778                g_list_free( c->in_room );
779                g_free( c->channel );
780                g_free( c->title );
781                g_free( c->topic );
782                g_free( c );
783        }
784}
785
786void imcb_chat_msg( struct groupchat *c, char *who, char *msg, uint32_t flags, time_t sent_at )
787{
788        struct im_connection *ic = c->ic;
789        char *wrapped;
790        user_t *u;
791       
792        /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */
793        if( g_strcasecmp( who, ic->acc->user ) == 0 )
794                return;
795       
796        u = user_findhandle( ic, who );
797       
798        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
799            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
800                strip_html( msg );
801       
802        wrapped = word_wrap( msg, 425 );
803        if( c && u )
804        {
805                irc_privmsg( ic->irc, u, "PRIVMSG", c->channel, "", wrapped );
806        }
807        else
808        {
809                imcb_log( ic, "Message from/to conversation %s@%p (unknown conv/user): %s", who, c, wrapped );
810        }
811        g_free( wrapped );
812}
813
814void imcb_chat_log( struct groupchat *c, char *format, ... )
815{
816        irc_t *irc = c->ic->irc;
817        va_list params;
818        char *text;
819        user_t *u;
820       
821        va_start( params, format );
822        text = g_strdup_vprintf( format, params );
823        va_end( params );
824       
825        u = user_find( irc, irc->mynick );
826       
827        irc_privmsg( irc, u, "PRIVMSG", c->channel, "System message: ", text );
828       
829        g_free( text );
830}
831
832void imcb_chat_topic( struct groupchat *c, char *who, char *topic, time_t set_at )
833{
834        struct im_connection *ic = c->ic;
835        user_t *u = NULL;
836       
837        if( who == NULL)
838                u = user_find( ic->irc, ic->irc->mynick );
839        else if( g_strcasecmp( who, ic->acc->user ) == 0 )
840                u = user_find( ic->irc, ic->irc->nick );
841        else
842                u = user_findhandle( ic, who );
843       
844        if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) ||
845            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) )
846                strip_html( topic );
847       
848        g_free( c->topic );
849        c->topic = g_strdup( topic );
850       
851        if( c->joined && u )
852                irc_write( ic->irc, ":%s!%s@%s TOPIC %s :%s", u->nick, u->user, u->host, c->channel, topic );
853}
854
855
856/* buddy_chat.c */
857
858void imcb_chat_add_buddy( struct groupchat *b, char *handle )
859{
860        user_t *u = user_findhandle( b->ic, handle );
861        int me = 0;
862       
863        if( set_getbool( &b->ic->irc->set, "debug" ) )
864                imcb_log( b->ic, "User %s added to conversation %p", handle, b );
865       
866        /* It might be yourself! */
867        if( b->ic->acc->prpl->handle_cmp( handle, b->ic->acc->user ) == 0 )
868        {
869                u = user_find( b->ic->irc, b->ic->irc->nick );
870                if( !b->joined )
871                        irc_join( b->ic->irc, u, b->channel );
872                b->joined = me = 1;
873        }
874       
875        /* Most protocols allow people to join, even when they're not in
876           your contact list. Try to handle that here */
877        if( !u )
878        {
879                imcb_add_buddy( b->ic, handle, NULL );
880                u = user_findhandle( b->ic, handle );
881        }
882       
883        /* Add the handle to the room userlist, if it's not 'me' */
884        if( !me )
885        {
886                if( b->joined )
887                        irc_join( b->ic->irc, u, b->channel );
888                b->in_room = g_list_append( b->in_room, g_strdup( handle ) );
889        }
890}
891
892/* This function is one BIG hack... :-( EREWRITE */
893void imcb_chat_remove_buddy( struct groupchat *b, char *handle, char *reason )
894{
895        user_t *u;
896        int me = 0;
897       
898        if( set_getbool( &b->ic->irc->set, "debug" ) )
899                imcb_log( b->ic, "User %s removed from conversation %p (%s)", handle, b, reason ? reason : "" );
900       
901        /* It might be yourself! */
902        if( g_strcasecmp( handle, b->ic->acc->user ) == 0 )
903        {
904                if( b->joined == 0 )
905                        return;
906               
907                u = user_find( b->ic->irc, b->ic->irc->nick );
908                b->joined = 0;
909                me = 1;
910        }
911        else
912        {
913                u = user_findhandle( b->ic, handle );
914        }
915       
916        if( me || ( remove_chat_buddy_silent( b, handle ) && b->joined && u ) )
917                irc_part( b->ic->irc, u, b->channel );
918}
919
920static int remove_chat_buddy_silent( struct groupchat *b, const char *handle )
921{
922        GList *i;
923       
924        /* Find the handle in the room userlist and shoot it */
925        i = b->in_room;
926        while( i )
927        {
928                if( g_strcasecmp( handle, i->data ) == 0 )
929                {
930                        g_free( i->data );
931                        b->in_room = g_list_remove( b->in_room, i->data );
932                        return( 1 );
933                }
934               
935                i = i->next;
936        }
937       
938        return( 0 );
939}
940
941
942/* Misc. BitlBee stuff which shouldn't really be here */
943
944char *set_eval_away_devoice( set_t *set, char *value )
945{
946        irc_t *irc = set->data;
947        int st;
948       
949        if( !is_bool( value ) )
950                return SET_INVALID;
951       
952        st = bool2int( value );
953       
954        /* Horror.... */
955       
956        if( st != set_getbool( &irc->set, "away_devoice" ) )
957        {
958                char list[80] = "";
959                user_t *u = irc->users;
960                int i = 0, count = 0;
961                char pm;
962                char v[80];
963               
964                if( st )
965                        pm = '+';
966                else
967                        pm = '-';
968               
969                while( u )
970                {
971                        if( u->ic && u->online && !u->away )
972                        {
973                                if( ( strlen( list ) + strlen( u->nick ) ) >= 79 )
974                                {
975                                        for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
976                                        irc_write( irc, ":%s MODE %s %c%s%s",
977                                                   irc->myhost,
978                                                   irc->channel, pm, v, list );
979                                       
980                                        *list = 0;
981                                        count = 0;
982                                }
983                               
984                                sprintf( list + strlen( list ), " %s", u->nick );
985                                count ++;
986                        }
987                        u = u->next;
988                }
989               
990                /* $v = 'v' x $i */
991                for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0;
992                irc_write( irc, ":%s MODE %s %c%s%s", irc->myhost,
993                                                            irc->channel, pm, v, list );
994        }
995       
996        return value;
997}
998
999
1000
1001
1002/* The plan is to not allow straight calls to prpl functions anymore, but do
1003   them all from some wrappers. We'll start to define some down here: */
1004
1005int imc_buddy_msg( struct im_connection *ic, char *handle, char *msg, int flags )
1006{
1007        char *buf = NULL;
1008        int st;
1009       
1010        if( ( ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
1011        {
1012                buf = escape_html( msg );
1013                msg = buf;
1014        }
1015       
1016        st = ic->acc->prpl->buddy_msg( ic, handle, msg, flags );
1017        g_free( buf );
1018       
1019        return st;
1020}
1021
1022int imc_chat_msg( struct groupchat *c, char *msg, int flags )
1023{
1024        char *buf = NULL;
1025       
1026        if( ( c->ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
1027        {
1028                buf = escape_html( msg );
1029                msg = buf;
1030        }
1031       
1032        c->ic->acc->prpl->chat_msg( c, msg, flags );
1033        g_free( buf );
1034       
1035        return 1;
1036}
1037
1038static char *imc_away_alias_find( GList *gcm, char *away );
1039
1040int imc_set_away( struct im_connection *ic, char *away )
1041{
1042        GList *m, *ms;
1043        char *s;
1044       
1045        if( !away ) away = "";
1046        ms = m = ic->acc->prpl->away_states( ic );
1047       
1048        while( m )
1049        {
1050                if( *away )
1051                {
1052                        if( g_strncasecmp( m->data, away, strlen( m->data ) ) == 0 )
1053                                break;
1054                }
1055                else
1056                {
1057                        if( g_strcasecmp( m->data, "Available" ) == 0 )
1058                                break;
1059                        if( g_strcasecmp( m->data, "Online" ) == 0 )
1060                                break;
1061                }
1062                m = m->next;
1063        }
1064       
1065        if( m )
1066        {
1067                ic->acc->prpl->set_away( ic, m->data, *away ? away : NULL );
1068        }
1069        else
1070        {
1071                s = imc_away_alias_find( ms, away );
1072                if( s )
1073                {
1074                        ic->acc->prpl->set_away( ic, s, away );
1075                        if( set_getbool( &ic->irc->set, "debug" ) )
1076                                imcb_log( ic, "Setting away state to %s", s );
1077                }
1078                else
1079                        ic->acc->prpl->set_away( ic, GAIM_AWAY_CUSTOM, away );
1080        }
1081       
1082        return( 1 );
1083}
1084
1085static char *imc_away_alias_list[8][5] =
1086{
1087        { "Away from computer", "Away", "Extended away", NULL },
1088        { "NA", "N/A", "Not available", NULL },
1089        { "Busy", "Do not disturb", "DND", "Occupied", NULL },
1090        { "Be right back", "BRB", NULL },
1091        { "On the phone", "Phone", "On phone", NULL },
1092        { "Out to lunch", "Lunch", "Food", NULL },
1093        { "Invisible", "Hidden" },
1094        { NULL }
1095};
1096
1097static char *imc_away_alias_find( GList *gcm, char *away )
1098{
1099        GList *m;
1100        int i, j;
1101       
1102        for( i = 0; *imc_away_alias_list[i]; i ++ )
1103        {
1104                for( j = 0; imc_away_alias_list[i][j]; j ++ )
1105                        if( g_strncasecmp( away, imc_away_alias_list[i][j], strlen( imc_away_alias_list[i][j] ) ) == 0 )
1106                                break;
1107               
1108                if( !imc_away_alias_list[i][j] )        /* If we reach the end, this row */
1109                        continue;                       /* is not what we want. Next!    */
1110               
1111                /* Now find an entry in this row which exists in gcm */
1112                for( j = 0; imc_away_alias_list[i][j]; j ++ )
1113                {
1114                        m = gcm;
1115                        while( m )
1116                        {
1117                                if( g_strcasecmp( imc_away_alias_list[i][j], m->data ) == 0 )
1118                                        return( imc_away_alias_list[i][j] );
1119                                m = m->next;
1120                        }
1121                }
1122        }
1123       
1124        return( NULL );
1125}
1126
1127void imc_add_allow( struct im_connection *ic, char *handle )
1128{
1129        if( g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL )
1130        {
1131                ic->permit = g_slist_prepend( ic->permit, g_strdup( handle ) );
1132        }
1133       
1134        ic->acc->prpl->add_permit( ic, handle );
1135}
1136
1137void imc_rem_allow( struct im_connection *ic, char *handle )
1138{
1139        GSList *l;
1140       
1141        if( ( l = g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) )
1142        {
1143                g_free( l->data );
1144                ic->permit = g_slist_delete_link( ic->permit, l );
1145        }
1146       
1147        ic->acc->prpl->rem_permit( ic, handle );
1148}
1149
1150void imc_add_block( struct im_connection *ic, char *handle )
1151{
1152        if( g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL )
1153        {
1154                ic->deny = g_slist_prepend( ic->deny, g_strdup( handle ) );
1155        }
1156       
1157        ic->acc->prpl->add_deny( ic, handle );
1158}
1159
1160void imc_rem_block( struct im_connection *ic, char *handle )
1161{
1162        GSList *l;
1163       
1164        if( ( l = g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) )
1165        {
1166                g_free( l->data );
1167                ic->deny = g_slist_delete_link( ic->deny, l );
1168        }
1169       
1170        ic->acc->prpl->rem_deny( ic, handle );
1171}
1172
1173void imcb_clean_handle( struct im_connection *ic, char *handle )
1174{
1175        /* Accepts a handle and does whatever is necessary to make it
1176           BitlBee-friendly. Currently this means removing everything
1177           outside 33-127 (ASCII printable excl spaces), @ (only one
1178           is allowed) and ! and : */
1179        char out[strlen(handle)+1];
1180        int s, d;
1181       
1182        s = d = 0;
1183        while( handle[s] )
1184        {
1185                if( handle[s] > ' ' && handle[s] != '!' && handle[s] != ':' &&
1186                    ( handle[s] & 0x80 ) == 0 )
1187                {
1188                        if( handle[s] == '@' )
1189                        {
1190                                /* See if we got an @ already? */
1191                                out[d] = 0;
1192                                if( strchr( out, '@' ) )
1193                                        continue;
1194                        }
1195                       
1196                        out[d++] = handle[s];
1197                }
1198                s ++;
1199        }
1200        out[d] = handle[s];
1201       
1202        strcpy( handle, out );
1203}
Note: See TracBrowser for help on using the repository browser.