source: protocols/nogaim.c @ 814aa52

Last change on this file since 814aa52 was 814aa52, checked in by Sven Moritz Hallberg <pesco@…>, at 2010-06-03T11:00:45Z

merge in bitlbee 1.2.6

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