Ejemplo n.º 1
0
 public static int quit(handleClient hc, string[] param, bool linefeed)
 {
     //hc.Send(Protocol.StrToByteArray("221 2.0.0 Bye\n"));
     hc.Write("221 2.0.0 Bye\r\n");
     hc.exit();
     return(0);
 }
Ejemplo n.º 2
0
        public static void execute(handleClient hc, string command, bool lastLineFeed)
        {
            char[] splitTab = new char[2];
            splitTab[0] = ' ';
            splitTab[1] = ' ';
            if (command.StartsWith("MAIL FROM:") || command.StartsWith("RCPT TO:"))
            {
                command = command.Replace("<", " ").Replace(">", " ");
                Console.WriteLine(command);
            }
            var commandTab = command.Split(splitTab);

            if (hc.currentMessage.dot)
            {
                data_body(hc, new string[] { command }, lastLineFeed);
                return;
            }

            if (Protocol.commands.Contains(commandTab[0].ToUpper()))
            {
                var index = Array.IndexOf(Protocol.commands, commandTab[0].ToUpper());
                try
                {
                    funcPtr[index](hc, commandTab, lastLineFeed);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Server Parse Error");
                }
            }
            else
            {
                hc.Write("500 5.5.1 Command unrecognized: \"" + command + "\"\n");
            }
        }
Ejemplo n.º 3
0
        public static int data_body(handleClient hc, string[] param, bool linefeed)
        {
            hc.READ_MAIL_DATA_COUNT++;
            if (hc.currentMessage.from == string.Empty ||
                hc.currentMessage.to == string.Empty)
            {
                // var tosend = Protocol.StrToByteArray(Protocol.data_need_mail);
                // hc.Send(tosend);
                hc.Write(Protocol.data_need_mail);
                return(0);
            }

            if (hc.currentMessage.subject == string.Empty)
            {
                String[] temp_ = param[0].Split('\n');
                foreach (String s in temp_)
                {
                    if (s.StartsWith("Subject:"))
                    {
                        hc.currentMessage.subject = s.Substring(8);
                        break;
                    }
                    else
                    {
                        Console.WriteLine(s);
                    }
                }
            }
            if (param[0] == ".") //|| (param[0].Length > 3 && param[0][param[0].Length - 3] == '.'))
            {
                dot(hc, param, false);
                return(0);
            }

            if (hc.currentMessage.dot == false)
            {
                hc.Write("354 Enter mail, end with \".\" on a line by itself\r\n");
            }
            //hc.Send(Protocol.StrToByteArray("354 Enter mail, end with \".\" on a line by itself\n"));
            hc.currentMessage.dot = true;

            //desplit params
            string o = String.Join(" ", param);

            if (param[0] != "DATA")
            {
                hc.currentMessage.body += (o + "\n");
                if ((param[0].Length > 3 && param[0][param[0].Length - 3] == '.'))
                {
                    dot(hc, param, false);
                    return(0);
                }
            }

            /*
             * foreach (string s in param)
             *  hc.currentMessage.body = hc.currentMessage.body + s + "\n";
             */
            return(0);
        }
Ejemplo n.º 4
0
        public static int rcpt_to(handleClient hc, string[] param, bool linefeed)
        {
            foreach (string s in param)
            {
                Console.Write("[" + s + "]");
            }
            Console.WriteLine();
            if (hc.currentMessage.from == string.Empty)
            {
                var tosend = Protocol.StrToByteArray(Protocol.rcpt_need_mail);
                hc.Write(Protocol.rcpt_need_mail);
                //hc.Send(tosend);
                return(0);
            }

            if (param.Length < 3)
            {
                var tosend = Protocol.StrToByteArray(Protocol.rcpt_error501_noparam);
                hc.Write(Protocol.rcpt_error501_noparam);
                //hc.Send(tosend);
                return(0);
            }//A CHANGER
            else if (Settings.users.Contains(param[2]) || Settings.users.Contains(param[2] + "@" + Settings.domain))
            {
                var tosend = Protocol.StrToByteArray(Protocol.rcpt_accept250.Replace("%1", param[2]));
                if (hc.currentMessage.to != string.Empty)
                {
                    hc.currentMessage.to += ";" + param[2];
                }
                else
                {
                    hc.currentMessage.to = param[2];
                }
                hc.Send(tosend);
            }
            else if (param.Length > 3 && Settings.users.Contains(param[3]))
            {
                var tosend = Protocol.StrToByteArray(Protocol.rcpt_accept250.Replace("%1", param[3]));
                if (hc.currentMessage.to != string.Empty)
                {
                    hc.currentMessage.to += ";" + param[3];
                }
                else
                {
                    hc.currentMessage.to = param[3];
                }
                hc.Send(tosend);
            }
            else
            {
                var tosend = Protocol.StrToByteArray(Protocol.rcpt_error550_relaydenied.Replace("%1", param[2]));
                //hc.Send(tosend);
                hc.Write(Protocol.rcpt_error550_relaydenied.Replace("%1", param[2]));
            }
            return(0);
        }
