console.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2010 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * console.c -- part of console.mod
  22. * saved console settings based on console.tcl
  23. * by cmwagner/billyjoe/D. Senso
  24. *
  25. */
  26. #include "console.h"
  27. #include "src/common.h"
  28. #include "src/mod/share.mod/share.h"
  29. #include "src/binds.h"
  30. #include "src/tandem.h"
  31. #include "src/cmds.h"
  32. #include "src/users.h"
  33. #include "src/userent.h"
  34. #include "src/botmsg.h"
  35. #include "src/userrec.h"
  36. #include "src/users.h"
  37. #include "src/misc.h"
  38. #include "src/core_binds.h"
  39. #include <bdlib/src/Stream.h>
  40. #include <bdlib/src/String.h>
  41. struct console_info {
  42. char *channel;
  43. int conflags;
  44. int stripflags;
  45. int echoflags;
  46. int page;
  47. int conchan;
  48. int color;
  49. int banner;
  50. int channels;
  51. int bots;
  52. int whom;
  53. };
  54. static bool
  55. console_unpack(struct userrec *u, struct user_entry *e)
  56. {
  57. struct console_info *ci = (struct console_info *) my_calloc(1, sizeof(struct console_info));
  58. char *par = e->u.list->extra, *arg = NULL;
  59. arg = newsplit(&par);
  60. ci->channel = strdup(arg);
  61. arg = newsplit(&par);
  62. ci->conflags = logmodes(arg);
  63. arg = newsplit(&par);
  64. ci->stripflags = stripmodes(arg);
  65. arg = newsplit(&par);
  66. ci->echoflags = (arg[0] == '1') ? 1 : 0;
  67. arg = newsplit(&par);
  68. ci->page = atoi(arg);
  69. arg = newsplit(&par);
  70. ci->conchan = atoi(arg);
  71. arg = newsplit(&par);
  72. ci->color = atoi(arg);
  73. arg = newsplit(&par);
  74. ci->banner = atoi(arg);
  75. arg = newsplit(&par);
  76. ci->channels = atoi(arg);
  77. arg = newsplit(&par);
  78. ci->bots = atoi(arg);
  79. arg = newsplit(&par);
  80. ci->whom = atoi(arg);
  81. list_type_kill(e->u.list);
  82. e->u.extra = ci;
  83. return 1;
  84. }
  85. static bool
  86. console_kill(struct user_entry *e)
  87. {
  88. struct console_info *i = (struct console_info *) e->u.extra;
  89. free(i->channel);
  90. free(i);
  91. free(e);
  92. return 1;
  93. }
  94. static void
  95. console_write_userfile(bd::Stream& stream, const struct userrec *u, const struct user_entry *e, int idx)
  96. {
  97. if (u->bot)
  98. return;
  99. struct console_info *i = (struct console_info *) e->u.extra;
  100. stream << bd::String::printf("--CONSOLE %s %s %s %d %d %d %d %d %d %d %d\n",
  101. i->channel, masktype(i->conflags),
  102. stripmasktype(i->stripflags), i->echoflags,
  103. i->page, i->conchan, i->color, i->banner, i->channels, i->bots, i->whom);
  104. }
  105. static bool
  106. console_set(struct userrec *u, struct user_entry *e, void *buf)
  107. {
  108. struct console_info *ci = (struct console_info *) e->u.extra;
  109. if (!ci && !buf)
  110. return 1;
  111. if (ci != buf) {
  112. if (ci) {
  113. free(ci->channel);
  114. free(ci);
  115. }
  116. ci = (struct console_info *) buf;
  117. e->u.extra = (struct console_info *) buf;
  118. }
  119. if (!noshare && !u->bot) {
  120. char string[501] = "";
  121. simple_snprintf(string, sizeof string, "%s %s %s %d %d %d %d %d %d %d %d", ci->channel,
  122. masktype(ci->conflags), stripmasktype(ci->stripflags), ci->echoflags, ci->page, ci->conchan,
  123. ci->color, ci->banner, ci->channels, ci->bots, ci->whom);
  124. /* shareout("c %s %s %s\n", e->type->name, u->handle, string); */
  125. shareout("c CONSOLE %s %s\n", u->handle, string);
  126. }
  127. return 1;
  128. }
  129. static bool
  130. console_gotshare(struct userrec *u, struct user_entry *e, char *par, int idx)
  131. {
  132. struct console_info *ci = (struct console_info *) e->u.extra;
  133. char *arg = NULL;
  134. arg = newsplit(&par);
  135. if (ci) {
  136. free(ci->channel);
  137. free(ci);
  138. }
  139. ci = (struct console_info *) my_calloc(1, sizeof(struct console_info));
  140. ci->channel = strdup(arg);
  141. arg = newsplit(&par);
  142. ci->conflags = logmodes(arg);
  143. arg = newsplit(&par);
  144. ci->stripflags = stripmodes(arg);
  145. arg = newsplit(&par);
  146. ci->echoflags = (arg[0] == '1') ? 1 : 0;
  147. arg = newsplit(&par);
  148. ci->page = atoi(arg);
  149. arg = newsplit(&par);
  150. ci->conchan = atoi(arg);
  151. arg = newsplit(&par);
  152. ci->color = atoi(arg);
  153. arg = newsplit(&par);
  154. ci->banner = atoi(arg);
  155. arg = newsplit(&par);
  156. ci->channels = atoi(arg);
  157. arg = newsplit(&par);
  158. ci->bots = atoi(arg);
  159. arg = newsplit(&par);
  160. ci->whom = atoi(arg);
  161. e->u.extra = ci;
  162. struct chat_info dummy;
  163. /* now let's propogate to the dcc list */
  164. for (int i = 0; i < dcc_total; i++) {
  165. if (dcc[i].type && (dcc[i].type == &DCC_CHAT) && !strcmp(dcc[i].user->handle, u->handle)) {
  166. if (ci->channel && ci->channel[0])
  167. strlcpy(dcc[i].u.chat->con_chan, ci->channel, sizeof(dummy.con_chan));
  168. dcc[i].u.chat->con_flags = ci->conflags;
  169. dcc[i].u.chat->strip_flags = ci->stripflags;
  170. if (ci->echoflags)
  171. dcc[i].status |= STAT_ECHO;
  172. else
  173. dcc[i].status &= ~STAT_ECHO;
  174. if (ci->page) {
  175. dcc[i].status |= STAT_PAGE;
  176. dcc[i].u.chat->max_line = ci->page;
  177. if (!dcc[i].u.chat->line_count)
  178. dcc[i].u.chat->current_lines = 0;
  179. }
  180. if (ci->color)
  181. dcc[i].status |= STAT_COLOR;
  182. else
  183. dcc[i].status &= ~STAT_COLOR;
  184. if (ci->banner)
  185. dcc[i].status |= STAT_BANNER;
  186. else
  187. dcc[i].status &= ~STAT_BANNER;
  188. if (ci->channels)
  189. dcc[i].status |= STAT_CHANNELS;
  190. else
  191. dcc[i].status &= ~STAT_CHANNELS;
  192. if (ci->bots)
  193. dcc[i].status |= STAT_BOTS;
  194. else
  195. dcc[i].status &= ~STAT_BOTS;
  196. if (ci->whom)
  197. dcc[i].status |= STAT_WHOM;
  198. else
  199. dcc[i].status &= ~STAT_WHOM;
  200. }
  201. }
  202. return 1;
  203. }
  204. static void
  205. console_display(int idx, struct user_entry *e, struct userrec *u)
  206. {
  207. struct console_info *i = (struct console_info *) e->u.extra;
  208. if (dcc[idx].user && (dcc[idx].user->flags & USER_MASTER)) {
  209. dprintf(idx, " %s\n", "Saved Console Settings:");
  210. dprintf(idx, " %s %s\n", "Channel:", i->channel);
  211. dprintf(idx, " %s %s, %s %s, %s %s\n", "Console flags:",
  212. masktype(i->conflags), "",
  213. stripmasktype(i->stripflags), "Echo:", i->echoflags ? "yes" : "no");
  214. dprintf(idx, " %s %d, %s %s%d\n", "Page setting:", i->page,
  215. "Console channel:", (i->conchan < GLOBAL_CHANS) ? "" : "*", i->conchan % GLOBAL_CHANS);
  216. dprintf(idx, " Color: $b%s$b\n", i->color ? "on" : "off");
  217. dprintf(idx, " Login settings:\n");
  218. dprintf(idx, " Banner: $b%-3s$b Bots: $b%-3s$b\n", i->banner ? "on" : "off", i->bots ? "on" : "off");
  219. dprintf(idx, " Channels: $b%-3s$b Whom: $b%-3s$b\n", i->channels ? "on" : "off", i->whom ? "on" : "off");
  220. }
  221. }
  222. struct user_entry_type USERENTRY_CONSOLE = {
  223. 0, /* always 0 ;) */
  224. console_gotshare,
  225. console_unpack,
  226. console_write_userfile,
  227. console_kill,
  228. def_get,
  229. console_set,
  230. console_display,
  231. "CONSOLE"
  232. };
  233. static int
  234. console_chon(char *handle, int idx)
  235. {
  236. if (dcc[idx].type == &DCC_CHAT) {
  237. struct console_info *i = (struct console_info *) get_user(&USERENTRY_CONSOLE, dcc[idx].user);
  238. if (i) {
  239. if (i->channel && i->channel[0]) {
  240. struct chat_info dummy;
  241. strlcpy(dcc[idx].u.chat->con_chan, i->channel, sizeof(dummy.con_chan));
  242. }
  243. dcc[idx].u.chat->con_flags = i->conflags;
  244. dcc[idx].u.chat->strip_flags = i->stripflags;
  245. if (i->echoflags)
  246. dcc[idx].status |= STAT_ECHO;
  247. else
  248. dcc[idx].status &= ~STAT_ECHO;
  249. if (i->page) {
  250. dcc[idx].status |= STAT_PAGE;
  251. dcc[idx].u.chat->max_line = i->page;
  252. if (!dcc[idx].u.chat->line_count)
  253. dcc[idx].u.chat->current_lines = 0;
  254. }
  255. if (i->color)
  256. dcc[idx].status |= STAT_COLOR;
  257. else
  258. dcc[idx].status &= ~STAT_COLOR;
  259. if (i->banner)
  260. dcc[idx].status |= STAT_BANNER;
  261. else
  262. dcc[idx].status &= ~STAT_BANNER;
  263. if (i->channels)
  264. dcc[idx].status |= STAT_CHANNELS;
  265. else
  266. dcc[idx].status &= ~STAT_CHANNELS;
  267. if (i->bots)
  268. dcc[idx].status |= STAT_BOTS;
  269. else
  270. dcc[idx].status &= ~STAT_BOTS;
  271. if (i->whom)
  272. dcc[idx].status |= STAT_WHOM;
  273. else
  274. dcc[idx].status &= ~STAT_WHOM;
  275. }
  276. if ((dcc[idx].u.chat->channel >= 0) && (dcc[idx].u.chat->channel < GLOBAL_CHANS)) {
  277. botnet_send_join_idx(idx);
  278. }
  279. }
  280. return 0;
  281. }
  282. static int
  283. console_store(int idx, char *par, bool displaySave)
  284. {
  285. struct console_info *i = (struct console_info *) get_user(&USERENTRY_CONSOLE, dcc[idx].user);
  286. if (!i)
  287. i = (struct console_info *) my_calloc(1, sizeof(struct console_info));
  288. if (i->channel)
  289. free(i->channel);
  290. i->channel = strdup(dcc[idx].u.chat->con_chan);
  291. i->conflags = dcc[idx].u.chat->con_flags;
  292. i->stripflags = dcc[idx].u.chat->strip_flags;
  293. i->echoflags = (dcc[idx].status & STAT_ECHO) ? 1 : 0;
  294. if (dcc[idx].status & STAT_PAGE)
  295. i->page = dcc[idx].u.chat->max_line;
  296. else
  297. i->page = 0;
  298. if (dcc[idx].status & STAT_COLOR)
  299. i->color = 1;
  300. else
  301. i->color = 0;
  302. if (dcc[idx].status & STAT_BANNER)
  303. i->banner = 1;
  304. else
  305. i->banner = 0;
  306. if (dcc[idx].status & STAT_CHANNELS)
  307. i->channels = 1;
  308. else
  309. i->channels = 0;
  310. if (dcc[idx].status & STAT_BOTS)
  311. i->bots = 1;
  312. else
  313. i->bots = 0;
  314. if (dcc[idx].status & STAT_WHOM)
  315. i->whom = 1;
  316. else
  317. i->whom = 0;
  318. i->conchan = dcc[idx].u.chat->channel;
  319. if (par) {
  320. dprintf(idx, "%s\n", "Saved your Console Settings:");
  321. dprintf(idx, " %s %s\n", "Channel:", i->channel);
  322. dprintf(idx, " %s %s, %s %s, %s %s\n", "Console flags:",
  323. masktype(i->conflags), "",
  324. stripmasktype(i->stripflags), "Echo:", i->echoflags ? "yes" : "no");
  325. dprintf(idx, " %s %d, %s %d\n", "Page setting:", i->page, "Console channel:", i->conchan);
  326. dprintf(idx, " Color: $b%s$b\n", i->color ? "on" : "off");
  327. dprintf(idx, " Login settings:\n");
  328. dprintf(idx, " Login settings:\n");
  329. dprintf(idx, " Banner: $b%-3s$b Bots: $b%-3s$b\n", i->banner ? "on" : "off", i->bots ? "on" : "off");
  330. dprintf(idx, " Channels: $b%-3s$b Whom: $b%-3s$b\n", i->channels ? "on" : "off", i->whom ? "on" : "off");
  331. }
  332. set_user(&USERENTRY_CONSOLE, dcc[idx].user, i);
  333. dprintf(idx, "Console setting stored.\n");
  334. if (conf.bot->hub)
  335. write_userfile(displaySave ? idx : -1);
  336. return 0;
  337. }
  338. /* cmds.c:cmd_console calls this, better than chof bind - drummer,07/25/1999 */
  339. void
  340. console_dostore(int idx, bool displaySave)
  341. {
  342. console_store(idx, NULL, displaySave);
  343. return;
  344. }
  345. static cmd_t mychon[] = {
  346. {"*", "", (Function) console_chon, "console:chon", 0},
  347. {NULL, NULL, NULL, NULL, 0}
  348. };
  349. static cmd_t mydcc[] = {
  350. {"store", "", (Function) console_store, NULL, 0},
  351. {NULL, NULL, NULL, NULL, 0}
  352. };
  353. void
  354. console_init()
  355. {
  356. add_builtins("dcc", mydcc);
  357. add_builtins("chon", mychon);
  358. add_entry_type(&USERENTRY_CONSOLE);
  359. }