cmdsirc.cc 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2014 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. * chancmds.c -- part of irc.mod
  22. * handles commands directly relating to channel interaction
  23. *
  24. */
  25. #include <bdlib/src/Stream.h>
  26. #include <bdlib/src/String.h>
  27. #include <algorithm>
  28. using std::swap;
  29. #include "src/misc_file.h"
  30. /* Do we have any flags that will allow us ops on a channel?
  31. */
  32. static struct chanset_t *get_channel(int idx, char *chname, bool check_console = 1, bool* all = NULL)
  33. {
  34. struct chanset_t *chan = NULL;
  35. if (all && ((chname && chname[0] && !strcmp(chname, "*")) || (check_console && !(chname && chname[0]) && !strcmp(dcc[idx].u.chat->con_chan, "*")))) {
  36. *all = 1;
  37. return NULL;
  38. }
  39. if (chname && chname[0]) {
  40. chan = findchan_by_dname(chname);
  41. if (chan)
  42. return chan;
  43. else
  44. dprintf(idx, "No such channel.\n");
  45. } else if (check_console) {
  46. chname = dcc[idx].u.chat->con_chan;
  47. chan = findchan_by_dname(chname);
  48. if (chan)
  49. return chan;
  50. else
  51. dprintf(idx, "Invalid console channel.\n");
  52. }
  53. return NULL;
  54. }
  55. /* Do we have any flags that will allow us ops on a channel?
  56. */
  57. static int has_op(int idx, struct chanset_t *chan)
  58. {
  59. get_user_flagrec(dcc[idx].user, &user, chan->dname);
  60. if (privchan(user, chan, PRIV_OP)) {
  61. dprintf(idx, "No such channel.\n");
  62. return 0;
  63. }
  64. if (real_chk_op(user, chan, 0))
  65. return 1;
  66. dprintf(idx, "You are not a channel op on %s.\n", chan->dname);
  67. return 0;
  68. }
  69. /* Finds a nick of the handle. Returns m->nick if
  70. * the nick was found, otherwise NULL (Sup 1Nov2000)
  71. */
  72. char *getnick(const char *handle, struct chanset_t *chan)
  73. {
  74. for (memberlist *m = chan->channel.member; m && m->nick[0]; m = m->next) {
  75. member_getuser(m);
  76. if (m->user && !strcasecmp(m->user->handle, handle))
  77. return m->nick;
  78. }
  79. return "";
  80. }
  81. static void cmd_act(int idx, char *par)
  82. {
  83. if (!par[0]) {
  84. dprintf(idx, "Usage: act [channel] <action>\n");
  85. return;
  86. }
  87. char *chname = NULL;
  88. struct chanset_t *chan = NULL;
  89. if (strchr(CHANMETA, par[0]) != NULL)
  90. chname = newsplit(&par);
  91. else
  92. chname = 0;
  93. chan = get_channel(idx, chname);
  94. if (!chan || !has_op(idx, chan))
  95. return;
  96. memberlist *m = ismember(chan, botname);
  97. if (!m) {
  98. dprintf(idx, "Cannot say to %s: I'm not on that channel.\n", chan->dname);
  99. return;
  100. }
  101. get_user_flagrec(dcc[idx].user, &user, chan->dname);
  102. if ((chan->channel.mode & CHANMODER) && !me_op(chan) && !me_voice(chan)) {
  103. dprintf(idx, "Cannot say to %s: It is moderated.\n", chan->dname);
  104. return;
  105. }
  106. putlog(LOG_CMDS, "*", "#%s# (%s) act %s", dcc[idx].nick,
  107. chan->dname, par);
  108. bd::String msg;
  109. msg = bd::String::printf("\001ACTION %s\001", par);
  110. privmsg(chan->name, msg, DP_HELP);
  111. dprintf(idx, "Action to %s: %s\n", chan->dname, par);
  112. }
  113. static void cmd_msg(int idx, char *par)
  114. {
  115. if (dcc[idx].simul >= 0) {
  116. dprintf(idx, "Sorry, that cmd isn't available over botcmd.\n");
  117. dprintf(idx, "Instead try: %sbotmsg %s %s\n", settings.dcc_prefix, conf.bot->nick, par);
  118. return;
  119. }
  120. if (!par[0]) {
  121. dprintf(idx, "Usage: msg <nick> <message>\n");
  122. return;
  123. }
  124. char *nick = newsplit(&par);
  125. putlog(LOG_CMDS, "*", "#%s# msg %s %s", dcc[idx].nick, nick, par);
  126. privmsg(nick, par, DP_HELP);
  127. dprintf(idx, "Msg to %s: %s\n", nick, par);
  128. }
  129. static void cmd_nick(int idx, char *par)
  130. {
  131. putlog(LOG_CMDS, "*", "#%s# nick", dcc[idx].nick);
  132. botnet_send_cmd(conf.bot->nick, conf.bot->nick, dcc[idx].nick, idx, "curnick");
  133. }
  134. static void cmd_say(int idx, char *par)
  135. {
  136. if (dcc[idx].simul >= 0) {
  137. dprintf(idx, "Sorry, that cmd isn't available over botcmd.\n");
  138. dprintf(idx, "Instead try: %sbotmsg %s %s\n", settings.dcc_prefix, conf.bot->nick, par);
  139. return;
  140. }
  141. if (!par[0]) {
  142. dprintf(idx, "Usage: say [channel] <message>\n");
  143. return;
  144. }
  145. char *chname = NULL;
  146. struct chanset_t *chan = NULL;
  147. if (strchr(CHANMETA, par[0]) != NULL)
  148. chname = newsplit(&par);
  149. else
  150. chname = 0;
  151. chan = get_channel(idx, chname);
  152. if (!chan || !has_op(idx, chan))
  153. return;
  154. memberlist *m = ismember(chan, botname);
  155. if (!m) {
  156. dprintf(idx, "Cannot say to %s: I'm not on that channel.\n", chan->dname);
  157. return;
  158. }
  159. get_user_flagrec(dcc[idx].user, &user, chan->dname);
  160. if ((chan->channel.mode & CHANMODER) && !me_op(chan) && !me_voice(chan)) {
  161. dprintf(idx, "Cannot say to %s: It is moderated.\n", chan->dname);
  162. return;
  163. }
  164. putlog(LOG_CMDS, "*", "#%s# (%s) say %s", dcc[idx].nick, chan->dname, par);
  165. privmsg(chan->name, par, DP_HELP);
  166. dprintf(idx, "Said to %s: %s\n", chan->dname, par);
  167. }
  168. static void cmd_swhois(int idx, char *par)
  169. {
  170. if (!par[0]) {
  171. dprintf(idx, "Usage: swhois [server/nick] nick\n");
  172. return;
  173. }
  174. putlog(LOG_CMDS, "*", "#%s# swhois %s", dcc[idx].nick, par);
  175. if (!server_online) {
  176. dprintf(idx, "I am currently not connected!\n");
  177. return;
  178. }
  179. char *server = newsplit(&par), *nick = NULL;
  180. if (par[0])
  181. nick = newsplit(&par);
  182. strlcpy(dcc[idx].whois, nick ? nick : server, UHOSTLEN);
  183. dprintf(DP_SERVER, "WHOIS %s %s\n", server, nick ? nick : "");
  184. }
  185. static void cmd_kickban(int idx, char *par)
  186. {
  187. if (!par[0]) {
  188. dprintf(idx, "Usage: kickban [channel|*] [-|@]<nick> [reason]\n");
  189. return;
  190. }
  191. char *chname = NULL;
  192. bool all = 0;
  193. if (strchr(CHANMETA, par[0]))
  194. chname = newsplit(&par);
  195. else
  196. chname = NULL;
  197. struct chanset_t* chan = get_channel(idx, chname, 1, &all);
  198. if (!all && !chan)
  199. return;
  200. putlog(LOG_CMDS, "*", "#%s# (%s) kickban %s", dcc[idx].nick, all ? "*" : chan->dname, par);
  201. char *nick = newsplit(&par), bantype = 0;
  202. if ((nick[0] == '@') || (nick[0] == '-')) {
  203. bantype = nick[0];
  204. nick++;
  205. }
  206. if (match_my_nick(nick)) {
  207. dprintf(idx, "I'm not going to kickban myself.\n");
  208. return;
  209. }
  210. memberlist *m = NULL;
  211. char *s1 = NULL, s[UHOSTLEN] = "";
  212. struct userrec *u = NULL;
  213. if (all)
  214. chan = chanset;
  215. while (chan) {
  216. get_user_flagrec(dcc[idx].user, &user, chan->dname);
  217. if (privchan(user, chan, PRIV_OP)) {
  218. if (all) goto next;
  219. dprintf(idx, "No such channel.\n");
  220. return;
  221. }
  222. else if (!real_chk_op(user, chan, 0)) {
  223. if (all) goto next;
  224. dprintf(idx, "You don't have access to %s\n", chan->dname);
  225. return;
  226. }
  227. if (!channel_active(chan)) {
  228. if (all) goto next;
  229. dprintf(idx, "I'm not on %s right now!\n", chan->dname);
  230. return;
  231. }
  232. if (!me_op(chan)) {
  233. if (all) goto next;
  234. dprintf(idx, "I can't help you now because I'm not a channel op"
  235. " on %s.\n", chan->dname);
  236. return;
  237. }
  238. m = ismember(chan, nick);
  239. if (!m) {
  240. if (all) goto next;
  241. dprintf(idx, "%s is not on %s\n", nick, chan->dname);
  242. return;
  243. }
  244. member_getuser(m);
  245. u = m->user;
  246. strlcpy(s, m->from, sizeof(s));
  247. get_user_flagrec(u, &victim, chan->dname);
  248. if ((chan_master(victim) || glob_master(victim)) &&
  249. !(glob_owner(user) || chan_owner(user))) {
  250. if (all) goto next;
  251. dprintf(idx, "%s is a %s master.\n", nick, chan->dname);
  252. return;
  253. }
  254. if (glob_bot(victim)) {
  255. if (all) goto next;
  256. dprintf(idx, "%s is another channel bot!\n", nick);
  257. return;
  258. }
  259. if (u_match_mask(global_exempts,s) || u_match_mask(chan->exempts, s)) {
  260. dprintf(idx, "%s is permanently exempted!\n", nick);
  261. return;
  262. }
  263. if (chan_hasop(m))
  264. add_mode(chan, '-', 'o', m);
  265. check_exemptlist(chan, s);
  266. switch (bantype) {
  267. case '@':
  268. s1 = strchr(s, '@');
  269. s1 -= 3;
  270. s1[0] = '*';
  271. s1[1] = '!';
  272. s1[2] = '*';
  273. break;
  274. case '-':
  275. s1 = strchr(s, '!');
  276. s1[1] = '*';
  277. s1--;
  278. s1[0] = '*';
  279. break;
  280. default:
  281. s1 = quickban(chan, m->userhost);
  282. break;
  283. }
  284. if (bantype == '@' || bantype == '-')
  285. do_mask(chan, chan->channel.ban, s1, 'b');
  286. if (!par[0])
  287. par = "requested";
  288. dprintf(DP_MODE, "KICK %s %s :%s%s\n", chan->name, m->nick, bankickprefix, par);
  289. m->flags |= SENTKICK;
  290. u_addmask('b', chan, s1, dcc[idx].nick, par, now + (60 * chan->ban_time), 0);
  291. dprintf(idx, "Kick-banned %s on %s.\n", nick, chan->dname);
  292. next:;
  293. if (!all)
  294. chan = NULL;
  295. else
  296. chan = chan->next;
  297. }
  298. }
  299. static void cmd_voice(int idx, char *par)
  300. {
  301. char *nick = newsplit(&par), *chname = newsplit(&par);
  302. if (strchr(CHANMETA, nick[0]) || (!chname[0] && !strcmp(nick, "*")))
  303. swap(nick, chname);
  304. bool all = 0;
  305. struct chanset_t* chan = get_channel(idx, chname, 1, &all);
  306. if (!all && !chan)
  307. return;
  308. memberlist *m = NULL;
  309. if (all)
  310. chan = chanset;
  311. putlog(LOG_CMDS, "*", "#%s# (%s) voice %s", dcc[idx].nick, all ? "*" : chan->dname , nick);
  312. while (chan) {
  313. if (!nick[0] && !(nick = getnick(dcc[idx].nick, chan))[0]) {
  314. if (all) goto next;
  315. dprintf(idx, "Usage: voice <nick> [channel|*]\n");
  316. return;
  317. }
  318. get_user_flagrec(dcc[idx].user, &user, chan->dname);
  319. if (privchan(user, chan, PRIV_VOICE)) {
  320. if (all) goto next;
  321. dprintf(idx, "No such channel.\n");
  322. return;
  323. }
  324. else if (!chk_voice(user, chan) && !chk_op(user, chan)) {
  325. if (all) goto next;
  326. dprintf(idx, "You don't have access to voice on %s\n", chan->dname);
  327. return;
  328. }
  329. if (!channel_active(chan)) {
  330. if (all) goto next;
  331. dprintf(idx, "I'm not on %s right now!\n", chan->dname);
  332. return;
  333. }
  334. if (!me_op(chan)) {
  335. if (all) goto next;
  336. dprintf(idx, "I can't help you now because I'm not a chan op on %s.\n",
  337. chan->dname);
  338. return;
  339. }
  340. m = ismember(chan, nick);
  341. if (!m) {
  342. if (all) goto next;
  343. dprintf(idx, "%s is not on %s.\n", nick, chan->dname);
  344. return;
  345. }
  346. add_mode(chan, '+', 'v', m);
  347. dprintf(idx, "Gave voice to %s on %s\n", nick, chan->dname);
  348. next:;
  349. if (!all)
  350. chan = NULL;
  351. else
  352. chan = chan->next;
  353. }
  354. }
  355. static void cmd_devoice(int idx, char *par)
  356. {
  357. char *nick = newsplit(&par), *chname = newsplit(&par);
  358. if (strchr(CHANMETA, nick[0]) || (!chname[0] && !strcmp(nick, "*")))
  359. swap(nick, chname);
  360. bool all = 0;
  361. struct chanset_t* chan = get_channel(idx, chname, 1, &all);
  362. if (!all && !chan)
  363. return;
  364. memberlist *m = NULL;
  365. if (all)
  366. chan = chanset;
  367. putlog(LOG_CMDS, "*", "#%s# (%s) devoice %s", dcc[idx].nick, all ? "*" : chan->dname, nick);
  368. while (chan) {
  369. if (!nick[0] && !(nick = getnick(dcc[idx].nick, chan))[0]) {
  370. if (all) goto next;
  371. dprintf(idx, "Usage: devoice <nick> [channel|*]\n");
  372. return;
  373. }
  374. get_user_flagrec(dcc[idx].user, &user, chan->dname);
  375. if (privchan(user, chan, PRIV_VOICE)) {
  376. if (all) goto next;
  377. dprintf(idx, "No such channel.\n");
  378. return;
  379. }
  380. else if (!chk_voice(user, chan) && !chk_op(user, chan)) {
  381. if (all) goto next;
  382. dprintf(idx, "You don't have access to devoice on %s\n", chan->dname);
  383. return;
  384. }
  385. if (!channel_active(chan)) {
  386. if (all) goto next;
  387. dprintf(idx, "I'm not on %s right now!\n", chan->dname);
  388. return;
  389. }
  390. if (!me_op(chan)) {
  391. if (all) goto next;
  392. dprintf(idx, "I can't do that right now I'm not a chan op on %s",
  393. chan->dname);
  394. return;
  395. }
  396. m = ismember(chan, nick);
  397. if (!m) {
  398. if (all) goto next;
  399. dprintf(idx, "%s is not on %s.\n", nick, chan->dname);
  400. return;
  401. }
  402. add_mode(chan, '-', 'v', m);
  403. dprintf(idx, "Devoiced %s on %s\n", nick, chan->dname);
  404. next:;
  405. if (!all)
  406. chan = NULL;
  407. else
  408. chan = chan->next;
  409. }
  410. }
  411. static void cmd_release(int idx, char *par) {
  412. putlog(LOG_CMDS, "*", "#%s# release", dcc[idx].nick);
  413. release_nick();
  414. }
  415. static void cmd_op(int idx, char *par)
  416. {
  417. char *nick = newsplit(&par), *chname = newsplit(&par);
  418. if (strchr(CHANMETA, nick[0]) || (!chname[0] && !strcmp(nick, "*")))
  419. swap(nick, chname);
  420. bool all = 0;
  421. struct chanset_t* chan = get_channel(idx, chname, 1, &all);
  422. if (!all && !chan)
  423. return;
  424. memberlist *m = NULL;
  425. struct userrec *u = NULL;
  426. if (all)
  427. chan = chanset;
  428. putlog(LOG_CMDS, "*", "#%s# (%s) op %s", dcc[idx].nick, all ? "*" : chan->dname, nick);
  429. while (chan) {
  430. get_user_flagrec(dcc[idx].user, &user, chan->dname);
  431. if (!nick[0] && !(nick = getnick(dcc[idx].nick, chan))[0]) {
  432. if (all) goto next;
  433. dprintf(idx, "Usage: op <nick> [channel|*]\n");
  434. return;
  435. }
  436. if (privchan(user, chan, PRIV_OP)) {
  437. if (!all)
  438. dprintf(idx, "No such channel.\n");
  439. goto next;
  440. }
  441. else if (!chk_op(user, chan)) {
  442. if (all) goto next;
  443. dprintf(idx, "You don't have access to op on %s\n", chan->dname);
  444. return;
  445. }
  446. if (!channel_active(chan)) {
  447. if (all) goto next;
  448. dprintf(idx, "I'm not on %s right now!\n", chan->dname);
  449. return;
  450. }
  451. if (!me_op(chan)) {
  452. if (all) goto next;
  453. dprintf(idx, "I can't help you now because I'm not a chan op on %s.\n",
  454. chan->dname);
  455. return;
  456. }
  457. m = ismember(chan, nick);
  458. if (!m) {
  459. if (all) goto next;
  460. dprintf(idx, "%s is not on %s.\n", nick, chan->dname);
  461. return;
  462. }
  463. member_getuser(m);
  464. u = m->user;
  465. get_user_flagrec(u, &victim, chan->dname);
  466. if (chk_deop(victim, chan)) {
  467. dprintf(idx, "%s is currently being auto-deopped on %s.\n", m->nick, chan->dname);
  468. if (all) goto next;
  469. return;
  470. }
  471. if (chan_bitch(chan) && !chk_op(victim, chan)) {
  472. dprintf(idx, "%s is not a registered op on %s.\n", m->nick, chan->dname);
  473. if (all) goto next;
  474. return;
  475. }
  476. if (do_op(m, chan, 0, 1)) {
  477. dprintf(idx, "Gave op to %s on %s.\n", nick, chan->dname);
  478. stats_add(u, 0, 1);
  479. }
  480. next:;
  481. if (!all)
  482. chan = NULL;
  483. else
  484. chan = chan->next;
  485. }
  486. }
  487. static void mass_mode(const char* chname, const char* mode, char *par)
  488. {
  489. char list[101] = "", buf[2048] = "";
  490. size_t list_len = 0, buf_len = 0, cnt = 0;
  491. const unsigned short max_lines = floodless ? 15 : default_alines;
  492. unsigned short lines = 0;
  493. while (par[0]) {
  494. *(buf + buf_len++) = 'M';
  495. *(buf + buf_len++) = 'O';
  496. *(buf + buf_len++) = 'D';
  497. *(buf + buf_len++) = 'E';
  498. *(buf + buf_len++) = ' ';
  499. buf_len += strlcpy(buf + buf_len, chname, sizeof(buf) - buf_len);
  500. *(buf + buf_len++) = ' ';
  501. while ((cnt < modesperline) && par[0]) {
  502. /* add +mode into irc cmd */
  503. buf_len += strlcpy(buf + buf_len, mode, sizeof(buf) - buf_len);
  504. /* Make list of nicks */
  505. const char* nick = newsplit(&par);
  506. list_len += strlcpy(list + list_len, nick, sizeof(list) - list_len);
  507. if ((++cnt < modesperline) && par[0])
  508. *(list + list_len++) = ' ';
  509. }
  510. /* buf is so far: 'MODE #chan +o+o+o+o' */
  511. *(buf + buf_len++) = ' ';
  512. buf_len += strlcpy(buf + buf_len, list, sizeof(buf) - buf_len);
  513. *(buf + buf_len++) = '\r';
  514. *(buf + buf_len++) = '\n';
  515. if (++lines >= max_lines) {
  516. buf[buf_len] = 0;
  517. tputs(serv, buf, buf_len);
  518. buf[0] = buf_len = lines = 0;
  519. }
  520. list[0] = list_len = cnt = 0;
  521. }
  522. if (buf[0]) {
  523. buf[buf_len] = 0;
  524. tputs(serv, buf, buf_len);
  525. }
  526. }
  527. void mass_request(char *botnick, char *code, char *par)
  528. {
  529. char* mode = newsplit(&par);
  530. if (strlen(mode) > 2 || !strchr("+-kb", mode[0]))
  531. return;
  532. char* chname = newsplit(&par);
  533. if (strchr("+-", mode[0]))
  534. mass_mode(chname, mode, par);
  535. }
  536. static void cmd_mmode(int idx, char *par)
  537. {
  538. char *mode = newsplit(&par);
  539. if (strlen(mode) > 2 || !strchr("+-", mode[0]) || !par[0]) {
  540. dprintf(idx, "Usage: mmode <(+|-)MODE> <#channel> <a|o|v|d|r> [bots=n] [alines=n] [slines=n] [overlap=n] [bitch] [botbitch] [simul] [local]\n");
  541. dprintf(idx, "Ie. mmode -o #chan a\n");
  542. return;
  543. }
  544. putlog(LOG_CMDS, "*", "#%s# mmode %s %s", dcc[idx].nick, mode, par);
  545. char *chname = NULL;
  546. if (strchr(CHANMETA, par[0]) != NULL)
  547. chname = newsplit(&par);
  548. else
  549. chname = NULL;
  550. struct chanset_t *chan = get_channel(idx, chname);
  551. if (chan) {
  552. if (!shouldjoin(chan) || !channel_active(chan)) {
  553. dprintf(idx, "I'm not on %s\n", chan->dname);
  554. return;
  555. }
  556. if (channel_pending(chan)) {
  557. dprintf(idx, "Channel pending.\n");
  558. return;
  559. }
  560. get_user_flagrec(dcc[idx].user, &user, chan->dname, chan);
  561. if (!glob_owner(user) && !chan_owner(user)) {
  562. dprintf(idx, "You do not have mass mode access for %s\n", chan->dname);
  563. return;
  564. }
  565. }
  566. if (!chan || !chname || !chname[0]) {
  567. dprintf(idx, "Usage: mmode <(+|-)MODE> <#channel> <a|o|v|d|r> [bots=n] [alines=n] [slines=n] [overlap=n] [bitch] [botbitch] [simul] [local]\n");
  568. dprintf(idx, "Ie. mmode -o #chan a\n");
  569. return;
  570. }
  571. char *who = newsplit(&par);
  572. if (strlen(who) > 1 || !strchr("aovdr", who[0])) {
  573. dprintf(idx, "Usage: mmode <(+|-)MODE> <#channel> <a|o|v|d|r> [bots=n] [alines=n] [slines=n] [overlap=n] [bitch] [botbitch] [simul] [local]\n");
  574. dprintf(idx, "Ie. mmode -o #chan a\n");
  575. dprintf(idx, "a = all.\n");
  576. dprintf(idx, "o = ops.\n");
  577. dprintf(idx, "O = user-op.\n");
  578. dprintf(idx, "v = voices.\n");
  579. dprintf(idx, "d = non-ops.\n");
  580. dprintf(idx, "r = regulars (-ov).\n");
  581. return;
  582. }
  583. if (mode[0] == '+' && mode[1] == 'o' && !channel_fastop(chan) && !cookies_disabled) {
  584. dprintf(idx, STR("Error: This channel is currently set -fastop.\n"));
  585. dprintf(idx, STR("Mass opping would result in missing op cookies.\n"));
  586. dprintf(idx, STR("Please chanset the channel +fastop first.\n"));
  587. return;
  588. }
  589. memberlist** targets = (memberlist**) calloc(1, chan->channel.members * sizeof(memberlist *));
  590. int* overlaps = (int *) calloc(1, chan->channel.members * sizeof(int *));
  591. memberlist** chanbots = (memberlist **) calloc(1, chan->channel.members * sizeof(memberlist *));
  592. memberlist *m = NULL;
  593. int chanbotcount = 0, targetcount = 0;
  594. for (m = chan->channel.member; m; m = m->next) {
  595. if (m->user) {
  596. if ((m->flags & CHANOP) && m->user->bot &&
  597. (!m->is_me) && (nextbot(m->user->handle) >= 0)) {
  598. chanbots[chanbotcount++] = m;
  599. continue;
  600. } else if (m->is_me)
  601. continue;
  602. get_user_flagrec(m->user, &user, chan->dname);
  603. }
  604. bool is_target = 0;
  605. if (who[0] == 'o' && (m->flags & CHANOP))
  606. is_target = 1;
  607. else if (who[0] == 'O' && chk_op(user, chan))
  608. is_target = 1;
  609. else if (who[0] == 'd' && !(m->flags & CHANOP))
  610. is_target = 1;
  611. else if (who[0] == 'v' && !(m->flags & CHANOP) && (m->flags & CHANVOICE))
  612. is_target = 1;
  613. else if (who[0] == 'r' && !(m->flags & CHANOP) && !(m->flags & CHANVOICE))
  614. is_target = 1;
  615. else if (who[0] == 'a')
  616. is_target = 1;
  617. if (is_target)
  618. targets[targetcount++] = m;
  619. }
  620. int force_bots = 0, force_alines = 0, force_slines = 0, force_overlap = 0;
  621. bool bitch = 0, botbitch = 0, simul = 0, local = 0;
  622. while (par && par[0]) {
  623. char *p = newsplit(&par);
  624. if (!strncasecmp(p, "bots=", 5)) {
  625. p += 5;
  626. force_bots = atoi(p);
  627. if ((force_bots < 1) || (force_bots > chanbotcount)) {
  628. dprintf(idx, "bots must be within 1-%i\n", chanbotcount);
  629. free(targets);
  630. free(overlaps);
  631. free(chanbots);
  632. return;
  633. }
  634. } else if (!strncasecmp(p, "alines=", 7)) {
  635. p += 7;
  636. force_alines = atoi(p);
  637. if (force_alines > 5)
  638. dprintf(idx, "alines >5 will fail without a floodless.\n");
  639. if ((force_alines < 1) || (force_alines > 100)) {
  640. dprintf(idx, "alines must be within 1-100\n");
  641. free(targets);
  642. free(overlaps);
  643. free(chanbots);
  644. return;
  645. }
  646. } else if (!strncasecmp(p, "slines=", 7)) {
  647. p += 7;
  648. force_slines = atoi(p);
  649. if ((force_slines < 1) || (force_slines > 20)) {
  650. dprintf(idx, "slines must be within 1-20\n");
  651. dprintf(idx, ">6 will fail without a floodless.\n");
  652. free(targets);
  653. free(overlaps);
  654. free(chanbots);
  655. return;
  656. }
  657. } else if (!strncasecmp(p, "overlap=", 8)) {
  658. p += 8;
  659. force_overlap = atoi(p);
  660. if ((force_overlap < 1) || (force_overlap > 8)) {
  661. dprintf(idx, "overlap must be within 1-8\n");
  662. free(targets);
  663. free(overlaps);
  664. free(chanbots);
  665. return;
  666. }
  667. } else if (!strncasecmp(p, "bitch", 5)) {
  668. bitch = 1;
  669. } else if (!strncasecmp(p, "botbitch", 8)) {
  670. botbitch = 1;
  671. } else if (!strncasecmp(p, "simul", 5)) {
  672. simul = 1;
  673. } else if (!strncasecmp(p, "local", 5)) {
  674. local = 1;
  675. } else {
  676. dprintf(idx, "Unrecognized option %s\n", p);
  677. free(targets);
  678. free(overlaps);
  679. free(chanbots);
  680. return;
  681. }
  682. }
  683. if (!targetcount) {
  684. dprintf(idx, "No targets found on %s\n", chan->name);
  685. free(targets);
  686. free(overlaps);
  687. free(chanbots);
  688. return;
  689. }
  690. if ((!chanbotcount && !local) || (local && !me_op(chan))) {
  691. dprintf(idx, "No bots opped on %s\n", chan->name);
  692. free(targets);
  693. free(overlaps);
  694. free(chanbots);
  695. return;
  696. }
  697. if (local)
  698. force_bots = 1;
  699. if (local && floodless)
  700. force_alines = force_slines = 100;
  701. int overlap = (force_overlap ? force_overlap : 1);
  702. int needed_modes = (overlap * targetcount);
  703. int my_max_modes = ((force_bots ? force_bots : chanbotcount) * (force_alines ? force_alines : default_alines) * modesperline);
  704. if (needed_modes > my_max_modes) {
  705. dprintf(idx, "Need to make %d modes, but the max is %d.\n", needed_modes, my_max_modes);
  706. dprintf(idx, "Try increasing [alines] or decreasing [overlap].\n");
  707. if (overlap == 1)
  708. dprintf(idx, "Not enough bots.\n");
  709. else
  710. dprintf(idx, "Not enough bots. Try with overlap=1\n");
  711. free(targets);
  712. free(overlaps);
  713. free(chanbots);
  714. return;
  715. }
  716. double bots = 0;
  717. int amodes = 0;
  718. /* ok it's possible... now let's figure out how */
  719. if (force_bots && force_alines) {
  720. /* not much choice... overlap should not autochange */
  721. bots = force_bots;
  722. amodes = force_alines * modesperline;
  723. } else {
  724. if (force_bots) {
  725. /* calc needed modes per bot */
  726. bots = force_bots;
  727. amodes = (needed_modes + ((int)bots - 1)) / (int)bots;
  728. } else if (force_alines) {
  729. amodes = force_alines * modesperline;
  730. bots = (double) needed_modes / amodes;
  731. if (bots > (int)bots) bots = (int)bots+1; /* ceil(bots) */
  732. } else {
  733. /* Try minimizing alines, 1,2,3,4,5 against bots needed */
  734. amodes = 1 * modesperline;
  735. bots = (double) needed_modes / amodes;
  736. if (bots > (int)bots) bots = (int)bots+1; /* ceil(bots) */
  737. for (int min_alines = 2; min_alines <= 5; ++min_alines) {
  738. if ((int)bots > chanbotcount) {
  739. amodes = min_alines * modesperline;
  740. bots = (double) needed_modes / amodes;
  741. if (bots > (int)bots) bots = (int)bots+1; /* ceil(bots) */
  742. } else
  743. break;
  744. }
  745. force_slines = (amodes / modesperline);
  746. /* lol einride is dumb */
  747. if ((int)bots > chanbotcount) {
  748. putlog(LOG_MISC, "*", "Totally fucked calculations in cmd_mmode. this CAN'T happen.");
  749. dprintf(idx, "Something's wrong... bug the coder\n");
  750. free(targets);
  751. free(overlaps);
  752. free(chanbots);
  753. return;
  754. }
  755. }
  756. }
  757. /* How many modes to send */
  758. int smodes;
  759. if (force_slines)
  760. smodes = force_slines * modesperline;
  761. else
  762. smodes = default_alines * modesperline;
  763. if (smodes < amodes)
  764. smodes = amodes;
  765. dprintf(idx, "Mass %s of %s\n", mode, chan->dname);
  766. dprintf(idx, " %d bots used.\n", (int)bots);
  767. dprintf(idx, " %d targets.\n", targetcount);
  768. dprintf(idx, " %d assumed modes per participating bot.\n", amodes);
  769. dprintf(idx, " %d assumed mode lines per participating bot.\n", amodes / modesperline);
  770. dprintf(idx, " %d max modes per participating bot.\n", smodes);
  771. dprintf(idx, " %d max mode lines per participating bot.\n", smodes / modesperline);
  772. dprintf(idx, " %d assumed modes per target nick.\n", overlap);
  773. int tpos = 0, bpos = 0, i = 0;
  774. char **work_list = (char**) calloc((int)bots, sizeof(char**));
  775. char *work = NULL;
  776. size_t work_size = 2048;
  777. /* now use bots/modes to distribute nicks to MODE on */
  778. while (bots) {
  779. work = work_list[bpos] = (char*) calloc(1, work_size);
  780. if (local) {
  781. strlcpy(work, mode, work_size);
  782. strlcat(work, " ", work_size);
  783. strlcat(work, chan->dname, work_size);
  784. } else if (!simul)
  785. simple_snprintf(work, work_size, "mr %s %s", mode, chan->dname);
  786. else
  787. work[0] = 0;
  788. /* Make list of assumed lines (alines) */
  789. for (i = 0; i < amodes; ++i) {
  790. if (overlaps[tpos]++ < overlap) {
  791. strlcat(work, " ", work_size);
  792. strlcat(work, targets[tpos]->nick, work_size);
  793. }
  794. if (++tpos >= targetcount)
  795. tpos = 0;
  796. }
  797. /* Now make lines of throttled modes (send lines) (slines) */
  798. if (smodes > amodes) {
  799. int atpos = tpos;
  800. for (i = 0; i < (smodes - amodes); ++i) {
  801. if (overlaps[atpos]++ < overlap) {
  802. strlcat(work, " ", work_size);
  803. strlcat(work, targets[atpos]->nick, work_size);
  804. }
  805. if (++atpos >= targetcount)
  806. atpos = 0;
  807. }
  808. }
  809. --bots;
  810. ++bpos;
  811. }
  812. bots = bpos;
  813. bpos = 0;
  814. /* QUEUE *ALL*, then dump */
  815. while (bots) {
  816. work = work_list[bpos];
  817. if (simul)
  818. dprintf(idx, "%-10s MODE %s %s\n", local ? botname : chanbots[bpos]->nick, mode, work);
  819. else if (local)
  820. mass_request(conf.bot->nick, "mr", work);
  821. else
  822. putbot(chanbots[bpos]->user->handle, work);
  823. free(work_list[bpos]);
  824. --bots;
  825. ++bpos;
  826. }
  827. free(work_list);
  828. if (bitch && !simul) {
  829. chan->status |= CHAN_BITCH;
  830. do_chanset(NULL, chan, "+bitch", DO_LOCAL | DO_NET);
  831. }
  832. if (botbitch && !simul) {
  833. chan->status |= CHAN_BOTBITCH;
  834. do_chanset(NULL, chan, "+botbitch", DO_LOCAL | DO_NET);
  835. }
  836. free(targets);
  837. free(overlaps);
  838. free(chanbots);
  839. return;
  840. }
  841. static void cmd_deop(int idx, char *par)
  842. {
  843. char *nick = newsplit(&par), *chname = newsplit(&par);
  844. if (strchr(CHANMETA, nick[0]) || (!chname[0] && !strcmp(nick, "*")))
  845. swap(nick, chname);
  846. bool all = 0;
  847. struct chanset_t* chan = get_channel(idx, chname, 1, &all);
  848. if (!all && !chan)
  849. return;
  850. memberlist *m = NULL;
  851. struct userrec *u = NULL;
  852. if (all)
  853. chan = chanset;
  854. putlog(LOG_CMDS, "*", "#%s# (%s) deop %s", dcc[idx].nick, all ? "*" : chan->dname, nick);
  855. while (chan) {
  856. get_user_flagrec(dcc[idx].user, &user, chan->dname);
  857. if (!nick[0] && !(nick = getnick(dcc[idx].nick, chan))[0]) {
  858. if (all) goto next;
  859. dprintf(idx, "Usage: deop <nick> [channel|*]\n");
  860. return;
  861. }
  862. if (privchan(user, chan, PRIV_OP)) {
  863. if (all) goto next;
  864. dprintf(idx, "No such channel.\n");
  865. }
  866. else if (!real_chk_op(user, chan, 0)) {
  867. if (all) goto next;
  868. dprintf(idx, "You don't have access to deop on %s\n", chan->dname);
  869. return;
  870. }
  871. if (!channel_active(chan)) {
  872. if (all) goto next;
  873. dprintf(idx, "I'm not on %s right now!\n", chan->dname);
  874. return;
  875. }
  876. if (!me_op(chan)) {
  877. if (all) goto next;
  878. dprintf(idx, "I can't help you now because I'm not a chan op on %s.\n",
  879. chan->dname);
  880. return;
  881. }
  882. m = ismember(chan, nick);
  883. if (!m) {
  884. if (all) goto next;
  885. dprintf(idx, "%s is not on %s.\n", nick, chan->dname);
  886. return;
  887. }
  888. if (match_my_nick(nick)) {
  889. if (all) goto next;
  890. dprintf(idx, "I'm not going to deop myself.\n");
  891. return;
  892. }
  893. member_getuser(m);
  894. u = m->user;
  895. get_user_flagrec(u, &victim, chan->dname);
  896. if ((chan_master(victim) || glob_master(victim)) &&
  897. !(chan_owner(user) || glob_owner(user))) {
  898. dprintf(idx, "%s is a master for %s.\n", m->nick, chan->dname);
  899. if (all) goto next;
  900. return;
  901. }
  902. if (glob_bot(victim)) {
  903. dprintf(idx, "%s is another channel bot!\n", nick);
  904. return;
  905. }
  906. if (chk_op(victim, chan) && !(chan_master(user) || glob_master(user))) {
  907. dprintf(idx, "%s has the op flag for %s.\n", m->nick, chan->dname);
  908. if (all) goto next;
  909. return;
  910. }
  911. add_mode(chan, '-', 'o', m);
  912. dprintf(idx, "Deopped %s on %s.\n", nick, chan->dname);
  913. next:;
  914. if (!all)
  915. chan = NULL;
  916. else
  917. chan = chan->next;
  918. }
  919. }
  920. static void cmd_kick(int idx, char *par)
  921. {
  922. if (!par[0]) {
  923. dprintf(idx, "Usage: kick [channel|*] <nick> [reason]\n");
  924. return;
  925. }
  926. char *chname = NULL;
  927. bool all = 0;
  928. if (strchr(CHANMETA, par[0]))
  929. chname = newsplit(&par);
  930. else
  931. chname = NULL;
  932. struct chanset_t* chan = get_channel(idx, chname, 1, &all);
  933. if (!all && !chan)
  934. return;
  935. putlog(LOG_CMDS, "*", "#%s# (%s) kick %s", dcc[idx].nick, all ? "*" : chan->dname, par);
  936. char *nick = newsplit(&par);
  937. if (!par[0])
  938. par = "requested";
  939. if (match_my_nick(nick)) {
  940. dprintf(idx, "I'm not going to kick myself.\n");
  941. return;
  942. }
  943. memberlist *m = NULL;
  944. struct userrec *u = NULL;
  945. if (all)
  946. chan = chanset;
  947. while (chan) {
  948. get_user_flagrec(dcc[idx].user, &user, chan->dname);
  949. if (privchan(user, chan, PRIV_OP)) {
  950. if (all) goto next;
  951. dprintf(idx, "No such channel.\n");
  952. return;
  953. }
  954. else if (!real_chk_op(user, chan, 0)) {
  955. if (all) goto next;
  956. dprintf(idx, "You don't have access to kick on %s\n", chan->dname);
  957. return;
  958. }
  959. if (!channel_active(chan)) {
  960. if (all) goto next;
  961. dprintf(idx, "I'm not on %s right now!\n", chan->dname);
  962. return;
  963. }
  964. if (!me_op(chan)) {
  965. if (all) goto next;
  966. dprintf(idx, "I can't help you now because I'm not a channel op on %s.\n", chan->dname);
  967. return;
  968. }
  969. m = ismember(chan, nick);
  970. if (!m) {
  971. if (all) goto next;
  972. dprintf(idx, "%s is not on %s\n", nick, chan->dname);
  973. return;
  974. }
  975. member_getuser(m);
  976. u = m->user;
  977. get_user_flagrec(u, &victim, chan->dname);
  978. if (chk_op(victim, chan) && !(chan_master(user) || glob_master(user))) {
  979. if (all) goto next;
  980. dprintf(idx, "%s is a legal op.\n", nick);
  981. return;
  982. }
  983. if ((chan_master(victim) || glob_master(victim)) &&
  984. !(glob_owner(user) || chan_owner(user))) {
  985. if (all) goto next;
  986. dprintf(idx, "%s is a %s master.\n", nick, chan->dname);
  987. return;
  988. }
  989. if (glob_bot(victim)) {
  990. dprintf(idx, "%s is another channel bot!\n", nick);
  991. return;
  992. }
  993. dprintf(DP_SERVER, "KICK %s %s :%s%s\n", chan->name, m->nick, kickprefix, par);
  994. m->flags |= SENTKICK;
  995. dprintf(idx, "Kicked %s on %s.\n", nick, chan->dname);
  996. next:;
  997. if (!all)
  998. chan = NULL;
  999. else
  1000. chan = chan->next;
  1001. }
  1002. }
  1003. static void cmd_getkey(int idx, char *par)
  1004. {
  1005. struct chanset_t *chan = NULL;
  1006. chan = get_channel(idx, par);
  1007. if (!chan || !has_op(idx, chan))
  1008. return;
  1009. putlog(LOG_CMDS, "*", "#%s getkey %s", dcc[idx].nick, par);
  1010. get_user_flagrec(dcc[idx].user, &user, chan->dname);
  1011. if (privchan(user, chan, PRIV_OP)) {
  1012. dprintf(idx, "No such channel.\n");
  1013. return;
  1014. }
  1015. else if (!real_chk_op(user, chan, 0)) {
  1016. dprintf(idx, "You don't have access for %s\n", chan->dname);
  1017. return;
  1018. }
  1019. if (!(channel_pending(chan) || channel_active(chan))) {
  1020. dprintf(idx, "I'm not on %s right now.\n", chan->dname);
  1021. return;
  1022. }
  1023. char outbuf[201] = "";
  1024. if (!chan->channel.key[0])
  1025. simple_snprintf(outbuf, sizeof(outbuf), "%s has no key set.", chan->dname);
  1026. else
  1027. simple_snprintf(outbuf, sizeof(outbuf), "Key for %s is: %s", chan->dname, chan->channel.key);
  1028. if (chan->key_prot[0]) {
  1029. size_t olen = strlen(outbuf);
  1030. simple_snprintf(&outbuf[olen], sizeof(outbuf) - olen, " (Enforcing +k %s)", chan->key_prot);
  1031. }
  1032. dprintf(idx, "%s\n", outbuf);
  1033. }
  1034. static void cmd_mop(int idx, char *par)
  1035. {
  1036. bool found = 0, all = 0;
  1037. char *chname = newsplit(&par);
  1038. struct chanset_t* chan = get_channel(idx, chname, 1, &all);
  1039. if (all) {
  1040. get_user_flagrec(dcc[idx].user, &user, NULL);
  1041. if (!glob_owner(user)) {
  1042. dprintf(idx, "You do not have access to mop '*'\n");
  1043. return;
  1044. }
  1045. chan = chanset;
  1046. }
  1047. if (!chan && !all) {
  1048. dprintf(idx, "Usage: mop <channel|*>\n");
  1049. return;
  1050. }
  1051. putlog(LOG_CMDS, "*", "#%s# (%s) mop %s", dcc[idx].nick, all ? "*" : chan->dname, par);
  1052. memberlist *m = NULL;
  1053. while (chan) {
  1054. get_user_flagrec(dcc[idx].user, &user, chan->dname);
  1055. if (privchan(user, chan, PRIV_OP)) {
  1056. if (all) goto next;
  1057. dprintf(idx, "No such channel.\n");
  1058. return;
  1059. }
  1060. if (!chk_op(user, chan)) {
  1061. if (all) goto next;
  1062. dprintf(idx, "You are not a channel op on %s.\n", chan->dname);
  1063. return;
  1064. }
  1065. if (!me_op(chan)) {
  1066. if (all) goto next;
  1067. dprintf(idx, "I am not opped on %s.\n", chan->dname);
  1068. return;
  1069. }
  1070. if (channel_active(chan) && !channel_pending(chan)) {
  1071. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1072. member_getuser(m);
  1073. if (m->user && u_pass_match(m->user, "-"))
  1074. continue; /* dont op users without a pass */
  1075. get_user_flagrec(m->user, &victim, chan->dname);
  1076. if (!chan_hasop(m) && !glob_bot(victim) && chk_op(victim, chan)) {
  1077. found = 1;
  1078. dprintf(idx, "Gave op to '%s' as '%s' on %s\n", m->user->handle, m->nick, chan->dname);
  1079. do_op(m, chan, 0, 0);
  1080. }
  1081. }
  1082. } else {
  1083. if (!all)
  1084. dprintf(idx, "Channel %s is not active or is pending.\n", chan->dname);
  1085. return;
  1086. }
  1087. if (!found && !all)
  1088. dprintf(idx, "No one to op on %s\n", chan->dname);
  1089. next:;
  1090. if (!all)
  1091. chan = NULL;
  1092. else
  1093. chan = chan->next;
  1094. }
  1095. }
  1096. static void cmd_find(int idx, char *par)
  1097. {
  1098. if (!par[0]) {
  1099. dprintf(idx, "Usage: find <nick!ident@host.com>|<user> (wildcard * allowed)\n");
  1100. return;
  1101. }
  1102. putlog(LOG_CMDS, "*", "#%s# find %s", dcc[idx].nick, par);
  1103. struct chanset_t *chan = NULL, **cfound = NULL;
  1104. memberlist *m = NULL, **found = NULL;
  1105. int fcount = 0;
  1106. bool tr = 0, lookup_user = 0;
  1107. struct userrec *u = NULL;
  1108. if (!strchr(par, '!')) {
  1109. lookup_user = 1;
  1110. u = get_user_by_handle(userlist, par);
  1111. if (!u) {
  1112. dprintf(idx, "No such user: %s\n", par);
  1113. return;
  1114. }
  1115. }
  1116. /* make a list of members in found[] */
  1117. for (chan = chanset; chan; chan = chan->next) {
  1118. get_user_flagrec(dcc[idx].user, &user, chan->dname);
  1119. if (!privchan(user, chan, PRIV_OP)) {
  1120. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1121. member_getuser(m, 1);
  1122. if ((!lookup_user && wild_match(par, m->from)) || (lookup_user && m->user == u)) {
  1123. fcount++;
  1124. if (!found) {
  1125. found = (memberlist **) calloc(1, sizeof(memberlist *) * 100);
  1126. cfound = (struct chanset_t **) calloc(1, sizeof(struct chanset_t *) * 100);
  1127. }
  1128. found[fcount - 1] = m;
  1129. cfound[fcount - 1] = chan;
  1130. if (fcount == 100) {
  1131. tr = 1;
  1132. break;
  1133. }
  1134. }
  1135. }
  1136. }
  1137. if (fcount == 100) {
  1138. tr = 1;
  1139. break;
  1140. }
  1141. }
  1142. if (fcount) {
  1143. char tmp[1024] = "";
  1144. int findex, i;
  1145. for (findex = 0; findex < fcount; findex++) {
  1146. if (found[findex]) {
  1147. simple_snprintf(tmp, sizeof(tmp), "%s!%s %s%s%s on %s", found[findex]->nick, found[findex]->userhost,
  1148. found[findex]->user ? "(user:" : "", found[findex]->user ? found[findex]->user->handle : "", found[findex]->user ? ")" : "",
  1149. cfound[findex]->name);
  1150. for (i = findex + 1; i < fcount; i++) {
  1151. if (found[i] && (!strcmp(found[i]->nick, found[findex]->nick))) {
  1152. strlcat(tmp, ", ", sizeof(tmp));
  1153. strlcat(tmp, cfound[i]->name, sizeof(tmp));
  1154. found[i] = NULL;
  1155. }
  1156. }
  1157. dprintf(idx, "%s\n", tmp);
  1158. }
  1159. }
  1160. free(found);
  1161. free(cfound);
  1162. } else {
  1163. dprintf(idx, "No matches for %s on any channels.\n", par);
  1164. }
  1165. if (tr)
  1166. dprintf(idx, "(more than 100 matches; list truncated)\n");
  1167. dprintf(idx, "--- Found %d matches.\n", fcount);
  1168. }
  1169. static void do_invite(int idx, char *par, bool op)
  1170. {
  1171. char *nick = newsplit(&par), *chname = newsplit(&par);
  1172. if (!nick[0]) {
  1173. dprintf(idx, "Usage: invite <nickname> [channel|*]\n");
  1174. return;
  1175. }
  1176. if (strchr(CHANMETA, nick[0]) || (!chname[0] && !strcmp(nick, "*")))
  1177. swap(nick, chname);
  1178. bool all = 0;
  1179. struct chanset_t* chan = get_channel(idx, chname, 1, &all);
  1180. if (!all && !chan)
  1181. return;
  1182. memberlist *m = NULL;
  1183. if (all)
  1184. chan = chanset;
  1185. putlog(LOG_CMDS, "*", "#%s# (%s) %s %s", dcc[idx].nick, all ? "*" : chan->dname, op ? "iop" : "invite", nick);
  1186. while (chan) {
  1187. get_user_flagrec(dcc[idx].user, &user, chan->dname);
  1188. if (privchan(user, chan, PRIV_OP)) {
  1189. if (all) goto next;
  1190. dprintf(idx, "No such channel.\n");
  1191. }
  1192. else if (!real_chk_op(user, chan, 0)) {
  1193. if (all) goto next;
  1194. dprintf(idx, "You don't have access to invite to %s\n", chan->dname);
  1195. return;
  1196. }
  1197. if (!me_op(chan)) {
  1198. if (all) goto next;
  1199. if (chan->channel.mode & CHANINV) {
  1200. dprintf(idx, "I can't help you now because I'm not a channel op on %s", chan->dname);
  1201. return;
  1202. }
  1203. if (!channel_active(chan)) {
  1204. dprintf(idx, "I'm not on %s right now!\n", chan->dname);
  1205. return;
  1206. }
  1207. }
  1208. m = ismember(chan, nick);
  1209. if (m && !chan_issplit(m)) {
  1210. if (all) goto next;
  1211. dprintf(idx, "%s is already on %s!\n", nick, chan->dname);
  1212. return;
  1213. }
  1214. cache_invite(chan, nick, NULL, NULL, op, 0);
  1215. dprintf(idx, "Inviting %s to %s.\n", nick, chan->dname);
  1216. next:;
  1217. if (!all)
  1218. chan = NULL;
  1219. else
  1220. chan = chan->next;
  1221. }
  1222. }
  1223. static void cmd_invite(int idx, char *par)
  1224. {
  1225. do_invite(idx, par, 0);
  1226. }
  1227. #ifdef CACHE
  1228. static void cmd_iop(int idx, char *par)
  1229. {
  1230. do_invite(idx, par, 1);
  1231. }
  1232. #endif /* CACHE */
  1233. static void cmd_authed(int idx, char *par)
  1234. {
  1235. putlog(LOG_CMDS, "*", STR("#%s# authed"), dcc[idx].nick);
  1236. dprintf(idx, STR("Authed:\n"));
  1237. Auth::TellAuthed(idx);
  1238. }
  1239. static void cmd_channel(int idx, char *par)
  1240. {
  1241. struct chanset_t *chan = NULL;
  1242. chan = get_channel(idx, par);
  1243. if (!chan || !has_op(idx, chan))
  1244. return;
  1245. char handle[HANDLEN + 1] = "", s[UHOSTLEN] = "", s1[UHOSTLEN] = "", atrflag = 0, chanflag[2] = "";
  1246. memberlist *m = NULL;
  1247. size_t maxnicklen, maxhandlen;
  1248. char format[81] = "";
  1249. get_user_flagrec(dcc[idx].user, &user, chan->dname);
  1250. putlog(LOG_CMDS, "*", "#%s# (%s) channel", dcc[idx].nick, chan->dname);
  1251. strlcpy(s, getchanmode(chan), sizeof s);
  1252. if (channel_pending(chan)) {
  1253. simple_snprintf(s1, sizeof s1, "Processing channel %s", chan->dname);
  1254. } else if (channel_active(chan)) {
  1255. simple_snprintf(s1, sizeof s1, "Channel %s", chan->dname);
  1256. } else {
  1257. simple_snprintf(s1, sizeof s1, "Desiring channel %s", chan->dname);
  1258. }
  1259. dprintf(idx, "%s, %d member%s, mode %s:\n", s1, chan->channel.members,
  1260. chan->channel.members == 1 ? "" : "s", s);
  1261. if (chan->channel.splitmembers)
  1262. dprintf(idx, "%d split members\n", chan->channel.splitmembers);
  1263. if (chan->channel.topic)
  1264. dprintf(idx, "%s: %s\n", "Channel Topic", chan->channel.topic);
  1265. if (channel_active(chan)) {
  1266. /* find max nicklen and handlen */
  1267. maxnicklen = maxhandlen = 0;
  1268. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1269. if (strlen(m->nick) > maxnicklen)
  1270. maxnicklen = strlen(m->nick);
  1271. if (m->user)
  1272. if (strlen(m->user->handle) > maxhandlen)
  1273. maxhandlen = strlen(m->user->handle);
  1274. }
  1275. if (maxnicklen < 9) maxnicklen = 9;
  1276. if (maxhandlen < 9) maxhandlen = 9;
  1277. dprintf(idx, "(n = owner, m = master, o = op, d = deop, b = bot) CAP:global\n");
  1278. simple_snprintf(format, sizeof format, " %%-%zus %%-%zus %%-6s %%-4s %%-5s %%s %%s\n",
  1279. maxnicklen, maxhandlen);
  1280. dprintf(idx, format, "NICKNAME", "HANDLE", " JOIN", " HOPS", "IDLE", "USER@HOST", "USER@IP");
  1281. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  1282. if (m->joined > 0) {
  1283. if ((now - (m->joined)) > 86400)
  1284. strftime(s, 6, "%d%b", gmtime(&(m->joined)));
  1285. else
  1286. strftime(s, 6, "%H:%M", gmtime(&(m->joined)));
  1287. } else
  1288. strlcpy(s, " --- ", sizeof s);
  1289. // Force +r (re)processing
  1290. if (!m->userip[0] && doresolv(chan)) {
  1291. char host[UHOSTLEN] = "", *p = NULL;
  1292. p = strchr(m->userhost, '@');
  1293. if (p) {
  1294. ++p;
  1295. strlcpy(host, p, strlen(m->userhost) - (p - host));
  1296. resolve_to_member(chan, m->nick, host);
  1297. }
  1298. }
  1299. member_getuser(m, 1);
  1300. if (m->user == NULL)
  1301. strlcpy(handle, "*", sizeof handle);
  1302. else
  1303. strlcpy(handle, m->user->handle, sizeof handle);
  1304. get_user_flagrec(m->user, &user, chan->dname);
  1305. /* Determine status char to use */
  1306. if (glob_bot(user) && chk_op(user, chan))
  1307. atrflag = 'B';
  1308. else if (glob_bot(user))
  1309. atrflag = 'b';
  1310. else if (glob_owner(user))
  1311. atrflag = 'N';
  1312. else if (chan_owner(user))
  1313. atrflag = 'n';
  1314. else if (glob_master(user))
  1315. atrflag = 'M';
  1316. else if (chan_master(user))
  1317. atrflag = 'm';
  1318. else if (glob_deop(user))
  1319. atrflag = 'D';
  1320. else if (chan_deop(user))
  1321. atrflag = 'd';
  1322. else if (glob_autoop(user))
  1323. atrflag = 'A';
  1324. else if (chan_autoop(user))
  1325. atrflag = 'a';
  1326. else if (glob_op(user) && !privchan(user, chan, PRIV_OP))
  1327. atrflag = 'O';
  1328. else if (chan_op(user))
  1329. atrflag = 'o';
  1330. else if (glob_quiet(user))
  1331. atrflag = 'Q';
  1332. else if (chan_quiet(user))
  1333. atrflag = 'q';
  1334. else if (glob_voice(user) && !privchan(user, chan, PRIV_VOICE))
  1335. atrflag = 'V';
  1336. else if (chan_voice(user))
  1337. atrflag = 'v';
  1338. else if (glob_kick(user))
  1339. atrflag = 'K';
  1340. else if (chan_kick(user))
  1341. atrflag = 'k';
  1342. else if (glob_wasoptest(user))
  1343. atrflag = 'W';
  1344. else if (chan_wasoptest(user))
  1345. atrflag = 'w';
  1346. else
  1347. atrflag = ' ';
  1348. if (chan_hasop(m))
  1349. chanflag[1] = '@';
  1350. else if (chan_hasvoice(m))
  1351. chanflag[1] = '+';
  1352. else
  1353. chanflag[1] = ' ';
  1354. if (m->flags & OPER)
  1355. chanflag[0] = 'O';
  1356. else
  1357. chanflag[0] = ' ';
  1358. if (chan_issplit(m)) {
  1359. simple_snprintf(format, sizeof format,
  1360. "%%c%%c%%-%zus %%-%zus %%s %%c <- netsplit, %%lus\n",
  1361. maxnicklen, maxhandlen);
  1362. dprintf(idx, format, chanflag[0],chanflag[1], m->nick, handle, s, atrflag,
  1363. now - (m->split));
  1364. } else if (m->is_me) {
  1365. simple_snprintf(format, sizeof format,
  1366. "%%c%%c%%-%zus %%-%zus %%s %%c <- it's me!\n",
  1367. maxnicklen, maxhandlen);
  1368. dprintf(idx, format, chanflag[0], chanflag[1], m->nick, handle, s, atrflag);
  1369. } else {
  1370. /* Determine idle time */
  1371. if (now - (m->last) > 86400)
  1372. simple_snprintf(s1, sizeof s1, "%2dd", (int) ((now - (m->last)) / 86400));
  1373. else if (now - (m->last) > 3600)
  1374. simple_snprintf(s1, sizeof s1, "%2dh", (int) ((now - (m->last)) / 3600));
  1375. else if (now - (m->last) > 180)
  1376. simple_snprintf(s1, sizeof s1, "%2dm", (int) ((now - (m->last)) / 60));
  1377. else
  1378. strlcpy(s1, " ", sizeof s1);
  1379. simple_snprintf(format, sizeof format, "%%c%%c%%-%zus %%-%zus %%s %%c %%d %%s %%s %%s\n",
  1380. maxnicklen, maxhandlen);
  1381. dprintf(idx, format, chanflag[0], chanflag[1], m->nick, handle, s, atrflag, m->hops,
  1382. s1, m->userhost, m->userip);
  1383. }
  1384. if (chan_fakeop(m))
  1385. dprintf(idx, " (FAKE CHANOP GIVEN BY SERVER)\n");
  1386. if (chan_sentop(m))
  1387. dprintf(idx, " (pending +o -- I'm lagged)\n");
  1388. if (chan_sentdeop(m))
  1389. dprintf(idx, " (pending -o -- I'm lagged)\n");
  1390. if (chan_sentkick(m))
  1391. dprintf(idx, " (pending kick)\n");
  1392. }
  1393. }
  1394. dprintf(idx, "End of channel info.\n");
  1395. }
  1396. static void cmd_topic(int idx, char *par)
  1397. {
  1398. struct chanset_t *chan = NULL;
  1399. if (par[0] && (strchr(CHANMETA, par[0]) != NULL)) {
  1400. chan = get_channel(idx, newsplit(&par));
  1401. } else
  1402. chan = get_channel(idx, "");
  1403. if (!chan || !has_op(idx, chan))
  1404. return;
  1405. get_user_flagrec(dcc[idx].user, &user, chan->dname);
  1406. if (!channel_active(chan)) {
  1407. dprintf(idx, "I'm not on %s right now!\n", chan->dname);
  1408. return;
  1409. }
  1410. if (!par[0]) {
  1411. if (chan->channel.topic) {
  1412. dprintf(idx, "The topic for %s is: %s\n", chan->dname,
  1413. chan->channel.topic);
  1414. } else {
  1415. dprintf(idx, "No topic is set for %s\n", chan->dname);
  1416. }
  1417. } else if (channel_optopic(chan) && !me_op(chan)) {
  1418. dprintf(idx, "I'm not a channel op on %s and the channel %s",
  1419. "is +t.\n", chan->dname);
  1420. } else {
  1421. dprintf(DP_SERVER, "TOPIC %s :%s\n", chan->name, par);
  1422. dprintf(idx, "Changing topic...\n");
  1423. putlog(LOG_CMDS, "*", "#%s# (%s) topic %s", dcc[idx].nick,
  1424. chan->dname, par);
  1425. }
  1426. }
  1427. static void cmd_resetbans(int idx, char *par)
  1428. {
  1429. struct chanset_t *chan = get_channel(idx, newsplit(&par));
  1430. if (!chan || !has_op(idx, chan))
  1431. return;
  1432. get_user_flagrec(dcc[idx].user, &user, chan->dname);
  1433. putlog(LOG_CMDS, "*", "#%s# (%s) resetbans", dcc[idx].nick, chan->dname);
  1434. dprintf(idx, "Resetting bans on %s...\n", chan->dname);
  1435. resetbans(chan);
  1436. }
  1437. static void cmd_resetexempts(int idx, char *par)
  1438. {
  1439. struct chanset_t *chan = get_channel(idx, newsplit(&par));
  1440. if (!chan || !has_op(idx, chan))
  1441. return;
  1442. get_user_flagrec(dcc[idx].user, &user, chan->dname);
  1443. putlog(LOG_CMDS, "*", "#%s# (%s) resetexempts", dcc[idx].nick, chan->dname);
  1444. dprintf(idx, "Resetting exempts on %s...\n", chan->dname);
  1445. resetexempts(chan);
  1446. }
  1447. static void cmd_resetinvites(int idx, char *par)
  1448. {
  1449. struct chanset_t *chan = get_channel(idx, newsplit(&par));
  1450. if (!chan || !has_op(idx, chan))
  1451. return;
  1452. get_user_flagrec(dcc[idx].user, &user, chan->dname);
  1453. putlog(LOG_CMDS, "*", "#%s# (%s) resetinvites", dcc[idx].nick, chan->dname);
  1454. dprintf(idx, "Resetting resetinvites on %s...\n", chan->dname);
  1455. resetinvites(chan);
  1456. }
  1457. static void cmd_adduser(int idx, char *par)
  1458. {
  1459. if ((!par[0]) || ((par[0] =='!') && (!par[1]))) {
  1460. dprintf(idx, "Usage: adduser <nick> [handle]\n");
  1461. return;
  1462. }
  1463. char *nick = NULL, *hand = NULL;
  1464. struct chanset_t *chan = NULL;
  1465. struct userrec *u = NULL;
  1466. memberlist *m = NULL;
  1467. char s[UHOSTLEN] = "", s1[UHOSTLEN] = "", s2[MAXPASSLEN + 1] = "", s3[MAXPASSLEN + 1] = "", tmp[50] = "";
  1468. int atr = dcc[idx].user ? dcc[idx].user->flags : 0;
  1469. bool statichost = 0;
  1470. char *p1 = s1;
  1471. putlog(LOG_CMDS, "*", "#%s# adduser %s", dcc[idx].nick, par);
  1472. nick = newsplit(&par);
  1473. /* This flag allows users to have static host (added by drummer, 20Apr99) */
  1474. if (nick[0] == '!') {
  1475. statichost = 1;
  1476. nick++;
  1477. }
  1478. if (!par[0]) {
  1479. hand = nick;
  1480. } else {
  1481. bool ok = 1;
  1482. for (char *p = par; *p; p++)
  1483. if ((*p <= 32) || (*p >= 127))
  1484. ok = 0;
  1485. if (!ok) {
  1486. dprintf(idx, "You can't have strange characters in a nick.\n");
  1487. return;
  1488. } else if (strchr("-,+*=:!.@#;$", par[0]) != NULL) {
  1489. dprintf(idx, "You can't start a nick with '%c'.\n", par[0]);
  1490. return;
  1491. }
  1492. hand = par;
  1493. }
  1494. for (chan = chanset; chan; chan = chan->next) {
  1495. m = ismember(chan, nick);
  1496. if (m)
  1497. break;
  1498. }
  1499. if (!m) {
  1500. dprintf(idx, "%s is not on any channels I monitor\n", nick);
  1501. return;
  1502. }
  1503. if (strlen(hand) > HANDLEN)
  1504. hand[HANDLEN] = 0;
  1505. member_getuser(m);
  1506. strlcpy(s, m->from, sizeof(s));
  1507. if ((u = m->user)) {
  1508. dprintf(idx, "%s is already known as %s.\n", nick, u->handle);
  1509. return;
  1510. }
  1511. u = get_user_by_handle(userlist, hand);
  1512. if (u && (u->flags & (USER_OWNER | USER_MASTER)) &&
  1513. !(atr & USER_OWNER) && strcasecmp(dcc[idx].nick, hand)) {
  1514. dprintf(idx, "You can't add hostmasks to the bot owner/master.\n");
  1515. return;
  1516. }
  1517. if (!statichost)
  1518. maskaddr(s, s1, 0); /* *!user@host.com */
  1519. else {
  1520. strlcpy(s1, s, sizeof s1);
  1521. p1 = strchr(s1, '!');
  1522. if (strchr("~^+=-", p1[1]))
  1523. p1[1] = '?';
  1524. p1--;
  1525. p1[0] = '*';
  1526. }
  1527. if (!u) {
  1528. userlist = adduser(userlist, hand, p1, "-", USER_DEFAULT, 0);
  1529. u = get_user_by_handle(userlist, hand);
  1530. simple_snprintf(tmp, sizeof(tmp), "%li %s", (long) now, dcc[idx].nick);
  1531. set_user(&USERENTRY_ADDED, u, tmp);
  1532. make_rand_str(s2, MAXPASSLEN);
  1533. set_user(&USERENTRY_PASS, u, s2);
  1534. make_rand_str(s3, MAXPASSLEN);
  1535. set_user(&USERENTRY_SECPASS, u, s3);
  1536. dprintf(idx, "Added [%s]%s with no flags.\n", hand, p1);
  1537. dprintf(idx, "%s's initial password set to \002%s\002\n", hand, s2);
  1538. dprintf(idx, "%s's initial secpass set to \002%s\002\n", hand, s3);
  1539. bd::String msg;
  1540. msg = bd::String::printf("*** You've been add to this botnet as '%s' with the host '%s'. Ask a botnet admin for the msg cmds. Your initial password is: %s", hand, p1, s2);
  1541. privmsg(nick, msg, DP_HELP);
  1542. } else {
  1543. dprintf(idx, "Added hostmask %s to %s.\n", p1, u->handle);
  1544. addhost_by_handle(hand, p1);
  1545. get_user_flagrec(u, &user, chan->dname);
  1546. check_this_user(hand, 0, NULL);
  1547. }
  1548. }
  1549. static void cmd_deluser(int idx, char *par)
  1550. {
  1551. if (!par[0]) {
  1552. dprintf(idx, "Usage: deluser <nick>\n");
  1553. return;
  1554. }
  1555. char *nick = NULL, *added = NULL;
  1556. struct chanset_t *chan = NULL;
  1557. memberlist *m = NULL;
  1558. struct userrec *u = NULL;
  1559. nick = newsplit(&par);
  1560. for (chan = chanset; chan; chan = chan->next) {
  1561. m = ismember(chan, nick);
  1562. if (m)
  1563. break;
  1564. }
  1565. if (!m) {
  1566. dprintf(idx, "%s is not on any channels I monitor\n", nick);
  1567. return;
  1568. }
  1569. get_user_flagrec(dcc[idx].user, &user, chan->dname);
  1570. member_getuser(m);
  1571. if (!(u = m->user)) {
  1572. dprintf(idx, "%s is not a valid user.\n", nick);
  1573. return;
  1574. }
  1575. added = (char *) get_user(&USERENTRY_ADDED, u);
  1576. newsplit(&added);
  1577. get_user_flagrec(u, &victim, NULL);
  1578. if (isowner(u->handle)) {
  1579. dprintf(idx, "You can't remove a permanent bot owner!\n");
  1580. } else if (glob_admin(victim) && !isowner(dcc[idx].nick)) {
  1581. dprintf(idx, "You can't remove an admin!\n");
  1582. } else if (glob_owner(victim)) {
  1583. dprintf(idx, "You can't remove a bot owner!\n");
  1584. } else if (chan_owner(victim) && !glob_owner(user)) {
  1585. dprintf(idx, "You can't remove a channel owner!\n");
  1586. } else if (chan_master(victim) && !(glob_owner(user) || chan_owner(user))) {
  1587. dprintf(idx, "You can't remove a channel master!\n");
  1588. } else if (glob_bot(victim) && !glob_owner(user)) {
  1589. dprintf(idx, "You can't remove a bot!\n");
  1590. } else if (!glob_master(user) && strcasecmp(dcc[idx].nick, added)) {
  1591. dprintf(idx, "Sorry, you may not delete this user as you did not add them.\n");
  1592. } else {
  1593. char buf[HANDLEN + 1] = "";
  1594. strlcpy(buf, u->handle, sizeof buf);
  1595. if (deluser(u->handle)) {
  1596. dprintf(idx, "Deleted %s.\n", buf); /* ?!?! :) */
  1597. putlog(LOG_CMDS, "*", "#%s# deluser %s [%s]", dcc[idx].nick, nick, buf);
  1598. } else {
  1599. dprintf(idx, "Failed.\n");
  1600. }
  1601. }
  1602. }
  1603. static void cmd_reset(int idx, char *par)
  1604. {
  1605. struct chanset_t *chan = NULL;
  1606. if (par[0]) {
  1607. chan = findchan_by_dname(par);
  1608. if (chan)
  1609. get_user_flagrec(dcc[idx].user, &user, chan->dname);
  1610. if (!chan || privchan(user, chan, PRIV_OP)) {
  1611. dprintf(idx, "I don't monitor that channel.\n");
  1612. } else {
  1613. get_user_flagrec(dcc[idx].user, &user, par);
  1614. if (!glob_master(user) && !chan_master(user)) {
  1615. dprintf(idx, "You are not a master on %s.\n", chan->dname);
  1616. } else if (!channel_active(chan)) {
  1617. dprintf(idx, "I'm not on %s at the moment!\n", chan->dname);
  1618. } else {
  1619. putlog(LOG_CMDS, "*", "#%s# reset %s", dcc[idx].nick, par);
  1620. dprintf(idx, "Resetting channel info for %s...\n", chan->dname);
  1621. reset_chan_info(chan);
  1622. }
  1623. }
  1624. } else if (!(dcc[idx].user->flags & USER_MASTER)) {
  1625. dprintf(idx, "You are not a Bot Master.\n");
  1626. } else {
  1627. putlog(LOG_CMDS, "*", "#%s# reset all", dcc[idx].nick);
  1628. dprintf(idx, "Resetting channel info for all channels...\n");
  1629. for (chan = chanset; chan; chan = chan->next) {
  1630. if (channel_active(chan) || (!channel_active(chan) && shouldjoin(chan)))
  1631. reset_chan_info(chan);
  1632. }
  1633. }
  1634. }
  1635. static void cmd_play(int idx, char *par)
  1636. {
  1637. if (!par[0]) {
  1638. dprintf(idx, "Usage: play [channel] <file>\n");
  1639. return;
  1640. }
  1641. char *chname = NULL;
  1642. struct chanset_t *chan = NULL;
  1643. if (strchr(CHANMETA, par[0]) != NULL)
  1644. chname = newsplit(&par);
  1645. else
  1646. chname = 0;
  1647. chan = get_channel(idx, chname);
  1648. if (!chan)
  1649. return;
  1650. if (!par[0]) {
  1651. dprintf(idx, "Usage: play [channel] <file>\n");
  1652. return;
  1653. }
  1654. memberlist *m = ismember(chan, botname);
  1655. if (!m) {
  1656. dprintf(idx, "Cannot play to %s: I'm not on that channel.\n", chan->dname);
  1657. return;
  1658. }
  1659. get_user_flagrec(dcc[idx].user, &user, chan->dname);
  1660. if (!me_op(chan) && !isowner(dcc[idx].nick)) {
  1661. dprintf(idx, "Cannot play to %s: I am not opped.\n", chan->dname);
  1662. dprintf(idx, "Only owners may play to a channel the bot is not opped in.\n");
  1663. return;
  1664. }
  1665. if (!me_op(chan) && !me_voice(chan)) {
  1666. dprintf(idx, "Cannot play to %s: I am not voiced or opped.\n", chan->dname);
  1667. return;
  1668. }
  1669. putlog(LOG_CMDS, "*", "#%s# (%s) play %s", dcc[idx].nick, chan->dname, par);
  1670. // Ensure file exists and is within proper path
  1671. bd::String file(par);
  1672. if (file[0] == '/' || file(0, 2) == "..") {
  1673. dprintf(idx, "Cannot play '%s': Illegal path.\n", par);
  1674. return;
  1675. }
  1676. if (!can_stat(par)) {
  1677. dprintf(idx, "Cannot play '%s': Cannot access file.\n", par);
  1678. return;
  1679. }
  1680. bd::Stream stream;
  1681. stream.loadFile(par);
  1682. bd::String str;
  1683. size_t lines = 0;
  1684. while (stream.tell() < stream.length()) {
  1685. str = stream.getline().chomp();
  1686. if (str.length()) {
  1687. privmsg(chan->name, str, DP_PLAY);
  1688. ++lines;
  1689. }
  1690. }
  1691. dprintf(idx, "Playing %zu lines from %s to %s\n", lines, par, chan->dname);
  1692. long time_to_play = 0;
  1693. if (floodless)
  1694. time_to_play = 0;
  1695. else if (lines < 10)
  1696. time_to_play = 3;
  1697. else
  1698. time_to_play = 3 + ((lines - 10) / 2);
  1699. dprintf(idx, "Estimated time-to-play: %li seconds\n", time_to_play);
  1700. }
  1701. static cmd_t irc_dcc[] =
  1702. {
  1703. {"act", "o|o", (Function) cmd_act, NULL, LEAF},
  1704. {"adduser", "m|m", (Function) cmd_adduser, NULL, LEAF|AUTH},
  1705. {"authed", "n", (Function) cmd_authed, NULL, LEAF},
  1706. {"channel", "o|o", (Function) cmd_channel, NULL, LEAF},
  1707. {"deluser", "m|m", (Function) cmd_deluser, NULL, LEAF|AUTH},
  1708. {"deop", "o|o", (Function) cmd_deop, NULL, LEAF|AUTH},
  1709. {"devoice", "o|o", (Function) cmd_devoice, NULL, LEAF|AUTH},
  1710. {"getkey", "o|o", (Function) cmd_getkey, NULL, LEAF|AUTH},
  1711. {"find", "", (Function) cmd_find, NULL, LEAF|AUTH},
  1712. {"invite", "o|o", (Function) cmd_invite, NULL, LEAF|AUTH},
  1713. #ifdef CACHE
  1714. {"iop", "o|o", (Function) cmd_iop, NULL, LEAF|AUTH},
  1715. #endif /* CACHE */
  1716. {"kick", "o|o", (Function) cmd_kick, NULL, LEAF|AUTH},
  1717. {"kickban", "o|o", (Function) cmd_kickban, NULL, LEAF|AUTH},
  1718. {"mmode", "n|n", (Function) cmd_mmode, NULL, LEAF|AUTH},
  1719. {"mop", "n|m", (Function) cmd_mop, NULL, LEAF|AUTH},
  1720. {"msg", "o", (Function) cmd_msg, NULL, LEAF|AUTH},
  1721. {"nick", "m", (Function) cmd_nick, NULL, LEAF},
  1722. {"op", "o|o", (Function) cmd_op, NULL, LEAF|AUTH},
  1723. {"play", "m|m", (Function) cmd_play, NULL, LEAF|AUTH},
  1724. {"release", "m", (Function) cmd_release, NULL, LEAF|AUTH},
  1725. {"reset", "m|m", (Function) cmd_reset, NULL, LEAF|AUTH},
  1726. {"resetbans", "o|o", (Function) cmd_resetbans, NULL, LEAF|AUTH},
  1727. {"resetexempts", "o|o", (Function) cmd_resetexempts, NULL, LEAF|AUTH},
  1728. {"resetinvites", "o|o", (Function) cmd_resetinvites, NULL, LEAF|AUTH},
  1729. {"say", "o|o", (Function) cmd_say, NULL, LEAF},
  1730. {"swhois", "", (Function) cmd_swhois, NULL, LEAF|AUTH},
  1731. {"topic", "o|o", (Function) cmd_topic, NULL, LEAF|AUTH},
  1732. {"voice", "o|o", (Function) cmd_voice, NULL, LEAF|AUTH},
  1733. {NULL, NULL, NULL, NULL, 0}
  1734. };
  1735. /* vim: set sts=2 sw=2 ts=8 et: */