[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 | |
---|
| 145 | if (load) { |
---|
| 146 | status = storage_load(irc, password); |
---|
| 147 | } else { |
---|
| 148 | status = storage_check_pass(irc->user->nick, password); |
---|
| 149 | } |
---|
| 150 | |
---|
[09adf08] | 151 | switch (status) { |
---|
| 152 | case STORAGE_INVALID_PASSWORD: |
---|
[5ebff60] | 153 | irc_rootmsg(irc, "Incorrect password"); |
---|
[09adf08] | 154 | break; |
---|
| 155 | case STORAGE_NO_SUCH_USER: |
---|
[5ebff60] | 156 | irc_rootmsg(irc, "The nick is (probably) not registered"); |
---|
[09adf08] | 157 | break; |
---|
| 158 | case STORAGE_OK: |
---|
[5ebff60] | 159 | irc_rootmsg(irc, "Password accepted%s", |
---|
| 160 | load ? ", settings and accounts loaded" : ""); |
---|
| 161 | irc_setpass(irc, password); |
---|
[3183c21] | 162 | irc->status |= USTATUS_IDENTIFIED; |
---|
[5ebff60] | 163 | irc_umode_set(irc, "+R", 1); |
---|
| 164 | |
---|
| 165 | bitlbee_whatsnew(irc); |
---|
| 166 | |
---|
[e92c4f4] | 167 | /* The following code is a bit hairy now. With takeover |
---|
| 168 | support, we shouldn't immediately auto_connect in case |
---|
| 169 | we're going to offer taking over an existing session. |
---|
| 170 | Do it in 200ms since that should give the parent process |
---|
| 171 | enough time to come back to us. */ |
---|
[5ebff60] | 172 | if (load) { |
---|
| 173 | irc_channel_auto_joins(irc, NULL); |
---|
| 174 | if (!set_getbool(&irc->default_channel->set, "auto_join")) { |
---|
| 175 | irc_channel_del_user(irc->default_channel, irc->user, |
---|
| 176 | IRC_CDU_PART, "auto_join disabled " |
---|
| 177 | "for this channel."); |
---|
| 178 | } |
---|
| 179 | if (set_getbool(&irc->b->set, "auto_connect")) { |
---|
| 180 | irc->login_source_id = b_timeout_add(200, |
---|
| 181 | cmd_identify_finish, irc); |
---|
| 182 | } |
---|
[6c2404e] | 183 | } |
---|
[5ebff60] | 184 | |
---|
[e92c4f4] | 185 | /* If ipc_child_identify() returns FALSE, it means we're |
---|
| 186 | already sure that there's no takeover target (only |
---|
| 187 | possible in 1-process daemon mode). Start auto_connect |
---|
| 188 | immediately. */ |
---|
[5ebff60] | 189 | if (!ipc_child_identify(irc) && load) { |
---|
| 190 | cmd_identify_finish(irc, 0, 0); |
---|
| 191 | } |
---|
| 192 | |
---|
[09adf08] | 193 | break; |
---|
[c121f89] | 194 | case STORAGE_OTHER_ERROR: |
---|
[09adf08] | 195 | default: |
---|
[5ebff60] | 196 | irc_rootmsg(irc, "Unknown error while loading configuration"); |
---|
[09adf08] | 197 | break; |
---|
[b7d3cc34] | 198 | } |
---|
| 199 | } |
---|
| 200 | |
---|
[5ebff60] | 201 | gboolean cmd_identify_finish(gpointer data, gint fd, b_input_condition cond) |
---|
[6c2404e] | 202 | { |
---|
| 203 | char *account_on[] = { "account", "on", NULL }; |
---|
| 204 | irc_t *irc = data; |
---|
[5ebff60] | 205 | |
---|
| 206 | if (set_getbool(&irc->b->set, "auto_connect")) { |
---|
| 207 | cmd_account(irc, account_on); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | b_event_remove(irc->login_source_id); |
---|
[f545372] | 211 | irc->login_source_id = -1; |
---|
[6c2404e] | 212 | return FALSE; |
---|
| 213 | } |
---|
| 214 | |
---|
[5ebff60] | 215 | static void cmd_register(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 216 | { |
---|
[8d93b4a] | 217 | char s[16]; |
---|
[5ebff60] | 218 | |
---|
| 219 | if (global.conf->authmode == AUTHMODE_REGISTERED) { |
---|
| 220 | irc_rootmsg(irc, "This server does not allow registering new accounts"); |
---|
[f73b969] | 221 | return; |
---|
[b7d3cc34] | 222 | } |
---|
[5ebff60] | 223 | |
---|
| 224 | if (cmd[1] == NULL) { |
---|
| 225 | irc_rootmsg(irc, "About to register, use /OPER to enter the password"); |
---|
[060d066] | 226 | irc->status |= OPER_HACK_REGISTER; |
---|
| 227 | return; |
---|
| 228 | } |
---|
[1ee6c18] | 229 | |
---|
[5ebff60] | 230 | switch (storage_save(irc, cmd[1], FALSE)) { |
---|
| 231 | case STORAGE_ALREADY_EXISTS: |
---|
| 232 | irc_rootmsg(irc, "Nick is already registered"); |
---|
| 233 | break; |
---|
[a1f17d4] | 234 | |
---|
[5ebff60] | 235 | case STORAGE_OK: |
---|
| 236 | irc_rootmsg(irc, "Account successfully created"); |
---|
| 237 | irc_setpass(irc, cmd[1]); |
---|
| 238 | irc->status |= USTATUS_IDENTIFIED; |
---|
| 239 | irc_umode_set(irc, "+R", 1); |
---|
| 240 | |
---|
| 241 | /* Set this var now, or anyone who logs in to his/her |
---|
| 242 | newly created account for the first time gets the |
---|
| 243 | whatsnew story. */ |
---|
| 244 | g_snprintf(s, sizeof(s), "%d", BITLBEE_VERSION_CODE); |
---|
| 245 | set_setstr(&irc->b->set, "last_version", s); |
---|
| 246 | break; |
---|
| 247 | |
---|
| 248 | default: |
---|
| 249 | irc_rootmsg(irc, "Error registering"); |
---|
| 250 | break; |
---|
[b7d3cc34] | 251 | } |
---|
| 252 | } |
---|
| 253 | |
---|
[5ebff60] | 254 | static void cmd_drop(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 255 | { |
---|
[a1f17d4] | 256 | storage_status_t status; |
---|
[5ebff60] | 257 | |
---|
| 258 | status = storage_remove(irc->user->nick, cmd[1]); |
---|
[a1f17d4] | 259 | switch (status) { |
---|
| 260 | case STORAGE_NO_SUCH_USER: |
---|
[5ebff60] | 261 | irc_rootmsg(irc, "That account does not exist"); |
---|
[f73b969] | 262 | break; |
---|
[a1f17d4] | 263 | case STORAGE_INVALID_PASSWORD: |
---|
[5ebff60] | 264 | irc_rootmsg(irc, "Password invalid"); |
---|
[f73b969] | 265 | break; |
---|
[a1f17d4] | 266 | case STORAGE_OK: |
---|
[5ebff60] | 267 | irc_setpass(irc, NULL); |
---|
[79e826a] | 268 | irc->status &= ~USTATUS_IDENTIFIED; |
---|
[5ebff60] | 269 | irc_umode_set(irc, "-R", 1); |
---|
| 270 | irc_rootmsg(irc, "Account `%s' removed", irc->user->nick); |
---|
[f73b969] | 271 | break; |
---|
[a1f17d4] | 272 | default: |
---|
[5ebff60] | 273 | irc_rootmsg(irc, "Error: `%d'", status); |
---|
[f73b969] | 274 | break; |
---|
[b7d3cc34] | 275 | } |
---|
| 276 | } |
---|
[1f92a58] | 277 | |
---|
[5ebff60] | 278 | static void cmd_save(irc_t *irc, char **cmd) |
---|
[1f92a58] | 279 | { |
---|
[5ebff60] | 280 | if ((irc->status & USTATUS_IDENTIFIED) == 0) { |
---|
| 281 | irc_rootmsg(irc, "Please create an account first (see \x02help register\x02)"); |
---|
| 282 | } else if (storage_save(irc, NULL, TRUE) == STORAGE_OK) { |
---|
| 283 | irc_rootmsg(irc, "Configuration saved"); |
---|
| 284 | } else { |
---|
| 285 | irc_rootmsg(irc, "Configuration could not be saved!"); |
---|
| 286 | } |
---|
[1f92a58] | 287 | } |
---|
[b7d3cc34] | 288 | |
---|
[5ebff60] | 289 | static void cmd_showset(irc_t *irc, set_t **head, char *key) |
---|
[f3579fd] | 290 | { |
---|
[09d4922] | 291 | set_t *set; |
---|
[f536a99] | 292 | char *val; |
---|
[5ebff60] | 293 | |
---|
| 294 | if ((val = set_getstr(head, key))) { |
---|
| 295 | irc_rootmsg(irc, "%s = `%s'", key, val); |
---|
| 296 | } else if (!(set = set_find(head, key))) { |
---|
| 297 | irc_rootmsg(irc, "Setting `%s' does not exist.", key); |
---|
| 298 | if (*head == irc->b->set) { |
---|
| 299 | irc_rootmsg(irc, "It might be an account or channel setting. " |
---|
| 300 | "See \x02help account set\x02 and \x02help channel set\x02."); |
---|
| 301 | } |
---|
| 302 | } else if (set->flags & SET_PASSWORD) { |
---|
| 303 | irc_rootmsg(irc, "%s = `********' (hidden)", key); |
---|
| 304 | } else { |
---|
| 305 | irc_rootmsg(irc, "%s is empty", key); |
---|
[5613af7] | 306 | } |
---|
[f3579fd] | 307 | } |
---|
| 308 | |
---|
[5ebff60] | 309 | typedef set_t** (*cmd_set_findhead)(irc_t*, char*); |
---|
| 310 | typedef int (*cmd_set_checkflags)(irc_t*, set_t *set); |
---|
[e7bc722] | 311 | |
---|
[5ebff60] | 312 | static int cmd_set_real(irc_t *irc, char **cmd, set_t **head, cmd_set_checkflags checkflags) |
---|
[e7bc722] | 313 | { |
---|
[e907683] | 314 | char *set_name = NULL, *value = NULL; |
---|
| 315 | gboolean del = FALSE; |
---|
[5ebff60] | 316 | |
---|
| 317 | if (cmd[1] && g_strncasecmp(cmd[1], "-del", 4) == 0) { |
---|
| 318 | MIN_ARGS(2, 0); |
---|
[e907683] | 319 | set_name = cmd[2]; |
---|
| 320 | del = TRUE; |
---|
[5ebff60] | 321 | } else { |
---|
[e907683] | 322 | set_name = cmd[1]; |
---|
| 323 | value = cmd[2]; |
---|
[e7bc722] | 324 | } |
---|
[5ebff60] | 325 | |
---|
| 326 | if (set_name && (value || del)) { |
---|
| 327 | set_t *s = set_find(head, set_name); |
---|
[e7bc722] | 328 | int st; |
---|
[5ebff60] | 329 | |
---|
| 330 | if (s && checkflags && checkflags(irc, s) == 0) { |
---|
[e7bc722] | 331 | return 0; |
---|
[5ebff60] | 332 | } |
---|
| 333 | |
---|
| 334 | if (del) { |
---|
| 335 | st = set_reset(head, set_name); |
---|
| 336 | } else { |
---|
| 337 | st = set_setstr(head, set_name, value); |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | if (set_getstr(head, set_name) == NULL && |
---|
| 341 | set_find(head, set_name)) { |
---|
[e907683] | 342 | /* This happens when changing the passwd, for example. |
---|
| 343 | Showing these msgs instead gives slightly clearer |
---|
| 344 | feedback. */ |
---|
[5ebff60] | 345 | if (st) { |
---|
| 346 | irc_rootmsg(irc, "Setting changed successfully"); |
---|
| 347 | } else { |
---|
| 348 | irc_rootmsg(irc, "Failed to change setting"); |
---|
| 349 | } |
---|
| 350 | } else { |
---|
| 351 | cmd_showset(irc, head, set_name); |
---|
[e7bc722] | 352 | } |
---|
[5ebff60] | 353 | } else if (set_name) { |
---|
| 354 | cmd_showset(irc, head, set_name); |
---|
| 355 | } else { |
---|
[e7bc722] | 356 | set_t *s = *head; |
---|
[5ebff60] | 357 | while (s) { |
---|
| 358 | if (set_isvisible(s)) { |
---|
| 359 | cmd_showset(irc, &s, s->key); |
---|
| 360 | } |
---|
[e7bc722] | 361 | s = s->next; |
---|
| 362 | } |
---|
| 363 | } |
---|
[5ebff60] | 364 | |
---|
[e7bc722] | 365 | return 1; |
---|
| 366 | } |
---|
| 367 | |
---|
[5ebff60] | 368 | static int cmd_account_set_checkflags(irc_t *irc, set_t *s) |
---|
[c05eb5b] | 369 | { |
---|
| 370 | account_t *a = s->data; |
---|
[5ebff60] | 371 | |
---|
| 372 | if (a->ic && s && s->flags & ACC_SET_OFFLINE_ONLY) { |
---|
| 373 | irc_rootmsg(irc, "This setting can only be changed when the account is %s-line", "off"); |
---|
[c05eb5b] | 374 | return 0; |
---|
[5ebff60] | 375 | } else if (!a->ic && s && s->flags & ACC_SET_ONLINE_ONLY) { |
---|
| 376 | irc_rootmsg(irc, "This setting can only be changed when the account is %s-line", "on"); |
---|
[c05eb5b] | 377 | return 0; |
---|
| 378 | } |
---|
[5ebff60] | 379 | |
---|
[c05eb5b] | 380 | return 1; |
---|
| 381 | } |
---|
| 382 | |
---|
[5ebff60] | 383 | static void cmd_account(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 384 | { |
---|
| 385 | account_t *a; |
---|
[c7eb771] | 386 | int len; |
---|
[5ebff60] | 387 | |
---|
| 388 | if (global.conf->authmode == AUTHMODE_REGISTERED && !(irc->status & USTATUS_IDENTIFIED)) { |
---|
| 389 | irc_rootmsg(irc, "This server only accepts registered users"); |
---|
[f73b969] | 390 | return; |
---|
[b7d3cc34] | 391 | } |
---|
[5ebff60] | 392 | |
---|
| 393 | len = strlen(cmd[1]); |
---|
| 394 | |
---|
| 395 | if (len >= 1 && g_strncasecmp(cmd[1], "add", len) == 0) { |
---|
[7b23afd] | 396 | struct prpl *prpl; |
---|
[5ebff60] | 397 | |
---|
| 398 | MIN_ARGS(3); |
---|
| 399 | |
---|
| 400 | if (cmd[4] == NULL) { |
---|
| 401 | for (a = irc->b->accounts; a; a = a->next) { |
---|
| 402 | if (strcmp(a->pass, PASSWORD_PENDING) == 0) { |
---|
| 403 | irc_rootmsg(irc, "Enter password for account %s " |
---|
| 404 | "first (use /OPER)", a->tag); |
---|
[9564e55] | 405 | return; |
---|
| 406 | } |
---|
[5ebff60] | 407 | } |
---|
| 408 | |
---|
[35987a1] | 409 | irc->status |= OPER_HACK_ACCOUNT_PASSWORD; |
---|
[da60f28] | 410 | } |
---|
[5ebff60] | 411 | |
---|
| 412 | prpl = find_protocol(cmd[2]); |
---|
| 413 | |
---|
| 414 | if (prpl == NULL) { |
---|
| 415 | irc_rootmsg(irc, "Unknown protocol"); |
---|
[f73b969] | 416 | return; |
---|
[b7d3cc34] | 417 | } |
---|
[5ebff60] | 418 | |
---|
| 419 | for (a = irc->b->accounts; a; a = a->next) { |
---|
| 420 | if (a->prpl == prpl && prpl->handle_cmp(a->user, cmd[3]) == 0) { |
---|
| 421 | irc_rootmsg(irc, "Warning: You already have an account with " |
---|
| 422 | "protocol `%s' and username `%s'. Are you accidentally " |
---|
| 423 | "trying to add it twice?", prpl->name, cmd[3]); |
---|
| 424 | } |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | a = account_add(irc->b, prpl, cmd[3], cmd[4] ? cmd[4] : PASSWORD_PENDING); |
---|
| 428 | if (cmd[5]) { |
---|
| 429 | irc_rootmsg(irc, "Warning: Passing a servername/other flags to `account add' " |
---|
| 430 | "is now deprecated. Use `account set' instead."); |
---|
| 431 | set_setstr(&a->set, "server", cmd[5]); |
---|
[30ce1ce] | 432 | } |
---|
[5ebff60] | 433 | |
---|
| 434 | irc_rootmsg(irc, "Account successfully added with tag %s", a->tag); |
---|
| 435 | |
---|
| 436 | if (cmd[4] == NULL) { |
---|
| 437 | set_t *oauth = set_find(&a->set, "oauth"); |
---|
| 438 | if (oauth && bool2int(set_value(oauth))) { |
---|
[ce199b7] | 439 | *a->pass = '\0'; |
---|
[5ebff60] | 440 | irc_rootmsg(irc, "No need to enter a password for this " |
---|
| 441 | "account since it's using OAuth"); |
---|
| 442 | } else { |
---|
| 443 | irc_rootmsg(irc, "You can now use the /OPER command to " |
---|
| 444 | "enter the password"); |
---|
| 445 | if (oauth) { |
---|
| 446 | irc_rootmsg(irc, "Alternatively, enable OAuth if " |
---|
| 447 | "the account supports it: account %s " |
---|
| 448 | "set oauth on", a->tag); |
---|
| 449 | } |
---|
[ce199b7] | 450 | } |
---|
| 451 | } |
---|
[5ebff60] | 452 | |
---|
[e907683] | 453 | return; |
---|
[5ebff60] | 454 | } else if (len >= 1 && g_strncasecmp(cmd[1], "list", len) == 0) { |
---|
[b7d3cc34] | 455 | int i = 0; |
---|
[5ebff60] | 456 | |
---|
| 457 | if (strchr(irc->umode, 'b')) { |
---|
| 458 | irc_rootmsg(irc, "Account list:"); |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | for (a = irc->b->accounts; a; a = a->next) { |
---|
[b7d3cc34] | 462 | char *con; |
---|
[5ebff60] | 463 | |
---|
| 464 | if (a->ic && (a->ic->flags & OPT_LOGGED_IN)) { |
---|
[b7d3cc34] | 465 | con = " (connected)"; |
---|
[5ebff60] | 466 | } else if (a->ic) { |
---|
[b7d3cc34] | 467 | con = " (connecting)"; |
---|
[5ebff60] | 468 | } else if (a->reconnect) { |
---|
[b7d3cc34] | 469 | con = " (awaiting reconnect)"; |
---|
[5ebff60] | 470 | } else { |
---|
[b7d3cc34] | 471 | con = ""; |
---|
[5ebff60] | 472 | } |
---|
| 473 | |
---|
| 474 | irc_rootmsg(irc, "%2d (%s): %s, %s%s", i, a->tag, a->prpl->name, a->user, con); |
---|
| 475 | |
---|
| 476 | i++; |
---|
[b7d3cc34] | 477 | } |
---|
[5ebff60] | 478 | irc_rootmsg(irc, "End of account list"); |
---|
| 479 | |
---|
[e907683] | 480 | return; |
---|
[5ebff60] | 481 | } else if (cmd[2]) { |
---|
[e907683] | 482 | /* Try the following two only if cmd[2] == NULL */ |
---|
[5ebff60] | 483 | } else if (len >= 2 && g_strncasecmp(cmd[1], "on", len) == 0) { |
---|
| 484 | if (irc->b->accounts) { |
---|
| 485 | irc_rootmsg(irc, "Trying to get all accounts connected..."); |
---|
| 486 | |
---|
| 487 | for (a = irc->b->accounts; a; a = a->next) { |
---|
| 488 | if (!a->ic && a->auto_connect) { |
---|
| 489 | if (strcmp(a->pass, PASSWORD_PENDING) == 0) { |
---|
| 490 | irc_rootmsg(irc, "Enter password for account %s " |
---|
| 491 | "first (use /OPER)", a->tag); |
---|
| 492 | } else { |
---|
| 493 | account_on(irc->b, a); |
---|
| 494 | } |
---|
[9564e55] | 495 | } |
---|
[5ebff60] | 496 | } |
---|
| 497 | } else { |
---|
| 498 | irc_rootmsg(irc, "No accounts known. Use `account add' to add one."); |
---|
[b7d3cc34] | 499 | } |
---|
[5ebff60] | 500 | |
---|
[e907683] | 501 | return; |
---|
[5ebff60] | 502 | } else if (len >= 2 && g_strncasecmp(cmd[1], "off", len) == 0) { |
---|
| 503 | irc_rootmsg(irc, "Deactivating all active (re)connections..."); |
---|
| 504 | |
---|
| 505 | for (a = irc->b->accounts; a; a = a->next) { |
---|
| 506 | if (a->ic) { |
---|
| 507 | account_off(irc->b, a); |
---|
| 508 | } else if (a->reconnect) { |
---|
| 509 | cancel_auto_reconnect(a); |
---|
| 510 | } |
---|
[e907683] | 511 | } |
---|
[5ebff60] | 512 | |
---|
[e907683] | 513 | return; |
---|
| 514 | } |
---|
[5ebff60] | 515 | |
---|
| 516 | MIN_ARGS(2); |
---|
| 517 | len = strlen(cmd[2]); |
---|
| 518 | |
---|
[e907683] | 519 | /* At least right now, don't accept on/off/set/del as account IDs even |
---|
| 520 | if they're a proper match, since people not familiar with the new |
---|
| 521 | syntax yet may get a confusing/nasty surprise. */ |
---|
[5ebff60] | 522 | if (g_strcasecmp(cmd[1], "on") == 0 || |
---|
| 523 | g_strcasecmp(cmd[1], "off") == 0 || |
---|
| 524 | g_strcasecmp(cmd[1], "set") == 0 || |
---|
| 525 | g_strcasecmp(cmd[1], "del") == 0 || |
---|
| 526 | (a = account_get(irc->b, cmd[1])) == NULL) { |
---|
| 527 | irc_rootmsg(irc, "Could not find account `%s'.", cmd[1]); |
---|
| 528 | |
---|
[e907683] | 529 | return; |
---|
| 530 | } |
---|
[5ebff60] | 531 | |
---|
| 532 | if (len >= 1 && g_strncasecmp(cmd[2], "del", len) == 0) { |
---|
| 533 | if (a->ic) { |
---|
| 534 | irc_rootmsg(irc, "Account is still logged in, can't delete"); |
---|
| 535 | } else { |
---|
| 536 | account_del(irc->b, a); |
---|
| 537 | irc_rootmsg(irc, "Account deleted"); |
---|
[e907683] | 538 | } |
---|
[5ebff60] | 539 | } else if (len >= 2 && g_strncasecmp(cmd[2], "on", len) == 0) { |
---|
| 540 | if (a->ic) { |
---|
| 541 | irc_rootmsg(irc, "Account already online"); |
---|
| 542 | } else if (strcmp(a->pass, PASSWORD_PENDING) == 0) { |
---|
| 543 | irc_rootmsg(irc, "Enter password for account %s " |
---|
| 544 | "first (use /OPER)", a->tag); |
---|
| 545 | } else { |
---|
| 546 | account_on(irc->b, a); |
---|
[e907683] | 547 | } |
---|
[5ebff60] | 548 | } else if (len >= 2 && g_strncasecmp(cmd[2], "off", len) == 0) { |
---|
| 549 | if (a->ic) { |
---|
| 550 | account_off(irc->b, a); |
---|
| 551 | } else if (a->reconnect) { |
---|
| 552 | cancel_auto_reconnect(a); |
---|
| 553 | irc_rootmsg(irc, "Reconnect cancelled"); |
---|
| 554 | } else { |
---|
| 555 | irc_rootmsg(irc, "Account already offline"); |
---|
[e907683] | 556 | } |
---|
[5ebff60] | 557 | } else if (len >= 1 && g_strncasecmp(cmd[2], "set", len) == 0) { |
---|
| 558 | cmd_set_real(irc, cmd + 2, &a->set, cmd_account_set_checkflags); |
---|
| 559 | } else { |
---|
| 560 | irc_rootmsg(irc, |
---|
| 561 | "Unknown command: %s [...] %s. Please use \x02help commands\x02 to get a list of available commands.", "account", |
---|
| 562 | cmd[2]); |
---|
[b7d3cc34] | 563 | } |
---|
| 564 | } |
---|
| 565 | |
---|
[5ebff60] | 566 | static void cmd_channel(irc_t *irc, char **cmd) |
---|
[c133d4b8] | 567 | { |
---|
| 568 | irc_channel_t *ic; |
---|
[c7eb771] | 569 | int len; |
---|
[5ebff60] | 570 | |
---|
| 571 | len = strlen(cmd[1]); |
---|
| 572 | |
---|
| 573 | if (len >= 1 && g_strncasecmp(cmd[1], "list", len) == 0) { |
---|
[36562b0] | 574 | GSList *l; |
---|
| 575 | int i = 0; |
---|
[5ebff60] | 576 | |
---|
| 577 | if (strchr(irc->umode, 'b')) { |
---|
| 578 | irc_rootmsg(irc, "Channel list:"); |
---|
| 579 | } |
---|
| 580 | |
---|
| 581 | for (l = irc->channels; l; l = l->next) { |
---|
[36562b0] | 582 | irc_channel_t *ic = l->data; |
---|
[5ebff60] | 583 | |
---|
| 584 | irc_rootmsg(irc, "%2d. %s, %s channel%s", i, ic->name, |
---|
| 585 | set_getstr(&ic->set, "type"), |
---|
| 586 | ic->flags & IRC_CHANNEL_JOINED ? " (joined)" : ""); |
---|
| 587 | |
---|
| 588 | i++; |
---|
[36562b0] | 589 | } |
---|
[5ebff60] | 590 | irc_rootmsg(irc, "End of channel list"); |
---|
| 591 | |
---|
[e907683] | 592 | return; |
---|
[36562b0] | 593 | } |
---|
[5ebff60] | 594 | |
---|
| 595 | if ((ic = irc_channel_get(irc, cmd[1])) == NULL) { |
---|
[4f22a68c] | 596 | /* If this doesn't match any channel, maybe this is the short |
---|
| 597 | syntax (only works when used inside a channel). */ |
---|
[5ebff60] | 598 | if ((ic = irc->root->last_channel) && |
---|
| 599 | (len = strlen(cmd[1])) && |
---|
| 600 | g_strncasecmp(cmd[1], "set", len) == 0) { |
---|
| 601 | cmd_set_real(irc, cmd + 1, &ic->set, NULL); |
---|
| 602 | } else { |
---|
| 603 | irc_rootmsg(irc, "Could not find channel `%s'", cmd[1]); |
---|
| 604 | } |
---|
| 605 | |
---|
[e907683] | 606 | return; |
---|
| 607 | } |
---|
[5ebff60] | 608 | |
---|
| 609 | MIN_ARGS(2); |
---|
| 610 | len = strlen(cmd[2]); |
---|
| 611 | |
---|
| 612 | if (len >= 1 && g_strncasecmp(cmd[2], "set", len) == 0) { |
---|
| 613 | cmd_set_real(irc, cmd + 2, &ic->set, NULL); |
---|
| 614 | } else if (len >= 1 && g_strncasecmp(cmd[2], "del", len) == 0) { |
---|
| 615 | if (!(ic->flags & IRC_CHANNEL_JOINED) && |
---|
| 616 | ic != ic->irc->default_channel) { |
---|
| 617 | irc_rootmsg(irc, "Channel %s deleted.", ic->name); |
---|
| 618 | irc_channel_free(ic); |
---|
| 619 | } else { |
---|
| 620 | irc_rootmsg(irc, "Couldn't remove channel (main channel %s or " |
---|
| 621 | "channels you're still in cannot be deleted).", |
---|
| 622 | irc->default_channel->name); |
---|
[a4d920b] | 623 | } |
---|
[5ebff60] | 624 | } else { |
---|
| 625 | irc_rootmsg(irc, |
---|
| 626 | "Unknown command: %s [...] %s. Please use \x02help commands\x02 to get a list of available commands.", "channel", |
---|
| 627 | cmd[1]); |
---|
[c133d4b8] | 628 | } |
---|
| 629 | } |
---|
| 630 | |
---|
[5ebff60] | 631 | static void cmd_add(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 632 | { |
---|
| 633 | account_t *a; |
---|
[f0cb961] | 634 | int add_on_server = 1; |
---|
[06eef80] | 635 | char *handle = NULL, *s; |
---|
[5ebff60] | 636 | |
---|
| 637 | if (g_strcasecmp(cmd[1], "-tmp") == 0) { |
---|
| 638 | MIN_ARGS(3); |
---|
[f0cb961] | 639 | add_on_server = 0; |
---|
[5ebff60] | 640 | cmd++; |
---|
[f8de26f] | 641 | } |
---|
[5ebff60] | 642 | |
---|
| 643 | if (!(a = account_get(irc->b, cmd[1]))) { |
---|
| 644 | irc_rootmsg(irc, "Invalid account"); |
---|
[f73b969] | 645 | return; |
---|
[5ebff60] | 646 | } else if (!(a->ic && (a->ic->flags & OPT_LOGGED_IN))) { |
---|
| 647 | irc_rootmsg(irc, "That account is not on-line"); |
---|
[f73b969] | 648 | return; |
---|
[b7d3cc34] | 649 | } |
---|
[5ebff60] | 650 | |
---|
| 651 | if (cmd[3]) { |
---|
| 652 | if (!nick_ok(irc, cmd[3])) { |
---|
| 653 | irc_rootmsg(irc, "The requested nick `%s' is invalid", cmd[3]); |
---|
[f73b969] | 654 | return; |
---|
[5ebff60] | 655 | } else if (irc_user_by_name(irc, cmd[3])) { |
---|
| 656 | irc_rootmsg(irc, "The requested nick `%s' already exists", cmd[3]); |
---|
[f73b969] | 657 | return; |
---|
[5ebff60] | 658 | } else { |
---|
| 659 | nick_set_raw(a, cmd[2], cmd[3]); |
---|
[b7d3cc34] | 660 | } |
---|
| 661 | } |
---|
[5ebff60] | 662 | |
---|
| 663 | if ((a->flags & ACC_FLAG_HANDLE_DOMAINS) && cmd[2][0] != '_' && |
---|
| 664 | (!(s = strchr(cmd[2], '@')) || s[1] == '\0')) { |
---|
[06eef80] | 665 | /* If there's no @ or it's the last char, append the user's |
---|
| 666 | domain name now. Exclude handles starting with a _ so |
---|
| 667 | adding _xmlconsole will keep working. */ |
---|
[5ebff60] | 668 | if (s) { |
---|
[06eef80] | 669 | *s = '\0'; |
---|
[5ebff60] | 670 | } |
---|
| 671 | if ((s = strchr(a->user, '@'))) { |
---|
| 672 | cmd[2] = handle = g_strconcat(cmd[2], s, NULL); |
---|
| 673 | } |
---|
[06eef80] | 674 | } |
---|
[5ebff60] | 675 | |
---|
| 676 | if (add_on_server) { |
---|
[f1d488e] | 677 | irc_channel_t *ic; |
---|
| 678 | char *s, *group = NULL;; |
---|
[5ebff60] | 679 | |
---|
| 680 | if ((ic = irc->root->last_channel) && |
---|
| 681 | (s = set_getstr(&ic->set, "fill_by")) && |
---|
| 682 | strcmp(s, "group") == 0 && |
---|
| 683 | (group = set_getstr(&ic->set, "group"))) { |
---|
| 684 | irc_rootmsg(irc, "Adding `%s' to contact list (group %s)", |
---|
| 685 | cmd[2], group); |
---|
| 686 | } else { |
---|
| 687 | irc_rootmsg(irc, "Adding `%s' to contact list", cmd[2]); |
---|
| 688 | } |
---|
| 689 | |
---|
| 690 | a->prpl->add_buddy(a->ic, cmd[2], group); |
---|
| 691 | } else { |
---|
[f1d488e] | 692 | bee_user_t *bu; |
---|
| 693 | irc_user_t *iu; |
---|
[5ebff60] | 694 | |
---|
[dbb0ce3] | 695 | /* Only for add -tmp. For regular adds, this callback will |
---|
| 696 | be called once the IM server confirms. */ |
---|
[5ebff60] | 697 | if ((bu = bee_user_new(irc->b, a->ic, cmd[2], BEE_USER_LOCAL)) && |
---|
| 698 | (iu = bu->ui_data)) { |
---|
| 699 | irc_rootmsg(irc, "Temporarily assigned nickname `%s' " |
---|
| 700 | "to contact `%s'", iu->nick, cmd[2]); |
---|
| 701 | } |
---|
[f1d488e] | 702 | } |
---|
[5ebff60] | 703 | |
---|
| 704 | g_free(handle); |
---|
[b7d3cc34] | 705 | } |
---|
| 706 | |
---|
[5ebff60] | 707 | static void cmd_remove(irc_t *irc, char **cmd) |
---|
[dbb0ce3] | 708 | { |
---|
| 709 | irc_user_t *iu; |
---|
| 710 | bee_user_t *bu; |
---|
| 711 | char *s; |
---|
[5ebff60] | 712 | |
---|
| 713 | if (!(iu = irc_user_by_name(irc, cmd[1])) || !(bu = iu->bu)) { |
---|
| 714 | irc_rootmsg(irc, "Buddy `%s' not found", cmd[1]); |
---|
[dbb0ce3] | 715 | return; |
---|
| 716 | } |
---|
[5ebff60] | 717 | s = g_strdup(bu->handle); |
---|
| 718 | |
---|
| 719 | bu->ic->acc->prpl->remove_buddy(bu->ic, bu->handle, NULL); |
---|
| 720 | nick_del(bu); |
---|
| 721 | if (g_slist_find(irc->users, iu)) { |
---|
| 722 | bee_user_free(irc->b, bu); |
---|
| 723 | } |
---|
| 724 | |
---|
| 725 | irc_rootmsg(irc, "Buddy `%s' (nick %s) removed from contact list", s, cmd[1]); |
---|
| 726 | g_free(s); |
---|
| 727 | |
---|
[dbb0ce3] | 728 | return; |
---|
| 729 | } |
---|
| 730 | |
---|
[5ebff60] | 731 | static void cmd_info(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 732 | { |
---|
[0da65d5] | 733 | struct im_connection *ic; |
---|
[b7d3cc34] | 734 | account_t *a; |
---|
[5ebff60] | 735 | |
---|
| 736 | if (!cmd[2]) { |
---|
| 737 | irc_user_t *iu = irc_user_by_name(irc, cmd[1]); |
---|
| 738 | if (!iu || !iu->bu) { |
---|
| 739 | irc_rootmsg(irc, "Nick `%s' does not exist", cmd[1]); |
---|
[f73b969] | 740 | return; |
---|
[b7d3cc34] | 741 | } |
---|
[aa44bdd] | 742 | ic = iu->bu->ic; |
---|
| 743 | cmd[2] = iu->bu->handle; |
---|
[5ebff60] | 744 | } else if (!(a = account_get(irc->b, cmd[1]))) { |
---|
| 745 | irc_rootmsg(irc, "Invalid account"); |
---|
[f73b969] | 746 | return; |
---|
[5ebff60] | 747 | } else if (!((ic = a->ic) && (a->ic->flags & OPT_LOGGED_IN))) { |
---|
| 748 | irc_rootmsg(irc, "That account is not on-line"); |
---|
[f73b969] | 749 | return; |
---|
[b7d3cc34] | 750 | } |
---|
[5ebff60] | 751 | |
---|
| 752 | if (!ic->acc->prpl->get_info) { |
---|
| 753 | irc_rootmsg(irc, "Command `%s' not supported by this protocol", cmd[0]); |
---|
| 754 | } else { |
---|
| 755 | ic->acc->prpl->get_info(ic, cmd[2]); |
---|
[f73b969] | 756 | } |
---|
[b7d3cc34] | 757 | } |
---|
| 758 | |
---|
[5ebff60] | 759 | static void cmd_rename(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 760 | { |
---|
[9a9b520] | 761 | irc_user_t *iu, *old; |
---|
[5ebff60] | 762 | gboolean del = g_strcasecmp(cmd[1], "-del") == 0; |
---|
| 763 | |
---|
| 764 | iu = irc_user_by_name(irc, cmd[del ? 2 : 1]); |
---|
| 765 | |
---|
| 766 | if (iu == NULL) { |
---|
| 767 | irc_rootmsg(irc, "Nick `%s' does not exist", cmd[1]); |
---|
| 768 | } else if (del) { |
---|
| 769 | if (iu->bu) { |
---|
| 770 | bee_irc_user_nick_reset(iu); |
---|
| 771 | } |
---|
| 772 | irc_rootmsg(irc, "Nickname reset to `%s'", iu->nick); |
---|
| 773 | } else if (iu == irc->user) { |
---|
| 774 | irc_rootmsg(irc, "Use /nick to change your own nickname"); |
---|
| 775 | } else if (!nick_ok(irc, cmd[2])) { |
---|
| 776 | irc_rootmsg(irc, "Nick `%s' is invalid", cmd[2]); |
---|
| 777 | } else if ((old = irc_user_by_name(irc, cmd[2])) && old != iu) { |
---|
| 778 | irc_rootmsg(irc, "Nick `%s' already exists", cmd[2]); |
---|
| 779 | } else { |
---|
| 780 | if (!irc_user_set_nick(iu, cmd[2])) { |
---|
| 781 | irc_rootmsg(irc, "Error while changing nick"); |
---|
[57c96f7] | 782 | return; |
---|
| 783 | } |
---|
[5ebff60] | 784 | |
---|
| 785 | if (iu == irc->root) { |
---|
[7125cb3] | 786 | /* If we're called internally (user did "set root_nick"), |
---|
| 787 | let's not go O(INF). :-) */ |
---|
[5ebff60] | 788 | if (strcmp(cmd[0], "set_rename") != 0) { |
---|
| 789 | set_setstr(&irc->b->set, "root_nick", cmd[2]); |
---|
| 790 | } |
---|
| 791 | } else if (iu->bu) { |
---|
| 792 | nick_set(iu->bu, cmd[2]); |
---|
[f73b969] | 793 | } |
---|
[5ebff60] | 794 | |
---|
| 795 | irc_rootmsg(irc, "Nick successfully changed"); |
---|
[b7d3cc34] | 796 | } |
---|
| 797 | } |
---|
| 798 | |
---|
[5ebff60] | 799 | char *set_eval_root_nick(set_t *set, char *new_nick) |
---|
[1195cec] | 800 | { |
---|
| 801 | irc_t *irc = set->data; |
---|
[5ebff60] | 802 | |
---|
| 803 | if (strcmp(irc->root->nick, new_nick) != 0) { |
---|
[0a6e5d1] | 804 | char *cmd[] = { "set_rename", irc->root->nick, new_nick, NULL }; |
---|
[5ebff60] | 805 | |
---|
| 806 | cmd_rename(irc, cmd); |
---|
[1195cec] | 807 | } |
---|
[5ebff60] | 808 | |
---|
| 809 | return strcmp(irc->root->nick, new_nick) == 0 ? new_nick : SET_INVALID; |
---|
[1195cec] | 810 | } |
---|
[0baed0d] | 811 | |
---|
[5ebff60] | 812 | static void cmd_block(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 813 | { |
---|
[0da65d5] | 814 | struct im_connection *ic; |
---|
[b7d3cc34] | 815 | account_t *a; |
---|
[5ebff60] | 816 | |
---|
| 817 | if (!cmd[2] && (a = account_get(irc->b, cmd[1])) && a->ic) { |
---|
[87b6a3e] | 818 | char *format; |
---|
| 819 | GSList *l; |
---|
[5ebff60] | 820 | |
---|
| 821 | if (strchr(irc->umode, 'b') != NULL) { |
---|
[87b6a3e] | 822 | format = "%s\t%s"; |
---|
[5ebff60] | 823 | } else { |
---|
[57ef864] | 824 | format = "%-32.32s %-16.16s"; |
---|
[5ebff60] | 825 | } |
---|
| 826 | |
---|
| 827 | irc_rootmsg(irc, format, "Handle", "Nickname"); |
---|
| 828 | for (l = a->ic->deny; l; l = l->next) { |
---|
| 829 | bee_user_t *bu = bee_user_by_handle(irc->b, a->ic, l->data); |
---|
[2272cb3] | 830 | irc_user_t *iu = bu ? bu->ui_data : NULL; |
---|
[5ebff60] | 831 | irc_rootmsg(irc, format, l->data, iu ? iu->nick : "(none)"); |
---|
[87b6a3e] | 832 | } |
---|
[5ebff60] | 833 | irc_rootmsg(irc, "End of list."); |
---|
| 834 | |
---|
[87b6a3e] | 835 | return; |
---|
[5ebff60] | 836 | } else if (!cmd[2]) { |
---|
| 837 | irc_user_t *iu = irc_user_by_name(irc, cmd[1]); |
---|
| 838 | if (!iu || !iu->bu) { |
---|
| 839 | irc_rootmsg(irc, "Nick `%s' does not exist", cmd[1]); |
---|
[f73b969] | 840 | return; |
---|
[b7d3cc34] | 841 | } |
---|
[2272cb3] | 842 | ic = iu->bu->ic; |
---|
| 843 | cmd[2] = iu->bu->handle; |
---|
[5ebff60] | 844 | } else if (!(a = account_get(irc->b, cmd[1]))) { |
---|
| 845 | irc_rootmsg(irc, "Invalid account"); |
---|
[f73b969] | 846 | return; |
---|
[5ebff60] | 847 | } else if (!((ic = a->ic) && (a->ic->flags & OPT_LOGGED_IN))) { |
---|
| 848 | irc_rootmsg(irc, "That account is not on-line"); |
---|
[f73b969] | 849 | return; |
---|
[b7d3cc34] | 850 | } |
---|
[5ebff60] | 851 | |
---|
| 852 | if (!ic->acc->prpl->add_deny || !ic->acc->prpl->rem_permit) { |
---|
| 853 | irc_rootmsg(irc, "Command `%s' not supported by this protocol", cmd[0]); |
---|
| 854 | } else { |
---|
| 855 | imc_rem_allow(ic, cmd[2]); |
---|
| 856 | imc_add_block(ic, cmd[2]); |
---|
| 857 | irc_rootmsg(irc, "Buddy `%s' moved from allow- to block-list", cmd[2]); |
---|
[b7d3cc34] | 858 | } |
---|
| 859 | } |
---|
| 860 | |
---|
[5ebff60] | 861 | static void cmd_allow(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 862 | { |
---|
[0da65d5] | 863 | struct im_connection *ic; |
---|
[b7d3cc34] | 864 | account_t *a; |
---|
[5ebff60] | 865 | |
---|
| 866 | if (!cmd[2] && (a = account_get(irc->b, cmd[1])) && a->ic) { |
---|
[87b6a3e] | 867 | char *format; |
---|
| 868 | GSList *l; |
---|
[5ebff60] | 869 | |
---|
| 870 | if (strchr(irc->umode, 'b') != NULL) { |
---|
[87b6a3e] | 871 | format = "%s\t%s"; |
---|
[5ebff60] | 872 | } else { |
---|
[57ef864] | 873 | format = "%-32.32s %-16.16s"; |
---|
[5ebff60] | 874 | } |
---|
| 875 | |
---|
| 876 | irc_rootmsg(irc, format, "Handle", "Nickname"); |
---|
| 877 | for (l = a->ic->permit; l; l = l->next) { |
---|
| 878 | bee_user_t *bu = bee_user_by_handle(irc->b, a->ic, l->data); |
---|
[2272cb3] | 879 | irc_user_t *iu = bu ? bu->ui_data : NULL; |
---|
[5ebff60] | 880 | irc_rootmsg(irc, format, l->data, iu ? iu->nick : "(none)"); |
---|
[87b6a3e] | 881 | } |
---|
[5ebff60] | 882 | irc_rootmsg(irc, "End of list."); |
---|
| 883 | |
---|
[87b6a3e] | 884 | return; |
---|
[5ebff60] | 885 | } else if (!cmd[2]) { |
---|
| 886 | irc_user_t *iu = irc_user_by_name(irc, cmd[1]); |
---|
| 887 | if (!iu || !iu->bu) { |
---|
| 888 | irc_rootmsg(irc, "Nick `%s' does not exist", cmd[1]); |
---|
[f73b969] | 889 | return; |
---|
[b7d3cc34] | 890 | } |
---|
[2272cb3] | 891 | ic = iu->bu->ic; |
---|
| 892 | cmd[2] = iu->bu->handle; |
---|
[5ebff60] | 893 | } else if (!(a = account_get(irc->b, cmd[1]))) { |
---|
| 894 | irc_rootmsg(irc, "Invalid account"); |
---|
[f73b969] | 895 | return; |
---|
[5ebff60] | 896 | } else if (!((ic = a->ic) && (a->ic->flags & OPT_LOGGED_IN))) { |
---|
| 897 | irc_rootmsg(irc, "That account is not on-line"); |
---|
[f73b969] | 898 | return; |
---|
[b7d3cc34] | 899 | } |
---|
[5ebff60] | 900 | |
---|
| 901 | if (!ic->acc->prpl->rem_deny || !ic->acc->prpl->add_permit) { |
---|
| 902 | irc_rootmsg(irc, "Command `%s' not supported by this protocol", cmd[0]); |
---|
| 903 | } else { |
---|
| 904 | imc_rem_block(ic, cmd[2]); |
---|
| 905 | imc_add_allow(ic, cmd[2]); |
---|
| 906 | |
---|
| 907 | irc_rootmsg(irc, "Buddy `%s' moved from block- to allow-list", cmd[2]); |
---|
[b7d3cc34] | 908 | } |
---|
| 909 | } |
---|
| 910 | |
---|
[5ebff60] | 911 | static void cmd_yesno(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 912 | { |
---|
| 913 | query_t *q = NULL; |
---|
| 914 | int numq = 0; |
---|
[5ebff60] | 915 | |
---|
| 916 | if (irc->queries == NULL) { |
---|
[65a4a64] | 917 | /* Alright, alright, let's add a tiny easter egg here. */ |
---|
| 918 | static irc_t *last_irc = NULL; |
---|
| 919 | static time_t last_time = 0; |
---|
| 920 | static int times = 0; |
---|
| 921 | static const char *msg[] = { |
---|
| 922 | "Oh yeah, that's right.", |
---|
| 923 | "Alright, alright. Now go back to work.", |
---|
| 924 | "Buuuuuuuuuuuuuuuurp... Excuse me!", |
---|
| 925 | "Yes?", |
---|
| 926 | "No?", |
---|
| 927 | }; |
---|
[5ebff60] | 928 | |
---|
| 929 | if (last_irc == irc && time(NULL) - last_time < 15) { |
---|
| 930 | if ((++times >= 3)) { |
---|
| 931 | irc_rootmsg(irc, "%s", msg[rand() % (sizeof(msg) / sizeof(char*))]); |
---|
[65a4a64] | 932 | last_irc = NULL; |
---|
| 933 | times = 0; |
---|
| 934 | return; |
---|
| 935 | } |
---|
[5ebff60] | 936 | } else { |
---|
| 937 | last_time = time(NULL); |
---|
[65a4a64] | 938 | last_irc = irc; |
---|
| 939 | times = 0; |
---|
| 940 | } |
---|
[5ebff60] | 941 | |
---|
| 942 | irc_rootmsg(irc, "Did I ask you something?"); |
---|
[f73b969] | 943 | return; |
---|
[b7d3cc34] | 944 | } |
---|
[5ebff60] | 945 | |
---|
[b7d3cc34] | 946 | /* If there's an argument, the user seems to want to answer another question than the |
---|
| 947 | first/last (depending on the query_order setting) one. */ |
---|
[5ebff60] | 948 | if (cmd[1]) { |
---|
| 949 | if (sscanf(cmd[1], "%d", &numq) != 1) { |
---|
| 950 | irc_rootmsg(irc, "Invalid query number"); |
---|
[f73b969] | 951 | return; |
---|
[b7d3cc34] | 952 | } |
---|
[5ebff60] | 953 | |
---|
| 954 | for (q = irc->queries; q; q = q->next, numq--) { |
---|
| 955 | if (numq == 0) { |
---|
[b7d3cc34] | 956 | break; |
---|
[5ebff60] | 957 | } |
---|
| 958 | } |
---|
| 959 | |
---|
| 960 | if (!q) { |
---|
| 961 | irc_rootmsg(irc, "Uhm, I never asked you something like that..."); |
---|
[f73b969] | 962 | return; |
---|
[b7d3cc34] | 963 | } |
---|
| 964 | } |
---|
[5ebff60] | 965 | |
---|
| 966 | if (g_strcasecmp(cmd[0], "yes") == 0) { |
---|
| 967 | query_answer(irc, q, 1); |
---|
| 968 | } else if (g_strcasecmp(cmd[0], "no") == 0) { |
---|
| 969 | query_answer(irc, q, 0); |
---|
| 970 | } |
---|
[b7d3cc34] | 971 | } |
---|
| 972 | |
---|
[5ebff60] | 973 | static void cmd_set(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 974 | { |
---|
[5ebff60] | 975 | cmd_set_real(irc, cmd, &irc->b->set, NULL); |
---|
[b7d3cc34] | 976 | } |
---|
| 977 | |
---|
[5ebff60] | 978 | static void cmd_blist(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 979 | { |
---|
[f93fd2d] | 980 | int online = 0, away = 0, offline = 0, ismatch = 0; |
---|
[4c3519a] | 981 | GSList *l; |
---|
[f93fd2d] | 982 | GRegex *regex = NULL; |
---|
| 983 | GError *error = NULL; |
---|
[aefa533e] | 984 | char s[256]; |
---|
| 985 | char *format; |
---|
[b7d3cc34] | 986 | int n_online = 0, n_away = 0, n_offline = 0; |
---|
[5ebff60] | 987 | |
---|
| 988 | if (cmd[1] && g_strcasecmp(cmd[1], "all") == 0) { |
---|
[b7d3cc34] | 989 | online = offline = away = 1; |
---|
[5ebff60] | 990 | } else if (cmd[1] && g_strcasecmp(cmd[1], "offline") == 0) { |
---|
[b7d3cc34] | 991 | offline = 1; |
---|
[5ebff60] | 992 | } else if (cmd[1] && g_strcasecmp(cmd[1], "away") == 0) { |
---|
[b7d3cc34] | 993 | away = 1; |
---|
[5ebff60] | 994 | } else if (cmd[1] && g_strcasecmp(cmd[1], "online") == 0) { |
---|
[b7d3cc34] | 995 | online = 1; |
---|
[5ebff60] | 996 | } else { |
---|
[449a51d] | 997 | online = away = 1; |
---|
[f93fd2d] | 998 | } |
---|
[5ebff60] | 999 | |
---|
| 1000 | if (cmd[2]) { |
---|
| 1001 | regex = g_regex_new(cmd[2], G_REGEX_CASELESS, 0, &error); |
---|
| 1002 | } |
---|
| 1003 | |
---|
| 1004 | if (error) { |
---|
| 1005 | irc_rootmsg(irc, error->message); |
---|
| 1006 | g_error_free(error); |
---|
| 1007 | } |
---|
| 1008 | |
---|
| 1009 | if (strchr(irc->umode, 'b') != NULL) { |
---|
[aefa533e] | 1010 | format = "%s\t%s\t%s"; |
---|
[5ebff60] | 1011 | } else { |
---|
[aefa533e] | 1012 | format = "%-16.16s %-40.40s %s"; |
---|
[5ebff60] | 1013 | } |
---|
| 1014 | |
---|
| 1015 | irc_rootmsg(irc, format, "Nick", "Handle/Account", "Status"); |
---|
| 1016 | |
---|
| 1017 | if (irc->root->last_channel && |
---|
| 1018 | strcmp(set_getstr(&irc->root->last_channel->set, "type"), "control") != 0) { |
---|
[ac2717b] | 1019 | irc->root->last_channel = NULL; |
---|
[5ebff60] | 1020 | } |
---|
| 1021 | |
---|
| 1022 | for (l = irc->users; l; l = l->next) { |
---|
[4c3519a] | 1023 | irc_user_t *iu = l->data; |
---|
| 1024 | bee_user_t *bu = iu->bu; |
---|
[5ebff60] | 1025 | |
---|
| 1026 | if (!regex || g_regex_match(regex, iu->nick, 0, NULL)) { |
---|
[f93fd2d] | 1027 | ismatch = 1; |
---|
[5ebff60] | 1028 | } else { |
---|
[f93fd2d] | 1029 | ismatch = 0; |
---|
[5ebff60] | 1030 | } |
---|
| 1031 | |
---|
| 1032 | if (!bu || (irc->root->last_channel && !irc_channel_wants_user(irc->root->last_channel, iu))) { |
---|
[4c3519a] | 1033 | continue; |
---|
[5ebff60] | 1034 | } |
---|
| 1035 | |
---|
| 1036 | if ((bu->flags & (BEE_USER_ONLINE | BEE_USER_AWAY)) == BEE_USER_ONLINE) { |
---|
| 1037 | if (ismatch == 1 && online == 1) { |
---|
[f93fd2d] | 1038 | char st[256] = "Online"; |
---|
[5ebff60] | 1039 | |
---|
| 1040 | if (bu->status_msg) { |
---|
| 1041 | g_snprintf(st, sizeof(st) - 1, "Online (%s)", bu->status_msg); |
---|
| 1042 | } |
---|
| 1043 | |
---|
| 1044 | g_snprintf(s, sizeof(s) - 1, "%s %s", bu->handle, bu->ic->acc->tag); |
---|
| 1045 | irc_rootmsg(irc, format, iu->nick, s, st); |
---|
[f93fd2d] | 1046 | } |
---|
[5ebff60] | 1047 | |
---|
| 1048 | n_online++; |
---|
[aefa533e] | 1049 | } |
---|
[5ebff60] | 1050 | |
---|
| 1051 | if ((bu->flags & BEE_USER_ONLINE) && (bu->flags & BEE_USER_AWAY)) { |
---|
| 1052 | if (ismatch == 1 && away == 1) { |
---|
| 1053 | g_snprintf(s, sizeof(s) - 1, "%s %s", bu->handle, bu->ic->acc->tag); |
---|
| 1054 | irc_rootmsg(irc, format, iu->nick, s, irc_user_get_away(iu)); |
---|
[f93fd2d] | 1055 | } |
---|
[5ebff60] | 1056 | n_away++; |
---|
[aefa533e] | 1057 | } |
---|
[5ebff60] | 1058 | |
---|
| 1059 | if (!(bu->flags & BEE_USER_ONLINE)) { |
---|
| 1060 | if (ismatch == 1 && offline == 1) { |
---|
| 1061 | g_snprintf(s, sizeof(s) - 1, "%s %s", bu->handle, bu->ic->acc->tag); |
---|
| 1062 | irc_rootmsg(irc, format, iu->nick, s, "Offline"); |
---|
[f93fd2d] | 1063 | } |
---|
[5ebff60] | 1064 | n_offline++; |
---|
[aefa533e] | 1065 | } |
---|
[b7d3cc34] | 1066 | } |
---|
[5ebff60] | 1067 | |
---|
| 1068 | irc_rootmsg(irc, "%d buddies (%d available, %d away, %d offline)", n_online + n_away + n_offline, n_online, |
---|
| 1069 | n_away, n_offline); |
---|
| 1070 | |
---|
| 1071 | if (regex) { |
---|
| 1072 | g_regex_unref(regex); |
---|
| 1073 | } |
---|
[b7d3cc34] | 1074 | } |
---|
| 1075 | |
---|
[5ebff60] | 1076 | static void cmd_qlist(irc_t *irc, char **cmd) |
---|
[b7d3cc34] | 1077 | { |
---|
| 1078 | query_t *q = irc->queries; |
---|
| 1079 | int num; |
---|
[5ebff60] | 1080 | |
---|
| 1081 | if (!q) { |
---|
| 1082 | irc_rootmsg(irc, "There are no pending questions."); |
---|
[f73b969] | 1083 | return; |
---|
[b7d3cc34] | 1084 | } |
---|
[5ebff60] | 1085 | |
---|
| 1086 | irc_rootmsg(irc, "Pending queries:"); |
---|
| 1087 | |
---|
| 1088 | for (num = 0; q; q = q->next, num++) { |
---|
| 1089 | if (q->ic) { /* Not necessary yet, but it might come later */ |
---|
| 1090 | irc_rootmsg(irc, "%d, %s: %s", num, q->ic->acc->tag, q->question); |
---|
| 1091 | } else { |
---|
| 1092 | irc_rootmsg(irc, "%d, BitlBee: %s", num, q->question); |
---|
| 1093 | } |
---|
| 1094 | } |
---|
[b7d3cc34] | 1095 | } |
---|
| 1096 | |
---|
[5ebff60] | 1097 | static void cmd_chat(irc_t *irc, char **cmd) |
---|
[a9a7287] | 1098 | { |
---|
| 1099 | account_t *acc; |
---|
[5ebff60] | 1100 | |
---|
| 1101 | if (g_strcasecmp(cmd[1], "add") == 0) { |
---|
[07054a5] | 1102 | char *channel, *s; |
---|
[7b71feb] | 1103 | struct irc_channel *ic; |
---|
[5ebff60] | 1104 | |
---|
| 1105 | MIN_ARGS(3); |
---|
| 1106 | |
---|
| 1107 | if (!(acc = account_get(irc->b, cmd[2]))) { |
---|
| 1108 | irc_rootmsg(irc, "Invalid account"); |
---|
[a9a7287] | 1109 | return; |
---|
[5ebff60] | 1110 | } else if (!acc->prpl->chat_join) { |
---|
| 1111 | irc_rootmsg(irc, "Named chatrooms not supported on that account."); |
---|
[5a75d15] | 1112 | return; |
---|
| 1113 | } |
---|
[5ebff60] | 1114 | |
---|
| 1115 | if (cmd[4] == NULL) { |
---|
| 1116 | channel = g_strdup(cmd[3]); |
---|
| 1117 | if ((s = strchr(channel, '@'))) { |
---|
[07054a5] | 1118 | *s = 0; |
---|
[5ebff60] | 1119 | } |
---|
| 1120 | } else { |
---|
| 1121 | channel = g_strdup(cmd[4]); |
---|
[07054a5] | 1122 | } |
---|
[5ebff60] | 1123 | |
---|
| 1124 | if (strchr(CTYPES, channel[0]) == NULL) { |
---|
| 1125 | s = g_strdup_printf("#%s", channel); |
---|
| 1126 | g_free(channel); |
---|
[07054a5] | 1127 | channel = s; |
---|
[5ebff60] | 1128 | |
---|
| 1129 | irc_channel_name_strip(channel); |
---|
[5a75d15] | 1130 | } |
---|
[5ebff60] | 1131 | |
---|
| 1132 | if ((ic = irc_channel_new(irc, channel)) && |
---|
| 1133 | set_setstr(&ic->set, "type", "chat") && |
---|
| 1134 | set_setstr(&ic->set, "chat_type", "room") && |
---|
| 1135 | set_setstr(&ic->set, "account", cmd[2]) && |
---|
| 1136 | set_setstr(&ic->set, "room", cmd[3])) { |
---|
| 1137 | irc_rootmsg(irc, "Chatroom successfully added."); |
---|
| 1138 | } else { |
---|
| 1139 | if (ic) { |
---|
| 1140 | irc_channel_free(ic); |
---|
| 1141 | } |
---|
| 1142 | |
---|
| 1143 | irc_rootmsg(irc, "Could not add chatroom."); |
---|
[d995c9b] | 1144 | } |
---|
[5ebff60] | 1145 | g_free(channel); |
---|
| 1146 | } else if (g_strcasecmp(cmd[1], "with") == 0) { |
---|
[c1a8a16] | 1147 | irc_user_t *iu; |
---|
[5ebff60] | 1148 | |
---|
| 1149 | MIN_ARGS(2); |
---|
| 1150 | |
---|
| 1151 | if ((iu = irc_user_by_name(irc, cmd[2])) && |
---|
| 1152 | iu->bu && iu->bu->ic->acc->prpl->chat_with) { |
---|
| 1153 | if (!iu->bu->ic->acc->prpl->chat_with(iu->bu->ic, iu->bu->handle)) { |
---|
| 1154 | irc_rootmsg(irc, "(Possible) failure while trying to open " |
---|
| 1155 | "a groupchat with %s.", iu->nick); |
---|
[39f93f0] | 1156 | } |
---|
[5ebff60] | 1157 | } else { |
---|
| 1158 | irc_rootmsg(irc, "Can't open a groupchat with %s.", cmd[2]); |
---|
[39f93f0] | 1159 | } |
---|
[5ebff60] | 1160 | } else if (g_strcasecmp(cmd[1], "list") == 0 || |
---|
| 1161 | g_strcasecmp(cmd[1], "set") == 0 || |
---|
| 1162 | g_strcasecmp(cmd[1], "del") == 0) { |
---|
| 1163 | irc_rootmsg(irc, |
---|
| 1164 | "Warning: The \002chat\002 command was mostly replaced with the \002channel\002 command."); |
---|
| 1165 | cmd_channel(irc, cmd); |
---|
| 1166 | } else { |
---|
| 1167 | irc_rootmsg(irc, |
---|
| 1168 | "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "chat", |
---|
| 1169 | cmd[1]); |
---|
[a9a7287] | 1170 | } |
---|
[fa29d093] | 1171 | } |
---|
| 1172 | |
---|
[5ebff60] | 1173 | static void cmd_group(irc_t *irc, char **cmd) |
---|
[f1d488e] | 1174 | { |
---|
| 1175 | GSList *l; |
---|
| 1176 | int len; |
---|
[5ebff60] | 1177 | |
---|
| 1178 | len = strlen(cmd[1]); |
---|
| 1179 | if (g_strncasecmp(cmd[1], "list", len) == 0) { |
---|
[f1d488e] | 1180 | int n = 0; |
---|
[5ebff60] | 1181 | |
---|
| 1182 | if (strchr(irc->umode, 'b')) { |
---|
| 1183 | irc_rootmsg(irc, "Group list:"); |
---|
| 1184 | } |
---|
| 1185 | |
---|
| 1186 | for (l = irc->b->groups; l; l = l->next) { |
---|
[f1d488e] | 1187 | bee_group_t *bg = l->data; |
---|
[5ebff60] | 1188 | irc_rootmsg(irc, "%d. %s", n++, bg->name); |
---|
[f1d488e] | 1189 | } |
---|
[5ebff60] | 1190 | irc_rootmsg(irc, "End of group list"); |
---|
| 1191 | } else if (g_strncasecmp(cmd[1], "info", len) == 0) { |
---|
[e4f5ca8] | 1192 | bee_group_t *bg; |
---|
[c2cc24c] | 1193 | int n = 0; |
---|
| 1194 | |
---|
| 1195 | MIN_ARGS(2); |
---|
[5ebff60] | 1196 | bg = bee_group_by_name(irc->b, cmd[2], FALSE); |
---|
| 1197 | |
---|
| 1198 | if (bg) { |
---|
| 1199 | if (strchr(irc->umode, 'b')) { |
---|
| 1200 | irc_rootmsg(irc, "Members of %s:", cmd[2]); |
---|
| 1201 | } |
---|
| 1202 | for (l = irc->b->users; l; l = l->next) { |
---|
[c2cc24c] | 1203 | bee_user_t *bu = l->data; |
---|
[5ebff60] | 1204 | if (bu->group == bg) { |
---|
| 1205 | irc_rootmsg(irc, "%d. %s", n++, bu->nick ? : bu->handle); |
---|
| 1206 | } |
---|
[c2cc24c] | 1207 | } |
---|
[5ebff60] | 1208 | irc_rootmsg(irc, "End of member list"); |
---|
| 1209 | } else { |
---|
| 1210 | irc_rootmsg(irc, |
---|
| 1211 | "Unknown group: %s. Please use \x02group list\x02 to get a list of available groups.", |
---|
| 1212 | cmd[2]); |
---|
[c2cc24c] | 1213 | } |
---|
[5ebff60] | 1214 | } else { |
---|
| 1215 | irc_rootmsg(irc, |
---|
| 1216 | "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "group", |
---|
| 1217 | cmd[1]); |
---|
[f1d488e] | 1218 | } |
---|
| 1219 | } |
---|
| 1220 | |
---|
[5ebff60] | 1221 | static void cmd_transfer(irc_t *irc, char **cmd) |
---|
[2c2df7d] | 1222 | { |
---|
| 1223 | GSList *files = irc->file_transfers; |
---|
[5ebff60] | 1224 | |
---|
[2c2df7d] | 1225 | enum { LIST, REJECT, CANCEL }; |
---|
| 1226 | int subcmd = LIST; |
---|
| 1227 | int fid; |
---|
| 1228 | |
---|
[5ebff60] | 1229 | if (!files) { |
---|
| 1230 | irc_rootmsg(irc, "No pending transfers"); |
---|
[2c2df7d] | 1231 | return; |
---|
| 1232 | } |
---|
| 1233 | |
---|
[5ebff60] | 1234 | if (cmd[1] && (strcmp(cmd[1], "reject") == 0)) { |
---|
[2c2df7d] | 1235 | subcmd = REJECT; |
---|
[5ebff60] | 1236 | } else if (cmd[1] && (strcmp(cmd[1], "cancel") == 0) && |
---|
| 1237 | cmd[2] && (sscanf(cmd[2], "%d", &fid) == 1)) { |
---|
[2c2df7d] | 1238 | subcmd = CANCEL; |
---|
| 1239 | } |
---|
| 1240 | |
---|
[5ebff60] | 1241 | for (; files; files = g_slist_next(files)) { |
---|
[2c2df7d] | 1242 | file_transfer_t *file = files->data; |
---|
[5ebff60] | 1243 | |
---|
| 1244 | switch (subcmd) { |
---|
[2c2df7d] | 1245 | case LIST: |
---|
[5ebff60] | 1246 | if (file->status == FT_STATUS_LISTENING) { |
---|
| 1247 | irc_rootmsg(irc, |
---|
| 1248 | "Pending file(id %d): %s (Listening...)", file->local_id, file->file_name); |
---|
| 1249 | } else { |
---|
[2c2df7d] | 1250 | int kb_per_s = 0; |
---|
[5ebff60] | 1251 | time_t diff = time(NULL) - file->started ? : 1; |
---|
| 1252 | if ((file->started > 0) && (file->bytes_transferred > 0)) { |
---|
[2c2df7d] | 1253 | kb_per_s = file->bytes_transferred / 1024 / diff; |
---|
[5ebff60] | 1254 | } |
---|
| 1255 | |
---|
| 1256 | irc_rootmsg(irc, |
---|
| 1257 | "Pending file(id %d): %s (%10zd/%zd kb, %d kb/s)", file->local_id, |
---|
| 1258 | file->file_name, |
---|
| 1259 | file->bytes_transferred / 1024, file->file_size / 1024, kb_per_s); |
---|
[2c2df7d] | 1260 | } |
---|
| 1261 | break; |
---|
| 1262 | case REJECT: |
---|
[5ebff60] | 1263 | if (file->status == FT_STATUS_LISTENING) { |
---|
| 1264 | irc_rootmsg(irc, "Rejecting file transfer for %s", file->file_name); |
---|
| 1265 | imcb_file_canceled(file->ic, file, "Denied by user"); |
---|
[2c2df7d] | 1266 | } |
---|
| 1267 | break; |
---|
| 1268 | case CANCEL: |
---|
[5ebff60] | 1269 | if (file->local_id == fid) { |
---|
| 1270 | irc_rootmsg(irc, "Canceling file transfer for %s", file->file_name); |
---|
| 1271 | imcb_file_canceled(file->ic, file, "Canceled by user"); |
---|
[2c2df7d] | 1272 | } |
---|
| 1273 | break; |
---|
| 1274 | } |
---|
| 1275 | } |
---|
| 1276 | } |
---|
| 1277 | |
---|
[5ebff60] | 1278 | static void cmd_nick(irc_t *irc, char **cmd) |
---|
[3cd4016] | 1279 | { |
---|
[5ebff60] | 1280 | irc_rootmsg(irc, "This command is deprecated. Try: account %s set display_name", cmd[1]); |
---|
[3cd4016] | 1281 | } |
---|
| 1282 | |
---|
[180ab31] | 1283 | /* Maybe this should be a stand-alone command as well? */ |
---|
[5ebff60] | 1284 | static void bitlbee_whatsnew(irc_t *irc) |
---|
[180ab31] | 1285 | { |
---|
[5ebff60] | 1286 | int last = set_getint(&irc->b->set, "last_version"); |
---|
[674a01d] | 1287 | char s[16], *msg; |
---|
[5ebff60] | 1288 | |
---|
| 1289 | if (last >= BITLBEE_VERSION_CODE) { |
---|
[180ab31] | 1290 | return; |
---|
[5ebff60] | 1291 | } |
---|
| 1292 | |
---|
| 1293 | msg = help_get_whatsnew(&(global.help), last); |
---|
| 1294 | |
---|
| 1295 | if (msg) { |
---|
| 1296 | irc_rootmsg(irc, "%s: This seems to be your first time using this " |
---|
| 1297 | "this version of BitlBee. Here's a list of new " |
---|
| 1298 | "features you may like to know about:\n\n%s\n", |
---|
| 1299 | irc->user->nick, msg); |
---|
| 1300 | } |
---|
| 1301 | |
---|
| 1302 | g_free(msg); |
---|
| 1303 | |
---|
| 1304 | g_snprintf(s, sizeof(s), "%d", BITLBEE_VERSION_CODE); |
---|
| 1305 | set_setstr(&irc->b->set, "last_version", s); |
---|
[180ab31] | 1306 | } |
---|
| 1307 | |
---|
[6c56f42] | 1308 | /* IMPORTANT: Keep this list sorted! The short command logic needs that. */ |
---|
[8358691] | 1309 | command_t root_commands[] = { |
---|
[d860a8d] | 1310 | { "account", 1, cmd_account, 0 }, |
---|
[6c56f42] | 1311 | { "add", 2, cmd_add, 0 }, |
---|
[2272cb3] | 1312 | { "allow", 1, cmd_allow, 0 }, |
---|
[4c3519a] | 1313 | { "blist", 0, cmd_blist, 0 }, |
---|
[2272cb3] | 1314 | { "block", 1, cmd_block, 0 }, |
---|
[c133d4b8] | 1315 | { "channel", 1, cmd_channel, 0 }, |
---|
| 1316 | { "chat", 1, cmd_chat, 0 }, |
---|
[6c56f42] | 1317 | { "drop", 1, cmd_drop, 0 }, |
---|
[9d4352c] | 1318 | { "ft", 0, cmd_transfer, 0 }, |
---|
[f1d488e] | 1319 | { "group", 1, cmd_group, 0 }, |
---|
[5ebff60] | 1320 | { "help", 0, cmd_help, 0 }, |
---|
[060d066] | 1321 | { "identify", 0, cmd_identify, 0 }, |
---|
[aa44bdd] | 1322 | { "info", 1, cmd_info, 0 }, |
---|
[3cd4016] | 1323 | { "nick", 1, cmd_nick, 0 }, |
---|
[6c56f42] | 1324 | { "no", 0, cmd_yesno, 0 }, |
---|
[9d4352c] | 1325 | { "qlist", 0, cmd_qlist, 0 }, |
---|
[060d066] | 1326 | { "register", 0, cmd_register, 0 }, |
---|
[dbb0ce3] | 1327 | { "remove", 1, cmd_remove, 0 }, |
---|
[0298d11] | 1328 | { "rename", 2, cmd_rename, 0 }, |
---|
[6c56f42] | 1329 | { "save", 0, cmd_save, 0 }, |
---|
[0298d11] | 1330 | { "set", 0, cmd_set, 0 }, |
---|
[9d4352c] | 1331 | { "transfer", 0, cmd_transfer, 0 }, |
---|
[0298d11] | 1332 | { "yes", 0, cmd_yesno, 0 }, |
---|
[8358691] | 1333 | /* Not expecting too many plugins adding root commands so just make a |
---|
| 1334 | dumb array with some empty entried at the end. */ |
---|
| 1335 | { NULL }, |
---|
| 1336 | { NULL }, |
---|
| 1337 | { NULL }, |
---|
| 1338 | { NULL }, |
---|
| 1339 | { NULL }, |
---|
| 1340 | { NULL }, |
---|
| 1341 | { NULL }, |
---|
| 1342 | { NULL }, |
---|
| 1343 | { NULL }, |
---|
[0298d11] | 1344 | }; |
---|
[5ebff60] | 1345 | static const int num_root_commands = sizeof(root_commands) / sizeof(command_t); |
---|
[8358691] | 1346 | |
---|
[5ebff60] | 1347 | gboolean root_command_add(const char *command, int params, void (*func)(irc_t *, char **args), int flags) |
---|
[8358691] | 1348 | { |
---|
| 1349 | int i; |
---|
[5ebff60] | 1350 | |
---|
| 1351 | if (root_commands[num_root_commands - 2].command) { |
---|
[8358691] | 1352 | /* Planning fail! List is full. */ |
---|
| 1353 | return FALSE; |
---|
[5ebff60] | 1354 | } |
---|
| 1355 | |
---|
| 1356 | for (i = 0; root_commands[i].command; i++) { |
---|
| 1357 | if (g_strcasecmp(root_commands[i].command, command) == 0) { |
---|
[8358691] | 1358 | return FALSE; |
---|
[5ebff60] | 1359 | } else if (g_strcasecmp(root_commands[i].command, command) > 0) { |
---|
[8358691] | 1360 | break; |
---|
[5ebff60] | 1361 | } |
---|
[8358691] | 1362 | } |
---|
[5ebff60] | 1363 | memmove(root_commands + i + 1, root_commands + i, |
---|
| 1364 | sizeof(command_t) * (num_root_commands - i - 1)); |
---|
| 1365 | |
---|
| 1366 | root_commands[i].command = g_strdup(command); |
---|
[8358691] | 1367 | root_commands[i].required_parameters = params; |
---|
| 1368 | root_commands[i].execute = func; |
---|
| 1369 | root_commands[i].flags = flags; |
---|
[5ebff60] | 1370 | |
---|
[8358691] | 1371 | return TRUE; |
---|
| 1372 | } |
---|