Exemple #1
0
        private static void Main(string[] args)
        {
            Session sess = new Session();  //Create a new Session object
            Console.WriteLine("ASP.NET_Id: [" + sess.Id + "]"); //Output the value of the ASP.NET_SessionId cookie
            Console.Write("CustomerId? ");          //Get the customer id from the user
            uint CustomerId = uint.Parse(Console.ReadLine());
            sess.Customer = new Customer(sess, CustomerId);

            Console.WriteLine("Customer: " + sess.Customer.Name); //Output the customer's name
            System.Diagnostics.Debug.Assert((new Customer(sess, sess.Customer.Name).Id == CustomerId) && (new Customer(sess, sess.Customer.Name).Name == sess.Customer.Name));  //Check that creating a customer from that name, gives the same customer as creating one with the CustomerId

            Console.Write("Username: "******"Password: "******"Welcome, " + Me.Name + "!");//Print my name

            while (true) //Until the user exits
            {
                try
                {
                    Console.Write("."); //Print prompt
                    string order = Console.ReadLine(); //Get command
                    string[] orders = order.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);   //Split command into parts
                    orders[0] = orders[0].ToLower();//Make part one lowercase (to simplify the if's in our code)
                    if ((orders[0] == "exit") || (orders[0] == "quit") || (orders[0] == "break")) break;
                    if (orders[0] == "project")
                    {
                        if (orders[1] == "set-active") new Project(sess, uint.Parse(orders[2])).setActive();//Set the active context to the project with this id
                        if (orders[1] == "get-active") //Get project id of active context
                        {
                            if (sess.ActiveContext.StartsWith("P")) Console.WriteLine(sess.ActiveContext);
                            else Console.WriteLine("Active context is not a project");
                        }
                        if (orders[1] == "get-root")    //Get the root directory of the active context if it's a project
                        {
                            if (sess.ActiveContext.StartsWith("P"))
                            {
                                Console.WriteLine((new Project(sess, uint.Parse(sess.ActiveContext.Substring(1))).getRootDirectory()).Name);
                            }
                            else Console.WriteLine("Active context is not a project");
                        }
                        if (orders[1] == "active-toggle-archive") //send project in active context to archive
                        {
                            if (sess.ActiveContext.StartsWith("P")) new Project(sess, uint.Parse(sess.ActiveContext.Substring(1))).toArchive();
                            else Console.WriteLine("Active context is not a project");
                        }
                    }
                    if (orders[0] == "course")
                    {
                        if (orders[1] == "set-active") new Course(sess, int.Parse(orders[2])).setActive(); //Set active context course id
                        if (orders[1] == "get-active") //Get the active context course id
                        {
                            if (sess.ActiveContext.StartsWith("C")) Console.WriteLine(sess.ActiveContext);
                            else Console.WriteLine("Active context is not a course");
                        }
                        if (orders[1] == "get-root")        //Get the root directory of the course
                        {
                            if (sess.ActiveContext.StartsWith("C"))
                            {
                                new Course(sess, int.Parse(sess.ActiveContext.Substring(1))).getRootDirectory();
                            }
                            else Console.WriteLine("Active context is not a course");
                        }
                    }
                    if (orders[0] == "keepalive")
                    {
                        if (orders[1] == "get-messenger-status")        //Show as online
                        {
                            Console.WriteLine(sess.KeepAlive.MessengerStatus);
                        }
                        if (orders[1] == "set-messenger-status")        //Show as offline
                        {
                            sess.KeepAlive.MessengerStatus = uint.Parse(orders[2]);
                        }
                    }
                    if (orders[0] == "make-http-request") sess.GetHttpWebRequest(orders[1]).GetResponse().Close();  //Make a HTTP-GET request, with the session cookies
                    if (orders[0] == "session")
                    {
                        if (orders[1] == "logout") sess.Logout();           //Log out
                        if (orders[1] == "login") sess.Login(orders[2], orders[3]); //login <user> <password>
                    }
                    if (orders[0] == "mail")
                    {
                        if (orders[1] == "get-in-folder")       //Get mails in folder by id
                        {
                            itsLib.Messaging.MailBox mb = new itsLib.Messaging.MailBox(sess, int.Parse(orders[2]));
                            Console.WriteLine("Messages in folder \"" + mb.Name + "\"");    //Print folder name
                            List<Mail> mails = mb.GetMails();   //Get the mails in message folder
                            foreach (Mail Mail in mails)
                            {
                                Console.Write("From: " + Mail.From.Username + "\t\t");  //Output who sent the mail
                                foreach (Person Person in Mail.To)
                                {
                                    if (Person != null) Console.Write(Person.Username + "\t\t"); //And all recipients
                                }
                                Console.WriteLine(Mail.Subject);    //And lastly the subject
                            }
                        }
                        if (orders[1] == "create")
                        {
                            MailCreator NewMail = new MailCreator();
                            do
                            {
                                Console.Write("To: ");
                                order = Console.ReadLine().Trim();
                                if (order != "") NewMail.To.Add(new Person(sess, uint.Parse(order)));
                                else break;
                                Console.SetCursorPosition(Console.CursorLeft, Console.CursorTop - 1);
                                Console.WriteLine("To: " + NewMail.To[NewMail.To.Count - 1].Name);
                            } while (true);
                            do
                            {
                                Console.Write("Cc: ");
                                order = Console.ReadLine().Trim();
                                if (order != "") NewMail.Cc.Add(new Person(sess, uint.Parse(order)));
                                else break;
                                Console.SetCursorPosition(Console.CursorLeft, Console.CursorTop - 1);
                                Console.WriteLine("Cc: " + NewMail.Cc[NewMail.Cc.Count - 1].Name);
                            } while (true);
                            do
                            {
                                Console.Write("Bcc: ");
                                order = Console.ReadLine().Trim();
                                if (order != "") NewMail.Bcc.Add(new Person(sess, uint.Parse(order)));
                                else break;
                                Console.SetCursorPosition(Console.CursorLeft, Console.CursorTop - 1);
                                Console.WriteLine("Bcc " + NewMail.Bcc[NewMail.Bcc.Count - 1].Name);
                            } while (true);
                            Console.Write("Subject: ");
                            NewMail.Subject = Console.ReadLine();
                            do
                            {
                                Console.Write("Text: ");
                                order = Console.ReadLine();
                                if (order != "") NewMail.Text += order + '\n'.ToString();
                                else break;
                            } while (true);
                            NewMail.SendMessage(sess);
                        }
                    }
                    if (orders[0] == "bulletin")
                    {
                        if (orders[1] == "in-active")   //Get bulletin (by id) in the currently active context (project or course)
                        {
                            List<Bulletin> bulletins;
                            if (sess.ActiveContext.StartsWith("C")) bulletins = Bulletin.inCP(sess, new Course(sess, int.Parse(sess.ActiveContext.Substring(1))));
                            else bulletins = Bulletin.inCP(sess, new Project(sess, uint.Parse(sess.ActiveContext.Substring(1))));
                            foreach (Bulletin bulletin in bulletins)
                            {
                                Console.WriteLine(bulletin.By.Username + ":" + bulletin.Title + ":" + bulletin.Text);
                            }
                        }
                    }
                    if (orders[0] == "find-person")
                    {
                        if (orders[1] == "any")         //List all persons
                        {
                            PersonSearch ps = PersonSearch.GetAll(sess);
                            foreach (Person Person in ps.Result)
                            {
                                Console.WriteLine(Person.Name);
                            }
                        }
                    }
                }
                catch (Exception e) { Console.WriteLine(e.Message); }
            }
        }
