binds.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2008 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 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 *) my_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. free(table->name);
  124. for (entry = table->entries; entry; entry = next) {
  125. next = entry->next;
  126. if (entry->function_name) free(entry->function_name);
  127. if (entry->mask) free(entry->mask);
  128. free(entry);
  129. }
  130. free(table);
  131. }
  132. bind_table_t *bind_table_lookup(const char *name)
  133. {
  134. bind_table_t *table = NULL;
  135. for (table = bind_table_list_head; table; table = table->next) {
  136. if (!(table->flags & BIND_DELETED) && !strcmp(table->name, name)) break;
  137. }
  138. return(table);
  139. }
  140. bind_table_t *bind_table_lookup_or_fake(const char *name)
  141. {
  142. bind_table_t *table = NULL;
  143. table = bind_table_lookup(name);
  144. if (!table) table = bind_table_add(name, 0, NULL, 0, BIND_FAKE);
  145. return(table);
  146. }
  147. /* Look up a bind entry based on either function name or id. */
  148. static bind_entry_t *bind_entry_lookup(bind_table_t *table, int id, const char *mask, const char *function_name, Function callback)
  149. {
  150. bind_entry_t *entry = NULL;
  151. int hit;
  152. for (entry = table->entries; entry; entry = entry->next) {
  153. if (entry->flags & BIND_DELETED) continue;
  154. if (entry->id >= 0) {
  155. if (entry->id == id) break;
  156. }
  157. else {
  158. hit = 0;
  159. if (entry->mask && !strcmp(entry->mask, mask)) hit++;
  160. else if (!entry->mask) hit++;
  161. if (entry->function_name && !strcmp(entry->function_name, function_name)) hit++;
  162. if (entry->callback == (HashFunc) callback || !callback) hit++;
  163. if (hit == 3) break;
  164. }
  165. }
  166. return(entry);
  167. }
  168. static int bind_entry_del(bind_table_t *table, int id, const char *mask, const char *function_name, Function callback)
  169. {
  170. bind_entry_t *entry = NULL;
  171. entry = bind_entry_lookup(table, id, mask, function_name, callback);
  172. if (!entry) return(-1);
  173. /* Delete it. */
  174. if (check_bind_executing) {
  175. entry->flags |= BIND_DELETED;
  176. schedule_bind_cleanup();
  177. }
  178. else bind_entry_really_del(table, entry);
  179. return(0);
  180. }
  181. static void bind_entry_really_del(bind_table_t *table, bind_entry_t *entry)
  182. {
  183. if (entry->next) entry->next->prev = entry->prev;
  184. if (entry->prev) entry->prev->next = entry->next;
  185. else table->entries = entry->next;
  186. if (entry->function_name) free(entry->function_name);
  187. if (entry->mask) free(entry->mask);
  188. memset(entry, 0, sizeof(*entry));
  189. free(entry);
  190. }
  191. 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)
  192. {
  193. bind_entry_t *entry = NULL, *old_entry = bind_entry_lookup(table, -1, mask, function_name, NULL);
  194. if (old_entry) {
  195. if (table->flags & BIND_STACKABLE) {
  196. entry = (bind_entry_t *) my_calloc(1, sizeof(*entry));
  197. entry->prev = old_entry;
  198. entry->next = old_entry->next;
  199. old_entry->next = entry;
  200. if (entry->next) entry->next->prev = entry;
  201. }
  202. else {
  203. entry = old_entry;
  204. if (entry->function_name) free(entry->function_name);
  205. if (entry->mask) free(entry->mask);
  206. }
  207. }
  208. else {
  209. for (old_entry = table->entries; old_entry && old_entry->next; old_entry = old_entry->next) {
  210. ; /* empty loop */
  211. }
  212. entry = (bind_entry_t *) my_calloc(1, sizeof(*entry));
  213. if (old_entry) old_entry->next = entry;
  214. else table->entries = entry;
  215. entry->prev = old_entry;
  216. }
  217. /* if (flags) flag_from_str(&entry->user_flags, flags); */
  218. if (flags) {
  219. entry->user_flags.match = FR_GLOBAL | FR_CHAN;
  220. break_down_flags(flags, &entry->user_flags, NULL);
  221. }
  222. entry->cflags = cflags;
  223. if (mask) entry->mask = strdup(mask);
  224. if (function_name) entry->function_name = strdup(function_name);
  225. entry->callback = (HashFunc) callback;
  226. entry->client_data = client_data;
  227. entry->flags = bind_flags;
  228. return(0);
  229. }
  230. /* Execute a bind entry with the given argument list. */
  231. static int bind_entry_exec(bind_table_t *table, bind_entry_t *entry, void **al)
  232. {
  233. bind_entry_t *prev = NULL;
  234. int retval;
  235. ContextNote(entry->mask);
  236. /* Give this entry a hit. */
  237. entry->nhits++;
  238. /* Does the callback want client data? */
  239. if (entry->flags & BIND_WANTS_CD) {
  240. *al = entry->client_data;
  241. }
  242. else al++;
  243. retval = entry->callback(al[0], al[1], al[2], al[3], al[4], al[5], al[6], al[7], al[8], al[9]);
  244. if (table->match_type & (MATCH_MASK | MATCH_PARTIAL | MATCH_NONE)) return(retval);
  245. /* Search for the last entry that isn't deleted. */
  246. for (prev = entry->prev; prev; prev = prev->prev) {
  247. if (!(prev->flags & BIND_DELETED) && (prev->nhits >= entry->nhits)) break;
  248. }
  249. /* See if this entry is more popular than the preceding one. */
  250. if (entry->prev != prev) {
  251. /* Remove entry. */
  252. if (entry->prev) entry->prev->next = entry->next;
  253. else table->entries = entry->next;
  254. if (entry->next) entry->next->prev = entry->prev;
  255. /* Re-add in correct position. */
  256. if (prev) {
  257. entry->next = prev->next;
  258. if (prev->next) prev->next->prev = entry;
  259. prev->next = entry;
  260. }
  261. else {
  262. entry->next = table->entries;
  263. table->entries = entry;
  264. }
  265. entry->prev = prev;
  266. if (entry->next) entry->next->prev = entry;
  267. }
  268. return(retval);
  269. }
  270. int check_bind(bind_table_t *table, const char *match, struct flag_record *flags, ...)
  271. {
  272. va_list args;
  273. int ret;
  274. va_start (args, flags);
  275. ret = bind_vcheck_hits (table, match, flags, NULL, args);
  276. va_end (args);
  277. return ret;
  278. }
  279. int check_bind_hits(bind_table_t *table, const char *match, struct flag_record *flags, int *hits, ...)
  280. {
  281. va_list args;
  282. int ret;
  283. va_start (args, hits);
  284. ret = bind_vcheck_hits (table, match, flags, hits, args);
  285. va_end (args);
  286. return ret;
  287. }
  288. static int bind_vcheck_hits (bind_table_t *table, const char *match, struct flag_record *flags, int *hits, va_list ap)
  289. {
  290. void *args[11] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
  291. bind_entry_t *entry = NULL, *next = NULL, *winner = NULL;
  292. int cmp, retval, tie = 0;
  293. size_t matchlen = 0;
  294. check_bind_executing++;
  295. for (int i = 1; i <= table->nargs; i++) {
  296. args[i] = va_arg(ap, void *);
  297. }
  298. if (hits) (*hits) = 0;
  299. /* Default return value is 0 */
  300. retval = 0;
  301. /* Check if we're searching for a partial match. */
  302. if (table->match_type & MATCH_PARTIAL) matchlen = strlen(match);
  303. for (entry = table->entries; entry; entry = next) {
  304. next = entry->next;
  305. if (entry->flags & BIND_DELETED) continue;
  306. /* Check flags. */
  307. if (table->match_type & MATCH_FLAGS) {
  308. /*wtf? if (!(entry->user_flags.builtin | entry->user_flags.udef)) cmp = 1;
  309. else if (!user_flags) cmp = 0;
  310. else
  311. */
  312. if (entry->flags & MATCH_FLAGS_AND) cmp = flagrec_eq(&entry->user_flags, flags);
  313. else cmp = flagrec_ok(&entry->user_flags, flags);
  314. if (!cmp) continue;
  315. }
  316. if (table->match_type & MATCH_MASK) {
  317. cmp = !wild_match_per(entry->mask, (char *) match);
  318. }
  319. else if (table->match_type & MATCH_NONE) {
  320. cmp = 0;
  321. }
  322. else if (table->match_type & MATCH_PARTIAL) {
  323. cmp = 1;
  324. if (!strncasecmp(match, entry->mask, matchlen)) {
  325. winner = entry;
  326. /* Is it an exact match? */
  327. if (!entry->mask[matchlen]) {
  328. tie = 1;
  329. break;
  330. }
  331. else tie++;
  332. }
  333. }
  334. else {
  335. if (table->match_type & MATCH_CASE) cmp = strcmp(entry->mask, match);
  336. /* MATCH_EXACT */
  337. else cmp = strcasecmp(entry->mask, match);
  338. }
  339. if (cmp) continue; /* Doesn't match. */
  340. if (hits) (*hits)++;
  341. retval = bind_entry_exec(table, entry, args);
  342. if ((table->flags & BIND_BREAKABLE) && (retval & BIND_RET_BREAK)) break;
  343. }
  344. /* If it's a partial match table, see if we have 1 winner. */
  345. if (winner && tie == 1) {
  346. if (hits) (*hits)++;
  347. retval = bind_entry_exec(table, winner, args);
  348. } else if (hits && tie) (*hits) = tie;
  349. check_bind_executing--;
  350. return(retval);
  351. }
  352. void add_builtins(const char *table_name, cmd_t *cmds)
  353. {
  354. char name[50] = "";
  355. bind_table_t *table = bind_table_lookup_or_fake(table_name);
  356. for (; cmds->name; cmds++) {
  357. if (have_cmd(cmds->name, cmds->type)) {
  358. /* add BT_dcc cmds to cmdlist[] :: add to the help system.. */
  359. if (!strcmp(table->name, "dcc") && (findhelp(cmds->name) != NULL)) {
  360. cmdlist[cmdi].name = cmds->name;
  361. cmdlist[cmdi].type = cmds->type;
  362. cmdlist[cmdi].flags.match = FR_GLOBAL | FR_CHAN;
  363. break_down_flags(cmds->flags, &(cmdlist[cmdi].flags), NULL);
  364. cmdi++;
  365. }
  366. simple_snprintf(name, sizeof name, "*%s:%s", table->name, cmds->funcname ? cmds->funcname : cmds->name);
  367. bind_entry_add(table, cmds->flags, cmds->type, cmds->name, name, 0, cmds->func, NULL);
  368. }
  369. }
  370. }
  371. void rem_builtins(const char *table_name, cmd_t *cmds)
  372. {
  373. bind_table_t *table = bind_table_lookup(table_name);
  374. if (!table)
  375. return;
  376. char name[50] = "";
  377. for (; cmds->name; cmds++) {
  378. simple_snprintf(name, sizeof(name), "*%s:%s", table->name, cmds->funcname ? cmds->funcname : cmds->name);
  379. bind_entry_del(table, -1, cmds->name, name, NULL);
  380. }
  381. }
  382. /* vim: set sts=4 sw=4 ts=4 noet: */