private static bool Passwd(CommandProcess process, string[] command)
        {
            if (command.Length == 1)
            {
                return(false);
            }
            var cmdArgs = command[1].Split(' ');

            if (cmdArgs.Length != 2)
            {
                return(false);
            }
            Account account = Account.FromId(process.Credentials.UserId, process.computer);

            if (account == null)
            {
                return(true);
            }
            string oldpass = cmdArgs[0];
            string newpass = cmdArgs[1];

            if (oldpass != account.Password)
            {
                process.Print("The old password is invalid");
                return(true);
            }

            account.Password = newpass;
            account.ApplyChanges();
            process.Print("Changes successfully applied");
            return(true);
        }
Exemple #2
0
        public static bool Touch(CommandProcess process, string[] command)
        {
            if (command.Length < 2)
            {
                process.Print("Usage : touch [fileName]");
            }

            var activeDirectory = process.ActiveDirectory;

            foreach (var fileC in activeDirectory.children)
            {
                if (fileC.Name == command[1])
                {
                    process.Print("File " + command[1] + " touched.");
                    fileC.Dirty = true;
                    return(true);
                }
            }
            if (!activeDirectory.HasWritePermission(process.Credentials))
            {
                process.Print("Permission denied.");
                return(true);
            }

            File file = process.computer.fileSystem.CreateFile(process.computer, activeDirectory, command[1]);

            file.OwnerId = process.Credentials.UserId;
            file.Permissions.SetPermission(FilePermissions.PermissionType.User, true, true, false);
            file.Permissions.SetPermission(FilePermissions.PermissionType.Group, true, true, false);
            file.Group = file.Parent.Group;

            process.Print("File " + command[1]);
            return(true);
        }
Exemple #3
0
        public static bool MkDir(CommandProcess process, string[] command)
        {
            if (command.Length < 2)
            {
                process.Print("Usage : mkdir [folderName]");
                return(true);
            }

            var activeDirectory = process.ActiveDirectory;

            foreach (var fileC in activeDirectory.children)
            {
                if (fileC.Name == command[1])
                {
                    process.Print("Folder " + command[1] + " already exists.");
                    return(true);
                }
            }

            bool passed = activeDirectory.HasWritePermission(process.Credentials);

            if (!passed)
            {
                process.Print("Permission denied.");
                return(true);
            }

            File file = process.computer.fileSystem.CreateFile(process.computer, activeDirectory, command[1]);

            file.OwnerId = process.Credentials.UserId;
            file.Permissions.SetPermission(FilePermissions.PermissionType.User, true, true, true);
            file.Permissions.SetPermission(FilePermissions.PermissionType.Group, true, true, true);
            file.Group = file.Parent.Group;
            return(true);
        }
Exemple #4
0
        public static bool Compile(CommandProcess process, string[] command)
        {
            ServerAdmin serverAdmin = (ServerAdmin)process;

            if (command.Length != 2)
            {
                process.Print("Usage: compile FILENAME TYPE");
                return(true);
            }
            string[] args = command[1].Split(' ');
            if (args.Length != 2)
            {
                process.Print("Usage: compile FILENAME TYPE");
                return(true);
            }
            File file = process.ActiveDirectory.GetFile(args[0]);

            if (file == null)
            {
                process.Print("File not found");
                return(true);
            }
            if (file.IsFolder())
            {
                process.Print("Invalid file, cannot be folder");
                return(true);
            }
            serverAdmin.client.server.GetCompileManager().AddType(file.Checksum, args[1]);
            return(true);
        }
Exemple #5
0
 private static bool CheckMissionId(string missionIdString, out MissionListing mission, MissionClient client, CommandProcess process, MissionDaemon daemon, bool employerCheck = false)
 {
     mission = null;
     if (daemon.missions.Count == 0)
     {
         process.Print("There are currently no missions available on this board");
         return(true);
     }
     if (!int.TryParse(missionIdString, out int missionId))
     {
         process.Print("The mission ID must be a number");
         return(true);
     }
     mission = daemon.missions[missionId];
     if (mission == null)
     {
         process.Print("Mission ID not found");
         return(true);
     }
     if (employerCheck)
     {
         if (mission.employer != client.loggedInAccount.accountName)
         {
             process.Print("Only the employer can edit the mission");
             return(true);
         }
     }
     return(false);
 }
