public override string Parse(ref SwitchboardUser User, string Command)
        {
            if (!Command.ToUpper().StartsWith("CQUOR"))
            {
                return(null);
            }

            //Make sure the user can access Colloquor
            if (!User.CanExecute(PermissionLevel))
            {
                return("NOEXECUTE");
            }

            Command = Command.Remove(0, 6); //Remove "CQUOR "
            String[] CommandSplit = Command.Split(' ');

            switch (CommandSplit[0].ToUpper())
            {
            case "JOIN":

                //First let's make sure the user isn't already in a channel
                if (UserChannelDictionary.ContainsKey(User))
                {
                    return("ALREADY CONNECTED");
                }

                if (CommandSplit.Length == 2)
                {
                    //Join without a password.
                    //First get the channel
                    if (!ChannelDictionary.ContainsKey(CommandSplit[1]))
                    {
                        return("NOT FOUND");
                    }
                    if (ChannelDictionary[CommandSplit[1]].HasPassword())
                    {
                        return("NEEDS PASS");
                    }

                    //Now that we know that the channel exists, let's add the user.
                    UserChannelDictionary.Add(User, ChannelDictionary[CommandSplit[1]]);
                    UserChannelDictionary[User].ReceiveMessage(User.GetUsername() + " joined the channel!");
                    return(UserChannelDictionary[User].GetWelcome() + "\n\n");
                }
                else if (CommandSplit.Length == 3)
                {
                    //Join with a password.
                    //First get the channel
                    if (!ChannelDictionary.ContainsKey(CommandSplit[1]))
                    {
                        return("NOT FOUND");
                    }
                    if (!ChannelDictionary[CommandSplit[1]].VerifyPassword(CommandSplit[2]))
                    {
                        return("WRONG PASS");
                    }

                    //Now that we know that the channel exists, let's add the user.
                    UserChannelDictionary.Add(User, ChannelDictionary[CommandSplit[1]]);
                    UserChannelDictionary[User].ReceiveMessage(User.GetUsername() + " joined the channel!");
                    return(UserChannelDictionary[User].GetWelcome() + "\n\n");
                }
                else
                {
                    return("ERR");
                }

            case "LISTCHANNELS":
                //Send a list of all channels. The list should appear as:
                //CHANNEL_NAME:CHANNEL_HAS_PASSWORD,CHANNEL_NAME2...

                List <String> AllChannels = new List <String>();
                foreach (ColloquorChannel channel in ChannelDictionary.Values)
                {
                    AllChannels.Add(channel.ToListChannelString());
                }
                return(String.Join(",", AllChannels));

            case "SEND":
                if (!UserChannelDictionary.ContainsKey(User))
                {
                    return("NOT CONNECTED");
                }
                UserChannelDictionary[User].ReceiveMessage("[" + User.GetUsername() + "] " + Command.Remove(0, 5));  //Remove "SEND "
                return(UserChannelDictionary[User].GetLastMessage());

            case "REQUEST":
                if (!UserChannelDictionary.ContainsKey(User))
                {
                    return("NOT CONNECTED");
                }
                return(UserChannelDictionary[User].GetLastMessage());

            case "LEAVE":
                if (!UserChannelDictionary.ContainsKey(User))
                {
                    return("NOT CONNECTED");
                }
                UserChannelDictionary[User].ReceiveMessage(User.GetUsername() + " left the channel");
                UserChannelDictionary.Remove(User);
                return("OK");

            case "PING":
                return("PONG");

            default:
                return(null);
            }
        }
