source: protocols/bee_chat.c @ c8eeadd

Last change on this file since c8eeadd was ad404ab, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-03T00:20:53Z

Restore add_* handle_unknown settings.

  • Property mode set to 100644
File size: 6.3 KB
RevLine 
[f1a0890]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2010 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* Stuff to handle rooms                                                */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
22  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23  Suite 330, Boston, MA  02111-1307  USA
24*/
25
26#define BITLBEE_CORE
27#include "bitlbee.h"
28
29struct groupchat *imcb_chat_new( struct im_connection *ic, const char *handle )
30{
31        struct groupchat *c = g_new0( struct groupchat, 1 );
32        bee_t *bee = ic->bee;
33       
34        /* This one just creates the conversation structure, user won't see
35           anything yet until s/he is joined to the conversation. (This
36           allows you to add other already present participants first.) */
37       
38        ic->groupchats = g_slist_prepend( ic->groupchats, c );
39        c->ic = ic;
40        c->title = g_strdup( handle );
41        c->topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title );
42       
43        if( set_getbool( &ic->bee->set, "debug" ) )
44                imcb_log( ic, "Creating new conversation: (id=%p,handle=%s)", c, handle );
45       
46        if( bee->ui->chat_new )
47                bee->ui->chat_new( bee, c );
48       
49        return c;
50}
51
52void imcb_chat_name_hint( struct groupchat *c, const char *name )
53{
[d343eaa]54        bee_t *bee = c->ic->bee;
55       
56        if( bee->ui->chat_name_hint )
57                bee->ui->chat_name_hint( bee, c, name );
[f1a0890]58}
59
60void imcb_chat_free( struct groupchat *c )
61{
62        struct im_connection *ic = c->ic;
63        bee_t *bee = ic->bee;
64        GList *ir;
65       
66        if( bee->ui->chat_free )
67                bee->ui->chat_free( bee, c );
68       
69        if( set_getbool( &ic->bee->set, "debug" ) )
70                imcb_log( ic, "You were removed from conversation %p", c );
71       
72        ic->groupchats = g_slist_remove( ic->groupchats, c );
73       
74        for( ir = c->in_room; ir; ir = ir->next )
75                g_free( ir->data );
76        g_list_free( c->in_room );
77        g_free( c->title );
78        g_free( c->topic );
79        g_free( c );
80}
81
82void imcb_chat_msg( struct groupchat *c, const char *who, char *msg, uint32_t flags, time_t sent_at )
83{
84        struct im_connection *ic = c->ic;
[27e2c66]85        bee_t *bee = ic->bee;
86        bee_user_t *bu;
87        char *s;
[f1a0890]88       
89        /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */
90        if( g_strcasecmp( who, ic->acc->user ) == 0 )
91                return;
92       
[27e2c66]93        bu = bee_user_by_handle( bee, ic, who );
[f1a0890]94       
[27e2c66]95        s = set_getstr( &ic->bee->set, "strip_html" );
96        if( ( g_strcasecmp( s, "always" ) == 0 ) ||
97            ( ( ic->flags & OPT_DOES_HTML ) && s ) )
[f1a0890]98                strip_html( msg );
99       
[27e2c66]100        if( bu && bee->ui->chat_msg )
101                bee->ui->chat_msg( bee, c, bu, msg, sent_at );
[f1a0890]102        else
[27e2c66]103                imcb_chat_log( c, "Message from unknown participant %s: %s", who, msg );
[f1a0890]104}
105
106void imcb_chat_log( struct groupchat *c, char *format, ... )
107{
[27e2c66]108        struct im_connection *ic = c->ic;
109        bee_t *bee = ic->bee;
[f1a0890]110        va_list params;
111        char *text;
[27e2c66]112       
113        if( !bee->ui->chat_log )
114                return;
[f1a0890]115       
116        va_start( params, format );
117        text = g_strdup_vprintf( format, params );
118        va_end( params );
119       
[27e2c66]120        bee->ui->chat_log( bee, c, text );
[f1a0890]121        g_free( text );
122}
123
124void imcb_chat_topic( struct groupchat *c, char *who, char *topic, time_t set_at )
125{
126        struct im_connection *ic = c->ic;
[9e27f18]127        bee_t *bee = ic->bee;
128        bee_user_t *bu;
129       
130        if( !bee->ui->chat_topic )
131                return;
[f1a0890]132       
133        if( who == NULL)
[9e27f18]134                bu = NULL;
[f1a0890]135        else if( g_strcasecmp( who, ic->acc->user ) == 0 )
[9e27f18]136                bu = bee->user;
[f1a0890]137        else
[9e27f18]138                bu = bee_user_by_handle( bee, ic, who );
[f1a0890]139       
140        if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) ||
141            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) )
142                strip_html( topic );
143       
[9e27f18]144        bee->ui->chat_topic( bee, c, topic, bu );
[f1a0890]145}
146
147void imcb_chat_add_buddy( struct groupchat *c, const char *handle )
148{
149        struct im_connection *ic = c->ic;
150        bee_t *bee = ic->bee;
151        bee_user_t *bu = bee_user_by_handle( bee, ic, handle );
152        gboolean me;
153       
154        if( set_getbool( &c->ic->bee->set, "debug" ) )
155                imcb_log( c->ic, "User %s added to conversation %p", handle, c );
156       
157        me = ic->acc->prpl->handle_cmp( handle, ic->acc->user ) == 0;
158       
159        /* Most protocols allow people to join, even when they're not in
160           your contact list. Try to handle that here */
161        if( !me && !bu )
[ad404ab]162                bu = bee_user_new( bee, ic, handle, BEE_USER_LOCAL );
[f1a0890]163       
164        /* Add the handle to the room userlist */
165        /* TODO: Use bu instead of a string */
166        c->in_room = g_list_append( c->in_room, g_strdup( handle ) );
167       
168        if( bee->ui->chat_add_user )
169                bee->ui->chat_add_user( bee, c, me ? bee->user : bu );
170       
171        if( me )
172                c->joined = 1;
173}
174
[b17ce85]175void imcb_chat_remove_buddy( struct groupchat *c, const char *handle, const char *reason )
[f1a0890]176{
[b17ce85]177        struct im_connection *ic = c->ic;
178        bee_t *bee = ic->bee;
179        bee_user_t *bu = NULL;
[f1a0890]180       
[b17ce85]181        if( set_getbool( &bee->set, "debug" ) )
182                imcb_log( ic, "User %s removed from conversation %p (%s)", handle, c, reason ? reason : "" );
[f1a0890]183       
184        /* It might be yourself! */
[b17ce85]185        if( g_strcasecmp( handle, ic->acc->user ) == 0 )
[f1a0890]186        {
[b17ce85]187                if( c->joined == 0 )
[f1a0890]188                        return;
189               
[b17ce85]190                bu = bee->user;
191                c->joined = 0;
[f1a0890]192        }
193        else
194        {
[b17ce85]195                bu = bee_user_by_handle( bee, ic, handle );
[f1a0890]196        }
197       
[b17ce85]198        if( bee->ui->chat_remove_user )
199                bee->ui->chat_remove_user( bee, c, bu );
[f1a0890]200}
201
[a87754b]202int bee_chat_msg( bee_t *bee, struct groupchat *c, const char *msg, int flags )
203{
204        struct im_connection *ic = c->ic;
205        char *buf = NULL;
206       
207        if( ( ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) )
208        {
209                buf = escape_html( msg );
210                msg = buf;
211        }
212        else
213                buf = g_strdup( msg );
214       
215        ic->acc->prpl->chat_msg( c, buf, flags );
216        g_free( buf );
217       
218        return 1;
219}
[eaaa986]220
221struct groupchat *bee_chat_by_title( bee_t *bee, struct im_connection *ic, const char *title )
222{
223        struct groupchat *c;
224        GSList *l;
225       
226        for( l = ic->groupchats; l; l = l->next )
227        {
228                c = l->data;
229                if( strcmp( c->title, title ) == 0 )
230                        return c;
231        }
232       
233        return NULL;
234}
Note: See TracBrowser for help on using the repository browser.