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