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); }
public static string ReturnStoreResponse(string tag, string argument, string userSession, string userMailBox, bool fromUIDCommand = false) { var math = Regex.Match(argument, @"^((?:(?:(?:[1-9]+|\*):(?:[1-9]+|\*)|[1-9]+),)*(?:(?:[1-9]+|\*):(?:[1-9]+|\*)|[1-9]+)) ([^\s]+) \(([^\(\)]*)\)"); if (!math.Success) { return(ReturnParseErrorResponse(tag, "STORE")); } string item = math.Groups[2].Value; if (!math.Success) { return(ReturnParseErrorResponse(tag, "FETCH")); } List <MailInfo> tempMailInfoList; List <MailInfo> mailInfoList = new List <MailInfo>(); 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)) { mailInfoList.Add(tempMail); } } } mailInfoList.Sort((x, y) => x.uid.CompareTo(y.uid)); string newArgument = math.Groups[1].Value + " (FLAGS)"; Flags flags = new Flags(); int success; if (math.Groups[3].Value != "") { string[] flagsArr = math.Groups[3].Value.Split(' '); if (!flags.BuildFlagItem(flagsArr)) { return(ReturnParseErrorResponse(tag, "STORE")); } } switch (item.ToLower()) { case "flags": if (flags.seen != "1") { flags.seen = "0"; } if (flags.answered != "1") { flags.answered = "0"; } if (flags.flagged != "1") { flags.flagged = "0"; } if (flags.deleted != "1") { flags.deleted = "0"; } if (flags.draft != "1") { flags.draft = "0"; } foreach (MailInfo mailInfo in mailInfoList) { success = SqliteQuery.UpdateFlagsWithUID(userSession, userMailBox, mailInfo.uid.ToString(), flags); } return(Response.StoreFormatReturn(tag, newArgument, userSession, userMailBox, fromUIDCommand)); case "flags.silent": if (flags.seen != "1") { flags.seen = "0"; } if (flags.answered != "1") { flags.answered = "0"; } if (flags.flagged != "1") { flags.flagged = "0"; } if (flags.deleted != "1") { flags.deleted = "0"; } if (flags.draft != "1") { flags.draft = "0"; } foreach (MailInfo mailInfo in mailInfoList) { success = SqliteQuery.UpdateFlagsWithUID(userSession, userMailBox, mailInfo.uid.ToString(), flags); } return(tag + " OK STORE completed"); case "+flags": foreach (MailInfo mailInfo in mailInfoList) { success = SqliteQuery.UpdateFlagsWithUID(userSession, userMailBox, mailInfo.uid.ToString(), flags); } return(Response.StoreFormatReturn(tag, newArgument, userSession, userMailBox, fromUIDCommand)); case "+flags.silent": foreach (MailInfo mailInfo in mailInfoList) { success = SqliteQuery.UpdateFlagsWithUID(userSession, userMailBox, mailInfo.uid.ToString(), flags); } return(tag + " OK STORE completed"); case "-flags": if (flags.seen == "1") { flags.seen = "0"; } if (flags.answered == "1") { flags.answered = "0"; } if (flags.flagged == "1") { flags.flagged = "0"; } if (flags.deleted == "1") { flags.deleted = "0"; } if (flags.draft == "1") { flags.draft = "0"; } foreach (MailInfo mailInfo in mailInfoList) { success = SqliteQuery.UpdateFlagsWithUID(userSession, userMailBox, mailInfo.uid.ToString(), flags); } return(Response.StoreFormatReturn(tag, newArgument, userSession, userMailBox, fromUIDCommand)); case "-flags.silent": if (flags.seen == "1") { flags.seen = "0"; } if (flags.answered == "1") { flags.answered = "0"; } if (flags.flagged == "1") { flags.flagged = "0"; } if (flags.deleted == "1") { flags.deleted = "0"; } if (flags.draft == "1") { flags.draft = "0"; } foreach (MailInfo mailInfo in mailInfoList) { success = SqliteQuery.UpdateFlagsWithUID(userSession, userMailBox, mailInfo.uid.ToString(), flags); } return(tag + " OK STORE completed"); default: return(ReturnParseErrorResponse(tag, "STORE")); } }
//selected state public static string ReturnFetchResponse(string tag, string argument, string userSession, string userMailBox, bool fromUIDCommand = false, bool slient = false) //mới được có 2 cái header và text body thôi nha mấy cha { string response = ""; 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; List <MailInfo> mailInfoList = new List <MailInfo>(); 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)) { mailInfoList.Add(tempMail); } } } mailInfoList.Sort((x, y) => x.uid.CompareTo(y.uid)); string[] items = math.Groups[2].Value.Split(' '); string[] arguments = argument.Split(' '); DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc); string mailbBoxDir = Environment.CurrentDirectory + $"/ImapMailBox/{userSession}/{userMailBox}/"; string emailPath; if (mailInfoList.Count == 0) { return(tag + " OK FETCH completed"); } foreach (MailInfo mailInfo in mailInfoList) { emailPath = mailbBoxDir + $"email_{mailInfo.uid}"; if (File.Exists(emailPath + ".eml")) { if (File.Exists(emailPath + ".msg")) { emailPath += ".msg"; } else { emailPath += ".eml"; } } else { if (File.Exists(emailPath + ".msg")) { emailPath += ".msg"; } else { continue; } } FileInfo email = new FileInfo(emailPath); response += "* " + (fromUIDCommand ? mailInfo.uid : mailInfo.numrow) + " FETCH ("; bool first = true; foreach (string item in items) { if (!first) { response += " "; } first = false; switch (item.ToLower()) { case "uid": response += $"UID {mailInfo.uid}"; break; case "flags": string[] tempArr = { (mailInfo.recent == 1 ? "\\Recent":""), (mailInfo.answered == 1 ? "\\Answered" : ""), (mailInfo.flagged == 1 ? "\\Flagged" : ""), (mailInfo.deleted == 1 ? "\\Deleted" : ""), (mailInfo.seen == 1 ? "\\Seen" : ""), (mailInfo.draft == 1 ? "\\Draft" : "") }; tempArr = tempArr.Where(x => !string.IsNullOrEmpty(x)).ToArray(); response += "FLAGS (" + string.Join(' ', tempArr) + ")"; break; case "rfc822.size": response += $"RFC822.SIZE {email.Length}"; break; case "body.peek[]": response += "BODY[] {" + email.Length + "}\r\n"; using (StreamReader sr = new StreamReader(email.OpenRead())) { string temp = sr.ReadToEnd(); response += temp; } slient = true; break; case "internaldate": dtDateTime = dtDateTime.AddSeconds(mailInfo.intertime).ToLocalTime(); response += "INTERNALDATE \"" + dtDateTime.ToString("dd-MMM-yyyy HH:mm:ss zzz") + "\""; break; default: break; } } response += $")\r\n"; } int success; if (!slient) { foreach (MailInfo mailInfo in mailInfoList) { if (fromUIDCommand) { success = SqliteQuery.UpdateSeenFlagWithUID(userSession, userMailBox, mailInfo.uid.ToString()); } else { success = SqliteQuery.UpdateSeenFlagWithIndex(userSession, userMailBox, mailInfo.numrow.ToString()); } } } response += tag + " OK FETCH completed"; return(response); }