source: protocols/nogaim.c @ b3117f2

Last change on this file since b3117f2 was e248c7f, checked in by Wilmer van der Gaast <wilmer@…>, at 2009-10-12T22:23:49Z

Automatically try prpl-$proto if $proto doesn't exist, and disable native
protocol modules if purple is enabled; they don't go together very well.

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