cmdsirc.cc 55 KB

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