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 19KB

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