4
0

dcccmds.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * Copyright (C) 2000,2001 Florian Sander
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. static int cmd_savestats(struct userrec *u, int idx, char *par)
  19. {
  20. write_stats();
  21. putlog(LOG_CMDS, "*", "#%s# savestats", dcc[idx].nick);
  22. return 0;
  23. }
  24. static int cmd_loadstats(struct userrec *u, int idx, char *par)
  25. {
  26. read_stats();
  27. putlog(LOG_CMDS, "*", "#%s# loadstats", dcc[idx].nick);
  28. return 0;
  29. }
  30. static int cmd_writewebstats(struct userrec *u, int idx, char *par)
  31. {
  32. Context;
  33. #if EGG_IS_MIN_VER(10500)
  34. write_new_webstats();
  35. #else
  36. dprintf(idx, "Sorry, this function isn't available with eggdrop1.4.\n");
  37. dprintf(idx, "Either update your bot to eggdrop1.5+ or use livestats instead of static webfiles.\n");
  38. dprintf(idx, "(or even better: do both! <g>)\n");
  39. #endif
  40. putlog(LOG_CMDS, "*", "#%s# writewebstats %s", dcc[idx].nick, par);
  41. Context;
  42. return 0;
  43. }
  44. static int cmd_purgestats(struct userrec *u, int idx, char *par)
  45. {
  46. Context;
  47. deloldstatusers();
  48. Context;
  49. purgestats();
  50. Context;
  51. update_schannel_members();
  52. Context;
  53. putlog(LOG_CMDS, "*", "#%s# purgestats", dcc[idx].nick);
  54. Context;
  55. return 0;
  56. }
  57. static int cmd_sumuser(struct userrec *user, int idx, char *par)
  58. {
  59. struct userrec *u1, *u2;
  60. struct stats_userlist *uu1, *uu2;
  61. struct stats_hostlist *h;
  62. char *user1, *user2, *hand1, *hand2;
  63. globstats *gs;
  64. locstats *ls;
  65. int i;
  66. struct list_type *q;
  67. Context;
  68. user1 = newsplit(&par);
  69. user2 = par;
  70. u1 = get_user_by_handle(userlist, user1);
  71. u2 = get_user_by_handle(userlist, user2);
  72. uu1 = findsuser_by_name(user1);
  73. uu2 = findsuser_by_name(user2);
  74. if (!user1[0] || !user2[0]) {
  75. dprintf(idx, "Usage: .sumuser <user1> <user2>\n");
  76. return 0;
  77. }
  78. if (use_userfile && !u1) {
  79. dprintf(idx, "%s isn't a valid user!\n", user1);
  80. return 0;
  81. }
  82. if (use_userfile && !u2) {
  83. dprintf(idx, "%s isn't a valid user!\n", user2);
  84. return 0;
  85. }
  86. if (!use_userfile && !uu1) {
  87. dprintf(idx, "%s isn't a valid user!\n", user1);
  88. return 0;
  89. }
  90. if (!use_userfile && !uu2) {
  91. dprintf(idx, "%s isn't a valid user!\n", user2);
  92. return 0;
  93. }
  94. if (use_userfile) {
  95. hand1 = u1->handle;
  96. hand2 = u2->handle;
  97. } else {
  98. hand1 = uu1->user;
  99. hand2 = uu2->user;
  100. }
  101. for (gs = sdata; gs; gs = gs->next) {
  102. for (ls = gs->local; ls; ls = ls->next) {
  103. if (!strcasecmp(ls->user, hand2)) {
  104. for (i = 0; i < TOTAL_TYPES; i++) {
  105. incrstats(hand1, gs->chan, i, ls->values[S_TOTAL][i], (S_TOTAL + 1) * (-1));
  106. incrstats(hand1, gs->chan, i, ls->values[S_DAILY][i], (S_DAILY + 1) * (-1));
  107. incrstats(hand1, gs->chan, i, ls->values[S_WEEKLY][i], (S_WEEKLY + 1) * (-1));
  108. incrstats(hand1, gs->chan, i, ls->values[S_MONTHLY][i], (S_MONTHLY + 1) * (-1));
  109. }
  110. if (getstats(hand1, "", "gstarted", 0)
  111. < getstats(hand2, "", "gstarted", 0))
  112. incrstats(hand1, "", T_LSTARTED, getstats(hand2, "", "gstarted", 0), 1);
  113. }
  114. }
  115. }
  116. if (use_userfile) {
  117. for (q = get_user(&USERENTRY_HOSTS, u2); q; q = q->next)
  118. addhost_by_handle(u1->handle, q->extra);
  119. deluser(u2->handle);
  120. } else {
  121. for (h = uu2->hosts; h; h = h->next)
  122. saddhost(uu1, h->mask, h->lastused);
  123. delsuser(uu2->user);
  124. }
  125. dprintf(idx, "Transferred stats from %s to %s and deleted %s\n", user2,
  126. hand1, user2);
  127. putlog(LOG_CMDS, "*", "#%s# sumuser %s %s", dcc[idx].nick, user1, user2);
  128. update_schannel_members();
  129. Context;
  130. return 0;
  131. }
  132. static int cmd_resetuser(struct userrec *u, int idx, char *par)
  133. {
  134. char *user, *chan;
  135. locstats *ls;
  136. Context;
  137. user = newsplit(&par);
  138. chan = newsplit(&par);
  139. ls = findlocstats(chan, user);
  140. if (!ls) {
  141. dprintf(idx, "Couldn't find stats for %s in %s.\n", user, chan);
  142. return 0;
  143. }
  144. resetlocstats(ls);
  145. dprintf(idx, "%s's stats set to 0 on %s.\n", ls->user, chan);
  146. putlog(LOG_CMDS, "*", "#%s# resetuser %s %s", dcc[idx].nick, user, chan);
  147. Context;
  148. return 0;
  149. }
  150. static int cmd_schannel(struct userrec *u, int idx, char *par)
  151. {
  152. char *chname, spaces[50], *user, ubuf[2];
  153. struct stats_chanset *chan;
  154. struct stats_memberlist *m;
  155. int len = 0;
  156. Context;
  157. ubuf[0] = '*';
  158. ubuf[1] = 0;
  159. #if EGG_IS_MIN_VER(10500)
  160. len = nick_len;
  161. #else
  162. len = NICKLEN;
  163. #endif
  164. chname = newsplit(&par);
  165. putlog(LOG_CMDS, "*", "#%s# (%s) schannel %s", dcc[idx].nick,
  166. dcc[idx].u.chat->con_chan, chname);
  167. if (!chname[0])
  168. chname = dcc[idx].u.chat->con_chan;
  169. chan = findschan(chname);
  170. if (!chan) {
  171. if (!findchan_by_dname(chname))
  172. dprintf(idx, "Invalid channel: %s\n", chname);
  173. else
  174. dprintf(idx, "Channel %s is inactive\n", chname);
  175. return 0;
  176. }
  177. dprintf(idx, "%d users on channel:\n", countmembers(chan->members));
  178. sprintf(spaces, " ");
  179. spaces[len - 4] = 0;
  180. dprintf(idx, "NICK%s", spaces);
  181. spaces[len - 4] = ' ';
  182. spaces[HANDLEN - 4] = 0;
  183. dprintf(idx, " USER%s", spaces);
  184. dprintf(idx, " UHOST\n");
  185. spaces[HANDLEN - 4] = ' ';
  186. for (m = chan->members; m; m = m->next) {
  187. if (m->user)
  188. user = m->user->user;
  189. else
  190. user = ubuf;
  191. spaces[len - strlen(m->nick)] = 0;
  192. dprintf(idx, "%s%s", m->nick, spaces);
  193. spaces[len - strlen(m->nick)] = ' ';
  194. spaces[HANDLEN - strlen(user)] = 0;
  195. dprintf(idx, " %s%s", user, spaces);
  196. spaces[HANDLEN - strlen(user)] = ' ';
  197. dprintf(idx, " %s\n", m->uhost);
  198. }
  199. Context;
  200. return 0;
  201. }
  202. static int cmd_swhois(struct userrec *user, int idx, char *par)
  203. {
  204. struct stats_userlist *u;
  205. Context;
  206. if (!par[0]) {
  207. dprintf(idx, "Usage: .swhois <stats-user>\n");
  208. return 0;
  209. }
  210. putlog(LOG_CMDS, "*", "#%s# swhois %s", dcc[idx].nick, par);
  211. u = findsuser_by_name(par);
  212. if (!u) {
  213. dprintf(idx, "No such user %s.\n", par);
  214. return 0;
  215. }
  216. dump_suser(idx, u);
  217. return 0;
  218. }
  219. static void dump_suser(int idx, struct stats_userlist *u)
  220. {
  221. struct stats_hostlist *h;
  222. struct userrec *uu;
  223. dprintf(idx, "------ %s ------\n", u->user);
  224. dprintf(idx, "flags: %clist %caddhosts\n", u->list ? '+' : '-', u->addhosts ? '+' : '-');
  225. if (u->hosts) {
  226. dprintf(idx, "Hosts: %s\n", u->hosts->mask);
  227. for (h = u->hosts->next; h; h = h->next)
  228. dprintf(idx, " %s\n", h->mask);
  229. }
  230. uu = get_user_by_handle(userlist, u->user);
  231. if (uu)
  232. dprintf(idx, "This user also exists in the eggdrop-userfile. There might be additional hosts.\n");
  233. }
  234. static int cmd_pls_shost(struct userrec *user, int idx, char *par)
  235. {
  236. char *suser, *host;
  237. struct stats_userlist *u;
  238. Context;
  239. suser = newsplit(&par);
  240. host = newsplit(&par);
  241. if (!suser[0] || !host[0]) {
  242. dprintf(idx, "Usage: .+shost <statuser> <host>\n");
  243. return 0;
  244. }
  245. putlog(LOG_CMDS, "*", "#%s# +shost %s %s", dcc[idx].nick, suser, host);
  246. u = findsuser_by_name(suser);
  247. if (!u) {
  248. dprintf(idx, "No such user: %s\n", suser);
  249. return 0;
  250. }
  251. saddhost(u, host, now);
  252. dprintf(idx, "Added hostmask %s to %s.\n", host, u->user);
  253. update_schannel_members();
  254. return 0;
  255. }
  256. static int cmd_mns_shost(struct userrec *user, int idx, char *par)
  257. {
  258. char *suser, *host;
  259. struct stats_userlist *u;
  260. Context;
  261. suser = newsplit(&par);
  262. host = newsplit(&par);
  263. if (!suser[0] || !host[0]) {
  264. dprintf(idx, "Usage: .-shost <statuser> <host>\n");
  265. return 0;
  266. }
  267. putlog(LOG_CMDS, "*", "#%s# -shost %s %s", dcc[idx].nick, suser, host);
  268. u = findsuser_by_name(suser);
  269. if (!u) {
  270. dprintf(idx, "No such user: %s\n", suser);
  271. return 0;
  272. }
  273. if (sdelhost(u, host)) {
  274. dprintf(idx, "Removed hostmask %s from %s.\n", host, u->user);
  275. update_schannel_members();
  276. } else
  277. dprintf(idx, "Couldn't remove hostmask %s from %s: no such mask!\n", host, u->user);
  278. update_schannel_members();
  279. return 0;
  280. }
  281. static int cmd_pls_suser(struct userrec *user, int idx, char *par)
  282. {
  283. struct stats_userlist *u;
  284. char *suser, *host;
  285. Context;
  286. suser = newsplit(&par);
  287. host = newsplit(&par);
  288. if (!suser[0]) {
  289. dprintf(idx, "Usage: .+suser <statuser> [host]\n");
  290. return 0;
  291. }
  292. putlog(LOG_CMDS, "*", "#%s# +suser %s %s", dcc[idx].nick, suser, host);
  293. u = findsuser_by_name(suser);
  294. if (u) {
  295. dprintf(idx, "That statuser already exists!\n");
  296. return 0;
  297. }
  298. u = addsuser(suser, 1, 1);
  299. if (host[0]) {
  300. saddhost(u, host, now);
  301. putlog(LOG_MISC, "Added stats user %s with hostmask %s.", u->user, host);
  302. } else
  303. putlog(LOG_MISC, "Added stats user %s without a hostmask.", u->user);
  304. update_schannel_members();
  305. return 0;
  306. }
  307. static int cmd_mns_suser(struct userrec *user, int idx, char *par)
  308. {
  309. struct stats_userlist *u;
  310. char *suser;
  311. Context;
  312. suser = newsplit(&par);
  313. if (!suser[0]) {
  314. dprintf(idx, "Usage: .-suser <statuser>\n");
  315. return 0;
  316. }
  317. putlog(LOG_CMDS, "*", "#%s# -suser %s", dcc[idx].nick, suser);
  318. u = findsuser_by_name(suser);
  319. if (!u) {
  320. dprintf(idx, "No such user.\n");
  321. return 0;
  322. }
  323. delsuser(suser);
  324. putlog(LOG_MISC, "Deleted stats user %s.", u->user);
  325. update_schannel_members();
  326. return 0;
  327. }
  328. static int cmd_schattr(struct userrec *user, int idx, char *par)
  329. {
  330. struct stats_userlist *u;
  331. char *suser, *mode1, *mode2;
  332. Context;
  333. suser = newsplit(&par);
  334. mode1 = newsplit(&par);
  335. mode2 = newsplit(&par);
  336. if (!suser[0] || !mode1[0]) {
  337. dprintf(idx, "Usage: .schattr <statuser> <+/-list +/-addhosts>\n");
  338. return 0;
  339. }
  340. putlog(LOG_CMDS, "*", "#%s# schattr %s %s %s", dcc[idx].nick, suser, mode1, mode2);
  341. u = findsuser_by_name(suser);
  342. if (!u) {
  343. dprintf(idx, "No such user.\n");
  344. return 0;
  345. }
  346. if (!strcasecmp(mode1, "+list"))
  347. u->list = 1;
  348. else if (!strcasecmp(mode1, "-list"))
  349. u->list = 0;
  350. if (!strcasecmp(mode1, "+addhosts"))
  351. u->addhosts = 1;
  352. else if (!strcasecmp(mode1, "-addhosts"))
  353. u->addhosts = 0;
  354. dprintf(idx, "New settings for %s: %clist %caddhosts\n", u->user,
  355. u->list ? '+' : '-', u->addhosts ? '+' : '-');
  356. return 0;
  357. }
  358. static int cmd_updateschans(struct userrec *user, int idx, char *par)
  359. {
  360. Context;
  361. putlog(LOG_CMDS, "*", "#%s# updateschans", dcc[idx].nick);
  362. update_schannel_members();
  363. return 0;
  364. }
  365. static int cmd_smatch(struct userrec *user, int idx, char *par)
  366. {
  367. char *tmp, *mask;
  368. int list, addhosts, match, matches, deb;
  369. struct stats_userlist *u;
  370. struct stats_hostlist *h;
  371. Context;
  372. mask = NULL;
  373. list = addhosts = -1;
  374. putlog(LOG_CMDS, "*", "#%s# smatch %s", dcc[idx].nick, par);
  375. while (par[0]) {
  376. tmp = newsplit(&par);
  377. if ((tmp[0] == '+') || (tmp[0] == '-')) {
  378. if (!strcasecmp(tmp + 1, "addhosts"))
  379. addhosts = (tmp[0] == '+');
  380. else if (!strcasecmp(tmp + 1, "list"))
  381. list = (tmp[0] == '+');
  382. else {
  383. dprintf(idx, "Unknown stats-user flag: %s\n", tmp);
  384. return 0;
  385. }
  386. } else {
  387. if (mask) {
  388. dprintf(idx, "Usage: .smatch [hostmask|nickmask] [+|-addhosts] [+|-list]\n");
  389. return 0;
  390. }
  391. mask = tmp;
  392. }
  393. }
  394. if (!mask && (list == -1) && (addhosts == -1)) {
  395. dprintf(idx, "Usage: .smatch [hostmask|nickmask] [+|-addhosts] [+|-list]\n");
  396. return 0;
  397. }
  398. debug3("mask: %s, list: %d, addhosts: %d", mask, list, addhosts);
  399. matches = deb = 0;
  400. for (u = suserlist; u; u = u->next) {
  401. deb++;
  402. match = 0;
  403. if ((list != (-1)) && (u->list != list))
  404. continue;
  405. if ((addhosts != (-1)) && (u->addhosts != addhosts))
  406. continue;
  407. if (mask) {
  408. if (wild_match(mask, u->user)) {
  409. match = 1;
  410. } else {
  411. for (h = u->hosts; h && !match; h = h->next) {
  412. if (wild_match(mask, h->mask)) {
  413. match = 1;
  414. }
  415. }
  416. }
  417. } else
  418. match = 1;
  419. if (match) {
  420. matches++;
  421. if (matches > 20) {
  422. dprintf(idx, "More than 20 matches. Truncated.\n");
  423. break;
  424. } else
  425. dump_suser(idx, u);
  426. }
  427. }
  428. if (!matches)
  429. dprintf(idx, "No matches!\n");
  430. else if (matches <= 20)
  431. dprintf(idx, "===\n%d matches found.\n", matches);
  432. debug2("%d matches, %d cycles", matches, deb);
  433. return 0;
  434. }
  435. static int cmd_chsusername(struct userrec *user, int idx, char *par)
  436. {
  437. char *oldnick, *newnick;
  438. struct stats_userlist *u;
  439. Context;
  440. putlog(LOG_CMDS, "*", "#%s# chsusername %s", dcc[idx].nick, par);
  441. oldnick = newsplit(&par);
  442. newnick = newsplit(&par);
  443. if (!newnick[0] || par[0]) {
  444. dprintf(idx, "Usage: .chsusername old_user_name new_user_name\n");
  445. return 0;
  446. }
  447. u = findsuser_by_name(oldnick);
  448. if (!u) {
  449. dprintf(idx, "No such user: %s\n", oldnick);
  450. return 0;
  451. }
  452. u = findsuser_by_name(newnick);
  453. if (u && strcasecmp(oldnick, newnick)) {
  454. dprintf(idx, "User %s already exists!\n", newnick);
  455. return 0;
  456. }
  457. if (get_user_by_handle(userlist, oldnick)) {
  458. dprintf(idx, "%s exists in the eggdrop userfile. Please use .chnick or "
  459. ".chhand instead to keep the stats synched.\n");
  460. return 0;
  461. }
  462. track_stat_user(oldnick, newnick);
  463. update_schannel_members();
  464. return 0;
  465. }
  466. static cmd_t mydcc[] =
  467. {
  468. {"savestats", "m", cmd_savestats, NULL},
  469. {"loadstats", "m", cmd_loadstats, NULL},
  470. {"writewebstats", "m|-", cmd_writewebstats, NULL},
  471. {"purgestats", "m|-", cmd_purgestats, NULL},
  472. {"sumuser", "n|-", cmd_sumuser, NULL},
  473. {"resetuser", "m|-", cmd_resetuser, NULL},
  474. {"schannel", "o|o", cmd_schannel, NULL},
  475. {"swhois", "o|o", cmd_swhois, NULL},
  476. {"+shost", "m|-", cmd_pls_shost, NULL},
  477. {"-shost", "m|-", cmd_mns_shost, NULL},
  478. {"+suser", "m|-", cmd_pls_suser, NULL},
  479. {"-suser", "m|-", cmd_mns_suser, NULL},
  480. {"schattr", "m|-", cmd_schattr, NULL},
  481. {"updateschans", "-|-", cmd_updateschans, NULL},
  482. {"smatch", "m", cmd_smatch, NULL},
  483. {"chsusername", "m", cmd_chsusername, NULL},
  484. {0, 0, 0, 0}
  485. };