Example #1
0
        public void LaunchDaemon(File daemonLauncher)
        {
            var lines = daemonLauncher.Content.Split(new string[] { "\r\n" }, StringSplitOptions.None);

            if (lines[0] == "IRC")
            {
                var newDaemon = new IrcDaemon(NextPID, null, this, new Credentials(GetUserId("guest"), Group.GUEST));
                daemons.Add(newDaemon);
            }
            else if (lines[0] == "DNS")
            {
                var newDaemon = new DNSDaemon(NextPID, null, this, new Credentials(GetUserId("guest"), Group.GUEST));
                daemons.Add(newDaemon);
            }
            else if (lines[0] == "HTTP")
            {
                var newDaemon = new HTTPDaemon(NextPID, null, this, new Credentials(GetUserId("guest"), Group.GUEST));
                daemons.Add(newDaemon);
            }
            else if (lines[0] == "BANK")
            {
                var newDaemon = new BankDaemon(NextPID, null, this, new Credentials(GetUserId("guest"), Group.GUEST));
                daemons.Add(newDaemon);
            }
            else if (lines[0] == "MISSION")
            {
                var newDaemon = new MissionDaemon(NextPID, null, this, new Credentials(GetUserId("guest"), Group.GUEST));
                daemons.Add(newDaemon);
            }
            else if (lines[0] == "MAIL")
            {
                var newDaemon = new MailDaemon(NextPID, null, this, new Credentials(GetUserId("guest"), Group.GUEST));
                daemons.Add(newDaemon);
            }
        }
Example #2
0
        public static bool ConfigCommand(CommandProcess process, string[] command)
        {
            MailClient client = (MailClient)process;
            MailDaemon daemon = (MailDaemon)client.Daemon;

            File mailFolder = process.computer.fileSystem.RootFile.GetFile("mail");

            if (command.Length < 2)
            {
                process.Print("Usage : config dns [IP]");
                return(true);
            }
            string[] cmdArgs = command[1].Split(' ');
            if (cmdArgs.Length != 2)
            {
                process.Print("Usage : config dns [IP]");
                return(true);
            }
            if (cmdArgs[0] == "dns")
            {
                if (!Permissions.PermissionHelper.CheckCredentials(process.Credentials, Permissions.Group.ROOT))
                {
                    process.Print("You must be logged in as root to use this command!");
                    return(true);
                }
                if (cmdArgs.Length != 2)
                {
                    process.Print("config dns [IP of DNS Server]");
                    return(true);
                }
                Node dnsServer = Server.Instance.GetComputerManager().GetNodeByIp(cmdArgs[1]);
                if (dnsServer == null)
                {
                    process.Print($"{cmdArgs[1]} does not exist!");
                    return(true);
                }
                File dnsDaemon = dnsServer.fileSystem.RootFile.GetFileAtPath("daemons/dns");
                if (dnsDaemon == null)
                {
                    process.Print($"{dnsServer.ip} doesn't have a DNS daemon");
                    return(true);
                }
                File    configFile   = client.computer.fileSystem.RootFile.GetFileAtPath("mail/config.json");
                JObject configObject = JObject.Parse(configFile.Content);
                configObject["DNS"] = cmdArgs[1];
                configFile.Content  = configObject.ToString();
                process.Print($"DNS changed to {cmdArgs[1]}");
                return(true);
            }
            process.Print("Config option not found");
            return(true);
        }
    private void sendEmail(string pageName, string body)
    {
        string path = Server.MapPath("~/UploadFile/email.txt");

        using (StreamReader sr = File.OpenText(path))
        {
            string s = String.Empty;
            while ((s = sr.ReadLine()) != null)
            {
                MailDaemon.sendmail(s, pageName, body);
            }
        }
    }
    protected void btnDH_Click(object sender, EventArgs e)
    {
        if (txtFullname.Text.Trim() == "" || txtSDT.Text == "")
        {
            // ltError.Text = "<span style='display:block;padding:5px;border-radius:5px; border:1px solid red;color:red;margin:10px'>Vui lòng điền đầy đủ thông tin!</span>";
        }
        else
        {
            DBClass _db = new DBClass();
            string  pageName = "Phòng khám sản khoa", address = "", name = txtFullname.Text, email = txtEmail.Text, phone = txtSDT.Text, content = txtMsg.Text, domain = "phongkhamsankhoa.com";

            string body = htmlBody(name, email, phone, address, pageName, content, domain);

            MailDaemon.sendmail("*****@*****.**", pageName, body);
            MailDaemon.sendmail("*****@*****.**", pageName, body);
            MailDaemon.sendmail("*****@*****.**", pageName, body);
            MailDaemon.sendmail("*****@*****.**", pageName, body);
            Response.Redirect("~/register-success/");
        }
    }
    protected void btnDatLich_Click(object sender, EventArgs e)
    {
        if (txtHoTen.Text.Trim() == "" || txtSoDienThoai.Text == "")
        {
            //ltError.Text = "<span style='display:block;padding:5px;border-radius:5px; border:1px solid red;'>Vui lòng điền đầy đủ thông tin!</span>";
        }
        else
        {
            DBClass _db = new DBClass();
            string  pageName = "Nâng ngực Thẩm mỹ", address = "", name = txtHoTen.Text, email = txtEmail.Text, phone = txtSoDienThoai.Text, content = txtLoiNhan.Text, domain = "nangngucthammy.info";

            string body = htmlBody(name, email, phone, address, pageName, content, domain);

            MailDaemon.sendmail("*****@*****.**", pageName, body);
            MailDaemon.sendmail("*****@*****.**", pageName, body);
            MailDaemon.sendmail("*****@*****.**", pageName, body);
            MailDaemon.sendmail("*****@*****.**", pageName, body);
            Response.Redirect("~/register-success/");
        }
    }
