core_binds.c 8.1 KB

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