The easy to use and full featured Irc Bot everyone is talking about!
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Moderation.cs 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. Channel foundChannel = Bot.IRC.Channels.Find(chan => chan.Nicks.Exists(nick => nick.Nickname == banMask));
  243. if (foundChannel != null)
  244. {
  245. Nick foundNick = foundChannel.Nicks.Find(nick => nick.Nickname == banMask);
  246. if (foundNick.Host != string.Empty && foundNick.Username != null)
  247. {
  248. banMask = string.Format("*!*{0}@{1}", foundNick.Username, foundNick.Host);
  249. }
  250. else if (foundNick.Host != string.Empty)
  251. {
  252. banMask = string.Format("*!*@{0}", foundNick.Host);
  253. }
  254. else if (foundNick.Username != string.Empty)
  255. {
  256. banMask = string.Format("{0}!*{1}@*", foundNick.Nickname, foundNick.Username);
  257. }
  258. else
  259. {
  260. banMask = string.Format("{0}!*@*", foundNick.Nickname);
  261. }
  262. }
  263. else
  264. {
  265. if (!banMask.Contains("@") || !banMask.Contains("!"))
  266. {
  267. banMask = string.Format("{0}!*@*", banMask);
  268. }
  269. }
  270. SetMode(set, channel, ChannelMode.b, banMask);
  271. }
  272. else
  273. {
  274. string banMessage = "ban";
  275. if (!set)
  276. {
  277. banMessage = "unban";
  278. }
  279. string noAccessMessage = string.Format("You do not have access to {0} \u0002{1}\u000F on \u0002{2}\u000F.", banMessage, command.Arguments["Nickname"], channel);
  280. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, noAccessMessage);
  281. }
  282. }
  283. private void TimedBan(Command curCommand, CommandMessage command)
  284. {
  285. double timeout;
  286. if (double.TryParse(command.Arguments["Time"], out timeout))
  287. {
  288. BanNick(true, curCommand, command);
  289. Timer unban_trigger = new Timer();
  290. unban_trigger.Interval = (timeout * 1000.0);
  291. unban_trigger.Enabled = true;
  292. unban_trigger.AutoReset = false;
  293. unban_trigger.Elapsed += (sender, e) => TimedUnBan(sender, e, curCommand, command);
  294. listLock.EnterWriteLock();
  295. unbanTimers.Add(unban_trigger);
  296. listLock.ExitWriteLock();
  297. }
  298. else
  299. {
  300. string notValid = "Please enter a valid time.";
  301. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, notValid);
  302. }
  303. }
  304. private void TimedUnBan(object sender, EventArgs e, Command curCommand, CommandMessage command)
  305. {
  306. Timer unbanTimer = (Timer)sender;
  307. unbanTimer.Enabled = false;
  308. BanNick(false, curCommand, command);
  309. listLock.EnterWriteLock();
  310. unbanTimers.Remove(unbanTimer);
  311. listLock.ExitWriteLock();
  312. }
  313. private void KickNick(Command curCommand, CommandMessage command)
  314. {
  315. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  316. if (Bot.CheckChannelAccess(channel, command.Nick.Nickname, curCommand.AllowedAccess) && Bot.CheckNickAccess(channel, command.Nick.Nickname, command.Arguments["Nickname"]))
  317. {
  318. if (command.Arguments.ContainsKey("Reason"))
  319. {
  320. Bot.IRC.Command.SendKick(channel, command.Arguments["Nickname"], command.Arguments["Reason"]);
  321. }
  322. else
  323. {
  324. Bot.IRC.Command.SendKick(channel, command.Arguments["Nickname"]);
  325. }
  326. }
  327. else
  328. {
  329. string noAccessMessage = string.Format("You do not have access to kick \u0002{0}\u000F from \u0002{1}\u000F.", command.Arguments["Nickname"], channel);
  330. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, noAccessMessage);
  331. }
  332. }
  333. private void KickSelf(CommandMessage command)
  334. {
  335. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  336. if (command.Arguments.ContainsKey("Reason"))
  337. {
  338. Bot.IRC.Command.SendKick(channel, command.Nick.Nickname, command.Arguments["Reason"]);
  339. }
  340. else
  341. {
  342. Bot.IRC.Command.SendKick(channel, command.Nick.Nickname);
  343. }
  344. }
  345. private void ClearChannel(Command curCommand, CommandMessage command)
  346. {
  347. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  348. if (Bot.CheckChannelAccess(channel, command.Nick.Nickname, curCommand.AllowedAccess))
  349. {
  350. Bot.IRC.Command.SendPrivateMessage("ChanServ", string.Format("CLEAR {0} {1}", channel, command.Arguments["Target"]));
  351. }
  352. else
  353. {
  354. string noAccessMessage = string.Format("You do not have access to clear \u0002{0}\u000F on \u0002{1}\u000F.", command.Arguments["Target"], channel);
  355. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, noAccessMessage);
  356. }
  357. }
  358. }
  359. }