/// <summary> /// Called when the Mail Database or Ignore list has changed on disk /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FileMonitorCallback(object sender, FileSystemEventArgs e) { lock (readWriteLock) { mailDatabase = MailDatabase.FromFile(Settings.Mailer_DatabaseFile); ignoreList = IgnoreList.FromFile(Settings.Mailer_IgnoreListFile); } }
/// <summary> /// Initialization of the Mailer bot /// </summary> public override void Initialize() { LogDebugToConsoleTranslated("bot.mailer.init"); LogDebugToConsoleTranslated("bot.mailer.init.db" + Settings.Mailer_DatabaseFile); LogDebugToConsoleTranslated("bot.mailer.init.ignore" + Settings.Mailer_IgnoreListFile); LogDebugToConsoleTranslated("bot.mailer.init.public" + Settings.Mailer_PublicInteractions); LogDebugToConsoleTranslated("bot.mailer.init.max_mails" + Settings.Mailer_MaxMailsPerPlayer); LogDebugToConsoleTranslated("bot.mailer.init.db_size" + Settings.Mailer_MaxDatabaseSize); LogDebugToConsoleTranslated("bot.mailer.init.mail_retention" + Settings.Mailer_MailRetentionDays + " days"); if (Settings.Mailer_MaxDatabaseSize <= 0) { LogToConsoleTranslated("bot.mailer.init_fail.db_size"); UnloadBot(); return; } if (Settings.Mailer_MaxMailsPerPlayer <= 0) { LogToConsoleTranslated("bot.mailer.init_fail.max_mails"); UnloadBot(); return; } if (Settings.Mailer_MailRetentionDays <= 0) { LogToConsoleTranslated("bot.mailer.init_fail.mail_retention"); UnloadBot(); return; } if (!File.Exists(Settings.Mailer_DatabaseFile)) { LogToConsoleTranslated("bot.mailer.create.db", Path.GetFullPath(Settings.Mailer_DatabaseFile)); new MailDatabase().SaveToFile(Settings.Mailer_DatabaseFile); } if (!File.Exists(Settings.Mailer_IgnoreListFile)) { LogToConsoleTranslated("bot.mailer.create.ignore", Path.GetFullPath(Settings.Mailer_IgnoreListFile)); new IgnoreList().SaveToFile(Settings.Mailer_IgnoreListFile); } lock (readWriteLock) { LogDebugToConsoleTranslated("bot.mailer.load.db", Path.GetFullPath(Settings.Mailer_DatabaseFile)); mailDatabase = MailDatabase.FromFile(Settings.Mailer_DatabaseFile); LogDebugToConsoleTranslated("bot.mailer.load.ignore", Path.GetFullPath(Settings.Mailer_IgnoreListFile)); ignoreList = IgnoreList.FromFile(Settings.Mailer_IgnoreListFile); } //Initialize file monitors. In case the bot needs to unload for some reason in the future, do not forget to .Dispose() them mailDbFileMonitor = new FileMonitor(Path.GetDirectoryName(Settings.Mailer_DatabaseFile), Path.GetFileName(Settings.Mailer_DatabaseFile), FileMonitorCallback); ignoreListFileMonitor = new FileMonitor(Path.GetDirectoryName(Settings.Mailer_IgnoreListFile), Path.GetFileName(Settings.Mailer_IgnoreListFile), FileMonitorCallback); RegisterChatBotCommand("mailer", Translations.Get("bot.mailer.cmd"), "mailer <getmails|addignored|getignored|removeignored>", ProcessInternalCommand); }
/// <summary> /// Initialization of the Mailer bot /// </summary> public override void Initialize() { LogDebugToConsole("邮箱机器人设置:"); LogDebugToConsole(" - 数据库文件: " + Settings.Mailer_DatabaseFile); LogDebugToConsole(" - 忽略列表: " + Settings.Mailer_IgnoreListFile); LogDebugToConsole(" - 公开互动者: " + Settings.Mailer_PublicInteractions); LogDebugToConsole(" - 每个玩家最多发送的邮件: " + Settings.Mailer_MaxMailsPerPlayer); LogDebugToConsole(" - 最大数据库大小: " + Settings.Mailer_MaxDatabaseSize); LogDebugToConsole(" - 邮件保留时间: " + Settings.Mailer_MailRetentionDays + " 天"); if (Settings.Mailer_MaxDatabaseSize <= 0) { LogToConsole("无法启用邮件功能: 邮件数据库最大大小必须大于0!"); UnloadBot(); return; } if (Settings.Mailer_MaxMailsPerPlayer <= 0) { LogToConsole("无法启用邮件功能: 每个玩家最大发送的邮件数量必须大于0!"); UnloadBot(); return; } if (Settings.Mailer_MailRetentionDays <= 0) { LogToConsole("无法启用邮件功能: 邮件保存时间必须大于0."); UnloadBot(); return; } if (!File.Exists(Settings.Mailer_DatabaseFile)) { LogToConsole("创建新的数据库文件: " + Path.GetFullPath(Settings.Mailer_DatabaseFile)); new MailDatabase().SaveToFile(Settings.Mailer_DatabaseFile); } if (!File.Exists(Settings.Mailer_IgnoreListFile)) { LogToConsole("创建新的忽略列表文件: " + Path.GetFullPath(Settings.Mailer_IgnoreListFile)); new IgnoreList().SaveToFile(Settings.Mailer_IgnoreListFile); } lock (readWriteLock) { LogDebugToConsole("正在加载数据库文件: " + Path.GetFullPath(Settings.Mailer_DatabaseFile)); mailDatabase = MailDatabase.FromFile(Settings.Mailer_DatabaseFile); LogDebugToConsole("正在加载忽略列表文件: " + Path.GetFullPath(Settings.Mailer_IgnoreListFile)); ignoreList = IgnoreList.FromFile(Settings.Mailer_IgnoreListFile); } //Initialize file monitors. In case the bot needs to unload for some reason in the future, do not forget to .Dispose() them mailDbFileMonitor = new FileMonitor(Path.GetDirectoryName(Settings.Mailer_DatabaseFile), Path.GetFileName(Settings.Mailer_DatabaseFile), FileMonitorCallback); ignoreListFileMonitor = new FileMonitor(Path.GetDirectoryName(Settings.Mailer_IgnoreListFile), Path.GetFileName(Settings.Mailer_IgnoreListFile), FileMonitorCallback); RegisterChatBotCommand("mailer", "子命令: getmails, addignored, getignored, removeignored", ProcessInternalCommand); }
/// <summary> /// Initialization of the Mailer bot /// </summary> public override void Initialize() { LogDebugToConsole("Initializing Mailer with settings:"); LogDebugToConsole(" - Database File: " + Settings.Mailer_DatabaseFile); LogDebugToConsole(" - Ignore List: " + Settings.Mailer_IgnoreListFile); LogDebugToConsole(" - Public Interactions: " + Settings.Mailer_PublicInteractions); LogDebugToConsole(" - Max Mails per Player: " + Settings.Mailer_MaxMailsPerPlayer); LogDebugToConsole(" - Max Database Size: " + Settings.Mailer_MaxDatabaseSize); LogDebugToConsole(" - Mail Retention: " + Settings.Mailer_MailRetentionDays + " days"); if (Settings.Mailer_MaxDatabaseSize <= 0) { LogToConsole("Cannot enable Mailer: Max Database Size must be greater than zero. Please review the settings."); UnloadBot(); return; } if (Settings.Mailer_MaxMailsPerPlayer <= 0) { LogToConsole("Cannot enable Mailer: Max Mails per Player must be greater than zero. Please review the settings."); UnloadBot(); return; } if (Settings.Mailer_MailRetentionDays <= 0) { LogToConsole("Cannot enable Mailer: Mail Retention must be greater than zero. Please review the settings."); UnloadBot(); return; } if (!File.Exists(Settings.Mailer_DatabaseFile)) { LogToConsole("Creating new database file: " + Path.GetFullPath(Settings.Mailer_DatabaseFile)); new MailDatabase().SaveToFile(Settings.Mailer_DatabaseFile); } if (!File.Exists(Settings.Mailer_IgnoreListFile)) { LogToConsole("Creating new ignore list: " + Path.GetFullPath(Settings.Mailer_IgnoreListFile)); new IgnoreList().SaveToFile(Settings.Mailer_IgnoreListFile); } lock (readWriteLock) { LogDebugToConsole("Loading database file: " + Path.GetFullPath(Settings.Mailer_DatabaseFile)); mailDatabase = MailDatabase.FromFile(Settings.Mailer_DatabaseFile); LogDebugToConsole("Loading ignore list: " + Path.GetFullPath(Settings.Mailer_IgnoreListFile)); ignoreList = IgnoreList.FromFile(Settings.Mailer_IgnoreListFile); } //Initialize file monitors. In case the bot needs to unload for some reason in the future, do not forget to .Dispose() them mailDbFileMonitor = new FileMonitor(Path.GetDirectoryName(Settings.Mailer_DatabaseFile), Path.GetFileName(Settings.Mailer_DatabaseFile), FileMonitorCallback); ignoreListFileMonitor = new FileMonitor(Path.GetDirectoryName(Settings.Mailer_IgnoreListFile), Path.GetFileName(Settings.Mailer_IgnoreListFile), FileMonitorCallback); RegisterChatBotCommand("mailer", "Subcommands: getmails, addignored, getignored, removeignored", ProcessInternalCommand); }
/// <summary> /// Read mail database from file /// </summary> /// <param name="filePath">Path to the database</param> /// <returns>Mail database</returns> public static MailDatabase FromFile(string filePath) { MailDatabase database = new MailDatabase(); Dictionary <string, Dictionary <string, string> > iniFileDict = INIFile.ParseFile(FileMonitor.ReadAllLinesWithRetries(filePath)); foreach (KeyValuePair <string, Dictionary <string, string> > iniSection in iniFileDict) { //iniSection.Key is "mailXX" but we don't need it here string sender = iniSection.Value["sender"]; string recipient = iniSection.Value["recipient"]; string content = iniSection.Value["content"]; DateTime timestamp = DateTime.Parse(iniSection.Value["timestamp"]); bool anonymous = INIFile.Str2Bool(iniSection.Value["anonymous"]); database.Add(new Mail(sender, recipient, content, anonymous, timestamp)); } return(database); }