Exemple #6
0
        public static bool Remove(CommandProcess process, string[] command)
        {
            if (command.Length < 2)
            {
                process.Print("Usage : rm [fileName]");
            }
            var activeDirectory = process.ActiveDirectory;

            foreach (var file in activeDirectory.children)
            {
                if (file.Name == command[1])
                {
                    if (!file.HasWritePermission(process.Credentials))
                    {
                        process.Print("Permission denied.");
                        return(true);
                    }
                    process.Print("File " + command[1] + " removed.");

                    process.computer.Kernel.RemoveFile(process, file);
                    return(true);
                }
            }



            process.Print("File does not exist.");
            return(true);
        }
Exemple #7
0
 public static bool PlayMusic(CommandProcess process, string[] commandUnsplit)
 {
     try
     {
         List <string> command = new List <string>();
         command.Add("music");
         command.AddRange(commandUnsplit[1].Split());
         if (command.Count < 2)
         {
             process.Print("Usage: music [(nameofsong) (Note: Must be in a folder called \"HNMPMusic\" in the Mods folder as an .wav file.)]\nOR music shuffle\nOR music list");
             return(true);
         }
         process.computer.Kernel.PlayMusic(process, command[1]);
         return(true);
     }
     catch (ObjectDisposedException e)
     {
         Console.WriteLine(e);
         return(true);
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
         return(true);
     }
 }
Exemple #8
0
        public static bool Login(CommandProcess process, string[] command)
        {
            if (command.Length < 2)
            {
                process.Print("Usage : login [username] [password]");
                return(true);
            }
            var args = command[1].Split(' ');

            if (args.Length < 2)
            {
                process.Print("Usage : login [username] [password]");
                return(true);
            }
            process.computer.Kernel.Login(process, args[0], args[1]);
            return(true);
        }
Exemple #9
0
 public static bool CommandExec(CommandProcess process, string[] command)
 {
     if (command.Length > 1)
     {
         return(process.RunCommand(command[1]));
     }
     process.Print(commands[command[0]].Item1);
     return(true);
 }
Exemple #10
0
        public static bool SetPrimaryGroup(CommandProcess process, string[] command)
        {
            if (command.Length == 1)
            {
                return(false);
            }
            var cmdArgs = command[1].Split(' ');

            if (cmdArgs.Length != 2)
            {
                return(false);
            }
            string username   = cmdArgs[0];
            string groupname  = cmdArgs[1];
            File   groupsFile = process.computer.fileSystem.RootFile.GetFile("etc").GetFile("group");
            File   usersFile  = process.computer.fileSystem.RootFile.GetFile("etc").GetFile("passwd");

            if (!groupsFile.HasWritePermission(process.Credentials) && !usersFile.HasWritePermission(process.Credentials))
            {
                process.Print("You do not have the required permissions");
                return(true);
            }
            string[]       groups   = groupsFile.Content.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            List <Account> accounts = process.computer.Kernel.GetAccounts();

            if (accounts.All(acc => acc.Username.ToLower() != username.ToLower()))
            {
                process.Print("The user \"" + username + "\" does not exists");
                return(true);
            }
            if (groups.All(g => !g.StartsWith(groupname.ToLower() + ":")))
            {
                process.Print("The group \"" + groupname + "\" does not exists");
                return(true);
            }

            Account user = accounts.Find(acc => acc.Username.ToLower() == username.ToLower());

            Enum.TryParse(groupname, true, out Group theGroup);
            user.GroupId = (int)theGroup;
            user.ApplyChanges();
            process.Print("done");
            return(true);
        }
Exemple #11
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);
        }
