core_binds.c 8.8 KB

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