[5ebff60] | 1 | /********************************************************************\ |
---|
[4be8239] | 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[0e788f5] | 4 | * Copyright 2002-2013 Wilmer van der Gaast and others * |
---|
[4be8239] | 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* The IRC-based UI - Representing (virtual) channels. */ |
---|
| 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 |
---|
[4be8239] | 24 | */ |
---|
| 25 | |
---|
| 26 | #include "bitlbee.h" |
---|
| 27 | |
---|
[5ebff60] | 28 | static char *set_eval_channel_type(set_t *set, char *value); |
---|
| 29 | static gint irc_channel_user_cmp(gconstpointer a_, gconstpointer b_); |
---|
[280c56a] | 30 | static const struct irc_channel_funcs control_channel_funcs; |
---|
[5a75d15] | 31 | |
---|
| 32 | extern const struct irc_channel_funcs irc_channel_im_chat_funcs; |
---|
[280c56a] | 33 | |
---|
[5ebff60] | 34 | irc_channel_t *irc_channel_new(irc_t *irc, const char *name) |
---|
[4be8239] | 35 | { |
---|
| 36 | irc_channel_t *ic; |
---|
[d4a4f1a] | 37 | set_t *s; |
---|
[5ebff60] | 38 | |
---|
| 39 | if (!irc_channel_name_ok(name) || irc_channel_by_name(irc, name)) { |
---|
[4be8239] | 40 | return NULL; |
---|
[5ebff60] | 41 | } |
---|
| 42 | |
---|
| 43 | ic = g_new0(irc_channel_t, 1); |
---|
[4be8239] | 44 | ic->irc = irc; |
---|
[5ebff60] | 45 | ic->name = g_strdup(name); |
---|
| 46 | strcpy(ic->mode, CMODE); |
---|
| 47 | |
---|
| 48 | irc_channel_add_user(ic, irc->root); |
---|
| 49 | |
---|
| 50 | irc->channels = g_slist_append(irc->channels, ic); |
---|
| 51 | |
---|
| 52 | set_add(&ic->set, "auto_join", "false", set_eval_bool, ic); |
---|
| 53 | |
---|
| 54 | s = set_add(&ic->set, "type", "control", set_eval_channel_type, ic); |
---|
[d4a4f1a] | 55 | s->flags |= SET_NOSAVE; /* Layer violation (XML format detail) */ |
---|
[5ebff60] | 56 | |
---|
| 57 | if (name[0] == '&') { |
---|
| 58 | set_setstr(&ic->set, "type", "control"); |
---|
| 59 | } else { /* if( name[0] == '#' ) */ |
---|
| 60 | set_setstr(&ic->set, "type", "chat"); |
---|
| 61 | } |
---|
| 62 | |
---|
[4be8239] | 63 | return ic; |
---|
| 64 | } |
---|
| 65 | |
---|
[5ebff60] | 66 | irc_channel_t *irc_channel_by_name(irc_t *irc, const char *name) |
---|
[b9e020a] | 67 | { |
---|
| 68 | GSList *l; |
---|
[5ebff60] | 69 | |
---|
| 70 | for (l = irc->channels; l; l = l->next) { |
---|
[b9e020a] | 71 | irc_channel_t *ic = l->data; |
---|
[5ebff60] | 72 | |
---|
| 73 | if (irc_channel_name_cmp(name, ic->name) == 0) { |
---|
[b9e020a] | 74 | return ic; |
---|
[5ebff60] | 75 | } |
---|
[b9e020a] | 76 | } |
---|
[5ebff60] | 77 | |
---|
[b9e020a] | 78 | return NULL; |
---|
| 79 | } |
---|
| 80 | |
---|
[5ebff60] | 81 | irc_channel_t *irc_channel_get(irc_t *irc, char *id) |
---|
[36562b0] | 82 | { |
---|
| 83 | irc_channel_t *ic, *ret = NULL; |
---|
| 84 | GSList *l; |
---|
| 85 | int nr; |
---|
[5ebff60] | 86 | |
---|
| 87 | if (sscanf(id, "%d", &nr) == 1 && nr < 1000) { |
---|
| 88 | for (l = irc->channels; l; l = l->next) { |
---|
[36562b0] | 89 | ic = l->data; |
---|
[5ebff60] | 90 | if ((nr--) == 0) { |
---|
[36562b0] | 91 | return ic; |
---|
[5ebff60] | 92 | } |
---|
[36562b0] | 93 | } |
---|
[5ebff60] | 94 | |
---|
[36562b0] | 95 | return NULL; |
---|
| 96 | } |
---|
[5ebff60] | 97 | |
---|
[36562b0] | 98 | /* Exact match first: Partial match only sucks if there's a channel |
---|
| 99 | #aa and #aabb */ |
---|
[5ebff60] | 100 | if ((ret = irc_channel_by_name(irc, id))) { |
---|
[36562b0] | 101 | return ret; |
---|
[5ebff60] | 102 | } |
---|
| 103 | |
---|
| 104 | for (l = irc->channels; l; l = l->next) { |
---|
[36562b0] | 105 | ic = l->data; |
---|
[5ebff60] | 106 | |
---|
| 107 | if (strstr(ic->name, id)) { |
---|
[36562b0] | 108 | /* Make sure it's a unique match. */ |
---|
[5ebff60] | 109 | if (!ret) { |
---|
[36562b0] | 110 | ret = ic; |
---|
[5ebff60] | 111 | } else { |
---|
[36562b0] | 112 | return NULL; |
---|
[5ebff60] | 113 | } |
---|
[36562b0] | 114 | } |
---|
| 115 | } |
---|
[5ebff60] | 116 | |
---|
[36562b0] | 117 | return ret; |
---|
| 118 | } |
---|
| 119 | |
---|
[5ebff60] | 120 | int irc_channel_free(irc_channel_t *ic) |
---|
[63a520b] | 121 | { |
---|
[7d4ffc2] | 122 | irc_t *irc; |
---|
[2fe5eb9] | 123 | GSList *l; |
---|
[5ebff60] | 124 | |
---|
| 125 | if (ic == NULL) { |
---|
[7d4ffc2] | 126 | return 0; |
---|
[5ebff60] | 127 | } |
---|
[7d4ffc2] | 128 | irc = ic->irc; |
---|
[5ebff60] | 129 | |
---|
| 130 | if (ic->flags & IRC_CHANNEL_JOINED) { |
---|
| 131 | irc_channel_del_user(ic, irc->user, IRC_CDU_KICK, "Cleaning up channel"); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | if (ic->f->_free) { |
---|
| 135 | ic->f->_free(ic); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | while (ic->set) { |
---|
| 139 | set_del(&ic->set, ic->set->key); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | irc->channels = g_slist_remove(irc->channels, ic); |
---|
| 143 | while (ic->users) { |
---|
| 144 | g_free(ic->users->data); |
---|
| 145 | ic->users = g_slist_remove(ic->users, ic->users->data); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | for (l = irc->users; l; l = l->next) { |
---|
[2fe5eb9] | 149 | irc_user_t *iu = l->data; |
---|
[5ebff60] | 150 | |
---|
| 151 | if (iu->last_channel == ic) { |
---|
[2fe5eb9] | 152 | iu->last_channel = irc->default_channel; |
---|
[5ebff60] | 153 | } |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | if (ic->pastebuf_timer) { |
---|
| 157 | b_event_remove(ic->pastebuf_timer); |
---|
[2fe5eb9] | 158 | } |
---|
[5ebff60] | 159 | |
---|
| 160 | g_free(ic->name); |
---|
| 161 | g_free(ic->topic); |
---|
| 162 | g_free(ic->topic_who); |
---|
| 163 | g_free(ic); |
---|
| 164 | |
---|
[63a520b] | 165 | return 1; |
---|
| 166 | } |
---|
| 167 | |
---|
[5ebff60] | 168 | struct irc_channel_free_data { |
---|
[ab6006c] | 169 | irc_t *irc; |
---|
| 170 | irc_channel_t *ic; |
---|
| 171 | char *name; |
---|
| 172 | }; |
---|
| 173 | |
---|
[5ebff60] | 174 | static gboolean irc_channel_free_callback(gpointer data, gint fd, b_input_condition cond) |
---|
[ab6006c] | 175 | { |
---|
| 176 | struct irc_channel_free_data *d = data; |
---|
[5ebff60] | 177 | |
---|
| 178 | if (g_slist_find(irc_connection_list, d->irc) && |
---|
| 179 | irc_channel_by_name(d->irc, d->name) == d->ic && |
---|
| 180 | !(d->ic->flags & IRC_CHANNEL_JOINED)) { |
---|
| 181 | irc_channel_free(d->ic); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | g_free(d->name); |
---|
| 185 | g_free(d); |
---|
[ab6006c] | 186 | return FALSE; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | /* Free the channel, but via the event loop, so after finishing whatever event |
---|
| 190 | we're currently handling. */ |
---|
[5ebff60] | 191 | void irc_channel_free_soon(irc_channel_t *ic) |
---|
[ab6006c] | 192 | { |
---|
[5ebff60] | 193 | struct irc_channel_free_data *d = g_new0(struct irc_channel_free_data, 1); |
---|
| 194 | |
---|
[ab6006c] | 195 | d->irc = ic->irc; |
---|
| 196 | d->ic = ic; |
---|
[5ebff60] | 197 | d->name = g_strdup(ic->name); |
---|
| 198 | |
---|
| 199 | b_timeout_add(0, irc_channel_free_callback, d); |
---|
[ab6006c] | 200 | } |
---|
| 201 | |
---|
[5ebff60] | 202 | static char *set_eval_channel_type(set_t *set, char *value) |
---|
[2b8473c] | 203 | { |
---|
| 204 | struct irc_channel *ic = set->data; |
---|
| 205 | const struct irc_channel_funcs *new; |
---|
[5ebff60] | 206 | |
---|
| 207 | if (strcmp(value, "control") == 0) { |
---|
[2b8473c] | 208 | new = &control_channel_funcs; |
---|
[5ebff60] | 209 | } else if (ic != ic->irc->default_channel && strcmp(value, "chat") == 0) { |
---|
[5a75d15] | 210 | new = &irc_channel_im_chat_funcs; |
---|
[5ebff60] | 211 | } else { |
---|
[2b8473c] | 212 | return SET_INVALID; |
---|
[5ebff60] | 213 | } |
---|
| 214 | |
---|
[2b8473c] | 215 | /* TODO: Return values. */ |
---|
[5ebff60] | 216 | if (ic->f && ic->f->_free) { |
---|
| 217 | ic->f->_free(ic); |
---|
| 218 | } |
---|
| 219 | |
---|
[2b8473c] | 220 | ic->f = new; |
---|
[5ebff60] | 221 | |
---|
| 222 | if (ic->f && ic->f->_init) { |
---|
| 223 | ic->f->_init(ic); |
---|
| 224 | } |
---|
| 225 | |
---|
[2b8473c] | 226 | return value; |
---|
| 227 | } |
---|
| 228 | |
---|
[5ebff60] | 229 | int irc_channel_add_user(irc_channel_t *ic, irc_user_t *iu) |
---|
[4be8239] | 230 | { |
---|
[e54112f] | 231 | irc_channel_user_t *icu; |
---|
[5ebff60] | 232 | |
---|
| 233 | if (irc_channel_has_user(ic, iu)) { |
---|
[4be8239] | 234 | return 0; |
---|
[5ebff60] | 235 | } |
---|
| 236 | |
---|
| 237 | icu = g_new0(irc_channel_user_t, 1); |
---|
[e54112f] | 238 | icu->iu = iu; |
---|
[5ebff60] | 239 | |
---|
| 240 | ic->users = g_slist_insert_sorted(ic->users, icu, irc_channel_user_cmp); |
---|
| 241 | |
---|
| 242 | irc_channel_update_ops(ic, set_getstr(&ic->irc->b->set, "ops")); |
---|
| 243 | |
---|
| 244 | if (iu == ic->irc->user || ic->flags & IRC_CHANNEL_JOINED) { |
---|
[4be8239] | 245 | ic->flags |= IRC_CHANNEL_JOINED; |
---|
[5ebff60] | 246 | irc_send_join(ic, iu); |
---|
[4be8239] | 247 | } |
---|
[5ebff60] | 248 | |
---|
[4be8239] | 249 | return 1; |
---|
| 250 | } |
---|
| 251 | |
---|
[5ebff60] | 252 | int irc_channel_del_user(irc_channel_t *ic, irc_user_t *iu, irc_channel_del_user_type_t type, const char *msg) |
---|
[4be8239] | 253 | { |
---|
[e54112f] | 254 | irc_channel_user_t *icu; |
---|
[5ebff60] | 255 | |
---|
| 256 | if (!(icu = irc_channel_has_user(ic, iu))) { |
---|
[4be8239] | 257 | return 0; |
---|
[5ebff60] | 258 | } |
---|
| 259 | |
---|
| 260 | ic->users = g_slist_remove(ic->users, icu); |
---|
| 261 | g_free(icu); |
---|
| 262 | |
---|
| 263 | if (!(ic->flags & IRC_CHANNEL_JOINED) || type == IRC_CDU_SILENT) { |
---|
| 264 | } |
---|
| 265 | /* Do nothing. The caller should promise it won't screw |
---|
| 266 | up state of the IRC client. :-) */ |
---|
| 267 | else if (type == IRC_CDU_PART) { |
---|
| 268 | irc_send_part(ic, iu, msg); |
---|
| 269 | } else if (type == IRC_CDU_KICK) { |
---|
| 270 | irc_send_kick(ic, iu, ic->irc->root, msg); |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | if (iu == ic->irc->user) { |
---|
[4be8239] | 274 | ic->flags &= ~IRC_CHANNEL_JOINED; |
---|
[5ebff60] | 275 | |
---|
| 276 | if (ic->irc->status & USTATUS_SHUTDOWN) { |
---|
[06f9548] | 277 | /* Don't do anything fancy when we're shutting down anyway. */ |
---|
[5ebff60] | 278 | } else if (ic->flags & IRC_CHANNEL_TEMP) { |
---|
| 279 | irc_channel_free_soon(ic); |
---|
| 280 | } else { |
---|
[9052bc1] | 281 | /* Flush userlist now. The user won't see it anyway. */ |
---|
[5ebff60] | 282 | while (ic->users) { |
---|
| 283 | g_free(ic->users->data); |
---|
| 284 | ic->users = g_slist_remove(ic->users, ic->users->data); |
---|
[9052bc1] | 285 | } |
---|
[5ebff60] | 286 | irc_channel_add_user(ic, ic->irc->root); |
---|
[9052bc1] | 287 | } |
---|
[1c40aa7] | 288 | } |
---|
[5ebff60] | 289 | |
---|
[4be8239] | 290 | return 1; |
---|
| 291 | } |
---|
| 292 | |
---|
[5ebff60] | 293 | irc_channel_user_t *irc_channel_has_user(irc_channel_t *ic, irc_user_t *iu) |
---|
[0b5cc72] | 294 | { |
---|
[e54112f] | 295 | GSList *l; |
---|
[5ebff60] | 296 | |
---|
| 297 | for (l = ic->users; l; l = l->next) { |
---|
[e54112f] | 298 | irc_channel_user_t *icu = l->data; |
---|
[5ebff60] | 299 | |
---|
| 300 | if (icu->iu == iu) { |
---|
[e54112f] | 301 | return icu; |
---|
[5ebff60] | 302 | } |
---|
[e54112f] | 303 | } |
---|
[5ebff60] | 304 | |
---|
[e54112f] | 305 | return NULL; |
---|
[0b5cc72] | 306 | } |
---|
| 307 | |
---|
[bb151f7] | 308 | /* Find a channel we're currently in, that currently has iu in it. */ |
---|
[5ebff60] | 309 | struct irc_channel *irc_channel_with_user(irc_t *irc, irc_user_t *iu) |
---|
[bb151f7] | 310 | { |
---|
| 311 | GSList *l; |
---|
[5ebff60] | 312 | |
---|
| 313 | for (l = irc->channels; l; l = l->next) { |
---|
[bb151f7] | 314 | irc_channel_t *ic = l->data; |
---|
[5ebff60] | 315 | |
---|
| 316 | if (strcmp(set_getstr(&ic->set, "type"), "control") != 0) { |
---|
[bb151f7] | 317 | continue; |
---|
[5ebff60] | 318 | } |
---|
| 319 | |
---|
| 320 | if ((ic->flags & IRC_CHANNEL_JOINED) && |
---|
| 321 | irc_channel_has_user(ic, iu)) { |
---|
[bb151f7] | 322 | return ic; |
---|
[5ebff60] | 323 | } |
---|
[bb151f7] | 324 | } |
---|
[5ebff60] | 325 | |
---|
[bb151f7] | 326 | /* If there was no match, try once more but just see if the user |
---|
| 327 | *would* be in the channel, i.e. if s/he were online. */ |
---|
[5ebff60] | 328 | if (iu->bu == NULL) { |
---|
[bb151f7] | 329 | return NULL; |
---|
[5ebff60] | 330 | } |
---|
| 331 | |
---|
| 332 | for (l = irc->channels; l; l = l->next) { |
---|
[bb151f7] | 333 | irc_channel_t *ic = l->data; |
---|
[5ebff60] | 334 | |
---|
| 335 | if (strcmp(set_getstr(&ic->set, "type"), "control") != 0) { |
---|
[bb151f7] | 336 | continue; |
---|
[5ebff60] | 337 | } |
---|
| 338 | |
---|
| 339 | if ((ic->flags & IRC_CHANNEL_JOINED) && |
---|
| 340 | irc_channel_wants_user(ic, iu)) { |
---|
[bb151f7] | 341 | return ic; |
---|
[5ebff60] | 342 | } |
---|
[bb151f7] | 343 | } |
---|
[5ebff60] | 344 | |
---|
[bb151f7] | 345 | return NULL; |
---|
| 346 | } |
---|
| 347 | |
---|
[5ebff60] | 348 | int irc_channel_set_topic(irc_channel_t *ic, const char *topic, const irc_user_t *iu) |
---|
[4be8239] | 349 | { |
---|
[5ebff60] | 350 | g_free(ic->topic); |
---|
| 351 | ic->topic = g_strdup(topic); |
---|
| 352 | |
---|
| 353 | g_free(ic->topic_who); |
---|
| 354 | if (iu) { |
---|
| 355 | ic->topic_who = g_strdup_printf("%s!%s@%s", iu->nick, iu->user, iu->host); |
---|
| 356 | } else { |
---|
[83e92bf] | 357 | ic->topic_who = NULL; |
---|
[5ebff60] | 358 | } |
---|
| 359 | |
---|
| 360 | ic->topic_time = time(NULL); |
---|
| 361 | |
---|
| 362 | if (ic->flags & IRC_CHANNEL_JOINED) { |
---|
| 363 | irc_send_topic(ic, TRUE); |
---|
| 364 | } |
---|
| 365 | |
---|
[4be8239] | 366 | return 1; |
---|
| 367 | } |
---|
[b919363] | 368 | |
---|
[5ebff60] | 369 | void irc_channel_user_set_mode(irc_channel_t *ic, irc_user_t *iu, irc_channel_user_flags_t flags) |
---|
[6a9d068] | 370 | { |
---|
[5ebff60] | 371 | irc_channel_user_t *icu = irc_channel_has_user(ic, iu); |
---|
| 372 | |
---|
| 373 | if (!icu || icu->flags == flags) { |
---|
[6a9d068] | 374 | return; |
---|
[5ebff60] | 375 | } |
---|
| 376 | |
---|
| 377 | if (ic->flags & IRC_CHANNEL_JOINED) { |
---|
| 378 | irc_send_channel_user_mode_diff(ic, iu, icu->flags, flags); |
---|
| 379 | } |
---|
| 380 | |
---|
[6a9d068] | 381 | icu->flags = flags; |
---|
| 382 | } |
---|
| 383 | |
---|
[5ebff60] | 384 | void irc_channel_set_mode(irc_channel_t *ic, const char *s) |
---|
[65016a6] | 385 | { |
---|
| 386 | irc_t *irc = ic->irc; |
---|
| 387 | char m[128], st = 1; |
---|
| 388 | const char *t; |
---|
| 389 | int i; |
---|
| 390 | char changes[512], *p, st2 = 2; |
---|
[5ebff60] | 391 | |
---|
| 392 | memset(m, 0, sizeof(m)); |
---|
| 393 | |
---|
| 394 | for (t = ic->mode; *t; t++) { |
---|
| 395 | if (*t < sizeof(m)) { |
---|
| 396 | m[(int) *t] = 1; |
---|
| 397 | } |
---|
| 398 | } |
---|
| 399 | |
---|
[65016a6] | 400 | p = changes; |
---|
[5ebff60] | 401 | for (t = s; *t; t++) { |
---|
| 402 | if (*t == '+' || *t == '-') { |
---|
[65016a6] | 403 | st = *t == '+'; |
---|
[5ebff60] | 404 | } else if (strchr(CMODES, *t)) { |
---|
| 405 | if (m[(int) *t] != st) { |
---|
| 406 | if (st != st2) { |
---|
[65016a6] | 407 | st2 = st, *p++ = st ? '+' : '-'; |
---|
[5ebff60] | 408 | } |
---|
[65016a6] | 409 | *p++ = *t; |
---|
| 410 | } |
---|
[5ebff60] | 411 | m[(int) *t] = st; |
---|
[65016a6] | 412 | } |
---|
| 413 | } |
---|
| 414 | *p = '\0'; |
---|
[5ebff60] | 415 | |
---|
| 416 | memset(ic->mode, 0, sizeof(ic->mode)); |
---|
| 417 | |
---|
| 418 | for (i = 'A'; i <= 'z' && strlen(ic->mode) < (sizeof(ic->mode) - 1); i++) { |
---|
| 419 | if (m[i]) { |
---|
[65016a6] | 420 | ic->mode[strlen(ic->mode)] = i; |
---|
[5ebff60] | 421 | } |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | if (*changes && (ic->flags & IRC_CHANNEL_JOINED)) { |
---|
| 425 | irc_write(irc, ":%s!%s@%s MODE %s :%s", irc->root->nick, |
---|
| 426 | irc->root->user, irc->root->host, ic->name, |
---|
| 427 | changes); |
---|
| 428 | } |
---|
[65016a6] | 429 | } |
---|
| 430 | |
---|
[5ebff60] | 431 | void irc_channel_auto_joins(irc_t *irc, account_t *acc) |
---|
[c8eeadd] | 432 | { |
---|
| 433 | GSList *l; |
---|
[5ebff60] | 434 | |
---|
| 435 | for (l = irc->channels; l; l = l->next) { |
---|
[c8eeadd] | 436 | irc_channel_t *ic = l->data; |
---|
[5ebff60] | 437 | gboolean aj = set_getbool(&ic->set, "auto_join"); |
---|
[c8eeadd] | 438 | char *type; |
---|
[5ebff60] | 439 | |
---|
| 440 | if (acc && |
---|
| 441 | (type = set_getstr(&ic->set, "chat_type")) && |
---|
| 442 | strcmp(type, "room") == 0) { |
---|
[c8eeadd] | 443 | /* Bit of an ugly special case: Handle chatrooms here, we |
---|
| 444 | can only auto-join them if their account is online. */ |
---|
| 445 | char *acc_s; |
---|
[5ebff60] | 446 | |
---|
| 447 | if (!aj || (ic->flags & IRC_CHANNEL_JOINED)) { |
---|
[c8eeadd] | 448 | /* Only continue if this one's marked as auto_join |
---|
| 449 | or if we're in it already. (Possible if the |
---|
| 450 | client auto-rejoined it before identyfing.) */ |
---|
| 451 | continue; |
---|
[5ebff60] | 452 | } else if (!(acc_s = set_getstr(&ic->set, "account"))) { |
---|
[c8eeadd] | 453 | continue; |
---|
[5ebff60] | 454 | } else if (account_get(irc->b, acc_s) != acc) { |
---|
[c8eeadd] | 455 | continue; |
---|
[5ebff60] | 456 | } else if (acc->ic == NULL || !(acc->ic->flags & OPT_LOGGED_IN)) { |
---|
[c8eeadd] | 457 | continue; |
---|
[5ebff60] | 458 | } else { |
---|
| 459 | ic->f->join(ic); |
---|
| 460 | } |
---|
| 461 | } else if (aj) { |
---|
| 462 | irc_channel_add_user(ic, irc->user); |
---|
[c8eeadd] | 463 | } |
---|
| 464 | } |
---|
| 465 | } |
---|
| 466 | |
---|
[5ebff60] | 467 | void irc_channel_printf(irc_channel_t *ic, char *format, ...) |
---|
[9893da3] | 468 | { |
---|
| 469 | va_list params; |
---|
| 470 | char *text; |
---|
[5ebff60] | 471 | |
---|
| 472 | va_start(params, format); |
---|
| 473 | text = g_strdup_vprintf(format, params); |
---|
| 474 | va_end(params); |
---|
| 475 | |
---|
| 476 | irc_send_msg(ic->irc->root, "PRIVMSG", ic->name, text, NULL); |
---|
| 477 | g_free(text); |
---|
[9893da3] | 478 | } |
---|
| 479 | |
---|
[5ebff60] | 480 | gboolean irc_channel_name_ok(const char *name_) |
---|
[b919363] | 481 | { |
---|
[5ebff60] | 482 | const unsigned char *name = (unsigned char *) name_; |
---|
[6b90431] | 483 | int i; |
---|
[5ebff60] | 484 | |
---|
| 485 | if (name_[0] == '\0') { |
---|
[a670aeb] | 486 | return FALSE; |
---|
[5ebff60] | 487 | } |
---|
| 488 | |
---|
[4c17d19] | 489 | /* Check if the first character is in CTYPES (#&) */ |
---|
[5ebff60] | 490 | if (strchr(CTYPES, name_[0]) == NULL) { |
---|
[4c17d19] | 491 | return FALSE; |
---|
[5ebff60] | 492 | } |
---|
| 493 | |
---|
[6b90431] | 494 | /* RFC 1459 keeps amazing me: While only a "few" chars are allowed |
---|
| 495 | in nicknames, channel names can be pretty much anything as long |
---|
| 496 | as they start with # or &. I'll be a little bit more strict and |
---|
| 497 | disallow all non-printable characters. */ |
---|
[5ebff60] | 498 | for (i = 1; name[i]; i++) { |
---|
| 499 | if (name[i] <= ' ' || name[i] == ',') { |
---|
[6b90431] | 500 | return FALSE; |
---|
[5ebff60] | 501 | } |
---|
| 502 | } |
---|
| 503 | |
---|
[6b90431] | 504 | return TRUE; |
---|
| 505 | } |
---|
| 506 | |
---|
[5ebff60] | 507 | void irc_channel_name_strip(char *name) |
---|
[134a02c] | 508 | { |
---|
| 509 | int i, j; |
---|
[5ebff60] | 510 | |
---|
| 511 | for (i = j = 0; name[i]; i++) { |
---|
| 512 | if (name[i] > ' ' && name[i] != ',') { |
---|
[134a02c] | 513 | name[j++] = name[i]; |
---|
[5ebff60] | 514 | } |
---|
| 515 | } |
---|
| 516 | |
---|
[134a02c] | 517 | name[j] = '\0'; |
---|
| 518 | } |
---|
| 519 | |
---|
[5ebff60] | 520 | int irc_channel_name_cmp(const char *a_, const char *b_) |
---|
[6b90431] | 521 | { |
---|
| 522 | static unsigned char case_map[256]; |
---|
[5ebff60] | 523 | const unsigned char *a = (unsigned char *) a_, *b = (unsigned char *) b_; |
---|
[6b90431] | 524 | int i; |
---|
[5ebff60] | 525 | |
---|
| 526 | if (case_map['A'] == '\0') { |
---|
| 527 | for (i = 33; i < 256; i++) { |
---|
| 528 | if (i != ',') { |
---|
[6b90431] | 529 | case_map[i] = i; |
---|
[5ebff60] | 530 | } |
---|
| 531 | } |
---|
| 532 | |
---|
| 533 | for (i = 0; i < 26; i++) { |
---|
| 534 | case_map['A' + i] = 'a' + i; |
---|
| 535 | } |
---|
| 536 | |
---|
[6b90431] | 537 | case_map['['] = '{'; |
---|
| 538 | case_map[']'] = '}'; |
---|
| 539 | case_map['~'] = '`'; |
---|
| 540 | case_map['\\'] = '|'; |
---|
| 541 | } |
---|
[5ebff60] | 542 | |
---|
| 543 | if (!irc_channel_name_ok(a_) || !irc_channel_name_ok(b_)) { |
---|
[6b90431] | 544 | return -1; |
---|
[5ebff60] | 545 | } |
---|
| 546 | |
---|
| 547 | for (i = 0; a[i] && b[i] && case_map[a[i]] && case_map[b[i]]; i++) { |
---|
| 548 | if (case_map[a[i]] == case_map[b[i]]) { |
---|
[6b90431] | 549 | continue; |
---|
[5ebff60] | 550 | } else { |
---|
[6b90431] | 551 | return case_map[a[i]] - case_map[b[i]]; |
---|
[5ebff60] | 552 | } |
---|
[6b90431] | 553 | } |
---|
[5ebff60] | 554 | |
---|
[6b90431] | 555 | return case_map[a[i]] - case_map[b[i]]; |
---|
[b919363] | 556 | } |
---|
[280c56a] | 557 | |
---|
[a9b1e0e] | 558 | gboolean irc_channel_is_unused(irc_t *irc, char *name) |
---|
[e3e2059] | 559 | { |
---|
| 560 | char *type, *chat_type; |
---|
| 561 | irc_channel_t *oic; |
---|
| 562 | |
---|
| 563 | if (!irc_channel_name_ok(name)) { |
---|
| 564 | return FALSE; |
---|
| 565 | } |
---|
| 566 | |
---|
[a9b1e0e] | 567 | if (!(oic = irc_channel_by_name(irc, name))) { |
---|
[e3e2059] | 568 | return TRUE; |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | type = set_getstr(&oic->set, "type"); |
---|
| 572 | chat_type = set_getstr(&oic->set, "chat_type"); |
---|
| 573 | |
---|
| 574 | if (type && chat_type && oic->data == FALSE && |
---|
| 575 | strcmp(type, "chat") == 0 && |
---|
| 576 | strcmp(chat_type, "groupchat") == 0) { |
---|
| 577 | /* There's a channel with this name already, but it looks |
---|
| 578 | like it's not in use yet. Most likely the IRC client |
---|
| 579 | rejoined the channel after a reconnect. Remove it so |
---|
| 580 | we can reuse its name. */ |
---|
| 581 | irc_channel_free(oic); |
---|
| 582 | return TRUE; |
---|
| 583 | } |
---|
| 584 | |
---|
| 585 | return FALSE; |
---|
| 586 | } |
---|
| 587 | |
---|
[a9b1e0e] | 588 | char *irc_channel_name_gen(irc_t *irc, const char *hint) |
---|
[e3e2059] | 589 | { |
---|
| 590 | char name[MAX_NICK_LENGTH + 1] = { 0 }; |
---|
| 591 | char *translit_name; |
---|
| 592 | gsize bytes_written; |
---|
| 593 | |
---|
| 594 | translit_name = g_convert_with_fallback(hint, -1, "ASCII//TRANSLIT", "UTF-8", "", NULL, &bytes_written, NULL); |
---|
| 595 | if (bytes_written > MAX_NICK_LENGTH) { |
---|
| 596 | translit_name[MAX_NICK_LENGTH] = '\0'; |
---|
| 597 | } |
---|
| 598 | |
---|
| 599 | name[0] = '#'; |
---|
| 600 | strncpy(name + 1, translit_name, MAX_NICK_LENGTH - 1); |
---|
| 601 | name[MAX_NICK_LENGTH] = '\0'; |
---|
| 602 | |
---|
| 603 | g_free(translit_name); |
---|
| 604 | |
---|
| 605 | irc_channel_name_strip(name); |
---|
| 606 | |
---|
[a9b1e0e] | 607 | if (set_getbool(&irc->b->set, "lcnicks")) { |
---|
| 608 | nick_lc(irc, name + 1); |
---|
[e3e2059] | 609 | } |
---|
| 610 | |
---|
[a9b1e0e] | 611 | while (!irc_channel_is_unused(irc, name)) { |
---|
[e3e2059] | 612 | underscore_dedupe(name); |
---|
| 613 | } |
---|
| 614 | |
---|
| 615 | return g_strdup(name); |
---|
| 616 | } |
---|
| 617 | |
---|
| 618 | gboolean irc_channel_name_hint(irc_channel_t *ic, const char *name) |
---|
| 619 | { |
---|
| 620 | irc_t *irc = ic->irc; |
---|
| 621 | char *full_name; |
---|
| 622 | |
---|
| 623 | /* Don't rename a channel if the user's in it already. */ |
---|
| 624 | if (ic->flags & IRC_CHANNEL_JOINED) { |
---|
| 625 | return FALSE; |
---|
| 626 | } |
---|
| 627 | |
---|
[a9b1e0e] | 628 | if (!(full_name = irc_channel_name_gen(irc, name))) { |
---|
[e3e2059] | 629 | return FALSE; |
---|
| 630 | } |
---|
| 631 | |
---|
| 632 | g_free(ic->name); |
---|
| 633 | ic->name = full_name; |
---|
| 634 | |
---|
| 635 | return TRUE; |
---|
| 636 | } |
---|
| 637 | |
---|
[5ebff60] | 638 | static gint irc_channel_user_cmp(gconstpointer a_, gconstpointer b_) |
---|
[e54112f] | 639 | { |
---|
| 640 | const irc_channel_user_t *a = a_, *b = b_; |
---|
[5ebff60] | 641 | |
---|
| 642 | return irc_user_cmp(a->iu, b->iu); |
---|
[e54112f] | 643 | } |
---|
| 644 | |
---|
[5ebff60] | 645 | void irc_channel_update_ops(irc_channel_t *ic, char *value) |
---|
[c5aefa4] | 646 | { |
---|
[5ebff60] | 647 | irc_channel_user_set_mode(ic, ic->irc->root, |
---|
| 648 | (strcmp(value, "both") == 0 || |
---|
| 649 | strcmp(value, "root") == 0) ? IRC_CHANNEL_USER_OP : 0); |
---|
| 650 | irc_channel_user_set_mode(ic, ic->irc->user, |
---|
| 651 | (strcmp(value, "both") == 0 || |
---|
| 652 | strcmp(value, "user") == 0) ? IRC_CHANNEL_USER_OP : 0); |
---|
[c5aefa4] | 653 | } |
---|
| 654 | |
---|
[5ebff60] | 655 | char *set_eval_irc_channel_ops(set_t *set, char *value) |
---|
[c5aefa4] | 656 | { |
---|
| 657 | irc_t *irc = set->data; |
---|
| 658 | GSList *l; |
---|
[5ebff60] | 659 | |
---|
| 660 | if (strcmp(value, "both") != 0 && strcmp(value, "none") != 0 && |
---|
| 661 | strcmp(value, "user") != 0 && strcmp(value, "root") != 0) { |
---|
[c5aefa4] | 662 | return SET_INVALID; |
---|
[5ebff60] | 663 | } |
---|
| 664 | |
---|
| 665 | for (l = irc->channels; l; l = l->next) { |
---|
| 666 | irc_channel_update_ops(l->data, value); |
---|
| 667 | } |
---|
| 668 | |
---|
[c5aefa4] | 669 | return value; |
---|
| 670 | } |
---|
| 671 | |
---|
[280c56a] | 672 | /* Channel-type dependent functions, for control channels: */ |
---|
[5ebff60] | 673 | static gboolean control_channel_privmsg(irc_channel_t *ic, const char *msg) |
---|
[280c56a] | 674 | { |
---|
[bce78c8] | 675 | irc_t *irc = ic->irc; |
---|
[f7ca587] | 676 | irc_user_t *iu; |
---|
[bce78c8] | 677 | const char *s; |
---|
[5ebff60] | 678 | |
---|
[bce78c8] | 679 | /* Scan for non-whitespace chars followed by a colon: */ |
---|
[5ebff60] | 680 | for (s = msg; *s && !g_ascii_isspace(*s) && *s != ':' && *s != ','; s++) { |
---|
| 681 | } |
---|
| 682 | |
---|
| 683 | if (*s == ':' || *s == ',') { |
---|
| 684 | char to[s - msg + 1]; |
---|
| 685 | |
---|
| 686 | memset(to, 0, sizeof(to)); |
---|
| 687 | strncpy(to, msg, s - msg); |
---|
| 688 | while (*(++s) && g_ascii_isspace(*s)) { |
---|
| 689 | } |
---|
[f7ca587] | 690 | msg = s; |
---|
[5ebff60] | 691 | |
---|
| 692 | if (!(iu = irc_user_by_name(irc, to))) { |
---|
| 693 | irc_channel_printf(ic, "User does not exist: %s", to); |
---|
| 694 | } else { |
---|
[f7ca587] | 695 | ic->last_target = iu; |
---|
[5ebff60] | 696 | } |
---|
| 697 | } else if (g_strcasecmp(set_getstr(&irc->b->set, "default_target"), "last") == 0 && |
---|
| 698 | ic->last_target && g_slist_find(irc->users, ic->last_target)) { |
---|
[f7ca587] | 699 | iu = ic->last_target; |
---|
[5ebff60] | 700 | } else { |
---|
[f7ca587] | 701 | iu = irc->root; |
---|
[5ebff60] | 702 | } |
---|
| 703 | |
---|
| 704 | if (iu && iu->f->privmsg) { |
---|
[f7ca587] | 705 | iu->last_channel = ic; |
---|
[5ebff60] | 706 | iu->f->privmsg(iu, msg); |
---|
[bce78c8] | 707 | } |
---|
[5ebff60] | 708 | |
---|
[280c56a] | 709 | return TRUE; |
---|
| 710 | } |
---|
| 711 | |
---|
[5ebff60] | 712 | static gboolean control_channel_invite(irc_channel_t *ic, irc_user_t *iu) |
---|
[46d215d] | 713 | { |
---|
| 714 | struct irc_control_channel *icc = ic->data; |
---|
| 715 | bee_user_t *bu = iu->bu; |
---|
[5ebff60] | 716 | |
---|
| 717 | if (bu == NULL) { |
---|
[46d215d] | 718 | return FALSE; |
---|
[5ebff60] | 719 | } |
---|
| 720 | |
---|
| 721 | if (icc->type != IRC_CC_TYPE_GROUP) { |
---|
| 722 | irc_send_num(ic->irc, 482, "%s :Invitations are only possible to fill_by=group channels", ic->name); |
---|
[46d215d] | 723 | return FALSE; |
---|
| 724 | } |
---|
[5ebff60] | 725 | |
---|
| 726 | bu->ic->acc->prpl->add_buddy(bu->ic, bu->handle, |
---|
| 727 | icc->group ? icc->group->name : NULL); |
---|
| 728 | |
---|
[46d215d] | 729 | return TRUE; |
---|
| 730 | } |
---|
| 731 | |
---|
[5ebff60] | 732 | static void control_channel_kick(irc_channel_t *ic, irc_user_t *iu, const char *msg) |
---|
[7821ee8] | 733 | { |
---|
| 734 | struct irc_control_channel *icc = ic->data; |
---|
| 735 | bee_user_t *bu = iu->bu; |
---|
[5ebff60] | 736 | |
---|
| 737 | if (bu == NULL) { |
---|
[7821ee8] | 738 | return; |
---|
[5ebff60] | 739 | } |
---|
| 740 | |
---|
| 741 | if (icc->type != IRC_CC_TYPE_GROUP) { |
---|
| 742 | irc_send_num(ic->irc, 482, "%s :Kicks are only possible to fill_by=group channels", ic->name); |
---|
[7821ee8] | 743 | return; |
---|
| 744 | } |
---|
[5ebff60] | 745 | |
---|
| 746 | bu->ic->acc->prpl->remove_buddy(bu->ic, bu->handle, |
---|
| 747 | icc->group ? icc->group->name : NULL); |
---|
[7821ee8] | 748 | } |
---|
| 749 | |
---|
[5ebff60] | 750 | static char *set_eval_by_account(set_t *set, char *value); |
---|
| 751 | static char *set_eval_fill_by(set_t *set, char *value); |
---|
| 752 | static char *set_eval_by_group(set_t *set, char *value); |
---|
| 753 | static char *set_eval_by_protocol(set_t *set, char *value); |
---|
| 754 | static char *set_eval_show_users(set_t *set, char *value); |
---|
[2b8473c] | 755 | |
---|
[5ebff60] | 756 | static gboolean control_channel_init(irc_channel_t *ic) |
---|
[9ac3ed1] | 757 | { |
---|
| 758 | struct irc_control_channel *icc; |
---|
[5ebff60] | 759 | |
---|
| 760 | set_add(&ic->set, "account", NULL, set_eval_by_account, ic); |
---|
| 761 | set_add(&ic->set, "fill_by", "all", set_eval_fill_by, ic); |
---|
| 762 | set_add(&ic->set, "group", NULL, set_eval_by_group, ic); |
---|
| 763 | set_add(&ic->set, "protocol", NULL, set_eval_by_protocol, ic); |
---|
| 764 | |
---|
[5a61bf59] | 765 | /* When changing the default, also change it below. */ |
---|
[5ebff60] | 766 | set_add(&ic->set, "show_users", "online+,special%,away", set_eval_show_users, ic); |
---|
| 767 | |
---|
| 768 | ic->data = icc = g_new0(struct irc_control_channel, 1); |
---|
[9ac3ed1] | 769 | icc->type = IRC_CC_TYPE_DEFAULT; |
---|
[5ebff60] | 770 | |
---|
[94d5da9c] | 771 | /* Have to run the evaluator to initialize icc->modes. */ |
---|
[5ebff60] | 772 | set_setstr(&ic->set, "show_users", "online+,special%,away"); |
---|
| 773 | |
---|
[65016a6] | 774 | /* For scripts that care. */ |
---|
[5ebff60] | 775 | irc_channel_set_mode(ic, "+C"); |
---|
| 776 | |
---|
[2b8473c] | 777 | return TRUE; |
---|
| 778 | } |
---|
| 779 | |
---|
[5ebff60] | 780 | static gboolean control_channel_join(irc_channel_t *ic) |
---|
[c8eeadd] | 781 | { |
---|
[5ebff60] | 782 | bee_irc_channel_update(ic->irc, ic, NULL); |
---|
| 783 | |
---|
[c8eeadd] | 784 | return TRUE; |
---|
| 785 | } |
---|
| 786 | |
---|
[5ebff60] | 787 | static char *set_eval_by_account(set_t *set, char *value) |
---|
[2b8473c] | 788 | { |
---|
| 789 | struct irc_channel *ic = set->data; |
---|
| 790 | struct irc_control_channel *icc = ic->data; |
---|
| 791 | account_t *acc; |
---|
[5ebff60] | 792 | |
---|
| 793 | if (!(acc = account_get(ic->irc->b, value))) { |
---|
[2b8473c] | 794 | return SET_INVALID; |
---|
[5ebff60] | 795 | } |
---|
| 796 | |
---|
[2b8473c] | 797 | icc->account = acc; |
---|
[5ebff60] | 798 | if ((icc->type & IRC_CC_TYPE_MASK) == IRC_CC_TYPE_ACCOUNT) { |
---|
| 799 | bee_irc_channel_update(ic->irc, ic, NULL); |
---|
| 800 | } |
---|
| 801 | |
---|
| 802 | return g_strdup(acc->tag); |
---|
[2b8473c] | 803 | } |
---|
| 804 | |
---|
[5ebff60] | 805 | static char *set_eval_fill_by(set_t *set, char *value) |
---|
[2b8473c] | 806 | { |
---|
| 807 | struct irc_channel *ic = set->data; |
---|
| 808 | struct irc_control_channel *icc = ic->data; |
---|
[a5c6ebd] | 809 | char *s; |
---|
[5ebff60] | 810 | |
---|
| 811 | icc->type &= ~(IRC_CC_TYPE_MASK | IRC_CC_TYPE_INVERT); |
---|
| 812 | |
---|
[a5c6ebd] | 813 | s = value; |
---|
[5ebff60] | 814 | if (s[0] == '!') { |
---|
[a5c6ebd] | 815 | icc->type |= IRC_CC_TYPE_INVERT; |
---|
[5ebff60] | 816 | s++; |
---|
[a5c6ebd] | 817 | } |
---|
[5ebff60] | 818 | |
---|
| 819 | if (strcmp(s, "all") == 0) { |
---|
[a5c6ebd] | 820 | icc->type |= IRC_CC_TYPE_DEFAULT; |
---|
[5ebff60] | 821 | } else if (strcmp(s, "rest") == 0) { |
---|
[a5c6ebd] | 822 | icc->type |= IRC_CC_TYPE_REST; |
---|
[5ebff60] | 823 | } else if (strcmp(s, "group") == 0) { |
---|
[a5c6ebd] | 824 | icc->type |= IRC_CC_TYPE_GROUP; |
---|
[5ebff60] | 825 | } else if (strcmp(s, "account") == 0) { |
---|
[a5c6ebd] | 826 | icc->type |= IRC_CC_TYPE_ACCOUNT; |
---|
[5ebff60] | 827 | } else if (strcmp(s, "protocol") == 0) { |
---|
[a5c6ebd] | 828 | icc->type |= IRC_CC_TYPE_PROTOCOL; |
---|
[5ebff60] | 829 | } else { |
---|
[2b8473c] | 830 | return SET_INVALID; |
---|
[5ebff60] | 831 | } |
---|
| 832 | |
---|
| 833 | bee_irc_channel_update(ic->irc, ic, NULL); |
---|
[2b8473c] | 834 | return value; |
---|
| 835 | } |
---|
| 836 | |
---|
[5ebff60] | 837 | static char *set_eval_by_group(set_t *set, char *value) |
---|
[2b8473c] | 838 | { |
---|
| 839 | struct irc_channel *ic = set->data; |
---|
| 840 | struct irc_control_channel *icc = ic->data; |
---|
[5ebff60] | 841 | |
---|
| 842 | icc->group = bee_group_by_name(ic->irc->b, value, TRUE); |
---|
| 843 | if ((icc->type & IRC_CC_TYPE_MASK) == IRC_CC_TYPE_GROUP) { |
---|
| 844 | bee_irc_channel_update(ic->irc, ic, NULL); |
---|
| 845 | } |
---|
| 846 | |
---|
| 847 | return g_strdup(icc->group->name); |
---|
[2b8473c] | 848 | } |
---|
| 849 | |
---|
[5ebff60] | 850 | static char *set_eval_by_protocol(set_t *set, char *value) |
---|
[7a6ba50] | 851 | { |
---|
| 852 | struct irc_channel *ic = set->data; |
---|
| 853 | struct irc_control_channel *icc = ic->data; |
---|
| 854 | struct prpl *prpl; |
---|
[5ebff60] | 855 | |
---|
| 856 | if (!(prpl = find_protocol(value))) { |
---|
[7a6ba50] | 857 | return SET_INVALID; |
---|
[5ebff60] | 858 | } |
---|
| 859 | |
---|
[7a6ba50] | 860 | icc->protocol = prpl; |
---|
[5ebff60] | 861 | if ((icc->type & IRC_CC_TYPE_MASK) == IRC_CC_TYPE_PROTOCOL) { |
---|
| 862 | bee_irc_channel_update(ic->irc, ic, NULL); |
---|
| 863 | } |
---|
| 864 | |
---|
[7a6ba50] | 865 | return value; |
---|
| 866 | } |
---|
| 867 | |
---|
[5ebff60] | 868 | static char *set_eval_show_users(set_t *set, char *value) |
---|
[94d5da9c] | 869 | { |
---|
| 870 | struct irc_channel *ic = set->data; |
---|
| 871 | struct irc_control_channel *icc = ic->data; |
---|
[5ebff60] | 872 | char **parts = g_strsplit(value, ",", 0), **part; |
---|
[7b8238d] | 873 | char modes[5]; |
---|
[5ebff60] | 874 | |
---|
| 875 | memset(modes, 0, 5); |
---|
| 876 | for (part = parts; *part; part++) { |
---|
[94d5da9c] | 877 | char last, modechar = IRC_CHANNEL_USER_NONE; |
---|
[5ebff60] | 878 | |
---|
| 879 | if (**part == '\0') { |
---|
[94d5da9c] | 880 | goto fail; |
---|
[5ebff60] | 881 | } |
---|
| 882 | |
---|
| 883 | last = (*part)[strlen(*part + 1)]; |
---|
| 884 | if (last == '+') { |
---|
[94d5da9c] | 885 | modechar = IRC_CHANNEL_USER_VOICE; |
---|
[5ebff60] | 886 | } else if (last == '%') { |
---|
[94d5da9c] | 887 | modechar = IRC_CHANNEL_USER_HALFOP; |
---|
[5ebff60] | 888 | } else if (last == '@') { |
---|
[94d5da9c] | 889 | modechar = IRC_CHANNEL_USER_OP; |
---|
[5ebff60] | 890 | } |
---|
| 891 | |
---|
| 892 | if (strncmp(*part, "offline", 7) == 0) { |
---|
[94d5da9c] | 893 | modes[0] = modechar; |
---|
[5ebff60] | 894 | } else if (strncmp(*part, "away", 4) == 0) { |
---|
[94d5da9c] | 895 | modes[1] = modechar; |
---|
[5ebff60] | 896 | } else if (strncmp(*part, "special", 7) == 0) { |
---|
[94d5da9c] | 897 | modes[2] = modechar; |
---|
[5ebff60] | 898 | } else if (strncmp(*part, "online", 6) == 0) { |
---|
[7b8238d] | 899 | modes[3] = modechar; |
---|
[5ebff60] | 900 | } else { |
---|
[94d5da9c] | 901 | goto fail; |
---|
[5ebff60] | 902 | } |
---|
[94d5da9c] | 903 | } |
---|
[5ebff60] | 904 | memcpy(icc->modes, modes, 5); |
---|
| 905 | bee_irc_channel_update(ic->irc, ic, NULL); |
---|
| 906 | |
---|
| 907 | g_strfreev(parts); |
---|
[94d5da9c] | 908 | return value; |
---|
[5ebff60] | 909 | |
---|
[94d5da9c] | 910 | fail: |
---|
[5ebff60] | 911 | g_strfreev(parts); |
---|
| 912 | return SET_INVALID; |
---|
[94d5da9c] | 913 | } |
---|
| 914 | |
---|
[ac2717b] | 915 | /* Figure out if a channel is supposed to have the user, assuming s/he is |
---|
| 916 | online or otherwise also selected by the show_users setting. Only works |
---|
| 917 | for control channels, but does *not* check if this channel is of that |
---|
| 918 | type. Beware! */ |
---|
[5ebff60] | 919 | gboolean irc_channel_wants_user(irc_channel_t *ic, irc_user_t *iu) |
---|
[ac2717b] | 920 | { |
---|
| 921 | struct irc_control_channel *icc = ic->data; |
---|
[a5c6ebd] | 922 | gboolean ret = FALSE; |
---|
[5ebff60] | 923 | |
---|
| 924 | if (iu->bu == NULL) { |
---|
[ac2717b] | 925 | return FALSE; |
---|
[5ebff60] | 926 | } |
---|
| 927 | |
---|
| 928 | switch (icc->type & IRC_CC_TYPE_MASK) { |
---|
[ac2717b] | 929 | case IRC_CC_TYPE_GROUP: |
---|
[a5c6ebd] | 930 | ret = iu->bu->group == icc->group; |
---|
| 931 | break; |
---|
[ac2717b] | 932 | case IRC_CC_TYPE_ACCOUNT: |
---|
[a5c6ebd] | 933 | ret = iu->bu->ic->acc == icc->account; |
---|
| 934 | break; |
---|
[ac2717b] | 935 | case IRC_CC_TYPE_PROTOCOL: |
---|
[a5c6ebd] | 936 | ret = iu->bu->ic->acc->prpl == icc->protocol; |
---|
| 937 | break; |
---|
[ac2717b] | 938 | case IRC_CC_TYPE_DEFAULT: |
---|
| 939 | default: |
---|
[a5c6ebd] | 940 | ret = TRUE; |
---|
| 941 | break; |
---|
[ac2717b] | 942 | } |
---|
[5ebff60] | 943 | |
---|
| 944 | if (icc->type & IRC_CC_TYPE_INVERT) { |
---|
[a5c6ebd] | 945 | ret = !ret; |
---|
[5ebff60] | 946 | } |
---|
| 947 | |
---|
[a5c6ebd] | 948 | return ret; |
---|
[ac2717b] | 949 | } |
---|
| 950 | |
---|
[5ebff60] | 951 | static gboolean control_channel_free(irc_channel_t *ic) |
---|
[2b8473c] | 952 | { |
---|
| 953 | struct irc_control_channel *icc = ic->data; |
---|
[5ebff60] | 954 | |
---|
| 955 | set_del(&ic->set, "account"); |
---|
| 956 | set_del(&ic->set, "fill_by"); |
---|
| 957 | set_del(&ic->set, "group"); |
---|
| 958 | set_del(&ic->set, "protocol"); |
---|
| 959 | set_del(&ic->set, "show_users"); |
---|
| 960 | |
---|
| 961 | g_free(icc); |
---|
[2b8473c] | 962 | ic->data = NULL; |
---|
[5ebff60] | 963 | |
---|
[65016a6] | 964 | /* For scripts that care. */ |
---|
[5ebff60] | 965 | irc_channel_set_mode(ic, "-C"); |
---|
| 966 | |
---|
[9ac3ed1] | 967 | return TRUE; |
---|
| 968 | } |
---|
| 969 | |
---|
[280c56a] | 970 | static const struct irc_channel_funcs control_channel_funcs = { |
---|
| 971 | control_channel_privmsg, |
---|
[c8eeadd] | 972 | control_channel_join, |
---|
[9ac3ed1] | 973 | NULL, |
---|
| 974 | NULL, |
---|
[46d215d] | 975 | control_channel_invite, |
---|
[7821ee8] | 976 | control_channel_kick, |
---|
[5ebff60] | 977 | |
---|
[9ac3ed1] | 978 | control_channel_init, |
---|
[2b8473c] | 979 | control_channel_free, |
---|
[280c56a] | 980 | }; |
---|