123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Reflection;
- using System.Threading;
- using Combot.Configurations;
- using Combot.Databases;
- using Combot.IRCServices;
- using Newtonsoft.Json;
-
- namespace Combot.Modules
- {
- public class Module
- {
- public string Name { get; set; }
- public string ClassName { get; set; }
- public bool Enabled { get; set; }
- public List<string> ChannelBlacklist { get; set; }
- public List<string> NickBlacklist { get; set; }
- public List<Command> Commands { get; set; }
- public List<Option> Options { get; set; }
-
- public bool Loaded { get; set; }
- public bool ShouldSerializeLoaded()
- {
- return false;
- }
-
- public string ConfigPath { get; set; }
- public bool ShouldSerializeConfigPath()
- {
- return false;
- }
-
- protected Bot Bot;
-
- private ReaderWriterLockSlim ConfigRWLock;
- private ReaderWriterLockSlim ConfigFileRWLock;
- private JsonSerializerSettings JsonSettings;
-
- public Module()
- {
- SetDefaults();
- ConfigRWLock = new ReaderWriterLockSlim();
- ConfigFileRWLock = new ReaderWriterLockSlim();
- JsonSettings = new JsonSerializerSettings();
- JsonSettings.Converters.Add(new IPAddressConverter());
- JsonSettings.Converters.Add(new IPEndPointConverter());
- JsonSettings.Formatting = Formatting.Indented;
- }
-
- public void HandleCommandEvent(CommandMessage command)
- {
- // Check to make sure the command exists, the nick or channel isn't on a blacklist, and the module is loaded.
- if (Loaded
- && Enabled
- && !ChannelBlacklist.Contains(command.Location)
- && !NickBlacklist.Contains(command.Nick.Nickname)
- && Commands.Exists(c => c.Triggers.Contains(command.Command)
- && c.Enabled
- && !c.ChannelBlacklist.Contains(command.Location)
- && !c.NickBlacklist.Contains(command.Nick.Nickname)
- )
- )
- {
- // Figure out access of the nick
- Command cmd = Commands.Find(c => c.Triggers.Contains(command.Command));
- List<AccessType> nickAccessTypes = new List<AccessType>() { AccessType.User };
- foreach (PrivilegeMode privilege in command.Nick.Privileges)
- {
- nickAccessTypes.Add(Bot.PrivilegeModeMapping[privilege]);
- }
- if ((Bot.ServerConfig.Owners.Contains(command.Nick.Nickname) && command.Nick.Modes.Contains(UserMode.r)) || command.Nick.Nickname == Bot.IRC.Nickname)
- {
- nickAccessTypes.Add(AccessType.Owner);
- }
- command.Access.AddRange(nickAccessTypes);
- // If they have the correct access for the command, send it
- if (cmd.AllowedAccess.Exists(access => nickAccessTypes.Contains(access)))
- {
- ParseCommand(command);
- }
- else
- {
- string noAccessMessage = string.Format("You do not have access to use \u0002{0}\u000F.", command.Command);
- switch (command.MessageType)
- {
- case MessageType.Channel:
- Bot.IRC.SendPrivateMessage(command.Location, noAccessMessage);
- break;
- case MessageType.Query:
- Bot.IRC.SendPrivateMessage(command.Nick.Nickname, noAccessMessage);
- break;
- case MessageType.Notice:
- Bot.IRC.SendNotice(command.Nick.Nickname, noAccessMessage);
- break;
- }
- }
- }
- }
-
- virtual public void Initialize() { }
-
- virtual public void ParseCommand(CommandMessage command) { }
-
- public void SetDefaults()
- {
- Name = string.Empty;
- ClassName = string.Empty;
- Enabled = false;
- ChannelBlacklist = new List<string>();
- NickBlacklist = new List<string>();
- ConfigPath = Directory.GetCurrentDirectory();
- Loaded = false;
- Commands = new List<Command>();
- Options = new List<Option>();
- }
-
- public void Copy(Module module)
- {
- Name = module.Name;
- ClassName = module.ClassName;
- Enabled = module.Enabled;
- ChannelBlacklist = new List<string>();
- foreach (string channel in module.ChannelBlacklist)
- {
- ChannelBlacklist.Add(channel);
- }
- NickBlacklist = new List<string>();
- foreach (string nick in module.NickBlacklist)
- {
- NickBlacklist.Add(nick);
- }
- Commands = new List<Command>();
- foreach (Command command in module.Commands)
- {
- Command newCommand = new Command();
- newCommand.Copy(command);
- Commands.Add(newCommand);
- }
- Options = new List<Option>();
- foreach (Option option in module.Options)
- {
- Option newOption = new Option();
- newOption.Copy(option);
- Options.Add(newOption);
- }
- }
-
- public Module CreateInstance(Bot bot)
- {
- Module newModule = new Module();
- if (!Loaded)
- {
- //create the class base on string
- //note : include the namespace and class name (namespace=Combot.Modules, class name=<class_name>)
- Assembly a = Assembly.LoadFrom(Path.Combine(ConfigPath, string.Format("{0}.dll", Name)));
- Type t = a.GetType("Combot.Modules.Plugins." + ClassName);
-
- //check to see if the class is instantiated or not
- if (t != null)
- {
- newModule = (Module)Activator.CreateInstance(t);
- newModule.Copy(this);
- newModule.Loaded = true;
- newModule.Bot = bot;
- newModule.Initialize();
- }
- }
-
- return newModule;
- }
-
- public dynamic GetOptionValue(string name)
- {
- dynamic foundValue = null;
- Option foundOption = Options.Find(opt => opt.Name == name);
- if (foundOption != null)
- {
- foundValue = foundOption.Value;
- if (foundValue == null)
- {
- foundValue = string.Empty;
- }
- }
- return foundValue;
- }
-
- public void SaveConfig()
- {
- ConfigFileRWLock.EnterWriteLock();
-
- // Serialize Config
- ConfigRWLock.EnterReadLock();
- string configContents = JsonConvert.SerializeObject(this, JsonSettings);
- ConfigRWLock.ExitReadLock();
-
- // Save config to file
- string path = Path.Combine(ConfigPath, "Module.config");
- using (StreamWriter streamWriter = new StreamWriter(path, false))
- {
- streamWriter.Write(configContents);
- }
-
- ConfigFileRWLock.ExitWriteLock();
- }
-
- public void LoadConfig()
- {
- ConfigFileRWLock.EnterReadLock();
- string path = Path.Combine(ConfigPath, "Module.config");
- if (File.Exists(path))
- {
- string configContents;
- using (StreamReader streamReader = new StreamReader(path, Encoding.UTF8))
- {
- configContents = streamReader.ReadToEnd();
- }
-
- // Load the deserialized file into the config
- ConfigRWLock.EnterWriteLock();
- Module newModule = JsonConvert.DeserializeObject<Module>(configContents, JsonSettings);
- Copy(newModule);
- ConfigRWLock.ExitWriteLock();
- }
- ConfigFileRWLock.ExitReadLock();
- }
-
- public void AddServer()
- {
- Database database = new Database(Bot.ServerConfig.Database);
- string search = "SELECT * FROM `servers` WHERE " +
- "`name` = {0}";
- List<Dictionary<string, object>> results = database.Query(search, new object[] { Bot.ServerConfig.Name });
-
- if (!results.Any())
- {
- string query = "INSERT INTO `servers` SET " +
- "`name` = {0}";
- database.Execute(query, new object[] { Bot.ServerConfig.Name });
- }
- }
-
- public void AddChannel(string channel)
- {
- Database database = new Database(Bot.ServerConfig.Database);
- string search = "SELECT * FROM `channels` WHERE " +
- "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}) AND " +
- "`name` = {1}";
- List<Dictionary<string, object>> results = database.Query(search, new object[] { Bot.ServerConfig.Name, channel });
-
- if (!results.Any())
- {
- string query = "INSERT INTO `channels` SET " +
- "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
- "`name` = {1}";
- database.Execute(query, new object[] { Bot.ServerConfig.Name, channel });
- }
- }
-
- public void AddNick(string nickname)
- {
- Database database = new Database(Bot.ServerConfig.Database);
- string search = "SELECT * FROM `nicks` WHERE " +
- "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}) AND " +
- "`nickname` = {1}";
- List<Dictionary<string, object>> results = database.Query(search, new object[] { Bot.ServerConfig.Name, nickname });
-
- if (!results.Any())
- {
- string insert = "INSERT INTO `nicks` SET " +
- "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
- "`nickname` = {1}";
- database.Execute(insert, new object[] { Bot.ServerConfig.Name, nickname });
- }
- }
-
- public string GetNickname(int id)
- {
- Database database = new Database(Bot.ServerConfig.Database);
- string search = "SELECT `nickname` FROM `nicks` " +
- "WHERE `id` = {0}";
- List<Dictionary<string, object>> results = database.Query(search, new object[] { id });
- string nickname = string.Empty;
- if (results.Any())
- {
- nickname = results.First()["nickname"].ToString();
- }
- return nickname;
- }
-
- public void SendResponse(MessageType messageType, string location, string nickname, string message)
- {
- switch (messageType)
- {
- case MessageType.Channel:
- Bot.IRC.SendPrivateMessage(location, message);
- break;
- case MessageType.Query:
- Bot.IRC.SendPrivateMessage(nickname, message);
- break;
- case MessageType.Notice:
- Bot.IRC.SendNotice(nickname, message);
- break;
- }
- }
- }
- }
|