cmdsserv.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2010 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * cmdsserv.c -- part of server.mod
  22. * handles commands from a user via dcc that cause server interaction
  23. *
  24. */
  25. static void cmd_servers(int idx, char *par)
  26. {
  27. struct server_list *x = serverlist;
  28. int i;
  29. char s[1024] = "";
  30. putlog(LOG_CMDS, "*", "#%s# servers", dcc[idx].nick);
  31. if (!x) {
  32. dprintf(idx, "There are no servers in the server list.\n");
  33. } else {
  34. dprintf(idx, "Server list:\n");
  35. i = 0;
  36. for (; x; x = x->next) {
  37. simple_snprintf(s, sizeof s, " %s:%d %s", x->name,
  38. x->port ? x->port : (ssl_use ? default_port_ssl : default_port),
  39. (i == curserv) ? "<- I am here" : "");
  40. dprintf(idx, "%s\n", s);
  41. i++;
  42. }
  43. dprintf(idx, "End of server list.\n");
  44. }
  45. }
  46. static void cmd_dump(int idx, char *par)
  47. {
  48. putlog(LOG_CMDS, "*", "#%s# dump %s", dcc[idx].nick, par);
  49. if (!isowner(dcc[idx].nick)) {
  50. putlog(LOG_WARN, "*", "%s attempted 'dump' %s", dcc[idx].nick, par);
  51. dprintf(idx, "dump is only available to permanent owners.\n");
  52. return;
  53. }
  54. if (!par[0]) {
  55. dprintf(idx, "Usage: dump <server stuff>\n");
  56. return;
  57. }
  58. dprintf(DP_DUMP, "%s\n", replace_vars(par));
  59. }
  60. static void cmd_umode(int idx, char *par)
  61. {
  62. putlog(LOG_CMDS, "*", "#%s# umode %s", dcc[idx].nick, par);
  63. if (!par[0]) {
  64. dprintf(idx, "Usage: umode <+flags>\n");
  65. return;
  66. }
  67. dprintf(DP_SERVER, "MODE %s %s\n", botname, par);
  68. }
  69. static void cmd_jump(int idx, char *par)
  70. {
  71. char *other = NULL, *p = NULL;
  72. int port;
  73. if (par[0]) {
  74. other = newsplit(&par);
  75. port = atoi(newsplit(&par));
  76. if ((p = strchr(other, ':'))) {
  77. *p = 0;
  78. p++;
  79. if (!port)
  80. port = atoi(p);
  81. }
  82. if (!port)
  83. port = (ssl_use ? default_port_ssl : default_port);
  84. putlog(LOG_CMDS, "*", "#%s# jump %s %d %s", dcc[idx].nick, other, port, par);
  85. strlcpy(newserver, other, sizeof newserver);
  86. newserverport = port;
  87. strlcpy(newserverpass, par, sizeof newserverpass);
  88. } else
  89. putlog(LOG_CMDS, "*", "#%s# jump", dcc[idx].nick);
  90. dprintf(idx, "Jumping servers...\n");
  91. nuke_server("changing servers");
  92. cycle_time = 0;
  93. }
  94. static void cmd_keyx(int idx, char *par) {
  95. putlog(LOG_CMDS, "*", "#%s# keyx %s", dcc[idx].nick, par);
  96. if (!par[0]) {
  97. dprintf(idx, "Usage: keyx <nick>\n");
  98. return;
  99. }
  100. if (strchr(CHANMETA, par[0])) {
  101. dprintf(idx, "Error: Cannot key-exchange with a channel.\n");
  102. return;
  103. }
  104. if (!server_online) {
  105. dprintf(idx, "Error: Not online.\n");
  106. return;
  107. }
  108. char *nick = newsplit(&par);
  109. bd::String myPublicKeyB64, myPrivateKey, sharedKey;
  110. DH1080_gen(myPrivateKey, myPublicKeyB64);
  111. putlog(LOG_MSGS, "*", "[FiSH] Initiating DH1080 key-exchange with %s - sending my public key", nick);
  112. notice(nick, "DH1080_INIT " + myPublicKeyB64, DP_HELP);
  113. fish_data_t* fishData = new fish_data_t;
  114. fishData->myPublicKeyB64 = myPublicKeyB64;
  115. fishData->myPrivateKey = myPrivateKey;
  116. fishData->timestamp = now;
  117. FishKeys[nick] = fishData;
  118. return;
  119. }
  120. static void cmd_setkey(int idx, char *par) {
  121. putlog(LOG_CMDS, "*", "#%s# setkey %s", dcc[idx].nick, par);
  122. if (!par[0]) {
  123. dprintf(idx, "Usage: setkey <nick|channel> [key]\n");
  124. return;
  125. }
  126. char *target = newsplit(&par);
  127. char *key = newsplit(&par);
  128. bool have_key = FishKeys.contains(target);
  129. fish_data_t* fishData = NULL;
  130. if (!key[0]) {
  131. // Clear the key
  132. if (have_key) {
  133. fishData = FishKeys[target];
  134. FishKeys.remove(target);
  135. delete fishData;
  136. dprintf(idx, "Key cleared for '%s'\n", target);
  137. } else {
  138. dprintf(idx, "No key found for '%s'\n", target);
  139. }
  140. } else {
  141. // Set the key
  142. fishData = new fish_data_t;
  143. fishData->sharedKey = key;
  144. fishData->timestamp = now;
  145. dprintf(idx, "Set key for '%s' to: %s", target, key);
  146. }
  147. return;
  148. }
  149. static void cmd_clearqueue(int idx, char *par)
  150. {
  151. int msgs;
  152. putlog(LOG_CMDS, "*", "#%s# clearqueue %s", dcc[idx].nick, par);
  153. if (!par[0]) {
  154. dprintf(idx, "Usage: clearqueue <mode|server|help|all>\n");
  155. return;
  156. }
  157. if (!strcasecmp(par, "all")) {
  158. msgs = modeq.tot + mq.tot + hq.tot + aq.tot;
  159. msgq_clear(&modeq);
  160. msgq_clear(&mq);
  161. msgq_clear(&hq);
  162. msgq_clear(&aq);
  163. burst = 0;
  164. double_warned = 0;
  165. dprintf(idx, "Removed %d message%s from all queues.\n", msgs,
  166. (msgs != 1) ? "s" : "");
  167. } else if (!strcasecmp(par, "mode")) {
  168. msgs = modeq.tot;
  169. msgq_clear(&modeq);
  170. if (mq.tot == 0)
  171. burst = 0;
  172. double_warned = 0;
  173. dprintf(idx, "Removed %d message%s from the mode queue.\n", msgs,
  174. (msgs != 1) ? "s" : "");
  175. } else if (!strcasecmp(par, "help")) {
  176. msgs = hq.tot;
  177. msgq_clear(&hq);
  178. double_warned = 0;
  179. dprintf(idx, "Removed %d message%s from the help queue.\n", msgs,
  180. (msgs != 1) ? "s" : "");
  181. } else if (!strcasecmp(par, "play")) {
  182. msgs = aq.tot;
  183. msgq_clear(&aq);
  184. double_warned = 0;
  185. dprintf(idx, "Removed %d message%s from the play queue.\n", msgs,
  186. (msgs != 1) ? "s" : "");
  187. } else if (!strcasecmp(par, "server")) {
  188. msgs = mq.tot;
  189. msgq_clear(&mq);
  190. if (modeq.tot == 0)
  191. burst = 0;
  192. double_warned = 0;
  193. dprintf(idx, "Removed %d message%s from the server queue.\n", msgs,
  194. (msgs != 1) ? "s" : "");
  195. } else {
  196. dprintf(idx, "Usage: clearqueue <mode|server|help|all>\n");
  197. return;
  198. }
  199. }
  200. /* Function call should be:
  201. * int cmd_whatever(idx,"parameters");
  202. *
  203. * As with msg commands, function is responsible for any logging.
  204. */
  205. static cmd_t C_dcc_serv[] =
  206. {
  207. {"clearqueue", "m", (Function) cmd_clearqueue, NULL, LEAF|AUTH},
  208. {"dump", "a", (Function) cmd_dump, NULL, LEAF},
  209. {"jump", "m", (Function) cmd_jump, NULL, LEAF},
  210. {"keyx", "o", (Function) cmd_keyx, NULL, LEAF},
  211. {"servers", "m", (Function) cmd_servers, NULL, LEAF},
  212. {"setkey", "m", (Function) cmd_setkey, NULL, LEAF},
  213. {"umode", "m", (Function) cmd_umode, NULL, LEAF},
  214. {NULL, NULL, NULL, NULL, 0}
  215. };