public static bool getGroupConfigurationValue(TGChat chat, string value, bool def)
        {
            SQLQueryInstance QueryInst;
            MySqlDataReader  cur;
            var vname = SQL.escape(value);
            var ok    = SQL.Query(string.Format("SELECT `{1}` FROM xenf_groupconfigs WHERE `group`={0}", chat.id, vname), out QueryInst);

            if (!ok)
            {
                Console.WriteLine("[!] Error reading chat information for {0} -- {1}", chat.id, SQL.getLastError());
                Console.WriteLine(Environment.StackTrace);
                if (QueryInst != null)
                {
                    QueryInst.Finish();
                }
                return(def);
            }
            else
            {
                cur = QueryInst.reader;
                if (cur.HasRows)
                {
                    cur.Read();
                    var b = (int)cur[value] > 0;
                    QueryInst.Finish();
                    return(b);
                }
                else
                {
                    QueryInst.Finish();
                    InitializeGroupConfiguration(chat);
                    return(def);
                }
            }
        }
示例#2
0
        public static void DoMessageCleanup()
        {
            var query = "SELECT * FROM xenf_spoken";
            SQLQueryInstance QueryInst;
            MySqlDataReader  data;

            Stack <long> MessageCleanupIndicies = new Stack <long>(32);

            var ok1 = SQL.Query(query, out QueryInst);

            // Console.WriteLine(ok1);
            if (!ok1 || QueryInst == null)
            {
                Console.WriteLine("Failed to clean up messages {0}", SQL.getLastError());
                return;
            }
            data = QueryInst.reader;
            var MessageIndex = 0;

            while (data.Read())
            {
                var idx         = (int)data["index"];
                var grp         = (long)data["group"];
                var mid         = (long)data["messageid"];
                var lifetime    = (int)data["lifetime"];
                var whencreated = (long)data["whencreated"];

                if ((lifetime + whencreated) < Helpers.getUnixTime())
                {
                    var chat = new TGChat();
                    chat.id = grp;
                    Telegram.deleteMessage(chat, mid);
                    MessageCleanupIndicies.Push(idx);
                }


                MessageIndex++;
                if (MessageIndex > 30)
                {
                    break;
                }
            }
            QueryInst.Finish();

            while (MessageCleanupIndicies.Count > 0) // Had to do this,  no concurrent queries.
            {
                Console.WriteLine("CLEANUP?");
                var mid = MessageCleanupIndicies.Pop();
                int ra  = 0;
                var ok  = SQL.NonQuery("DELETE FROM xenf_spoken WHERE `index`=" + mid, out ra);
                if (!ok)
                {
                    Console.WriteLine("Message cannot be cleaned up {0}", SQL.getLastError());
                }
            }
        }
        public static bool writeGroupConfiguration(TGChat chat, string index, string value)
        {
            var query = "UPDATE xenf_groupconfigs SET `{0}`='{1}' WHERE `group`={2}";

            int ra = 0;

            var success = SQL.NonQuery(string.Format(query, SQL.escape(index), SQL.escape(value), chat.id), out ra);

            if (!success || ra == 0)
            {
                return(false);
            }
            return(true);
        }
        public static bool InitializeGroupConfiguration(TGChat chat)
        {
            int rowsAff = 0;
            var success = SQL.NonQuery(string.Format("INSERT INTO `xenf_groupconfigs` (`group`) VALUES({0})", chat.id), out rowsAff);

            if (!success)
            {
                Console.WriteLine("[!] Can't write configuration for {0} -- {1}", chat.id, SQL.getLastError());
                Console.WriteLine(Environment.StackTrace);
                return(false);
            }
            Console.WriteLine("Wrote new configuration index for group {0}", chat.id);
            return(true);
        }
        public static XenforceGroupConfiguration getGroupConfiguration(TGChat chat)
        {
            SQLQueryInstance QueryInst;
            MySqlDataReader  cur;
            var ok = SQL.Query(string.Format("SELECT * FROM xenf_groupconfigs WHERE `group`={0}", chat.id), out QueryInst);

            if (!ok)
            {
                Console.WriteLine("[!] Error reading chat information for {0} -- {1}", chat.id, SQL.getLastError());
                Console.WriteLine(Environment.StackTrace);
                return(null);
            }
            else
            {
                cur = QueryInst.reader;
                if (cur.HasRows)
                {
                    cur.Read();
                    XenforceGroupConfiguration b = new XenforceGroupConfiguration()
                    {
                        group             = (long)cur["group"],
                        kicktime          = (int)cur["kicktime"],
                        message           = (string)cur["message"],
                        autoban           = (int)cur["autoban"],
                        muteuntilverified = (int)cur["muteuntilverified"],
                        announcekicks     = (int)cur["announcekicks"],
                        activationmode    = ((int)cur["activationmode"])
                    };

                    QueryInst.Finish();
                    return(b);
                }
                else
                {
                    QueryInst.Finish();
                    InitializeGroupConfiguration(chat);
                    return(new XenforceGroupConfiguration());
                }
            }
        }
