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")); } }
public static string ReturnAppendResponse(string tag, string argument, string userSession, AppendCall appendCall) { //var maths = Regex.Match(argument, ""); var math = Regex.Match(argument, "^(?:((?:\"[\\w\\s]+\"|\\w+) \\((?:\\\\\\w+)*\\) \"[^\"]*\" \\{\\d*\\})|((?:\"[\\w\\s]+\"|\\w+) \\((?:\\\\\\w+)*\\) \\{\\d*\\})|((?:\"[\\w\\s]+\"|\\w+) \\{\\d*\\}))"); if (!math.Success) { return(Response.ReturnParseErrorResponse(tag, "APPEND")); } MailInfo mailAppend = new MailInfo(); Flags flags = new Flags(); string mailBoxName; List <string> strFlag = new List <string>(); DateTime Date = DateTime.Now; int messageSize = 0; if (math.Groups[3].Success) { math = Regex.Match(math.Groups[3].Value, "(?:(\"[\\w\\s]+\"|\\w+) \\{(\\d*)\\})"); mailBoxName = math.Groups[1].Value.Replace("\"", ""); if (math.Groups[2].Value != "" && !Int32.TryParse(math.Groups[3].Value, out messageSize)) { return(Response.ReturnParseErrorResponse(tag, "APPEND")); } } else { if (math.Groups[2].Success) { math = Regex.Match(math.Groups[2].Value, "(?:(\"[\\w\\s]+\"|\\w+) \\(((?:\\\\\\w+)*)\\) \\{(\\d*)\\})"); mailBoxName = math.Groups[1].Value.Replace("\"", ""); if (math.Groups[2].Value != "") { strFlag = math.Groups[2].Value.Split(' ').ToList(); } if (math.Groups[3].Value != "" && !Int32.TryParse(math.Groups[3].Value, out messageSize)) { return(Response.ReturnParseErrorResponse(tag, "APPEND")); } } else { math = Regex.Match(math.Groups[1].Value, "(?:(\"[\\w\\s]+\"|\\w+) \\(((?:\\\\\\w+)*)\\) \"([^\"]*)\" \\{(\\d*)\\})"); mailBoxName = math.Groups[1].Value.Replace("\"", ""); if (math.Groups[2].Value != "") { strFlag = math.Groups[2].Value.Split(' ').ToList(); } if (math.Groups[3].Value != "") { try { Date = Convert.ToDateTime(math.Groups[3].Value); } catch { return(Response.ReturnParseErrorResponse(tag, "APPEND")); } } if (math.Groups[4].Value != "" && !Int32.TryParse(math.Groups[4].Value, out messageSize)) { return(Response.ReturnParseErrorResponse(tag, "APPEND")); } } } List <MailBoxInfo> mailBoxInfoList = SqliteQuery.LoadMailBoxInfo(userSession, mailBoxName); if (mailBoxInfoList.Count == 0) { return(tag + " NO Mailbox does not exist"); } if (!flags.BuildFlagItem(strFlag.ToArray())) { return(Response.ReturnParseErrorResponse(tag, "APPEND")); } mailAppend.user = userSession; mailAppend.mailboxname = mailBoxName; mailAppend.uid = mailBoxInfoList[0].uidnext; mailAppend.recent = 1; mailAppend.seen = (flags.seen == "1" ? 1 : 0); mailAppend.answered = (flags.answered == "1" ? 1 : 0); mailAppend.deleted = (flags.deleted == "1" ? 1 : 0); mailAppend.draft = (flags.draft == "1" ? 1 : 0); mailAppend.flagged = (flags.flagged == "1" ? 1 : 0); mailAppend.intertime = ((DateTimeOffset)Date).ToUnixTimeSeconds(); appendCall.isCall = true; appendCall.mailInfo = mailAppend; appendCall.size = messageSize; appendCall.tag = tag; return("+ Ready for append literal"); }