Example #6
0
 protected void LinkButton1_Click(object sender, EventArgs e)
 {
     if (txtCatpcha.Text.ToLower() == "thammydiamond.com")
     {
         LinkButton1.Visible = false;
         DateTime times = BaseView.getDateTimeNow();
         _db.insert_update_delete_tuvan(0, txtxSkype.Text, times.ToShortDateString() + " - " + times.ToShortTimeString(), txtEmail.Text, txtDiaChi.Text, txtDienThoai.Text, txtxSkype.Text, txtNoiDung.Text, false, 0, "", "insert");
         string body = "Họ tên: " + txtxSkype.Text + "<br/>";
         body += "Email: " + txtEmail.Text + "<br/>";
         body += "Số Điện Thoại: " + txtDienThoai.Text + "<br/>";
         body += "Địa chỉ: " + txtDiaChi.Text + "<br/>";
         body += "Nội dung: " + txtNoiDung.Text + "<br/>";
         MailDaemon.sendmail("*****@*****.**", "Khách hàng vừa gửi yêu cầu đến từ thammyDiamond.com", body);
         Response.Redirect("~/tu-van/hoan-tat.aspx");
     }
     else
     {
         lbError.Text = "Mã xác nhận không đúng";
     }
 }
Example #7
0
        public static bool ShowCommand(CommandProcess process, string[] command)
        {
            MailClient client = (MailClient)process;
            MailDaemon daemon = (MailDaemon)client.Daemon;

            File mailFolder = process.computer.fileSystem.RootFile.GetFile("mail");

            if (command.Length < 2)
            {
                process.Print("Usage : show [message ID]");
                return(true);
            }
            string[] cmdArgs = command[1].Split(' ');
            if (client.loggedInAccount == null)
            {
                process.Print("You aren't logged in!");
                return(true);
            }
            if (cmdArgs.Length != 1)
            {
                process.Print("Usage : show [message ID]");
                return(true);
            }
            if (!int.TryParse(cmdArgs[0], out int number))
            {
                process.Print("Please provide a proper message number");
                return(true);
            }
            File message = process.computer.fileSystem.RootFile.GetFileAtPath($"mail/users/{client.loggedInAccount.accountName}/Inbox/{number}.json");

            if (message == null)
            {
                process.Print("That message doesn't exist!");
                return(true);
            }
            MailMessage messageObject = new MailMessage(message);

            process.Print(messageObject.Body);
            return(true);
        }
Example #8
0
        public static bool AccountCommand(CommandProcess process, string[] command)
        {
            MailClient client = (MailClient)process;
            MailDaemon daemon = (MailDaemon)client.Daemon;

            File mailFolder  = process.computer.fileSystem.RootFile.GetFile("mail");
            File accountFile = mailFolder.GetFile("accounts.db");

            if (command.Length < 2)
            {
                process.Print("Usage : account [create/login/resetpass]");
                return(true);
            }
            string[] cmdArgs = command[1].Split(' ');
            if (cmdArgs[0] == "create")
            {
                if (cmdArgs.Length != 3)
                {
                    process.Print("Usage : account create [username] [password]");
                    return(true);
                }
                List <MailAccount> accounts = new List <MailAccount>();
                accounts.AddRange(accountFile.Content.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Select(x => {
                    // The mail account format is MAILACCOUNT,(username),(password)
                    var data = x.Split(',');
                    if (data[0] != "MAILACCOUNT" || data.Length < 3)
                    {
                        return(null);
                    }
                    return(new MailAccount(data[1], data[2]));
                }));
                foreach (MailAccount account in accounts)
                {
                    if (account != null)
                    {
                        if (account.accountName == cmdArgs[1])
                        {
                            process.Print("This username already exists!");
                            return(true);
                        }
                    }
                }
                daemon.AddAccount(new MailAccount(cmdArgs[1], cmdArgs[2]));
                process.Print($"Created an account with the name {cmdArgs[1]}");
                return(true);
            }
            else if (cmdArgs[0] == "login")
            {
                if (cmdArgs.Length != 3)
                {
                    process.Print("Usage : account login [username] [password]");
                    return(true);
                }
                MailAccount accountToLogin = new MailAccount(cmdArgs[1], cmdArgs[2]);
                if (daemon.accounts.Count == 0)
                {
                    process.Print("This server has no accounts.");
                    return(true);
                }
                bool accountExists = false;
                foreach (MailAccount account in daemon.accounts)
                {
                    if (account.accountName == accountToLogin.accountName && account.password == accountToLogin.password)
                    {
                        accountExists = true;
                    }
                }
                if (!accountExists)
                {
                    process.Print("This account either doesn't exist or the password is incorrect!");
                    return(true);
                }
                client.loggedInAccount = accountToLogin;
                process.Print($"Logged in as {accountToLogin.accountName}");
                return(true);
            }
            else if (cmdArgs[0] == "resetpass")
            {
                if (cmdArgs.Length != 2)
                {
                    process.Print("Usage : account resetpass [new password]");
                    return(true);
                }
                if (client.loggedInAccount == null)
                {
                    process.Print("You are not logged in!");
                    return(true);
                }
                daemon.accounts.Remove(client.loggedInAccount);
                client.loggedInAccount.password = cmdArgs[1];
                daemon.AddAccount(client.loggedInAccount);
                process.Print($"Your new password is \"{cmdArgs[1]}\"!");
                return(true);
            }
            process.Print("Command not found, try using \"help\"");
            return(true);
        }
