예제 #1
0
        public static string ReturnMessagesAppendResponse(string commandLine, AppendCall appendCall, bool startTLS)
        {
            try
            {
                if (!appendCall.completed)
                {
                    if (startTLS)
                    {
                        appendCall.message += commandLine;
                    }
                    else
                    {
                        appendCall.message += commandLine + "\r\n";
                    }
                    if (appendCall.message.Length < appendCall.size)
                    {
                        return("");
                    }
                    if (appendCall.message.Length > appendCall.size)
                    {
                        appendCall.reset();
                        return(appendCall.tag + " NO APPEND Failed");
                    }
                    appendCall.completed = true;
                    return("");
                }

                File.WriteAllText(Environment.CurrentDirectory + $"/ImapMailBox/{appendCall.mailInfo.user}/{appendCall.mailInfo.mailboxname}/email_{appendCall.mailInfo.uid}.msg", appendCall.message);
                int success = SqliteQuery.InsertMailIntoMailBox(appendCall.mailInfo.user, appendCall.mailInfo.mailboxname, appendCall.mailInfo);
                appendCall.reset();
                return(appendCall.tag + " OK APPEND completed");
            }
            catch (Exception)
            {
                appendCall.reset();
                return(appendCall.tag + " No Can't APPEND");
            }
        }
예제 #2
0
        public static string ReturnExpungeResponse(string tag, string userSession, string userMailBox, bool fromUIDCommand = false, string argument = "")
        {
            List <MailInfo> mailInfoList = new List <MailInfo>();

            if (argument != "")
            {
                var math = Regex.Match(argument, @"^((?:(?:(?:[1-9]+|\*):(?:[1-9]+|\*)|[1-9]+),)*(?:(?:[1-9]+|\*):(?:[1-9]+|\*)|[1-9]+))");
                if (!math.Success)
                {
                    return(ReturnParseErrorResponse(tag, "FETCH"));
                }
                List <MailInfo> tempMailInfoList;
                string          right        = "";
                string          left         = "";
                string[]        mailIndexArr = math.Groups[1].Value.Split(',');
                foreach (string mailIndex in mailIndexArr)
                {
                    if (mailIndex.Contains(':'))
                    {
                        string[] temp = mailIndex.Split(':', StringSplitOptions.RemoveEmptyEntries);
                        if (temp[0] == "*")
                        {
                            if (fromUIDCommand)
                            {
                                left = "uid";
                            }
                            else
                            {
                                left = "numrow";
                            }
                        }
                        else
                        {
                            left = temp[0];
                        }
                        if (temp[1] == "*")
                        {
                            if (fromUIDCommand)
                            {
                                right = "uid";
                            }
                            else
                            {
                                right = "numrow";
                            }
                        }
                        else
                        {
                            right = temp[1];
                        }
                        if (fromUIDCommand)
                        {
                            tempMailInfoList = SqliteQuery.LoadMailInfoWithUID(userSession, userMailBox, left, right);
                        }
                        else
                        {
                            tempMailInfoList = SqliteQuery.LoadMailInfoWithIndex(userSession, userMailBox, left, right);
                        }
                    }
                    else
                    {
                        if (fromUIDCommand)
                        {
                            tempMailInfoList = SqliteQuery.LoadMailInfoWithUID(userSession, userMailBox, mailIndex);
                        }
                        else
                        {
                            tempMailInfoList = SqliteQuery.LoadMailInfoWithIndex(userSession, userMailBox, mailIndex);
                        }
                    }
                    foreach (MailInfo tempMail in tempMailInfoList)
                    {
                        if (!mailInfoList.Contains(tempMail) && tempMail.deleted == 1)
                        {
                            mailInfoList.Add(tempMail);
                        }
                    }
                }
                mailInfoList.Sort((x, y) => x.uid.CompareTo(y.uid));
            }
            else
            {
                mailInfoList = SqliteQuery.LoadDeletedMail(userSession, userMailBox);
            }
            if (mailInfoList.Count == 0)
            {
                return(tag + " OK EXPUNGE completed");
            }
            List <string> TrashMailbox = SqliteQuery.LoadTrashMailBoxName(userSession);
            string        response     = "";
            int           success;
            string        soursePath;
            string        desPath;
            long          baseUID;
            string        root = Environment.CurrentDirectory + $"/ImapMailBox/{userSession}";

            if (TrashMailbox.IndexOf(userMailBox) == -1 && TrashMailbox.Count != 0)
            {
                foreach (string mailBox in TrashMailbox)
                {
                    List <MailBoxInfo> mailBoxInfo = SqliteQuery.LoadMailBoxInfo(userSession, mailBox);
                    if (mailBoxInfo.Count == 0)
                    {
                        return("OK EXPUNGE completed");
                    }
                    baseUID = mailBoxInfo[0].uidnext;
                    foreach (MailInfo mail in mailInfoList)
                    {
                        soursePath = root + $"/{userMailBox}/email_{baseUID}";
                        if (File.Exists(soursePath + ".msg"))
                        {
                            soursePath += ".msg";
                            desPath     = root + $"/{mailBox}/email_{baseUID}.msg";
                        }
                        else
                        {
                            if (File.Exists(soursePath + ".eml"))
                            {
                                soursePath += ".eml";
                                desPath     = root + $"/{mailBox}/email_{baseUID}.eml";
                            }
                            else
                            {
                                return(tag + "OK EXPUNGE completed");
                            }
                        }
                        File.Copy(soursePath, desPath);
                        File.Delete(soursePath);
                        success          = SqliteQuery.DeleteMailWithUID(userSession, userMailBox, mail.uid);
                        mail.uid         = baseUID++;
                        mail.mailboxname = mailBox;
                        success          = SqliteQuery.InsertMailIntoMailBox(userSession, mailBox, mail);
                        response        += $"* {mail.numrow} EXPUNGE\r\n";
                    }
                }
            }
            else
            {
                foreach (MailInfo mail in mailInfoList)
                {
                    soursePath = root + $"/{userMailBox}/email_{mail.uid}";
                    if (File.Exists(soursePath + ".msg"))
                    {
                        soursePath += ".msg";
                    }
                    else
                    {
                        if (File.Exists(soursePath + ".eml"))
                        {
                            soursePath += ".eml";
                        }
                        else
                        {
                            return(tag + "OK EXPUNGE completed");
                        }
                    }
                    File.Delete(soursePath);
                    success   = SqliteQuery.DeleteMailWithUID(userSession, userMailBox, mail.uid);
                    response += $"* {mail.numrow} EXPUNGE\r\n";
                }
            }
            response += tag + " OK EXPUNGE completed";
            return(response);
        }