Example #1
0
        public string recv()
        {
            byte[] data = new byte[2048];
            sock.Receive(data, 2048, System.Net.Sockets.SocketFlags.None);
            mail = System.Text.ASCIIEncoding.UTF8.GetString(data).Replace("\0", "");
            if (mail.Contains(" "))
            {
                if (mail.Substring(0, 4) == "PING")
                {
                    string pserv = mail.Substring(mail.IndexOf(":"), mail.Length - mail.IndexOf(":"));
                    pserv = pserv.TrimEnd((char)0);
                    mail = "PING from " + pserv + "\r" + "PONG to " + pserv;
                    send("PONG " + pserv);
                }
                else if (mail.Substring(mail.IndexOf(" ") + 1, 7) == "PRIVMSG")
                {
                    string[] tmparr = null;
                    mail = mail.Remove(0, 1);
                    tmparr = mail.Split('!');
                    rnick = tmparr[0];
                    tmparr = mail.Split(':');
                    rmsg = tmparr[1];
                    bool nickblacklisted = isBlacklisted(rnick);

                    if (nickblacklisted == false)
                    {
                        if (rmsg.Contains("!respond") == true)
                        {
                            string response = "PRIVMSG " + channel + " :Response";
                            send(response);
                        }
                        else if (rmsg.Contains("!greet "))
                        {
                            string query = rmsg.Remove(0, 7);
                            try
                            {
                                int queryindex = Int32.Parse(query);
                                if (queryindex <= greetnumber)
                                {
                                    FervorLibrary.Library Greeting = new FervorLibrary.Library();
                                    string returngreet = Greeting.greet(queryindex);

                                    string response = "PRIVMSG " + channel + " :" + returngreet;
                                    send(response);
                                }
                                else
                                {
                                    send("PRIVMSG " + channel + " :I don't know that many languages!");
                                }
                            }
                            catch (Exception ex)
                            {
                                send("PRIVMSG " + channel + " :Something went wrong: " + ex);
                            }

                        }
                        else if (rmsg.Contains("!farewell "))
                        {
                            string query = rmsg.Remove(0, 10);
                            try
                            {
                                int queryindex = Int32.Parse(query);
                                if (queryindex <= greetnumber)
                                {
                                    FervorLibrary.Library Farewelling = new FervorLibrary.Library();
                                    string returnfarewell = Farewelling.farewell(queryindex);
                                    string response = "PRIVMSG " + channel + " :" + returnfarewell;
                                    send(response);
                                }
                                else
                                {
                                    send("PRIVMSG " + channel + " :I don't know that many languages!");
                                }
                            }
                            catch (Exception ex)
                            {
                                send("PRIVMSG " + channel + " :Something went wrong: " + ex);
                            }
                        }
                        else if (rmsg.Contains("!md5 "))
                        {
                            generateMD5message ();
                        }
                        else if (rmsg.Contains("!register "))
                        {
                            reqreguser ();
                        }
                        else if (rmsg.Contains("!registerlist"))
                        {
                            listregs ();
                        }
                        else if (rmsg.Contains("!clearregisterlist"))
                        {
                            clearregs ();
                        }
                        else if (rmsg.Contains("!active "))
                        {
                            activateUser ();
                        }
                        else if (rmsg.Contains("!deactivate"))
                        {
                            deactivateUser ();
                        }
                        else if (rmsg.Contains("!adduser "))
                        {
                            System.IO.StreamReader sr = new StreamReader(".activeusers");
                            string[] users = sr.ReadToEnd().Split(':');
                            sr.Close();
                            foreach (string x in users)
                            {
                                if (x.Contains(rnick))
                                {
                                    string query = rmsg.Remove(0, 9);
                                    ObsidianFunctions.Functions obsidfunc = new ObsidianFunctions.Functions();
                                    string list = obsidfunc.addUser(query);
                                    send("PRIVMSG " + channel + " :" + list);
                                }
                            }
                        }

                        else if (rmsg.Contains("!removeuser "))
                        {
                            System.IO.StreamReader sr = new StreamReader(".activeusers");
                            string[] users = sr.ReadToEnd().Split(':');
                            sr.Close();
                            foreach (string x in users)
                            {
                                if (x.Contains(rnick))
                                {
                                    string query = rmsg.Remove(0, 12);
                                    ObsidianFunctions.Functions obsidfunc = new ObsidianFunctions.Functions();
                                    string list = obsidfunc.removeUser(query);
                                    send("PRIVMSG " + channel + " :" + list);
                                }
                            }
                        }
                        else if (rmsg.Contains("!userlist"))
                        {
                            listUsers();
                        }
                        else if (rmsg.Contains("!botquit"))
                        {
                            bool nickisuser = isActiveUser(rnick);
                            if (nickisuser == true)
                            {
                                send("QUIT");
                            }
                            else
                            {
                                send("PRIVMSG " + channel + " :Insufficient permissions!");
                            }
                        }
                        else if (rmsg.Contains("!addops "))
                        {
                            if (isOperator == true)
                            {
                                string query = rmsg.Remove(0, 8);
                                bool nickisuser = isActiveUser(rnick);
                                if (nickisuser == true)
                                {
                                    send("MODE " + channel + " +o " + query);
                                }
                                else
                                {
                                    send("PRIVMSG " + channel + " :Insufficient permissions!");
                                }
                            }
                        }
                        else if (rmsg.Contains("!removeops "))
                        {
                            if (isOperator == true)
                            {
                                string query = rmsg.Remove(0, 11);
                                bool nickisuser = isActiveUser(rnick);
                                if (nickisuser == true)
                                {
                                    send("MODE " + channel + " -o " + query);
                                }
                                else
                                {
                                    send("PRIVMSG " + channel + " :Insufficient permissions!");
                                }
                            }
                        }
                        else if (rmsg.Contains("!kick "))
                        {
                            if (isOperator == true)
                            {
                                string query = rmsg.Remove(0, 6);
                                bool nickuser = isActiveUser(rnick);
                                if (nickuser == true)
                                {
                                    send("KICK " + channel + " " + query + " User-requested kick");
                                }
                                else
                                {
                                    send("PRIVMSG " + channel + " :Insufficient permissions!");
                                }
                            }
                        }

                        else if (rmsg.Contains("!batch "))
                        {
                            string query = rmsg.Remove(0, 7);
                            bool nickuser = isActiveUser(rnick);
                            if (rnick == ownernick && nickuser == true)
                            {
                                ObsidianFunctions.Functions ObsidFunc = new ObsidianFunctions.Functions();
                                ObsidFunc.batch(query);
                                send("PRIVMSG " + channel + " :Success!");
                            }
                            else
                            {
                                send("PRIVMSG " + channel + " :Insufficient Permissions!");
                            }
                        }
                        else if (rmsg.Contains("!cscompile "))
                        {
                            string query = rmsg.Remove(0, 11).Replace("\0", "").Trim();
                            bool nickuser = isActiveUser(rnick);
                            if (rnick == ownernick && nickuser == true)
                            {
                                ObsidianFunctions.Functions ObsidFunc = new Functions();
                                say(channel, ObsidFunc.CSCompile(query));

                            }
                            else
                            {
                                send("PRIVMSG " + channel + " :Insufficient Permissions!");
                            }
                        }
                        else if (rmsg.Contains("!log start"))
                        {
                            bool nickuser = isActiveUser(rnick);
                            if (nickuser == true)
                            {
                                isLogging = true;
                                send("PRIVMSG " + channel + " :Success!");
                            }
                            else
                            {
                                send("PRIVMSG " + channel + " :Insufficient Permissions!");
                            }
                        }
                        else if (rmsg.Contains("!log stop"))
                        {
                            bool nickuser = isActiveUser(rnick);
                            if (nickuser == true)
                            {
                                isLogging = false;
                                send("PRIVMSG " + channel + " :Success!");
                            }
                            else
                            {
                                send("PRIVMSG " + channel + " :Insufficient Permissions!");
                            }
                        }
                        else if (rmsg.Contains("!calc "))
                        {
                            ObsidianFunctions.Functions ObsidFunc = new ObsidianFunctions.Functions();
                            string query = rmsg.Remove(0, 6);
                            float answer = ObsidFunc.calc(query);
                            send("PRIVMSG " + channel + " :" + answer.ToString());
                        }
                        else if (rmsg.Contains("!isOperator true"))
                        {
                            bool nickuser = isActiveUser(rnick);
                            if (nickuser == true)
                            {
                                isOperator = true;
                                send("PRIVMSG " + channel + " :isOperator = true");
                            }
                            else
                            {
                                send("PRIVMSG " + channel + " :Insufficient Permissions!");
                            }
                        }
                        else if (rmsg.Contains("!isOperator false"))
                        {
                            bool nickuser = isActiveUser(rnick);
                            if (nickuser == true)
                            {
                                isOperator = false;
                                send("PRIVMSG " + channel + " :isOperator = false");
                            }
                            else
                            {
                                send("PRIVMSG " + channel + " :Insufficient Permissions!");
                            }
                        }
                        else if (rmsg.Contains("!botchat"))
                        {
                            if (talkingTo != "nobody")
                            {
                                send("PRIVMSG " + rnick + " :Sorry, I'm already talking with someone");
                            }
                            else
                            {
                                talkingTo = rnick;
                                FervorLibrary.Library Greetings = new FervorLibrary.Library();
                                Random rand = new Random();
                                int indexgreet = rand.Next(0, greetnumber);
                                string greeting = Greetings.Greeting(rnick, indexgreet);
                                send("PRIVMSG " + rnick + " :" + greeting);
                                botChat();
                            }

                        }
                        else if (rmsg.Contains("quit"))
                        {
                            if (rnick == talkingTo)
                            {
                                talkingTo = "nobody";
                            }
                        }
                        else if (rnick == talkingTo)
                        {

                            Request r = new Request(rmsg, chatUser, chatBot);
                            Result res = chatBot.Chat(r);
                            send("PRIVMSG " + talkingTo + " :" + res.Output);

                        }
                        else if (rmsg.Contains("!udefine "))
                        {
                            string query = rmsg.Remove(0, 9);
                            ObsidianFunctions.Functions ObsidFunc = new ObsidianFunctions.Functions();
                            string definition = ObsidFunc.uDefine(query);
                            send("PRIVMSG " + channel + " :" + definition);
                        }
                        else if (rmsg.Contains("!ddefine "))
                        {
                            string query = rmsg.Remove(0, 9);
                            ObsidianFunctions.Functions ObsidFunc = new ObsidianFunctions.Functions();
                            string definition = ObsidFunc.dDefine(query);
                            send("PRIVMSG " + channel + " :" + definition);
                        }
                        else if (rmsg.Contains("!canGreet true"))
                        {
                            bool nickuser = isActiveUser(rnick);
                            if (nickuser == true)
                            {
                                canGreet = true;
                                send("PRIVMSG " + channel + " :Success!");
                            }
                            else
                            {
                                send("PRIVMSG " + channel + " :Insufficient permissions!");
                            }
                        }
                        else if (rmsg.Contains("!canGreet false"))
                        {
                            bool nickuser = isActiveUser(rnick);
                            if (nickuser == true)
                            {
                                canGreet = false;
                                send("PRIVMSG " + channel + " :Success!");
                            }
                            else
                            {
                                send("PRIVMSG " + channel + " :Insufficient permissions!");
                            }
                        }
                        else if (rmsg.Contains("!wdefine "))
                        {
                            string query = rmsg.Remove(0, 9);
                            ObsidianFunctions.Functions ObsidFunc = new ObsidianFunctions.Functions();
                            string definition = ObsidFunc.wDefine(query);
                            send("PRIVMSG " + channel + " :" + definition);
                        }
                        else if (rmsg.Contains("!sha1 "))
                        {
                            Thread sha1hash = new Thread(generatesha1message);
                            sha1hash.Start();
                        }
                        else if (rmsg.Contains("!sha256 "))
                        {
                            Thread sha256hash = new Thread(generatesha256message);
                            sha256hash.Start();
                        }
                        else if (rmsg.Contains("!sha384 "))
                        {
                            Thread sha384hash = new Thread(generatesha384message);
                            sha384hash.Start();
                        }
                        else if (rmsg.Contains("!sha512 "))
                        {
                            Thread sha512hash = new Thread(generatesha512message);
                            sha512hash.Start();
                        }
                        else if (rmsg.Contains("!blacklistadd "))
                        {
                            bool nickuser = isActiveUser(rnick);
                            if (nickuser == true)
                            {
                                string query = rmsg.Remove(0, 14);
                                Functions ObsidFunc = new Functions();
                                blacklist = ObsidFunc.addBlacklist(query);
                                string newlist = String.Join(":", blacklist);
                                send("PRIVMSG " + channel + " :" + newlist);
                            }
                            else
                            {
                                send("PRIVMSG " + channel + " :Insufficient Permissions!");
                            }
                        }
                        else if (rmsg.Contains("!blacklistremove "))
                        {
                            bool nickuser = isActiveUser(rnick);
                            if (nickuser == true)
                            {
                                string query = rmsg.Remove(0, 17);
                                Functions ObsidFunc = new Functions();
                                blacklist = ObsidFunc.removeBlacklist(query);
                                string newlist = String.Join(":", blacklist);
                                if (newlist == null || newlist == "")
                                {
                                    newlist = "None";
                                }
                                send("PRIVMSG " + channel + " :" + newlist);
                            }
                            else
                            {
                                send("PRIVMSG " + channel + ":Insufficient Permissions!");
                            }
                        }
                        else if (rmsg.Contains("!blacklist"))
                        {
                            listBlacklist();
                        }
                        else if (rmsg.Contains("!get "))
                        {
                            string query = rmsg.Remove(0, 5);
                            if (query.StartsWith("me "))
                            {
                                string query2 = query.Remove(0, 3);
                                query = rnick + " " + query2;
                            }
                            say(channel, "Let me get that for you!");
                            say(channel, "\u0001ACTION " + "gives " + query + "\u0001");
                            say(channel, "There you go!");
                        }
                        else if (rmsg.Contains("!google "))
                        {
                            string query = rmsg.Remove(0, 8);
                            string googleURL = "http://google.com/search?q=" + query;
                            say(channel, googleURL);
                        }
                        else if (rmsg.Contains("!ircuserlist"))
                        {
                            Thread listircusers = new Thread(channelUsers);
                            listircusers.Start();
                            Thread saylistircusers = new Thread(saychannelUsers);
                            saylistircusers.Start();
                        }
                        else if (rmsg.Contains("!message "))
                        {
                            try
                            {
                                string sender = rnick;
                                string query = rmsg.Remove(0, 9);
                                string[] parsenick = query.Split('>');
                                string recipient = parsenick[0];
                                string message = "<" + sender + ">" + parsenick[1].Replace("~", "");
                                bool nickOnline = isOnline(recipient);
                                if (nickOnline == true)
                                {
                                    say(recipient, message);
                                    say(sender, "Message sent!");
                                }
                                else if (nickOnline == false)
                                {
                                    say(sender, "I'll tell " + recipient + " when he or she is online.");
                                    addMessage(recipient, message);
                                }

                            }
                            catch (Exception ex)
                            {
                                say(rnick, ex.ToString());
                            }
                        }
                        else if (rmsg.Contains("!spamControl true"))
                        {
                            bool nickuser = isActiveUser(rnick);
                            if (nickuser == true)
                            {
                                controlSpam = true;
                                say(channel, "spamControl = true");
                            }
                            else
                            {
                                say(rnick, "Insufficient permissions!");
                            }
                        }
                        else if (rmsg.Contains("!spamControl false"))
                        {
                            bool nickuser = isActiveUser(rnick);
                            if (nickuser == true)
                            {
                                controlSpam = false;
                                say(channel, "spamControl = false");
                            }
                            else
                            {
                                say(rnick, "Insufficient permissions!");
                            }
                        }
                        else if (rmsg.Contains("forcequit"))
                        {
                            bool nickuser = isActiveUser(rnick);
                            if (nickuser == true)
                            {
                                talkingTo = "nobody";
                            }
                            else
                            {
                                say(rnick, "Insufficient permissions!");
                            }
                        }
                        //commands end
                    }
                    if (controlSpam == true)
                    {
                        if (rmsg == oldmsg)
                        {
                            increaseSpamCount();
                            if (spamcount >= 4)
                            {
                                if (isOperator == true)
                                {
                                    send("KICK " + channel + " " + rnick + " No Spamming or Repeating one's self");
                                    spamcount = 0;
                                }
                                else
                                {
                                    send("PRIVMSG " + channel + " :Try not to spam or excessively repeat yourself");
                                    spamcount = 0;
                                }
                            }

                        }
                    }
                    if (isLogging == true)
                    {
                        logMsg();
                    }
                    detectLang();
                    bool newMessages = hasMessages(rnick);
                    if (newMessages == true)
                    {
                        sayMessages(rnick);
                    }
                }
                else if (mail.Substring(mail.IndexOf(" ") + 1, 4) == "JOIN")
                {

                    string[] tmparr = null;
                    mail = mail.Remove(0, 1);
                    tmparr = mail.Split('!');
                    rnick = tmparr[0];
                    if (canGreet == true)
                    {
                        FervorLibrary.Library Greetings = new FervorLibrary.Library();
                        Random rand = new Random();
                        int indexgreet = rand.Next(0, greetnumber);
                        string greeting = Greetings.Greeting(rnick, indexgreet);
                        string greetingmessage = "PRIVMSG " + channel + " :" + greeting;
                        send(greetingmessage);
                    }
                    bool newMessages = hasMessages(rnick);
                    if (newMessages == true)
                    {
                        sayMessages(rnick);
                    }
                }
                else if (mail.Substring(mail.IndexOf(" ") + 1, 4) == "PART" | mail.Substring(mail.IndexOf(" ") + 1, 4) == "QUIT")
                {

                    string[] tmparr = null;
                    mail = mail.Remove(0, 1);
                    tmparr = mail.Split('!');
                    rnick = tmparr[0];
                    if (canGreet == true)
                    {
                        FervorLibrary.Library Farewells = new FervorLibrary.Library();
                        Random rand = new Random();
                        int indexfarewell = rand.Next(0, farewellnumber);
                        string farewell = Farewells.Farewell(rnick, indexfarewell);
                        string farewellmessage = "PRIVMSG " + channel + " :" + farewell;
                        send(farewellmessage);
                    }
                    Thread deactive = new Thread(deactivateUser);
                    deactive.Start();
                    if (rnick == talkingTo)
                    {
                        talkingTo = "nobody";
                    }

                }
                else if (mail.Substring(mail.IndexOf(" ") + 1, 4) == "MODE")
                {
                    mail = mail.Replace("\0", "").Trim();
                    int nameopslength = nick.Length + 3;
                    string action = mail.Substring(mail.Length - nameopslength);
                    if (action.StartsWith("+o") | action.StartsWith("+r") | action.StartsWith("+h"))
                    {
                        isOperator = true;
                        send("PRIVMSG " + channel + " :isOperator = true");

                    }
                    else if (action.StartsWith("-o") | action.StartsWith("-r") | action.StartsWith("-h"))
                    {
                        isOperator = false;
                        send("PRIVMSG " + channel + " :isOperator = false");
                    }

                }
                else if (mail.Substring(mail.IndexOf(" ") + 1, 4) == "KICK")
                {
                    string[] tmparr = null;
                    mail = mail.Remove(0, 1);
                    tmparr = mail.Split('!');
                    rnick = tmparr[0];
                    tmparr = mail.Split(':');
                    rmsg = tmparr[1];
                    deactivateUser();
                }
                else if (mail.Substring(mail.IndexOf(" ") + 1, 4) == "NICK")
                {
                    string[] tmparr = null;
                    mail = mail.Remove(0, 1);
                    tmparr = mail.Split('!');
                    rnick = tmparr[0];
                    tmparr = mail.Split(':');
                    string newnick = tmparr[1];
                    deactivateUser();
                    bool newMessages = hasMessages(newnick);
                    if (newMessages == true)
                    {
                        sayMessages(newnick);
                    }
                }
            }
            oldMsg();
            return mail;
        }