示例#2
0
        public override string Parse(ref SwitchboardUser User, string Command)
        {
            if (!Command.ToUpper().StartsWith("LBL~"))
            {
                return(null);
            }

            string[] CommandSplit = Command.Split('~');
            try {
                switch (CommandSplit[1].ToUpper())
                {
                case "DOWNLOAD":
                    //Covering "\.." to avoid having people get out of the root directory.
                    if (!User.CanExecute(DownloadPLevel) || CommandSplit[2].Contains("\\.."))
                    {
                        return("LBL.N");
                    }
                    if (CommandSplit.Length != 3)
                    {
                        return("LBL.A");
                    }
                    if (!File.Exists(RootDir + CommandSplit[2]))
                    {
                        return("LBL.NOTFOUND");
                    }
                    return(CreateTransfer(LBLServerTransfer.LBLTransferType.Send, false, CommandSplit[2]));

                case "UPLOAD":
                    if (!User.CanExecute(UploadPLevel) || CommandSplit[2].Contains("\\.."))
                    {
                        return("LBL.N");
                    }
                    if (CommandSplit.Length != 3)
                    {
                        return("LBL.A");
                    }
                    if (FileBusy(CommandSplit[2], out int BusyID))
                    {
                        return(BusyID.ToString());
                    }
                    return(CreateTransfer(LBLServerTransfer.LBLTransferType.Receive, false, CommandSplit[2]));

                case "OVERWRITE":
                    //Especially important here!!
                    if (!User.CanExecute(UploadPLevel) || CommandSplit[2].Contains("\\.."))
                    {
                        return("LBL.N");
                    }
                    if (CommandSplit.Length != 3)
                    {
                        return("LBL.A");
                    }
                    if (FileBusy(CommandSplit[2], out int doot))
                    {
                        return("LBL.BUSY");
                    }
                    return(CreateTransfer(LBLServerTransfer.LBLTransferType.Receive, true, CommandSplit[2]));

                case "APPEND":
                    if (!User.CanExecute(UploadPLevel))
                    {
                        return("LBL.N");
                    }
                    if (CommandSplit.Length < 4)
                    {
                        return("LBL.A");
                    }
                    if (!int.TryParse(CommandSplit[2], out int AppendID))
                    {
                        return("LBL.A");
                    }
                    if (!Transfers.ContainsKey(AppendID))
                    {
                        return("LBL.NOTFOUND");
                    }

                    Transfers[AppendID].Receive(CommandSplit[3]);
                    return("LBL.OK");

                case "REQUEST":
                    if (!User.CanExecute(DownloadPLevel))
                    {
                        return("LBL.N");
                    }
                    if (CommandSplit.Length < 3)
                    {
                        return("LBL.A");
                    }
                    if (!int.TryParse(CommandSplit[2], out int RequestID))
                    {
                        return("LBL.A");
                    }
                    if (!Transfers.ContainsKey(RequestID))
                    {
                        return("LBL.NOTFOUND");
                    }

                    string Line = Transfers[RequestID].Send();
                    if (string.IsNullOrEmpty(Line))
                    {
                        return("LBL.EMPTY");
                    }
                    else
                    {
                        return(Line);
                    }

                case "CLOSE":
                    if (!User.CanExecute(DownloadPLevel))
                    {
                        return("LBL.N");
                    }
                    if (!int.TryParse(CommandSplit[2], out int CloseID))
                    {
                        return("LBL.A");
                    }
                    if (!Transfers.ContainsKey(CloseID))
                    {
                        return("LBL.NOTFOUND");
                    }

                    Transfers[CloseID].Close();
                    Transfers.TryRemove(CloseID, out LBLServerTransfer D);

                    return("LBL.OK");

                case "DIR":
                    if (CommandSplit.Length != 3)
                    {
                        return("LBL.A");
                    }

                    if (!Directory.Exists(RootDir + CommandSplit[2]?.ToString()))
                    {
                        return("LBL.NOTFOUND");
                    }

                    //Get dirs
                    string[] Directories = Directory.GetDirectories(RootDir + CommandSplit[2]?.ToString());

                    //get files
                    string[] Files = Directory.GetFiles(RootDir + CommandSplit[2]?.ToString());

                    //empty directories will return just "~"
                    //if(Files.Length == 0) { return "LBL.EMPTY"; }

                    //replace prefix
                    for (int i = 0; i < Files.Length; i++)
                    {
                        Files[i] = Files[i].Replace(RootDir + CommandSplit[2]?.ToString(), "");
                    }
                    for (int i = 0; i < Directories.Length; i++)
                    {
                        Directories[i] = Directories[i].Replace(RootDir + CommandSplit[2]?.ToString(), "");
                    }

                    return(String.Join("~", String.Join(",", Directories), String.Join(",", Files)));

                case "PING":
                    return("PONG");

                default:
                    return(null);
                }
            } catch (Exception E) {
                Console.WriteLine(E.Message + "\n\n" + E.StackTrace);
                return("LBL.E:" + E.Message + ":" + E.StackTrace);
            }
        }