[5ebff60] | 1 | /********************************************************************\ |
---|
[b7d3cc34] | 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[0e788f5] | 4 | * Copyright 2002-2013 Wilmer van der Gaast and others * |
---|
[b7d3cc34] | 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* User manager (root) commands */ |
---|
| 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 |
---|
[b7d3cc34] | 24 | */ |
---|
| 25 | |
---|
| 26 | #define BITLBEE_CORE |
---|
| 27 | #include "commands.h" |
---|
| 28 | #include "bitlbee.h" |
---|
| 29 | #include "help.h" |
---|
[f1c2b21] | 30 | #include "ipc.h" |
---|
[b7d3cc34] | 31 | |
---|
[5ebff60] | 32 | void root_command_string(irc_t *irc, char *command) |
---|
[7e563ed] | 33 | { |
---|
[5ebff60] | 34 | root_command(irc, split_command_parts(command, 0)); |
---|
[7e563ed] | 35 | } |
---|
| 36 | |
---|
[5ebff60] | 37 | #define MIN_ARGS(x, y ...) \ |
---|
[3b99524] | 38 | do \ |
---|
| 39 | { \ |
---|
[07054a5] | 40 | int blaat; \ |
---|
[5ebff60] | 41 | for (blaat = 0; blaat <= x; blaat++) { \ |
---|
| 42 | if (cmd[blaat] == NULL) \ |
---|
[3b99524] | 43 | { \ |
---|
[5ebff60] | 44 | irc_rootmsg(irc, "Not enough parameters given (need %d).", x); \ |
---|
[3b99524] | 45 | return y; \ |
---|
[5ebff60] | 46 | } } \ |
---|
| 47 | } while (0) |
---|
[3b99524] | 48 | |
---|
[5ebff60] | 49 | void root_command(irc_t *irc, char *cmd[]) |
---|
| 50 | { |
---|
[6c56f42] | 51 | int i, len; |
---|
[5ebff60] | 52 | |
---|
| 53 | if (!cmd[0]) { |
---|
[f73b969] | 54 | return; |
---|
[5ebff60] | 55 | } |
---|
| 56 | |
---|
| 57 | len = strlen(cmd[0]); |
---|
| 58 | for (i = 0; root_commands[i].command; i++) { |
---|
| 59 | if (g_strncasecmp(root_commands[i].command, cmd[0], len) == 0) { |
---|
| 60 | if (root_commands[i + 1].command && |
---|
| 61 | g_strncasecmp(root_commands[i + 1].command, cmd[0], len) == 0) { |
---|
| 62 | /* Only match on the first letters if the match is unique. */ |
---|
| 63 | break; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | MIN_ARGS(root_commands[i].required_parameters); |
---|
| 67 | |
---|
| 68 | root_commands[i].execute(irc, cmd); |
---|
[f73b969] | 69 | return; |
---|
[7e563ed] | 70 | } |
---|
[5ebff60] | 71 | } |
---|
| 72 | |
---|
| 73 | irc_rootmsg(irc, "Unknown command: %s. Please use \x02help commands\x02 to get a list of available commands.", |
---|
| 74 | cmd[0]); |
---|
[7e563ed] | 75 | } |
---|
| 76 | |
---|
[5ebff60] | 77 | static void cmd_help(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 78 | { |
---|
| 79 | char param[80]; |
---|
| 80 | int i; |
---|
| 81 | char *s; |
---|
[5ebff60] | 82 | |
---|
| 83 | memset(param, 0, sizeof(param)); |
---|
| 84 | for (i = 1; (cmd[i] != NULL && (strlen(param) < (sizeof(param) - 1))); i++) { |
---|
| 85 | if (i != 1) { // prepend space except for the first parameter |
---|
[b7d3cc34] | 86 | strcat(param, " "); |
---|
[5ebff60] | 87 | } |
---|
| 88 | strncat(param, cmd[i], sizeof(param) - strlen(param) - 1); |
---|
[b7d3cc34] | 89 | } |
---|
| 90 | |
---|
[5ebff60] | 91 | s = help_get(&(global.help), param); |
---|
| 92 | if (!s) { |
---|
| 93 | s = help_get(&(global.help), ""); |
---|
[b7d3cc34] | 94 | } |
---|
[5ebff60] | 95 | |
---|
| 96 | if (s) { |
---|
| 97 | irc_rootmsg(irc, "%s", s); |
---|
| 98 | g_free(s); |
---|
| 99 | } else { |
---|
| 100 | irc_rootmsg(irc, "Error opening helpfile."); |
---|
[b7d3cc34] | 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
[5ebff60] | 104 | static void cmd_account(irc_t *irc, char **cmd); |
---|
| 105 | static void bitlbee_whatsnew(irc_t *irc); |
---|
[90bbb0e] | 106 | |
---|
[5ebff60] | 107 | static void cmd_identify(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 108 | { |
---|
[92cb8c4] | 109 | storage_status_t status; |
---|
| 110 | gboolean load = TRUE; |
---|
| 111 | char *password = cmd[1]; |
---|
[5ebff60] | 112 | |
---|
| 113 | if (irc->status & USTATUS_IDENTIFIED) { |
---|
| 114 | irc_rootmsg(irc, "You're already logged in."); |
---|
[e9cf291] | 115 | return; |
---|
| 116 | } |
---|
[5ebff60] | 117 | |
---|
| 118 | if (cmd[1] == NULL) { |
---|
| 119 | } else if (strncmp(cmd[1], "-no", 3) == 0) { |
---|
[92cb8c4] | 120 | load = FALSE; |
---|
| 121 | password = cmd[2]; |
---|
[5ebff60] | 122 | if (password == NULL) { |
---|
[fda194f] | 123 | irc->status |= OPER_HACK_IDENTIFY_NOLOAD; |
---|
[5ebff60] | 124 | } |
---|
| 125 | } else if (strncmp(cmd[1], "-force", 6) == 0) { |
---|
[92cb8c4] | 126 | password = cmd[2]; |
---|
[5ebff60] | 127 | if (password == NULL) { |
---|
[fda194f] | 128 | irc->status |= OPER_HACK_IDENTIFY_FORCE; |
---|
[5ebff60] | 129 | } |
---|
| 130 | } else if (irc->b->accounts != NULL) { |
---|
| 131 | irc_rootmsg(irc, |
---|
| 132 | "You're trying to identify yourself, but already have " |
---|
| 133 | "at least one IM account set up. " |
---|
| 134 | "Use \x02identify -noload\x02 or \x02identify -force\x02 " |
---|
| 135 | "instead (see \x02help identify\x02)."); |
---|
[92cb8c4] | 136 | return; |
---|
| 137 | } |
---|
[5ebff60] | 138 | |
---|
| 139 | if (password == NULL) { |
---|
| 140 | irc_rootmsg(irc, "About to identify, use /OPER to enter the password"); |
---|
[060d066] | 141 | irc->status |= OPER_HACK_IDENTIFY; |
---|
| 142 | return; |
---|
[92cb8c4] | 143 | } |
---|
[5ebff60] | 144 | |
---|
[8e6ecfe] | 145 | status = auth_check_pass(irc, irc->user->nick, password); |
---|
| 146 | if (load && (status == STORAGE_OK)) { |
---|
[5ebff60] | 147 | status = storage_load(irc, password); |
---|
| 148 | } |
---|
| 149 | |
---|
[09adf08] | 150 | switch (status) { |
---|
| 151 | case STORAGE_INVALID_PASSWORD: |
---|
[5ebff60] | 152 | irc_rootmsg(irc, "Incorrect password"); |
---|
[09adf08] | 153 | break; |
---|
| 154 | case STORAGE_NO_SUCH_USER: |
---|
[5ebff60] | 155 | irc_rootmsg(irc, "The nick is (probably) not registered"); |
---|
[09adf08] | 156 | break; |
---|
| 157 | case STORAGE_OK: |
---|
[5ebff60] | 158 | irc_rootmsg(irc, "Password accepted%s", |
---|
| 159 | load ? ", settings and accounts loaded" : ""); |
---|
[3183c21] | 160 | irc->status |= USTATUS_IDENTIFIED; |
---|
[5ebff60] | 161 | irc_umode_set(irc, "+R", 1); |
---|
| 162 | |
---|
[58b63de6] | 163 | if (irc->caps & CAP_SASL) { |
---|
| 164 | irc_user_t *iu = irc->user; |
---|
| 165 | irc_send_num(irc, 900, "%s!%s@%s %s :You are now logged in as %s", |
---|
| 166 | iu->nick, iu->user, iu->host, iu->nick, iu->nick); |
---|
| 167 | } |
---|
| 168 | |
---|
[5ebff60] | 169 | bitlbee_whatsnew(irc); |
---|
| 170 | |
---|
[e92c4f4] | 171 | /* The following code is a bit hairy now. With takeover |
---|
| 172 | support, we shouldn't immediately auto_connect in case |
---|
| 173 | we're going to offer taking over an existing session. |
---|
| 174 | Do it in 200ms since that should give the parent process |
---|
| 175 | enough time to come back to us. */ |
---|
[5ebff60] | 176 | if (load) { |
---|
| 177 | irc_channel_auto_joins(irc, NULL); |
---|
| 178 | if (!set_getbool(&irc->default_channel->set, "auto_join")) { |
---|
| 179 | irc_channel_del_user(irc->default_channel, irc->user, |
---|
| 180 | IRC_CDU_PART, "auto_join disabled " |
---|
| 181 | "for this channel."); |
---|
| 182 | } |
---|
| 183 | if (set_getbool(&irc->b->set, "auto_connect")) { |
---|
| 184 | irc->login_source_id = b_timeout_add(200, |
---|
| 185 | cmd_identify_finish, irc); |
---|
| 186 | } |
---|
[6c2404e] | 187 | } |
---|
[5ebff60] | 188 | |
---|
[e92c4f4] | 189 | /* If ipc_child_identify() returns FALSE, it means we're |
---|
| 190 | already sure that there's no takeover target (only |
---|
| 191 | possible in 1-process daemon mode). Start auto_connect |
---|
| 192 | immediately. */ |
---|
[5ebff60] | 193 | if (!ipc_child_identify(irc) && load) { |
---|
| 194 | cmd_identify_finish(irc, 0, 0); |
---|
| 195 | } |
---|
| 196 | |
---|
[09adf08] | 197 | break; |
---|
[c121f89] | 198 | case STORAGE_OTHER_ERROR: |
---|
[09adf08] | 199 | default: |
---|
[5ebff60] | 200 | irc_rootmsg(irc, "Unknown error while loading configuration"); |
---|
[09adf08] | 201 | break; |
---|
[b7d3cc34] | 202 | } |
---|
| 203 | } |
---|
| 204 | |
---|
[5ebff60] | 205 | gboolean cmd_identify_finish(gpointer data, gint fd, b_input_condition cond) |
---|
[6c2404e] | 206 | { |
---|
| 207 | char *account_on[] = { "account", "on", NULL }; |
---|
| 208 | irc_t *irc = data; |
---|
[5ebff60] | 209 | |
---|
| 210 | if (set_getbool(&irc->b->set, "auto_connect")) { |
---|
| 211 | cmd_account(irc, account_on); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | b_event_remove(irc->login_source_id); |
---|
[f545372] | 215 | irc->login_source_id = -1; |
---|
[6c2404e] | 216 | return FALSE; |
---|
| 217 | } |
---|
| 218 | |
---|
[5ebff60] | 219 | static void cmd_register(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 220 | { |
---|
[8d93b4a] | 221 | char s[16]; |
---|
[5ebff60] | 222 | |
---|
| 223 | if (global.conf->authmode == AUTHMODE_REGISTERED) { |
---|
| 224 | irc_rootmsg(irc, "This server does not allow registering new accounts"); |
---|
[f73b969] | 225 | return; |
---|
[b7d3cc34] | 226 | } |
---|
[5ebff60] | 227 | |
---|
| 228 | if (cmd[1] == NULL) { |
---|
| 229 | irc_rootmsg(irc, "About to register, use /OPER to enter the password"); |
---|
[060d066] | 230 | irc->status |= OPER_HACK_REGISTER; |
---|
| 231 | return; |
---|
| 232 | } |
---|
[1ee6c18] | 233 | |
---|
[5ebff60] | 234 | switch (storage_save(irc, cmd[1], FALSE)) { |
---|
| 235 | case STORAGE_ALREADY_EXISTS: |
---|
| 236 | irc_rootmsg(irc, "Nick is already registered"); |
---|
| 237 | break; |
---|
[a1f17d4] | 238 | |
---|
[5ebff60] | 239 | case STORAGE_OK: |
---|
| 240 | irc_rootmsg(irc, "Account successfully created"); |
---|
| 241 | irc_setpass(irc, cmd[1]); |
---|
| 242 | irc->status |= USTATUS_IDENTIFIED; |
---|
| 243 | irc_umode_set(irc, "+R", 1); |
---|
| 244 | |
---|
[58b63de6] | 245 | if (irc->caps & CAP_SASL) { |
---|
| 246 | irc_user_t *iu = irc->user; |
---|
| 247 | irc_send_num(irc, 900, "%s!%s@%s %s :You are now logged in as %s", |
---|
| 248 | iu->nick, iu->user, iu->host, iu->nick, iu->nick); |
---|
| 249 | } |
---|
| 250 | |
---|
[5ebff60] | 251 | /* Set this var now, or anyone who logs in to his/her |
---|
| 252 | newly created account for the first time gets the |
---|
| 253 | whatsnew story. */ |
---|
| 254 | g_snprintf(s, sizeof(s), "%d", BITLBEE_VERSION_CODE); |
---|
| 255 | set_setstr(&irc->b->set, "last_version", s); |
---|
| 256 | break; |
---|
| 257 | |
---|
| 258 | default: |
---|
| 259 | irc_rootmsg(irc, "Error registering"); |
---|
| 260 | break; |
---|
[b7d3cc34] | 261 | } |
---|
| 262 | } |
---|
| 263 | |
---|
[5ebff60] | 264 | static void cmd_drop(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 265 | { |
---|
[a1f17d4] | 266 | storage_status_t status; |
---|
[5ebff60] | 267 | |
---|
[8e6ecfe] | 268 | status = auth_check_pass(irc, irc->user->nick, cmd[1]); |
---|
| 269 | if (status == STORAGE_OK) { |
---|
| 270 | status = storage_remove(irc->user->nick); |
---|
| 271 | } |
---|
| 272 | |
---|
[a1f17d4] | 273 | switch (status) { |
---|
| 274 | case STORAGE_NO_SUCH_USER: |
---|
[5ebff60] | 275 | irc_rootmsg(irc, "That account does not exist"); |
---|
[f73b969] | 276 | break; |
---|
[a1f17d4] | 277 | case STORAGE_INVALID_PASSWORD: |
---|
[5ebff60] | 278 | irc_rootmsg(irc, "Password invalid"); |
---|
[f73b969] | 279 | break; |
---|
[a1f17d4] | 280 | case STORAGE_OK: |
---|
[5ebff60] | 281 | irc_setpass(irc, NULL); |
---|
[79e826a] | 282 | irc->status &= ~USTATUS_IDENTIFIED; |
---|
[5ebff60] | 283 | irc_umode_set(irc, "-R", 1); |
---|
| 284 | irc_rootmsg(irc, "Account `%s' removed", irc->user->nick); |
---|
[f73b969] | 285 | break; |
---|
[a1f17d4] | 286 | default: |
---|
[5ebff60] | 287 | irc_rootmsg(irc, "Error: `%d'", status); |
---|
[f73b969] | 288 | break; |
---|
[b7d3cc34] | 289 | } |
---|
| 290 | } |
---|
[1f92a58] | 291 | |
---|
[5ebff60] | 292 | static void cmd_save(irc_t *irc, char **cmd) |
---|
[1f92a58] | 293 | { |
---|
[5ebff60] | 294 | if ((irc->status & USTATUS_IDENTIFIED) == 0) { |
---|
| 295 | irc_rootmsg(irc, "Please create an account first (see \x02help register\x02)"); |
---|
| 296 | } else if (storage_save(irc, NULL, TRUE) == STORAGE_OK) { |
---|
| 297 | irc_rootmsg(irc, "Configuration saved"); |
---|
| 298 | } else { |
---|
| 299 | irc_rootmsg(irc, "Configuration could not be saved!"); |
---|
| 300 | } |
---|
[1f92a58] | 301 | } |
---|
[b7d3cc34] | 302 | |
---|
[5ebff60] | 303 | static void cmd_showset(irc_t *irc, set_t **head, char *key) |
---|
[f3579fd] | 304 | { |
---|
[09d4922] | 305 | set_t *set; |
---|
[f536a99] | 306 | char *val; |
---|
[5ebff60] | 307 | |
---|
| 308 | if ((val = set_getstr(head, key))) { |
---|
| 309 | irc_rootmsg(irc, "%s = `%s'", key, val); |
---|
| 310 | } else if (!(set = set_find(head, key))) { |
---|
| 311 | irc_rootmsg(irc, "Setting `%s' does not exist.", key); |
---|
| 312 | if (*head == irc->b->set) { |
---|
| 313 | irc_rootmsg(irc, "It might be an account or channel setting. " |
---|
| 314 | "See \x02help account set\x02 and \x02help channel set\x02."); |
---|
| 315 | } |
---|
| 316 | } else if (set->flags & SET_PASSWORD) { |
---|
| 317 | irc_rootmsg(irc, "%s = `********' (hidden)", key); |
---|
| 318 | } else { |
---|
| 319 | irc_rootmsg(irc, "%s is empty", key); |
---|
[5613af7] | 320 | } |
---|
[f3579fd] | 321 | } |
---|
| 322 | |
---|
[5ebff60] | 323 | typedef set_t** (*cmd_set_findhead)(irc_t*, char*); |
---|
| 324 | typedef int (*cmd_set_checkflags)(irc_t*, set_t *set); |
---|
[e7bc722] | 325 | |
---|
[5ebff60] | 326 | static int cmd_set_real(irc_t *irc, char **cmd, set_t **head, cmd_set_checkflags checkflags) |
---|
[e7bc722] | 327 | { |
---|
[e907683] | 328 | char *set_name = NULL, *value = NULL; |
---|
| 329 | gboolean del = FALSE; |
---|
[5ebff60] | 330 | |
---|
| 331 | if (cmd[1] && g_strncasecmp(cmd[1], "-del", 4) == 0) { |
---|
| 332 | MIN_ARGS(2, 0); |
---|
[e907683] | 333 | set_name = cmd[2]; |
---|
| 334 | del = TRUE; |
---|
[5ebff60] | 335 | } else { |
---|
[e907683] | 336 | set_name = cmd[1]; |
---|
| 337 | value = cmd[2]; |
---|
[e7bc722] | 338 | } |
---|
[5ebff60] | 339 | |
---|
| 340 | if (set_name && (value || del)) { |
---|
| 341 | set_t *s = set_find(head, set_name); |
---|
[e7bc722] | 342 | int st; |
---|
[5ebff60] | 343 | |
---|
[e41ba05] | 344 | if (s && s->flags & SET_LOCKED) { |
---|
| 345 | irc_rootmsg(irc, "This setting can not be changed"); |
---|
| 346 | return 0; |
---|
| 347 | } |
---|
[5ebff60] | 348 | if (s && checkflags && checkflags(irc, s) == 0) { |
---|
[e7bc722] | 349 | return 0; |
---|
[5ebff60] | 350 | } |
---|
| 351 | |
---|
| 352 | if (del) { |
---|
| 353 | st = set_reset(head, set_name); |
---|
| 354 | } else { |
---|
| 355 | st = set_setstr(head, set_name, value); |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | if (set_getstr(head, set_name) == NULL && |
---|
| 359 | set_find(head, set_name)) { |
---|
[e907683] | 360 | /* This happens when changing the passwd, for example. |
---|
| 361 | Showing these msgs instead gives slightly clearer |
---|
| 362 | feedback. */ |
---|
[5ebff60] | 363 | if (st) { |
---|
| 364 | irc_rootmsg(irc, "Setting changed successfully"); |
---|
| 365 | } else { |
---|
| 366 | irc_rootmsg(irc, "Failed to change setting"); |
---|
| 367 | } |
---|
| 368 | } else { |
---|
| 369 | cmd_showset(irc, head, set_name); |
---|
[e7bc722] | 370 | } |
---|
[5ebff60] | 371 | } else if (set_name) { |
---|
| 372 | cmd_showset(irc, head, set_name); |
---|
| 373 | } else { |
---|
[e7bc722] | 374 | set_t *s = *head; |
---|
[5ebff60] | 375 | while (s) { |
---|
| 376 | if (set_isvisible(s)) { |
---|
| 377 | cmd_showset(irc, &s, s->key); |
---|
| 378 | } |
---|
[e7bc722] | 379 | s = s->next; |
---|
| 380 | } |
---|
| 381 | } |
---|
[5ebff60] | 382 | |
---|
[e7bc722] | 383 | return 1; |
---|
| 384 | } |
---|
| 385 | |
---|
[5ebff60] | 386 | static int cmd_account_set_checkflags(irc_t *irc, set_t *s) |
---|
[c05eb5b] | 387 | { |
---|
| 388 | account_t *a = s->data; |
---|
[5ebff60] | 389 | |
---|
| 390 | if (a->ic && s && s->flags & ACC_SET_OFFLINE_ONLY) { |
---|
| 391 | irc_rootmsg(irc, "This setting can only be changed when the account is %s-line", "off"); |
---|
[c05eb5b] | 392 | return 0; |
---|
[5ebff60] | 393 | } else if (!a->ic && s && s->flags & ACC_SET_ONLINE_ONLY) { |
---|
| 394 | irc_rootmsg(irc, "This setting can only be changed when the account is %s-line", "on"); |
---|
[c05eb5b] | 395 | return 0; |
---|
[3ac6d9f] | 396 | } else if (a->flags & ACC_FLAG_LOCKED && s && s->flags & ACC_SET_LOCKABLE) { |
---|
| 397 | irc_rootmsg(irc, "This setting can not be changed for locked accounts"); |
---|
| 398 | return 0; |
---|
[c05eb5b] | 399 | } |
---|
[5ebff60] | 400 | |
---|
[c05eb5b] | 401 | return 1; |
---|
| 402 | } |
---|
| 403 | |
---|
[5ebff60] | 404 | static void cmd_account(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 405 | { |
---|
| 406 | account_t *a; |
---|
[c7eb771] | 407 | int len; |
---|
[5ebff60] | 408 | |
---|
| 409 | if (global.conf->authmode == AUTHMODE_REGISTERED && !(irc->status & USTATUS_IDENTIFIED)) { |
---|
| 410 | irc_rootmsg(irc, "This server only accepts registered users"); |
---|
[f73b969] | 411 | return; |
---|
[b7d3cc34] | 412 | } |
---|
[5ebff60] | 413 | |
---|
| 414 | len = strlen(cmd[1]); |
---|
| 415 | |
---|
| 416 | if (len >= 1 && g_strncasecmp(cmd[1], "add", len) == 0) { |
---|
[7b23afd] | 417 | struct prpl *prpl; |
---|
[5ebff60] | 418 | |
---|
| 419 | MIN_ARGS(3); |
---|
| 420 | |
---|
[446a23e] | 421 | if (!global.conf->allow_account_add) { |
---|
| 422 | irc_rootmsg(irc, "This server does not allow adding new accounts"); |
---|
| 423 | return; |
---|
| 424 | } |
---|
| 425 | |
---|
[5ebff60] | 426 | if (cmd[4] == NULL) { |
---|
| 427 | for (a = irc->b->accounts; a; a = a->next) { |
---|
| 428 | if (strcmp(a->pass, PASSWORD_PENDING) == 0) { |
---|
| 429 | irc_rootmsg(irc, "Enter password for account %s " |
---|
| 430 | "first (use /OPER)", a->tag); |
---|
[9564e55] | 431 | return; |
---|
| 432 | } |
---|
[5ebff60] | 433 | } |
---|
| 434 | |
---|
[35987a1] | 435 | irc->status |= OPER_HACK_ACCOUNT_PASSWORD; |
---|
[da60f28] | 436 | } |
---|
[5ebff60] | 437 | |
---|
| 438 | prpl = find_protocol(cmd[2]); |
---|
| 439 | |
---|
| 440 | if (prpl == NULL) { |
---|
[b4f496e] | 441 | char *msg = explain_unknown_protocol(cmd[2]); |
---|
| 442 | irc_rootmsg(irc, "Unknown protocol"); |
---|
[a81e6a0] | 443 | irc_rootmsg(irc, "%s", msg); |
---|
[b4f496e] | 444 | g_free(msg); |
---|
[f73b969] | 445 | return; |
---|
[b7d3cc34] | 446 | } |
---|
[5ebff60] | 447 | |
---|
| 448 | for (a = irc->b->accounts; a; a = a->next) { |
---|
| 449 | if (a->prpl == prpl && prpl->handle_cmp(a->user, cmd[3]) == 0) { |
---|
| 450 | irc_rootmsg(irc, "Warning: You already have an account with " |
---|
| 451 | "protocol `%s' and username `%s'. Are you accidentally " |
---|
| 452 | "trying to add it twice?", prpl->name, cmd[3]); |
---|
| 453 | } |
---|
| 454 | } |
---|
| 455 | |
---|
| 456 | a = account_add(irc->b, prpl, cmd[3], cmd[4] ? cmd[4] : PASSWORD_PENDING); |
---|
| 457 | if (cmd[5]) { |
---|
| 458 | irc_rootmsg(irc, "Warning: Passing a servername/other flags to `account add' " |
---|
| 459 | "is now deprecated. Use `account set' instead."); |
---|
| 460 | set_setstr(&a->set, "server", cmd[5]); |
---|
[30ce1ce] | 461 | } |
---|
[5ebff60] | 462 | |
---|
| 463 | irc_rootmsg(irc, "Account successfully added with tag %s", a->tag); |
---|
| 464 | |
---|
| 465 | if (cmd[4] == NULL) { |
---|
| 466 | set_t *oauth = set_find(&a->set, "oauth"); |
---|
| 467 | if (oauth && bool2int(set_value(oauth))) { |
---|
[ce199b7] | 468 | *a->pass = '\0'; |
---|
[5ebff60] | 469 | irc_rootmsg(irc, "No need to enter a password for this " |
---|
| 470 | "account since it's using OAuth"); |
---|
[9f03c47] | 471 | } else if (prpl->options & PRPL_OPT_NO_PASSWORD) { |
---|
| 472 | *a->pass = '\0'; |
---|
| 473 | } else if (prpl->options & PRPL_OPT_PASSWORD_OPTIONAL) { |
---|
| 474 | *a->pass = '\0'; |
---|
| 475 | irc_rootmsg(irc, "Passwords are optional for this account. " |
---|
| 476 | "If you wish to enter the password with /OPER, do " |
---|
| 477 | "account %s set -del password", a->tag); |
---|
[5ebff60] | 478 | } else { |
---|
| 479 | irc_rootmsg(irc, "You can now use the /OPER command to " |
---|
| 480 | "enter the password"); |
---|
| 481 | if (oauth) { |
---|
| 482 | irc_rootmsg(irc, "Alternatively, enable OAuth if " |
---|
| 483 | "the account supports it: account %s " |
---|
| 484 | "set oauth on", a->tag); |
---|
| 485 | } |
---|
[ce199b7] | 486 | } |
---|
[9f03c47] | 487 | } else if (prpl->options & PRPL_OPT_NO_PASSWORD) { |
---|
| 488 | irc_rootmsg(irc, "Note: this account doesn't use password for login"); |
---|
[ce199b7] | 489 | } |
---|
[5ebff60] | 490 | |
---|
[e907683] | 491 | return; |
---|
[5ebff60] | 492 | } else if (len >= 1 && g_strncasecmp(cmd[1], "list", len) == 0) { |
---|
[b7d3cc34] | 493 | int i = 0; |
---|
[5ebff60] | 494 | |
---|
| 495 | if (strchr(irc->umode, 'b')) { |
---|
| 496 | irc_rootmsg(irc, "Account list:"); |
---|
| 497 | } |
---|
| 498 | |
---|
| 499 | for (a = irc->b->accounts; a; a = a->next) { |
---|
[5a8afc3] | 500 | char *con = NULL, *protocol = NULL; |
---|
[5ebff60] | 501 | |
---|
| 502 | if (a->ic && (a->ic->flags & OPT_LOGGED_IN)) { |
---|
[b7d3cc34] | 503 | con = " (connected)"; |
---|
[5ebff60] | 504 | } else if (a->ic) { |
---|
[b7d3cc34] | 505 | con = " (connecting)"; |
---|
[5ebff60] | 506 | } else if (a->reconnect) { |
---|
[b7d3cc34] | 507 | con = " (awaiting reconnect)"; |
---|
[5ebff60] | 508 | } else { |
---|
[b7d3cc34] | 509 | con = ""; |
---|
[5ebff60] | 510 | } |
---|
| 511 | |
---|
[5a8afc3] | 512 | if (a->prpl == &protocol_missing) { |
---|
| 513 | protocol = g_strdup_printf("%s (missing!)", set_getstr(&a->set, "_protocol_name")); |
---|
| 514 | } else { |
---|
| 515 | protocol = g_strdup(a->prpl->name); |
---|
| 516 | } |
---|
| 517 | |
---|
| 518 | irc_rootmsg(irc, "%2d (%s): %s, %s%s", i, a->tag, protocol, a->user, con); |
---|
| 519 | g_free(protocol); |
---|
[5ebff60] | 520 | |
---|
| 521 | i++; |
---|
[b7d3cc34] | 522 | } |
---|
[5ebff60] | 523 | irc_rootmsg(irc, "End of account list"); |
---|
| 524 | |
---|
[e907683] | 525 | return; |
---|
[5ebff60] | 526 | } else if (cmd[2]) { |
---|
[e907683] | 527 | /* Try the following two only if cmd[2] == NULL */ |
---|
[5ebff60] | 528 | } else if (len >= 2 && g_strncasecmp(cmd[1], "on", len) == 0) { |
---|
| 529 | if (irc->b->accounts) { |
---|
| 530 | irc_rootmsg(irc, "Trying to get all accounts connected..."); |
---|
| 531 | |
---|
| 532 | for (a = irc->b->accounts; a; a = a->next) { |
---|
[5a8afc3] | 533 | if (!a->ic && a->auto_connect && a->prpl != &protocol_missing) { |
---|
[5ebff60] | 534 | if (strcmp(a->pass, PASSWORD_PENDING) == 0) { |
---|
| 535 | irc_rootmsg(irc, "Enter password for account %s " |
---|
| 536 | "first (use /OPER)", a->tag); |
---|
| 537 | } else { |
---|
| 538 | account_on(irc->b, a); |
---|
| 539 | } |
---|
[9564e55] | 540 | } |
---|
[5ebff60] | 541 | } |
---|
| 542 | } else { |
---|
| 543 | irc_rootmsg(irc, "No accounts known. Use `account add' to add one."); |
---|
[b7d3cc34] | 544 | } |
---|
[5ebff60] | 545 | |
---|
[e907683] | 546 | return; |
---|
[5ebff60] | 547 | } else if (len >= 2 && g_strncasecmp(cmd[1], "off", len) == 0) { |
---|
| 548 | irc_rootmsg(irc, "Deactivating all active (re)connections..."); |
---|
| 549 | |
---|
| 550 | for (a = irc->b->accounts; a; a = a->next) { |
---|
| 551 | if (a->ic) { |
---|
| 552 | account_off(irc->b, a); |
---|
| 553 | } else if (a->reconnect) { |
---|
| 554 | cancel_auto_reconnect(a); |
---|
| 555 | } |
---|
[e907683] | 556 | } |
---|
[5ebff60] | 557 | |
---|
[e907683] | 558 | return; |
---|
| 559 | } |
---|
[5ebff60] | 560 | |
---|
| 561 | MIN_ARGS(2); |
---|
| 562 | len = strlen(cmd[2]); |
---|
| 563 | |
---|
[e907683] | 564 | /* At least right now, don't accept on/off/set/del as account IDs even |
---|
| 565 | if they're a proper match, since people not familiar with the new |
---|
| 566 | syntax yet may get a confusing/nasty surprise. */ |
---|
[5ebff60] | 567 | if (g_strcasecmp(cmd[1], "on") == 0 || |
---|
| 568 | g_strcasecmp(cmd[1], "off") == 0 || |
---|
| 569 | g_strcasecmp(cmd[1], "set") == 0 || |
---|
| 570 | g_strcasecmp(cmd[1], "del") == 0 || |
---|
| 571 | (a = account_get(irc->b, cmd[1])) == NULL) { |
---|
| 572 | irc_rootmsg(irc, "Could not find account `%s'.", cmd[1]); |
---|
| 573 | |
---|
[e907683] | 574 | return; |
---|
| 575 | } |
---|
[5ebff60] | 576 | |
---|
| 577 | if (len >= 1 && g_strncasecmp(cmd[2], "del", len) == 0) { |
---|
[3ac6d9f] | 578 | if (a->flags & ACC_FLAG_LOCKED) { |
---|
| 579 | irc_rootmsg(irc, "Account is locked, can't delete"); |
---|
| 580 | } |
---|
| 581 | else if (a->ic) { |
---|
[5ebff60] | 582 | irc_rootmsg(irc, "Account is still logged in, can't delete"); |
---|
| 583 | } else { |
---|
| 584 | account_del(irc->b, a); |
---|
| 585 | irc_rootmsg(irc, "Account deleted"); |
---|
[e907683] | 586 | } |
---|
[5ebff60] | 587 | } else if (len >= 2 && g_strncasecmp(cmd[2], "on", len) == 0) { |
---|
| 588 | if (a->ic) { |
---|
| 589 | irc_rootmsg(irc, "Account already online"); |
---|
| 590 | } else if (strcmp(a->pass, PASSWORD_PENDING) == 0) { |
---|
| 591 | irc_rootmsg(irc, "Enter password for account %s " |
---|
| 592 | "first (use /OPER)", a->tag); |
---|
[5a8afc3] | 593 | } else if (a->prpl == &protocol_missing) { |
---|
| 594 | char *proto = set_getstr(&a->set, "_protocol_name"); |
---|
| 595 | char *msg = explain_unknown_protocol(proto); |
---|
| 596 | irc_rootmsg(irc, "Unknown protocol `%s'", proto); |
---|
[a81e6a0] | 597 | irc_rootmsg(irc, "%s", msg); |
---|
[5a8afc3] | 598 | g_free(msg); |
---|
[5ebff60] | 599 | } else { |
---|
| 600 | account_on(irc->b, a); |
---|
[e907683] | 601 | } |
---|
[5ebff60] | 602 | } else if (len >= 2 && g_strncasecmp(cmd[2], "off", len) == 0) { |
---|
| 603 | if (a->ic) { |
---|
| 604 | account_off(irc->b, a); |
---|
| 605 | } else if (a->reconnect) { |
---|
| 606 | cancel_auto_reconnect(a); |
---|
| 607 | irc_rootmsg(irc, "Reconnect cancelled"); |
---|
| 608 | } else { |
---|
| 609 | irc_rootmsg(irc, "Account already offline"); |
---|
[e907683] | 610 | } |
---|
[5ebff60] | 611 | } else if (len >= 1 && g_strncasecmp(cmd[2], "set", len) == 0) { |
---|
| 612 | cmd_set_real(irc, cmd + 2, &a->set, cmd_account_set_checkflags); |
---|
| 613 | } else { |
---|
| 614 | irc_rootmsg(irc, |
---|
| 615 | "Unknown command: %s [...] %s. Please use \x02help commands\x02 to get a list of available commands.", "account", |
---|
| 616 | cmd[2]); |
---|
[b7d3cc34] | 617 | } |
---|
| 618 | } |
---|
| 619 | |
---|
[5ebff60] | 620 | static void cmd_channel(irc_t *irc, char **cmd) |
---|
[c133d4b8] | 621 | { |
---|
| 622 | irc_channel_t *ic; |
---|
[c7eb771] | 623 | int len; |
---|
[5ebff60] | 624 | |
---|
| 625 | len = strlen(cmd[1]); |
---|
| 626 | |
---|
| 627 | if (len >= 1 && g_strncasecmp(cmd[1], "list", len) == 0) { |
---|
[36562b0] | 628 | GSList *l; |
---|
| 629 | int i = 0; |
---|
[5ebff60] | 630 | |
---|
| 631 | if (strchr(irc->umode, 'b')) { |
---|
| 632 | irc_rootmsg(irc, "Channel list:"); |
---|
| 633 | } |
---|
| 634 | |
---|
| 635 | for (l = irc->channels; l; l = l->next) { |
---|
[36562b0] | 636 | irc_channel_t *ic = l->data; |
---|
[5ebff60] | 637 | |
---|
| 638 | irc_rootmsg(irc, "%2d. %s, %s channel%s", i, ic->name, |
---|
| 639 | set_getstr(&ic->set, "type"), |
---|
| 640 | ic->flags & IRC_CHANNEL_JOINED ? " (joined)" : ""); |
---|
| 641 | |
---|
| 642 | i++; |
---|
[36562b0] | 643 | } |
---|
[5ebff60] | 644 | irc_rootmsg(irc, "End of channel list"); |
---|
| 645 | |
---|
[e907683] | 646 | return; |
---|
[36562b0] | 647 | } |
---|
[5ebff60] | 648 | |
---|
| 649 | if ((ic = irc_channel_get(irc, cmd[1])) == NULL) { |
---|
[4f22a68c] | 650 | /* If this doesn't match any channel, maybe this is the short |
---|
| 651 | syntax (only works when used inside a channel). */ |
---|
[5ebff60] | 652 | if ((ic = irc->root->last_channel) && |
---|
| 653 | (len = strlen(cmd[1])) && |
---|
| 654 | g_strncasecmp(cmd[1], "set", len) == 0) { |
---|
| 655 | cmd_set_real(irc, cmd + 1, &ic->set, NULL); |
---|
| 656 | } else { |
---|
| 657 | irc_rootmsg(irc, "Could not find channel `%s'", cmd[1]); |
---|
| 658 | } |
---|
| 659 | |
---|
[e907683] | 660 | return; |
---|
| 661 | } |
---|
[5ebff60] | 662 | |
---|
| 663 | MIN_ARGS(2); |
---|
| 664 | len = strlen(cmd[2]); |
---|
| 665 | |
---|
| 666 | if (len >= 1 && g_strncasecmp(cmd[2], "set", len) == 0) { |
---|
| 667 | cmd_set_real(irc, cmd + 2, &ic->set, NULL); |
---|
| 668 | } else if (len >= 1 && g_strncasecmp(cmd[2], "del", len) == 0) { |
---|
| 669 | if (!(ic->flags & IRC_CHANNEL_JOINED) && |
---|
| 670 | ic != ic->irc->default_channel) { |
---|
| 671 | irc_rootmsg(irc, "Channel %s deleted.", ic->name); |
---|
| 672 | irc_channel_free(ic); |
---|
| 673 | } else { |
---|
| 674 | irc_rootmsg(irc, "Couldn't remove channel (main channel %s or " |
---|
| 675 | "channels you're still in cannot be deleted).", |
---|
| 676 | irc->default_channel->name); |
---|
[a4d920b] | 677 | } |
---|
[5ebff60] | 678 | } else { |
---|
| 679 | irc_rootmsg(irc, |
---|
| 680 | "Unknown command: %s [...] %s. Please use \x02help commands\x02 to get a list of available commands.", "channel", |
---|
| 681 | cmd[1]); |
---|
[c133d4b8] | 682 | } |
---|
| 683 | } |
---|
| 684 | |
---|
[5ebff60] | 685 | static void cmd_add(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 686 | { |
---|
| 687 | account_t *a; |
---|
[f0cb961] | 688 | int add_on_server = 1; |
---|
[06eef80] | 689 | char *handle = NULL, *s; |
---|
[5ebff60] | 690 | |
---|
| 691 | if (g_strcasecmp(cmd[1], "-tmp") == 0) { |
---|
| 692 | MIN_ARGS(3); |
---|
[f0cb961] | 693 | add_on_server = 0; |
---|
[5ebff60] | 694 | cmd++; |
---|
[f8de26f] | 695 | } |
---|
[5ebff60] | 696 | |
---|
| 697 | if (!(a = account_get(irc->b, cmd[1]))) { |
---|
| 698 | irc_rootmsg(irc, "Invalid account"); |
---|
[f73b969] | 699 | return; |
---|
[5ebff60] | 700 | } else if (!(a->ic && (a->ic->flags & OPT_LOGGED_IN))) { |
---|
| 701 | irc_rootmsg(irc, "That account is not on-line"); |
---|
[f73b969] | 702 | return; |
---|
[b7d3cc34] | 703 | } |
---|
[5ebff60] | 704 | |
---|
| 705 | if (cmd[3]) { |
---|
| 706 | if (!nick_ok(irc, cmd[3])) { |
---|
| 707 | irc_rootmsg(irc, "The requested nick `%s' is invalid", cmd[3]); |
---|
[f73b969] | 708 | return; |
---|
[5ebff60] | 709 | } else if (irc_user_by_name(irc, cmd[3])) { |
---|
| 710 | irc_rootmsg(irc, "The requested nick `%s' already exists", cmd[3]); |
---|
[f73b969] | 711 | return; |
---|
[5ebff60] | 712 | } else { |
---|
| 713 | nick_set_raw(a, cmd[2], cmd[3]); |
---|
[b7d3cc34] | 714 | } |
---|
| 715 | } |
---|
[5ebff60] | 716 | |
---|
| 717 | if ((a->flags & ACC_FLAG_HANDLE_DOMAINS) && cmd[2][0] != '_' && |
---|
| 718 | (!(s = strchr(cmd[2], '@')) || s[1] == '\0')) { |
---|
[06eef80] | 719 | /* If there's no @ or it's the last char, append the user's |
---|
| 720 | domain name now. Exclude handles starting with a _ so |
---|
| 721 | adding _xmlconsole will keep working. */ |
---|
[5ebff60] | 722 | if (s) { |
---|
[06eef80] | 723 | *s = '\0'; |
---|
[5ebff60] | 724 | } |
---|
| 725 | if ((s = strchr(a->user, '@'))) { |
---|
| 726 | cmd[2] = handle = g_strconcat(cmd[2], s, NULL); |
---|
| 727 | } |
---|
[06eef80] | 728 | } |
---|
[5ebff60] | 729 | |
---|
| 730 | if (add_on_server) { |
---|
[f1d488e] | 731 | irc_channel_t *ic; |
---|
| 732 | char *s, *group = NULL;; |
---|
[5ebff60] | 733 | |
---|
| 734 | if ((ic = irc->root->last_channel) && |
---|
| 735 | (s = set_getstr(&ic->set, "fill_by")) && |
---|
| 736 | strcmp(s, "group") == 0 && |
---|
| 737 | (group = set_getstr(&ic->set, "group"))) { |
---|
| 738 | irc_rootmsg(irc, "Adding `%s' to contact list (group %s)", |
---|
| 739 | cmd[2], group); |
---|
| 740 | } else { |
---|
| 741 | irc_rootmsg(irc, "Adding `%s' to contact list", cmd[2]); |
---|
| 742 | } |
---|
| 743 | |
---|
| 744 | a->prpl->add_buddy(a->ic, cmd[2], group); |
---|
| 745 | } else { |
---|
[f1d488e] | 746 | bee_user_t *bu; |
---|
| 747 | irc_user_t *iu; |
---|
[5ebff60] | 748 | |
---|
[dbb0ce3] | 749 | /* Only for add -tmp. For regular adds, this callback will |
---|
| 750 | be called once the IM server confirms. */ |
---|
[5ebff60] | 751 | if ((bu = bee_user_new(irc->b, a->ic, cmd[2], BEE_USER_LOCAL)) && |
---|
| 752 | (iu = bu->ui_data)) { |
---|
| 753 | irc_rootmsg(irc, "Temporarily assigned nickname `%s' " |
---|
| 754 | "to contact `%s'", iu->nick, cmd[2]); |
---|
| 755 | } |
---|
[f1d488e] | 756 | } |
---|
[5ebff60] | 757 | |
---|
| 758 | g_free(handle); |
---|
[b7d3cc34] | 759 | } |
---|
| 760 | |
---|
[5ebff60] | 761 | static void cmd_remove(irc_t *irc, char **cmd) |
---|
[dbb0ce3] | 762 | { |
---|
| 763 | irc_user_t *iu; |
---|
| 764 | bee_user_t *bu; |
---|
| 765 | char *s; |
---|
[5ebff60] | 766 | |
---|
| 767 | if (!(iu = irc_user_by_name(irc, cmd[1])) || !(bu = iu->bu)) { |
---|
| 768 | irc_rootmsg(irc, "Buddy `%s' not found", cmd[1]); |
---|
[dbb0ce3] | 769 | return; |
---|
| 770 | } |
---|
[5ebff60] | 771 | s = g_strdup(bu->handle); |
---|
| 772 | |
---|
| 773 | bu->ic->acc->prpl->remove_buddy(bu->ic, bu->handle, NULL); |
---|
| 774 | nick_del(bu); |
---|
[5c90890] | 775 | if (g_hash_table_contains(irc->nick_user_hash, iu->key)) { |
---|
[5ebff60] | 776 | bee_user_free(irc->b, bu); |
---|
| 777 | } |
---|
| 778 | |
---|
| 779 | irc_rootmsg(irc, "Buddy `%s' (nick %s) removed from contact list", s, cmd[1]); |
---|
| 780 | g_free(s); |
---|
| 781 | |
---|
[dbb0ce3] | 782 | return; |
---|
| 783 | } |
---|
| 784 | |
---|
[5ebff60] | 785 | static void cmd_info(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 786 | { |
---|
[0da65d5] | 787 | struct im_connection *ic; |
---|
[b7d3cc34] | 788 | account_t *a; |
---|
[5ebff60] | 789 | |
---|
| 790 | if (!cmd[2]) { |
---|
| 791 | irc_user_t *iu = irc_user_by_name(irc, cmd[1]); |
---|
| 792 | if (!iu || !iu->bu) { |
---|
| 793 | irc_rootmsg(irc, "Nick `%s' does not exist", cmd[1]); |
---|
[f73b969] | 794 | return; |
---|
[b7d3cc34] | 795 | } |
---|
[aa44bdd] | 796 | ic = iu->bu->ic; |
---|
| 797 | cmd[2] = iu->bu->handle; |
---|
[5ebff60] | 798 | } else if (!(a = account_get(irc->b, cmd[1]))) { |
---|
| 799 | irc_rootmsg(irc, "Invalid account"); |
---|
[f73b969] | 800 | return; |
---|
[5ebff60] | 801 | } else if (!((ic = a->ic) && (a->ic->flags & OPT_LOGGED_IN))) { |
---|
| 802 | irc_rootmsg(irc, "That account is not on-line"); |
---|
[f73b969] | 803 | return; |
---|
[b7d3cc34] | 804 | } |
---|
[5ebff60] | 805 | |
---|
| 806 | if (!ic->acc->prpl->get_info) { |
---|
| 807 | irc_rootmsg(irc, "Command `%s' not supported by this protocol", cmd[0]); |
---|
| 808 | } else { |
---|
| 809 | ic->acc->prpl->get_info(ic, cmd[2]); |
---|
[f73b969] | 810 | } |
---|
[b7d3cc34] | 811 | } |
---|
| 812 | |
---|
[5ebff60] | 813 | static void cmd_rename(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 814 | { |
---|
[9a9b520] | 815 | irc_user_t *iu, *old; |
---|
[5ebff60] | 816 | gboolean del = g_strcasecmp(cmd[1], "-del") == 0; |
---|
| 817 | |
---|
| 818 | iu = irc_user_by_name(irc, cmd[del ? 2 : 1]); |
---|
| 819 | |
---|
| 820 | if (iu == NULL) { |
---|
[19ba56e] | 821 | irc_rootmsg(irc, "Nick `%s' does not exist", cmd[del ? 2 : 1]); |
---|
[5ebff60] | 822 | } else if (del) { |
---|
| 823 | if (iu->bu) { |
---|
| 824 | bee_irc_user_nick_reset(iu); |
---|
| 825 | } |
---|
| 826 | irc_rootmsg(irc, "Nickname reset to `%s'", iu->nick); |
---|
| 827 | } else if (iu == irc->user) { |
---|
| 828 | irc_rootmsg(irc, "Use /nick to change your own nickname"); |
---|
| 829 | } else if (!nick_ok(irc, cmd[2])) { |
---|
| 830 | irc_rootmsg(irc, "Nick `%s' is invalid", cmd[2]); |
---|
| 831 | } else if ((old = irc_user_by_name(irc, cmd[2])) && old != iu) { |
---|
| 832 | irc_rootmsg(irc, "Nick `%s' already exists", cmd[2]); |
---|
| 833 | } else { |
---|
| 834 | if (!irc_user_set_nick(iu, cmd[2])) { |
---|
| 835 | irc_rootmsg(irc, "Error while changing nick"); |
---|
[57c96f7] | 836 | return; |
---|
| 837 | } |
---|
[5ebff60] | 838 | |
---|
| 839 | if (iu == irc->root) { |
---|
[7125cb3] | 840 | /* If we're called internally (user did "set root_nick"), |
---|
| 841 | let's not go O(INF). :-) */ |
---|
[5ebff60] | 842 | if (strcmp(cmd[0], "set_rename") != 0) { |
---|
| 843 | set_setstr(&irc->b->set, "root_nick", cmd[2]); |
---|
| 844 | } |
---|
| 845 | } else if (iu->bu) { |
---|
| 846 | nick_set(iu->bu, cmd[2]); |
---|
[f73b969] | 847 | } |
---|
[5ebff60] | 848 | |
---|
| 849 | irc_rootmsg(irc, "Nick successfully changed"); |
---|
[b7d3cc34] | 850 | } |
---|
| 851 | } |
---|
| 852 | |
---|
[5ebff60] | 853 | char *set_eval_root_nick(set_t *set, char *new_nick) |
---|
[1195cec] | 854 | { |
---|
| 855 | irc_t *irc = set->data; |
---|
[5ebff60] | 856 | |
---|
| 857 | if (strcmp(irc->root->nick, new_nick) != 0) { |
---|
[0a6e5d1] | 858 | char *cmd[] = { "set_rename", irc->root->nick, new_nick, NULL }; |
---|
[5ebff60] | 859 | |
---|
| 860 | cmd_rename(irc, cmd); |
---|
[1195cec] | 861 | } |
---|
[5ebff60] | 862 | |
---|
| 863 | return strcmp(irc->root->nick, new_nick) == 0 ? new_nick : SET_INVALID; |
---|
[1195cec] | 864 | } |
---|
[0baed0d] | 865 | |
---|
[5ebff60] | 866 | static void cmd_block(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 867 | { |
---|
[0da65d5] | 868 | struct im_connection *ic; |
---|
[b7d3cc34] | 869 | account_t *a; |
---|
[5ebff60] | 870 | |
---|
| 871 | if (!cmd[2] && (a = account_get(irc->b, cmd[1])) && a->ic) { |
---|
[87b6a3e] | 872 | char *format; |
---|
| 873 | GSList *l; |
---|
[5ebff60] | 874 | |
---|
| 875 | if (strchr(irc->umode, 'b') != NULL) { |
---|
[87b6a3e] | 876 | format = "%s\t%s"; |
---|
[5ebff60] | 877 | } else { |
---|
[57ef864] | 878 | format = "%-32.32s %-16.16s"; |
---|
[5ebff60] | 879 | } |
---|
| 880 | |
---|
| 881 | irc_rootmsg(irc, format, "Handle", "Nickname"); |
---|
| 882 | for (l = a->ic->deny; l; l = l->next) { |
---|
| 883 | bee_user_t *bu = bee_user_by_handle(irc->b, a->ic, l->data); |
---|
[2272cb3] | 884 | irc_user_t *iu = bu ? bu->ui_data : NULL; |
---|
[5ebff60] | 885 | irc_rootmsg(irc, format, l->data, iu ? iu->nick : "(none)"); |
---|
[87b6a3e] | 886 | } |
---|
[5ebff60] | 887 | irc_rootmsg(irc, "End of list."); |
---|
| 888 | |
---|
[87b6a3e] | 889 | return; |
---|
[5ebff60] | 890 | } else if (!cmd[2]) { |
---|
| 891 | irc_user_t *iu = irc_user_by_name(irc, cmd[1]); |
---|
| 892 | if (!iu || !iu->bu) { |
---|
| 893 | irc_rootmsg(irc, "Nick `%s' does not exist", cmd[1]); |
---|
[f73b969] | 894 | return; |
---|
[b7d3cc34] | 895 | } |
---|
[2272cb3] | 896 | ic = iu->bu->ic; |
---|
| 897 | cmd[2] = iu->bu->handle; |
---|
[5ebff60] | 898 | } else if (!(a = account_get(irc->b, cmd[1]))) { |
---|
| 899 | irc_rootmsg(irc, "Invalid account"); |
---|
[f73b969] | 900 | return; |
---|
[5ebff60] | 901 | } else if (!((ic = a->ic) && (a->ic->flags & OPT_LOGGED_IN))) { |
---|
| 902 | irc_rootmsg(irc, "That account is not on-line"); |
---|
[f73b969] | 903 | return; |
---|
[b7d3cc34] | 904 | } |
---|
[5ebff60] | 905 | |
---|
| 906 | if (!ic->acc->prpl->add_deny || !ic->acc->prpl->rem_permit) { |
---|
| 907 | irc_rootmsg(irc, "Command `%s' not supported by this protocol", cmd[0]); |
---|
| 908 | } else { |
---|
| 909 | imc_rem_allow(ic, cmd[2]); |
---|
| 910 | imc_add_block(ic, cmd[2]); |
---|
| 911 | irc_rootmsg(irc, "Buddy `%s' moved from allow- to block-list", cmd[2]); |
---|
[b7d3cc34] | 912 | } |
---|
| 913 | } |
---|
| 914 | |
---|
[5ebff60] | 915 | static void cmd_allow(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 916 | { |
---|
[0da65d5] | 917 | struct im_connection *ic; |
---|
[b7d3cc34] | 918 | account_t *a; |
---|
[5ebff60] | 919 | |
---|
| 920 | if (!cmd[2] && (a = account_get(irc->b, cmd[1])) && a->ic) { |
---|
[87b6a3e] | 921 | char *format; |
---|
| 922 | GSList *l; |
---|
[5ebff60] | 923 | |
---|
| 924 | if (strchr(irc->umode, 'b') != NULL) { |
---|
[87b6a3e] | 925 | format = "%s\t%s"; |
---|
[5ebff60] | 926 | } else { |
---|
[57ef864] | 927 | format = "%-32.32s %-16.16s"; |
---|
[5ebff60] | 928 | } |
---|
| 929 | |
---|
| 930 | irc_rootmsg(irc, format, "Handle", "Nickname"); |
---|
| 931 | for (l = a->ic->permit; l; l = l->next) { |
---|
| 932 | bee_user_t *bu = bee_user_by_handle(irc->b, a->ic, l->data); |
---|
[2272cb3] | 933 | irc_user_t *iu = bu ? bu->ui_data : NULL; |
---|
[5ebff60] | 934 | irc_rootmsg(irc, format, l->data, iu ? iu->nick : "(none)"); |
---|
[87b6a3e] | 935 | } |
---|
[5ebff60] | 936 | irc_rootmsg(irc, "End of list."); |
---|
| 937 | |
---|
[87b6a3e] | 938 | return; |
---|
[5ebff60] | 939 | } else if (!cmd[2]) { |
---|
| 940 | irc_user_t *iu = irc_user_by_name(irc, cmd[1]); |
---|
| 941 | if (!iu || !iu->bu) { |
---|
| 942 | irc_rootmsg(irc, "Nick `%s' does not exist", cmd[1]); |
---|
[f73b969] | 943 | return; |
---|
[b7d3cc34] | 944 | } |
---|
[2272cb3] | 945 | ic = iu->bu->ic; |
---|
| 946 | cmd[2] = iu->bu->handle; |
---|
[5ebff60] | 947 | } else if (!(a = account_get(irc->b, cmd[1]))) { |
---|
| 948 | irc_rootmsg(irc, "Invalid account"); |
---|
[f73b969] | 949 | return; |
---|
[5ebff60] | 950 | } else if (!((ic = a->ic) && (a->ic->flags & OPT_LOGGED_IN))) { |
---|
| 951 | irc_rootmsg(irc, "That account is not on-line"); |
---|
[f73b969] | 952 | return; |
---|
[b7d3cc34] | 953 | } |
---|
[5ebff60] | 954 | |
---|
| 955 | if (!ic->acc->prpl->rem_deny || !ic->acc->prpl->add_permit) { |
---|
| 956 | irc_rootmsg(irc, "Command `%s' not supported by this protocol", cmd[0]); |
---|
| 957 | } else { |
---|
| 958 | imc_rem_block(ic, cmd[2]); |
---|
| 959 | imc_add_allow(ic, cmd[2]); |
---|
| 960 | |
---|
| 961 | irc_rootmsg(irc, "Buddy `%s' moved from block- to allow-list", cmd[2]); |
---|
[b7d3cc34] | 962 | } |
---|
| 963 | } |
---|
| 964 | |
---|
[5ebff60] | 965 | static void cmd_yesno(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 966 | { |
---|
| 967 | query_t *q = NULL; |
---|
| 968 | int numq = 0; |
---|
[5ebff60] | 969 | |
---|
| 970 | if (irc->queries == NULL) { |
---|
[65a4a64] | 971 | /* Alright, alright, let's add a tiny easter egg here. */ |
---|
| 972 | static irc_t *last_irc = NULL; |
---|
| 973 | static time_t last_time = 0; |
---|
| 974 | static int times = 0; |
---|
| 975 | static const char *msg[] = { |
---|
| 976 | "Oh yeah, that's right.", |
---|
| 977 | "Alright, alright. Now go back to work.", |
---|
| 978 | "Buuuuuuuuuuuuuuuurp... Excuse me!", |
---|
| 979 | "Yes?", |
---|
| 980 | "No?", |
---|
| 981 | }; |
---|
[5ebff60] | 982 | |
---|
| 983 | if (last_irc == irc && time(NULL) - last_time < 15) { |
---|
| 984 | if ((++times >= 3)) { |
---|
| 985 | irc_rootmsg(irc, "%s", msg[rand() % (sizeof(msg) / sizeof(char*))]); |
---|
[65a4a64] | 986 | last_irc = NULL; |
---|
| 987 | times = 0; |
---|
| 988 | return; |
---|
| 989 | } |
---|
[5ebff60] | 990 | } else { |
---|
| 991 | last_time = time(NULL); |
---|
[65a4a64] | 992 | last_irc = irc; |
---|
| 993 | times = 0; |
---|
| 994 | } |
---|
[5ebff60] | 995 | |
---|
| 996 | irc_rootmsg(irc, "Did I ask you something?"); |
---|
[f73b969] | 997 | return; |
---|
[b7d3cc34] | 998 | } |
---|
[5ebff60] | 999 | |
---|
[b7d3cc34] | 1000 | /* If there's an argument, the user seems to want to answer another question than the |
---|
| 1001 | first/last (depending on the query_order setting) one. */ |
---|
[5ebff60] | 1002 | if (cmd[1]) { |
---|
| 1003 | if (sscanf(cmd[1], "%d", &numq) != 1) { |
---|
| 1004 | irc_rootmsg(irc, "Invalid query number"); |
---|
[f73b969] | 1005 | return; |
---|
[b7d3cc34] | 1006 | } |
---|
[5ebff60] | 1007 | |
---|
| 1008 | for (q = irc->queries; q; q = q->next, numq--) { |
---|
| 1009 | if (numq == 0) { |
---|
[b7d3cc34] | 1010 | break; |
---|
[5ebff60] | 1011 | } |
---|
| 1012 | } |
---|
| 1013 | |
---|
| 1014 | if (!q) { |
---|
| 1015 | irc_rootmsg(irc, "Uhm, I never asked you something like that..."); |
---|
[f73b969] | 1016 | return; |
---|
[b7d3cc34] | 1017 | } |
---|
| 1018 | } |
---|
[5ebff60] | 1019 | |
---|
| 1020 | if (g_strcasecmp(cmd[0], "yes") == 0) { |
---|
| 1021 | query_answer(irc, q, 1); |
---|
| 1022 | } else if (g_strcasecmp(cmd[0], "no") == 0) { |
---|
| 1023 | query_answer(irc, q, 0); |
---|
| 1024 | } |
---|
[b7d3cc34] | 1025 | } |
---|
| 1026 | |
---|
[5ebff60] | 1027 | static void cmd_set(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 1028 | { |
---|
[5ebff60] | 1029 | cmd_set_real(irc, cmd, &irc->b->set, NULL); |
---|
[b7d3cc34] | 1030 | } |
---|
| 1031 | |
---|
[5ebff60] | 1032 | static void cmd_blist(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 1033 | { |
---|
[f93fd2d] | 1034 | int online = 0, away = 0, offline = 0, ismatch = 0; |
---|
[5c90890] | 1035 | GList *l, *users; |
---|
[f93fd2d] | 1036 | GRegex *regex = NULL; |
---|
| 1037 | GError *error = NULL; |
---|
[aefa533e] | 1038 | char s[256]; |
---|
| 1039 | char *format; |
---|
[b7d3cc34] | 1040 | int n_online = 0, n_away = 0, n_offline = 0; |
---|
[5ebff60] | 1041 | |
---|
| 1042 | if (cmd[1] && g_strcasecmp(cmd[1], "all") == 0) { |
---|
[b7d3cc34] | 1043 | online = offline = away = 1; |
---|
[5ebff60] | 1044 | } else if (cmd[1] && g_strcasecmp(cmd[1], "offline") == 0) { |
---|
[b7d3cc34] | 1045 | offline = 1; |
---|
[5ebff60] | 1046 | } else if (cmd[1] && g_strcasecmp(cmd[1], "away") == 0) { |
---|
[b7d3cc34] | 1047 | away = 1; |
---|
[5ebff60] | 1048 | } else if (cmd[1] && g_strcasecmp(cmd[1], "online") == 0) { |
---|
[b7d3cc34] | 1049 | online = 1; |
---|
[5ebff60] | 1050 | } else { |
---|
[449a51d] | 1051 | online = away = 1; |
---|
[f93fd2d] | 1052 | } |
---|
[5ebff60] | 1053 | |
---|
| 1054 | if (cmd[2]) { |
---|
| 1055 | regex = g_regex_new(cmd[2], G_REGEX_CASELESS, 0, &error); |
---|
| 1056 | } |
---|
| 1057 | |
---|
| 1058 | if (error) { |
---|
[a81e6a0] | 1059 | irc_rootmsg(irc, "%s", error->message); |
---|
[5ebff60] | 1060 | g_error_free(error); |
---|
| 1061 | } |
---|
| 1062 | |
---|
| 1063 | if (strchr(irc->umode, 'b') != NULL) { |
---|
[aefa533e] | 1064 | format = "%s\t%s\t%s"; |
---|
[5ebff60] | 1065 | } else { |
---|
[683e4e0] | 1066 | format = "%-24.24s %-40.40s %s"; |
---|
[5ebff60] | 1067 | } |
---|
| 1068 | |
---|
| 1069 | irc_rootmsg(irc, format, "Nick", "Handle/Account", "Status"); |
---|
| 1070 | |
---|
| 1071 | if (irc->root->last_channel && |
---|
| 1072 | strcmp(set_getstr(&irc->root->last_channel->set, "type"), "control") != 0) { |
---|
[ac2717b] | 1073 | irc->root->last_channel = NULL; |
---|
[5ebff60] | 1074 | } |
---|
| 1075 | |
---|
[5c90890] | 1076 | users = g_hash_table_get_values(irc->nick_user_hash); |
---|
| 1077 | users = g_list_sort(users, irc_user_cmp); |
---|
| 1078 | |
---|
| 1079 | for (l = users; l; l = l->next) { |
---|
[4c3519a] | 1080 | irc_user_t *iu = l->data; |
---|
| 1081 | bee_user_t *bu = iu->bu; |
---|
[5ebff60] | 1082 | |
---|
| 1083 | if (!regex || g_regex_match(regex, iu->nick, 0, NULL)) { |
---|
[f93fd2d] | 1084 | ismatch = 1; |
---|
[5ebff60] | 1085 | } else { |
---|
[f93fd2d] | 1086 | ismatch = 0; |
---|
[5ebff60] | 1087 | } |
---|
| 1088 | |
---|
| 1089 | if (!bu || (irc->root->last_channel && !irc_channel_wants_user(irc->root->last_channel, iu))) { |
---|
[4c3519a] | 1090 | continue; |
---|
[5ebff60] | 1091 | } |
---|
| 1092 | |
---|
| 1093 | if ((bu->flags & (BEE_USER_ONLINE | BEE_USER_AWAY)) == BEE_USER_ONLINE) { |
---|
| 1094 | if (ismatch == 1 && online == 1) { |
---|
[f93fd2d] | 1095 | char st[256] = "Online"; |
---|
[5ebff60] | 1096 | |
---|
| 1097 | if (bu->status_msg) { |
---|
| 1098 | g_snprintf(st, sizeof(st) - 1, "Online (%s)", bu->status_msg); |
---|
| 1099 | } |
---|
| 1100 | |
---|
| 1101 | g_snprintf(s, sizeof(s) - 1, "%s %s", bu->handle, bu->ic->acc->tag); |
---|
| 1102 | irc_rootmsg(irc, format, iu->nick, s, st); |
---|
[f93fd2d] | 1103 | } |
---|
[5ebff60] | 1104 | |
---|
| 1105 | n_online++; |
---|
[aefa533e] | 1106 | } |
---|
[5ebff60] | 1107 | |
---|
| 1108 | if ((bu->flags & BEE_USER_ONLINE) && (bu->flags & BEE_USER_AWAY)) { |
---|
| 1109 | if (ismatch == 1 && away == 1) { |
---|
| 1110 | g_snprintf(s, sizeof(s) - 1, "%s %s", bu->handle, bu->ic->acc->tag); |
---|
| 1111 | irc_rootmsg(irc, format, iu->nick, s, irc_user_get_away(iu)); |
---|
[f93fd2d] | 1112 | } |
---|
[5ebff60] | 1113 | n_away++; |
---|
[aefa533e] | 1114 | } |
---|
[5ebff60] | 1115 | |
---|
| 1116 | if (!(bu->flags & BEE_USER_ONLINE)) { |
---|
| 1117 | if (ismatch == 1 && offline == 1) { |
---|
| 1118 | g_snprintf(s, sizeof(s) - 1, "%s %s", bu->handle, bu->ic->acc->tag); |
---|
| 1119 | irc_rootmsg(irc, format, iu->nick, s, "Offline"); |
---|
[f93fd2d] | 1120 | } |
---|
[5ebff60] | 1121 | n_offline++; |
---|
[aefa533e] | 1122 | } |
---|
[b7d3cc34] | 1123 | } |
---|
[5ebff60] | 1124 | |
---|
[5c90890] | 1125 | g_list_free(users); |
---|
| 1126 | |
---|
[5ebff60] | 1127 | irc_rootmsg(irc, "%d buddies (%d available, %d away, %d offline)", n_online + n_away + n_offline, n_online, |
---|
| 1128 | n_away, n_offline); |
---|
| 1129 | |
---|
| 1130 | if (regex) { |
---|
| 1131 | g_regex_unref(regex); |
---|
| 1132 | } |
---|
[b7d3cc34] | 1133 | } |
---|
| 1134 | |
---|
[808825e] | 1135 | static gint prplcmp(gconstpointer a, gconstpointer b) |
---|
| 1136 | { |
---|
| 1137 | const struct prpl *pa = a; |
---|
| 1138 | const struct prpl *pb = b; |
---|
| 1139 | |
---|
| 1140 | return g_strcasecmp(pa->name, pb->name); |
---|
| 1141 | } |
---|
| 1142 | |
---|
| 1143 | static void prplstr(GList *prpls, GString *gstr) |
---|
| 1144 | { |
---|
| 1145 | const char *last = NULL; |
---|
| 1146 | GList *l; |
---|
| 1147 | struct prpl *p; |
---|
| 1148 | |
---|
| 1149 | prpls = g_list_copy(prpls); |
---|
| 1150 | prpls = g_list_sort(prpls, prplcmp); |
---|
| 1151 | |
---|
| 1152 | for (l = prpls; l; l = l->next) { |
---|
| 1153 | p = l->data; |
---|
| 1154 | |
---|
| 1155 | if (last && g_strcasecmp(p->name, last) == 0) { |
---|
| 1156 | /* Ignore duplicates (mainly for libpurple) */ |
---|
| 1157 | continue; |
---|
| 1158 | } |
---|
| 1159 | |
---|
| 1160 | if (gstr->len != 0) { |
---|
| 1161 | g_string_append(gstr, ", "); |
---|
| 1162 | } |
---|
| 1163 | |
---|
| 1164 | g_string_append(gstr, p->name); |
---|
| 1165 | last = p->name; |
---|
| 1166 | } |
---|
| 1167 | |
---|
| 1168 | g_list_free(prpls); |
---|
| 1169 | } |
---|
| 1170 | |
---|
[6908ab7] | 1171 | static void cmd_plugins_info(irc_t *irc, char **cmd) |
---|
| 1172 | { |
---|
| 1173 | GList *l; |
---|
| 1174 | struct plugin_info *info; |
---|
| 1175 | |
---|
| 1176 | MIN_ARGS(2); |
---|
| 1177 | |
---|
| 1178 | for (l = get_plugins(); l; l = l->next) { |
---|
| 1179 | info = l->data; |
---|
| 1180 | if (g_strcasecmp(cmd[2], info->name) == 0) { |
---|
| 1181 | break; |
---|
| 1182 | } |
---|
| 1183 | } |
---|
| 1184 | |
---|
| 1185 | if (!l) { |
---|
| 1186 | return; |
---|
| 1187 | } |
---|
| 1188 | |
---|
| 1189 | irc_rootmsg(irc, "%s:", info->name); |
---|
| 1190 | irc_rootmsg(irc, " Version: %s", info->version); |
---|
| 1191 | |
---|
| 1192 | if (info->description) { |
---|
| 1193 | irc_rootmsg(irc, " Description: %s", info->description); |
---|
| 1194 | } |
---|
| 1195 | |
---|
| 1196 | if (info->author) { |
---|
| 1197 | irc_rootmsg(irc, " Author: %s", info->author); |
---|
| 1198 | } |
---|
| 1199 | |
---|
| 1200 | if (info->url) { |
---|
| 1201 | irc_rootmsg(irc, " URL: %s", info->url); |
---|
| 1202 | } |
---|
| 1203 | } |
---|
| 1204 | |
---|
[d28fe1c4] | 1205 | static void cmd_plugins(irc_t *irc, char **cmd) |
---|
| 1206 | { |
---|
[808825e] | 1207 | GList *prpls; |
---|
| 1208 | GString *gstr; |
---|
| 1209 | |
---|
[6908ab7] | 1210 | if (cmd[1] && g_strcasecmp(cmd[1], "info") == 0) { |
---|
| 1211 | cmd_plugins_info(irc, cmd); |
---|
| 1212 | return; |
---|
| 1213 | } |
---|
| 1214 | |
---|
[808825e] | 1215 | #ifdef WITH_PLUGINS |
---|
[d28fe1c4] | 1216 | GList *l; |
---|
| 1217 | struct plugin_info *info; |
---|
[6908ab7] | 1218 | char *format; |
---|
[d28fe1c4] | 1219 | |
---|
[6908ab7] | 1220 | if (strchr(irc->umode, 'b') != NULL) { |
---|
| 1221 | format = "%s\t%s"; |
---|
| 1222 | } else { |
---|
| 1223 | format = "%-30s %s"; |
---|
| 1224 | } |
---|
[d28fe1c4] | 1225 | |
---|
[6908ab7] | 1226 | irc_rootmsg(irc, format, "Plugin", "Version"); |
---|
[d28fe1c4] | 1227 | |
---|
[6908ab7] | 1228 | for (l = get_plugins(); l; l = l->next) { |
---|
[6d212f4] | 1229 | char *c; |
---|
[6908ab7] | 1230 | info = l->data; |
---|
[6d212f4] | 1231 | |
---|
| 1232 | /* some purple plugins like to include several versions separated by newlines... */ |
---|
| 1233 | if ((c = strchr(info->version, '\n'))) { |
---|
| 1234 | char *version = g_strndup(info->version, c - info->version); |
---|
| 1235 | irc_rootmsg(irc, format, info->name, version); |
---|
| 1236 | g_free(version); |
---|
| 1237 | } else { |
---|
| 1238 | irc_rootmsg(irc, format, info->name, info->version); |
---|
| 1239 | } |
---|
[d28fe1c4] | 1240 | } |
---|
| 1241 | #endif |
---|
| 1242 | |
---|
[1dcbd3e] | 1243 | irc_rootmsg(irc, " "); |
---|
[6908ab7] | 1244 | |
---|
[808825e] | 1245 | gstr = g_string_new(NULL); |
---|
| 1246 | prpls = get_protocols(); |
---|
| 1247 | |
---|
| 1248 | if (prpls) { |
---|
| 1249 | prplstr(prpls, gstr); |
---|
| 1250 | irc_rootmsg(irc, "Enabled Protocols: %s", gstr->str); |
---|
| 1251 | g_string_truncate(gstr, 0); |
---|
| 1252 | } |
---|
| 1253 | |
---|
| 1254 | prpls = get_protocols_disabled(); |
---|
| 1255 | |
---|
| 1256 | if (prpls) { |
---|
| 1257 | prplstr(prpls, gstr); |
---|
| 1258 | irc_rootmsg(irc, "Disabled Protocols: %s", gstr->str); |
---|
| 1259 | } |
---|
| 1260 | |
---|
| 1261 | g_string_free(gstr, TRUE); |
---|
| 1262 | } |
---|
| 1263 | |
---|
[5ebff60] | 1264 | static void cmd_qlist(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 1265 | { |
---|
| 1266 | query_t *q = irc->queries; |
---|
| 1267 | int num; |
---|
[5ebff60] | 1268 | |
---|
| 1269 | if (!q) { |
---|
| 1270 | irc_rootmsg(irc, "There are no pending questions."); |
---|
[f73b969] | 1271 | return; |
---|
[b7d3cc34] | 1272 | } |
---|
[5ebff60] | 1273 | |
---|
| 1274 | irc_rootmsg(irc, "Pending queries:"); |
---|
| 1275 | |
---|
| 1276 | for (num = 0; q; q = q->next, num++) { |
---|
| 1277 | if (q->ic) { /* Not necessary yet, but it might come later */ |
---|
| 1278 | irc_rootmsg(irc, "%d, %s: %s", num, q->ic->acc->tag, q->question); |
---|
| 1279 | } else { |
---|
| 1280 | irc_rootmsg(irc, "%d, BitlBee: %s", num, q->question); |
---|
| 1281 | } |
---|
| 1282 | } |
---|
[b7d3cc34] | 1283 | } |
---|
| 1284 | |
---|
[5ebff60] | 1285 | static void cmd_chat(irc_t *irc, char **cmd) |
---|
[a9a7287] | 1286 | { |
---|
| 1287 | account_t *acc; |
---|
[5ebff60] | 1288 | |
---|
| 1289 | if (g_strcasecmp(cmd[1], "add") == 0) { |
---|
[a33ee0f] | 1290 | bee_chat_info_t *ci; |
---|
| 1291 | char *channel, *room, *s; |
---|
[7b71feb] | 1292 | struct irc_channel *ic; |
---|
[a33ee0f] | 1293 | guint i; |
---|
[5ebff60] | 1294 | |
---|
| 1295 | MIN_ARGS(3); |
---|
| 1296 | |
---|
| 1297 | if (!(acc = account_get(irc->b, cmd[2]))) { |
---|
| 1298 | irc_rootmsg(irc, "Invalid account"); |
---|
[a9a7287] | 1299 | return; |
---|
[5ebff60] | 1300 | } else if (!acc->prpl->chat_join) { |
---|
| 1301 | irc_rootmsg(irc, "Named chatrooms not supported on that account."); |
---|
[5a75d15] | 1302 | return; |
---|
| 1303 | } |
---|
[5ebff60] | 1304 | |
---|
[a33ee0f] | 1305 | if (cmd[3][0] == '!') { |
---|
[7e4f439c] | 1306 | if (!acc->ic || !(acc->ic->flags & OPT_LOGGED_IN)) { |
---|
| 1307 | irc_rootmsg(irc, "Not logged in to account."); |
---|
| 1308 | return; |
---|
| 1309 | } else if (!acc->prpl->chat_list) { |
---|
[a33ee0f] | 1310 | irc_rootmsg(irc, "Listing chatrooms not supported on that account."); |
---|
| 1311 | return; |
---|
| 1312 | } |
---|
| 1313 | |
---|
| 1314 | i = g_ascii_strtoull(cmd[3] + 1, NULL, 10); |
---|
| 1315 | ci = g_slist_nth_data(acc->ic->chatlist, i - 1); |
---|
| 1316 | |
---|
| 1317 | if (ci == NULL) { |
---|
| 1318 | irc_rootmsg(irc, "Invalid chatroom index"); |
---|
| 1319 | return; |
---|
| 1320 | } |
---|
| 1321 | |
---|
| 1322 | room = ci->title; |
---|
| 1323 | } else { |
---|
| 1324 | room = cmd[3]; |
---|
| 1325 | } |
---|
| 1326 | |
---|
[5ebff60] | 1327 | if (cmd[4] == NULL) { |
---|
[a33ee0f] | 1328 | channel = g_strdup(room); |
---|
[5ebff60] | 1329 | if ((s = strchr(channel, '@'))) { |
---|
[07054a5] | 1330 | *s = 0; |
---|
[5ebff60] | 1331 | } |
---|
| 1332 | } else { |
---|
| 1333 | channel = g_strdup(cmd[4]); |
---|
[07054a5] | 1334 | } |
---|
[5ebff60] | 1335 | |
---|
| 1336 | if (strchr(CTYPES, channel[0]) == NULL) { |
---|
| 1337 | s = g_strdup_printf("#%s", channel); |
---|
| 1338 | g_free(channel); |
---|
[07054a5] | 1339 | channel = s; |
---|
[5ebff60] | 1340 | |
---|
| 1341 | irc_channel_name_strip(channel); |
---|
[5a75d15] | 1342 | } |
---|
[5ebff60] | 1343 | |
---|
| 1344 | if ((ic = irc_channel_new(irc, channel)) && |
---|
| 1345 | set_setstr(&ic->set, "type", "chat") && |
---|
| 1346 | set_setstr(&ic->set, "chat_type", "room") && |
---|
| 1347 | set_setstr(&ic->set, "account", cmd[2]) && |
---|
[a33ee0f] | 1348 | set_setstr(&ic->set, "room", room)) { |
---|
[16ea00d] | 1349 | irc_rootmsg(irc, "Chatroom successfully added, join with \002/join %s\002", channel); |
---|
[5ebff60] | 1350 | } else { |
---|
| 1351 | if (ic) { |
---|
| 1352 | irc_channel_free(ic); |
---|
| 1353 | |
---|
[16ea00d] | 1354 | irc_rootmsg(irc, "Error adding chatroom."); |
---|
| 1355 | } else if (irc_channel_by_name(irc, channel)) { |
---|
| 1356 | irc_rootmsg(irc, "A channel named `%s' already exists. " |
---|
| 1357 | "Join with \002/join %s\002 or see \002help channel\002 to modify it", |
---|
| 1358 | channel, channel); |
---|
| 1359 | } else { |
---|
| 1360 | irc_rootmsg(irc, "Error creating channel for chatroom."); |
---|
| 1361 | } |
---|
[d995c9b] | 1362 | } |
---|
[5ebff60] | 1363 | g_free(channel); |
---|
[a33ee0f] | 1364 | } else if (g_strcasecmp(cmd[1], "list") == 0) { |
---|
| 1365 | MIN_ARGS(2); |
---|
| 1366 | |
---|
| 1367 | if (!(acc = account_get(irc->b, cmd[2]))) { |
---|
| 1368 | irc_rootmsg(irc, "Invalid account"); |
---|
| 1369 | return; |
---|
[7e4f439c] | 1370 | } else if (!acc->ic || !(acc->ic->flags & OPT_LOGGED_IN)) { |
---|
| 1371 | irc_rootmsg(irc, "Not logged in to account."); |
---|
| 1372 | return; |
---|
[a33ee0f] | 1373 | } else if (!acc->prpl->chat_list) { |
---|
| 1374 | irc_rootmsg(irc, "Listing chatrooms not supported on that account."); |
---|
| 1375 | return; |
---|
| 1376 | } |
---|
| 1377 | |
---|
| 1378 | acc->prpl->chat_list(acc->ic, cmd[3]); |
---|
[5ebff60] | 1379 | } else if (g_strcasecmp(cmd[1], "with") == 0) { |
---|
[c1a8a16] | 1380 | irc_user_t *iu; |
---|
[5ebff60] | 1381 | |
---|
| 1382 | MIN_ARGS(2); |
---|
| 1383 | |
---|
| 1384 | if ((iu = irc_user_by_name(irc, cmd[2])) && |
---|
| 1385 | iu->bu && iu->bu->ic->acc->prpl->chat_with) { |
---|
| 1386 | if (!iu->bu->ic->acc->prpl->chat_with(iu->bu->ic, iu->bu->handle)) { |
---|
| 1387 | irc_rootmsg(irc, "(Possible) failure while trying to open " |
---|
| 1388 | "a groupchat with %s.", iu->nick); |
---|
[39f93f0] | 1389 | } |
---|
[5ebff60] | 1390 | } else { |
---|
| 1391 | irc_rootmsg(irc, "Can't open a groupchat with %s.", cmd[2]); |
---|
[39f93f0] | 1392 | } |
---|
[a33ee0f] | 1393 | } else if (g_strcasecmp(cmd[1], "set") == 0 || |
---|
[5ebff60] | 1394 | g_strcasecmp(cmd[1], "del") == 0) { |
---|
[398b3198] | 1395 | irc_rootmsg(irc, "Unknown command: chat %s. Did you mean \002channel %s\002?", cmd[1], cmd[1]); |
---|
[5ebff60] | 1396 | } else { |
---|
| 1397 | irc_rootmsg(irc, |
---|
| 1398 | "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "chat", |
---|
| 1399 | cmd[1]); |
---|
[a9a7287] | 1400 | } |
---|
[fa29d093] | 1401 | } |
---|
| 1402 | |
---|
[f95e606] | 1403 | /* some arbitrary numbers */ |
---|
| 1404 | #define CHAT_TITLE_LEN_MIN 20 |
---|
| 1405 | #define CHAT_TITLE_LEN_MAX 100 |
---|
| 1406 | |
---|
[a33ee0f] | 1407 | void cmd_chat_list_finish(struct im_connection *ic) |
---|
| 1408 | { |
---|
| 1409 | account_t *acc = ic->acc; |
---|
| 1410 | bee_chat_info_t *ci; |
---|
[f95e606] | 1411 | char *hformat, *iformat, *topic, *padded; |
---|
[a33ee0f] | 1412 | GSList *l; |
---|
| 1413 | guint i = 0; |
---|
[f95e606] | 1414 | long title_len, new_len; |
---|
[a33ee0f] | 1415 | irc_t *irc = ic->bee->ui_data; |
---|
| 1416 | |
---|
| 1417 | if (ic->chatlist == NULL) { |
---|
| 1418 | irc_rootmsg(irc, "No existing chatrooms"); |
---|
| 1419 | return; |
---|
| 1420 | } |
---|
| 1421 | |
---|
[f95e606] | 1422 | /* find a reasonable width for the table */ |
---|
| 1423 | title_len = CHAT_TITLE_LEN_MIN; |
---|
| 1424 | |
---|
| 1425 | for (l = ic->chatlist; l; l = l->next) { |
---|
| 1426 | ci = l->data; |
---|
| 1427 | new_len = g_utf8_strlen(ci->title, -1); |
---|
| 1428 | |
---|
| 1429 | if (new_len >= CHAT_TITLE_LEN_MAX) { |
---|
| 1430 | title_len = CHAT_TITLE_LEN_MAX; |
---|
| 1431 | break; |
---|
| 1432 | } else if (title_len < new_len) { |
---|
| 1433 | title_len = new_len; |
---|
| 1434 | } |
---|
| 1435 | } |
---|
| 1436 | |
---|
[a33ee0f] | 1437 | if (strchr(irc->umode, 'b') != NULL) { |
---|
| 1438 | hformat = "%s\t%s\t%s"; |
---|
| 1439 | iformat = "%u\t%s\t%s"; |
---|
| 1440 | } else { |
---|
[f95e606] | 1441 | hformat = "%s %s %s"; |
---|
| 1442 | iformat = "%5u %s %s"; |
---|
[a33ee0f] | 1443 | } |
---|
| 1444 | |
---|
[f95e606] | 1445 | padded = str_pad_and_truncate("Title", title_len, NULL); |
---|
| 1446 | irc_rootmsg(irc, hformat, "Index", padded, "Topic"); |
---|
| 1447 | g_free(padded); |
---|
[a33ee0f] | 1448 | |
---|
| 1449 | for (l = ic->chatlist; l; l = l->next) { |
---|
| 1450 | ci = l->data; |
---|
| 1451 | topic = ci->topic ? ci->topic : ""; |
---|
[f95e606] | 1452 | |
---|
[4a9c6b0] | 1453 | padded = str_pad_and_truncate(ci->title ? ci->title : "", title_len, "[...]"); |
---|
[f95e606] | 1454 | irc_rootmsg(irc, iformat, ++i, padded, topic); |
---|
| 1455 | g_free(padded); |
---|
[a33ee0f] | 1456 | } |
---|
| 1457 | |
---|
| 1458 | irc_rootmsg(irc, "%u %s chatrooms", i, acc->tag); |
---|
| 1459 | } |
---|
| 1460 | |
---|
[5ebff60] | 1461 | static void cmd_group(irc_t *irc, char **cmd) |
---|
[f1d488e] | 1462 | { |
---|
| 1463 | GSList *l; |
---|
| 1464 | int len; |
---|
[5ebff60] | 1465 | |
---|
| 1466 | len = strlen(cmd[1]); |
---|
| 1467 | if (g_strncasecmp(cmd[1], "list", len) == 0) { |
---|
[f1d488e] | 1468 | int n = 0; |
---|
[5ebff60] | 1469 | |
---|
| 1470 | if (strchr(irc->umode, 'b')) { |
---|
| 1471 | irc_rootmsg(irc, "Group list:"); |
---|
| 1472 | } |
---|
| 1473 | |
---|
| 1474 | for (l = irc->b->groups; l; l = l->next) { |
---|
[f1d488e] | 1475 | bee_group_t *bg = l->data; |
---|
[5ebff60] | 1476 | irc_rootmsg(irc, "%d. %s", n++, bg->name); |
---|
[f1d488e] | 1477 | } |
---|
[5ebff60] | 1478 | irc_rootmsg(irc, "End of group list"); |
---|
| 1479 | } else if (g_strncasecmp(cmd[1], "info", len) == 0) { |
---|
[e4f5ca8] | 1480 | bee_group_t *bg; |
---|
[c2cc24c] | 1481 | int n = 0; |
---|
| 1482 | |
---|
| 1483 | MIN_ARGS(2); |
---|
[5ebff60] | 1484 | bg = bee_group_by_name(irc->b, cmd[2], FALSE); |
---|
| 1485 | |
---|
| 1486 | if (bg) { |
---|
| 1487 | if (strchr(irc->umode, 'b')) { |
---|
| 1488 | irc_rootmsg(irc, "Members of %s:", cmd[2]); |
---|
| 1489 | } |
---|
| 1490 | for (l = irc->b->users; l; l = l->next) { |
---|
[c2cc24c] | 1491 | bee_user_t *bu = l->data; |
---|
[5ebff60] | 1492 | if (bu->group == bg) { |
---|
| 1493 | irc_rootmsg(irc, "%d. %s", n++, bu->nick ? : bu->handle); |
---|
| 1494 | } |
---|
[c2cc24c] | 1495 | } |
---|
[5ebff60] | 1496 | irc_rootmsg(irc, "End of member list"); |
---|
| 1497 | } else { |
---|
| 1498 | irc_rootmsg(irc, |
---|
| 1499 | "Unknown group: %s. Please use \x02group list\x02 to get a list of available groups.", |
---|
| 1500 | cmd[2]); |
---|
[c2cc24c] | 1501 | } |
---|
[5ebff60] | 1502 | } else { |
---|
| 1503 | irc_rootmsg(irc, |
---|
| 1504 | "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "group", |
---|
| 1505 | cmd[1]); |
---|
[f1d488e] | 1506 | } |
---|
| 1507 | } |
---|
| 1508 | |
---|
[5ebff60] | 1509 | static void cmd_transfer(irc_t *irc, char **cmd) |
---|
[2c2df7d] | 1510 | { |
---|
| 1511 | GSList *files = irc->file_transfers; |
---|
[7616eec] | 1512 | GSList *next; |
---|
[5ebff60] | 1513 | |
---|
[2c2df7d] | 1514 | enum { LIST, REJECT, CANCEL }; |
---|
| 1515 | int subcmd = LIST; |
---|
| 1516 | int fid; |
---|
| 1517 | |
---|
[5ebff60] | 1518 | if (!files) { |
---|
| 1519 | irc_rootmsg(irc, "No pending transfers"); |
---|
[2c2df7d] | 1520 | return; |
---|
| 1521 | } |
---|
| 1522 | |
---|
[5ebff60] | 1523 | if (cmd[1] && (strcmp(cmd[1], "reject") == 0)) { |
---|
[2c2df7d] | 1524 | subcmd = REJECT; |
---|
[5ebff60] | 1525 | } else if (cmd[1] && (strcmp(cmd[1], "cancel") == 0) && |
---|
| 1526 | cmd[2] && (sscanf(cmd[2], "%d", &fid) == 1)) { |
---|
[2c2df7d] | 1527 | subcmd = CANCEL; |
---|
| 1528 | } |
---|
| 1529 | |
---|
[7616eec] | 1530 | for (; files; files = next) { |
---|
| 1531 | next = files->next; |
---|
[2c2df7d] | 1532 | file_transfer_t *file = files->data; |
---|
[5ebff60] | 1533 | |
---|
| 1534 | switch (subcmd) { |
---|
[2c2df7d] | 1535 | case LIST: |
---|
[5ebff60] | 1536 | if (file->status == FT_STATUS_LISTENING) { |
---|
| 1537 | irc_rootmsg(irc, |
---|
| 1538 | "Pending file(id %d): %s (Listening...)", file->local_id, file->file_name); |
---|
| 1539 | } else { |
---|
[2c2df7d] | 1540 | int kb_per_s = 0; |
---|
[5ebff60] | 1541 | time_t diff = time(NULL) - file->started ? : 1; |
---|
| 1542 | if ((file->started > 0) && (file->bytes_transferred > 0)) { |
---|
[2c2df7d] | 1543 | kb_per_s = file->bytes_transferred / 1024 / diff; |
---|
[5ebff60] | 1544 | } |
---|
| 1545 | |
---|
| 1546 | irc_rootmsg(irc, |
---|
| 1547 | "Pending file(id %d): %s (%10zd/%zd kb, %d kb/s)", file->local_id, |
---|
| 1548 | file->file_name, |
---|
| 1549 | file->bytes_transferred / 1024, file->file_size / 1024, kb_per_s); |
---|
[2c2df7d] | 1550 | } |
---|
| 1551 | break; |
---|
| 1552 | case REJECT: |
---|
[5ebff60] | 1553 | if (file->status == FT_STATUS_LISTENING) { |
---|
| 1554 | irc_rootmsg(irc, "Rejecting file transfer for %s", file->file_name); |
---|
| 1555 | imcb_file_canceled(file->ic, file, "Denied by user"); |
---|
[2c2df7d] | 1556 | } |
---|
| 1557 | break; |
---|
| 1558 | case CANCEL: |
---|
[5ebff60] | 1559 | if (file->local_id == fid) { |
---|
| 1560 | irc_rootmsg(irc, "Canceling file transfer for %s", file->file_name); |
---|
| 1561 | imcb_file_canceled(file->ic, file, "Canceled by user"); |
---|
[2c2df7d] | 1562 | } |
---|
| 1563 | break; |
---|
| 1564 | } |
---|
| 1565 | } |
---|
| 1566 | } |
---|
| 1567 | |
---|
[180ab31] | 1568 | /* Maybe this should be a stand-alone command as well? */ |
---|
[5ebff60] | 1569 | static void bitlbee_whatsnew(irc_t *irc) |
---|
[180ab31] | 1570 | { |
---|
[5ebff60] | 1571 | int last = set_getint(&irc->b->set, "last_version"); |
---|
[674a01d] | 1572 | char s[16], *msg; |
---|
[5ebff60] | 1573 | |
---|
| 1574 | if (last >= BITLBEE_VERSION_CODE) { |
---|
[180ab31] | 1575 | return; |
---|
[5ebff60] | 1576 | } |
---|
| 1577 | |
---|
| 1578 | msg = help_get_whatsnew(&(global.help), last); |
---|
| 1579 | |
---|
| 1580 | if (msg) { |
---|
| 1581 | irc_rootmsg(irc, "%s: This seems to be your first time using this " |
---|
| 1582 | "this version of BitlBee. Here's a list of new " |
---|
| 1583 | "features you may like to know about:\n\n%s\n", |
---|
| 1584 | irc->user->nick, msg); |
---|
| 1585 | } |
---|
| 1586 | |
---|
| 1587 | g_free(msg); |
---|
| 1588 | |
---|
| 1589 | g_snprintf(s, sizeof(s), "%d", BITLBEE_VERSION_CODE); |
---|
| 1590 | set_setstr(&irc->b->set, "last_version", s); |
---|
[180ab31] | 1591 | } |
---|
| 1592 | |
---|
[6c56f42] | 1593 | /* IMPORTANT: Keep this list sorted! The short command logic needs that. */ |
---|
[8358691] | 1594 | command_t root_commands[] = { |
---|
[d860a8d] | 1595 | { "account", 1, cmd_account, 0 }, |
---|
[6c56f42] | 1596 | { "add", 2, cmd_add, 0 }, |
---|
[2272cb3] | 1597 | { "allow", 1, cmd_allow, 0 }, |
---|
[4c3519a] | 1598 | { "blist", 0, cmd_blist, 0 }, |
---|
[2272cb3] | 1599 | { "block", 1, cmd_block, 0 }, |
---|
[c133d4b8] | 1600 | { "channel", 1, cmd_channel, 0 }, |
---|
| 1601 | { "chat", 1, cmd_chat, 0 }, |
---|
[6c56f42] | 1602 | { "drop", 1, cmd_drop, 0 }, |
---|
[9d4352c] | 1603 | { "ft", 0, cmd_transfer, 0 }, |
---|
[f1d488e] | 1604 | { "group", 1, cmd_group, 0 }, |
---|
[5ebff60] | 1605 | { "help", 0, cmd_help, 0 }, |
---|
[060d066] | 1606 | { "identify", 0, cmd_identify, 0 }, |
---|
[aa44bdd] | 1607 | { "info", 1, cmd_info, 0 }, |
---|
[6c56f42] | 1608 | { "no", 0, cmd_yesno, 0 }, |
---|
[d28fe1c4] | 1609 | { "plugins", 0, cmd_plugins, 0 }, |
---|
[9d4352c] | 1610 | { "qlist", 0, cmd_qlist, 0 }, |
---|
[060d066] | 1611 | { "register", 0, cmd_register, 0 }, |
---|
[dbb0ce3] | 1612 | { "remove", 1, cmd_remove, 0 }, |
---|
[0298d11] | 1613 | { "rename", 2, cmd_rename, 0 }, |
---|
[6c56f42] | 1614 | { "save", 0, cmd_save, 0 }, |
---|
[0298d11] | 1615 | { "set", 0, cmd_set, 0 }, |
---|
[9d4352c] | 1616 | { "transfer", 0, cmd_transfer, 0 }, |
---|
[0298d11] | 1617 | { "yes", 0, cmd_yesno, 0 }, |
---|
[8358691] | 1618 | /* Not expecting too many plugins adding root commands so just make a |
---|
| 1619 | dumb array with some empty entried at the end. */ |
---|
| 1620 | { NULL }, |
---|
| 1621 | { NULL }, |
---|
| 1622 | { NULL }, |
---|
| 1623 | { NULL }, |
---|
| 1624 | { NULL }, |
---|
| 1625 | { NULL }, |
---|
| 1626 | { NULL }, |
---|
| 1627 | { NULL }, |
---|
| 1628 | { NULL }, |
---|
[0298d11] | 1629 | }; |
---|
[5ebff60] | 1630 | static const int num_root_commands = sizeof(root_commands) / sizeof(command_t); |
---|
[8358691] | 1631 | |
---|
[5ebff60] | 1632 | gboolean root_command_add(const char *command, int params, void (*func)(irc_t *, char **args), int flags) |
---|
[8358691] | 1633 | { |
---|
| 1634 | int i; |
---|
[5ebff60] | 1635 | |
---|
| 1636 | if (root_commands[num_root_commands - 2].command) { |
---|
[8358691] | 1637 | /* Planning fail! List is full. */ |
---|
| 1638 | return FALSE; |
---|
[5ebff60] | 1639 | } |
---|
| 1640 | |
---|
| 1641 | for (i = 0; root_commands[i].command; i++) { |
---|
| 1642 | if (g_strcasecmp(root_commands[i].command, command) == 0) { |
---|
[8358691] | 1643 | return FALSE; |
---|
[5ebff60] | 1644 | } else if (g_strcasecmp(root_commands[i].command, command) > 0) { |
---|
[8358691] | 1645 | break; |
---|
[5ebff60] | 1646 | } |
---|
[8358691] | 1647 | } |
---|
[5ebff60] | 1648 | memmove(root_commands + i + 1, root_commands + i, |
---|
| 1649 | sizeof(command_t) * (num_root_commands - i - 1)); |
---|
| 1650 | |
---|
| 1651 | root_commands[i].command = g_strdup(command); |
---|
[8358691] | 1652 | root_commands[i].required_parameters = params; |
---|
| 1653 | root_commands[i].execute = func; |
---|
| 1654 | root_commands[i].flags = flags; |
---|
[5ebff60] | 1655 | |
---|
[8358691] | 1656 | return TRUE; |
---|
| 1657 | } |
---|