Exemple #12
0
 public static bool ChangeDirectory(CommandProcess process, string[] command)
 {
     if (command.Length < 2)
     {
         process.Print("Usage : cd [folder]");
         return(true);
     }
     if (command[1] == "..")
     {
         if (process.ActiveDirectory.Parent != null)
         {
             process.ActiveDirectory = process.ActiveDirectory.Parent;
             return(true);
         }
         else
         {
             process.Print("Invalid operation.");
             return(true);
         }
     }
     foreach (var file in process.ActiveDirectory.children)
     {
         if (file.Name == command[1])
         {
             if (!file.IsFolder())
             {
                 process.Print("You cannot change active directory to a file.");
                 return(true);
             }
             if (!file.HasExecutePermission(process.Credentials))
             {
                 process.Print("You do not have permission to do this. You must have execute permission to access a directory.");
                 return(true);
             }
             process.ActiveDirectory = file;
             process.computer.Kernel.CD(process, file.Name);
             return(true);
         }
     }
     process.Print("No such folder.");
     return(true);
 }
Exemple #13
0
        public static bool Connect(CommandProcess process, string[] command)
        {
            if (command.Length < 2)
            {
                process.Print("Usage : command [ip]");
                return(true);
            }

            process.computer.Kernel.Connect(process, command[1]);

            return(true);
        }
Exemple #14
0
        private static bool Daemon(CommandProcess process, string[] command)
        {
            if (command.Length != 2)
            {
                process.Print("Usage : daemon [name of daemon]");
                return(true);
            }
            string target = command[1];

            process.computer.Kernel.OpenDaemon(process, target);

            return(true);
        }