Exemple #2
0
        private static void Connection(object o)
        {
            TcpClient tc = (TcpClient)o;
            TcpClient dataClient = null;
            TcpListener pasvListener = null;

            NetworkStream ns = new NetworkStream(tc.Client);
            StreamReader sr = new StreamReader(ns);
            StreamWriter sw = new StreamWriter(ns);
            StreamReader dataStreamReader;
            StreamWriter dataStreamWriter = null;

            Session sess = new Session();
            string user = "", pass = "", usernameEncoded, usernameDecoded;
            sw.AutoFlush = true;
            string tmpS = "";
            uint CustomerId = 0;
            string wd = "/";
            bool binaryFlag = false;

            sw.WriteLine("220 its ftpd");
            while (tc.Connected)
            {
                string command;
                try
                {
                    sw.Flush();
                    if ((sr == null) || (sw == null)) return;
                    command = sr.ReadLine().Trim();
                    Console.WriteLine(">" + command);
                }
                catch (IOException) { return; }
                string cmd = command.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[0].ToUpper();
                if (cmd == "USER")
                {
                    try
                    {
                        usernameEncoded = command.Substring(cmd.IndexOf("USER ") + "USER ".Length + 1);
                        usernameDecoded = Base16.from16(usernameEncoded);
                        user = usernameDecoded.Split('@')[0];
                        CustomerId = uint.Parse(usernameDecoded.Split('@')[1]);
                        sw.WriteLine("331 Password required");
                    }
                    catch (Exception e) { sw.WriteLine("530 " + e.Message); }
                    continue;
                }
                if (cmd == "PASS")
                {
                    pass = command.Substring(cmd.IndexOf("PASS ") + "PASS ".Length + 1);
                    try
                    {
                        Customer Customer = new Customer(sess, CustomerId);
                        sess.Customer = Customer;
                        sess.Login(user, Base16.from16(pass));
                        if (sess.LoggedIn) sw.WriteLine("230 You may continue");
                        else sw.WriteLine("530 Denied");
                    }
                    catch (Exception e) { sw.WriteLine("530 " + e.Message); }
                    continue;
                }
                if (cmd == "PWD")
                {
                    sw.WriteLine("257 \"" + wd + "\"");
                    continue;
                }
                if (cmd == "CWD")
                {
                    wd = command.Substring("CWD ".Length + 1);
                    if (wd == "") wd = "/";
                    sw.WriteLine("250 " + wd);
                    continue;
                }
                if (cmd == "TYPE")
                {
                    tmpS = command.Substring("TYPE ".Length + 1);
                    if ((tmpS == "A") || (tmpS == "A N")) binaryFlag = false;
                    if ((tmpS == "I") || (tmpS == "L 8")) binaryFlag = true;
                    sw.WriteLine("200 " + binaryFlag.ToString());
                    continue;
                }

                if (cmd == "SYST")
                {
                    sw.WriteLine("215 ITSL");
                    continue;
                }
                if (cmd == "EPSV")
                {
                    sw.WriteLine("502 NO IPv6");
                    continue;
                }
                if (cmd == "FEAT")
                {
                    sw.WriteLine("211 No features"); //Should probably use some other code
                    continue;
                }
                if (cmd == "PASV")
                {
                    try
                    {
                        sw.WriteLine("227 =127,0,0,1,0,20"); // Implement proper code for finding IP/Port of server
                    }
                    catch (Exception)
                    {
                    }
                    if (pasvListener == null)
                    {
                        pasvListener = new TcpListener(20);
                        pasvListener.Start();
                    }
                    dataClient = pasvListener.AcceptTcpClient();
                    continue;
                }
                if (cmd == "PORT")
                {
                    string uri = command.Substring("PORT".Length + 1);
                    string[] parts = uri.Split(',');
                    int port = int.Parse(parts[4]) * 256 + int.Parse(parts[5]);
                    string ip = "";
                    foreach (string part in parts)
                    {
                        ip += part + ".";
                    }
                    ip = ip.Substring(0, ip.LastIndexOf('.') - 1);
                    ip = ip.Substring(0, ip.LastIndexOf('.'));
                    ip = ip.Substring(0, ip.LastIndexOf('.'));
                    dataClient = new TcpClient(ip, port);
                    sw.WriteLine("200 Connected");
                    dataStreamReader = new StreamReader(new NetworkStream(dataClient.Client));
                    dataStreamWriter = new StreamWriter(new NetworkStream(dataClient.Client));

                    continue;
                }
                if (cmd == "NLST")
                {
                    if (wd == "/")
                    {
                        sw.WriteLine("150");
                        dataStreamWriter.WriteLine("eportfolio\r\ncourses\r\nprojects\r\n");
                        dataStreamWriter.Flush();
                        dataStreamWriter.Close();
                        dataClient.Close();
                        sw.WriteLine("226");
                    }
                    continue;
                }
                if (command.Length >= (cmd.Length + 2)) Console.WriteLine("UNKNOWN \"" + cmd + "\" in \"" + command.Substring(cmd.Length + 1) + "\"");
                else Console.WriteLine("UNKNOWN \"" + cmd + "\"");
            }
        }