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 12KB

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