Ejemplo n.º 1
0
        /*
         * The SEARCH command searches the mailbox for messages that match
         * the given searching criteria. Searching criteria consist of one
         * or more search keys. The untagged SEARCH response from the server
         * contains a listing of message sequence numbers corresponding to
         * those messages that match the searching criteria.
         */
        public static void Search(string[] command, Connection connectionState)
        {
            if (connectionState.SelectedState && command.Length > SEARCH_MINSPLIT)
            {
                var messages = IMAP_Search.Search(command.Skip(2).ToArray(), connectionState);
                if (messages.First() != -1)
                {
                    string response = "";
                    foreach (var message in messages)
                    {
                        response += $"{message} ";
                    }

                    connectionState.SendToStream($"* SEARCH {response}");
                    connectionState.SendToStream($"{command[0]} OK SEARCH completed");
                }
                else
                {
                    connectionState.SendToStream($"{command[0]} NO - search error: can’t search that [CHARSET] or criteria");
                }
            }
            else
            {
                connectionState.SendToStream($"{command[0]} BAD - no search criteria specified");
            }
        }
Ejemplo n.º 2
0
        /*
         * The UID command has two forms. In the first form, it takes as its
         * arguments a COPY, FETCH, or STORE command with arguments
         * appropriate for the associated command. However, the numbers in
         * the sequence set argument are unique identifiers instead of
         * message sequence numbers. Sequence set ranges are permitted, but
         * there is no guarantee that unique identifiers will be contiguous...
         *
         * In the second form, the UID command takes a SEARCH command with
         * SEARCH command arguments. The interpretation of the arguments is
         * the same as with SEARCH; however, the numbers returned in a SEARCH
         * response for a UID SEARCH command are unique identifiers instead of message sequence numbers.
         */
        public static void UID(string command, Connection connectionState)
        {
            var tag = command.Split(' ').First();

            if (connectionState.SelectedState)
            {
                if (command.ToLower().Contains("fetch"))
                {
                    if (IMAP_Fetch.TryFetch(command, connectionState, true))
                    {
                        connectionState.SendToStream($"{tag} OK UID FETCH completed");
                    }
                }
                else if (command.ToLower().Contains("search"))
                {
                    var commandParams = IMAP_Fetch.ParseFetchCommand(command);
                    var messagesNums  = IMAP_Search.Search(commandParams.Skip(1).ToArray(), connectionState);
                    if (messagesNums.First() != -1)
                    {
                        string response = "";
                        foreach (var messageNum in messagesNums)
                        {
                            foreach (var message in connectionState.SelectedMailBox.EmailMessages)
                            {
                                if (message.MsgNum == messageNum)
                                {
                                    response += $"{message.MessageId} ";
                                    break;
                                }
                            }
                        }

                        connectionState.SendToStream($"* SEARCH {response}");
                        connectionState.SendToStream($"{command[0]} OK UID completed");
                    }
                    else
                    {
                        connectionState.SendToStream($"{command[0]} BAD - this command is not supported");
                    }
                }
            }
            else
            {
                connectionState.SendToStream($"{tag} BAD - command unknown or arguments invalid");
            }
        }