Example #9
0
        public static bool ListCommand(CommandProcess process, string[] command)
        {
            MailClient client = (MailClient)process;
            MailDaemon daemon = (MailDaemon)client.Daemon;

            File mailFolder = process.computer.fileSystem.RootFile.GetFile("mail");

            string[] cmdArgs = command.Length > 1 ? command[1].Split(' ') : new string[] { };
            if (client.loggedInAccount == null)
            {
                process.Print("You aren't logged in!");
                return(true);
            }
            List <File> children = new List <File>(process.computer.fileSystem.RootFile.GetFileAtPath($"mail/users/{client.loggedInAccount.accountName}/Inbox").children);

            if (children.Count == 0)
            {
                process.Print("You have no messgaes!");
                return(true);
            }
            if (cmdArgs.Length != 1)
            {
                string outputString = "Here are your ten most recent messages :\n";
                int    i            = 0;
                foreach (File message in process.computer.fileSystem.RootFile.GetFileAtPath($"mail/users/{client.loggedInAccount.accountName}/Inbox").children)
                {
                    MailMessage messageObject = new MailMessage(message);
                    outputString += $"[{message.Name.Replace(".json", "")}] From {messageObject.From} At {messageObject.TimeSent}\n";
                    i++;
                    if (i >= 10)
                    {
                        break;
                    }
                }
                process.Print(outputString);
                return(true);
            }
            if (!int.TryParse(cmdArgs[0], out int page))
            {
                process.Print("Please provide a valid page number!");
                return(true);
            }
            if (!(page > 0) || (double)page - 1 > (double)children.Count / 10)
            {
                process.Print("Please provide a valid page number!");
                return(true);
            }
            int         startInt = page == 1 ? 1 : page * 10 - 10;
            int         endInt   = startInt + 9;
            int         index    = 1;
            List <File> messages = new List <File>();

            foreach (File message in children)
            {
                if (index >= startInt && index <= endInt)
                {
                    messages.Add(message);
                }
                index++;
            }
            string printString = $"Page {page} :\n";

            foreach (File message in messages)
            {
                MailMessage messageObject = new MailMessage(message);
                printString += $"[{message.Name.Replace(".json", "")}] From {messageObject.From} At {messageObject.TimeSent}\n";
            }
            process.Print(printString);
            return(true);
        }
Example #10
0
        public static bool SendCommand(CommandProcess process, string[] command)
        {
            MailClient client = (MailClient)process;
            MailDaemon daemon = (MailDaemon)client.Daemon;

            File mailFolder = process.computer.fileSystem.RootFile.GetFile("mail");

            if (command.Length < 2)
            {
                process.Print("Usage : send [username@ip] [message]");
                return(true);
            }
            string[] cmdArgs = command[1].Split(' ');
            if (cmdArgs.Length < 2)
            {
                process.Print("Usage : send [username@ip] [message]");
                return(true);
            }
            var    email   = cmdArgs[0].Split('@');
            int    i       = 0;
            string message = "";

            foreach (string word in cmdArgs)
            {
                if (i > 0)
                {
                    message += word + " ";
                }
                i++;
            }
            if (client.loggedInAccount == null)
            {
                process.Print("You aren't logged in!");
                return(true);
            }
            JObject config    = JObject.Parse(process.computer.fileSystem.RootFile.GetFileAtPath("mail/config.json").Content);
            Node    dnsServer = Server.Instance.GetComputerManager().GetNodeByIp(config.Properties()
                                                                                 .Where(x => x.Name == "DNS")
                                                                                 .Select(y => { return((string)y.Value); })
                                                                                 .DefaultIfEmpty(null)
                                                                                 .First());

            if (dnsServer == null)
            {
                process.Print($"Error! The specified DNS server ({dnsServer.ip}) does not exist! Please notify the network admin!");
                return(true);
            }
            DNSDaemon dnsDaemon = (DNSDaemon)dnsServer.GetDaemon("dns");

            if (dnsDaemon == null)
            {
                process.Print($"Error! {dnsServer.ip} does not have a DNS server installed! Please notify the network admin!");
                return(true);
            }
            Node mailServer = Server.Instance.GetComputerManager().GetNodeByIp(dnsDaemon.LookUp(email[1], true));

            if (mailServer == null)
            {
                process.Print("The receiving server does not exist!");
                return(true);
            }
            MailDaemon mailDaemon = (MailDaemon)mailServer.GetDaemon("mail");

            if (mailDaemon == null)
            {
                process.Print("The receiving end does not have a mail server set up!");
                return(true);
            }
            MailMessage messageObject = new MailMessage(email[0], client.loggedInAccount.accountName + "@" + process.computer.ip, message);

            if (!mailDaemon.ReceiveMail(messageObject))
            {
                process.Print("The receiving account does not exist!");
                return(true);
            }
            File userSentDir = process.computer.fileSystem.RootFile.GetFileAtPath($"mail/users/{client.loggedInAccount.accountName}/Sent");
            File messageFile = File.CreateNewFile(userSentDir, $"{userSentDir.children.Count + 1}.json");

            messageFile.Content = messageObject.ToJObject().ToString();
            messageFile.OwnerId = 0;
            messageFile.Permissions.SetPermission(FilePermissions.PermissionType.User, true, true, true);
            process.Print("The email has been sent!");
            return(true);
        }
