core_binds.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2014 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. * core_binds.c -- handles:
  22. *
  23. * binds for the CORE
  24. *
  25. */
  26. #include "common.h"
  27. #include "dccutil.h"
  28. #include "auth.h"
  29. #include "core_binds.h"
  30. #include "userrec.h"
  31. #include "main.h"
  32. #include "settings.h"
  33. #include "set.h"
  34. #include "users.h"
  35. #include "misc.h"
  36. #include "binds.h"
  37. #include "dcc.h"
  38. #include "cmds.h"
  39. extern cmd_t C_dcc[];
  40. static bind_table_t *BT_away = NULL, *BT_dcc = NULL;
  41. static bind_table_t *BT_note = NULL;
  42. static bind_table_t *BT_bot = NULL, *BT_nkch = NULL, *BT_chon = NULL;
  43. static bind_table_t *BT_time = NULL;
  44. void core_binds_init()
  45. {
  46. BT_away = bind_table_add("away", 3, "sis", MATCH_MASK, BIND_STACKABLE);
  47. BT_bot = bind_table_add("bot", 3, "sss", MATCH_EXACT, 0);
  48. BT_chon = bind_table_add("chon", 2, "si", MATCH_MASK | MATCH_FLAGS, BIND_STACKABLE);
  49. BT_dcc = bind_table_add("dcc", 2, "is", MATCH_PARTIAL | MATCH_FLAGS, 0);
  50. bzero(&cmdlist, 500);
  51. add_builtins("dcc", C_dcc);
  52. BT_nkch = bind_table_add("nkch", 2, "ss", MATCH_MASK, BIND_STACKABLE);
  53. BT_note = bind_table_add("note", 3 , "sss", MATCH_EXACT, 0);
  54. BT_time = bind_table_add("time", 5, "iiiii", MATCH_MASK, BIND_STACKABLE);
  55. }
  56. bool check_aliases(int idx, const char *cmd, const char *args)
  57. {
  58. char *a = NULL, *p = NULL, *aliasp = NULL, *aliasdup = NULL, *argsp = NULL, *argsdup = NULL;
  59. bool found = 0;
  60. bind_entry_t *entry = NULL;
  61. bind_table_t *table = NULL;
  62. aliasp = aliasdup = strdup(alias);
  63. if (args && args[0])
  64. argsp = argsdup = strdup(args);
  65. while ((a = strsep(&aliasdup, ","))) { //a = entire alias "alias cmd params"
  66. p = newsplit(&a); //p = alias //a = cmd params
  67. if (!strcasecmp(p, cmd)) { //a match on the cmd we were given!
  68. p = newsplit(&a); //p = cmd //a = params
  69. /*
  70. p = cmd to be executed
  71. a = params to be passed to the cmd from the alias listing
  72. args = params given by the user -- may be a cmdpass that needs inserting before the alias params
  73. */
  74. /* Simple loop check */
  75. if (!strcasecmp(cmd, p)) {
  76. putlog(LOG_WARN, "*", "Loop detected in alias '%s'", p);
  77. free(aliasp);
  78. free(argsp);
  79. return 0;
  80. }
  81. /* Sanity check - Aliases cannot reference other aliases */
  82. bool find = 0;
  83. table = bind_table_lookup("dcc");
  84. for (entry = table->entries; entry && entry->next; entry = entry->next) {
  85. if (!strncasecmp(p, entry->mask, strlen(p))) {
  86. find = 1;
  87. break;
  88. }
  89. }
  90. if (!find) {
  91. /* Does the cmd exist though? (Hub-only cmd from a leaf or a leaf-only cmd from a hub, or restricted cmd) */
  92. if (findcmd(cmd, 0)) {
  93. free(argsp);
  94. free(aliasp);
  95. return 0; /* Show bad cmd */
  96. } else {
  97. /* nope, show alias error */
  98. dprintf(idx, "'%s' is an invalid alias: references alias '%s'.\n", cmd, p);
  99. putlog(LOG_ERROR, "*", "Invalid alias '%s' attempted: references alias '%s'.", cmd, p);
  100. free(argsp);
  101. free(aliasp);
  102. return 1; /* Alias was found -- just not accepted */
  103. }
  104. }
  105. char *myargs = NULL, *pass = NULL;
  106. size_t size = 0;
  107. found = 1;
  108. size = strlen(a) + 1 + (argsdup ? strlen(argsdup) : 0) + 1 + 2;
  109. myargs = (char *) calloc(1, size);
  110. /* Rewrite the cmd including the inserted cmdpass if the cmd has one, and the user provided any param */
  111. if (argsdup && argsdup[0] && has_cmd_pass(p)) {
  112. pass = newsplit(&argsdup);
  113. strlcpy(myargs, pass, size);
  114. if (a && a[0]) {
  115. strlcat(myargs, " ", size);
  116. strlcat(myargs, a, size);
  117. }
  118. if (argsdup[0]) { /* was split */
  119. strlcat(myargs, " ", size);
  120. strlcat(myargs, argsdup, size);
  121. }
  122. } else {
  123. /* Otherwise, just construct it based on cmd and params if provided */
  124. if (argsdup && argsdup[0]) {
  125. strlcpy(myargs, a, size);
  126. strlcat(myargs, " ", size);
  127. strlcat(myargs, argsdup, size);
  128. } else
  129. strlcpy(myargs, a, size);
  130. }
  131. if (a && a[0])
  132. putlog(LOG_CMDS, "*", "@ #%s# [%s -> %s %s] ...", dcc[idx].nick, cmd, p, a);
  133. else
  134. putlog(LOG_CMDS, "*", "@ #%s# [%s -> %s] ...", dcc[idx].nick, cmd, p);
  135. check_bind_dcc(p, idx, myargs);
  136. free(myargs);
  137. break;
  138. }
  139. }
  140. free(aliasp);
  141. free(argsp);
  142. return found;
  143. }
  144. int check_bind_dcc(const char *cmd, int idx, const char *text)
  145. {
  146. return real_check_bind_dcc(cmd, idx, text, NULL);
  147. }
  148. int real_check_bind_dcc(const char *cmd, int idx, const char *text, Auth *auth)
  149. {
  150. struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYCH, 0, 0, 0 };
  151. bind_entry_t *entry = NULL;
  152. bind_table_t *table = NULL;
  153. char *args = strdup(text);
  154. size_t args_siz = strlen(args) + 1;
  155. get_user_flagrec(dcc[idx].user, &fr, NULL);
  156. table = bind_table_lookup("dcc");
  157. size_t cmdlen = strlen(cmd);
  158. int hits = 0;
  159. for (entry = table->entries; entry && entry->next; entry = entry->next)
  160. if (!strncasecmp(cmd, entry->mask, cmdlen))
  161. ++hits;
  162. if (hits == 1) {
  163. for (entry = table->entries; entry && entry->next; entry = entry->next) {
  164. if (!strncasecmp(cmd, entry->mask, cmdlen)) {
  165. if (has_cmd_pass(entry->mask)) {
  166. if (flagrec_ok(&entry->user_flags, &fr)) {
  167. char *p = NULL, work[1024] = "", pass[MAXPASSLEN + 1] = "";
  168. p = strchr(args, ' ');
  169. if (p)
  170. *p = 0;
  171. strlcpy(pass, args, sizeof(pass));
  172. if (check_cmd_pass(entry->mask, pass)) {
  173. if (p)
  174. *p = ' ';
  175. strlcpy(work, args, sizeof(work));
  176. p = work;
  177. newsplit(&p);
  178. strlcpy(args, p, args_siz);
  179. } else {
  180. dprintf(idx, "Invalid command password.\n");
  181. dprintf(idx, "Use: $b%scommand <password> [arguments]$b\n", (dcc[idx].u.chat->channel >= 0) ? settings.dcc_prefix : "");
  182. if (p)
  183. p++;
  184. putlog(LOG_CMDS, "*", "$ #%s# %s **hidden** %s", dcc[idx].nick, entry->mask, p && *p ? p : "");
  185. putlog(LOG_MISC, "*", "%s attempted %s%s with missing or incorrect command password", dcc[idx].nick, settings.dcc_prefix, entry->mask);
  186. free(args);
  187. return 0;
  188. }
  189. } else {
  190. putlog(LOG_CMDS, "*", "! #%s# %s %s", dcc[idx].nick, cmd, args);
  191. dprintf(idx, "What? You need '%shelp'\n", (dcc[idx].u.chat->channel >= 0) ? settings.dcc_prefix : "");
  192. free(args);
  193. return 0;
  194. }
  195. }
  196. break;
  197. }
  198. }
  199. }
  200. if (entry && auth) {
  201. if (!(entry->cflags & AUTH)) {
  202. free(args);
  203. return 0;
  204. }
  205. }
  206. hits = 0;
  207. bool log_bad = 0;
  208. int ret = check_bind_hits(BT_dcc, cmd, &fr, &hits, idx, args);
  209. if (hits != 1)
  210. log_bad = 1;
  211. if (hits == 0) {
  212. if (!check_aliases(idx, cmd, args)) {
  213. log_bad = 1;
  214. dprintf(idx, "What? You need '%shelp'\n", (dcc[idx].u.chat->channel >= 0) ? settings.dcc_prefix : "");
  215. } else
  216. log_bad = 0;
  217. } else if (hits > 1)
  218. dprintf(idx, "Ambiguous command.\n");
  219. if (log_bad)
  220. putlog(LOG_CMDS, "*", "! #%s# %s %s", dcc[idx].nick, cmd, args);
  221. free(args);
  222. return ret;
  223. }
  224. void check_bind_bot(const char *nick, const char *code, const char *param)
  225. {
  226. char *mynick = NULL, *myparam = NULL, *p1 = NULL, *p2 = NULL;
  227. mynick = p1 = strdup(nick);
  228. myparam = p2 = strdup(param);
  229. check_bind(BT_bot, code, NULL, mynick, code, myparam);
  230. free(p1);
  231. free(p2);
  232. }
  233. void check_bind_chon(char *hand, int idx)
  234. {
  235. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  236. struct userrec *u = NULL;
  237. u = get_user_by_handle(userlist, hand);
  238. touch_laston(u, "partyline", now);
  239. get_user_flagrec(u, &fr, NULL);
  240. check_bind(BT_chon, hand, &fr, hand, idx);
  241. }
  242. void check_bind_chof(char *hand, int idx)
  243. {
  244. struct userrec *u = NULL;
  245. u = get_user_by_handle(userlist, hand);
  246. touch_laston(u, "partyline", now);
  247. }
  248. void check_bind_time(struct tm *tm)
  249. {
  250. char full[32] = "";
  251. simple_snprintf(full, sizeof(full), "%02d %02d %02d %02d %04d", tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year + 1900);
  252. check_bind(BT_time, full, NULL, tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year + 1900);
  253. }
  254. /* vim: set sts=2 sw=2 ts=8 et: */