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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Reflection;
  7. using Combot.IRCServices;
  8. namespace Combot.Modules
  9. {
  10. public class Module
  11. {
  12. public string Name { get; set; }
  13. public string ClassName { get; set; }
  14. public bool Enabled { get; set; }
  15. public List<string> ChannelBlacklist { get; set; }
  16. public List<string> NickBlacklist { get; set; }
  17. public List<Command> Commands { get; set; }
  18. public List<Option> Options { get; set; }
  19. public bool Loaded { get; set; }
  20. public bool ShouldSerializeLoaded()
  21. {
  22. return false;
  23. }
  24. protected Bot Bot;
  25. public Module()
  26. {
  27. SetDefaults();
  28. }
  29. public void HandleCommandEvent(CommandMessage command)
  30. {
  31. // Check to make sure the command exists, the nick or channel isn't on a blacklist, and the module is loaded.
  32. if (Loaded
  33. && !ChannelBlacklist.Contains(command.Location)
  34. && !NickBlacklist.Contains(command.Nick.Nickname)
  35. && Commands.Exists(c => c.Triggers.Contains(command.Command)
  36. && c.Enabled
  37. && !c.ChannelBlacklist.Contains(command.Location)
  38. && !c.NickBlacklist.Contains(command.Nick.Nickname)
  39. )
  40. )
  41. {
  42. // Figure out access of the nick
  43. Command cmd = Commands.Find(c => c.Triggers.Contains(command.Command));
  44. List<AccessType> nickAccessTypes = new List<AccessType>() { AccessType.User };
  45. foreach (PrivilegeMode privilege in command.Nick.Privileges)
  46. {
  47. nickAccessTypes.Add(Bot.AccessTypeMapping[privilege]);
  48. }
  49. if (Bot.ServerConfig.Owners.Contains(command.Nick.Nickname) && command.Nick.Identified)
  50. {
  51. nickAccessTypes.Add(AccessType.Owner);
  52. }
  53. // If they have the correct access for the command, send it
  54. if (cmd.AllowedAccess.Exists(access => nickAccessTypes.Contains(access)))
  55. {
  56. ParseCommand(command);
  57. }
  58. }
  59. }
  60. virtual public void Initialize() { }
  61. virtual public void ParseCommand(CommandMessage command) { }
  62. public void SetDefaults()
  63. {
  64. Name = string.Empty;
  65. ClassName = string.Empty;
  66. Enabled = false;
  67. ChannelBlacklist = new List<string>();
  68. NickBlacklist = new List<string>();
  69. Loaded = false;
  70. Commands = new List<Command>();
  71. Options = new List<Option>();
  72. }
  73. public void Copy(Module module)
  74. {
  75. Name = module.Name;
  76. ClassName = module.ClassName;
  77. Enabled = module.Enabled;
  78. ChannelBlacklist = new List<string>();
  79. foreach (string channel in module.ChannelBlacklist)
  80. {
  81. ChannelBlacklist.Add(channel);
  82. }
  83. NickBlacklist = new List<string>();
  84. foreach (string nick in module.NickBlacklist)
  85. {
  86. NickBlacklist.Add(nick);
  87. }
  88. Commands = new List<Command>();
  89. foreach (Command command in module.Commands)
  90. {
  91. Command newCommand = new Command();
  92. newCommand.Copy(command);
  93. Commands.Add(newCommand);
  94. }
  95. Options = new List<Option>();
  96. foreach (Option option in module.Options)
  97. {
  98. Option newOption = new Option();
  99. newOption.Copy(option);
  100. Options.Add(newOption);
  101. }
  102. }
  103. public Module CreateInstance(Bot bot)
  104. {
  105. Module newModule = new Module();
  106. if (!Loaded)
  107. {
  108. //create the class base on string
  109. //note : include the namespace and class name (namespace=Bot.Modules, class name=<class_name>)
  110. Assembly a = Assembly.Load("Combot");
  111. Type t = a.GetType("Combot.Modules.ModuleClasses." + ClassName);
  112. //check to see if the class is instantiated or not
  113. if (t != null)
  114. {
  115. newModule = (Module)Activator.CreateInstance(t);
  116. newModule.Copy(this);
  117. newModule.Loaded = true;
  118. newModule.Bot = bot;
  119. newModule.Initialize();
  120. }
  121. }
  122. return newModule;
  123. }
  124. public dynamic GetOptionValue(string name)
  125. {
  126. dynamic foundValue = null;
  127. Option foundOption = Options.Find(opt => opt.Name == name);
  128. if (foundOption != null)
  129. {
  130. foundValue = foundOption.Value;
  131. if (foundValue == null)
  132. {
  133. foundValue = string.Empty;
  134. }
  135. }
  136. return foundValue;
  137. }
  138. }
  139. }