Example #11
0
        public static bool BankAccount(CommandProcess process, string[] command)
        {
            BankClient client = (BankClient)process;
            BankDaemon daemon = (BankDaemon)client.Daemon;

            var bankFolder  = process.computer.fileSystem.rootFile.GetFile("bank");
            var accountFile = bankFolder.GetFile("accounts.db");

            if (command[0] == "account")
            {
                if (command.Length < 2)
                {
                    process.Print("Usage : account [create/login/resetpass/balance/transfer/transactions/close]");
                    return(true);
                }
                var cmdArgs = command[1].Split(' ');
                if (cmdArgs[0] == "create")
                {
                    if (cmdArgs.Length < 4)
                    {
                        process.Print("Usage : account create [accountname] [email] [password]");
                        return(true);
                    }
                    string[] emailArgs = cmdArgs[2].Split('@');
                    if (emailArgs.Length != 2)
                    {
                        process.Print("Not a valid email address");
                        return(true);
                    }
                    Node emailServer = Server.Instance.GetComputerManager().GetNodeByIp(emailArgs[1]);
                    if (emailServer == null)
                    {
                        process.Print("The email server does not exist!\nMake sure you use the IP of the mail server, not the domain.");
                    }
                    MailDaemon mailDaemon = (MailDaemon)emailServer.GetDaemon("mail");
                    if (mailDaemon == null)
                    {
                        process.Print("IP of the email address does not have an email server installed!");
                        return(true);
                    }
                    if (!mailDaemon.accounts.Any(x => x.accountName == emailArgs[0]))
                    {
                        process.Print("The email account on that email server does not exist!");
                        return(true);
                    }
                    List <string> accounts     = new List <string>();
                    var           accountsFile = accountFile.Content.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                    if (accountsFile.Length != 0)
                    {
                        foreach (string line in accountFile.Content.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries))
                        {
                            var data = line.Split(',');
                            if (data.Length < 5)
                            {
                                continue;
                            }
                            accounts.Add(data[0]);
                        }
                    }
                    if (accounts.Contains(cmdArgs[1]))
                    {
                        process.Print("This account name is not available");
                        return(true);
                    }
                    daemon.accounts.Add(new BankAccount(cmdArgs[1], 0, cmdArgs[3], client.Session.owner.username, cmdArgs[2]));
                    daemon.UpdateAccountDatabase();
                    process.Print("Your account has been opened. Use account login [accountname] [password] to login.");
                }
                if (cmdArgs[0] == "login")
                {
                    if (cmdArgs.Length < 3)
                    {
                        process.Print("Usage : account login [accountname] [password]");
                        return(true);
                    }
                    foreach (var account in daemon.accounts)
                    {
                        if (account.accountName == cmdArgs[1] && account.password == cmdArgs[2])
                        {
                            client.loggedInAccount = account;
                            daemon.computer.Log(Log.LogEvents.Login, daemon.computer.logs.Count + 1 + " " + client.Session.owner.homeComputer.ip + " logged in as bank account " + account.accountName, client.Session.sessionId, client.Session.owner.homeComputer.ip);
                            process.Print($"Logged into bank account {account.accountName} successfully");
                            return(true);
                        }
                    }
                    process.Print("Invalid account name or password");
                }
                if (cmdArgs[0] == "resetpass")
                {
                    if (cmdArgs.Length == 2)
                    {
                        BankAccount account = daemon.accounts.Where(x => x.accountName == cmdArgs[1]).DefaultIfEmpty(null).First();
                        if (account == null)
                        {
                            process.Print("That account does not exist!");
                            return(true);
                        }
                        if (!MailDaemon.SendPasswordResetEmail(process.computer, account.email, account.accountName))
                        {
                            process.Print("The email failed to send! Either the account no longer exists, or an authentication code has already been sent to this email less than an hour ago!");
                            return(true);
                        }
                        process.Print("Password reset email sent to the email associated with this account!");
                        return(true);
                    }
                    if (cmdArgs.Length < 4)
                    {
                        process.Print("Usage : account resetpass [accountname] [authentication code] [newpassword]");
                        return(true);
                    }
                    if (!int.TryParse(cmdArgs[2], out int authCode))
                    {
                        process.Print("Please use a valid authentication code!");
                        return(true);
                    }
                    if (!MailDaemon.CheckIfAuthRequestIsValid(process.computer, cmdArgs[1], authCode) || !daemon.accounts.Any(x => x.accountName == cmdArgs[1]))
                    {
                        process.Print("Something went wrong when trying to authenticate!\nEither the username does not exist, or the authentication code is invalid!");
                        return(true);
                    }

                    foreach (BankAccount account in daemon.accounts)
                    {
                        if (account.accountName == cmdArgs[1])
                        {
                            account.password = cmdArgs[3];
                            daemon.UpdateAccountDatabase();
                        }
                    }
                    process.Print("Password reset successful!");
                    return(true);
                }
                if (cmdArgs[0] == "balance")
                {
                    if (client.loggedInAccount == null)
                    {
                        process.Print("You are not logged in");
                        return(true);
                    }
                    process.Print($"Account balance for {client.loggedInAccount.accountName} is {client.loggedInAccount.balance}");
                }
                if (cmdArgs[0] == "transfer")
                {
                    if (cmdArgs.Length < 4)
                    {
                        process.Print("Usage : account transfer [receivingaccountname] [receivingbankip] [amount]");
                        return(true);
                    }
                    if (client.loggedInAccount == null)
                    {
                        process.Print("You are not logged in");
                        return(true);
                    }
                    if (client.loggedInAccount.balance < Convert.ToInt32(cmdArgs[3]))
                    {
                        process.Print("Account does not have enough balance");
                        return(true);
                    }
                    if (Server.Instance.GetComputerManager().GetNodeByIp(cmdArgs[2]) == null)
                    {
                        process.Print("The receiving computer does not exist");
                        return(true);
                    }
                    BankDaemon targetBank = null;
                    foreach (var computer in Server.Instance.GetComputerManager().NodeList)
                    {
                        if (computer.ip == cmdArgs[2])
                        {
                            Daemon targetDaemon = computer.GetDaemon("Bank");
                            if (targetDaemon == null)
                            {
                                process.Print("The receiving computer does not have a bank daemon");
                                return(true);
                            }
                            targetBank = (BankDaemon)targetDaemon;
                            break;
                        }
                    }
                    BankAccount accountTo = null;
                    foreach (var account in targetBank.accounts)
                    {
                        if (account.accountName == cmdArgs[1])
                        {
                            accountTo = account;
                            break;
                        }
                    }
                    if (accountTo == null)
                    {
                        process.Print("The receiving account does not exist");
                        return(true);
                    }
                    targetBank.ProcessBankTransfer(client.loggedInAccount, accountTo, cmdArgs[2], int.Parse(cmdArgs[3]), client.Session);
                    daemon.LogTransaction($"{client.loggedInAccount.accountName},{client.Session.owner.homeComputer.ip} transferred {cmdArgs[3]} from {client.loggedInAccount.accountName} to {accountTo.accountName}@{targetBank.computer.ip}", client.Session.sessionId, client.Session.owner.homeComputer.ip);
                }
                if (cmdArgs[0] == "transactions")
                {
                    if (client.loggedInAccount == null)
                    {
                        process.Print("You are not logged in");
                        return(true);
                    }
                    File transactionLog = bankFolder.GetFile("transactionlog.db");
                    if (transactionLog == null)
                    {
                        process.Print("This bank does not keep transaction logs");
                        return(true);
                    }
                    List <string> transactions = transactionLog.Content.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();
                    if (transactions.Count == 0)
                    {
                        process.Print("The transaction log database is empty");
                        return(true);
                    }
                    int pageNumber = 1;
                    int totalPages = transactions.Count / 10 == 0 ? 1 : transactions.Count / 10 + transactions.Count % 10 >= 1 ? 1 : 0;
                    if (cmdArgs.Length > 1)
                    {
                        if (int.TryParse(cmdArgs[1], out pageNumber))
                        {
                            if (pageNumber != 1)
                            {
                                if (pageNumber <= 0 || pageNumber > totalPages)
                                {
                                    process.Print("Invalid page number");
                                    return(true);
                                }
                            }
                        }
                    }
                    string transactionLogForClient = "------ Transaction Log for " + client.loggedInAccount.accountName + " ------\n";
                    if (pageNumber == 1)
                    {
                        transactions = (List <string>)transactions.Where(transaction => transaction.Split(',')[0] == client.loggedInAccount.accountName).Take(10);
                    }
                    else
                    {
                        transactions = (List <string>)transactions.Where(transaction => transaction.Split(',')[0] == client.loggedInAccount.accountName).Skip(pageNumber * 10).Take(10);
                    }
                    //for (int i = transactions.Length - pageModifier; transactions.Length - pageModifier < i; i--)
                    //{
                    //    string[] transaction = transactions[currentTransaction].Split(',');
                    //    if (transaction[0] == client.loggedInAccount.accountName)
                    //    {
                    //        transactionLogForClient += transaction[1] + "\n";
                    //    }
                    //    else
                    //        i++;
                    //    currentTransaction--;
                    //}
                    foreach (var transaction in transactions)
                    {
                        transactionLogForClient += transaction[1] + "\n";
                    }
                    if (transactionLogForClient == "------ Transaction Log for " + client.loggedInAccount.accountName + " ------\n")
                    {
                        transactionLogForClient += "\nThis account's transaction log is empty\n";
                    }
                    transactionLogForClient += "------ Page " + pageNumber + "/" + totalPages + " ------";
                    process.Print(transactionLogForClient);
                }
                if (cmdArgs[0] == "close")
                {
                    if (client.loggedInAccount == null)
                    {
                        process.Print("You are not logged in");
                        return(true);
                    }
                    if (client.loggedInAccount.balance != 0)
                    {
                        process.Print("Your account balance must be zero before you can close your account.\nUse account transfer to transfer your money out of your account");
                        return(true);
                    }
                    daemon.accounts.Remove(client.loggedInAccount);
                    daemon.UpdateAccountDatabase();
                    client.loggedInAccount = null;
                    process.Print("Your account has been closed");
                }
                return(true);
            }
            return(false);
        }