Ejemplo n.º 5
0
 public void run()
 {
     while (true)
     {
         var          client = this._masterSocket.AcceptTcpClient();
         handleClient handle = new handleClient(client);
         if (handle.init())
         {
             handle.start();
         }
     }
 }
Ejemplo n.º 6
0
 public static int helo_command(handleClient hc, string[] param, bool linefeed)
 {
     if (param.Length <= 1)
     {
         var tosend = Protocol.StrToByteArray(Protocol.helo_error501);
         //hc.Send(tosend);
         hc.Write(Protocol.helo_error501);
     }
     else
     {
         var tosend = Protocol.StrToByteArray(Protocol.helo_accept250.Replace("%1", hc.getIpAddress()));
         // hc.Send(tosend);
         hc.Write(Protocol.helo_accept250.Replace("%1", hc.getIpAddress()));
     }
     return(0);
 }
Ejemplo n.º 7
0
        public static int dot(handleClient hc, string[] param, bool linefeed)
        {
            hc.currentMessage.dot = false;
            //hc.Send(Protocol.StrToByteArray("250 2.0.0 Message accepted for delivery\n"));

            if (hc.currentMessage.getSize() > 4194304)
            {
                hc.Write("501 data bigger then 2 mb !\r\n");
            }
            else
            {
                hc.Write("250 2.0.0 Message accepted for delivery\r\n");
                messageManager.push(hc.currentMessage);
            }
            hc.currentMessage = new Message();
            return(0);
        }
Ejemplo n.º 8
0
        public static int mail_from(handleClient hc, string[] param, bool linefeed)
        {
            foreach (string s in param)
            {
                Console.Write("[" + s + "]");
            }
            Console.WriteLine();
            if (param.Length <= 2)
            {
                var tab = param[1].Split(':');
                if (param[1].Substring(0, 5).ToUpper() != "FROM:")
                {
                    hc.Write(Protocol.mail_error501);
                    // var tosend = Protocol.StrToByteArray(Protocol.mail_error501);
                    // hc.Send(tosend);
                    return(0);
                }
                if (tab.Length == 0)
                {
                    hc.Write(Protocol.mail_error501);
                    //  var tosend = Protocol.StrToByteArray(Protocol.mail_error501);
                    //  hc.Send(tosend);
                    return(0);
                }
                if (tab.Length > 0 && Protocol.IsValidEmail(tab[1]))
                {
                    var tosend2 = Protocol.StrToByteArray(Protocol.mail_accept250.Replace("%1", tab[1]));
                    hc.currentMessage.from = tab[1];
                    hc.Write(Protocol.mail_accept250.Replace("%1", tab[1]));
                    //hc.Send(tosend2);
                }
                else
                {
                    var tosend = Protocol.StrToByteArray(Protocol.mail_error501);
                    hc.Write(Protocol.mail_error501);
                    // hc.Send(tosend);
                }
                return(0);
            }
            string mailfrom = null;

            if (param.Length > 2 && Protocol.IsValidEmail(param[2]))
            {
                mailfrom = param[2];
            }
            if (param.Length > 3 && Protocol.IsValidEmail(param[3]))
            {
                mailfrom = param[3];
            }
            if (param.Length > 2)
            {
                if (mailfrom == null || param[1].Length < 5 || (param[1].Substring(0, 5).ToUpper() != "FROM:"))
                {
                    var tosend = Protocol.StrToByteArray(Protocol.mail_error501_wrongparam.Replace("%1", param[2]));
                    //hc.Send(tosend);
                    hc.Write(Protocol.mail_error501_wrongparam.Replace("%1", param[2]));
                    return(0);
                }
                var tosend2 = Protocol.StrToByteArray(Protocol.mail_accept250.Replace("%1", mailfrom));
                hc.currentMessage.from = mailfrom;
                hc.Write(Protocol.mail_accept250.Replace("%1", mailfrom));
                //hc.Send(tosend2);
            }
            return(0);
        }