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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.IRCServices;
  11. using Newtonsoft.Json;
  12. namespace Combot.Modules
  13. {
  14. public class Module
  15. {
  16. public string Name { get; set; }
  17. public string ClassName { get; set; }
  18. public bool Enabled { get; set; }
  19. public List<string> ChannelBlacklist { get; set; }
  20. public List<string> NickBlacklist { get; set; }
  21. public List<Command> Commands { get; set; }
  22. public List<Option> Options { get; set; }
  23. public bool Loaded { get; set; }
  24. public bool ShouldSerializeLoaded()
  25. {
  26. return false;
  27. }
  28. public string ConfigPath { get; set; }
  29. public bool ShouldSerializeConfigPath()
  30. {
  31. return false;
  32. }
  33. protected Bot Bot;
  34. private ReaderWriterLockSlim ConfigRWLock;
  35. private ReaderWriterLockSlim ConfigFileRWLock;
  36. private JsonSerializerSettings JsonSettings;
  37. public Module()
  38. {
  39. SetDefaults();
  40. ConfigRWLock = new ReaderWriterLockSlim();
  41. ConfigFileRWLock = new ReaderWriterLockSlim();
  42. JsonSettings = new JsonSerializerSettings();
  43. JsonSettings.Converters.Add(new IPAddressConverter());
  44. JsonSettings.Converters.Add(new IPEndPointConverter());
  45. JsonSettings.Formatting = Formatting.Indented;
  46. }
  47. public void HandleCommandEvent(CommandMessage command)
  48. {
  49. // Check to make sure the command exists, the nick or channel isn't on a blacklist, and the module is loaded.
  50. if (Loaded
  51. && !ChannelBlacklist.Contains(command.Location)
  52. && !NickBlacklist.Contains(command.Nick.Nickname)
  53. && Commands.Exists(c => c.Triggers.Contains(command.Command)
  54. && c.Enabled
  55. && !c.ChannelBlacklist.Contains(command.Location)
  56. && !c.NickBlacklist.Contains(command.Nick.Nickname)
  57. )
  58. )
  59. {
  60. // Figure out access of the nick
  61. Command cmd = Commands.Find(c => c.Triggers.Contains(command.Command));
  62. List<AccessType> nickAccessTypes = new List<AccessType>() { AccessType.User };
  63. foreach (PrivilegeMode privilege in command.Nick.Privileges)
  64. {
  65. nickAccessTypes.Add(Bot.PrivilegeModeMapping[privilege]);
  66. }
  67. if (Bot.ServerConfig.Owners.Contains(command.Nick.Nickname) && command.Nick.Modes.Contains(UserMode.r))
  68. {
  69. nickAccessTypes.Add(AccessType.Owner);
  70. }
  71. command.Access.AddRange(nickAccessTypes);
  72. // If they have the correct access for the command, send it
  73. if (cmd.AllowedAccess.Exists(access => nickAccessTypes.Contains(access)))
  74. {
  75. ParseCommand(command);
  76. }
  77. else
  78. {
  79. string noAccessMessage = string.Format("You do not have access to use \u0002{0}\u000F.", command.Command);
  80. switch (command.MessageType)
  81. {
  82. case MessageType.Channel:
  83. Bot.IRC.SendPrivateMessage(command.Location, noAccessMessage);
  84. break;
  85. case MessageType.Query:
  86. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, noAccessMessage);
  87. break;
  88. case MessageType.Notice:
  89. Bot.IRC.SendNotice(command.Nick.Nickname, noAccessMessage);
  90. break;
  91. }
  92. }
  93. }
  94. }
  95. virtual public void Initialize() { }
  96. virtual public void ParseCommand(CommandMessage command) { }
  97. public void SetDefaults()
  98. {
  99. Name = string.Empty;
  100. ClassName = string.Empty;
  101. Enabled = false;
  102. ChannelBlacklist = new List<string>();
  103. NickBlacklist = new List<string>();
  104. ConfigPath = Directory.GetCurrentDirectory();
  105. Loaded = false;
  106. Commands = new List<Command>();
  107. Options = new List<Option>();
  108. }
  109. public void Copy(Module module)
  110. {
  111. Name = module.Name;
  112. ClassName = module.ClassName;
  113. Enabled = module.Enabled;
  114. ChannelBlacklist = new List<string>();
  115. foreach (string channel in module.ChannelBlacklist)
  116. {
  117. ChannelBlacklist.Add(channel);
  118. }
  119. NickBlacklist = new List<string>();
  120. foreach (string nick in module.NickBlacklist)
  121. {
  122. NickBlacklist.Add(nick);
  123. }
  124. Commands = new List<Command>();
  125. foreach (Command command in module.Commands)
  126. {
  127. Command newCommand = new Command();
  128. newCommand.Copy(command);
  129. Commands.Add(newCommand);
  130. }
  131. Options = new List<Option>();
  132. foreach (Option option in module.Options)
  133. {
  134. Option newOption = new Option();
  135. newOption.Copy(option);
  136. Options.Add(newOption);
  137. }
  138. }
  139. public Module CreateInstance(Bot bot)
  140. {
  141. Module newModule = new Module();
  142. if (!Loaded)
  143. {
  144. //create the class base on string
  145. //note : include the namespace and class name (namespace=Combot.Modules, class name=<class_name>)
  146. Assembly a = Assembly.LoadFrom(Path.Combine(ConfigPath, string.Format("{0}.dll", Name)));
  147. Type t = a.GetType("Combot.Modules.Plugins." + ClassName);
  148. //check to see if the class is instantiated or not
  149. if (t != null)
  150. {
  151. newModule = (Module)Activator.CreateInstance(t);
  152. newModule.Copy(this);
  153. newModule.Loaded = true;
  154. newModule.Bot = bot;
  155. newModule.Initialize();
  156. }
  157. }
  158. return newModule;
  159. }
  160. public dynamic GetOptionValue(string name)
  161. {
  162. dynamic foundValue = null;
  163. Option foundOption = Options.Find(opt => opt.Name == name);
  164. if (foundOption != null)
  165. {
  166. foundValue = foundOption.Value;
  167. if (foundValue == null)
  168. {
  169. foundValue = string.Empty;
  170. }
  171. }
  172. return foundValue;
  173. }
  174. public void SaveConfig()
  175. {
  176. ConfigFileRWLock.EnterWriteLock();
  177. // Serialize Config
  178. ConfigRWLock.EnterReadLock();
  179. string configContents = JsonConvert.SerializeObject(this, JsonSettings);
  180. ConfigRWLock.ExitReadLock();
  181. // Save config to file
  182. string path = Path.Combine(ConfigPath, "Module.config");
  183. using (StreamWriter streamWriter = new StreamWriter(path, false))
  184. {
  185. streamWriter.Write(configContents);
  186. }
  187. ConfigFileRWLock.ExitWriteLock();
  188. }
  189. public void LoadConfig()
  190. {
  191. ConfigFileRWLock.EnterReadLock();
  192. string path = Path.Combine(ConfigPath, "Module.config");
  193. if (File.Exists(path))
  194. {
  195. string configContents;
  196. using (StreamReader streamReader = new StreamReader(path, Encoding.UTF8))
  197. {
  198. configContents = streamReader.ReadToEnd();
  199. }
  200. // Load the deserialized file into the config
  201. ConfigRWLock.EnterWriteLock();
  202. Module newModule = JsonConvert.DeserializeObject<Module>(configContents, JsonSettings);
  203. Copy(newModule);
  204. ConfigRWLock.ExitWriteLock();
  205. }
  206. ConfigFileRWLock.ExitReadLock();
  207. }
  208. }
  209. }