core_binds.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. if (argsp)
  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. if (argsp)
  94. free(argsp);
  95. free(aliasp);
  96. return 0; /* Show bad cmd */
  97. } else {
  98. /* nope, show alias error */
  99. dprintf(idx, "'%s' is an invalid alias: references alias '%s'.\n", cmd, p);
  100. putlog(LOG_ERROR, "*", "Invalid alias '%s' attempted: references alias '%s'.", cmd, p);
  101. if (argsp)
  102. free(argsp);
  103. free(aliasp);
  104. return 1; /* Alias was found -- just not accepted */
  105. }
  106. }
  107. char *myargs = NULL, *pass = NULL;
  108. size_t size = 0;
  109. found = 1;
  110. size = strlen(a) + 1 + (argsdup ? strlen(argsdup) : 0) + 1 + 2;
  111. myargs = (char *) calloc(1, size);
  112. /* Rewrite the cmd including the inserted cmdpass if the cmd has one, and the user provided any param */
  113. if (argsdup && argsdup[0] && has_cmd_pass(p)) {
  114. pass = newsplit(&argsdup);
  115. strlcpy(myargs, pass, size);
  116. if (a && a[0]) {
  117. strlcat(myargs, " ", size);
  118. strlcat(myargs, a, size);
  119. }
  120. if (argsdup[0]) { /* was split */
  121. strlcat(myargs, " ", size);
  122. strlcat(myargs, argsdup, size);
  123. }
  124. } else {
  125. /* Otherwise, just construct it based on cmd and params if provided */
  126. if (argsdup && argsdup[0]) {
  127. strlcpy(myargs, a, size);
  128. strlcat(myargs, " ", size);
  129. strlcat(myargs, argsdup, size);
  130. } else
  131. strlcpy(myargs, a, size);
  132. }
  133. if (a && a[0])
  134. putlog(LOG_CMDS, "*", "@ #%s# [%s -> %s %s] ...", dcc[idx].nick, cmd, p, a);
  135. else
  136. putlog(LOG_CMDS, "*", "@ #%s# [%s -> %s] ...", dcc[idx].nick, cmd, p);
  137. check_bind_dcc(p, idx, myargs);
  138. if (myargs)
  139. free(myargs);
  140. break;
  141. }
  142. }
  143. free(aliasp);
  144. if (argsp)
  145. free(argsp);
  146. return found;
  147. }
  148. int check_bind_dcc(const char *cmd, int idx, const char *text)
  149. {
  150. return real_check_bind_dcc(cmd, idx, text, NULL);
  151. }
  152. int real_check_bind_dcc(const char *cmd, int idx, const char *text, Auth *auth)
  153. {
  154. struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYCH, 0, 0, 0 };
  155. bind_entry_t *entry = NULL;
  156. bind_table_t *table = NULL;
  157. char *args = strdup(text);
  158. size_t args_siz = strlen(args) + 1;
  159. get_user_flagrec(dcc[idx].user, &fr, NULL);
  160. table = bind_table_lookup("dcc");
  161. size_t cmdlen = strlen(cmd);
  162. int hits = 0;
  163. for (entry = table->entries; entry && entry->next; entry = entry->next)
  164. if (!strncasecmp(cmd, entry->mask, cmdlen))
  165. ++hits;
  166. if (hits == 1) {
  167. for (entry = table->entries; entry && entry->next; entry = entry->next) {
  168. if (!strncasecmp(cmd, entry->mask, cmdlen)) {
  169. if (has_cmd_pass(entry->mask)) {
  170. if (flagrec_ok(&entry->user_flags, &fr)) {
  171. char *p = NULL, work[1024] = "", pass[MAXPASSLEN + 1] = "";
  172. p = strchr(args, ' ');
  173. if (p)
  174. *p = 0;
  175. strlcpy(pass, args, sizeof(pass));
  176. if (check_cmd_pass(entry->mask, pass)) {
  177. if (p)
  178. *p = ' ';
  179. strlcpy(work, args, sizeof(work));
  180. p = work;
  181. newsplit(&p);
  182. strlcpy(args, p, args_siz);
  183. } else {
  184. dprintf(idx, "Invalid command password.\n");
  185. dprintf(idx, "Use: $b%scommand <password> [arguments]$b\n", (dcc[idx].u.chat->channel >= 0) ? settings.dcc_prefix : "");
  186. if (p)
  187. p++;
  188. putlog(LOG_CMDS, "*", "$ #%s# %s **hidden** %s", dcc[idx].nick, entry->mask, p && *p ? p : "");
  189. putlog(LOG_MISC, "*", "%s attempted %s%s with missing or incorrect command password", dcc[idx].nick, settings.dcc_prefix, entry->mask);
  190. free(args);
  191. return 0;
  192. }
  193. } else {
  194. putlog(LOG_CMDS, "*", "! #%s# %s %s", dcc[idx].nick, cmd, args);
  195. dprintf(idx, "What? You need '%shelp'\n", (dcc[idx].u.chat->channel >= 0) ? settings.dcc_prefix : "");
  196. free(args);
  197. return 0;
  198. }
  199. }
  200. break;
  201. }
  202. }
  203. }
  204. if (entry && auth) {
  205. if (!(entry->cflags & AUTH))
  206. return 0;
  207. }
  208. hits = 0;
  209. bool log_bad = 0;
  210. int ret = check_bind_hits(BT_dcc, cmd, &fr, &hits, idx, args);
  211. if (hits != 1)
  212. log_bad = 1;
  213. if (hits == 0) {
  214. if (!check_aliases(idx, cmd, args)) {
  215. log_bad = 1;
  216. dprintf(idx, "What? You need '%shelp'\n", (dcc[idx].u.chat->channel >= 0) ? settings.dcc_prefix : "");
  217. } else
  218. log_bad = 0;
  219. } else if (hits > 1)
  220. dprintf(idx, "Ambiguous command.\n");
  221. if (log_bad)
  222. putlog(LOG_CMDS, "*", "! #%s# %s %s", dcc[idx].nick, cmd, args);
  223. free(args);
  224. return ret;
  225. }
  226. void check_bind_bot(const char *nick, const char *code, const char *param)
  227. {
  228. char *mynick = NULL, *myparam = NULL, *p1 = NULL, *p2 = NULL;
  229. mynick = p1 = strdup(nick);
  230. myparam = p2 = strdup(param);
  231. check_bind(BT_bot, code, NULL, mynick, code, myparam);
  232. free(p1);
  233. free(p2);
  234. }
  235. void check_bind_chon(char *hand, int idx)
  236. {
  237. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  238. struct userrec *u = NULL;
  239. u = get_user_by_handle(userlist, hand);
  240. touch_laston(u, "partyline", now);
  241. get_user_flagrec(u, &fr, NULL);
  242. check_bind(BT_chon, hand, &fr, hand, idx);
  243. }
  244. void check_bind_chof(char *hand, int idx)
  245. {
  246. struct userrec *u = NULL;
  247. u = get_user_by_handle(userlist, hand);
  248. touch_laston(u, "partyline", now);
  249. }
  250. void check_bind_time(struct tm *tm)
  251. {
  252. char full[32] = "";
  253. 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);
  254. check_bind(BT_time, full, NULL, tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year + 1900);
  255. }
  256. /* vim: set sts=2 sw=2 ts=8 et: */