console.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * console.c -- part of console.mod
  3. * saved console settings based on console.tcl
  4. * by cmwagner/billyjoe/D. Senso
  5. *
  6. */
  7. #define MODULE_NAME "console"
  8. #define MAKING_CONSOLE
  9. #include "src/mod/module.h"
  10. #include <stdlib.h>
  11. #include "console.h"
  12. static Function *global = NULL;
  13. static int console_autosave = 1;
  14. static int force_channel = 0;
  15. static int info_party = 1;
  16. struct console_info {
  17. char *channel;
  18. int conflags;
  19. int stripflags;
  20. int echoflags;
  21. int page;
  22. int conchan;
  23. int colour;
  24. };
  25. static struct user_entry_type USERENTRY_CONSOLE;
  26. static int console_unpack(struct userrec *u, struct user_entry *e)
  27. {
  28. struct console_info *ci = user_malloc(sizeof(struct console_info));
  29. char *par, *arg;
  30. par = e->u.list->extra;
  31. arg = newsplit(&par);
  32. ci->channel = user_malloc(strlen(arg) + 1);
  33. strcpy(ci->channel, arg);
  34. arg = newsplit(&par);
  35. ci->conflags = logmodes(arg);
  36. arg = newsplit(&par);
  37. ci->stripflags = stripmodes(arg);
  38. arg = newsplit(&par);
  39. ci->echoflags = (arg[0] == '1') ? 1 : 0;
  40. arg = newsplit(&par);
  41. ci->page = atoi(arg);
  42. arg = newsplit(&par);
  43. ci->conchan = atoi(arg);
  44. arg = newsplit(&par);
  45. ci->colour = atoi(arg);
  46. list_type_kill(e->u.list);
  47. e->u.extra = ci;
  48. return 1;
  49. }
  50. static int console_pack(struct userrec *u, struct user_entry *e)
  51. {
  52. char work[1024];
  53. struct console_info *ci;
  54. int l;
  55. ci = (struct console_info *) e->u.extra;
  56. l = simple_sprintf(work, "%s %s %s %d %d %d %d",
  57. ci->channel, masktype(ci->conflags),
  58. stripmasktype(ci->stripflags), ci->echoflags,
  59. ci->page, ci->conchan, ci->colour);
  60. e->u.list = user_malloc(sizeof(struct list_type));
  61. e->u.list->next = NULL;
  62. e->u.list->extra = user_malloc(l + 1);
  63. strcpy(e->u.list->extra, work);
  64. nfree(ci->channel);
  65. nfree(ci);
  66. return 1;
  67. }
  68. static int console_kill(struct user_entry *e)
  69. {
  70. struct console_info *i = e->u.extra;
  71. nfree(i->channel);
  72. nfree(i);
  73. nfree(e);
  74. return 1;
  75. }
  76. static int console_write_userfile(FILE *f, struct userrec *u,
  77. struct user_entry *e)
  78. {
  79. struct console_info *i = e->u.extra;
  80. if (lfprintf(f, "--CONSOLE %s %s %s %d %d %d %d\n",
  81. i->channel, masktype(i->conflags),
  82. stripmasktype(i->stripflags), i->echoflags,
  83. i->page, i->conchan, i->colour) == EOF)
  84. return 0;
  85. return 1;
  86. }
  87. static int console_set(struct userrec *u, struct user_entry *e, void *buf)
  88. {
  89. struct console_info *ci = (struct console_info *) e->u.extra;
  90. if (!ci && !buf)
  91. return 1;
  92. if (ci != buf) {
  93. if (ci) {
  94. nfree(ci->channel);
  95. nfree(ci);
  96. }
  97. ci = e->u.extra = buf;
  98. }
  99. if (!noshare && !(u->flags & (USER_BOT | USER_UNSHARED))) {
  100. char string[501];
  101. egg_snprintf(string, sizeof string, "%s %s %s %d %d %d %d", ci->channel, masktype(ci->conflags),
  102. stripmasktype(ci->stripflags), ci->echoflags, ci->page, ci->conchan,
  103. ci->colour);
  104. /* shareout(NULL, "c %s %s %s\n", e->type->name, u->handle, string); */
  105. shareout(NULL, "c CONSOLE %s %s\n", u->handle, string);
  106. }
  107. return 1;
  108. }
  109. static int console_gotshare(struct userrec *u, struct user_entry *e, char *par, int idx)
  110. {
  111. struct console_info *ci = (struct console_info *) e->u.extra;
  112. char *arg;
  113. int i;
  114. arg = newsplit(&par);
  115. if (ci) {
  116. nfree(ci->channel);
  117. nfree(ci);
  118. }
  119. ci = user_malloc(sizeof(struct console_info));
  120. ci->channel = user_malloc(strlen(arg) + 1);
  121. strcpy(ci->channel, arg);
  122. arg = newsplit(&par);
  123. ci->conflags = logmodes(arg);
  124. arg = newsplit(&par);
  125. ci->stripflags = stripmodes(arg);
  126. arg = newsplit(&par);
  127. ci->echoflags = (arg[0] == '1') ? 1 : 0;
  128. arg = newsplit(&par);
  129. ci->page = atoi(arg);
  130. arg = newsplit(&par);
  131. ci->conchan = atoi(arg);
  132. arg = newsplit(&par);
  133. ci->colour = atoi(arg);
  134. e->u.extra = ci;
  135. /* now let's propogate to the dcc list */
  136. for (i = 0; i < dcc_total; i++) {
  137. if ((dcc[i].type == &DCC_CHAT) && !strcmp(dcc[i].user->handle, u->handle)) {
  138. if (ci->channel && ci->channel[0])
  139. strcpy(dcc[i].u.chat->con_chan, ci->channel);
  140. dcc[i].u.chat->con_flags = ci->conflags;
  141. dcc[i].u.chat->strip_flags = ci->stripflags;
  142. if (ci->echoflags)
  143. dcc[i].status |= STAT_ECHO;
  144. else
  145. dcc[i].status &= ~STAT_ECHO;
  146. if (ci->page) {
  147. dcc[i].status |= STAT_PAGE;
  148. dcc[i].u.chat->max_line = ci->page;
  149. if (!dcc[i].u.chat->line_count)
  150. dcc[i].u.chat->current_lines = 0;
  151. }
  152. if (ci->colour)
  153. dcc[i].status |= (STAT_COLOR);
  154. else
  155. dcc[i].status &= ~(STAT_COLOR);
  156. }
  157. }
  158. return 1;
  159. }
  160. static int console_tcl_get(Tcl_Interp *irp, struct userrec *u,
  161. struct user_entry *e, int argc, char **argv)
  162. {
  163. char work[1024];
  164. struct console_info *i = e->u.extra;
  165. simple_sprintf(work, "%s %s %s %d %d %d %d",
  166. i->channel, masktype(i->conflags),
  167. stripmasktype(i->stripflags), i->echoflags,
  168. i->page, i->conchan, i->colour);
  169. Tcl_AppendResult(irp, work, NULL);
  170. return TCL_OK;
  171. }
  172. static int console_tcl_set(Tcl_Interp *irp, struct userrec *u,
  173. struct user_entry *e, int argc, char **argv)
  174. {
  175. struct console_info *i = e->u.extra;
  176. int l;
  177. BADARGS(4, 9, " handle CONSOLE channel flags strip echo page conchan");
  178. if (!i) {
  179. i = user_malloc(sizeof(struct console_info));
  180. egg_bzero(i, sizeof(struct console_info));
  181. }
  182. if (i->channel)
  183. nfree(i->channel);
  184. l = strlen(argv[3]);
  185. if (l > 80)
  186. l = 80;
  187. i->channel = user_malloc(l + 1);
  188. strncpy(i->channel, argv[3], l);
  189. i->channel[l] = 0;
  190. if (argc > 4) {
  191. i->conflags = logmodes(argv[4]);
  192. if (argc > 5) {
  193. i->stripflags = stripmodes(argv[5]);
  194. if (argc > 6) {
  195. i->echoflags = (argv[6][0] == '1') ? 1 : 0;
  196. if (argc > 7) {
  197. i->page = atoi(argv[7]);
  198. if (argc > 8) {
  199. i->conchan = atoi(argv[8]);
  200. if (argc > 9)
  201. i->colour = atoi(argv[9]);
  202. }
  203. }
  204. }
  205. }
  206. }
  207. set_user(&USERENTRY_CONSOLE, u, i);
  208. return TCL_OK;
  209. }
  210. static int console_expmem(struct user_entry *e)
  211. {
  212. struct console_info *i = e->u.extra;
  213. return sizeof(struct console_info) + strlen(i->channel) + 1;
  214. }
  215. static void console_display(int idx, struct user_entry *e, struct userrec *u)
  216. {
  217. struct console_info *i = e->u.extra;
  218. char tmp[100];
  219. if (dcc[idx].user && (dcc[idx].user->flags & USER_MASTER)) {
  220. dprintf(idx, " %s\n", CONSOLE_SAVED_SETTINGS);
  221. dprintf(idx, " %s %s\n", CONSOLE_CHANNEL, i->channel);
  222. dprintf(idx, " %s %s, %s %s, %s %s\n", CONSOLE_FLAGS,
  223. masktype(i->conflags), CONSOLE_STRIPFLAGS,
  224. stripmasktype(i->stripflags), CONSOLE_ECHO,
  225. i->echoflags ? CONSOLE_YES : CONSOLE_NO);
  226. dprintf(idx, " %s %d, %s %s%d\n", CONSOLE_PAGE_SETTING, i->page,
  227. CONSOLE_CHANNEL2, (i->conchan < GLOBAL_CHANS) ? "" : "*",
  228. i->conchan % GLOBAL_CHANS);
  229. sprintf(tmp, " Color:");
  230. if (i->colour == 1)
  231. sprintf(tmp, "%s on", tmp);
  232. else
  233. sprintf(tmp, "%s off", tmp);
  234. dprintf(idx, "%s\n", tmp);
  235. }
  236. }
  237. static int console_dupuser(struct userrec *new, struct userrec *old,
  238. struct user_entry *e)
  239. {
  240. struct console_info *i = e->u.extra, *j;
  241. j = user_malloc(sizeof(struct console_info));
  242. my_memcpy(j, i, sizeof(struct console_info));
  243. j->channel = user_malloc(strlen(i->channel) + 1);
  244. strcpy(j->channel, i->channel);
  245. return set_user(e->type, new, j);
  246. }
  247. static struct user_entry_type USERENTRY_CONSOLE =
  248. {
  249. 0, /* always 0 ;) */
  250. console_gotshare,
  251. console_dupuser,
  252. console_unpack,
  253. console_pack,
  254. console_write_userfile,
  255. console_kill,
  256. NULL,
  257. console_set,
  258. console_tcl_get,
  259. console_tcl_set,
  260. console_expmem,
  261. console_display,
  262. "CONSOLE"
  263. };
  264. static int console_chon(char *handle, int idx)
  265. {
  266. struct console_info *i = get_user(&USERENTRY_CONSOLE, dcc[idx].user);
  267. if (dcc[idx].type == &DCC_CHAT) {
  268. if (i) {
  269. if (i->channel && i->channel[0])
  270. strcpy(dcc[idx].u.chat->con_chan, i->channel);
  271. dcc[idx].u.chat->con_flags = i->conflags;
  272. dcc[idx].u.chat->strip_flags = i->stripflags;
  273. if (i->echoflags)
  274. dcc[idx].status |= STAT_ECHO;
  275. else
  276. dcc[idx].status &= ~STAT_ECHO;
  277. if (i->page) {
  278. dcc[idx].status |= STAT_PAGE;
  279. dcc[idx].u.chat->max_line = i->page;
  280. if (!dcc[idx].u.chat->line_count)
  281. dcc[idx].u.chat->current_lines = 0;
  282. }
  283. if (i->colour)
  284. dcc[idx].status |= (STAT_COLOR);
  285. else
  286. dcc[idx].status &= ~(STAT_COLOR);
  287. }
  288. if ((dcc[idx].u.chat->channel >= 0) &&
  289. (dcc[idx].u.chat->channel < GLOBAL_CHANS)) {
  290. botnet_send_join_idx(idx, -1);
  291. check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
  292. geticon(idx), dcc[idx].sock, dcc[idx].host);
  293. }
  294. if (info_party) {
  295. char *p = get_user(&USERENTRY_INFO, dcc[idx].user);
  296. if (p) {
  297. if (dcc[idx].u.chat->channel >= 0) {
  298. char x[1024];
  299. chanout_but(-1, dcc[idx].u.chat->channel,
  300. "*** [%s] %s\n", dcc[idx].nick, p);
  301. simple_sprintf(x, "[%s] %s", dcc[idx].nick, p);
  302. botnet_send_chan(-1, botnetnick, NULL,
  303. dcc[idx].u.chat->channel, x);
  304. }
  305. }
  306. }
  307. }
  308. return 0;
  309. }
  310. static int console_store(struct userrec *u, int idx, char *par)
  311. {
  312. struct console_info *i = get_user(&USERENTRY_CONSOLE, u);
  313. if (!i) {
  314. i = user_malloc(sizeof(struct console_info));
  315. egg_bzero(i, sizeof(struct console_info));
  316. }
  317. if (i->channel)
  318. nfree(i->channel);
  319. i->channel = user_malloc(strlen(dcc[idx].u.chat->con_chan) + 1);
  320. strcpy(i->channel, dcc[idx].u.chat->con_chan);
  321. i->conflags = dcc[idx].u.chat->con_flags;
  322. i->stripflags = dcc[idx].u.chat->strip_flags;
  323. i->echoflags = (dcc[idx].status & STAT_ECHO) ? 1 : 0;
  324. if (dcc[idx].status & STAT_PAGE)
  325. i->page = dcc[idx].u.chat->max_line;
  326. else
  327. i->page = 0;
  328. if (dcc[idx].status & STAT_COLOR)
  329. i->colour = 1;
  330. else
  331. i->colour = 0;
  332. i->conchan = dcc[idx].u.chat->channel;
  333. if (par) {
  334. char tmp[100];
  335. dprintf(idx, "%s\n", CONSOLE_SAVED_SETTINGS2);
  336. dprintf(idx, " %s %s\n", CONSOLE_CHANNEL, i->channel);
  337. dprintf(idx, " %s %s, %s %s, %s %s\n", CONSOLE_FLAGS,
  338. masktype(i->conflags), CONSOLE_STRIPFLAGS,
  339. stripmasktype(i->stripflags), CONSOLE_ECHO,
  340. i->echoflags ? CONSOLE_YES : CONSOLE_NO);
  341. dprintf(idx, " %s %d, %s %d\n", CONSOLE_PAGE_SETTING, i->page,
  342. CONSOLE_CHANNEL2, i->conchan);
  343. sprintf(tmp, " Color:");
  344. if (i->colour == 1)
  345. sprintf(tmp, "%s on", tmp);
  346. else
  347. sprintf(tmp, "%s off", tmp);
  348. dprintf(idx, "%s\n", tmp);
  349. }
  350. set_user(&USERENTRY_CONSOLE, u, i);
  351. dprintf(idx, "Console setting stored.\n");
  352. #ifdef HUB
  353. write_userfile(idx);
  354. #endif /* HUB */
  355. return 0;
  356. }
  357. /* cmds.c:cmd_console calls this, better than chof bind - drummer,07/25/1999 */
  358. static int console_dostore(int idx)
  359. {
  360. if (console_autosave)
  361. console_store(dcc[idx].user, idx, NULL);
  362. return 0;
  363. }
  364. static tcl_ints myints[] =
  365. {
  366. {"console-autosave", &console_autosave, 0},
  367. {"force-channel", &force_channel, 0},
  368. {"info-party", &info_party, 0},
  369. {NULL, NULL, 0}
  370. };
  371. static cmd_t mychon[] =
  372. {
  373. {"*", "", console_chon, "console:chon"},
  374. {NULL, NULL, NULL, NULL}
  375. };
  376. static cmd_t mydcc[] =
  377. {
  378. {"store", "", console_store, NULL},
  379. {NULL, NULL, NULL, NULL}
  380. };
  381. EXPORT_SCOPE char *console_start();
  382. static Function console_table[] =
  383. {
  384. (Function) console_start,
  385. (Function) NULL,
  386. (Function) NULL,
  387. (Function) NULL,
  388. (Function) console_dostore,
  389. };
  390. char *console_start(Function * global_funcs)
  391. {
  392. global = global_funcs;
  393. module_register(MODULE_NAME, console_table, 1, 1);
  394. add_builtins(H_chon, mychon);
  395. add_builtins(H_dcc, mydcc);
  396. add_tcl_ints(myints);
  397. USERENTRY_CONSOLE.get = def_get;
  398. add_entry_type(&USERENTRY_CONSOLE);
  399. return NULL;
  400. }