tclhash.c 11 KB

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