using System; using System.Collections.Generic; using System.Text; using System.IO; using Bot.CommandSystem; using OpenMetaverse; using Bot.WebHookServer; using System.Collections.Specialized; namespace Bot { public sealed class GroupLog { private static readonly object _lock = new object(); private static GroupLog _in; private static readonly object _writeLock = new object(); static GroupLog() { } public static GroupLog Instance { get { lock (_lock) { if (_in == null) _in = new GroupLog(); return _in; } } } public void WriteLogEntry(string LogName, string ToAppend) { // Log filename will ALWAYS contain the date try { lock (_writeLock) { string date = DateTime.Now.ToString("M-d-yyyy"); date += " " + LogName + ".log"; if (!Directory.Exists("GroupChatLogs")) Directory.CreateDirectory("GroupChatLogs"); date = "GroupChatLogs/" + date; File.AppendAllText(date, "[" + DateTime.Now.ToString("hh:mm:ss") + "]: " + ToAppend + "\n"); } } catch (Exception e) { } } public void WriteLogEntry(bool isLocal, bool isIM, string SenderName, UUID SenderID, string Message) { string filename = ""; if (isLocal) filename = DateTime.Now.ToString("M-d-yyyy")+"-LocalChat.log"; else filename = DateTime.Now.ToString("M-d-yyyy") + "-"+SenderName + ".log"; string LogFormat = MainConfiguration.Instance.IMAndChatFormat; LogFormat=LogFormat.Replace("%TIME%", DateTime.Now.ToString("hh:mm:ss")); LogFormat = LogFormat.Replace("%NAME%", SenderName); LogFormat = LogFormat.Replace("%MESSAGE%", Message); LogFormat = LogFormat.Replace("%UUID%", SenderID.ToString()); filename = "GroupChatLogs/" + filename; try { lock (_writeLock) { if (!Directory.Exists("GroupChatLogs")) Directory.CreateDirectory("GroupChatLogs"); File.AppendAllText(filename, LogFormat + "\n"); } }catch(Exception e) { Console.WriteLine("Could not write a log using overload1 (chat or IM)\nError code: " + e.Message + "\nStack: " + e.StackTrace); } } private static readonly object _fileRead = new object(); [CommandGroupMaster("Logging")] [CommandGroup("search_log", 5, 2, "search_log [groupName] [search_term] - Searches for the search term in all logs relating to the group name (Use a underscore to show where spaces are!). The search term may also include the pipe (|) delimiter to include more than 1 word.", Bot.MessageHandler.Destinations.DEST_AGENT | Bot.MessageHandler.Destinations.DEST_LOCAL | MessageHandler.Destinations.DEST_GROUP | MessageHandler.Destinations.DEST_DISCORD)] public void search_log(UUID client, int level, GridClient grid, string[] additionalArgs, MessageHandler.MessageHandleEvent MHE, MessageHandler.Destinations source, CommandRegistry registry, UUID agentKey, string agentName) { string GrpName = additionalArgs[0].Replace('_', ' '); string[] search = additionalArgs[1].Split('|'); DirectoryInfo di = new DirectoryInfo("GroupChatLogs"); foreach (FileInfo fi in di.GetFiles()) { // check if filename contains the group name string onlyName = Path.GetFileNameWithoutExtension(fi.Name); if (onlyName.Contains(GrpName)) { // read file lock (_fileRead) { foreach (string S in File.ReadLines("GroupChatLogs/" + onlyName + ".log")) { foreach (string V in search) { if (S.Contains(V, StringComparison.OrdinalIgnoreCase)) { MHE(source, client, "{[https://zontreck.dev:35591/viewlog/" + Uri.EscapeUriString(onlyName) + " " + onlyName + "]} " + S); } } } } } } MHE(source, client, ".\n \n[Search Completed]"); } [WebhookAttribs("/viewlog/%", HTTPMethod = "GET")] public WebhookRegistry.HTTPResponseData View_Log(List arguments, string body, string method, NameValueCollection headers) { WebhookRegistry.HTTPResponseData rd = new WebhookRegistry.HTTPResponseData(); string FinalOutput = ""; lock (_fileRead) { try { foreach (string s in File.ReadLines("GroupChatLogs/" + Uri.UnescapeDataString(arguments[0]) + ".log")) { string tmp = s; string[] Ltmp = tmp.Split(' '); tmp = ""; foreach (string K in Ltmp) { if (K.StartsWith("secondlife://")) { // DO NOT ADD TO OUTPUT } else { tmp += K + " "; } } FinalOutput += tmp + "
"; } rd.Status = 200; rd.ReplyString = FinalOutput; rd.ReturnContentType = "text/html"; } catch (Exception e) { rd.Status = 418; rd.ReplyString = "You burned... the tea"; } } return rd; } [WebhookAttribs("/logs", HTTPMethod = "GET")] public WebhookRegistry.HTTPResponseData List_Logs(List arguments, string body, string method, NameValueCollection headers) { WebhookRegistry.HTTPResponseData hrd = new WebhookRegistry.HTTPResponseData(); hrd.Status = 200; hrd.ReplyString = "

Group Chat Logs

"; DirectoryInfo di = new DirectoryInfo("GroupChatLogs"); foreach (FileInfo fi in di.GetFiles()) { hrd.ReplyString += "
" + fi.Name + ""; } hrd.ReturnContentType = "text/html"; return hrd; } } }