The easy to use and full featured Irc Bot everyone is talking about!
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Moderation.cs 20KB

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