source: protocols/bee_chat.c @ f1a0890

Last change on this file since f1a0890 was f1a0890, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-05-07T23:41:49Z

Would be nice to include bee_chat.c in the repo...

  • Property mode set to 100644
File size: 7.0 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/* 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{
54#if 0
55        if( !c->joined )
56        {
57                struct im_connection *ic = c->ic;
58                char stripped[MAX_NICK_LENGTH+1], *full_name;
59               
60                strncpy( stripped, name, MAX_NICK_LENGTH );
61                stripped[MAX_NICK_LENGTH] = '\0';
62                nick_strip( stripped );
63                if( set_getbool( &ic->irc->set, "lcnicks" ) )
64                        nick_lc( stripped );
65               
66                full_name = g_strdup_printf( "&%s", stripped );
67               
68                if( stripped[0] &&
69                    nick_cmp( stripped, ic->irc->channel + 1 ) != 0 &&
70                    irc_chat_by_channel( ic->irc, full_name ) == NULL )
71                {
72                        g_free( c->channel );
73                        c->channel = full_name;
74                }
75                else
76                {
77                        g_free( full_name );
78                }
79        }
80#endif
81}
82
83void imcb_chat_free( struct groupchat *c )
84{
85        struct im_connection *ic = c->ic;
86        bee_t *bee = ic->bee;
87        GList *ir;
88       
89        if( bee->ui->chat_free )
90                bee->ui->chat_free( bee, c );
91       
92        if( set_getbool( &ic->bee->set, "debug" ) )
93                imcb_log( ic, "You were removed from conversation %p", c );
94       
95        ic->groupchats = g_slist_remove( ic->groupchats, c );
96       
97        for( ir = c->in_room; ir; ir = ir->next )
98                g_free( ir->data );
99        g_list_free( c->in_room );
100        g_free( c->title );
101        g_free( c->topic );
102        g_free( c );
103}
104
105void imcb_chat_msg( struct groupchat *c, const char *who, char *msg, uint32_t flags, time_t sent_at )
106{
107#if 0
108        struct im_connection *ic = c->ic;
109        char *wrapped;
110        user_t *u;
111       
112        /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */
113        if( g_strcasecmp( who, ic->acc->user ) == 0 )
114                return;
115       
116        u = user_findhandle( ic, who );
117       
118        if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) ||
119            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) )
120                strip_html( msg );
121       
122        wrapped = word_wrap( msg, 425 );
123        if( c && u )
124        {
125                char *ts = NULL;
126                if( set_getbool( &ic->irc->set, "display_timestamps" ) )
127                        ts = format_timestamp( ic->irc, sent_at );
128                irc_privmsg( ic->irc, u, "PRIVMSG", c->channel, ts ? : "", wrapped );
129                g_free( ts );
130        }
131        else
132        {
133                imcb_log( ic, "Message from/to conversation %s@%p (unknown conv/user): %s", who, c, wrapped );
134        }
135        g_free( wrapped );
136#endif
137}
138
139void imcb_chat_log( struct groupchat *c, char *format, ... )
140{
141#if 0
142        irc_t *irc = c->ic->irc;
143        va_list params;
144        char *text;
145        user_t *u;
146       
147        va_start( params, format );
148        text = g_strdup_vprintf( format, params );
149        va_end( params );
150       
151        u = user_find( irc, irc->mynick );
152       
153        irc_privmsg( irc, u, "PRIVMSG", c->channel, "System message: ", text );
154       
155        g_free( text );
156#endif
157}
158
159void imcb_chat_topic( struct groupchat *c, char *who, char *topic, time_t set_at )
160{
161#if 0
162        struct im_connection *ic = c->ic;
163        user_t *u = NULL;
164       
165        if( who == NULL)
166                u = user_find( ic->irc, ic->irc->mynick );
167        else if( g_strcasecmp( who, ic->acc->user ) == 0 )
168                u = user_find( ic->irc, ic->irc->nick );
169        else
170                u = user_findhandle( ic, who );
171       
172        if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) ||
173            ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) )
174                strip_html( topic );
175       
176        g_free( c->topic );
177        c->topic = g_strdup( topic );
178       
179        if( c->joined && u )
180                irc_write( ic->irc, ":%s!%s@%s TOPIC %s :%s", u->nick, u->user, u->host, c->channel, topic );
181#endif
182}
183
184void imcb_chat_add_buddy( struct groupchat *c, const char *handle )
185{
186        struct im_connection *ic = c->ic;
187        bee_t *bee = ic->bee;
188        bee_user_t *bu = bee_user_by_handle( bee, ic, handle );
189        gboolean me;
190       
191        if( set_getbool( &c->ic->bee->set, "debug" ) )
192                imcb_log( c->ic, "User %s added to conversation %p", handle, c );
193       
194        me = ic->acc->prpl->handle_cmp( handle, ic->acc->user ) == 0;
195       
196        /* Most protocols allow people to join, even when they're not in
197           your contact list. Try to handle that here */
198        if( !me && !bu )
199                bu = bee_user_new( bee, ic, handle );
200       
201        /* Add the handle to the room userlist */
202        /* TODO: Use bu instead of a string */
203        c->in_room = g_list_append( c->in_room, g_strdup( handle ) );
204       
205        if( bee->ui->chat_add_user )
206                bee->ui->chat_add_user( bee, c, me ? bee->user : bu );
207       
208        if( me )
209                c->joined = 1;
210}
211
212/* This function is one BIG hack... :-( EREWRITE */
213void imcb_chat_remove_buddy( struct groupchat *b, const char *handle, const char *reason )
214{
215#if 0
216        user_t *u;
217        int me = 0;
218       
219        if( set_getbool( &b->ic->bee->set, "debug" ) )
220                imcb_log( b->ic, "User %s removed from conversation %p (%s)", handle, b, reason ? reason : "" );
221       
222        /* It might be yourself! */
223        if( g_strcasecmp( handle, b->ic->acc->user ) == 0 )
224        {
225                if( b->joined == 0 )
226                        return;
227               
228                u = user_find( b->ic->irc, b->ic->irc->nick );
229                b->joined = 0;
230                me = 1;
231        }
232        else
233        {
234                u = user_findhandle( b->ic, handle );
235        }
236       
237        if( me || ( remove_chat_buddy_silent( b, handle ) && b->joined && u ) )
238                irc_part( b->ic->irc, u, b->channel );
239#endif
240}
241
242#if 0
243static int remove_chat_buddy_silent( struct groupchat *b, const char *handle )
244{
245        GList *i;
246       
247        /* Find the handle in the room userlist and shoot it */
248        i = b->in_room;
249        while( i )
250        {
251                if( g_strcasecmp( handle, i->data ) == 0 )
252                {
253                        g_free( i->data );
254                        b->in_room = g_list_remove( b->in_room, i->data );
255                        return( 1 );
256                }
257               
258                i = i->next;
259        }
260       
261        return 0;
262}
263#endif
Note: See TracBrowser for help on using the repository browser.