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.

Relay.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using Combot.IRCServices;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace Combot.Modules.Plugins
  6. {
  7. public class Relay : Module
  8. {
  9. public override void Initialize()
  10. {
  11. Bot.CommandReceivedEvent += HandleCommandEvent;
  12. }
  13. public override void ParseCommand(CommandMessage command)
  14. {
  15. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  16. switch (foundCommand.Name)
  17. {
  18. case "Relay":
  19. string method = command.Arguments["Method"];
  20. switch (method.ToLower())
  21. {
  22. case "add":
  23. AddRelay(command);
  24. break;
  25. case "edit":
  26. EditRelay(command);
  27. break;
  28. case "delete":
  29. case "del":
  30. DeleteRelay(command);
  31. break;
  32. case "view":
  33. ViewRelay(command);
  34. break;
  35. }
  36. break;
  37. }
  38. }
  39. private void AddRelay(CommandMessage command)
  40. {
  41. string source = command.Arguments.ContainsKey("Source") ? command.Arguments["Source"] : command.Location;
  42. string target = command.Arguments.ContainsKey("Target") ? command.Arguments["Target"] : command.Nick.Nickname;
  43. string type = command.Arguments.ContainsKey("Type") ? command.Arguments["Type"] : "Message";
  44. string modes = command.Arguments.ContainsKey("Modes") ? command.Arguments["Modes"] : string.Empty;
  45. // verify access in source and target
  46. if (!CheckAccess(source, command.Nick.Nickname, command.Access))
  47. {
  48. string invalid = string.Format("You do not have permission to use '{0}' as a source.", source);
  49. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
  50. return;
  51. }
  52. if (!CheckAccess(target, command.Nick.Nickname, command.Access))
  53. {
  54. string invalid = string.Format("You do not have permission to use '{0}' as a target.", source);
  55. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
  56. return;
  57. }
  58. RelayType relayType = RelayType.Message;
  59. Enum.TryParse(type, out relayType);
  60. AddNick(command.Nick);
  61. string query = "INSERT INTO `relays` SET " +
  62. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
  63. "`nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {1} && `nickname` = {2}), " +
  64. "`source` = {3}, " +
  65. "`target` = {4}, " +
  66. "`type` = {5}, " +
  67. "`modes` = {6}, " +
  68. "`date_added` = {7}";
  69. Bot.Database.Execute(query, new object[] { Bot.ServerConfig.Name, Bot.ServerConfig.Name, command.Nick.Nickname, source, target, (int)relayType, modes, command.TimeStamp });
  70. List<Dictionary<string, object>> results = GetRelayList(command.Nick.Nickname);
  71. 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);
  72. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, relayMessage);
  73. }
  74. private void EditRelay(CommandMessage command)
  75. {
  76. string source = command.Arguments.ContainsKey("Source") ? command.Arguments["Source"] : command.Location;
  77. string target = command.Arguments.ContainsKey("Target") ? command.Arguments["Target"] : command.Nick.Nickname;
  78. string type = command.Arguments.ContainsKey("Type") ? command.Arguments["Type"] : "Message";
  79. string modes = command.Arguments.ContainsKey("Modes") ? command.Arguments["Modes"] : string.Empty;
  80. // verify access in source and target
  81. if (!CheckAccess(source, command.Nick.Nickname, command.Access))
  82. {
  83. string invalid = string.Format("You do not have permission to use '{0}' as a source.", source);
  84. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
  85. return;
  86. }
  87. if (!CheckAccess(target, command.Nick.Nickname, command.Access))
  88. {
  89. string invalid = string.Format("You do not have permission to use '{0}' as a target.", source);
  90. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
  91. return;
  92. }
  93. RelayType relayType = RelayType.Message;
  94. Enum.TryParse(type, out relayType);
  95. int num = HasValidID(command);
  96. if (num > 0)
  97. {
  98. List<Dictionary<string, object>> results = GetRelayList(command.Nick.Nickname);
  99. int id = Convert.ToInt32(results[num - 1]["id"]);
  100. string query = "UPDATE `relays` SET " +
  101. "`source` = {0}, " +
  102. "`target` = {1}, " +
  103. "`type` = {2}, " +
  104. "`modes` = {3} " +
  105. "WHERE `id` = {4}";
  106. Bot.Database.Execute(query, new object[] { source, target, (int)relayType, modes, id });
  107. string relayMessage = string.Format("Updated relay \u0002{0}\u0002 to be from \u0002{1}\u0002 to \u0002{2}\u0002.", num, source, target);
  108. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, relayMessage);
  109. }
  110. else
  111. {
  112. string invalid = "Invalid relay ID.";
  113. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
  114. }
  115. }
  116. private void DeleteRelay(CommandMessage command)
  117. {
  118. int num = HasValidID(command);
  119. if (num > 0)
  120. {
  121. List<Dictionary<string, object>> results = GetRelayList(command.Nick.Nickname);
  122. int id = Convert.ToInt32(results[num - 1]["id"]);
  123. string query = "DELETE FROM `relays` " +
  124. "WHERE `id` = {0}";
  125. Bot.Database.Execute(query, new object[] { id });
  126. string relayMessage = string.Format("Relay #\u0002{0}\u0002 has been deleted.", num);
  127. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, relayMessage);
  128. }
  129. else
  130. {
  131. string invalid = "Invalid relay ID.";
  132. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
  133. }
  134. }
  135. private void ViewRelay(CommandMessage command)
  136. {
  137. List<Dictionary<string, object>> results = GetRelayList(command.Nick.Nickname);
  138. if (command.Arguments.ContainsKey("ID"))
  139. {
  140. int num = HasValidID(command);
  141. if (num > 0)
  142. {
  143. int relayInt = 0;
  144. Int32.TryParse(results[num - 1]["type"].ToString(), out relayInt);
  145. string relayMessage = string.Format("Relay #\u0002{0}\u0002 - Source: \u0002{1}\u0002 | Target: \u0002{2}\u0002 | Type: \u0002{3}\u0002",
  146. num, results[num - 1]["source"], results[num - 1]["target"], (RelayType)relayInt);
  147. if ((RelayType)relayInt == RelayType.Mode)
  148. {
  149. relayMessage = string.Format("{0} | Modes: \u0002{1}\u0002", relayMessage, results[num - 1]["modes"]);
  150. }
  151. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, relayMessage);
  152. }
  153. else
  154. {
  155. string invalid = "Invalid relay ID.";
  156. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
  157. }
  158. }
  159. else
  160. {
  161. if (results.Any())
  162. {
  163. for (int i = 0; i < results.Count; i++)
  164. {
  165. int relayInt = 0;
  166. Int32.TryParse(results[i]["type"].ToString(), out relayInt);
  167. string relayMessage = string.Format("Relay #\u0002{0}\u0002 - Source: \u0002{1}\u0002 | Target: \u0002{2}\u0002 | Type: \u0002{3}\u0002",
  168. i + 1, results[i]["source"], results[i]["target"], (RelayType)relayInt);
  169. if ((RelayType)relayInt == RelayType.Mode)
  170. {
  171. relayMessage = string.Format("{0} | Modes: \u0002{1}\u0002", relayMessage, results[i]["modes"]);
  172. }
  173. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, relayMessage, true);
  174. }
  175. }
  176. else
  177. {
  178. string invalid = "You do not have any relays set.";
  179. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
  180. }
  181. }
  182. }
  183. private List<Dictionary<string, object>> GetRelayList(string nickname)
  184. {
  185. // Check to see if they have reached the max number of introductions
  186. string search = "SELECT `relays`.`id`, `relays`.`source`, `relays`.`target`, `relays`.`type`, `relays`.`modes` FROM `relays` " +
  187. "INNER JOIN `nicks` " +
  188. "ON `relays`.`nick_id` = `nicks`.`id` " +
  189. "INNER JOIN `servers` " +
  190. "ON `nicks`.`server_id` = `servers`.`id` " +
  191. "WHERE `servers`.`name` = {0} AND `nicks`.`nickname` = {1}";
  192. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, nickname });
  193. }
  194. private bool CheckAccess(string source, string nick, List<AccessType> types)
  195. {
  196. // Owners get to have all the fun
  197. if (Bot.ServerConfig.Owners.Contains(nick))
  198. return true;
  199. // The source is a channel
  200. if (Bot.IRC.Channels.Exists(chan => chan.Name == source))
  201. {
  202. foreach (AccessType access in types)
  203. {
  204. bool valid = Bot.CheckChannelAccess(source, nick, AccessType.Operator);
  205. if (!valid)
  206. return false;
  207. }
  208. }
  209. // The source is a nickname
  210. else
  211. {
  212. if (source != nick)
  213. return false;
  214. }
  215. return true;
  216. }
  217. /* Returns the parsed ID field if valid, otherwise returns 0 */
  218. private int HasValidID(CommandMessage command)
  219. {
  220. int num = 0;
  221. int ret = 0;
  222. List<Dictionary<string, object>> results = GetRelayList(command.Nick.Nickname);
  223. if (int.TryParse(command.Arguments["ID"], out num))
  224. {
  225. if (results.Count >= num && num > 0)
  226. {
  227. ret = num;
  228. }
  229. }
  230. return ret;
  231. }
  232. }
  233. }