1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2012 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* Some glue to put the IRC and the IM stuff together. */ |
---|
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., 51 Franklin St., |
---|
23 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
24 | */ |
---|
25 | |
---|
26 | #include "bitlbee.h" |
---|
27 | #include "dcc.h" |
---|
28 | |
---|
29 | /* IM->IRC callbacks: Simple IM/buddy-related stuff. */ |
---|
30 | |
---|
31 | static const struct irc_user_funcs irc_user_im_funcs; |
---|
32 | |
---|
33 | static void bee_irc_imc_connected(struct im_connection *ic) |
---|
34 | { |
---|
35 | irc_t *irc = (irc_t *) ic->bee->ui_data; |
---|
36 | |
---|
37 | irc_channel_auto_joins(irc, ic->acc); |
---|
38 | } |
---|
39 | |
---|
40 | static void bee_irc_imc_disconnected(struct im_connection *ic) |
---|
41 | { |
---|
42 | /* Maybe try to send /QUITs here instead of later on. */ |
---|
43 | } |
---|
44 | |
---|
45 | static gboolean bee_irc_user_new(bee_t *bee, bee_user_t *bu) |
---|
46 | { |
---|
47 | irc_user_t *iu; |
---|
48 | irc_t *irc = (irc_t *) bee->ui_data; |
---|
49 | char nick[MAX_NICK_LENGTH + 1], *s; |
---|
50 | |
---|
51 | memset(nick, 0, MAX_NICK_LENGTH + 1); |
---|
52 | strncpy(nick, nick_get(bu), MAX_NICK_LENGTH); |
---|
53 | |
---|
54 | bu->ui_data = iu = irc_user_new(irc, nick); |
---|
55 | iu->bu = bu; |
---|
56 | |
---|
57 | if (set_getbool(&irc->b->set, "private")) { |
---|
58 | iu->last_channel = NULL; |
---|
59 | } else { |
---|
60 | iu->last_channel = irc_channel_with_user(irc, iu); |
---|
61 | } |
---|
62 | |
---|
63 | if ((s = strchr(bu->handle, '@'))) { |
---|
64 | iu->host = g_strdup(s + 1); |
---|
65 | iu->user = g_strndup(bu->handle, s - bu->handle); |
---|
66 | } else { |
---|
67 | iu->user = g_strdup(bu->handle); |
---|
68 | if (bu->ic->acc->server) { |
---|
69 | iu->host = g_strdup(bu->ic->acc->server); |
---|
70 | } else { |
---|
71 | char *s; |
---|
72 | for (s = bu->ic->acc->tag; g_ascii_isalnum(*s); s++) { |
---|
73 | ; |
---|
74 | } |
---|
75 | /* Only use the tag if we got to the end of the string. |
---|
76 | (So allow alphanumerics only. Hopefully not too |
---|
77 | restrictive.) */ |
---|
78 | if (*s) { |
---|
79 | iu->host = g_strdup(bu->ic->acc->prpl->name); |
---|
80 | } else { |
---|
81 | iu->host = g_strdup(bu->ic->acc->tag); |
---|
82 | } |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | /* Sanitize */ |
---|
87 | str_reject_chars(iu->user, " ", '_'); |
---|
88 | str_reject_chars(iu->host, " ", '_'); |
---|
89 | |
---|
90 | if (bu->flags & BEE_USER_LOCAL) { |
---|
91 | char *s = set_getstr(&bu->ic->acc->set, "handle_unknown") ? : |
---|
92 | set_getstr(&bee->set, "handle_unknown"); |
---|
93 | |
---|
94 | if (g_strcasecmp(s, "add_private") == 0) { |
---|
95 | iu->last_channel = NULL; |
---|
96 | } else if (g_strcasecmp(s, "add_channel") == 0) { |
---|
97 | iu->last_channel = irc->default_channel; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | iu->f = &irc_user_im_funcs; |
---|
102 | |
---|
103 | return TRUE; |
---|
104 | } |
---|
105 | |
---|
106 | static gboolean bee_irc_user_free(bee_t *bee, bee_user_t *bu) |
---|
107 | { |
---|
108 | return irc_user_free(bee->ui_data, (irc_user_t *) bu->ui_data); |
---|
109 | } |
---|
110 | |
---|
111 | static gboolean bee_irc_user_status(bee_t *bee, bee_user_t *bu, bee_user_t *old) |
---|
112 | { |
---|
113 | irc_t *irc = bee->ui_data; |
---|
114 | irc_user_t *iu = bu->ui_data; |
---|
115 | |
---|
116 | /* Do this outside the if below since away state can change without |
---|
117 | the online state changing. */ |
---|
118 | iu->flags &= ~IRC_USER_AWAY; |
---|
119 | if (bu->flags & BEE_USER_AWAY || !(bu->flags & BEE_USER_ONLINE)) { |
---|
120 | iu->flags |= IRC_USER_AWAY; |
---|
121 | } |
---|
122 | |
---|
123 | if ((bu->flags & BEE_USER_ONLINE) != (old->flags & BEE_USER_ONLINE)) { |
---|
124 | if (bu->flags & BEE_USER_ONLINE) { |
---|
125 | if (g_hash_table_lookup(irc->watches, iu->key)) { |
---|
126 | irc_send_num(irc, 600, "%s %s %s %d :%s", iu->nick, iu->user, |
---|
127 | iu->host, (int) time(NULL), "logged online"); |
---|
128 | } |
---|
129 | } else { |
---|
130 | if (g_hash_table_lookup(irc->watches, iu->key)) { |
---|
131 | irc_send_num(irc, 601, "%s %s %s %d :%s", iu->nick, iu->user, |
---|
132 | iu->host, (int) time(NULL), "logged offline"); |
---|
133 | } |
---|
134 | |
---|
135 | /* Send a QUIT since those will also show up in any |
---|
136 | query windows the user may have, plus it's only |
---|
137 | one QUIT instead of possibly many (in case of |
---|
138 | multiple control chans). If there's a channel that |
---|
139 | shows offline people, a JOIN will follow. */ |
---|
140 | if (set_getbool(&bee->set, "offline_user_quits")) { |
---|
141 | irc_user_quit(iu, "Leaving..."); |
---|
142 | } |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | /* Reset this one since the info may have changed. */ |
---|
147 | iu->away_reply_timeout = 0; |
---|
148 | |
---|
149 | bee_irc_channel_update(irc, NULL, iu); |
---|
150 | |
---|
151 | /* If away-notify enabled, send status updates when: |
---|
152 | * Away or Online state changes |
---|
153 | * Status changes (e.g. "Away" to "Mobile") |
---|
154 | * Status message changes |
---|
155 | */ |
---|
156 | if ((irc->caps & CAP_AWAY_NOTIFY) && |
---|
157 | ((bu->flags & BEE_USER_AWAY) != (old->flags & BEE_USER_AWAY) || |
---|
158 | (bu->flags & BEE_USER_ONLINE) != (old->flags & BEE_USER_ONLINE) || |
---|
159 | (g_strcmp0(bu->status, old->status) != 0) || |
---|
160 | (g_strcmp0(bu->status_msg, old->status_msg) != 0))) { |
---|
161 | irc_send_away_notify(iu); |
---|
162 | } |
---|
163 | |
---|
164 | return TRUE; |
---|
165 | } |
---|
166 | |
---|
167 | void bee_irc_channel_update(irc_t *irc, irc_channel_t *ic, irc_user_t *iu) |
---|
168 | { |
---|
169 | GSList *l; |
---|
170 | |
---|
171 | if (ic == NULL) { |
---|
172 | for (l = irc->channels; l; l = l->next) { |
---|
173 | ic = l->data; |
---|
174 | /* TODO: Just add a type flag or so.. */ |
---|
175 | if (ic->f == irc->default_channel->f && |
---|
176 | (ic->flags & IRC_CHANNEL_JOINED)) { |
---|
177 | bee_irc_channel_update(irc, ic, iu); |
---|
178 | } |
---|
179 | } |
---|
180 | return; |
---|
181 | } |
---|
182 | if (iu == NULL) { |
---|
183 | GHashTableIter iter; |
---|
184 | gpointer itervalue; |
---|
185 | g_hash_table_iter_init(&iter, irc->nick_user_hash); |
---|
186 | |
---|
187 | while (g_hash_table_iter_next(&iter, NULL, &itervalue)) { |
---|
188 | iu = itervalue; |
---|
189 | if (iu->bu) { |
---|
190 | bee_irc_channel_update(irc, ic, iu); |
---|
191 | } |
---|
192 | } |
---|
193 | return; |
---|
194 | } |
---|
195 | |
---|
196 | if (!irc_channel_wants_user(ic, iu)) { |
---|
197 | irc_channel_del_user(ic, iu, IRC_CDU_PART, NULL); |
---|
198 | } else { |
---|
199 | struct irc_control_channel *icc = ic->data; |
---|
200 | int mode = 0; |
---|
201 | |
---|
202 | if (!(iu->bu->flags & BEE_USER_ONLINE)) { |
---|
203 | mode = icc->modes[0]; |
---|
204 | } else if (iu->bu->flags & BEE_USER_AWAY) { |
---|
205 | mode = icc->modes[1]; |
---|
206 | } else if (iu->bu->flags & BEE_USER_SPECIAL) { |
---|
207 | mode = icc->modes[2]; |
---|
208 | } else { |
---|
209 | mode = icc->modes[3]; |
---|
210 | } |
---|
211 | |
---|
212 | if (!mode) { |
---|
213 | irc_channel_del_user(ic, iu, IRC_CDU_PART, NULL); |
---|
214 | } else { |
---|
215 | irc_channel_add_user(ic, iu); |
---|
216 | irc_channel_user_set_mode(ic, iu, mode); |
---|
217 | } |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | static gboolean bee_irc_user_msg(bee_t *bee, bee_user_t *bu, const char *msg_, guint32 flags, time_t sent_at) |
---|
222 | { |
---|
223 | irc_t *irc = bee->ui_data; |
---|
224 | irc_user_t *iu = (irc_user_t *) bu->ui_data; |
---|
225 | irc_user_t *src_iu = iu; |
---|
226 | irc_user_t *dst_iu = irc->user; |
---|
227 | const char *dst; |
---|
228 | char *prefix = NULL; |
---|
229 | char *wrapped, *ts = NULL; |
---|
230 | char *msg = g_strdup(msg_); |
---|
231 | char *message_type = "PRIVMSG"; |
---|
232 | GSList *l; |
---|
233 | |
---|
234 | if (sent_at > 0 && |
---|
235 | !(irc->caps & CAP_SERVER_TIME) && |
---|
236 | set_getbool(&irc->b->set, "display_timestamps")) { |
---|
237 | ts = irc_format_timestamp(irc, sent_at); |
---|
238 | } |
---|
239 | |
---|
240 | dst = irc_user_msgdest(iu); |
---|
241 | |
---|
242 | if (flags & OPT_SELFMESSAGE) { |
---|
243 | char *setting = set_getstr(&irc->b->set, "self_messages"); |
---|
244 | |
---|
245 | if (is_bool(setting)) { |
---|
246 | if (bool2int(setting)) { |
---|
247 | /* set to true, send it with src/dst flipped */ |
---|
248 | |
---|
249 | dst_iu = iu; |
---|
250 | src_iu = irc->user; |
---|
251 | |
---|
252 | if (dst == irc->user->nick) { |
---|
253 | dst = dst_iu->nick; |
---|
254 | } |
---|
255 | } else { |
---|
256 | /* set to false, skip the message completely */ |
---|
257 | goto cleanup; |
---|
258 | } |
---|
259 | } else if (g_strncasecmp(setting, "prefix", 6) == 0) { |
---|
260 | /* third state, prefix, loosely imitates the znc privmsg_prefix module */ |
---|
261 | |
---|
262 | g_free(msg); |
---|
263 | if (g_strncasecmp(msg_, "/me ", 4) == 0) { |
---|
264 | msg = g_strdup_printf("/me -> %s", msg_ + 4); |
---|
265 | } else { |
---|
266 | msg = g_strdup_printf("-> %s", msg_); |
---|
267 | } |
---|
268 | |
---|
269 | if (g_strcasecmp(setting, "prefix_notice") == 0) { |
---|
270 | message_type = "NOTICE"; |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | } |
---|
275 | |
---|
276 | if (dst != dst_iu->nick) { |
---|
277 | /* if not messaging directly (control channel), call user by name */ |
---|
278 | prefix = g_strdup_printf("%s%s%s", dst_iu->nick, set_getstr(&bee->set, "to_char"), ts ? : ""); |
---|
279 | } else { |
---|
280 | prefix = ts; |
---|
281 | ts = NULL; /* don't double-free */ |
---|
282 | } |
---|
283 | |
---|
284 | for (l = irc_plugins; l; l = l->next) { |
---|
285 | irc_plugin_t *p = l->data; |
---|
286 | if (p->filter_msg_in) { |
---|
287 | char *s = p->filter_msg_in(iu, msg, 0); |
---|
288 | if (s) { |
---|
289 | if (s != msg) { |
---|
290 | g_free(msg); |
---|
291 | } |
---|
292 | msg = s; |
---|
293 | } else { |
---|
294 | /* Modules can swallow messages. */ |
---|
295 | goto cleanup; |
---|
296 | } |
---|
297 | } |
---|
298 | } |
---|
299 | |
---|
300 | if ((g_strcasecmp(set_getstr(&bee->set, "strip_html"), "always") == 0) || |
---|
301 | ((bu->ic->flags & OPT_DOES_HTML) && set_getbool(&bee->set, "strip_html"))) { |
---|
302 | char *s = g_strdup(msg); |
---|
303 | strip_html(s); |
---|
304 | g_free(msg); |
---|
305 | msg = s; |
---|
306 | } |
---|
307 | |
---|
308 | wrapped = word_wrap(msg, IRC_WORD_WRAP); |
---|
309 | irc_send_msg_ts(src_iu, message_type, dst, wrapped, prefix, sent_at); |
---|
310 | g_free(wrapped); |
---|
311 | |
---|
312 | cleanup: |
---|
313 | g_free(prefix); |
---|
314 | g_free(msg); |
---|
315 | g_free(ts); |
---|
316 | |
---|
317 | return TRUE; |
---|
318 | } |
---|
319 | |
---|
320 | static gboolean bee_irc_user_typing(bee_t *bee, bee_user_t *bu, guint32 flags) |
---|
321 | { |
---|
322 | irc_t *irc = (irc_t *) bee->ui_data; |
---|
323 | |
---|
324 | if (set_getbool(&bee->set, "typing_notice")) { |
---|
325 | irc_send_msg_f((irc_user_t *) bu->ui_data, "PRIVMSG", irc->user->nick, |
---|
326 | "\001TYPING %d\001", (flags >> 8) & 3); |
---|
327 | } else { |
---|
328 | return FALSE; |
---|
329 | } |
---|
330 | |
---|
331 | return TRUE; |
---|
332 | } |
---|
333 | |
---|
334 | static gboolean bee_irc_user_action_response(bee_t *bee, bee_user_t *bu, const char *action, char * const args[], |
---|
335 | void *data) |
---|
336 | { |
---|
337 | irc_t *irc = (irc_t *) bee->ui_data; |
---|
338 | GString *msg = g_string_new("\001"); |
---|
339 | |
---|
340 | g_string_append(msg, action); |
---|
341 | while (*args) { |
---|
342 | if (strchr(*args, ' ')) { |
---|
343 | g_string_append_printf(msg, " \"%s\"", *args); |
---|
344 | } else { |
---|
345 | g_string_append_printf(msg, " %s", *args); |
---|
346 | } |
---|
347 | args++; |
---|
348 | } |
---|
349 | g_string_append_c(msg, '\001'); |
---|
350 | |
---|
351 | irc_send_msg((irc_user_t *) bu->ui_data, "NOTICE", irc->user->nick, msg->str, NULL); |
---|
352 | |
---|
353 | g_string_free(msg, TRUE); |
---|
354 | |
---|
355 | return TRUE; |
---|
356 | } |
---|
357 | |
---|
358 | static gboolean bee_irc_user_nick_update(irc_user_t *iu, gboolean offline_only); |
---|
359 | |
---|
360 | static gboolean bee_irc_user_fullname(bee_t *bee, bee_user_t *bu) |
---|
361 | { |
---|
362 | irc_user_t *iu = (irc_user_t *) bu->ui_data; |
---|
363 | char *s; |
---|
364 | |
---|
365 | if (iu->fullname != iu->nick) { |
---|
366 | g_free(iu->fullname); |
---|
367 | } |
---|
368 | iu->fullname = g_strdup(bu->fullname); |
---|
369 | |
---|
370 | /* Strip newlines (unlikely, but IRC-unfriendly so they must go) |
---|
371 | TODO(wilmer): Do the same with away msgs again! */ |
---|
372 | for (s = iu->fullname; *s; s++) { |
---|
373 | if (g_ascii_isspace(*s)) { |
---|
374 | *s = ' '; |
---|
375 | } |
---|
376 | } |
---|
377 | |
---|
378 | if ((bu->ic->flags & OPT_LOGGED_IN) && set_getbool(&bee->set, "display_namechanges")) { |
---|
379 | /* People don't like this /NOTICE. Meh, let's go back to the old one. |
---|
380 | char *msg = g_strdup_printf( "<< \002BitlBee\002 - Changed name to `%s' >>", iu->fullname ); |
---|
381 | irc_send_msg( iu, "NOTICE", irc->user->nick, msg, NULL ); |
---|
382 | */ |
---|
383 | imcb_log(bu->ic, "User `%s' changed name to `%s'", iu->nick, iu->fullname); |
---|
384 | } |
---|
385 | |
---|
386 | bee_irc_user_nick_update(iu, TRUE); |
---|
387 | |
---|
388 | return TRUE; |
---|
389 | } |
---|
390 | |
---|
391 | static gboolean bee_irc_user_nick_hint(bee_t *bee, bee_user_t *bu, const char *hint) |
---|
392 | { |
---|
393 | bee_irc_user_nick_update((irc_user_t *) bu->ui_data, TRUE); |
---|
394 | |
---|
395 | return TRUE; |
---|
396 | } |
---|
397 | |
---|
398 | static gboolean bee_irc_user_nick_change(bee_t *bee, bee_user_t *bu, const char *nick) |
---|
399 | { |
---|
400 | bee_irc_user_nick_update((irc_user_t *) bu->ui_data, FALSE); |
---|
401 | |
---|
402 | return TRUE; |
---|
403 | } |
---|
404 | |
---|
405 | static gboolean bee_irc_user_group(bee_t *bee, bee_user_t *bu) |
---|
406 | { |
---|
407 | irc_user_t *iu = (irc_user_t *) bu->ui_data; |
---|
408 | irc_t *irc = (irc_t *) bee->ui_data; |
---|
409 | |
---|
410 | bee_irc_channel_update(irc, NULL, iu); |
---|
411 | bee_irc_user_nick_update(iu, FALSE); |
---|
412 | |
---|
413 | return TRUE; |
---|
414 | } |
---|
415 | |
---|
416 | static gboolean bee_irc_user_nick_update(irc_user_t *iu, gboolean offline_only) |
---|
417 | { |
---|
418 | bee_user_t *bu = iu->bu; |
---|
419 | char *newnick; |
---|
420 | |
---|
421 | if (offline_only && bu->flags & BEE_USER_ONLINE) { |
---|
422 | /* Ignore if the user is visible already. */ |
---|
423 | return TRUE; |
---|
424 | } |
---|
425 | |
---|
426 | if (nick_saved(bu)) { |
---|
427 | /* The user already assigned a nickname to this person. */ |
---|
428 | return TRUE; |
---|
429 | } |
---|
430 | |
---|
431 | newnick = nick_get(bu); |
---|
432 | |
---|
433 | if (strcmp(iu->nick, newnick) != 0) { |
---|
434 | nick_dedupe(bu, newnick); |
---|
435 | irc_user_set_nick(iu, newnick); |
---|
436 | } |
---|
437 | |
---|
438 | return TRUE; |
---|
439 | } |
---|
440 | |
---|
441 | void bee_irc_user_nick_reset(irc_user_t *iu) |
---|
442 | { |
---|
443 | bee_user_t *bu = iu->bu; |
---|
444 | |
---|
445 | if (bu == FALSE) { |
---|
446 | return; |
---|
447 | } |
---|
448 | |
---|
449 | nick_del(bu); |
---|
450 | bee_irc_user_nick_update(iu, FALSE); |
---|
451 | |
---|
452 | } |
---|
453 | |
---|
454 | #define PASTEBUF_LONG_SPACELESS_LINE_LENGTH 350 |
---|
455 | |
---|
456 | /* Returns FALSE if the last line of the message is near the typical irc length |
---|
457 | * limit and the message has no spaces, indicating that it's probably desirable |
---|
458 | * to join messages without the newline. |
---|
459 | * |
---|
460 | * The main use case for this is pasting long URLs and not breaking them */ |
---|
461 | static gboolean bee_irc_pastebuf_should_start_with_newline(const char *msg) |
---|
462 | { |
---|
463 | int i; |
---|
464 | const char *last_line = strrchr(msg, '\n') ? : msg; |
---|
465 | |
---|
466 | if (*last_line == '\n') { |
---|
467 | last_line++; |
---|
468 | } |
---|
469 | |
---|
470 | for (i = 0; last_line[i]; i++) { |
---|
471 | if (g_ascii_isspace(last_line[i])) { |
---|
472 | return TRUE; |
---|
473 | } |
---|
474 | } |
---|
475 | |
---|
476 | if (i < PASTEBUF_LONG_SPACELESS_LINE_LENGTH) { |
---|
477 | return TRUE; |
---|
478 | } |
---|
479 | |
---|
480 | return FALSE; |
---|
481 | } |
---|
482 | |
---|
483 | /* IRC->IM calls */ |
---|
484 | |
---|
485 | static gboolean bee_irc_user_privmsg_cb(gpointer data, gint fd, b_input_condition cond); |
---|
486 | |
---|
487 | static gboolean bee_irc_user_privmsg(irc_user_t *iu, const char *msg) |
---|
488 | { |
---|
489 | const char *away; |
---|
490 | |
---|
491 | if (iu->bu == NULL) { |
---|
492 | return FALSE; |
---|
493 | } |
---|
494 | |
---|
495 | if (iu->last_channel == NULL && |
---|
496 | (away = irc_user_get_away(iu)) && |
---|
497 | time(NULL) >= iu->away_reply_timeout) { |
---|
498 | irc_send_num(iu->irc, 301, "%s :%s", iu->nick, away); |
---|
499 | iu->away_reply_timeout = time(NULL) + |
---|
500 | set_getint(&iu->irc->b->set, "away_reply_timeout"); |
---|
501 | } |
---|
502 | |
---|
503 | if (iu->pastebuf == NULL) { |
---|
504 | iu->pastebuf = g_string_new(msg); |
---|
505 | } else { |
---|
506 | b_event_remove(iu->pastebuf_timer); |
---|
507 | if (bee_irc_pastebuf_should_start_with_newline(iu->pastebuf->str)) { |
---|
508 | g_string_append_c(iu->pastebuf, '\n'); |
---|
509 | } |
---|
510 | g_string_append(iu->pastebuf, msg); |
---|
511 | } |
---|
512 | |
---|
513 | if (set_getbool(&iu->irc->b->set, "paste_buffer")) { |
---|
514 | int delay; |
---|
515 | |
---|
516 | if ((delay = set_getint(&iu->irc->b->set, "paste_buffer_delay")) <= 5) { |
---|
517 | delay *= 1000; |
---|
518 | } |
---|
519 | |
---|
520 | iu->pastebuf_timer = b_timeout_add(delay, bee_irc_user_privmsg_cb, iu); |
---|
521 | |
---|
522 | return TRUE; |
---|
523 | } else { |
---|
524 | bee_irc_user_privmsg_cb(iu, 0, 0); |
---|
525 | |
---|
526 | return TRUE; |
---|
527 | } |
---|
528 | } |
---|
529 | |
---|
530 | static gboolean bee_irc_user_privmsg_cb(gpointer data, gint fd, b_input_condition cond) |
---|
531 | { |
---|
532 | irc_user_t *iu = data; |
---|
533 | char *msg; |
---|
534 | GSList *l; |
---|
535 | |
---|
536 | msg = g_string_free(iu->pastebuf, FALSE); |
---|
537 | iu->pastebuf = NULL; |
---|
538 | iu->pastebuf_timer = 0; |
---|
539 | |
---|
540 | for (l = irc_plugins; l; l = l->next) { |
---|
541 | irc_plugin_t *p = l->data; |
---|
542 | if (p->filter_msg_out) { |
---|
543 | char *s = p->filter_msg_out(iu, msg, 0); |
---|
544 | if (s) { |
---|
545 | if (s != msg) { |
---|
546 | g_free(msg); |
---|
547 | } |
---|
548 | msg = s; |
---|
549 | } else { |
---|
550 | /* Modules can swallow messages. */ |
---|
551 | iu->pastebuf = NULL; |
---|
552 | g_free(msg); |
---|
553 | return FALSE; |
---|
554 | } |
---|
555 | } |
---|
556 | } |
---|
557 | |
---|
558 | bee_user_msg(iu->irc->b, iu->bu, msg, 0); |
---|
559 | |
---|
560 | g_free(msg); |
---|
561 | |
---|
562 | return FALSE; |
---|
563 | } |
---|
564 | |
---|
565 | static gboolean bee_irc_user_ctcp(irc_user_t *iu, char *const *ctcp) |
---|
566 | { |
---|
567 | if (ctcp[1] && g_strcasecmp(ctcp[0], "DCC") == 0 |
---|
568 | && g_strcasecmp(ctcp[1], "SEND") == 0) { |
---|
569 | if (iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->transfer_request) { |
---|
570 | file_transfer_t *ft = dcc_request(iu->bu->ic, ctcp); |
---|
571 | if (ft) { |
---|
572 | iu->bu->ic->acc->prpl->transfer_request(iu->bu->ic, ft, iu->bu->handle); |
---|
573 | } |
---|
574 | |
---|
575 | return TRUE; |
---|
576 | } |
---|
577 | } else if (g_strcasecmp(ctcp[0], "TYPING") == 0) { |
---|
578 | if (iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->send_typing && ctcp[1]) { |
---|
579 | int st = ctcp[1][0]; |
---|
580 | if (st >= '0' && st <= '2') { |
---|
581 | st <<= 8; |
---|
582 | iu->bu->ic->acc->prpl->send_typing(iu->bu->ic, iu->bu->handle, st); |
---|
583 | } |
---|
584 | |
---|
585 | return TRUE; |
---|
586 | } |
---|
587 | } else if (g_strcasecmp(ctcp[0], "HELP") == 0 && iu->bu) { |
---|
588 | GString *supp = g_string_new("Supported CTCPs:"); |
---|
589 | GList *l; |
---|
590 | |
---|
591 | if (iu->bu->ic && iu->bu->ic->acc->prpl->transfer_request) { |
---|
592 | g_string_append(supp, " DCC SEND,"); |
---|
593 | } |
---|
594 | if (iu->bu->ic && iu->bu->ic->acc->prpl->send_typing) { |
---|
595 | g_string_append(supp, " TYPING,"); |
---|
596 | } |
---|
597 | if (iu->bu->ic->acc->prpl->buddy_action_list) { |
---|
598 | for (l = iu->bu->ic->acc->prpl->buddy_action_list(iu->bu); l; l = l->next) { |
---|
599 | struct buddy_action *ba = l->data; |
---|
600 | g_string_append_printf(supp, " %s (%s),", |
---|
601 | ba->name, ba->description); |
---|
602 | } |
---|
603 | } |
---|
604 | g_string_truncate(supp, supp->len - 1); |
---|
605 | irc_send_msg_f(iu, "NOTICE", iu->irc->user->nick, "\001HELP %s\001", supp->str); |
---|
606 | g_string_free(supp, TRUE); |
---|
607 | } else if (iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->buddy_action) { |
---|
608 | iu->bu->ic->acc->prpl->buddy_action(iu->bu, ctcp[0], ctcp + 1, NULL); |
---|
609 | } |
---|
610 | |
---|
611 | return FALSE; |
---|
612 | } |
---|
613 | |
---|
614 | static const struct irc_user_funcs irc_user_im_funcs = { |
---|
615 | bee_irc_user_privmsg, |
---|
616 | bee_irc_user_ctcp, |
---|
617 | }; |
---|
618 | |
---|
619 | |
---|
620 | /* IM->IRC: Groupchats */ |
---|
621 | const struct irc_channel_funcs irc_channel_im_chat_funcs; |
---|
622 | |
---|
623 | static gboolean bee_irc_chat_new(bee_t *bee, struct groupchat *c) |
---|
624 | { |
---|
625 | irc_t *irc = bee->ui_data; |
---|
626 | irc_channel_t *ic; |
---|
627 | char *topic; |
---|
628 | GSList *l; |
---|
629 | int i; |
---|
630 | |
---|
631 | /* Try to find a channel that expects to receive a groupchat. |
---|
632 | This flag is set earlier in our current call trace. */ |
---|
633 | for (l = irc->channels; l; l = l->next) { |
---|
634 | ic = l->data; |
---|
635 | if (ic->flags & IRC_CHANNEL_CHAT_PICKME) { |
---|
636 | break; |
---|
637 | } |
---|
638 | } |
---|
639 | |
---|
640 | /* If we found none, just generate some stupid name. */ |
---|
641 | if (l == NULL) { |
---|
642 | for (i = 0; i <= 999; i++) { |
---|
643 | char name[16]; |
---|
644 | sprintf(name, "#chat_%03d", i); |
---|
645 | if ((ic = irc_channel_new(irc, name))) { |
---|
646 | break; |
---|
647 | } |
---|
648 | } |
---|
649 | } |
---|
650 | |
---|
651 | if (ic == NULL) { |
---|
652 | return FALSE; |
---|
653 | } |
---|
654 | |
---|
655 | c->ui_data = ic; |
---|
656 | ic->data = c; |
---|
657 | |
---|
658 | topic = g_strdup_printf( |
---|
659 | "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", |
---|
660 | c->title); |
---|
661 | irc_channel_set_topic(ic, topic, irc->root); |
---|
662 | g_free(topic); |
---|
663 | |
---|
664 | return TRUE; |
---|
665 | } |
---|
666 | |
---|
667 | static gboolean bee_irc_chat_free(bee_t *bee, struct groupchat *c) |
---|
668 | { |
---|
669 | irc_channel_t *ic = c->ui_data; |
---|
670 | |
---|
671 | if (ic == NULL) { |
---|
672 | return FALSE; |
---|
673 | } |
---|
674 | |
---|
675 | ic->data = NULL; |
---|
676 | c->ui_data = NULL; |
---|
677 | irc_channel_del_user(ic, ic->irc->user, IRC_CDU_KICK, "Chatroom closed by server"); |
---|
678 | |
---|
679 | return TRUE; |
---|
680 | } |
---|
681 | |
---|
682 | static gboolean bee_irc_chat_log(bee_t *bee, struct groupchat *c, const char *text) |
---|
683 | { |
---|
684 | irc_channel_t *ic = c->ui_data; |
---|
685 | |
---|
686 | if (ic == NULL) { |
---|
687 | return FALSE; |
---|
688 | } |
---|
689 | |
---|
690 | irc_channel_printf(ic, "%s", text); |
---|
691 | |
---|
692 | return TRUE; |
---|
693 | } |
---|
694 | |
---|
695 | static gboolean bee_irc_chat_msg(bee_t *bee, struct groupchat *c, bee_user_t *bu, const char *msg, guint32 flags, time_t sent_at) |
---|
696 | { |
---|
697 | irc_t *irc = bee->ui_data; |
---|
698 | irc_user_t *iu = flags & OPT_SELFMESSAGE ? irc->user : bu->ui_data; |
---|
699 | irc_channel_t *ic = c->ui_data; |
---|
700 | char *wrapped, *ts = NULL; |
---|
701 | |
---|
702 | if (ic == NULL) { |
---|
703 | return FALSE; |
---|
704 | } |
---|
705 | |
---|
706 | if (sent_at > 0 && |
---|
707 | !(irc->caps & CAP_SERVER_TIME) && |
---|
708 | set_getbool(&bee->set, "display_timestamps")) { |
---|
709 | ts = irc_format_timestamp(irc, sent_at); |
---|
710 | } |
---|
711 | |
---|
712 | wrapped = word_wrap(msg, IRC_WORD_WRAP); |
---|
713 | irc_send_msg_ts(iu, "PRIVMSG", ic->name, wrapped, ts, sent_at); |
---|
714 | g_free(ts); |
---|
715 | g_free(wrapped); |
---|
716 | |
---|
717 | return TRUE; |
---|
718 | } |
---|
719 | |
---|
720 | static gboolean bee_irc_chat_add_user(bee_t *bee, struct groupchat *c, bee_user_t *bu) |
---|
721 | { |
---|
722 | irc_t *irc = bee->ui_data; |
---|
723 | irc_channel_t *ic = c->ui_data; |
---|
724 | |
---|
725 | if (ic == NULL) { |
---|
726 | return FALSE; |
---|
727 | } |
---|
728 | |
---|
729 | irc_channel_add_user(ic, bu == bee->user ? irc->user : bu->ui_data); |
---|
730 | |
---|
731 | return TRUE; |
---|
732 | } |
---|
733 | |
---|
734 | static gboolean bee_irc_chat_remove_user(bee_t *bee, struct groupchat *c, bee_user_t *bu, const char *reason) |
---|
735 | { |
---|
736 | irc_t *irc = bee->ui_data; |
---|
737 | irc_channel_t *ic = c->ui_data; |
---|
738 | |
---|
739 | if (ic == NULL || bu == NULL) { |
---|
740 | return FALSE; |
---|
741 | } |
---|
742 | |
---|
743 | /* TODO: Possible bug here: If a module removes $user here instead of just |
---|
744 | using imcb_chat_free() and the channel was IRC_CHANNEL_TEMP, we get into |
---|
745 | a broken state around here. */ |
---|
746 | irc_channel_del_user(ic, bu == bee->user ? irc->user : bu->ui_data, IRC_CDU_PART, reason); |
---|
747 | |
---|
748 | return TRUE; |
---|
749 | } |
---|
750 | |
---|
751 | static gboolean bee_irc_chat_topic(bee_t *bee, struct groupchat *c, const char *new, bee_user_t *bu) |
---|
752 | { |
---|
753 | irc_channel_t *ic = c->ui_data; |
---|
754 | irc_t *irc = bee->ui_data; |
---|
755 | irc_user_t *iu; |
---|
756 | |
---|
757 | if (ic == NULL) { |
---|
758 | return FALSE; |
---|
759 | } |
---|
760 | |
---|
761 | if (bu == NULL) { |
---|
762 | iu = irc->root; |
---|
763 | } else if (bu == bee->user) { |
---|
764 | iu = irc->user; |
---|
765 | } else { |
---|
766 | iu = bu->ui_data; |
---|
767 | } |
---|
768 | |
---|
769 | irc_channel_set_topic(ic, new, iu); |
---|
770 | |
---|
771 | return TRUE; |
---|
772 | } |
---|
773 | |
---|
774 | static gboolean bee_irc_chat_name_hint(bee_t *bee, struct groupchat *c, const char *name) |
---|
775 | { |
---|
776 | return irc_channel_name_hint(c->ui_data, name); |
---|
777 | } |
---|
778 | |
---|
779 | static gboolean bee_irc_chat_invite(bee_t *bee, bee_user_t *bu, const char *name, const char *msg) |
---|
780 | { |
---|
781 | char *channel, *s; |
---|
782 | irc_t *irc = bee->ui_data; |
---|
783 | irc_user_t *iu = bu->ui_data; |
---|
784 | irc_channel_t *chan; |
---|
785 | |
---|
786 | if (strchr(CTYPES, name[0])) { |
---|
787 | channel = g_strdup(name); |
---|
788 | } else { |
---|
789 | channel = g_strdup_printf("#%s", name); |
---|
790 | } |
---|
791 | |
---|
792 | if ((s = strchr(channel, '@'))) { |
---|
793 | *s = '\0'; |
---|
794 | } |
---|
795 | |
---|
796 | if (strlen(channel) > MAX_NICK_LENGTH) { |
---|
797 | /* If the channel name is very long (like those insane GTalk |
---|
798 | UUID names), try if we can use the inviter's nick. */ |
---|
799 | s = g_strdup_printf("#%s", iu->nick); |
---|
800 | if (irc_channel_by_name(irc, s) == NULL) { |
---|
801 | g_free(channel); |
---|
802 | channel = s; |
---|
803 | } else { |
---|
804 | g_free(s); |
---|
805 | } |
---|
806 | } |
---|
807 | |
---|
808 | if ((chan = irc_channel_new(irc, channel)) && |
---|
809 | set_setstr(&chan->set, "type", "chat") && |
---|
810 | set_setstr(&chan->set, "chat_type", "room") && |
---|
811 | set_setstr(&chan->set, "account", bu->ic->acc->tag) && |
---|
812 | set_setstr(&chan->set, "room", (char *) name)) { |
---|
813 | /* I'm assuming that if the user didn't "chat add" the room |
---|
814 | himself but got invited, it's temporary, so make this a |
---|
815 | temporary mapping that is removed as soon as we /PART. */ |
---|
816 | chan->flags |= IRC_CHANNEL_TEMP; |
---|
817 | } else { |
---|
818 | irc_channel_free(chan); |
---|
819 | chan = NULL; |
---|
820 | } |
---|
821 | g_free(channel); |
---|
822 | |
---|
823 | irc_send_msg_f(iu, "PRIVMSG", irc->user->nick, "<< \002BitlBee\002 - Invitation to chatroom %s >>", name); |
---|
824 | if (msg) { |
---|
825 | irc_send_msg(iu, "PRIVMSG", irc->user->nick, msg, NULL); |
---|
826 | } |
---|
827 | if (chan) { |
---|
828 | irc_send_msg_f(iu, "PRIVMSG", irc->user->nick, "To join the room, just /join %s", chan->name); |
---|
829 | irc_send_invite(iu, chan); |
---|
830 | } |
---|
831 | |
---|
832 | return TRUE; |
---|
833 | } |
---|
834 | |
---|
835 | /* IRC->IM */ |
---|
836 | static gboolean bee_irc_channel_chat_privmsg_cb(gpointer data, gint fd, b_input_condition cond); |
---|
837 | |
---|
838 | static gboolean bee_irc_channel_chat_privmsg(irc_channel_t *ic, const char *msg) |
---|
839 | { |
---|
840 | struct groupchat *c = ic->data; |
---|
841 | char *trans = NULL, *s; |
---|
842 | |
---|
843 | if (c == NULL) { |
---|
844 | return FALSE; |
---|
845 | } |
---|
846 | |
---|
847 | if (set_getbool(&ic->set, "translate_to_nicks")) { |
---|
848 | char nick[MAX_NICK_LENGTH + 1]; |
---|
849 | irc_user_t *iu; |
---|
850 | |
---|
851 | strncpy(nick, msg, MAX_NICK_LENGTH); |
---|
852 | nick[MAX_NICK_LENGTH] = '\0'; |
---|
853 | if ((s = strchr(nick, ':')) || (s = strchr(nick, ','))) { |
---|
854 | *s = '\0'; |
---|
855 | if ((iu = irc_user_by_name(ic->irc, nick)) && iu->bu && |
---|
856 | iu->bu->nick && irc_channel_has_user(ic, iu)) { |
---|
857 | trans = g_strconcat(iu->bu->nick, msg + (s - nick), NULL); |
---|
858 | msg = trans; |
---|
859 | } |
---|
860 | } |
---|
861 | } |
---|
862 | |
---|
863 | if (set_getbool(&ic->irc->b->set, "paste_buffer")) { |
---|
864 | int delay; |
---|
865 | |
---|
866 | if (ic->pastebuf == NULL) { |
---|
867 | ic->pastebuf = g_string_new(msg); |
---|
868 | } else { |
---|
869 | b_event_remove(ic->pastebuf_timer); |
---|
870 | g_string_append_printf(ic->pastebuf, "\n%s", msg); |
---|
871 | } |
---|
872 | |
---|
873 | if ((delay = set_getint(&ic->irc->b->set, "paste_buffer_delay")) <= 5) { |
---|
874 | delay *= 1000; |
---|
875 | } |
---|
876 | |
---|
877 | ic->pastebuf_timer = b_timeout_add(delay, bee_irc_channel_chat_privmsg_cb, ic); |
---|
878 | |
---|
879 | g_free(trans); |
---|
880 | return TRUE; |
---|
881 | } else { |
---|
882 | bee_chat_msg(ic->irc->b, c, msg, 0); |
---|
883 | } |
---|
884 | |
---|
885 | g_free(trans); |
---|
886 | return TRUE; |
---|
887 | } |
---|
888 | |
---|
889 | static gboolean bee_irc_channel_chat_privmsg_cb(gpointer data, gint fd, b_input_condition cond) |
---|
890 | { |
---|
891 | irc_channel_t *ic = data; |
---|
892 | |
---|
893 | if (ic->data) { |
---|
894 | bee_chat_msg(ic->irc->b, ic->data, ic->pastebuf->str, 0); |
---|
895 | } |
---|
896 | |
---|
897 | g_string_free(ic->pastebuf, TRUE); |
---|
898 | ic->pastebuf = 0; |
---|
899 | ic->pastebuf_timer = 0; |
---|
900 | |
---|
901 | return FALSE; |
---|
902 | } |
---|
903 | |
---|
904 | static gboolean bee_irc_channel_chat_join(irc_channel_t *ic) |
---|
905 | { |
---|
906 | char *acc_s, *room; |
---|
907 | account_t *acc; |
---|
908 | |
---|
909 | if (strcmp(set_getstr(&ic->set, "chat_type"), "room") != 0) { |
---|
910 | return TRUE; |
---|
911 | } |
---|
912 | |
---|
913 | if ((acc_s = set_getstr(&ic->set, "account")) && |
---|
914 | (room = set_getstr(&ic->set, "room")) && |
---|
915 | (acc = account_get(ic->irc->b, acc_s)) && |
---|
916 | acc->ic && (acc->ic->flags & OPT_LOGGED_IN) && |
---|
917 | acc->prpl->chat_join) { |
---|
918 | char *nick; |
---|
919 | struct groupchat *gc; |
---|
920 | |
---|
921 | if (!(nick = set_getstr(&ic->set, "nick"))) { |
---|
922 | nick = ic->irc->user->nick; |
---|
923 | } |
---|
924 | |
---|
925 | ic->flags |= IRC_CHANNEL_CHAT_PICKME; |
---|
926 | gc = acc->prpl->chat_join(acc->ic, room, nick, NULL, &ic->set); |
---|
927 | ic->flags &= ~IRC_CHANNEL_CHAT_PICKME; |
---|
928 | |
---|
929 | if (!gc) { |
---|
930 | irc_send_num(ic->irc, 403, "%s :Error joining channel (check control channel?)", ic->name); |
---|
931 | } |
---|
932 | |
---|
933 | return FALSE; |
---|
934 | } else { |
---|
935 | irc_send_num(ic->irc, 403, "%s :Can't join channel, account offline?", ic->name); |
---|
936 | return FALSE; |
---|
937 | } |
---|
938 | } |
---|
939 | |
---|
940 | static gboolean bee_irc_channel_chat_part(irc_channel_t *ic, const char *msg) |
---|
941 | { |
---|
942 | struct groupchat *c = ic->data; |
---|
943 | |
---|
944 | if (c && c->ic->acc->prpl->chat_leave) { |
---|
945 | c->ic->acc->prpl->chat_leave(c); |
---|
946 | } |
---|
947 | |
---|
948 | if (!(ic->flags & IRC_CHANNEL_TEMP)) { |
---|
949 | /* Remove the reference. |
---|
950 | * We only need it for temp channels that are being freed */ |
---|
951 | ic->data = NULL; |
---|
952 | } |
---|
953 | |
---|
954 | return TRUE; |
---|
955 | } |
---|
956 | |
---|
957 | static gboolean bee_irc_channel_chat_topic(irc_channel_t *ic, const char *new) |
---|
958 | { |
---|
959 | struct groupchat *c = ic->data; |
---|
960 | |
---|
961 | if (c == NULL) { |
---|
962 | return FALSE; |
---|
963 | } |
---|
964 | |
---|
965 | if (c->ic->acc->prpl->chat_topic == NULL) { |
---|
966 | irc_send_num(ic->irc, 482, "%s :IM network does not support channel topics", ic->name); |
---|
967 | } else { |
---|
968 | /* TODO: Need more const goodness here, sigh */ |
---|
969 | char *topic = g_strdup(new); |
---|
970 | c->ic->acc->prpl->chat_topic(c, topic); |
---|
971 | g_free(topic); |
---|
972 | } |
---|
973 | |
---|
974 | /* Whatever happened, the IM module should ack the topic change. */ |
---|
975 | return FALSE; |
---|
976 | } |
---|
977 | |
---|
978 | static gboolean bee_irc_channel_chat_invite(irc_channel_t *ic, irc_user_t *iu) |
---|
979 | { |
---|
980 | struct groupchat *c = ic->data; |
---|
981 | bee_user_t *bu = iu->bu; |
---|
982 | |
---|
983 | if (bu == NULL) { |
---|
984 | return FALSE; |
---|
985 | } |
---|
986 | |
---|
987 | if (c) { |
---|
988 | if (iu->bu->ic != c->ic) { |
---|
989 | irc_send_num(ic->irc, 482, "%s :Can't mix different IM networks in one groupchat", ic->name); |
---|
990 | } else if (c->ic->acc->prpl->chat_invite) { |
---|
991 | c->ic->acc->prpl->chat_invite(c, iu->bu->handle, NULL); |
---|
992 | } else { |
---|
993 | irc_send_num(ic->irc, 482, "%s :IM protocol does not support room invitations", ic->name); |
---|
994 | } |
---|
995 | } else if (bu->ic->acc->prpl->chat_with && |
---|
996 | strcmp(set_getstr(&ic->set, "chat_type"), "groupchat") == 0) { |
---|
997 | ic->flags |= IRC_CHANNEL_CHAT_PICKME; |
---|
998 | iu->bu->ic->acc->prpl->chat_with(bu->ic, bu->handle); |
---|
999 | ic->flags &= ~IRC_CHANNEL_CHAT_PICKME; |
---|
1000 | } else { |
---|
1001 | irc_send_num(ic->irc, 482, "%s :IM protocol does not support room invitations", ic->name); |
---|
1002 | } |
---|
1003 | |
---|
1004 | return TRUE; |
---|
1005 | } |
---|
1006 | |
---|
1007 | static void bee_irc_channel_chat_kick(irc_channel_t *ic, irc_user_t *iu, const char *msg) |
---|
1008 | { |
---|
1009 | struct groupchat *c = ic->data; |
---|
1010 | bee_user_t *bu = iu->bu; |
---|
1011 | |
---|
1012 | if ((c == NULL) || (bu == NULL)) { |
---|
1013 | return; |
---|
1014 | } |
---|
1015 | |
---|
1016 | if (!c->ic->acc->prpl->chat_kick) { |
---|
1017 | irc_send_num(ic->irc, 482, "%s :IM protocol does not support room kicking", ic->name); |
---|
1018 | return; |
---|
1019 | } |
---|
1020 | |
---|
1021 | c->ic->acc->prpl->chat_kick(c, iu->bu->handle, msg); |
---|
1022 | } |
---|
1023 | |
---|
1024 | static char *set_eval_room_account(set_t *set, char *value); |
---|
1025 | static char *set_eval_chat_type(set_t *set, char *value); |
---|
1026 | |
---|
1027 | static gboolean bee_irc_channel_init(irc_channel_t *ic) |
---|
1028 | { |
---|
1029 | set_t *s; |
---|
1030 | |
---|
1031 | set_add(&ic->set, "account", NULL, set_eval_room_account, ic); |
---|
1032 | set_add(&ic->set, "chat_type", "groupchat", set_eval_chat_type, ic); |
---|
1033 | |
---|
1034 | s = set_add(&ic->set, "nick", NULL, NULL, ic); |
---|
1035 | s->flags |= SET_NULL_OK; |
---|
1036 | |
---|
1037 | set_add(&ic->set, "room", NULL, NULL, ic); |
---|
1038 | set_add(&ic->set, "translate_to_nicks", "true", set_eval_bool, ic); |
---|
1039 | |
---|
1040 | /* chat_type == groupchat */ |
---|
1041 | ic->flags |= IRC_CHANNEL_TEMP; |
---|
1042 | |
---|
1043 | return TRUE; |
---|
1044 | } |
---|
1045 | |
---|
1046 | static char *set_eval_room_account(set_t *set, char *value) |
---|
1047 | { |
---|
1048 | struct irc_channel *ic = set->data; |
---|
1049 | account_t *acc, *oa; |
---|
1050 | |
---|
1051 | if (!(acc = account_get(ic->irc->b, value))) { |
---|
1052 | return SET_INVALID; |
---|
1053 | } else if (!acc->prpl->chat_join && acc->prpl != &protocol_missing) { |
---|
1054 | irc_rootmsg(ic->irc, "Named chatrooms not supported on that account."); |
---|
1055 | return SET_INVALID; |
---|
1056 | } |
---|
1057 | |
---|
1058 | if (set->value && (oa = account_get(ic->irc->b, set->value)) && |
---|
1059 | oa->prpl->chat_free_settings) { |
---|
1060 | oa->prpl->chat_free_settings(oa, &ic->set); |
---|
1061 | } |
---|
1062 | |
---|
1063 | if (acc->prpl->chat_add_settings) { |
---|
1064 | acc->prpl->chat_add_settings(acc, &ic->set); |
---|
1065 | } |
---|
1066 | |
---|
1067 | return g_strdup(acc->tag); |
---|
1068 | } |
---|
1069 | |
---|
1070 | static char *set_eval_chat_type(set_t *set, char *value) |
---|
1071 | { |
---|
1072 | struct irc_channel *ic = set->data; |
---|
1073 | |
---|
1074 | if (strcmp(value, "groupchat") == 0) { |
---|
1075 | ic->flags |= IRC_CHANNEL_TEMP; |
---|
1076 | } else if (strcmp(value, "room") == 0) { |
---|
1077 | ic->flags &= ~IRC_CHANNEL_TEMP; |
---|
1078 | } else { |
---|
1079 | return NULL; |
---|
1080 | } |
---|
1081 | |
---|
1082 | return value; |
---|
1083 | } |
---|
1084 | |
---|
1085 | static gboolean bee_irc_channel_free(irc_channel_t *ic) |
---|
1086 | { |
---|
1087 | struct groupchat *c = ic->data; |
---|
1088 | |
---|
1089 | set_del(&ic->set, "account"); |
---|
1090 | set_del(&ic->set, "chat_type"); |
---|
1091 | set_del(&ic->set, "nick"); |
---|
1092 | set_del(&ic->set, "room"); |
---|
1093 | set_del(&ic->set, "translate_to_nicks"); |
---|
1094 | |
---|
1095 | ic->flags &= ~IRC_CHANNEL_TEMP; |
---|
1096 | |
---|
1097 | /* That one still points at this channel. Don't. */ |
---|
1098 | if (c) { |
---|
1099 | c->ui_data = NULL; |
---|
1100 | } |
---|
1101 | |
---|
1102 | return TRUE; |
---|
1103 | } |
---|
1104 | |
---|
1105 | const struct irc_channel_funcs irc_channel_im_chat_funcs = { |
---|
1106 | bee_irc_channel_chat_privmsg, |
---|
1107 | bee_irc_channel_chat_join, |
---|
1108 | bee_irc_channel_chat_part, |
---|
1109 | bee_irc_channel_chat_topic, |
---|
1110 | bee_irc_channel_chat_invite, |
---|
1111 | bee_irc_channel_chat_kick, |
---|
1112 | |
---|
1113 | bee_irc_channel_init, |
---|
1114 | bee_irc_channel_free, |
---|
1115 | }; |
---|
1116 | |
---|
1117 | |
---|
1118 | /* IM->IRC: File transfers */ |
---|
1119 | static file_transfer_t *bee_irc_ft_in_start(bee_t *bee, bee_user_t *bu, const char *file_name, size_t file_size) |
---|
1120 | { |
---|
1121 | return dccs_send_start(bu->ic, (irc_user_t *) bu->ui_data, file_name, file_size); |
---|
1122 | } |
---|
1123 | |
---|
1124 | static gboolean bee_irc_ft_out_start(struct im_connection *ic, file_transfer_t *ft) |
---|
1125 | { |
---|
1126 | return dccs_recv_start(ft); |
---|
1127 | } |
---|
1128 | |
---|
1129 | static void bee_irc_ft_close(struct im_connection *ic, file_transfer_t *ft) |
---|
1130 | { |
---|
1131 | return dcc_close(ft); |
---|
1132 | } |
---|
1133 | |
---|
1134 | static void bee_irc_ft_finished(struct im_connection *ic, file_transfer_t *file) |
---|
1135 | { |
---|
1136 | dcc_file_transfer_t *df = file->priv; |
---|
1137 | |
---|
1138 | if (file->bytes_transferred >= file->file_size) { |
---|
1139 | dcc_finish(file); |
---|
1140 | } else { |
---|
1141 | df->proto_finished = TRUE; |
---|
1142 | } |
---|
1143 | } |
---|
1144 | |
---|
1145 | static void bee_irc_log(bee_t *bee, const char *tag, const char *msg) |
---|
1146 | { |
---|
1147 | irc_t *irc = (irc_t *) bee->ui_data; |
---|
1148 | |
---|
1149 | irc_rootmsg(irc, "%s - %s", tag, msg); |
---|
1150 | } |
---|
1151 | |
---|
1152 | const struct bee_ui_funcs irc_ui_funcs = { |
---|
1153 | bee_irc_imc_connected, |
---|
1154 | bee_irc_imc_disconnected, |
---|
1155 | |
---|
1156 | bee_irc_user_new, |
---|
1157 | bee_irc_user_free, |
---|
1158 | bee_irc_user_fullname, |
---|
1159 | bee_irc_user_nick_hint, |
---|
1160 | bee_irc_user_group, |
---|
1161 | bee_irc_user_status, |
---|
1162 | bee_irc_user_msg, |
---|
1163 | bee_irc_user_typing, |
---|
1164 | bee_irc_user_action_response, |
---|
1165 | |
---|
1166 | bee_irc_chat_new, |
---|
1167 | bee_irc_chat_free, |
---|
1168 | bee_irc_chat_log, |
---|
1169 | bee_irc_chat_msg, |
---|
1170 | bee_irc_chat_add_user, |
---|
1171 | bee_irc_chat_remove_user, |
---|
1172 | bee_irc_chat_topic, |
---|
1173 | bee_irc_chat_name_hint, |
---|
1174 | bee_irc_chat_invite, |
---|
1175 | |
---|
1176 | bee_irc_ft_in_start, |
---|
1177 | bee_irc_ft_out_start, |
---|
1178 | bee_irc_ft_close, |
---|
1179 | bee_irc_ft_finished, |
---|
1180 | |
---|
1181 | bee_irc_log, |
---|
1182 | bee_irc_user_nick_change, |
---|
1183 | }; |
---|