示例#6
0
        public static bool captcha_CheckExpired()
        {
            MySql.Data.MySqlClient.MySqlDataReader cur;
            SQLQueryInstance QueryInst;

            var ss = SQL.Query("SELECT * FROM xen_activations WHERE activated=0 OR activation_checked=0", out QueryInst);

            if (!ss)
            {
                Console.WriteLine("Query for activation checks failed {0}", SQL.getLastError());
                if (QueryInst != null)
                {
                    QueryInst.Finish();
                }
                return(false);
            }
            Stack <CaptchaActivationIndex> captchaActivationIndices = new Stack <CaptchaActivationIndex>(1024); // hax?

            // totally hax, I have to pull group configuration to check and see if the group has specific features enabled.
            // But I can't do that if I already have an SQL cursor open. So i'll have to read all of the results of it

            cur = QueryInst.reader;
            // before I can make a call to get group configuration.

            var ib = 0;

            while (cur.Read())
            {
                ib++;
                // Console.WriteLine(ib);
                var b = new CaptchaActivationIndex
                {
                    index              = (long)cur["index"],
                    activation_id      = (string)cur["activation_id"],
                    activated          = (int)cur["activated"],
                    forwho             = (long)cur["forwho"],
                    group              = (long)cur["group"],
                    whencreated        = (long)cur["whencreated"],
                    activation_checked = (int)cur["activation_checked"],
                    username           = (string)cur["username"],
                    actmessage         = (long)cur["actmessage"]
                };

                captchaActivationIndices.Push(b);
            }
            QueryInst.Finish();  // close it up.

            for (int i = 0; i < captchaActivationIndices.Count; i++)
            {
                var CurrentActivation = captchaActivationIndices.Pop();
                var chat = new TGChat();
                var user = new TGUser();
                user.id = CurrentActivation.forwho;
                chat.id = CurrentActivation.group;
                var kicktime = XenforceRoot.getGroupConfigurationValue(chat, "kicktime", 30);
                var announce = XenforceRoot.getGroupConfigurationValue(chat, "announcekicks", 1);
                var unmute   = XenforceRoot.getGroupConfigurationValue(chat, "muteuntilverified", false);
                //Console.WriteLine("Wtf {0} {1}",CurrentActivation.activated,CurrentActivation.activation_checked);
                if (CurrentActivation.activated == 0)
                {
                    if (CurrentActivation.whencreated < Helpers.getUnixTime() - (kicktime * 60))
                    {
                        Telegram.deleteMessage(chat, CurrentActivation.actmessage);
                        Telegram.kickChatMember(chat, user, 0); // kick them from the chat.
                        Console.WriteLine("Remove user?");
                        var rar = 0;
                        var ok  = SQL.NonQuery(string.Format("DELETE FROM xen_activations WHERE activation_id='{0}'", CurrentActivation.activation_id), out rar);
                        if (announce > 0)
                        {
                            var mymessage = Telegram.sendMessage(chat, CurrentActivation.username + " was removed from the chat for not completing the CAPTCHA.");
                            if (mymessage != null)
                            {
                                XenforceRoot.AddCleanupMessage(chat.id, mymessage.message_id, 30); // Clean up after 30 seconds.
                            }
                        }
                    }
                }
                else if (CurrentActivation.activated == 1 && CurrentActivation.activation_checked == 0)
                {
                    Telegram.deleteMessage(chat, CurrentActivation.actmessage);

                    var Oldmsg  = CurrentActivation.username + ", thanks for verifying you're not a robot.";
                    var actiMsg = XenforceRoot.getGroupConfigurationValue(chat, "activationmessage", "%NAME, thanks for verifying you're not a robot.");
                    if (actiMsg == null || actiMsg.Length < 5)
                    {
                        actiMsg = Oldmsg;
                    }

                    actiMsg = actiMsg.Replace("%NAME", CurrentActivation.username);

                    var mymessage = Telegram.sendMessage(chat, actiMsg);
                    var ra        = 0;
                    var ok        = SQL.NonQuery("UPDATE xen_activations SET activation_checked=1 WHERE activation_id='" + SQL.escape(CurrentActivation.activation_id) + "'", out ra);
                    if (!ok)
                    {
                        Console.WriteLine("Updating activation message failed! Might spam!!!?");
                    }
                    if (mymessage != null)
                    {
                        XenforceRoot.AddCleanupMessage(chat.id, mymessage.message_id, 30); // Clean up after 30 seconds.
                    }

                    if (unmute)
                    {
                        Telegram.restrictChatMember(chat, user, 0, true, true, true, true);
                    }
                }
            }
            return(true);
        }