tclhash.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * tclhash.c -- handles:
  3. * bind and unbind
  4. * checking and triggering the various in-bot bindings
  5. * listing current bindings
  6. * adding/removing new binding tables
  7. * (non-Tcl) procedure lookups for msg/dcc/file commands
  8. * (Tcl) binding internal procedures to msg/dcc/file commands
  9. *
  10. */
  11. #include "common.h"
  12. #include "tclhash.h"
  13. #include "cmds.h"
  14. #include "debug.h"
  15. #include "chan.h"
  16. #include "users.h"
  17. #include "match.h"
  18. #include "egg_timer.h"
  19. #include <stdarg.h>
  20. /* The head of the bind table linked list. */
  21. static bind_table_t *bind_table_list_head = NULL;
  22. /* Garbage collection stuff. */
  23. static int check_bind_executing = 0;
  24. static int already_scheduled = 0;
  25. /* main routine for bind checks */
  26. static int bind_vcheck_hits (bind_table_t *table, const char *match, struct flag_record *flags, int *hits, va_list args);
  27. static void bind_table_really_del(bind_table_t *table);
  28. static void bind_entry_really_del(bind_table_t *table, bind_entry_t *entry);
  29. static char *maze = ".===============================================================.\n| ,-----------------, |\n| /| HELP THE BUNNY |==========.===. .===. .===========. |\n|| | FIND HIS | | | | | | .-. | E |\n|| | EASTER EGGS! | |===| | | | | |..==./xxx\\ | N |\n|| |_________________| | | | /<<<<<\\ || D |\n||/_________________/ .=======' | . | \\>>>>>/xxxx/--. |\n| | | | | | | | |`'==''---; * *`\\\n| | '===========' | |=======' | | | ,===. \\* * */\n| | | | | | | | '--'`|\n| '===============| '===. |===. | | |===' '=======|\n| | | | | | |\n| |===============. . '===| | |===| | .=======. |\n| | | | | | | | | |\n| .===========. | | |===. | | | | | | | |\n| | | | | | | | | | | | | |\n| | .===. | | |===. '===| | '===' | | | |\n| | | | | | | | | | | | | |\n| '===' /`\\ '===' | '===. | '===========| | | |\n| / : | | | | | | |\n| _.._=====| '/ '===| .=======' '===========. | | | |\n .-._ '-\"` (=======. | .===============. | | '===. |\n_/ |/ e e\\==. | | | | | | | |\n| S || > @ )<| | | | .=======. | | |===. | |\n| T | \\ '--`/ | | | | | | | | | | | |\n| A | / '--<` | | | | | | | | '===' | ' |\n| R || , \\\\ | | | | | | | |\n| T |; ; \\\\__'======. | | '===' | .===. | | |\n| / / |.__)==, | | | | | | | | |\n| (_/,--. ; //\"\"\"\\\\ | | '===========' | '===' | |\n| { `| \\_/ ||___|| | | | | |\n| ;-\\ / | |(___)| | '===========. | '=======. | |\n| | | / | |XXXXX| | | | | | |\n| | / \\ '-,\\XXXXX/ | .===========' '=======. | | |\n| | \\__|----' `\"\"\"` | | | | | |\n| '===================' '=======================' '===' |\n| |\n'==============================================================='";
  30. static int internal_bind_cleanup()
  31. {
  32. bind_table_t *table = NULL, *next_table = NULL;
  33. bind_entry_t *entry = NULL, *next_entry = NULL;
  34. if (maze) { ; } /* gcc warnings */
  35. for (table = bind_table_list_head; table; table = next_table) {
  36. next_table = table->next;
  37. if (table->flags & BIND_DELETED) {
  38. bind_table_really_del(table);
  39. continue;
  40. }
  41. for (entry = table->entries; entry; entry = next_entry) {
  42. next_entry = entry->next;
  43. if (entry->flags & BIND_DELETED) bind_entry_really_del(table, entry);
  44. }
  45. }
  46. already_scheduled = 0;
  47. return(0);
  48. }
  49. static void schedule_bind_cleanup()
  50. {
  51. if (already_scheduled)
  52. return;
  53. already_scheduled = 1;
  54. egg_timeval_t when = { 0, 0 };
  55. timer_create(&when, "internal_bind_cleanup", internal_bind_cleanup);
  56. }
  57. void kill_binds(void)
  58. {
  59. while (bind_table_list_head) bind_table_del(bind_table_list_head);
  60. }
  61. bind_table_t *bind_table_add(const char *name, int nargs, const char *syntax, int match_type, int flags)
  62. {
  63. bind_table_t *table = NULL;
  64. for (table = bind_table_list_head; table; table = table->next) {
  65. if (!strcmp(table->name, name)) break;
  66. }
  67. /* If it doesn't exist, create it. */
  68. if (!table) {
  69. table = (bind_table_t *) my_calloc(1, sizeof(*table));
  70. table->name = strdup(name);
  71. table->next = bind_table_list_head;
  72. bind_table_list_head = table;
  73. }
  74. else if (!(table->flags & BIND_FAKE)) return(table);
  75. table->nargs = nargs;
  76. if (syntax) table->syntax = strdup(syntax);
  77. table->match_type = match_type;
  78. table->flags = flags;
  79. return(table);
  80. }
  81. void bind_table_del(bind_table_t *table)
  82. {
  83. bind_table_t *cur = NULL, *prev = NULL;
  84. for (prev = NULL, cur = bind_table_list_head; cur; prev = cur, cur = cur->next) {
  85. if (!strcmp(table->name, cur->name)) break;
  86. }
  87. /* If it's found, remove it from the list. */
  88. if (cur) {
  89. if (prev) prev->next = cur->next;
  90. else bind_table_list_head = cur->next;
  91. }
  92. /* Now delete it. */
  93. if (check_bind_executing) {
  94. table->flags |= BIND_DELETED;
  95. schedule_bind_cleanup();
  96. }
  97. else {
  98. bind_table_really_del(table);
  99. }
  100. }
  101. static void bind_table_really_del(bind_table_t *table)
  102. {
  103. bind_entry_t *entry = NULL, *next = NULL;
  104. free(table->name);
  105. for (entry = table->entries; entry; entry = next) {
  106. next = entry->next;
  107. if (entry->function_name) free(entry->function_name);
  108. if (entry->mask) free(entry->mask);
  109. free(entry);
  110. }
  111. free(table);
  112. }
  113. bind_table_t *bind_table_lookup(const char *name)
  114. {
  115. bind_table_t *table = NULL;
  116. for (table = bind_table_list_head; table; table = table->next) {
  117. if (!(table->flags & BIND_DELETED) && !strcmp(table->name, name)) break;
  118. }
  119. return(table);
  120. }
  121. bind_table_t *bind_table_lookup_or_fake(const char *name)
  122. {
  123. bind_table_t *table = NULL;
  124. table = bind_table_lookup(name);
  125. if (!table) table = bind_table_add(name, 0, NULL, 0, BIND_FAKE);
  126. return(table);
  127. }
  128. /* Look up a bind entry based on either function name or id. */
  129. static bind_entry_t *bind_entry_lookup(bind_table_t *table, int id, const char *mask, const char *function_name, Function callback)
  130. {
  131. bind_entry_t *entry = NULL;
  132. int hit;
  133. for (entry = table->entries; entry; entry = entry->next) {
  134. if (entry->flags & BIND_DELETED) continue;
  135. if (entry->id >= 0) {
  136. if (entry->id == id) break;
  137. }
  138. else {
  139. hit = 0;
  140. if (entry->mask && !strcmp(entry->mask, mask)) hit++;
  141. else if (!entry->mask) hit++;
  142. if (entry->function_name && !strcmp(entry->function_name, function_name)) hit++;
  143. if (entry->callback == (HashFunc) callback || !callback) hit++;
  144. if (hit == 3) break;
  145. }
  146. }
  147. return(entry);
  148. }
  149. int bind_entry_del(bind_table_t *table, int id, const char *mask, const char *function_name, Function callback)
  150. {
  151. bind_entry_t *entry = NULL;
  152. entry = bind_entry_lookup(table, id, mask, function_name, callback);
  153. if (!entry) return(-1);
  154. /* Delete it. */
  155. if (check_bind_executing) {
  156. entry->flags |= BIND_DELETED;
  157. schedule_bind_cleanup();
  158. }
  159. else bind_entry_really_del(table, entry);
  160. return(0);
  161. }
  162. static void bind_entry_really_del(bind_table_t *table, bind_entry_t *entry)
  163. {
  164. if (entry->next) entry->next->prev = entry->prev;
  165. if (entry->prev) entry->prev->next = entry->next;
  166. else table->entries = entry->next;
  167. if (entry->function_name) free(entry->function_name);
  168. if (entry->mask) free(entry->mask);
  169. memset(entry, 0, sizeof(*entry));
  170. free(entry);
  171. }
  172. /* Modify a bind entry's flags and mask. */
  173. int bind_entry_modify(bind_table_t *table, int id, const char *mask, const char *function_name, const char *newflags, const char *newmask)
  174. {
  175. bind_entry_t *entry = bind_entry_lookup(table, id, mask, function_name, NULL);
  176. if (!entry)
  177. return(-1);
  178. /* Modify it. */
  179. if (newflags) {
  180. entry->user_flags.match = FR_GLOBAL | FR_CHAN;
  181. break_down_flags(newflags, &entry->user_flags, NULL);
  182. }
  183. /* if (newflags) flag_from_str(&entry->user_flags, newflags); */
  184. if (newmask) str_redup(&entry->mask, newmask);
  185. return(0);
  186. }
  187. /* void blah()
  188. {
  189. bind_entry_t *entry = NULL;
  190. bind_table_t *table = NULL;
  191. table = bind_table_lookup_or_fake("dcc");
  192. for (entry = table->entries; entry && entry->next; entry = entry->next) {
  193. printf("MASK: %s\n", entry->mask);
  194. }
  195. }
  196. */
  197. /* Overwrite a bind entry's callback and client_data. */
  198. int bind_entry_overwrite(bind_table_t *table, int id, const char *mask, const char *function_name, Function callback, void *client_data)
  199. {
  200. bind_entry_t *entry = bind_entry_lookup(table, id, mask, function_name, NULL);
  201. if (!entry)
  202. return(-1);
  203. entry->callback = (HashFunc) callback;
  204. entry->client_data = client_data;
  205. return(0);
  206. }
  207. int bind_entry_add(bind_table_t *table, const char *flags, const char *mask, const char *function_name, int bind_flags, Function callback, void *client_data)
  208. {
  209. bind_entry_t *entry = NULL, *old_entry = bind_entry_lookup(table, -1, mask, function_name, NULL);
  210. if (old_entry) {
  211. if (table->flags & BIND_STACKABLE) {
  212. entry = (bind_entry_t *) my_calloc(1, sizeof(*entry));
  213. entry->prev = old_entry;
  214. entry->next = old_entry->next;
  215. old_entry->next = entry;
  216. if (entry->next) entry->next->prev = entry;
  217. }
  218. else {
  219. entry = old_entry;
  220. if (entry->function_name) free(entry->function_name);
  221. if (entry->mask) free(entry->mask);
  222. }
  223. }
  224. else {
  225. for (old_entry = table->entries; old_entry && old_entry->next; old_entry = old_entry->next) {
  226. ; /* empty loop */
  227. }
  228. entry = (bind_entry_t *) my_calloc(1, sizeof(*entry));
  229. if (old_entry) old_entry->next = entry;
  230. else table->entries = entry;
  231. entry->prev = old_entry;
  232. }
  233. /* if (flags) flag_from_str(&entry->user_flags, flags); */
  234. if (flags) {
  235. entry->user_flags.match = FR_GLOBAL | FR_CHAN;
  236. break_down_flags(flags, &entry->user_flags, NULL);
  237. }
  238. if (mask) entry->mask = strdup(mask);
  239. if (function_name) entry->function_name = strdup(function_name);
  240. entry->callback = (HashFunc) callback;
  241. entry->client_data = client_data;
  242. entry->flags = bind_flags;
  243. return(0);
  244. }
  245. /* Execute a bind entry with the given argument list. */
  246. static int bind_entry_exec(bind_table_t *table, bind_entry_t *entry, void **al)
  247. {
  248. bind_entry_t *prev = NULL;
  249. int retval;
  250. ContextNote(entry->mask);
  251. /* Give this entry a hit. */
  252. entry->nhits++;
  253. /* Does the callback want client data? */
  254. if (entry->flags & BIND_WANTS_CD) {
  255. *al = entry->client_data;
  256. }
  257. else al++;
  258. retval = entry->callback(al[0], al[1], al[2], al[3], al[4], al[5], al[6], al[7], al[8], al[9]);
  259. if (table->match_type & (MATCH_MASK | MATCH_PARTIAL | MATCH_NONE)) return(retval);
  260. /* Search for the last entry that isn't deleted. */
  261. for (prev = entry->prev; prev; prev = prev->prev) {
  262. if (!(prev->flags & BIND_DELETED) && (prev->nhits >= entry->nhits)) break;
  263. }
  264. /* See if this entry is more popular than the preceding one. */
  265. if (entry->prev != prev) {
  266. /* Remove entry. */
  267. if (entry->prev) entry->prev->next = entry->next;
  268. else table->entries = entry->next;
  269. if (entry->next) entry->next->prev = entry->prev;
  270. /* Re-add in correct position. */
  271. if (prev) {
  272. entry->next = prev->next;
  273. if (prev->next) prev->next->prev = entry;
  274. prev->next = entry;
  275. }
  276. else {
  277. entry->next = table->entries;
  278. table->entries = entry;
  279. }
  280. entry->prev = prev;
  281. if (entry->next) entry->next->prev = entry;
  282. }
  283. return(retval);
  284. }
  285. int check_bind(bind_table_t *table, const char *match, struct flag_record *flags, ...)
  286. {
  287. va_list args;
  288. int ret;
  289. va_start (args, flags);
  290. ret = bind_vcheck_hits (table, match, flags, NULL, args);
  291. va_end (args);
  292. return ret;
  293. }
  294. int check_bind_hits(bind_table_t *table, const char *match, struct flag_record *flags, int *hits, ...)
  295. {
  296. va_list args;
  297. int ret;
  298. va_start (args, hits);
  299. ret = bind_vcheck_hits (table, match, flags, hits, args);
  300. va_end (args);
  301. return ret;
  302. }
  303. static int bind_vcheck_hits (bind_table_t *table, const char *match, struct flag_record *flags, int *hits, va_list ap)
  304. {
  305. void *args[11] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
  306. bind_entry_t *entry = NULL, *next = NULL, *winner = NULL;
  307. int cmp, retval, tie = 0;
  308. size_t matchlen = 0;
  309. Assert(table);
  310. check_bind_executing++;
  311. for (int i = 1; i <= table->nargs; i++) {
  312. args[i] = va_arg(ap, void *);
  313. }
  314. if (hits) (*hits) = 0;
  315. /* Default return value is 0 */
  316. retval = 0;
  317. /* Check if we're searching for a partial match. */
  318. if (table->match_type & MATCH_PARTIAL) matchlen = strlen(match);
  319. for (entry = table->entries; entry; entry = next) {
  320. next = entry->next;
  321. if (entry->flags & BIND_DELETED) continue;
  322. /* Check flags. */
  323. if (table->match_type & MATCH_FLAGS) {
  324. /*wtf? if (!(entry->user_flags.builtin | entry->user_flags.udef)) cmp = 1;
  325. else if (!user_flags) cmp = 0;
  326. else
  327. */
  328. if (entry->flags & MATCH_FLAGS_AND) cmp = flagrec_eq(&entry->user_flags, flags);
  329. else cmp = flagrec_ok(&entry->user_flags, flags);
  330. if (!cmp) continue;
  331. }
  332. if (table->match_type & MATCH_MASK) {
  333. cmp = !wild_match_per(entry->mask, (char *) match);
  334. }
  335. else if (table->match_type & MATCH_NONE) {
  336. cmp = 0;
  337. }
  338. else if (table->match_type & MATCH_PARTIAL) {
  339. cmp = 1;
  340. if (!egg_strncasecmp(match, entry->mask, matchlen)) {
  341. winner = entry;
  342. /* Is it an exact match? */
  343. if (!entry->mask[matchlen]) {
  344. tie = 1;
  345. break;
  346. }
  347. else tie++;
  348. }
  349. }
  350. else {
  351. if (table->match_type & MATCH_CASE) cmp = strcmp(entry->mask, match);
  352. /* MATCH_EXACT */
  353. else cmp = strcasecmp(entry->mask, match);
  354. }
  355. if (cmp) continue; /* Doesn't match. */
  356. if (hits) (*hits)++;
  357. retval = bind_entry_exec(table, entry, args);
  358. if ((table->flags & BIND_BREAKABLE) && (retval & BIND_RET_BREAK)) break;
  359. }
  360. /* If it's a partial match table, see if we have 1 winner. */
  361. if (winner && tie == 1) {
  362. if (hits) (*hits)++;
  363. retval = bind_entry_exec(table, winner, args);
  364. } else if (hits && tie) (*hits) = tie;
  365. check_bind_executing--;
  366. return(retval);
  367. }
  368. void add_builtins(const char *table_name, cmd_t *cmds)
  369. {
  370. char name[50] = "";
  371. bind_table_t *table = bind_table_lookup_or_fake(table_name);
  372. for (; cmds->name; cmds++) {
  373. /* FIXME: replace 1/2 with HUB/LEAF after they are removed */
  374. if (!cmds->type || (cmds->type == HUB && conf.bot->hub) || (cmds->type == LEAF && !conf.bot->hub)) {
  375. /* add BT_dcc cmds to cmdlist[] :: add to the help system.. */
  376. if (!strcmp(table->name, "dcc") && (findhelp(cmds->name) != -1)) {
  377. cmdlist[cmdi].name = cmds->name;
  378. cmdlist[cmdi].flags.match = FR_GLOBAL | FR_CHAN;
  379. break_down_flags(cmds->flags, &(cmdlist[cmdi].flags), NULL);
  380. cmdi++;
  381. }
  382. egg_snprintf(name, sizeof name, "*%s:%s", table->name, cmds->funcname ? cmds->funcname : cmds->name);
  383. bind_entry_add(table, cmds->flags, cmds->name, name, 0, cmds->func, NULL);
  384. }
  385. }
  386. }
  387. void rem_builtins(const char *table_name, cmd_t *cmds)
  388. {
  389. bind_table_t *table = bind_table_lookup(table_name);
  390. if (!table)
  391. return;
  392. char name[50] = "";
  393. for (; cmds->name; cmds++) {
  394. sprintf(name, "*%s:%s", table->name, cmds->funcname ? cmds->funcname : cmds->name);
  395. bind_entry_del(table, -1, cmds->name, name, NULL);
  396. }
  397. }