binds.cc 15 KB

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