console.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * console.c -- part of console.mod
  3. * saved console settings based on console.tcl
  4. * by cmwagner/billyjoe/D. Senso
  5. *
  6. * $Id: console.c,v 1.25 2002/06/06 18:52:23 wcc Exp $
  7. */
  8. /*
  9. * Copyright (C) 1999, 2000, 2001, 2002 Eggheads Development Team
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. */
  25. #define MODULE_NAME "console"
  26. #define MAKING_CONSOLE
  27. #include "src/mod/module.h"
  28. #include <stdlib.h>
  29. #include "console.h"
  30. static Function *global = NULL;
  31. static int console_autosave = 0;
  32. static int force_channel = 0;
  33. static int info_party = 0;
  34. struct console_info {
  35. char *channel;
  36. int conflags;
  37. int stripflags;
  38. int echoflags;
  39. int page;
  40. int conchan;
  41. };
  42. static struct user_entry_type USERENTRY_CONSOLE;
  43. static int console_unpack(struct userrec *u, struct user_entry *e)
  44. {
  45. struct console_info *ci = user_malloc(sizeof(struct console_info));
  46. char *par, *arg;
  47. par = e->u.list->extra;
  48. arg = newsplit(&par);
  49. ci->channel = user_malloc(strlen(arg) + 1);
  50. strcpy(ci->channel, arg);
  51. arg = newsplit(&par);
  52. ci->conflags = logmodes(arg);
  53. arg = newsplit(&par);
  54. ci->stripflags = stripmodes(arg);
  55. arg = newsplit(&par);
  56. ci->echoflags = (arg[0] == '1') ? 1 : 0;
  57. arg = newsplit(&par);
  58. ci->page = atoi(arg);
  59. arg = newsplit(&par);
  60. ci->conchan = atoi(arg);
  61. list_type_kill(e->u.list);
  62. e->u.extra = ci;
  63. return 1;
  64. }
  65. static int console_pack(struct userrec *u, struct user_entry *e)
  66. {
  67. char work[1024];
  68. struct console_info *ci;
  69. int l;
  70. ci = (struct console_info *) e->u.extra;
  71. l = simple_sprintf(work, "%s %s %s %d %d %d",
  72. ci->channel, masktype(ci->conflags),
  73. stripmasktype(ci->stripflags), ci->echoflags,
  74. ci->page, ci->conchan);
  75. e->u.list = user_malloc(sizeof(struct list_type));
  76. e->u.list->next = NULL;
  77. e->u.list->extra = user_malloc(l + 1);
  78. strcpy(e->u.list->extra, work);
  79. nfree(ci->channel);
  80. nfree(ci);
  81. return 1;
  82. }
  83. static int console_kill(struct user_entry *e)
  84. {
  85. struct console_info *i = e->u.extra;
  86. nfree(i->channel);
  87. nfree(i);
  88. nfree(e);
  89. return 1;
  90. }
  91. static int console_write_userfile(FILE *f, struct userrec *u,
  92. struct user_entry *e)
  93. {
  94. struct console_info *i = e->u.extra;
  95. if (fprintf(f, "--CONSOLE %s %s %s %d %d %d\n",
  96. i->channel, masktype(i->conflags),
  97. stripmasktype(i->stripflags), i->echoflags,
  98. i->page, i->conchan) == EOF)
  99. return 0;
  100. return 1;
  101. }
  102. static int console_set(struct userrec *u, struct user_entry *e, void *buf)
  103. {
  104. struct console_info *ci = (struct console_info *) e->u.extra;
  105. if (!ci && !buf)
  106. return 1;
  107. if (ci != buf) {
  108. if (ci) {
  109. nfree(ci->channel);
  110. nfree(ci);
  111. }
  112. ci = e->u.extra = buf;
  113. }
  114. /* Note: Do not share console info */
  115. return 1;
  116. }
  117. static int console_tcl_get(Tcl_Interp *irp, struct userrec *u,
  118. struct user_entry *e, int argc, char **argv)
  119. {
  120. char work[1024];
  121. struct console_info *i = e->u.extra;
  122. simple_sprintf(work, "%s %s %s %d %d %d",
  123. i->channel, masktype(i->conflags),
  124. stripmasktype(i->stripflags), i->echoflags,
  125. i->page, i->conchan);
  126. Tcl_AppendResult(irp, work, NULL);
  127. return TCL_OK;
  128. }
  129. static int console_tcl_set(Tcl_Interp *irp, struct userrec *u,
  130. struct user_entry *e, int argc, char **argv)
  131. {
  132. struct console_info *i = e->u.extra;
  133. int l;
  134. BADARGS(4, 9, " handle CONSOLE channel flags strip echo page conchan");
  135. if (!i) {
  136. i = user_malloc(sizeof(struct console_info));
  137. egg_bzero(i, sizeof(struct console_info));
  138. }
  139. if (i->channel)
  140. nfree(i->channel);
  141. l = strlen(argv[3]);
  142. if (l > 80)
  143. l = 80;
  144. i->channel = user_malloc(l + 1);
  145. strncpy(i->channel, argv[3], l);
  146. i->channel[l] = 0;
  147. if (argc > 4) {
  148. i->conflags = logmodes(argv[4]);
  149. if (argc > 5) {
  150. i->stripflags = stripmodes(argv[5]);
  151. if (argc > 6) {
  152. i->echoflags = (argv[6][0] == '1') ? 1 : 0;
  153. if (argc > 7) {
  154. i->page = atoi(argv[7]);
  155. if (argc > 8)
  156. i->conchan = atoi(argv[8]);
  157. }
  158. }
  159. }
  160. }
  161. set_user(&USERENTRY_CONSOLE, u, i);
  162. return TCL_OK;
  163. }
  164. static int console_expmem(struct user_entry *e)
  165. {
  166. struct console_info *i = e->u.extra;
  167. return sizeof(struct console_info) + strlen(i->channel) + 1;
  168. }
  169. static void console_display(int idx, struct user_entry *e)
  170. {
  171. struct console_info *i = e->u.extra;
  172. if (dcc[idx].user && (dcc[idx].user->flags & USER_MASTER)) {
  173. dprintf(idx, " %s\n", CONSOLE_SAVED_SETTINGS);
  174. dprintf(idx, " %s %s\n", CONSOLE_CHANNEL, i->channel);
  175. dprintf(idx, " %s %s, %s %s, %s %s\n", CONSOLE_FLAGS,
  176. masktype(i->conflags), CONSOLE_STRIPFLAGS,
  177. stripmasktype(i->stripflags), CONSOLE_ECHO,
  178. i->echoflags ? CONSOLE_YES : CONSOLE_NO);
  179. dprintf(idx, " %s %d, %s %s%d\n", CONSOLE_PAGE_SETTING, i->page,
  180. CONSOLE_CHANNEL2, (i->conchan < GLOBAL_CHANS) ? "" : "*",
  181. i->conchan % GLOBAL_CHANS);
  182. }
  183. }
  184. static int console_dupuser(struct userrec *new, struct userrec *old,
  185. struct user_entry *e)
  186. {
  187. struct console_info *i = e->u.extra, *j;
  188. j = user_malloc(sizeof(struct console_info));
  189. my_memcpy(j, i, sizeof(struct console_info));
  190. j->channel = user_malloc(strlen(i->channel) + 1);
  191. strcpy(j->channel, i->channel);
  192. return set_user(e->type, new, j);
  193. }
  194. static struct user_entry_type USERENTRY_CONSOLE =
  195. {
  196. NULL, /* always 0 ;) */
  197. NULL,
  198. console_dupuser,
  199. console_unpack,
  200. console_pack,
  201. console_write_userfile,
  202. console_kill,
  203. NULL,
  204. console_set,
  205. console_tcl_get,
  206. console_tcl_set,
  207. console_expmem,
  208. console_display,
  209. "CONSOLE"
  210. };
  211. static int console_chon(char *handle, int idx)
  212. {
  213. struct console_info *i = get_user(&USERENTRY_CONSOLE, dcc[idx].user);
  214. if (dcc[idx].type == &DCC_CHAT) {
  215. if (i) {
  216. if (i->channel && i->channel[0])
  217. strcpy(dcc[idx].u.chat->con_chan, i->channel);
  218. dcc[idx].u.chat->con_flags = i->conflags;
  219. dcc[idx].u.chat->strip_flags = i->stripflags;
  220. if (i->echoflags)
  221. dcc[idx].status |= STAT_ECHO;
  222. else
  223. dcc[idx].status &= ~STAT_ECHO;
  224. if (i->page) {
  225. dcc[idx].status |= STAT_PAGE;
  226. dcc[idx].u.chat->max_line = i->page;
  227. if (!dcc[idx].u.chat->line_count)
  228. dcc[idx].u.chat->current_lines = 0;
  229. }
  230. dcc[idx].u.chat->channel = i->conchan;
  231. } else if (force_channel > -1)
  232. dcc[idx].u.chat->channel = force_channel;
  233. if ((dcc[idx].u.chat->channel >= 0) &&
  234. (dcc[idx].u.chat->channel < GLOBAL_CHANS)) {
  235. botnet_send_join_idx(idx, -1);
  236. check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
  237. geticon(idx), dcc[idx].sock, dcc[idx].host);
  238. }
  239. if (info_party) {
  240. char *p = get_user(&USERENTRY_INFO, dcc[idx].user);
  241. if (p) {
  242. if (dcc[idx].u.chat->channel >= 0) {
  243. char x[1024];
  244. chanout_but(-1, dcc[idx].u.chat->channel,
  245. "*** [%s] %s\n", dcc[idx].nick, p);
  246. simple_sprintf(x, "[%s] %s", dcc[idx].nick, p);
  247. botnet_send_chan(-1, botnetnick, NULL,
  248. dcc[idx].u.chat->channel, x);
  249. }
  250. }
  251. }
  252. }
  253. return 0;
  254. }
  255. static int console_store(struct userrec *u, int idx, char *par)
  256. {
  257. struct console_info *i = get_user(&USERENTRY_CONSOLE, u);
  258. if (!i) {
  259. i = user_malloc(sizeof(struct console_info));
  260. egg_bzero(i, sizeof(struct console_info));
  261. }
  262. if (i->channel)
  263. nfree(i->channel);
  264. i->channel = user_malloc(strlen(dcc[idx].u.chat->con_chan) + 1);
  265. strcpy(i->channel, dcc[idx].u.chat->con_chan);
  266. i->conflags = dcc[idx].u.chat->con_flags;
  267. i->stripflags = dcc[idx].u.chat->strip_flags;
  268. i->echoflags = (dcc[idx].status & STAT_ECHO) ? 1 : 0;
  269. if (dcc[idx].status & STAT_PAGE)
  270. i->page = dcc[idx].u.chat->max_line;
  271. else
  272. i->page = 0;
  273. i->conchan = dcc[idx].u.chat->channel;
  274. if (par) {
  275. dprintf(idx, "%s\n", CONSOLE_SAVED_SETTINGS2);
  276. dprintf(idx, " %s %s\n", CONSOLE_CHANNEL, i->channel);
  277. dprintf(idx, " %s %s, %s %s, %s %s\n", CONSOLE_FLAGS,
  278. masktype(i->conflags), CONSOLE_STRIPFLAGS,
  279. stripmasktype(i->stripflags), CONSOLE_ECHO,
  280. i->echoflags ? CONSOLE_YES : CONSOLE_NO);
  281. dprintf(idx, " %s %d, %s %d\n", CONSOLE_PAGE_SETTING, i->page,
  282. CONSOLE_CHANNEL2, i->conchan);
  283. }
  284. set_user(&USERENTRY_CONSOLE, u, i);
  285. return 0;
  286. }
  287. /* cmds.c:cmd_console calls this, better than chof bind - drummer,07/25/1999 */
  288. static int console_dostore(int idx)
  289. {
  290. if (console_autosave)
  291. console_store(dcc[idx].user, idx, NULL);
  292. return 0;
  293. }
  294. static tcl_ints myints[] =
  295. {
  296. {"console-autosave", &console_autosave, 0},
  297. {"force-channel", &force_channel, 0},
  298. {"info-party", &info_party, 0},
  299. {NULL, NULL, 0}
  300. };
  301. static cmd_t mychon[] =
  302. {
  303. {"*", "", console_chon, "console:chon"},
  304. {NULL, NULL, NULL, NULL}
  305. };
  306. static cmd_t mydcc[] =
  307. {
  308. {"store", "", console_store, NULL},
  309. {NULL, NULL, NULL, NULL}
  310. };
  311. static char *console_close()
  312. {
  313. rem_builtins(H_chon, mychon);
  314. rem_builtins(H_dcc, mydcc);
  315. rem_tcl_ints(myints);
  316. rem_help_reference("console.help");
  317. del_entry_type(&USERENTRY_CONSOLE);
  318. del_lang_section("console");
  319. module_undepend(MODULE_NAME);
  320. return NULL;
  321. }
  322. EXPORT_SCOPE char *console_start();
  323. static Function console_table[] =
  324. {
  325. (Function) console_start,
  326. (Function) console_close,
  327. (Function) NULL,
  328. (Function) NULL,
  329. (Function) console_dostore,
  330. };
  331. char *console_start(Function * global_funcs)
  332. {
  333. global = global_funcs;
  334. module_register(MODULE_NAME, console_table, 1, 1);
  335. if (!module_depend(MODULE_NAME, "eggdrop", 106, 0)) {
  336. module_undepend(MODULE_NAME);
  337. return "This module requires Eggdrop 1.6.0 or later.";
  338. }
  339. add_builtins(H_chon, mychon);
  340. add_builtins(H_dcc, mydcc);
  341. add_tcl_ints(myints);
  342. add_help_reference("console.help");
  343. USERENTRY_CONSOLE.get = def_get;
  344. add_entry_type(&USERENTRY_CONSOLE);
  345. add_lang_section("console");
  346. return NULL;
  347. }