console.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. struct console_info {
  21. char *channel;
  22. int conflags;
  23. int stripflags;
  24. int echoflags;
  25. int page;
  26. int conchan;
  27. int color;
  28. int banner;
  29. int channels;
  30. int bots;
  31. int whom;
  32. };
  33. static bool
  34. console_unpack(struct userrec *u, struct user_entry *e)
  35. {
  36. struct console_info *ci = (struct console_info *) my_calloc(1, sizeof(struct console_info));
  37. char *par = e->u.list->extra, *arg = NULL;
  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->color = atoi(arg);
  52. arg = newsplit(&par);
  53. ci->banner = atoi(arg);
  54. arg = newsplit(&par);
  55. ci->channels = atoi(arg);
  56. arg = newsplit(&par);
  57. ci->bots = atoi(arg);
  58. arg = newsplit(&par);
  59. ci->whom = atoi(arg);
  60. list_type_kill(e->u.list);
  61. e->u.extra = ci;
  62. return 1;
  63. }
  64. static bool
  65. console_kill(struct user_entry *e)
  66. {
  67. struct console_info *i = (struct console_info *) e->u.extra;
  68. free(i->channel);
  69. free(i);
  70. free(e);
  71. return 1;
  72. }
  73. static bool
  74. console_write_userfile(FILE * f, struct userrec *u, struct user_entry *e, int idx)
  75. {
  76. if (u->bot)
  77. return 1;
  78. struct console_info *i = (struct console_info *) e->u.extra;
  79. if (lfprintf(f, "--CONSOLE %s %s %s %d %d %d %d %d %d %d %d\n",
  80. i->channel, masktype(i->conflags),
  81. stripmasktype(i->stripflags), i->echoflags,
  82. i->page, i->conchan, i->color, i->banner, i->channels, i->bots, i->whom) == EOF)
  83. return 0;
  84. return 1;
  85. }
  86. static bool
  87. 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. free(ci->channel);
  95. free(ci);
  96. }
  97. ci = (struct console_info *) buf;
  98. e->u.extra = (struct console_info *) buf;
  99. }
  100. if (!noshare && !u->bot) {
  101. char string[501] = "";
  102. egg_snprintf(string, sizeof string, "%s %s %s %d %d %d %d %d %d %d %d", ci->channel,
  103. masktype(ci->conflags), stripmasktype(ci->stripflags), ci->echoflags, ci->page, ci->conchan,
  104. ci->color, ci->banner, ci->channels, ci->bots, ci->whom);
  105. /* shareout("c %s %s %s\n", e->type->name, u->handle, string); */
  106. shareout("c CONSOLE %s %s\n", u->handle, string);
  107. }
  108. return 1;
  109. }
  110. static bool
  111. console_gotshare(struct userrec *u, struct user_entry *e, char *par, int idx)
  112. {
  113. struct console_info *ci = (struct console_info *) e->u.extra;
  114. char *arg = NULL;
  115. arg = newsplit(&par);
  116. if (ci) {
  117. free(ci->channel);
  118. free(ci);
  119. }
  120. ci = (struct console_info *) my_calloc(1, sizeof(struct console_info));
  121. ci->channel = strdup(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->color = atoi(arg);
  134. arg = newsplit(&par);
  135. ci->banner = atoi(arg);
  136. arg = newsplit(&par);
  137. ci->channels = atoi(arg);
  138. arg = newsplit(&par);
  139. ci->bots = atoi(arg);
  140. arg = newsplit(&par);
  141. ci->whom = atoi(arg);
  142. e->u.extra = ci;
  143. /* now let's propogate to the dcc list */
  144. for (int i = 0; i < dcc_total; i++) {
  145. if (dcc[i].type && (dcc[i].type == &DCC_CHAT) && !strcmp(dcc[i].user->handle, u->handle)) {
  146. if (ci->channel && ci->channel[0])
  147. strcpy(dcc[i].u.chat->con_chan, ci->channel);
  148. dcc[i].u.chat->con_flags = ci->conflags;
  149. dcc[i].u.chat->strip_flags = ci->stripflags;
  150. if (ci->echoflags)
  151. dcc[i].status |= STAT_ECHO;
  152. else
  153. dcc[i].status &= ~STAT_ECHO;
  154. if (ci->page) {
  155. dcc[i].status |= STAT_PAGE;
  156. dcc[i].u.chat->max_line = ci->page;
  157. if (!dcc[i].u.chat->line_count)
  158. dcc[i].u.chat->current_lines = 0;
  159. }
  160. if (ci->color)
  161. dcc[i].status |= STAT_COLOR;
  162. else
  163. dcc[i].status &= ~STAT_COLOR;
  164. if (ci->banner)
  165. dcc[i].status |= STAT_BANNER;
  166. else
  167. dcc[i].status &= ~STAT_BANNER;
  168. if (ci->channels)
  169. dcc[i].status |= STAT_CHANNELS;
  170. else
  171. dcc[i].status &= ~STAT_CHANNELS;
  172. if (ci->bots)
  173. dcc[i].status |= STAT_BOTS;
  174. else
  175. dcc[i].status &= ~STAT_BOTS;
  176. if (ci->whom)
  177. dcc[i].status |= STAT_WHOM;
  178. else
  179. dcc[i].status &= ~STAT_WHOM;
  180. }
  181. }
  182. return 1;
  183. }
  184. static void
  185. console_display(int idx, struct user_entry *e, struct userrec *u)
  186. {
  187. struct console_info *i = (struct console_info *) e->u.extra;
  188. if (dcc[idx].user && (dcc[idx].user->flags & USER_MASTER)) {
  189. dprintf(idx, " %s\n", CONSOLE_SAVED_SETTINGS);
  190. dprintf(idx, " %s %s\n", CONSOLE_CHANNEL, i->channel);
  191. dprintf(idx, " %s %s, %s %s, %s %s\n", CONSOLE_FLAGS,
  192. masktype(i->conflags), CONSOLE_STRIPFLAGS,
  193. stripmasktype(i->stripflags), CONSOLE_ECHO, i->echoflags ? CONSOLE_YES : CONSOLE_NO);
  194. dprintf(idx, " %s %d, %s %s%d\n", CONSOLE_PAGE_SETTING, i->page,
  195. CONSOLE_CHANNEL2, (i->conchan < GLOBAL_CHANS) ? "" : "*", i->conchan % GLOBAL_CHANS);
  196. dprintf(idx, " Color: $b%s$b\n", i->color ? "on" : "off");
  197. dprintf(idx, " Login settings:\n");
  198. dprintf(idx, " Banner: $b%-3s$b Bots: $b%-3s$b\n", i->banner ? "on" : "off", i->bots ? "on" : "off");
  199. dprintf(idx, " Channels: $b%-3s$b Whom: $b%-3s$b\n", i->channels ? "on" : "off", i->whom ? "on" : "off");
  200. }
  201. }
  202. static struct user_entry_type USERENTRY_CONSOLE = {
  203. 0, /* always 0 ;) */
  204. console_gotshare,
  205. console_unpack,
  206. console_write_userfile,
  207. console_kill,
  208. def_get,
  209. console_set,
  210. console_display,
  211. "CONSOLE"
  212. };
  213. static int
  214. console_chon(char *handle, int idx)
  215. {
  216. if (dcc[idx].type == &DCC_CHAT) {
  217. struct console_info *i = (struct console_info *) get_user(&USERENTRY_CONSOLE, dcc[idx].user);
  218. if (i) {
  219. if (i->channel && i->channel[0])
  220. strcpy(dcc[idx].u.chat->con_chan, i->channel);
  221. dcc[idx].u.chat->con_flags = i->conflags;
  222. dcc[idx].u.chat->strip_flags = i->stripflags;
  223. if (i->echoflags)
  224. dcc[idx].status |= STAT_ECHO;
  225. else
  226. dcc[idx].status &= ~STAT_ECHO;
  227. if (i->page) {
  228. dcc[idx].status |= STAT_PAGE;
  229. dcc[idx].u.chat->max_line = i->page;
  230. if (!dcc[idx].u.chat->line_count)
  231. dcc[idx].u.chat->current_lines = 0;
  232. }
  233. if (i->color)
  234. dcc[idx].status |= STAT_COLOR;
  235. else
  236. dcc[idx].status &= ~STAT_COLOR;
  237. if (i->banner)
  238. dcc[idx].status |= STAT_BANNER;
  239. else
  240. dcc[idx].status &= ~STAT_BANNER;
  241. if (i->channels)
  242. dcc[idx].status |= STAT_CHANNELS;
  243. else
  244. dcc[idx].status &= ~STAT_CHANNELS;
  245. if (i->bots)
  246. dcc[idx].status |= STAT_BOTS;
  247. else
  248. dcc[idx].status &= ~STAT_BOTS;
  249. if (i->whom)
  250. dcc[idx].status |= STAT_WHOM;
  251. else
  252. dcc[idx].status &= ~STAT_WHOM;
  253. }
  254. if ((dcc[idx].u.chat->channel >= 0) && (dcc[idx].u.chat->channel < GLOBAL_CHANS)) {
  255. botnet_send_join_idx(idx);
  256. }
  257. }
  258. return 0;
  259. }
  260. static int
  261. console_store(int idx, char *par, bool displaySave)
  262. {
  263. struct console_info *i = (struct console_info *) get_user(&USERENTRY_CONSOLE, dcc[idx].user);
  264. if (!i)
  265. i = (struct console_info *) my_calloc(1, sizeof(struct console_info));
  266. if (i->channel)
  267. free(i->channel);
  268. i->channel = strdup(dcc[idx].u.chat->con_chan);
  269. i->conflags = dcc[idx].u.chat->con_flags;
  270. i->stripflags = dcc[idx].u.chat->strip_flags;
  271. i->echoflags = (dcc[idx].status & STAT_ECHO) ? 1 : 0;
  272. if (dcc[idx].status & STAT_PAGE)
  273. i->page = dcc[idx].u.chat->max_line;
  274. else
  275. i->page = 0;
  276. if (dcc[idx].status & STAT_COLOR)
  277. i->color = 1;
  278. else
  279. i->color = 0;
  280. if (dcc[idx].status & STAT_BANNER)
  281. i->banner = 1;
  282. else
  283. i->banner = 0;
  284. if (dcc[idx].status & STAT_CHANNELS)
  285. i->channels = 1;
  286. else
  287. i->channels = 0;
  288. if (dcc[idx].status & STAT_BOTS)
  289. i->bots = 1;
  290. else
  291. i->bots = 0;
  292. if (dcc[idx].status & STAT_WHOM)
  293. i->whom = 1;
  294. else
  295. i->whom = 0;
  296. i->conchan = dcc[idx].u.chat->channel;
  297. if (par) {
  298. dprintf(idx, "%s\n", CONSOLE_SAVED_SETTINGS2);
  299. dprintf(idx, " %s %s\n", CONSOLE_CHANNEL, i->channel);
  300. dprintf(idx, " %s %s, %s %s, %s %s\n", CONSOLE_FLAGS,
  301. masktype(i->conflags), CONSOLE_STRIPFLAGS,
  302. stripmasktype(i->stripflags), CONSOLE_ECHO, i->echoflags ? CONSOLE_YES : CONSOLE_NO);
  303. dprintf(idx, " %s %d, %s %d\n", CONSOLE_PAGE_SETTING, i->page, CONSOLE_CHANNEL2, i->conchan);
  304. dprintf(idx, " Color: $b%s$b\n", i->color ? "on" : "off");
  305. dprintf(idx, " Login settings:\n");
  306. dprintf(idx, " Login settings:\n");
  307. dprintf(idx, " Banner: $b%-3s$b Bots: $b%-3s$b\n", i->banner ? "on" : "off", i->bots ? "on" : "off");
  308. dprintf(idx, " Channels: $b%-3s$b Whom: $b%-3s$b\n", i->channels ? "on" : "off", i->whom ? "on" : "off");
  309. }
  310. set_user(&USERENTRY_CONSOLE, dcc[idx].user, i);
  311. dprintf(idx, "Console setting stored.\n");
  312. if (conf.bot->hub)
  313. write_userfile(displaySave ? idx : -1);
  314. return 0;
  315. }
  316. /* cmds.c:cmd_console calls this, better than chof bind - drummer,07/25/1999 */
  317. void
  318. console_dostore(int idx, bool displaySave)
  319. {
  320. console_store(idx, NULL, displaySave);
  321. return;
  322. }
  323. static cmd_t mychon[] = {
  324. {"*", "", (Function) console_chon, "console:chon", 0},
  325. {NULL, NULL, NULL, NULL, 0}
  326. };
  327. static cmd_t mydcc[] = {
  328. {"store", "", (Function) console_store, NULL, 0},
  329. {NULL, NULL, NULL, NULL, 0}
  330. };
  331. void
  332. console_init()
  333. {
  334. add_builtins("dcc", mydcc);
  335. add_builtins("chon", mychon);
  336. add_entry_type(&USERENTRY_CONSOLE);
  337. }