internal static List <long> GetActiveChats() { List <long> chatIds = new List <long>(); Connect(); using (SqlCommand cmd = _db.CreateCommand()) { cmd.CommandText = "SELECT * FROM Chats"; try { SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { chatIds.Add(long.Parse(reader["ChatID"].ToString())); } } catch (Exception e) { FormatHelpers.Error(e.Message); CallHome.SomethingBad($"{DateTime.UtcNow.ToString()} || {e.Message}"); } } Disconnect(); return(chatIds); }
public static List <ITelegramDbEntry> GetAllMessages() { Connect(); SqlCommand cmd = _db.CreateCommand(); cmd.CommandText = "SELECT Text, Id FROM Messages"; List <TelegramDbEntry> messages = new List <TelegramDbEntry>(); try { SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { messages.Add(new TelegramDbEntry(uint.Parse(reader["Id"].ToString()), reader["Text"].ToString())); } } catch (Exception e) { FormatHelpers.Error(e.Message); CallHome.SomethingBad(DateTime.UtcNow.ToString() + "|| " + e.Message); } Disconnect(); return(messages.Cast <ITelegramDbEntry>().ToList()); /* <-- What the f**k is this monster */ }
internal static void AddMessage(long chatId, string text) { Connect(); using (SqlCommand cmd = _db.CreateCommand()) { cmd.CommandText = $"INSERT INTO Messages (Id, Text) VALUES ({Properties.Settings.Default.NextId}, @message)"; cmd.Parameters.Add("@message", System.Data.SqlDbType.NVarChar); cmd.Parameters["@message"].Value = text; try { int rows = cmd.ExecuteNonQuery(); Properties.Settings.Default.NextId++; Properties.Settings.Default.Save(); } catch (Exception e) { if (e.Message.StartsWith("Violation of PRIMARY KEY")) { Disconnect(); return; } FormatHelpers.Error(e.Message); CallHome.SomethingBad($"{DateTime.UtcNow.ToString()} || {e.Message}"); } } Disconnect(); }
public static void Handle(string[] args) { for (int i = 0; i < args.Length; i++) { if (args[i] == "--dump") { Dump = true; } else if (args[i] == "--post") { Post = true; } else if (args[i] == "--verbose" || args[i] == "-v") { Verbose = true; } else if (args[i] == "--text-file") { try { DatabaseAccess.AddMessagesFromText(args[i + 1]); } catch (IndexOutOfRangeException) { FormatHelpers.Error("No text file location specified"); Environment.Exit(0x1); } } else if (args[i] == "--log-file") { try { DatabaseAccess.AddMessagesFromLog(args[i + 1]); } catch (IndexOutOfRangeException) { FormatHelpers.Error("No text file location specified"); Environment.Exit(0x1); } } else if (args[i] == "-r") { Properties.Settings.Default.Reset(); Properties.Settings.Default.Save(); Environment.Exit(0); } } }
public static void DeactivateChat(long chatId) { Connect(); SqlCommand cmd = _db.CreateCommand(); cmd.CommandText = $"DELETE FROM Chats WHERE ChatId = {chatId}"; try { cmd.ExecuteNonQuery(); } catch (Exception e) { FormatHelpers.Error(e.Message); CallHome.SomethingBad(DateTime.UtcNow.ToString() + "|| " + e.Message); } Disconnect(); }
public static void AddMessagesFromLog(string path) { string[] file = File.ReadAllLines(path); foreach (string line in file) { try { AddMessage(TelegramDbEntry.Parse(line)); } catch (Exception e) { FormatHelpers.Error($"{e.Message} WHEN LOADING MESSAGES FROM LOG"); } } Console.WriteLine("COMPLETED."); Environment.Exit(0x0); }
static void Main(string[] args) { RunArgs.Handle(args); while (true) { try { Run(); } catch (Exception e) { FormatHelpers.Error(e.Message); ConsoleHelper.WriteLineIf(RunArgs.Verbose, "An unhandled error has occured. Waiting 60 seconds to restart..."); Thread.Sleep(60 * 1000); } } }
internal static void AddChat(long chatId) { Connect(); using (SqlCommand cmd = _db.CreateCommand()) { cmd.CommandText = $"INSERT INTO Chats VALUES ({chatId})"; try { cmd.ExecuteNonQuery(); } catch (Exception e) { FormatHelpers.Error(e.Message); CallHome.SomethingBad($"{DateTime.UtcNow.ToString()} || {e.Message}"); } } Disconnect(); }
internal static int CountMessages() { Connect(); int val; using (SqlCommand cmd = _db.CreateCommand()) { cmd.CommandText = "SELECT COUNT(*) FROM Messages"; val = 0; try { val = (int)cmd.ExecuteScalar(); } catch (Exception e) { FormatHelpers.Error(e.Message); } } Disconnect(); return(val); }
/// <summary> /// WHAT F*****G EEEEEEVER DUUUUUUDE /// </summary> public void Init() { _chatIds = DatabaseAccess.GetActiveChats(); _chain = new MarkovChain <string>(2); List <ITelegramDbEntry> messages = DatabaseAccess.GetAllMessages(); List <string> messagesStr = FormatHelpers.CollectionToString(messages).ToList(); if (RunArgs.Dump) { System.IO.File.WriteAllLines($"dbDump-{DateTime.Now.ToShortDateString()}-{DateTime.Now.Hour}.{DateTime.Now.Minute}.log", messagesStr); } foreach (ITelegramDbEntry entry in messages) { Feed(entry.Text); } if (RunArgs.Post) { SendOutMessages(); Console.WriteLine("Done. Exiting."); Environment.Exit(0); } }
public static void DumpDbToFile() { ConsoleHelper.WriteIf(RunArgs.Verbose, "Beginning timely database dump..."); System.IO.File.WriteAllLines($"dbDump-{DateString(DateTime.Now)}-{DateTime.Now.Hour}.{DateTime.Now.Minute}.log", FormatHelpers.CollectionToString(GetAllMessages())); ConsoleHelper.WriteLineIf(RunArgs.Verbose, "done."); }