tclhash.c 13 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. void binds_init(void)
  30. {
  31. bind_table_list_head = NULL;
  32. egg_bzero(&cmdlist, 500);
  33. }
  34. static int internal_bind_cleanup()
  35. {
  36. bind_table_t *table = NULL, *next_table = NULL;
  37. bind_entry_t *entry = NULL, *next_entry = NULL;
  38. for (table = bind_table_list_head; table; table = next_table) {
  39. next_table = table->next;
  40. if (table->flags & BIND_DELETED) {
  41. bind_table_really_del(table);
  42. continue;
  43. }
  44. for (entry = table->entries; entry; entry = next_entry) {
  45. next_entry = entry->next;
  46. if (entry->flags & BIND_DELETED) bind_entry_really_del(table, entry);
  47. }
  48. }
  49. already_scheduled = 0;
  50. return(0);
  51. }
  52. static void schedule_bind_cleanup()
  53. {
  54. egg_timeval_t when;
  55. if (already_scheduled) return;
  56. already_scheduled = 1;
  57. when.sec = 0;
  58. when.usec = 0;
  59. timer_create(&when, "internal_bind_cleanup", internal_bind_cleanup);
  60. }
  61. void kill_binds(void)
  62. {
  63. while (bind_table_list_head) bind_table_del(bind_table_list_head);
  64. }
  65. bind_table_t *bind_table_add(const char *name, int nargs, const char *syntax, int match_type, int flags)
  66. {
  67. bind_table_t *table = NULL;
  68. for (table = bind_table_list_head; table; table = table->next) {
  69. if (!strcmp(table->name, name)) break;
  70. }
  71. /* If it doesn't exist, create it. */
  72. if (!table) {
  73. table = (bind_table_t *)calloc(1, sizeof(*table));
  74. table->name = strdup(name);
  75. table->next = bind_table_list_head;
  76. bind_table_list_head = table;
  77. }
  78. else if (!(table->flags & BIND_FAKE)) return(table);
  79. table->nargs = nargs;
  80. if (syntax) table->syntax = strdup(syntax);
  81. table->match_type = match_type;
  82. table->flags = flags;
  83. return(table);
  84. }
  85. void bind_table_del(bind_table_t *table)
  86. {
  87. bind_table_t *cur = NULL, *prev = NULL;
  88. for (prev = NULL, cur = bind_table_list_head; cur; prev = cur, cur = cur->next) {
  89. if (!strcmp(table->name, cur->name)) break;
  90. }
  91. /* If it's found, remove it from the list. */
  92. if (cur) {
  93. if (prev) prev->next = cur->next;
  94. else bind_table_list_head = cur->next;
  95. }
  96. /* Now delete it. */
  97. if (check_bind_executing) {
  98. table->flags |= BIND_DELETED;
  99. schedule_bind_cleanup();
  100. }
  101. else {
  102. bind_table_really_del(table);
  103. }
  104. }
  105. static void bind_table_really_del(bind_table_t *table)
  106. {
  107. bind_entry_t *entry = NULL, *next = NULL;
  108. free(table->name);
  109. for (entry = table->entries; entry; entry = next) {
  110. next = entry->next;
  111. if (entry->function_name) free(entry->function_name);
  112. if (entry->mask) free(entry->mask);
  113. free(entry);
  114. }
  115. free(table);
  116. }
  117. bind_table_t *bind_table_lookup(const char *name)
  118. {
  119. bind_table_t *table = NULL;
  120. for (table = bind_table_list_head; table; table = table->next) {
  121. if (!(table->flags & BIND_DELETED) && !strcmp(table->name, name)) break;
  122. }
  123. return(table);
  124. }
  125. bind_table_t *bind_table_lookup_or_fake(const char *name)
  126. {
  127. bind_table_t *table = NULL;
  128. table = bind_table_lookup(name);
  129. if (!table) table = bind_table_add(name, 0, NULL, 0, BIND_FAKE);
  130. return(table);
  131. }
  132. /* Look up a bind entry based on either function name or id. */
  133. static bind_entry_t *bind_entry_lookup(bind_table_t *table, int id, const char *mask, const char *function_name, Function callback)
  134. {
  135. bind_entry_t *entry = NULL;
  136. int hit;
  137. for (entry = table->entries; entry; entry = entry->next) {
  138. if (entry->flags & BIND_DELETED) continue;
  139. if (entry->id >= 0) {
  140. if (entry->id == id) break;
  141. }
  142. else {
  143. hit = 0;
  144. if (entry->mask && !strcmp(entry->mask, mask)) hit++;
  145. else if (!entry->mask) hit++;
  146. if (entry->function_name && !strcmp(entry->function_name, function_name)) hit++;
  147. if (entry->callback == callback || !callback) hit++;
  148. if (hit == 3) break;
  149. }
  150. }
  151. return(entry);
  152. }
  153. int bind_entry_del(bind_table_t *table, int id, const char *mask, const char *function_name, Function callback)
  154. {
  155. bind_entry_t *entry = NULL;
  156. entry = bind_entry_lookup(table, id, mask, function_name, callback);
  157. if (!entry) return(-1);
  158. /* Delete it. */
  159. if (check_bind_executing) {
  160. entry->flags |= BIND_DELETED;
  161. schedule_bind_cleanup();
  162. }
  163. else bind_entry_really_del(table, entry);
  164. return(0);
  165. }
  166. static void bind_entry_really_del(bind_table_t *table, bind_entry_t *entry)
  167. {
  168. if (entry->next) entry->next->prev = entry->prev;
  169. if (entry->prev) entry->prev->next = entry->next;
  170. else table->entries = entry->next;
  171. if (entry->function_name) free(entry->function_name);
  172. if (entry->mask) free(entry->mask);
  173. memset(entry, 0, sizeof(*entry));
  174. free(entry);
  175. }
  176. /* Modify a bind entry's flags and mask. */
  177. int bind_entry_modify(bind_table_t *table, int id, const char *mask, const char *function_name, const char *newflags, const char *newmask)
  178. {
  179. bind_entry_t *entry = NULL;
  180. entry = bind_entry_lookup(table, id, mask, function_name, NULL);
  181. if (!entry) return(-1);
  182. /* Modify it. */
  183. if (newflags) {
  184. entry->user_flags.match = FR_GLOBAL | FR_CHAN;
  185. break_down_flags(newflags, &entry->user_flags, NULL);
  186. }
  187. /* if (newflags) flag_from_str(&entry->user_flags, newflags); */
  188. if (newmask) str_redup(&entry->mask, newmask);
  189. return(0);
  190. }
  191. /* void blah()
  192. {
  193. bind_entry_t *entry = NULL;
  194. bind_table_t *table = NULL;
  195. table = bind_table_lookup_or_fake("dcc");
  196. for (entry = table->entries; entry && entry->next; entry = entry->next) {
  197. printf("MASK: %s\n", entry->mask);
  198. }
  199. }
  200. */
  201. /* Overwrite a bind entry's callback and client_data. */
  202. int bind_entry_overwrite(bind_table_t *table, int id, const char *mask, const char *function_name, Function callback, void *client_data)
  203. {
  204. bind_entry_t *entry = NULL;
  205. entry = bind_entry_lookup(table, id, mask, function_name, NULL);
  206. if (!entry) return(-1);
  207. entry->callback = callback;
  208. entry->client_data = client_data;
  209. return(0);
  210. }
  211. 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)
  212. {
  213. bind_entry_t *entry = NULL, *old_entry = NULL;
  214. 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 = 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 i, cmp, retval;
  313. int tie = 0, matchlen = 0;
  314. Assert(table);
  315. check_bind_executing++;
  316. for (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((unsigned char *)entry->mask, (unsigned 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. else cmp = strcasecmp(entry->mask, match);
  358. }
  359. if (cmp) continue; /* Doesn't match. */
  360. if (hits) (*hits)++;
  361. retval = bind_entry_exec(table, entry, args);
  362. if ((table->flags & BIND_BREAKABLE) && (retval & BIND_RET_BREAK)) break;
  363. }
  364. /* If it's a partial match table, see if we have 1 winner. */
  365. if (winner && tie == 1) {
  366. if (hits) (*hits)++;
  367. retval = bind_entry_exec(table, winner, args);
  368. } else if (hits && tie) (*hits) = tie;
  369. check_bind_executing--;
  370. return(retval);
  371. }
  372. void add_builtins(const char *table_name, cmd_t *cmds)
  373. {
  374. char name[50] = "";
  375. bind_table_t *table = NULL;
  376. 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")) {
  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. char name[50] = "";
  392. bind_table_t *table = NULL;
  393. table = bind_table_lookup(table_name);
  394. if (!table) return;
  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. }