console.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. #undef MAKING_MODS
  8. #include "console.h"
  9. #include "src/common.h"
  10. #include "src/tclhash.h"
  11. #include "src/tandem.h"
  12. #include "src/cmds.h"
  13. #include "src/users.h"
  14. #include "src/userent.h"
  15. #include "src/botmsg.h"
  16. #include "src/userrec.h"
  17. #include "src/users.h"
  18. #include "src/misc.h"
  19. #include "src/core_binds.h"
  20. extern int noshare;
  21. extern char botnetnick[];
  22. static int console_autosave = 1;
  23. static int info_party = 1;
  24. struct console_info {
  25. char *channel;
  26. int conflags;
  27. int stripflags;
  28. int echoflags;
  29. int page;
  30. int conchan;
  31. int colour;
  32. };
  33. static struct user_entry_type USERENTRY_CONSOLE;
  34. static int console_unpack(struct userrec *u, struct user_entry *e)
  35. {
  36. struct console_info *ci = malloc(sizeof(struct console_info));
  37. char *par, *arg;
  38. par = e->u.list->extra;
  39. arg = newsplit(&par);
  40. ci->channel = strdup(arg);
  41. arg = newsplit(&par);
  42. ci->conflags = logmodes(arg);
  43. arg = newsplit(&par);
  44. ci->stripflags = stripmodes(arg);
  45. arg = newsplit(&par);
  46. ci->echoflags = (arg[0] == '1') ? 1 : 0;
  47. arg = newsplit(&par);
  48. ci->page = atoi(arg);
  49. arg = newsplit(&par);
  50. ci->conchan = atoi(arg);
  51. arg = newsplit(&par);
  52. ci->colour = atoi(arg);
  53. list_type_kill(e->u.list);
  54. e->u.extra = ci;
  55. return 1;
  56. }
  57. static int console_pack(struct userrec *u, struct user_entry *e)
  58. {
  59. char work[1024];
  60. struct console_info *ci;
  61. int l;
  62. ci = (struct console_info *) e->u.extra;
  63. l = simple_sprintf(work, "%s %s %s %d %d %d %d",
  64. ci->channel, masktype(ci->conflags),
  65. stripmasktype(ci->stripflags), ci->echoflags,
  66. ci->page, ci->conchan, ci->colour);
  67. e->u.list = malloc(sizeof(struct list_type));
  68. e->u.list->next = NULL;
  69. e->u.list->extra = malloc(l + 1);
  70. strcpy(e->u.list->extra, work);
  71. free(ci->channel);
  72. free(ci);
  73. return 1;
  74. }
  75. static int console_kill(struct user_entry *e)
  76. {
  77. struct console_info *i = e->u.extra;
  78. free(i->channel);
  79. free(i);
  80. free(e);
  81. return 1;
  82. }
  83. static int console_write_userfile(FILE *f, struct userrec *u,
  84. struct user_entry *e)
  85. {
  86. struct console_info *i = e->u.extra;
  87. if (lfprintf(f, "--CONSOLE %s %s %s %d %d %d %d\n",
  88. i->channel, masktype(i->conflags),
  89. stripmasktype(i->stripflags), i->echoflags,
  90. i->page, i->conchan, i->colour) == EOF)
  91. return 0;
  92. return 1;
  93. }
  94. static int console_set(struct userrec *u, struct user_entry *e, void *buf)
  95. {
  96. struct console_info *ci = (struct console_info *) e->u.extra;
  97. if (!ci && !buf)
  98. return 1;
  99. if (ci != buf) {
  100. if (ci) {
  101. free(ci->channel);
  102. free(ci);
  103. }
  104. ci = e->u.extra = buf;
  105. }
  106. if (!noshare && !(u->flags & (USER_BOT | USER_UNSHARED))) {
  107. char string[501];
  108. egg_snprintf(string, sizeof string, "%s %s %s %d %d %d %d", ci->channel, masktype(ci->conflags),
  109. stripmasktype(ci->stripflags), ci->echoflags, ci->page, ci->conchan,
  110. ci->colour);
  111. /* shareout(NULL, "c %s %s %s\n", e->type->name, u->handle, string); */
  112. shareout(NULL, "c CONSOLE %s %s\n", u->handle, string);
  113. }
  114. return 1;
  115. }
  116. static int console_gotshare(struct userrec *u, struct user_entry *e, char *par, int idx)
  117. {
  118. struct console_info *ci = (struct console_info *) e->u.extra;
  119. char *arg;
  120. int i;
  121. arg = newsplit(&par);
  122. if (ci) {
  123. free(ci->channel);
  124. free(ci);
  125. }
  126. ci = malloc(sizeof(struct console_info));
  127. ci->channel = strdup(arg);
  128. arg = newsplit(&par);
  129. ci->conflags = logmodes(arg);
  130. arg = newsplit(&par);
  131. ci->stripflags = stripmodes(arg);
  132. arg = newsplit(&par);
  133. ci->echoflags = (arg[0] == '1') ? 1 : 0;
  134. arg = newsplit(&par);
  135. ci->page = atoi(arg);
  136. arg = newsplit(&par);
  137. ci->conchan = atoi(arg);
  138. arg = newsplit(&par);
  139. ci->colour = atoi(arg);
  140. e->u.extra = ci;
  141. /* now let's propogate to the dcc list */
  142. for (i = 0; i < dcc_total; i++) {
  143. if ((dcc[i].type == &DCC_CHAT) && !strcmp(dcc[i].user->handle, u->handle)) {
  144. if (ci->channel && ci->channel[0])
  145. strcpy(dcc[i].u.chat->con_chan, ci->channel);
  146. dcc[i].u.chat->con_flags = ci->conflags;
  147. dcc[i].u.chat->strip_flags = ci->stripflags;
  148. if (ci->echoflags)
  149. dcc[i].status |= STAT_ECHO;
  150. else
  151. dcc[i].status &= ~STAT_ECHO;
  152. if (ci->page) {
  153. dcc[i].status |= STAT_PAGE;
  154. dcc[i].u.chat->max_line = ci->page;
  155. if (!dcc[i].u.chat->line_count)
  156. dcc[i].u.chat->current_lines = 0;
  157. }
  158. if (ci->colour)
  159. dcc[i].status |= (STAT_COLOR);
  160. else
  161. dcc[i].status &= ~(STAT_COLOR);
  162. }
  163. }
  164. return 1;
  165. }
  166. static void console_display(int idx, struct user_entry *e, struct userrec *u)
  167. {
  168. struct console_info *i = e->u.extra;
  169. char tmp[100];
  170. if (dcc[idx].user && (dcc[idx].user->flags & USER_MASTER)) {
  171. dprintf(idx, " %s\n", CONSOLE_SAVED_SETTINGS);
  172. dprintf(idx, " %s %s\n", CONSOLE_CHANNEL, i->channel);
  173. dprintf(idx, " %s %s, %s %s, %s %s\n", CONSOLE_FLAGS,
  174. masktype(i->conflags), CONSOLE_STRIPFLAGS,
  175. stripmasktype(i->stripflags), CONSOLE_ECHO,
  176. i->echoflags ? CONSOLE_YES : CONSOLE_NO);
  177. dprintf(idx, " %s %d, %s %s%d\n", CONSOLE_PAGE_SETTING, i->page,
  178. CONSOLE_CHANNEL2, (i->conchan < GLOBAL_CHANS) ? "" : "*",
  179. i->conchan % GLOBAL_CHANS);
  180. sprintf(tmp, " Color:");
  181. if (i->colour == 1)
  182. sprintf(tmp, "%s on", tmp);
  183. else
  184. sprintf(tmp, "%s off", tmp);
  185. dprintf(idx, "%s\n", tmp);
  186. }
  187. }
  188. static int console_dupuser(struct userrec *new, struct userrec *old,
  189. struct user_entry *e)
  190. {
  191. struct console_info *i = e->u.extra, *j;
  192. j = malloc(sizeof(struct console_info));
  193. egg_memcpy(j, i, sizeof(struct console_info));
  194. j->channel = strdup(i->channel);
  195. return set_user(e->type, new, j);
  196. }
  197. static struct user_entry_type USERENTRY_CONSOLE =
  198. {
  199. 0, /* always 0 ;) */
  200. console_gotshare,
  201. console_dupuser,
  202. console_unpack,
  203. console_pack,
  204. console_write_userfile,
  205. console_kill,
  206. NULL,
  207. console_set,
  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. if (i->colour)
  231. dcc[idx].status |= (STAT_COLOR);
  232. else
  233. dcc[idx].status &= ~(STAT_COLOR);
  234. }
  235. if ((dcc[idx].u.chat->channel >= 0) &&
  236. (dcc[idx].u.chat->channel < GLOBAL_CHANS)) {
  237. botnet_send_join_idx(idx, -1);
  238. check_bind_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
  239. geticon(idx), dcc[idx].sock, dcc[idx].host);
  240. }
  241. if (info_party) {
  242. char *p = get_user(&USERENTRY_INFO, dcc[idx].user);
  243. if (p) {
  244. if (dcc[idx].u.chat->channel >= 0) {
  245. char x[1024];
  246. chanout_but(-1, dcc[idx].u.chat->channel,
  247. "*** [%s] %s\n", dcc[idx].nick, p);
  248. simple_sprintf(x, "[%s] %s", dcc[idx].nick, p);
  249. botnet_send_chan(-1, botnetnick, NULL,
  250. dcc[idx].u.chat->channel, x);
  251. }
  252. }
  253. }
  254. }
  255. return 0;
  256. }
  257. static int console_store(struct userrec *u, int idx, char *par)
  258. {
  259. struct console_info *i = get_user(&USERENTRY_CONSOLE, u);
  260. if (!i) {
  261. i = malloc(sizeof(struct console_info));
  262. egg_bzero(i, sizeof(struct console_info));
  263. }
  264. if (i->channel)
  265. free(i->channel);
  266. i->channel = strdup(dcc[idx].u.chat->con_chan);
  267. i->conflags = dcc[idx].u.chat->con_flags;
  268. i->stripflags = dcc[idx].u.chat->strip_flags;
  269. i->echoflags = (dcc[idx].status & STAT_ECHO) ? 1 : 0;
  270. if (dcc[idx].status & STAT_PAGE)
  271. i->page = dcc[idx].u.chat->max_line;
  272. else
  273. i->page = 0;
  274. if (dcc[idx].status & STAT_COLOR)
  275. i->colour = 1;
  276. else
  277. i->colour = 0;
  278. i->conchan = dcc[idx].u.chat->channel;
  279. if (par) {
  280. char tmp[100];
  281. dprintf(idx, "%s\n", CONSOLE_SAVED_SETTINGS2);
  282. dprintf(idx, " %s %s\n", CONSOLE_CHANNEL, i->channel);
  283. dprintf(idx, " %s %s, %s %s, %s %s\n", CONSOLE_FLAGS,
  284. masktype(i->conflags), CONSOLE_STRIPFLAGS,
  285. stripmasktype(i->stripflags), CONSOLE_ECHO,
  286. i->echoflags ? CONSOLE_YES : CONSOLE_NO);
  287. dprintf(idx, " %s %d, %s %d\n", CONSOLE_PAGE_SETTING, i->page,
  288. CONSOLE_CHANNEL2, i->conchan);
  289. sprintf(tmp, " Color:");
  290. if (i->colour == 1)
  291. sprintf(tmp, "%s on", tmp);
  292. else
  293. sprintf(tmp, "%s off", tmp);
  294. dprintf(idx, "%s\n", tmp);
  295. }
  296. set_user(&USERENTRY_CONSOLE, u, i);
  297. dprintf(idx, "Console setting stored.\n");
  298. #ifdef HUB
  299. write_userfile(idx);
  300. #endif /* HUB */
  301. return 0;
  302. }
  303. /* cmds.c:cmd_console calls this, better than chof bind - drummer,07/25/1999 */
  304. int console_dostore(int idx)
  305. {
  306. if (console_autosave)
  307. console_store(dcc[idx].user, idx, NULL);
  308. return 0;
  309. }
  310. static cmd_t mychon[] =
  311. {
  312. {"*", "", console_chon, "console:chon"},
  313. {NULL, NULL, NULL, NULL}
  314. };
  315. static cmd_t mydcc[] =
  316. {
  317. {"store", "", console_store, NULL},
  318. {NULL, NULL, NULL, NULL}
  319. };
  320. void console_init()
  321. {
  322. add_builtins("dcc", mydcc);
  323. add_builtins("chon", mychon);
  324. USERENTRY_CONSOLE.get = def_get;
  325. add_entry_type(&USERENTRY_CONSOLE);
  326. }