The easy to use and full featured Irc Bot everyone is talking about!
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Relay.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. using Combot.IRCServices;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Combot.IRCServices.Messaging;
  6. namespace Combot.Modules.Plugins
  7. {
  8. public class Relay : Module
  9. {
  10. public override void Initialize()
  11. {
  12. Bot.CommandReceivedEvent += HandleCommandEvent;
  13. Bot.IRC.Message.CTCPMessageReceivedEvent += CTCPRelayHandler;
  14. Bot.IRC.Message.CTCPNoticeReceivedEvent += CTCPRelayHandler;
  15. Bot.IRC.Message.ChannelMessageReceivedEvent += RelayChannelMessage;
  16. Bot.IRC.Message.PrivateMessageReceivedEvent += RelayPrivateMessage;
  17. Bot.IRC.Message.ChannelNoticeReceivedEvent += RelayChannelNotice;
  18. Bot.IRC.Message.PrivateNoticeReceivedEvent += RelayPrivateNotice;
  19. Bot.IRC.Message.JoinChannelEvent += RelayChannelJoin;
  20. Bot.IRC.Message.InviteChannelEvent += RelayChannelInvite;
  21. Bot.IRC.Message.PartChannelEvent += RelayChannelPart;
  22. Bot.IRC.Message.KickEvent += RelayChannelKick;
  23. Bot.IRC.Message.QuitEvent += RelayQuit;
  24. }
  25. public override void ParseCommand(CommandMessage command)
  26. {
  27. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  28. switch (foundCommand.Name)
  29. {
  30. case "Relay":
  31. string method = command.Arguments["Method"];
  32. switch (method.ToLower())
  33. {
  34. case "add":
  35. AddRelay(command);
  36. break;
  37. case "edit":
  38. EditRelay(command);
  39. break;
  40. case "delete":
  41. case "del":
  42. DeleteRelay(command);
  43. break;
  44. case "view":
  45. ViewRelay(command);
  46. break;
  47. }
  48. break;
  49. }
  50. }
  51. private void AddRelay(CommandMessage command)
  52. {
  53. string source = command.Arguments.ContainsKey("Source") ? command.Arguments["Source"] : command.Location;
  54. string target = command.Arguments.ContainsKey("Target") ? command.Arguments["Target"] : command.Nick.Nickname;
  55. string type = command.Arguments.ContainsKey("Type") ? command.Arguments["Type"] : "Message";
  56. string modes = command.Arguments.ContainsKey("Modes") ? command.Arguments["Modes"] : string.Empty;
  57. // verify access in source and target
  58. if (!CheckAccess(source, command.Nick.Nickname, command.Access))
  59. {
  60. string invalid = string.Format("You do not have permission to use '{0}' as a source.", source);
  61. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
  62. return;
  63. }
  64. if (!CheckAccess(target, command.Nick.Nickname, command.Access))
  65. {
  66. string invalid = string.Format("You do not have permission to use '{0}' as a target.", source);
  67. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
  68. return;
  69. }
  70. RelayType relayType = RelayType.Message;
  71. Enum.TryParse(type, out relayType);
  72. AddNick(command.Nick);
  73. string query = "INSERT INTO `relays` SET " +
  74. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
  75. "`nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {1} && `nickname` = {2}), " +
  76. "`source` = {3}, " +
  77. "`target` = {4}, " +
  78. "`type` = {5}, " +
  79. "`modes` = {6}, " +
  80. "`date_added` = {7}";
  81. Bot.Database.Execute(query, new object[] { Bot.ServerConfig.Name, Bot.ServerConfig.Name, command.Nick.Nickname, source, target, (int)relayType, modes, command.TimeStamp });
  82. List<Dictionary<string, object>> results = GetRelayList(command.Nick.Nickname);
  83. string relayMessage = string.Format("Added relay from \u0002{0}\u0002 to \u0002{1}\u0002. You now have \u0002{2}\u0002 relays created.", source, target, results.Count);
  84. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, relayMessage);
  85. }
  86. private void EditRelay(CommandMessage command)
  87. {
  88. string source = command.Arguments.ContainsKey("Source") ? command.Arguments["Source"] : command.Location;
  89. string target = command.Arguments.ContainsKey("Target") ? command.Arguments["Target"] : command.Nick.Nickname;
  90. string type = command.Arguments.ContainsKey("Type") ? command.Arguments["Type"] : "Message";
  91. string modes = command.Arguments.ContainsKey("Modes") ? command.Arguments["Modes"] : string.Empty;
  92. // verify access in source and target
  93. if (!CheckAccess(source, command.Nick.Nickname, command.Access))
  94. {
  95. string invalid = string.Format("You do not have permission to use '{0}' as a source.", source);
  96. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
  97. return;
  98. }
  99. if (!CheckAccess(target, command.Nick.Nickname, command.Access))
  100. {
  101. string invalid = string.Format("You do not have permission to use '{0}' as a target.", source);
  102. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
  103. return;
  104. }
  105. RelayType relayType = RelayType.Message;
  106. Enum.TryParse(type, out relayType);
  107. int num = HasValidID(command);
  108. if (num > 0)
  109. {
  110. List<Dictionary<string, object>> results = GetRelayList(command.Nick.Nickname);
  111. int id = Convert.ToInt32(results[num - 1]["id"]);
  112. string query = "UPDATE `relays` SET " +
  113. "`source` = {0}, " +
  114. "`target` = {1}, " +
  115. "`type` = {2}, " +
  116. "`modes` = {3} " +
  117. "WHERE `id` = {4}";
  118. Bot.Database.Execute(query, new object[] { source, target, (int)relayType, modes, id });
  119. string relayMessage = string.Format("Updated relay \u0002{0}\u0002 to be from \u0002{1}\u0002 to \u0002{2}\u0002.", num, source, target);
  120. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, relayMessage);
  121. }
  122. else
  123. {
  124. string invalid = "Invalid relay ID.";
  125. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
  126. }
  127. }
  128. private void DeleteRelay(CommandMessage command)
  129. {
  130. int num = HasValidID(command);
  131. if (num > 0)
  132. {
  133. List<Dictionary<string, object>> results = GetRelayList(command.Nick.Nickname);
  134. int id = Convert.ToInt32(results[num - 1]["id"]);
  135. string query = "DELETE FROM `relays` " +
  136. "WHERE `id` = {0}";
  137. Bot.Database.Execute(query, new object[] { id });
  138. string relayMessage = string.Format("Relay #\u0002{0}\u0002 has been deleted.", num);
  139. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, relayMessage);
  140. }
  141. else
  142. {
  143. string invalid = "Invalid relay ID.";
  144. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
  145. }
  146. }
  147. private void ViewRelay(CommandMessage command)
  148. {
  149. List<Dictionary<string, object>> results = GetRelayList(command.Nick.Nickname);
  150. if (command.Arguments.ContainsKey("ID"))
  151. {
  152. int num = HasValidID(command);
  153. if (num > 0)
  154. {
  155. int relayInt = 0;
  156. Int32.TryParse(results[num - 1]["type"].ToString(), out relayInt);
  157. string relayMessage = string.Format("Relay #\u0002{0}\u0002 - Source: \u0002{1}\u0002 | Target: \u0002{2}\u0002 | Type: \u0002{3}\u0002",
  158. num, results[num - 1]["source"], results[num - 1]["target"], (RelayType)relayInt);
  159. if ((RelayType)relayInt == RelayType.Mode)
  160. {
  161. relayMessage = string.Format("{0} | Modes: \u0002{1}\u0002", relayMessage, results[num - 1]["modes"]);
  162. }
  163. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, relayMessage);
  164. }
  165. else
  166. {
  167. string invalid = "Invalid relay ID.";
  168. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
  169. }
  170. }
  171. else
  172. {
  173. if (results.Any())
  174. {
  175. for (int i = 0; i < results.Count; i++)
  176. {
  177. int relayInt = 0;
  178. Int32.TryParse(results[i]["type"].ToString(), out relayInt);
  179. string relayMessage = string.Format("Relay #\u0002{0}\u0002 - Source: \u0002{1}\u0002 | Target: \u0002{2}\u0002 | Type: \u0002{3}\u0002",
  180. i + 1, results[i]["source"], results[i]["target"], (RelayType)relayInt);
  181. if ((RelayType)relayInt == RelayType.Mode)
  182. {
  183. relayMessage = string.Format("{0} | Modes: \u0002{1}\u0002", relayMessage, results[i]["modes"]);
  184. }
  185. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, relayMessage, true);
  186. }
  187. }
  188. else
  189. {
  190. string invalid = "You do not have any relays set.";
  191. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
  192. }
  193. }
  194. }
  195. private List<Dictionary<string, object>> GetRelayList(string nickname)
  196. {
  197. string search = "SELECT `relays`.`id`, `relays`.`source`, `relays`.`target`, `relays`.`type`, `relays`.`modes` FROM `relays` " +
  198. "INNER JOIN `nicks` " +
  199. "ON `relays`.`nick_id` = `nicks`.`id` " +
  200. "INNER JOIN `servers` " +
  201. "ON `nicks`.`server_id` = `servers`.`id` " +
  202. "WHERE `servers`.`name` = {0} AND `nicks`.`nickname` = {1}";
  203. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, nickname });
  204. }
  205. private List<Dictionary<string, object>> GetRelayList(string source, RelayType type)
  206. {
  207. string search = "SELECT `relays`.`target`, `relays`.`modes` FROM `relays` " +
  208. "INNER JOIN `servers` " +
  209. "ON `relays`.`server_id` = `servers`.`id` " +
  210. "WHERE `servers`.`name` = {0} AND `relays`.`source` = {1} AND `relays`.`type` = {2}";
  211. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, source, (int)type });
  212. }
  213. private bool CheckAccess(string source, string nick, List<AccessType> types)
  214. {
  215. // Owners get to have all the fun
  216. if (Bot.ServerConfig.Owners.Contains(nick))
  217. return true;
  218. // The source is a channel
  219. if (Bot.IRC.Channels.Exists(chan => chan.Name == source))
  220. {
  221. foreach (AccessType access in types)
  222. {
  223. bool valid = Bot.CheckChannelAccess(source, nick, AccessType.Operator);
  224. if (!valid)
  225. return false;
  226. }
  227. }
  228. // The source is a nickname
  229. else
  230. {
  231. if (source != nick)
  232. return false;
  233. }
  234. return true;
  235. }
  236. /* Returns the parsed ID field if valid, otherwise returns 0 */
  237. private int HasValidID(CommandMessage command)
  238. {
  239. int num = 0;
  240. int ret = 0;
  241. List<Dictionary<string, object>> results = GetRelayList(command.Nick.Nickname);
  242. if (int.TryParse(command.Arguments["ID"], out num))
  243. {
  244. if (results.Count >= num && num > 0)
  245. {
  246. ret = num;
  247. }
  248. }
  249. return ret;
  250. }
  251. private void ProcessRelay(string source, RelayType type, string message)
  252. {
  253. switch (type)
  254. {
  255. }
  256. }
  257. private void RelayQuit(object sender, QuitInfo e)
  258. {
  259. string msg = string.Format("{0} has quit.");
  260. ProcessRelay(e.Nick.Nickname, RelayType.Quit, msg);
  261. }
  262. private void RelayChannelKick(object sender, KickInfo e)
  263. {
  264. string msg = string.Format("{0} has kicked {1} from {2} ({3})", e.Nick.Nickname, e.KickedNick.Nickname, e.Channel, e.Reason);
  265. ProcessRelay(e.Channel, RelayType.Kick, msg);
  266. }
  267. private void RelayChannelPart(object sender, PartChannelInfo e)
  268. {
  269. throw new NotImplementedException();
  270. }
  271. private void RelayChannelInvite(object sender, InviteChannelInfo e)
  272. {
  273. throw new NotImplementedException();
  274. }
  275. private void RelayChannelJoin(object sender, JoinChannelInfo e)
  276. {
  277. throw new NotImplementedException();
  278. }
  279. private void RelayPrivateNotice(object sender, PrivateNotice e)
  280. {
  281. throw new NotImplementedException();
  282. }
  283. private void RelayChannelNotice(object sender, ChannelNotice e)
  284. {
  285. throw new NotImplementedException();
  286. }
  287. private void RelayPrivateMessage(object sender, PrivateMessage e)
  288. {
  289. throw new NotImplementedException();
  290. }
  291. private void RelayChannelMessage(object sender, ChannelMessage e)
  292. {
  293. throw new NotImplementedException();
  294. }
  295. private void CTCPRelayHandler(object sender, CTCPMessage e)
  296. {
  297. throw new NotImplementedException();
  298. }
  299. }
  300. }