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.

Module.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Reflection;
  8. using System.Threading;
  9. using Combot.Configurations;
  10. using Combot.Databases;
  11. using Combot.IRCServices;
  12. using Newtonsoft.Json;
  13. namespace Combot.Modules
  14. {
  15. public class Module
  16. {
  17. public string Name { get; set; }
  18. public string ClassName { get; set; }
  19. public bool Enabled { get; set; }
  20. public List<string> ChannelBlacklist { get; set; }
  21. public List<string> NickBlacklist { get; set; }
  22. public List<Command> Commands { get; set; }
  23. public List<Option> Options { get; set; }
  24. public bool Loaded { get; set; }
  25. public bool ShouldSerializeLoaded()
  26. {
  27. return false;
  28. }
  29. public string ConfigPath { get; set; }
  30. public bool ShouldSerializeConfigPath()
  31. {
  32. return false;
  33. }
  34. protected Bot Bot;
  35. private ReaderWriterLockSlim ConfigRWLock;
  36. private ReaderWriterLockSlim ConfigFileRWLock;
  37. private JsonSerializerSettings JsonSettings;
  38. public Module()
  39. {
  40. SetDefaults();
  41. ConfigRWLock = new ReaderWriterLockSlim();
  42. ConfigFileRWLock = new ReaderWriterLockSlim();
  43. JsonSettings = new JsonSerializerSettings();
  44. JsonSettings.Converters.Add(new IPAddressConverter());
  45. JsonSettings.Converters.Add(new IPEndPointConverter());
  46. JsonSettings.Formatting = Formatting.Indented;
  47. }
  48. public void HandleCommandEvent(CommandMessage command)
  49. {
  50. // Check to make sure the command exists, the nick or channel isn't on a blacklist, and the module is loaded.
  51. if (Loaded
  52. && Enabled
  53. && !ChannelBlacklist.Contains(command.Location)
  54. && !NickBlacklist.Contains(command.Nick.Nickname)
  55. && Commands.Exists(c => c.Triggers.Contains(command.Command)
  56. && c.Enabled
  57. && !c.ChannelBlacklist.Contains(command.Location)
  58. && !c.NickBlacklist.Contains(command.Nick.Nickname)
  59. )
  60. )
  61. {
  62. // Figure out access of the nick
  63. Command cmd = Commands.Find(c => c.Triggers.Contains(command.Command));
  64. List<AccessType> nickAccessTypes = new List<AccessType>() { AccessType.User };
  65. foreach (PrivilegeMode privilege in command.Nick.Privileges)
  66. {
  67. nickAccessTypes.Add(Bot.PrivilegeModeMapping[privilege]);
  68. }
  69. if ((Bot.ServerConfig.Owners.Contains(command.Nick.Nickname) && command.Nick.Modes.Contains(UserMode.r)) || command.Nick.Nickname == Bot.IRC.Nickname)
  70. {
  71. nickAccessTypes.Add(AccessType.Owner);
  72. }
  73. command.Access.AddRange(nickAccessTypes);
  74. // If they have the correct access for the command, send it
  75. if (cmd.AllowedAccess.Exists(access => nickAccessTypes.Contains(access)))
  76. {
  77. ParseCommand(command);
  78. }
  79. else
  80. {
  81. string noAccessMessage = string.Format("You do not have access to use \u0002{0}\u000F.", command.Command);
  82. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, noAccessMessage);
  83. }
  84. }
  85. }
  86. virtual public void Initialize() { }
  87. virtual public void ParseCommand(CommandMessage command) { }
  88. public void SetDefaults()
  89. {
  90. Name = string.Empty;
  91. ClassName = string.Empty;
  92. Enabled = false;
  93. ChannelBlacklist = new List<string>();
  94. NickBlacklist = new List<string>();
  95. ConfigPath = Directory.GetCurrentDirectory();
  96. Loaded = false;
  97. Commands = new List<Command>();
  98. Options = new List<Option>();
  99. }
  100. public void Copy(Module module)
  101. {
  102. Name = module.Name;
  103. ClassName = module.ClassName;
  104. Enabled = module.Enabled;
  105. ChannelBlacklist = new List<string>();
  106. foreach (string channel in module.ChannelBlacklist)
  107. {
  108. ChannelBlacklist.Add(channel);
  109. }
  110. NickBlacklist = new List<string>();
  111. foreach (string nick in module.NickBlacklist)
  112. {
  113. NickBlacklist.Add(nick);
  114. }
  115. Commands = new List<Command>();
  116. foreach (Command command in module.Commands)
  117. {
  118. Command newCommand = new Command();
  119. newCommand.Copy(command);
  120. Commands.Add(newCommand);
  121. }
  122. Options = new List<Option>();
  123. foreach (Option option in module.Options)
  124. {
  125. Option newOption = new Option();
  126. newOption.Copy(option);
  127. Options.Add(newOption);
  128. }
  129. }
  130. public Module CreateInstance(Bot bot)
  131. {
  132. Module newModule = new Module();
  133. if (!Loaded)
  134. {
  135. //create the class base on string
  136. //note : include the namespace and class name (namespace=Combot.Modules, class name=<class_name>)
  137. Assembly a = Assembly.LoadFrom(Path.Combine(ConfigPath, string.Format("{0}.dll", Name)));
  138. Type t = a.GetType("Combot.Modules.Plugins." + ClassName);
  139. //check to see if the class is instantiated or not
  140. if (t != null)
  141. {
  142. newModule = (Module)Activator.CreateInstance(t);
  143. newModule.Copy(this);
  144. newModule.Loaded = true;
  145. newModule.ConfigPath = ConfigPath;
  146. newModule.Bot = bot;
  147. newModule.Initialize();
  148. }
  149. }
  150. return newModule;
  151. }
  152. public object GetOptionValue(string name)
  153. {
  154. object foundValue = null;
  155. Option foundOption = Options.Find(opt => opt.Name == name);
  156. if (foundOption != null)
  157. {
  158. foundValue = foundOption.Value;
  159. if (foundValue == null)
  160. {
  161. foundValue = string.Empty;
  162. }
  163. }
  164. return foundValue;
  165. }
  166. public void SaveConfig()
  167. {
  168. ConfigFileRWLock.EnterWriteLock();
  169. // Serialize Config
  170. ConfigRWLock.EnterReadLock();
  171. string configContents = JsonConvert.SerializeObject(this, JsonSettings);
  172. ConfigRWLock.ExitReadLock();
  173. // Save config to file
  174. string path = Path.Combine(ConfigPath, "Module.config");
  175. using (StreamWriter streamWriter = new StreamWriter(path, false))
  176. {
  177. streamWriter.Write(configContents);
  178. }
  179. ConfigFileRWLock.ExitWriteLock();
  180. }
  181. public void LoadConfig()
  182. {
  183. ConfigFileRWLock.EnterReadLock();
  184. string path = Path.Combine(ConfigPath, "Module.config");
  185. if (File.Exists(path))
  186. {
  187. string configContents;
  188. using (StreamReader streamReader = new StreamReader(path, Encoding.UTF8))
  189. {
  190. configContents = streamReader.ReadToEnd();
  191. }
  192. // Load the deserialized file into the config
  193. ConfigRWLock.EnterWriteLock();
  194. Module newModule = JsonConvert.DeserializeObject<Module>(configContents, JsonSettings);
  195. Copy(newModule);
  196. ConfigRWLock.ExitWriteLock();
  197. }
  198. ConfigFileRWLock.ExitReadLock();
  199. }
  200. public void AddServer()
  201. {
  202. string search = "SELECT * FROM `servers` WHERE " +
  203. "`name` = {0}";
  204. List<Dictionary<string, object>> results = Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name });
  205. if (!results.Any())
  206. {
  207. string query = "INSERT INTO `servers` SET " +
  208. "`name` = {0}";
  209. Bot.Database.Execute(query, new object[] { Bot.ServerConfig.Name });
  210. }
  211. }
  212. public void AddChannel(string channel)
  213. {
  214. string search = "SELECT * FROM `channels` WHERE " +
  215. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}) AND " +
  216. "`name` = {1}";
  217. List<Dictionary<string, object>> results = Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, channel });
  218. if (!results.Any())
  219. {
  220. string query = "INSERT INTO `channels` SET " +
  221. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
  222. "`name` = {1}";
  223. Bot.Database.Execute(query, new object[] { Bot.ServerConfig.Name, channel });
  224. }
  225. }
  226. public void AddNick(string nickname)
  227. {
  228. string search = "SELECT * FROM `nicks` WHERE " +
  229. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}) AND " +
  230. "`nickname` = {1}";
  231. List<Dictionary<string, object>> results = Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, nickname });
  232. if (!results.Any())
  233. {
  234. string insert = "INSERT INTO `nicks` SET " +
  235. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
  236. "`nickname` = {1}";
  237. Bot.Database.Execute(insert, new object[] { Bot.ServerConfig.Name, nickname });
  238. }
  239. }
  240. public string GetNickname(int id)
  241. {
  242. string search = "SELECT `nickname` FROM `nicks` " +
  243. "WHERE `id` = {0}";
  244. List<Dictionary<string, object>> results = Bot.Database.Query(search, new object[] { id });
  245. string nickname = string.Empty;
  246. if (results.Any())
  247. {
  248. nickname = results.First()["nickname"].ToString();
  249. }
  250. return nickname;
  251. }
  252. public void SendResponse(MessageType messageType, string location, string nickname, string message)
  253. {
  254. switch (messageType)
  255. {
  256. case MessageType.Channel:
  257. Bot.IRC.Command.SendPrivateMessage(location, message);
  258. break;
  259. case MessageType.Query:
  260. Bot.IRC.Command.SendPrivateMessage(nickname, message);
  261. break;
  262. case MessageType.Notice:
  263. Bot.IRC.Command.SendNotice(nickname, message);
  264. break;
  265. }
  266. }
  267. }
  268. }