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