The easy to use and full featured Irc Bot everyone is talking about!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Moderation.cs 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using Combot.IRCServices;
  5. using Timer = System.Timers.Timer;
  6. namespace Combot.Modules.Plugins
  7. {
  8. public class Moderation : Module
  9. {
  10. private List<Timer> unbanTimers;
  11. private ReaderWriterLockSlim listLock;
  12. public override void Initialize()
  13. {
  14. unbanTimers = new List<Timer>();
  15. listLock = new ReaderWriterLockSlim();
  16. Bot.CommandReceivedEvent += HandleCommandEvent;
  17. }
  18. public override void ParseCommand(CommandMessage command)
  19. {
  20. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  21. switch (foundCommand.Name)
  22. {
  23. // Privilege Mode Commands
  24. case "Founder":
  25. ModifyUserPrivilege(true, command, ChannelMode.q);
  26. break;
  27. case "Remove Founder":
  28. ModifyUserPrivilege(false, command, ChannelMode.q);
  29. break;
  30. case "SOP":
  31. ModifyUserPrivilege(true, command, ChannelMode.a);
  32. break;
  33. case "Remove SOP":
  34. ModifyUserPrivilege(false, command, ChannelMode.a);
  35. break;
  36. case "OP":
  37. ModifyUserPrivilege(true, command, ChannelMode.o);
  38. break;
  39. case "Remove OP":
  40. ModifyUserPrivilege(false, command, ChannelMode.o);
  41. break;
  42. case "HOP":
  43. ModifyUserPrivilege(true, command, ChannelMode.h);
  44. break;
  45. case "Remove HOP":
  46. ModifyUserPrivilege(false, command, ChannelMode.h);
  47. break;
  48. case "Voice":
  49. ModifyUserPrivilege(true, command, ChannelMode.v);
  50. break;
  51. case "Remove Voice":
  52. ModifyUserPrivilege(false, command, ChannelMode.v);
  53. break;
  54. // Auto Privilege Management
  55. case "ASOP":
  56. ModifyAutoUserPrivilege("SOP", command, ChannelMode.a);
  57. break;
  58. case "AOP":
  59. ModifyAutoUserPrivilege("AOP", command, ChannelMode.o);
  60. break;
  61. case "AHOP":
  62. ModifyAutoUserPrivilege("HOP", command, ChannelMode.h);
  63. break;
  64. case "AVoice":
  65. ModifyAutoUserPrivilege("VOP", command, ChannelMode.v);
  66. break;
  67. case "Mode":
  68. ModifyChannelModes(foundCommand, command);
  69. break;
  70. case "Topic":
  71. ModifyChannelTopic(foundCommand, command);
  72. break;
  73. case "Invite":
  74. InviteNick(foundCommand, command);
  75. break;
  76. case "Ban":
  77. BanNick(true, foundCommand, command);
  78. break;
  79. case "UnBan":
  80. BanNick(false, foundCommand, command);
  81. break;
  82. case "Kick Ban":
  83. BanNick(true, foundCommand, command);
  84. KickNick(foundCommand, command);
  85. break;
  86. case "Timed Ban":
  87. TimedBan(foundCommand, command);
  88. break;
  89. case "Timed Kick Ban":
  90. TimedBan(foundCommand, command);
  91. KickNick(foundCommand, command);
  92. break;
  93. case "Kick":
  94. KickNick(foundCommand, command);
  95. break;
  96. case "Kick Self":
  97. KickSelf(command);
  98. break;
  99. case "Clear":
  100. ClearChannel(foundCommand, command);
  101. break;
  102. }
  103. }
  104. private void ModifyUserPrivilege(bool set, CommandMessage command, ChannelMode mode)
  105. {
  106. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  107. if (Bot.CheckChannelAccess(channel, command.Nick.Nickname, Bot.ChannelModeMapping[mode]))
  108. {
  109. SetMode(set, channel, mode, command.Arguments["Nickname"]);
  110. }
  111. else
  112. {
  113. string noAccessMessage = string.Format("You do not have access to set mode \u0002+{0}\u000F for \u0002{1}\u000F on \u0002{2}\u000F.", mode, command.Arguments["Nickname"], channel);
  114. switch (command.MessageType)
  115. {
  116. case MessageType.Channel:
  117. Bot.IRC.SendPrivateMessage(command.Location, noAccessMessage);
  118. break;
  119. case MessageType.Query:
  120. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, noAccessMessage);
  121. break;
  122. case MessageType.Notice:
  123. Bot.IRC.SendNotice(command.Nick.Nickname, noAccessMessage);
  124. break;
  125. }
  126. }
  127. }
  128. private void ModifyAutoUserPrivilege(string optionCommand, CommandMessage command, ChannelMode mode)
  129. {
  130. bool set = true;
  131. if (command.Arguments["Option"].ToLower() == "del")
  132. {
  133. set = false;
  134. }
  135. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  136. if (Bot.CheckChannelAccess(channel, command.Nick.Nickname, Bot.ChannelModeMapping[mode]))
  137. {
  138. SetMode(set, channel, mode, command.Arguments["Nickname"]);
  139. Bot.IRC.SendPrivateMessage("ChanServ", string.Format("{0} {1} {2} {3}", optionCommand, channel, command.Arguments["Option"], command.Arguments["Nickname"]));
  140. }
  141. else
  142. {
  143. string noAccessMessage = string.Format("You do not have access to \u0002{0}\u000F \u0002{1}\u000F to the \u0002{2}\u000F list on \u0002{3}\u000F.", command.Arguments["Option"].ToLower(), command.Arguments["Nickname"], optionCommand, channel);
  144. switch (command.MessageType)
  145. {
  146. case MessageType.Channel:
  147. Bot.IRC.SendPrivateMessage(command.Location, noAccessMessage);
  148. break;
  149. case MessageType.Query:
  150. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, noAccessMessage);
  151. break;
  152. case MessageType.Notice:
  153. Bot.IRC.SendNotice(command.Nick.Nickname, noAccessMessage);
  154. break;
  155. }
  156. }
  157. }
  158. private void ModifyChannelModes(Command curCommand, CommandMessage command)
  159. {
  160. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  161. bool allowedMode = true;
  162. bool allowedCommand = Bot.CheckChannelAccess(channel, command.Nick.Nickname, curCommand.AllowedAccess);
  163. if (allowedCommand)
  164. {
  165. List<ChannelModeInfo> modeList = new List<ChannelModeInfo>();
  166. if (command.Arguments.ContainsKey("Parameters"))
  167. {
  168. modeList = Bot.IRC.ParseChannelModeString(command.Arguments["Modes"],
  169. command.Arguments["Parameters"]);
  170. }
  171. else
  172. {
  173. modeList = Bot.IRC.ParseChannelModeString(command.Arguments["Modes"], string.Empty);
  174. }
  175. ChannelMode mode = ChannelMode.q;
  176. for (int i = 0; i < modeList.Count; i++)
  177. {
  178. switch (modeList[i].Mode)
  179. {
  180. case ChannelMode.v:
  181. case ChannelMode.h:
  182. case ChannelMode.o:
  183. case ChannelMode.a:
  184. case ChannelMode.q:
  185. allowedMode = Bot.CheckChannelAccess(channel, command.Nick.Nickname, Bot.ChannelModeMapping[modeList[i].Mode]);
  186. if (!allowedMode)
  187. {
  188. mode = modeList[i].Mode;
  189. }
  190. break;
  191. }
  192. }
  193. if (allowedMode)
  194. {
  195. Bot.IRC.SendMode(channel, modeList);
  196. }
  197. else
  198. {
  199. string noAccessMessage = string.Format("You do not have access to set mode \u0002+{0}\u000F on \u0002{1}\u000F.", mode, channel);
  200. switch (command.MessageType)
  201. {
  202. case MessageType.Channel:
  203. Bot.IRC.SendPrivateMessage(command.Location, noAccessMessage);
  204. break;
  205. case MessageType.Query:
  206. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, noAccessMessage);
  207. break;
  208. case MessageType.Notice:
  209. Bot.IRC.SendNotice(command.Nick.Nickname, noAccessMessage);
  210. break;
  211. }
  212. }
  213. }
  214. else
  215. {
  216. string noAccessMessage = string.Format("You do not have access to use \u0002{0}\u000F on \u0002{1}\u000F.", command.Command, channel);
  217. switch (command.MessageType)
  218. {
  219. case MessageType.Channel:
  220. Bot.IRC.SendPrivateMessage(command.Location, noAccessMessage);
  221. break;
  222. case MessageType.Query:
  223. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, noAccessMessage);
  224. break;
  225. case MessageType.Notice:
  226. Bot.IRC.SendNotice(command.Nick.Nickname, noAccessMessage);
  227. break;
  228. }
  229. }
  230. }
  231. private void SetMode(bool set, string channel, ChannelMode mode, string nickname)
  232. {
  233. ChannelModeInfo modeInfo = new ChannelModeInfo();
  234. modeInfo.Mode = mode;
  235. modeInfo.Parameter = nickname;
  236. modeInfo.Set = set;
  237. Bot.IRC.SendMode(channel, modeInfo);
  238. }
  239. private void ModifyChannelTopic(Command curCommand, CommandMessage command)
  240. {
  241. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  242. if (Bot.CheckChannelAccess(channel, command.Nick.Nickname, curCommand.AllowedAccess))
  243. {
  244. Bot.IRC.SendTopic(channel, command.Arguments["Message"]);
  245. }
  246. else
  247. {
  248. string noAccessMessage = string.Format("You do not have access to change the topic on \u0002{0}\u000F.", channel);
  249. switch (command.MessageType)
  250. {
  251. case MessageType.Channel:
  252. Bot.IRC.SendPrivateMessage(command.Location, noAccessMessage);
  253. break;
  254. case MessageType.Query:
  255. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, noAccessMessage);
  256. break;
  257. case MessageType.Notice:
  258. Bot.IRC.SendNotice(command.Nick.Nickname, noAccessMessage);
  259. break;
  260. }
  261. }
  262. }
  263. private void InviteNick(Command curCommand, CommandMessage command)
  264. {
  265. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  266. if (Bot.CheckChannelAccess(channel, command.Nick.Nickname, curCommand.AllowedAccess))
  267. {
  268. Bot.IRC.SendInvite(channel, command.Arguments["Nickname"]);
  269. }
  270. else
  271. {
  272. string noAccessMessage = string.Format("You do not have access to invite someone to \u0002{0}\u000F.", channel);
  273. switch (command.MessageType)
  274. {
  275. case MessageType.Channel:
  276. Bot.IRC.SendPrivateMessage(command.Location, noAccessMessage);
  277. break;
  278. case MessageType.Query:
  279. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, noAccessMessage);
  280. break;
  281. case MessageType.Notice:
  282. Bot.IRC.SendNotice(command.Nick.Nickname, noAccessMessage);
  283. break;
  284. }
  285. }
  286. }
  287. private void BanNick(bool set, Command curCommand, CommandMessage command)
  288. {
  289. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  290. if (Bot.CheckChannelAccess(channel, command.Nick.Nickname, curCommand.AllowedAccess))
  291. {
  292. string banMask = command.Arguments["Nickname"];
  293. Channel foundChannel = Bot.IRC.Channels.Find(chan => chan.Nicks.Exists(nick => nick.Nickname == banMask));
  294. if (foundChannel != null)
  295. {
  296. Nick foundNick = foundChannel.Nicks.Find(nick => nick.Nickname == banMask);
  297. if (foundNick.Host != string.Empty && foundNick.Username != null)
  298. {
  299. banMask = string.Format("{0}!*{1}@{2}", foundNick.Nickname, foundNick.Username, foundNick.Host);
  300. }
  301. else if (foundNick.Host != string.Empty)
  302. {
  303. banMask = string.Format("{0}!*@{1}", foundNick.Nickname, foundNick.Host);
  304. }
  305. else if (foundNick.Username != string.Empty)
  306. {
  307. banMask = string.Format("{0}!*{1}@*", foundNick.Nickname, foundNick.Username);
  308. }
  309. else
  310. {
  311. banMask = string.Format("{0}!*@*", foundNick.Nickname);
  312. }
  313. }
  314. else
  315. {
  316. if (!banMask.Contains("@") || !banMask.Contains("!"))
  317. {
  318. banMask = string.Format("{0}!*@*", banMask);
  319. }
  320. }
  321. SetMode(set, channel, ChannelMode.b, banMask);
  322. }
  323. else
  324. {
  325. string banMessage = "ban";
  326. if (!set)
  327. {
  328. banMessage = "unban";
  329. }
  330. string noAccessMessage = string.Format("You do not have access to {0} \u0002{1}\u000F on \u0002{2}\u000F.", banMessage, command.Arguments["Nickname"], channel);
  331. switch (command.MessageType)
  332. {
  333. case MessageType.Channel:
  334. Bot.IRC.SendPrivateMessage(command.Location, noAccessMessage);
  335. break;
  336. case MessageType.Query:
  337. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, noAccessMessage);
  338. break;
  339. case MessageType.Notice:
  340. Bot.IRC.SendNotice(command.Nick.Nickname, noAccessMessage);
  341. break;
  342. }
  343. }
  344. }
  345. private void TimedBan(Command curCommand, CommandMessage command)
  346. {
  347. double timeout;
  348. if (double.TryParse(command.Arguments["Time"], out timeout))
  349. {
  350. BanNick(true, curCommand, command);
  351. Timer unban_trigger = new Timer();
  352. unban_trigger.Interval = (timeout * 1000.0);
  353. unban_trigger.Enabled = true;
  354. unban_trigger.AutoReset = false;
  355. unban_trigger.Elapsed += (sender, e) => TimedUnBan(sender, e, curCommand, command);
  356. listLock.EnterWriteLock();
  357. unbanTimers.Add(unban_trigger);
  358. listLock.ExitWriteLock();
  359. }
  360. else
  361. {
  362. string noAccessMessage = "Please enter a valid time.";
  363. switch (command.MessageType)
  364. {
  365. case MessageType.Channel:
  366. Bot.IRC.SendPrivateMessage(command.Location, noAccessMessage);
  367. break;
  368. case MessageType.Query:
  369. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, noAccessMessage);
  370. break;
  371. case MessageType.Notice:
  372. Bot.IRC.SendNotice(command.Nick.Nickname, noAccessMessage);
  373. break;
  374. }
  375. }
  376. }
  377. private void TimedUnBan(object sender, EventArgs e, Command curCommand, CommandMessage command)
  378. {
  379. Timer unbanTimer = (Timer)sender;
  380. unbanTimer.Enabled = false;
  381. BanNick(false, curCommand, command);
  382. listLock.EnterWriteLock();
  383. unbanTimers.Remove(unbanTimer);
  384. listLock.ExitWriteLock();
  385. }
  386. private void KickNick(Command curCommand, CommandMessage command)
  387. {
  388. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  389. if (Bot.CheckChannelAccess(channel, command.Nick.Nickname, curCommand.AllowedAccess))
  390. {
  391. Bot.IRC.SendKick(channel, command.Arguments["Nickname"]);
  392. }
  393. else
  394. {
  395. string noAccessMessage = string.Format("You do not have access to kick \u0002{0}\u000F from \u0002{1}\u000F.", command.Arguments["Nickname"], channel);
  396. switch (command.MessageType)
  397. {
  398. case MessageType.Channel:
  399. Bot.IRC.SendPrivateMessage(command.Location, noAccessMessage);
  400. break;
  401. case MessageType.Query:
  402. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, noAccessMessage);
  403. break;
  404. case MessageType.Notice:
  405. Bot.IRC.SendNotice(command.Nick.Nickname, noAccessMessage);
  406. break;
  407. }
  408. }
  409. }
  410. private void KickSelf(CommandMessage command)
  411. {
  412. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  413. Bot.IRC.SendKick(channel, command.Nick.Nickname);
  414. }
  415. private void ClearChannel(Command curCommand, CommandMessage command)
  416. {
  417. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  418. if (Bot.CheckChannelAccess(channel, command.Nick.Nickname, curCommand.AllowedAccess))
  419. {
  420. Bot.IRC.SendPrivateMessage("ChanServ", string.Format("CLEAR {0} {1}", channel, command.Arguments["Target"]));
  421. }
  422. else
  423. {
  424. string noAccessMessage = string.Format("You do not have access to clear \u0002{0}\u000F on \u0002{1}\u000F.", command.Arguments["Target"], channel);
  425. switch (command.MessageType)
  426. {
  427. case MessageType.Channel:
  428. Bot.IRC.SendPrivateMessage(command.Location, noAccessMessage);
  429. break;
  430. case MessageType.Query:
  431. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, noAccessMessage);
  432. break;
  433. case MessageType.Notice:
  434. Bot.IRC.SendNotice(command.Nick.Nickname, noAccessMessage);
  435. break;
  436. }
  437. }
  438. }
  439. }
  440. }