Example #12
0
        public static bool Account(CommandProcess process, string[] command)
        {
            MissionClient client = (MissionClient)process;
            MissionDaemon daemon = (MissionDaemon)client.Daemon;

            var missionFolder = process.computer.fileSystem.RootFile.GetFile("mission");
            var accountFile   = missionFolder.GetFile("accounts.db");

            if (command[0] == "account")
            {
                if (command.Length < 2)
                {
                    process.Print("Usage : account [create/login/resetpass/delete]");
                    return(true);
                }
                var cmdArgs = command[1].Split(' ');
                if (cmdArgs[0] == "create")
                {
                    // TODO: When mail daemon is implemented, require an email address for password reset
                    if (cmdArgs.Length < 3)
                    {
                        process.Print("Usage : account create [accountname] [email] [password]");
                        return(true);
                    }
                    string[] emailArgs = cmdArgs[2].Split('@');
                    if (emailArgs.Length != 2)
                    {
                        process.Print("Not a valid email address");
                        return(true);
                    }
                    Node emailServer = Server.Instance.GetComputerManager().GetNodeByIp(emailArgs[1]);
                    if (emailServer == null)
                    {
                        process.Print("The email server does not exist!\nMake sure you use the IP of the mail server, not the domain.");
                    }
                    MailDaemon mailDaemon = (MailDaemon)emailServer.GetDaemon("mail");
                    if (mailDaemon == null)
                    {
                        process.Print("That server does not have an email daemon!");
                        return(true);
                    }
                    if (!mailDaemon.accounts.Any(x => x.accountName == emailArgs[0]))
                    {
                        process.Print("The email account on that email server does not exist!");
                        return(true);
                    }
                    if (daemon.accounts.Count != 0)
                    {
                        foreach (var account in daemon.accounts)
                        {
                            if (account.ServerAccount == client.Session.owner.account)
                            {
                                process.Print("You already have an account on this mission board.\nTo reset your password, use account resetpass");
                                return(true);
                            }
                        }
                    }
                    List <string> accounts     = new List <string>();
                    var           accountsFile = accountFile.Content.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                    if (accountsFile.Length != 0)
                    {
                        foreach (string line in accountFile.Content.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries))
                        {
                            var data = line.Split(',');
                            if (data.Length < 5)
                            {
                                continue;
                            }
                            accounts.Add(data[0]);
                        }
                    }
                    if (accounts.Contains(cmdArgs[1]))
                    {
                        process.Print("This account name is not available");
                        return(true);
                    }
                    daemon.accounts.Add(new MissionAccount(cmdArgs[1], 0, 0, cmdArgs[3], client.Session.owner.account, cmdArgs[2]));
                    daemon.UpdateAccountDatabase();
                    process.Print("Your account has been opened. Use account login [accountname] [password] to login.");
                }
                if (cmdArgs[0] == "login")
                {
                    if (cmdArgs.Length < 3)
                    {
                        process.Print("Usage : account login [accountname] [password]");
                        return(true);
                    }
                    foreach (var account in daemon.accounts)
                    {
                        if (account.accountName == cmdArgs[1] && account.password == cmdArgs[2])
                        {
                            client.loggedInAccount = account;
                            daemon.computer.Log(Log.LogEvents.Login, daemon.computer.logs.Count + 1 + " " + client.Session.owner.homeComputer.ip + " logged in as bank account " + account.accountName, client.Session.sessionId, client.Session.owner.homeComputer.ip);
                            process.Print($"Logged into mission board account {account.accountName} successfully");
                            return(true);
                        }
                    }
                    process.Print("Invalid account name or password");
                }
                if (cmdArgs[0] == "resetpass")
                {
                    if (cmdArgs.Length == 2)
                    {
                        MissionAccount account = daemon.accounts.Where(x => x.accountName == cmdArgs[1]).DefaultIfEmpty(null).First();
                        if (account == null)
                        {
                            process.Print("That account does not exist!");
                            return(true);
                        }
                        if (!MailDaemon.SendPasswordResetEmail(process.computer, account.email, account.accountName))
                        {
                            process.Print("The email failed to send! Either the account no longer exists, or an authentication code has already been sent to this email less than an hour ago!");
                            return(true);
                        }
                        process.Print("Password reset email sent to the email associated with this account!");
                        return(true);
                    }
                    if (cmdArgs.Length < 4)
                    {
                        process.Print("Usage : account resetpass [accountname] [authentication code] [newpassword]");
                        return(true);
                    }
                    if (!int.TryParse(cmdArgs[2], out int authCode))
                    {
                        process.Print("Please use a valid authentication code!");
                        return(true);
                    }
                    if (!MailDaemon.CheckIfAuthRequestIsValid(process.computer, cmdArgs[1], authCode) || !daemon.accounts.Any(x => x.accountName == cmdArgs[1]))
                    {
                        process.Print("Something went wrong when trying to authenticate!\nEither the username does not exist, or the authentication code is invalid!");
                        return(true);
                    }

                    foreach (MissionAccount account in daemon.accounts)
                    {
                        if (account.accountName == cmdArgs[1])
                        {
                            account.password = cmdArgs[3];
                            daemon.UpdateAccountDatabase();
                        }
                    }
                    process.Print("Password reset successful!");
                    return(true);
                }
                if (cmdArgs[0] == "delete")
                {
                    if (client.loggedInAccount == null)
                    {
                        process.Print("You are not logged in");
                        return(true);
                    }
                    if (client.loggedInAccount.currentMission != 0)
                    {
                        process.Print("You cannot delete your account while you have a mission in progress\nYou must complete or abandon the mission before you can delete your account");
                        return(true);
                    }
                    if (cmdArgs.Length >= 2)
                    {
                        if (cmdArgs[1] != "y")
                        {
                            process.Print("Are you sure you want to delete your account?\nRun account delete y if you are sure you want to delete your account");
                            return(true);
                        }
                    }
                    else
                    {
                        process.Print("Are you sure you want to delete your account?\nRun account delete y if you are sure you want to delete your account");
                        return(true);
                    }
                    daemon.accounts.Remove(client.loggedInAccount);
                    daemon.UpdateAccountDatabase();
                    client.loggedInAccount = null;
                    process.Print("Your account has been deleted");
                }
                return(true);
            }
            return(false);
        }
