示例#1
0
        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);
        }
示例#2
0
        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 */
        }
示例#3
0
 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();
 }
示例#4
0
 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);
         }
     }
 }
示例#5
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();
        }
示例#6
0
 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);
 }
示例#7
0
 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);
         }
     }
 }
示例#8
0
 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();
 }
示例#9
0
        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);
        }