BitServer.Program.SMTP_SMTPcommand C# (CSharp) Method

SMTP_SMTPcommand() protected static method

protected static SMTP_SMTPcommand ( SMTPconnection SMTP, string command, string args, string raw ) : void
SMTP SMTPconnection
command string
args string
raw string
return void
        protected static void SMTP_SMTPcommand(SMTPconnection SMTP, string command, string[] args, string raw)
        {
            lock (SMTP)
            {
                if (SMS.onData)
                {
                    if (raw == ".")
                    {
                        SMS.onData = false;
                        SMTP.dataMode = false;
                        if (SMS.spam)
                        {
                            SMTP.msg(521, "Someone gets nasty!");
                            //spam protection to be implemented
                        }
                        else
                        {
                            if (SMS.isCommand)
                            {
                                switch (SMS.subject.ToLower().Split(' ')[0])
                                {
                                    case "help":
                                        adminMsg("OK: BMS Help", Troll.ADMINHELP);

                                        break;
                                    case "status":
                                        if (SMS.subject.Split(' ').Length > 1)
                                        {
                                            BitAPIserver.BA.statusBar(SMS.subject.Split(new char[] { ' ' }, 2)[1]);
                                            adminMsg("OK: status", "status set to " + SMS.subject.Split(new char[] { ' ' }, 2)[1]);
                                        }
                                        else
                                        {
                                            adminMsg("ERR: status", "cannot set status. You need to specify the value for it");
                                        }
                                        break;
                                    case "killall":
                                        foreach (BitMsg m in Bitmessage.getMessages(BitAPIserver.BA))
                                        {
                                            BitAPIserver.BA.trashMessage(m.msgid);
                                        }
                                        adminMsg("OK: killall", "All messages deleted.");
                                        break;
                                    case "createaddr":
                                        if (SMS.subject.Split(' ').Length == 2)
                                        {
                                            adminMsg("OK: Address generation", Bitmessage.generateAddress(BitAPIserver.BA, SMS.subject.Split(new char[]{' '},2)[1], "B2M generated addr"));
                                        }
                                        else
                                        {
                                            adminMsg("OK: Address generation", Bitmessage.generateAddress(BitAPIserver.BA, "B2M generated addr"));
                                        }
                                        break;
                                    case "listsub":
                                        string sublist = string.Format("{0} {1} Enabled\r\n", size("Address", 38), size("Label", 20));
                                        foreach (Subscription s in Bitmessage.getSubscriptions(BitAPIserver.BA))
                                        {
                                            sublist += string.Format("{0} {1} {2}\r\n",
                                                size(s.address, 38), size(s.label, 20), size(s.enabled.ToString(), 7));
                                        }
                                        adminMsg("OK: Subscription list", sublist);
                                        break;
                                    case "list":
                                        string addrlist = string.Format("{0} {1} Enabled Stream\r\n",
                                            size("Address",38),size("Label",20));
                                        foreach (BitAddr a in Bitmessage.getAddresses(BitAPIserver.BA))
                                        {
                                            addrlist += string.Format("{0} {1} {2} {3}\r\n",
                                                size(a.address,38), size(a.label,20), size(a.enabled.ToString(),7), size(a.stream.ToString(),6));
                                        }
                                        adminMsg("OK: Address list", addrlist);
                                        break;
                                    case "subscribe":
                                    case "sub":
                                        if (SMS.subject.Split(' ').Length >= 2)
                                        {
                                            if (SMS.subject.Split(' ').Length > 2)
                                            {
                                                BitAPIserver.BA.addSubscription(SMS.subject.Split(' ')[1], B64e(SMS.subject.Split(new char[] { ' ' }, 3)[2]));
                                            }
                                            else
                                            {
                                                BitAPIserver.BA.addSubscription(SMS.subject.Split(' ')[1], B64e("--NO NAME--"));
                                            }
                                            adminMsg("OK: subscribe", "subscribed to " + SMS.subject.Split(' ')[1]);
                                        }
                                        else
                                        {
                                            adminMsg("ERR: subscribe", "You need to specify an address");
                                        }
                                        break;
                                    case "unsubscribe":
                                    case "usub":
                                        if (SMS.subject.Split(' ').Length == 2)
                                        {
                                            BitAPIserver.BA.deleteSubscription(SMS.subject.Split(' ')[1]);
                                            adminMsg("OK: unsubscribe", string.Format("Subscription for {0} deleted",SMS.subject.Split(' ')[1]));
                                        }
                                        else
                                        {
                                            adminMsg("ERR: unsubscribe", "You need to specify an address");
                                        }
                                        break;
                                    default:
                                        adminMsg("ERR: unknown command", string.Format(@"The command you used is not valid.
            To get a list of valid commands, use the 'help' command.
            Your command line: {0}",SMS.subject));
                                        break;
                                }
                            }
                            else
                            {
                                if (BS.StripHdr)
                                {
                                    SMS.message = FilterHeaders(SMS.message);
                                }
                                foreach (string s in SMS.to)
                                {
                                    if (s.ToUpper() == "BROADCAST")
                                    {
                                        AckMessage.Add(BitAPIserver.BA.sendBroadcast(SMS.from, B64e(SMS.subject), B64e(SMS.message)));
                                    }
                                    else
                                    {
                                        AckMessage.Add(BitAPIserver.BA.sendMessage(s, SMS.from, B64e(SMS.subject), B64e(SMS.message)));
                                    }
                                }
                            }
                            SMTP.msg(250, "This better not be spam!");
                        }
                        SMS = new SMTPstate();
                    }
                    else
                    {
                        if (raw.ToLower().StartsWith("subject: ") && string.IsNullOrEmpty(SMS.subject))
                        {
                            if (BS.StripHdr)
                            {
                                SMS.subject = stripQuoted(raw.Substring(9).Trim());
                            }
                            else
                            {
                                SMS.subject = raw.Substring(9).Trim();
                            }
                        }
                        //MIME from field, if the address exists set this.
                        if (raw.ToLower().StartsWith("from: "))
                        {
                            string fromAddr=getAddress(raw);
                            if (!string.IsNullOrEmpty(fromAddr))
                            {
                                foreach (BitAddr a in myAddr)
                                {
                                    if (a.address.ToLower() == fromAddr.ToLower())
                                    {
                                        SMS.from = a.address;
                                    }
                                }
                            }
                        }
                        if (BS.StripHdr)
                        {
                            SMS.message += stripQuoted(raw + "\r\n");
                        }
                        else
                        {
                            SMS.message += raw + "\r\n";
                        }
                    }
                }
                else
                {
                    switch (command)
                    {
                        case "EHLO":
                        case "HELO":
                            SMTP.msg(250, "I am busy stealing your secrets, it better be important");
                            break;
                        case "MAIL":
                            if (args.Length > 0 && args[0].ToUpper().StartsWith("FROM:"))
                            {
                                string addr;
                                if (!string.IsNullOrEmpty(addr = check(raw)))
                                {
                                    if (addr == RANDOM.Split('@')[0] && BS.Random)
                                    {
                                        SMS.from=Bitmessage.generateAddress(BitAPIserver.BA, "RANDOM");
                                        SMTP.msg(250, "Generated your address: "+SMS.from);
                                    }
                                    else
                                    {
                                        bool found = false;
                                        foreach (var Addr in myAddr)
                                        {
                                            if (Addr.address.ToLower() == addr.ToLower() || Addr.label.ToLower() == addr.ToLower())
                                            {
                                                SMS.from = Addr.address;
                                                found = true;
                                            }
                                        }
                                        if (found)
                                        {
                                            SMTP.msg(250, "At least it is your address");
                                        }
                                        else
                                        {
                                            SMTP.msg(530, "Fuckoff");
                                        }
                                    }
                                }
                                else
                                {
                                    SMTP.msg(500, "HOW DUMB ARE YOU? THIS IS NOT EVEN A CORRECT BITMESSAGE ADDRESS...");
                                }

                            }
                            else
                            {
                                SMTP.msg(500, "WRONG!!!!");
                            }
                            break;
                        case "RCPT":
                            if (args.Length > 0 && args[0].ToUpper().StartsWith("TO:"))
                            {
                                string addr = check(raw);
                                if (!string.IsNullOrEmpty(addr))
                                {
                                    if (addr == COMMAND.Split('@')[0])
                                    {
                                        SMS.isCommand = true;
                                        SMTP.msg(250, "Give me the damn subject line already");
                                    }
                                    else
                                    {
                                        SMS.to.Add(addr);
                                        SMTP.msg(250, "I added the address");
                                    }
                                }
                                else
                                {
                                    SMTP.msg(551, "TRY A FUCKING EXTERNAL SERVER OR USE <BITMESSAGE.CH>");
                                }

                            }
                            else
                            {
                                SMTP.msg(500, "WRONG!!!!");
                            }
                            break;
                        case "DATA":
                            if (!string.IsNullOrEmpty(SMS.from) && (SMS.to.Count > 0 || SMS.isCommand))
                            {
                                SMS.onData = true;
                                SMTP.dataMode = true;
                                //As a Mail Server we add this
                                SMS.message = "Return-Path: <" + SMS.from + "@bitmessage.ch>\r\n";
                                SMTP.msg(354, "I am waiting...");
                            }
                            else
                            {
                                SMTP.msg(500, "DEFINE MAIL AND RCPT YOU IDIOT");
                            }
                            break;
                        case "HELP":
                            SMTP.msg(241, "THERE IS NO FUCKING HELP FOR YOUR PROBLEM. JUST LIVE WITH IT!");
                            break;
                        case "NOOP":
                            SMTP.msg(Troll.Trollface);
                            break;
                        case "QUIT":
                            if (string.IsNullOrEmpty(SMS.from) || SMS.to.Count == 0)
                            {
                                SMTP.msg(221, "NEXT TIME SEND A MAIL YOU STOOPID BITCH");
                            }
                            else
                            {
                                SMTP.msg(221, "I relay your message, but only because it's you!");
                            }
                            SMTP.close();
                            break;
                        case "RSET":
                            SMS = new SMTPstate();
                            SMTP.msg(250, new string[] { "Who is too stupid to write a message without errors?", "You are too stupid to write a message!" });
                            break;
                        case "VRFY":
                        case "EXPN":
                        case "TURN":
                        case "AUTH":
                            SMTP.msg(502, "I HAVE NO FUCKING TIME FOR THIS. SCREW THIS COMMAND");
                            break;
                        default:
                            SMTP.msg(500, "STOP DOING THIS!");
                            break;
                    }
                }
            }
        }