source: protocols/nogaim.c @ ba16895

Last change on this file since ba16895 was fa295e36, checked in by Wilmer van der Gaast <wilmer@…>, at 2009-10-10T13:39:51Z

Added imcb_ask_auth() instead of reimplementing authorization requests
in every protocol module.

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