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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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);
  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);
  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);
  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);
  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 ModifyChannelTopic(Command curCommand, CommandMessage command)
  211. {
  212. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  213. if (Bot.CheckChannelAccess(channel, command.Nick.Nickname, curCommand.AllowedAccess))
  214. {
  215. Bot.IRC.Command.SendTopic(channel, command.Arguments["Message"]);
  216. }
  217. else
  218. {
  219. string noAccessMessage = string.Format("You do not have access to change the topic on \u0002{0}\u000F.", channel);
  220. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, noAccessMessage);
  221. }
  222. }
  223. private void InviteNick(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.SendInvite(channel, command.Arguments["Nickname"]);
  229. }
  230. else
  231. {
  232. string noAccessMessage = string.Format("You do not have access to invite someone to \u0002{0}\u000F.", channel);
  233. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, noAccessMessage);
  234. }
  235. }
  236. private void BanNick(bool set, 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) && Bot.CheckNickAccess(channel, command.Nick.Nickname, command.Arguments["Nickname"]))
  240. {
  241. string banMask = command.Arguments["Nickname"];
  242. if (!banMask.Contains("@") || !banMask.Contains("!"))
  243. {
  244. string search = "SELECT `nickinfo`.`username`, `nickinfo`.`host`, `nicks`.`nickname` FROM `nickinfo` " +
  245. "INNER JOIN `nicks` " +
  246. "ON `nickinfo`.`nick_id` = `nicks`.`id` " +
  247. "INNER JOIN `servers` " +
  248. "ON `nicks`.`server_id` = `servers`.`id` " +
  249. "WHERE `servers`.`name` = {0} AND `nicks`.`nickname` = {1}";
  250. List<Dictionary<string, object>> results = Bot.Database.Query(search, new object[] {Bot.ServerConfig.Name, banMask});
  251. if (results.Any())
  252. {
  253. foreach (Dictionary<string, object> result in results)
  254. {
  255. var nickname = result["nickname"].ToString();
  256. var host = result["host"].ToString();
  257. var username = result["username"].ToString();
  258. if (!string.IsNullOrEmpty(host) && !string.IsNullOrEmpty(username))
  259. {
  260. banMask = string.Format("*!*{0}@{1}", username, host);
  261. }
  262. else if (!string.IsNullOrEmpty(host))
  263. {
  264. banMask = string.Format("*!*@{0}", host);
  265. }
  266. else if (!string.IsNullOrEmpty(username))
  267. {
  268. banMask = string.Format("{0}!*{1}@*", nickname, username);
  269. }
  270. else
  271. {
  272. banMask = string.Format("{0}!*@*", nickname);
  273. }
  274. SetMode(set, channel, ChannelMode.b, banMask);
  275. }
  276. }
  277. else
  278. {
  279. SetMode(set, channel, ChannelMode.b, string.Format("{0}!*@*", banMask));
  280. }
  281. }
  282. else
  283. {
  284. SetMode(set, channel, ChannelMode.b, banMask);
  285. }
  286. }
  287. else
  288. {
  289. string banMessage = "ban";
  290. if (!set)
  291. {
  292. banMessage = "unban";
  293. }
  294. string noAccessMessage = string.Format("You do not have access to {0} \u0002{1}\u000F on \u0002{2}\u000F.", banMessage, command.Arguments["Nickname"], channel);
  295. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, noAccessMessage);
  296. }
  297. }
  298. private void TimedBan(Command curCommand, CommandMessage command)
  299. {
  300. double timeout;
  301. if (double.TryParse(command.Arguments["Time"], out timeout))
  302. {
  303. BanNick(true, curCommand, command);
  304. Timer unban_trigger = new Timer();
  305. unban_trigger.Interval = (timeout * 1000.0);
  306. unban_trigger.Enabled = true;
  307. unban_trigger.AutoReset = false;
  308. unban_trigger.Elapsed += (sender, e) => TimedUnBan(sender, e, curCommand, command);
  309. listLock.EnterWriteLock();
  310. unbanTimers.Add(unban_trigger);
  311. listLock.ExitWriteLock();
  312. }
  313. else
  314. {
  315. string notValid = "Please enter a valid time.";
  316. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, notValid);
  317. }
  318. }
  319. private void TimedUnBan(object sender, EventArgs e, Command curCommand, CommandMessage command)
  320. {
  321. Timer unbanTimer = (Timer)sender;
  322. unbanTimer.Enabled = false;
  323. BanNick(false, curCommand, command);
  324. listLock.EnterWriteLock();
  325. unbanTimers.Remove(unbanTimer);
  326. listLock.ExitWriteLock();
  327. }
  328. private void KickNick(Command curCommand, CommandMessage command)
  329. {
  330. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  331. if (Bot.CheckChannelAccess(channel, command.Nick.Nickname, curCommand.AllowedAccess) && Bot.CheckNickAccess(channel, command.Nick.Nickname, command.Arguments["Nickname"]))
  332. {
  333. if (command.Arguments.ContainsKey("Reason"))
  334. {
  335. Bot.IRC.Command.SendKick(channel, command.Arguments["Nickname"], command.Arguments["Reason"]);
  336. }
  337. else
  338. {
  339. Bot.IRC.Command.SendKick(channel, command.Arguments["Nickname"]);
  340. }
  341. }
  342. else
  343. {
  344. string noAccessMessage = string.Format("You do not have access to kick \u0002{0}\u000F from \u0002{1}\u000F.", command.Arguments["Nickname"], channel);
  345. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, noAccessMessage);
  346. }
  347. }
  348. private void KickSelf(CommandMessage command)
  349. {
  350. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  351. if (command.Arguments.ContainsKey("Reason"))
  352. {
  353. Bot.IRC.Command.SendKick(channel, command.Nick.Nickname, command.Arguments["Reason"]);
  354. }
  355. else
  356. {
  357. Bot.IRC.Command.SendKick(channel, command.Nick.Nickname);
  358. }
  359. }
  360. private void ClearChannel(Command curCommand, CommandMessage command)
  361. {
  362. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  363. if (Bot.CheckChannelAccess(channel, command.Nick.Nickname, curCommand.AllowedAccess))
  364. {
  365. Bot.IRC.Command.SendPrivateMessage("ChanServ", string.Format("CLEAR {0} {1}", channel, command.Arguments["Target"]));
  366. }
  367. else
  368. {
  369. string noAccessMessage = string.Format("You do not have access to clear \u0002{0}\u000F on \u0002{1}\u000F.", command.Arguments["Target"], channel);
  370. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, noAccessMessage);
  371. }
  372. }
  373. }
  374. }