console.c 9.3 KB

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