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

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