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 | |
---|
29 | struct 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 | |
---|
52 | void 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 | |
---|
83 | void 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 | |
---|
105 | void imcb_chat_msg( struct groupchat *c, const char *who, char *msg, uint32_t flags, time_t sent_at ) |
---|
106 | { |
---|
107 | struct im_connection *ic = c->ic; |
---|
108 | bee_t *bee = ic->bee; |
---|
109 | bee_user_t *bu; |
---|
110 | char *s; |
---|
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 | bu = bee_user_by_handle( bee, ic, who ); |
---|
117 | |
---|
118 | s = set_getstr( &ic->bee->set, "strip_html" ); |
---|
119 | if( ( g_strcasecmp( s, "always" ) == 0 ) || |
---|
120 | ( ( ic->flags & OPT_DOES_HTML ) && s ) ) |
---|
121 | strip_html( msg ); |
---|
122 | |
---|
123 | if( bu && bee->ui->chat_msg ) |
---|
124 | bee->ui->chat_msg( bee, c, bu, msg, sent_at ); |
---|
125 | else |
---|
126 | imcb_chat_log( c, "Message from unknown participant %s: %s", who, msg ); |
---|
127 | } |
---|
128 | |
---|
129 | void imcb_chat_log( struct groupchat *c, char *format, ... ) |
---|
130 | { |
---|
131 | struct im_connection *ic = c->ic; |
---|
132 | bee_t *bee = ic->bee; |
---|
133 | va_list params; |
---|
134 | char *text; |
---|
135 | |
---|
136 | if( !bee->ui->chat_log ) |
---|
137 | return; |
---|
138 | |
---|
139 | va_start( params, format ); |
---|
140 | text = g_strdup_vprintf( format, params ); |
---|
141 | va_end( params ); |
---|
142 | |
---|
143 | bee->ui->chat_log( bee, c, text ); |
---|
144 | g_free( text ); |
---|
145 | } |
---|
146 | |
---|
147 | void imcb_chat_topic( struct groupchat *c, char *who, char *topic, time_t set_at ) |
---|
148 | { |
---|
149 | #if 0 |
---|
150 | struct im_connection *ic = c->ic; |
---|
151 | user_t *u = NULL; |
---|
152 | |
---|
153 | if( who == NULL) |
---|
154 | u = user_find( ic->irc, ic->irc->mynick ); |
---|
155 | else if( g_strcasecmp( who, ic->acc->user ) == 0 ) |
---|
156 | u = user_find( ic->irc, ic->irc->nick ); |
---|
157 | else |
---|
158 | u = user_findhandle( ic, who ); |
---|
159 | |
---|
160 | if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) || |
---|
161 | ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) ) |
---|
162 | strip_html( topic ); |
---|
163 | |
---|
164 | g_free( c->topic ); |
---|
165 | c->topic = g_strdup( topic ); |
---|
166 | |
---|
167 | if( c->joined && u ) |
---|
168 | irc_write( ic->irc, ":%s!%s@%s TOPIC %s :%s", u->nick, u->user, u->host, c->channel, topic ); |
---|
169 | #endif |
---|
170 | } |
---|
171 | |
---|
172 | void imcb_chat_add_buddy( struct groupchat *c, const char *handle ) |
---|
173 | { |
---|
174 | struct im_connection *ic = c->ic; |
---|
175 | bee_t *bee = ic->bee; |
---|
176 | bee_user_t *bu = bee_user_by_handle( bee, ic, handle ); |
---|
177 | gboolean me; |
---|
178 | |
---|
179 | if( set_getbool( &c->ic->bee->set, "debug" ) ) |
---|
180 | imcb_log( c->ic, "User %s added to conversation %p", handle, c ); |
---|
181 | |
---|
182 | me = ic->acc->prpl->handle_cmp( handle, ic->acc->user ) == 0; |
---|
183 | |
---|
184 | /* Most protocols allow people to join, even when they're not in |
---|
185 | your contact list. Try to handle that here */ |
---|
186 | if( !me && !bu ) |
---|
187 | bu = bee_user_new( bee, ic, handle ); |
---|
188 | |
---|
189 | /* Add the handle to the room userlist */ |
---|
190 | /* TODO: Use bu instead of a string */ |
---|
191 | c->in_room = g_list_append( c->in_room, g_strdup( handle ) ); |
---|
192 | |
---|
193 | if( bee->ui->chat_add_user ) |
---|
194 | bee->ui->chat_add_user( bee, c, me ? bee->user : bu ); |
---|
195 | |
---|
196 | if( me ) |
---|
197 | c->joined = 1; |
---|
198 | } |
---|
199 | |
---|
200 | /* This function is one BIG hack... :-( EREWRITE */ |
---|
201 | void imcb_chat_remove_buddy( struct groupchat *b, const char *handle, const char *reason ) |
---|
202 | { |
---|
203 | #if 0 |
---|
204 | user_t *u; |
---|
205 | int me = 0; |
---|
206 | |
---|
207 | if( set_getbool( &b->ic->bee->set, "debug" ) ) |
---|
208 | imcb_log( b->ic, "User %s removed from conversation %p (%s)", handle, b, reason ? reason : "" ); |
---|
209 | |
---|
210 | /* It might be yourself! */ |
---|
211 | if( g_strcasecmp( handle, b->ic->acc->user ) == 0 ) |
---|
212 | { |
---|
213 | if( b->joined == 0 ) |
---|
214 | return; |
---|
215 | |
---|
216 | u = user_find( b->ic->irc, b->ic->irc->nick ); |
---|
217 | b->joined = 0; |
---|
218 | me = 1; |
---|
219 | } |
---|
220 | else |
---|
221 | { |
---|
222 | u = user_findhandle( b->ic, handle ); |
---|
223 | } |
---|
224 | |
---|
225 | if( me || ( remove_chat_buddy_silent( b, handle ) && b->joined && u ) ) |
---|
226 | irc_part( b->ic->irc, u, b->channel ); |
---|
227 | #endif |
---|
228 | } |
---|
229 | |
---|
230 | #if 0 |
---|
231 | static int remove_chat_buddy_silent( struct groupchat *b, const char *handle ) |
---|
232 | { |
---|
233 | GList *i; |
---|
234 | |
---|
235 | /* Find the handle in the room userlist and shoot it */ |
---|
236 | i = b->in_room; |
---|
237 | while( i ) |
---|
238 | { |
---|
239 | if( g_strcasecmp( handle, i->data ) == 0 ) |
---|
240 | { |
---|
241 | g_free( i->data ); |
---|
242 | b->in_room = g_list_remove( b->in_room, i->data ); |
---|
243 | return( 1 ); |
---|
244 | } |
---|
245 | |
---|
246 | i = i->next; |
---|
247 | } |
---|
248 | |
---|
249 | return 0; |
---|
250 | } |
---|
251 | #endif |
---|