Exemple #15
0
        public static bool Ban(CommandProcess process, string[] commandUnsplit)
        {
            GameClient client = ((ServerAdmin)process).client;

            List <string> command = new List <string>();

            command.Add("ban");
            command.AddRange(commandUnsplit[1].Split());

            if (client.permissions.Contains(HackLinks_Server.Permissions.Admin) == false && client.permissions.Contains(HackLinks_Server.Permissions.Ban) == false)
            {
                process.Print("Insufficent Privileges");
                return(true);
            }
            if (command.Count < 3)
            {
                process.Print("Usage: ban [username] [unban (t/f)] [permban (t/f)] [days] [hr] [mins]");
                return(true);
            }
            if (command.Count < 4)
            {
                Server.Instance.DatabaseLink.SetUserBanStatus(command[1], command[3] == "t" ? -1 : 0, false);
                return(true);
            }
            int days    = Convert.ToInt32(command[4]);
            int hours   = command.Count <= 6 ? Convert.ToInt32(command[5]) : 0;
            int minutes = command.Count <= 7 ? Convert.ToInt32(command[6]) : 0;

            days    = days * 86400;
            hours   = hours * 3600;
            minutes = minutes * 60;
            int banExpiry = (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds() + days + hours + minutes;

            if (!Server.Instance.DatabaseLink.SetUserBanStatus(command[1], banExpiry, false))
            {
                process.Print("The user does not exist in the user database");
            }
            return(true);
        }
Exemple #16
0
        public static bool Web(CommandProcess process, string[] arguments)
        {
            HTTPClient client = (HTTPClient)process;

            if (arguments.Length < 2)
            {
                process.Print("Usage : web [interface name] [arguments]");
                return(true);
            }

            client.ActivePage.UseInterfaces(client, arguments[1].Split(' '));

            return(true);
        }
Exemple #17
0
        public static bool PlayMusic(CommandProcess process, string[] commandUnsplit)
        {
            List <string> command = new List <string>();

            command.Add("music");
            command.AddRange(commandUnsplit[1].Split());
            if (command.Count < 3)
            {
                process.Print("Usage: music [file ((DLC\\)Music\\NameOfFile)] [playimmediately (0/1)]");
                return(true);
            }
            process.computer.Kernel.PlayMusic(process, command[1], command[2]);
            return(true);
        }
Exemple #18
0
        public static bool Balance(CommandProcess process, string[] command)
        {
            BankClient client = (BankClient)process;
            BankDaemon daemon = (BankDaemon)client.Daemon;

            if (command[0] == "balance")
            {
                if (command.Length < 2)
                {
                    process.Print("Usage : balance set [accountname] [value]/get [accountname]");
                    return(true);
                }
                var cmdArgs = command[1].Split(' ');
                if (cmdArgs.Length < 2)
                {
                    process.Print("Usage : balance set [accountname] [value]/get [accountname]");
                    return(true);
                }
                if (cmdArgs[0] == "set" && cmdArgs.Length < 3)
                {
                    process.Print("Usage : balance set [accountname] [value]/get [accountname]");
                    return(true);
                }
                BankAccount account = null;
                foreach (var account2 in daemon.accounts)
                {
                    if (account2.accountName == cmdArgs[1])
                    {
                        account = account2;
                        break;
                    }
                }
                if (account == null)
                {
                    process.Print("Account data for this account does not exist in the database");
                    return(true);
                }
                if (cmdArgs[0] == "set")
                {
                    if (int.TryParse(cmdArgs[2], out int val))
                    {
                        account.balance = val;
                        daemon.UpdateAccountDatabase();
                        var bankFolder = process.computer.fileSystem.rootFile.GetFile("bank");
                        daemon.LogTransaction($"{account.accountName},CHEATED Balance set to {val}", client.Session.sessionId, client.Session.owner.homeComputer.ip);
                    }
                    else
                    {
                        process.Print("Error: non-integer value specified");
                        return(true);
                    }
                }
                process.Print($"Account balance for {account.accountName} is {account.balance}");
                return(true);
            }
            return(false);
        }
Exemple #19
0
        public static bool Ls(CommandProcess process, string[] command)
        {
            File root = process.computer.fileSystem.rootFile;

            if (command.Length == 2)
            {
                foreach (File file in process.ActiveDirectory.children)
                {
                    if (command[1] == file.Name)
                    {
                        process.Print($"File {file.Name} > Owner '{process.computer.GetUsername(file.OwnerId)}' Group '{file.Group}' Permissions '{Permissions.PermissionHelper.PermissionToDisplayString(file.Permissions)}'");
                        return(true);
                    }
                }
                process.Print("File " + command[1] + " not found.");
                return(true);
            }
            else
            {
                List <string> fileList = new List <string>(new string[] { process.ActiveDirectory.Name });
                foreach (File file in process.ActiveDirectory.children)
                {
                    if (file.HasReadPermission(process.Credentials))
                    {
                        fileList.AddRange(new string[] {
                            file.Name, (file.IsFolder() ? "d" : "f"), (file.HasWritePermission(process.Credentials) ? "w" : "-")
                        });
                    }
                    else
                    {
                        Logger.Warning($"User {process.computer.GetUsername(process.Credentials.UserId)} doesn't have permission for {file.Name} {file.Group} {file.Permissions.PermissionValue}");
                    }
                }
                process.computer.Kernel.LS(process, fileList.ToArray());
                return(true);
            }
        }
Exemple #20
0
        public static bool View(CommandProcess process, string[] command)
        {
            if (command.Length < 2)
            {
                process.Print("Usage : view [file]");
                return(true);
            }
            var cmdArgs = command[1].Split(' ');

            if (cmdArgs.Length != 1)
            {
                process.Print("Usage : view [file]");
                return(true);
            }
            var activeDirectory = process.ActiveDirectory;
            var file            = activeDirectory.GetFile(cmdArgs[0]);

            if (file == null)
            {
                process.Print("File " + cmdArgs[0] + " not found.");
                return(true);
            }
            if (file.IsFolder())
            {
                process.Print("You cannot display a directory.");
                return(true);
            }
            if (!file.HasReadPermission(process.Credentials))
            {
                process.Print("Permission denied.");
                return(true);
            }

            process.computer.Kernel.Display(process, "view", file.Name, file.Content);
            return(true);
        }
Exemple #21
0
        public static bool DeleteUser(CommandProcess process, string[] command)
        {
            if (command.Length == 1)
            {
                return(false);
            }
            var cmdArgs = command[1].Split(' ');

            if (cmdArgs.Length != 1)
            {
                return(false);
            }
            File groupsFile = process.computer.fileSystem.RootFile.GetFile("etc").GetFile("group");
            File usersFile  = process.computer.fileSystem.RootFile.GetFile("etc").GetFile("passwd");

            if (!groupsFile.HasWritePermission(process.Credentials) && !usersFile.HasWritePermission(process.Credentials))
            {
                process.Print("You do not have the required permissions");
                return(true);
            }
            string         username = cmdArgs[0];
            List <Account> accounts = process.computer.Kernel.GetAccounts();

            if (accounts.Find(acc => acc.Username == username) == null)
            {
                process.Print("The user \"" + username + "\" does not exists");
                return(true);
            }

            Account account = accounts.Find(acc => acc.Username == username);

            account.Delete();
            account.ApplyChanges();
            process.Print("The account \"" + account.Username + "\" has been successfully deleted");
            return(true);
        }
Exemple #22
0
        public static bool Balance(CommandProcess process, string[] command)
        {
            BankClient client = (BankClient)process;
            BankDaemon daemon = (BankDaemon)client.Daemon;

            if (command[0] == "balance")
            {
                if (command.Length < 2)
                {
                    process.Print("Usage : balance set [accountname] [value]/get [accountname]");
                    return(true);
                }
                var cmdArgs = command[1].Split(' ');
                if (cmdArgs.Length < 2)
                {
                    process.Print("Usage : balance set [accountname] [value]/get [accountname]");
                    return(true);
                }
                if (cmdArgs[0] == "set" && cmdArgs.Length < 3)
                {
                    process.Print("Usage : balance set [accountname] [value]/get [accountname]");
                    return(true);
                }
                Account account = null;
                foreach (var account2 in daemon.accounts)
                {
                    if (account2.accountName == cmdArgs[1])
                    {
                        account = account2;
                        break;
                    }
                }
                if (account == null)
                {
                    process.Print("Account data for this account does not exist in the database");
                    return(true);
                }
                if (cmdArgs[0] == "set")
                {
                    if (int.TryParse(cmdArgs[2], out int val))
                    {
                        account.balance = val;
                        daemon.UpdateAccountDatabase();
                    }
                    else
                    {
                        process.Print("Error: non-integer value specified");
                        return(true);
                    }
                }
                process.Print($"Account balance for {account.accountName} is {account.balance}");
                return(true);
            }
            return(false);
        }
Exemple #23
0
        public static bool AddToNetMap(CommandProcess process, string[] commandUnsplit)
        {
            List <string> command = new List <string>();

            command.Add("netmap");
            command.AddRange(commandUnsplit[1].Split());
            if (command.Count < 4)
            {
                //TODO kernel
                process.Print("Usage: netmap [ip] [x] [y]");
                return(true);
            }
            //TODO kernel
            process.computer.Kernel.AddNodeToNetMap(process, Server.Instance.DatabaseLink.Computers.Find(command[1]), command[2], command[3]);
            return(true);
        }
Exemple #24
0
        public static bool ChMod(CommandProcess process, string[] command)
        {
            if (command.Length < 2)
            {
                process.Print(commands[command[0]].Item1);
                return(true);
            }
            var cmdArgs = command[1].Split(' ');

            if (cmdArgs.Length != 2)
            {
                process.Print(commands[command[0]].Item1);
                return(true);
            }
            var activePrivs = process.Credentials.Groups;

            var activeDirectory = process.ActiveDirectory;

            foreach (var fileC in activeDirectory.children)
            {
                if (fileC.Name == cmdArgs[1])
                {
                    if (process.Credentials.UserId != fileC.OwnerId)
                    {
                        process.Print("Permission denied.");
                        return(true);
                    }

                    if (!Permissions.PermissionHelper.ApplyModifiers(cmdArgs[0], fileC.Permissions))
                    {
                        process.Print($"Invalid mode '{cmdArgs[0]}'\r\nUsage : chmod [permissions] [file]");
                        return(true);
                    }

                    process.Print($"File {fileC.Name} permissions changed. to {fileC.Permissions.PermissionValue}");

                    return(true);
                }
            }
            process.Print("File " + cmdArgs[1] + " was not found.");
            return(true);
        }
Exemple #25
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);
        }
