source: protocols/nogaim.c @ 823de9d

Last change on this file since 823de9d was 823de9d, checked in by Sven Moritz Hallberg <pesco@…>, at 2009-03-12T19:10:06Z

commit updates by ashish shukla <wahjava@…>

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