Example #13
0
        public static bool MailServerCommands(CommandProcess process, string[] command)
        {
            MailClient client = (MailClient)process;
            MailDaemon daemon = (MailDaemon)client.Daemon;

            File mailFolder  = process.computer.fileSystem.rootFile.GetFile("mail");
            File accountFile = mailFolder.GetFile("accounts.db");

            if (command.Length < 2)
            {
                process.Print(help);
                return(true);
            }
            string[] cmdArgs = command[1].Split(' ');
            if (cmdArgs[0] == "account")
            {
                if (cmdArgs.Length < 2)
                {
                    process.Print(help);
                    return(true);
                }
                if (cmdArgs[1] == "create")
                {
                    if (cmdArgs.Length != 4)
                    {
                        process.Print("Usage : mail account create [username] [password]");
                        return(true);
                    }
                    List <MailAccount> accounts = new List <MailAccount>();
                    accounts.AddRange(accountFile.Content.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Select(x => {
                        // The mail account format is MAILACCOUNT,(username),(password)
                        var data = x.Split(',');
                        if (data[0] != "MAILACCOUNT" || data.Length < 3)
                        {
                            return(null);
                        }
                        return(new MailAccount(data[1], data[2]));
                    }));
                    foreach (MailAccount account in accounts)
                    {
                        if (account != null)
                        {
                            if (account.accountName == cmdArgs[2])
                            {
                                process.Print("This username already exists!");
                                return(true);
                            }
                        }
                    }
                    daemon.AddAccount(new MailAccount(cmdArgs[2], cmdArgs[3]));
                    process.Print($"Created an account with the name {cmdArgs[2]}");
                    return(true);
                }
                else if (cmdArgs[1] == "login")
                {
                    if (cmdArgs.Length != 4)
                    {
                        process.Print("Usage : mail account login [username] [password]");
                        return(true);
                    }
                    MailAccount accountToLogin = new MailAccount(cmdArgs[2], cmdArgs[3]);
                    if (daemon.accounts.Count == 0)
                    {
                        process.Print("This server has no accounts.");
                        return(true);
                    }
                    bool accountExists = false;
                    foreach (MailAccount account in daemon.accounts)
                    {
                        if (account.accountName == accountToLogin.accountName && account.password == accountToLogin.password)
                        {
                            accountExists = true;
                        }
                    }
                    if (!accountExists)
                    {
                        process.Print("This account either doesn't exist or the password is incorrect!");
                        return(true);
                    }
                    client.loggedInAccount = accountToLogin;
                    process.Print($"Logged in as {accountToLogin.accountName}");
                }
                else if (cmdArgs[1] == "resetpass")
                {
                    if (cmdArgs.Length != 3)
                    {
                        process.Print("Usage : mail account resetpass [new password]");
                        return(true);
                    }
                    if (client.loggedInAccount == null)
                    {
                        process.Print("You are not logged in!");
                        return(true);
                    }
                    daemon.accounts.Remove(client.loggedInAccount);
                    client.loggedInAccount.password = cmdArgs[1];
                    daemon.AddAccount(client.loggedInAccount);
                    process.Print($"Your new password is \"{cmdArgs[1]}\"!");
                    return(true);
                }
            }
            else if (cmdArgs[0] == "config")
            {
                if (cmdArgs.Length < 2)
                {
                    process.Print(help);
                    return(true);
                }
                if (cmdArgs[1] == "dns")
                {
                    if (!Permissions.PermissionHelper.CheckCredentials(process.Credentials, Permissions.Group.ROOT))
                    {
                        process.Print("You must be logged in as root to use this command!");
                        return(true);
                    }
                    if (cmdArgs.Length != 3)
                    {
                        process.Print("mail config dns [IP of DNS Server]");
                        return(true);
                    }
                    Node dnsServer = Server.Instance.GetComputerManager().GetNodeByIp(cmdArgs[1]);
                    if (dnsServer == null)
                    {
                        process.Print($"{cmdArgs[2]} does not exist!");
                        return(true);
                    }
                    File dnsDaemon = dnsServer.fileSystem.rootFile.GetFileAtPath("daemons/dns");
                    if (dnsDaemon == null)
                    {
                        process.Print($"{dnsServer.ip} doesn't have a DNS daemon");
                        return(true);
                    }
                    File    configFile   = client.computer.fileSystem.rootFile.GetFileAtPath("mail/config.json");
                    JObject configObject = JObject.Parse(configFile.Content);
                    configObject["DNS"] = cmdArgs[2];
                    configFile.Content  = configObject.ToString();
                }
            }
            else if (cmdArgs[0] == "send")
            {
                if (cmdArgs.Length < 2)
                {
                    process.Print(help);
                    return(true);
                }
                var    email   = cmdArgs[1].Split('@');
                int    i       = 0;
                string message = "";
                foreach (string word in cmdArgs)
                {
                    if (i > 1)
                    {
                        message += word + " ";
                    }
                    i++;
                }
                if (client.loggedInAccount == null)
                {
                    process.Print("You aren't logged in!");
                    return(true);
                }
                JObject config    = JObject.Parse(process.computer.fileSystem.rootFile.GetFileAtPath("mail/config.json").Content);
                Node    dnsServer = Server.Instance.GetComputerManager().GetNodeByIp(config.Properties()
                                                                                     .Where(x => x.Name == "DNS")
                                                                                     .Select(y => { return((string)y.Value); })
                                                                                     .Single());
                if (dnsServer == null)
                {
                    process.Print($"Error! The specified DNS server ({dnsServer.ip}) does not exist! Please notify the network admin!");
                    return(true);
                }
                File dnsFile = dnsServer.fileSystem.rootFile.GetFileAtPath("daemons/dns");
                if (dnsFile == null)
                {
                    process.Print($"Error! {dnsServer.ip} does not have a DNS server installed! Please notify the network admin!");
                    return(true);
                }
                string ipOfMailServer = new DNSDaemon(dnsServer.NextPID, null, dnsServer, new Credentials(dnsServer.GetUserId("guest"), Permissions.Group.GUEST)).LookUp(email[1], true);
                Node   mailServer     = Server.Instance.GetComputerManager().GetNodeByIp(ipOfMailServer);
                if (mailServer == null)
                {
                    process.Print("The receiving server does not exist!");
                    return(true);
                }
                File mailDaemonFile = mailServer.fileSystem.rootFile.GetFileAtPath("mail/config.json");
                if (mailDaemonFile == null)
                {
                    process.Print("The receiving end does not have a mail server set up!");
                    return(true);
                }
                MailMessage messageObject = new MailMessage(email[0], client.loggedInAccount.accountName + "@" + process.computer.ip, message);
                if (!(new MailDaemon(mailServer.NextPID, null, mailServer, new Credentials(mailServer.GetUserId("guest"), Permissions.Group.GUEST)).ReceiveMail(messageObject)))
                {
                    process.Print("The receiving account does not exist!");
                    return(true);
                }
                process.Print("The email has been sent!");
                return(true);
            }
            else if (cmdArgs[0] == "list")
            {
                if (client.loggedInAccount == null)
                {
                    process.Print("You aren't logged in!");
                    return(true);
                }
                List <File> children = process.computer.fileSystem.rootFile.GetFileAtPath($"mail/users/{client.loggedInAccount.accountName}/Inbox").children;
                if (cmdArgs.Length != 2)
                {
                    string outputString = "Here are your ten most recent messages :\n";
                    int    i            = 0;
                    foreach (File message in process.computer.fileSystem.rootFile.GetFileAtPath($"mail/users/{client.loggedInAccount.accountName}/Inbox").children)
                    {
                        MailMessage messageObject = new MailMessage(message);
                        outputString += $"[{message.Name.Replace(".json", "")}] From {messageObject.From} At {messageObject.TimeSent}\n";
                        i++;
                        if (i >= 10)
                        {
                            break;
                        }
                    }
                    process.Print(outputString);
                    return(true);
                }
                if (!int.TryParse(cmdArgs[1], out _))
                {
                    process.Print("Please provide a valid page number!");
                    return(true);
                }
                int page = int.Parse(cmdArgs[1]);
                if (!(page > 0) || (double)page - 1 > (double)children.Count / 10)
                {
                    process.Print("Please provide a valid page number!");
                    return(true);
                }
                int         startInt = page == 1 ? 1 : page * 10 - 10;
                int         endInt   = startInt + 9;
                int         index    = 1;
                List <File> messages = new List <File>();
                foreach (File message in children)
                {
                    if (index >= startInt && index <= endInt)
                    {
                        messages.Add(message);
                    }
                    index++;
                }
                string printString = $"Page {page} :\n";
                foreach (File message in messages)
                {
                    MailMessage messageObject = new MailMessage(message);
                    printString += $"[{message.Name.Replace(".json", "")}] From {messageObject.From} At {messageObject.TimeSent}\n";
                }
                process.Print(printString);
                return(true);
            }
            else if (cmdArgs[0] == "show")
            {
                if (client.loggedInAccount == null)
                {
                    process.Print("You aren't logged in!");
                    return(true);
                }
                if (cmdArgs.Length != 2)
                {
                    process.Print(help);
                    return(true);
                }
                if (!int.TryParse(cmdArgs[1], out int number))
                {
                    process.Print("Please provide a proper message number");
                    return(true);
                }
                File message = process.computer.fileSystem.rootFile.GetFileAtPath($"mail/users/{client.loggedInAccount.accountName}/Inbox/{number}.json");
                if (message == null)
                {
                    process.Print("That message doesn't exist!");
                    return(true);
                }
                MailMessage messageObject = new MailMessage(message);
                process.Print(messageObject.Body);
                return(true);
            }
            return(true);
        }