Exemple #26
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);
        }
Exemple #27
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);
        }
Exemple #28
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);
        }
Exemple #29
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);
        }
Exemple #30
0
        public static bool Fedit(CommandProcess process, string[] command)
        {
            if (command.Length < 2)
            {
                process.Print("Usage : fedit [append/line/remove/insert/help]");
                return(true);
            }
            var cmdArgs = command[1].Split(' ');

            if (cmdArgs[0] == "help")
            {
                process.Print("fedit [append] [file] [text] - Appends 'text' in a new line, at the bottom of the file.\n" +
                              "fedit [line] [file] [n] [text] - Changes content of line 'n' to 'text'.\n" +
                              "fedit [remove] [file] [n] - Removes line 'n' of the file.\n" +
                              "fedit [insert] [file] [n] [text] - Insert a new line containing 'text' in the 'n' line number.");
                return(true);
            }
            if (cmdArgs[0] == "append")
            {
                if (cmdArgs.Length < 3)
                {
                    process.Print("Missing arguments");
                    return(true);
                }
                var file = process.ActiveDirectory.GetFile(cmdArgs[1]);
                if (file == null)
                {
                    process.Print("File " + cmdArgs[1] + " not found.");
                    return(true);
                }
                if (!file.HasWritePermission(process.Credentials))
                {
                    process.Print("Permission denied.");
                    return(true);
                }

                file.Content += '\n' + cmdArgs.JoinWords(" ", 2);
                process.Print("Content appended.");
                return(true);
            }
            if (cmdArgs[0] == "line")
            {
                if (cmdArgs.Length < 3)
                {
                    process.Print("Missing arguments");
                    return(true);
                }
                var file = process.ActiveDirectory.GetFile(cmdArgs[1]);
                if (file == null)
                {
                    process.Print("File " + cmdArgs[1] + " not found.");
                    return(true);
                }
                if (!file.HasWritePermission(process.Credentials))
                {
                    process.Print("Permission denied.");
                    return(true);
                }
                int n;
                if (!int.TryParse(cmdArgs[2], out n))
                {
                    process.Print("Wrong line number.");
                    return(true);
                }
                var nth = file.Content.GetNthOccurence(n, '\n');
                file.Content = file.Content.Remove(nth, file.Content.GetNthOccurence(n + 1, '\n') - nth);
                file.Content = file.Content.Insert(nth, '\n' + cmdArgs.JoinWords(" ", 3));
                process.Print("Line edited.");
                return(true);
            }
            if (cmdArgs[0] == "remove")
            {
                if (cmdArgs.Length < 3)
                {
                    process.Print("Missing arguments");
                    return(true);
                }
                var file = process.ActiveDirectory.GetFile(cmdArgs[1]);
                if (file == null)
                {
                    process.Print("File " + cmdArgs[1] + " not found.");
                    return(true);
                }
                if (!file.HasWritePermission(process.Credentials))
                {
                    process.Print("Permission denied.");
                    return(true);
                }
                int n;
                if (!int.TryParse(cmdArgs[2], out n))
                {
                    process.Print("Wrong line number.");
                    return(true);
                }
                var nth = file.Content.GetNthOccurence(n, '\n');
                file.Content = file.Content.Remove(nth, file.Content.GetNthOccurence(n + 1, '\n') - nth);
                process.Print("Line removed");
                return(true);
            }
            if (cmdArgs[0] == "insert")
            {
                if (cmdArgs.Length < 3)
                {
                    process.Print("Missing arguments");
                    return(true);
                }
                var file = process.ActiveDirectory.GetFile(cmdArgs[1]);
                if (file == null)
                {
                    process.Print("File " + cmdArgs[1] + " not found.");
                    return(true);
                }
                if (!file.HasWritePermission(process.Credentials))
                {
                    process.Print("Permission denied.");
                    return(true);
                }
                int n;
                if (!int.TryParse(cmdArgs[2], out n))
                {
                    process.Print("Wrong line number.");
                    return(true);
                }
                file.Content = file.Content.Insert(file.Content.GetNthOccurence(n, '\n'), '\n' + cmdArgs.JoinWords(" ", 3));
                process.Print("Content inserted");
                return(true);
            }
            process.Print("Usage : fedit [append/line/remove/insert/help]");
            return(true);
        }