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