Example #1
0
        public static List <Message> GetCollection(MailDb maildb, SelectFolder Folder, SearchItem item)
        {
            if (item == null || item.Name == null)
            {
                return(maildb.Messages.FolderQuery(Folder.Folder).SelectAll());
            }

            if (item.Name == "LARGER" || item.Name == "SMALLER")
            {
                int value = (int)item.Parameters["N"];

                var msg = maildb.Messages.GetBySeqNo(Folder.Folder, Folder.Stat.LastestMsgId, Folder.Stat.Exists, value);

                if (msg == null)
                {
                    throw new CommandException("NO", "not a valid sequence no. ");
                }

                if (item.Name == "LARGER")
                {
                    return(maildb.Messages.FolderQuery(Folder.Folder).Where(o => o.Id > msg.Id).SelectAll());
                }
                else if (item.Name == "SMALLER")
                {
                    return(maildb.Messages.FolderQuery(Folder.Folder).Where(o => o.Id < msg.Id).SelectAll());
                }
            }
            else if (item.Name == "UID")
            {
                var set   = item.Parameters["SEQUENCE-SET"].ToString();
                var range = Kooboo.Mail.Imap.ImapHelper.GetSequenceRange(set);
                ImapHelper.CorrectRange(range, Folder, true);

                List <Message> result = new List <Message>();
                foreach (var uidrange in range)
                {
                    for (int i = uidrange.LowBound; i <= uidrange.UpBound; i++)
                    {
                        var msg = maildb.Messages.Get(i);
                        if (msg != null)
                        {
                            result.Add(msg);
                        }
                    }
                }
                return(result);
            }
            else
            {
                if (item.Name == "BEFORE" || item.Name == "SENTBEFORE")
                {
                    var date = (DateTime)item.Parameters["DATE"];
                    var tick = date.Ticks;
                    return(maildb.Messages.FolderQuery(Folder.Folder).Where(o => o.CreationTimeTick < tick).SelectAll());
                }
                else if (item.Name == "SENTSINCE" || item.Name == "SINCE")
                {
                    var date = (DateTime)item.Parameters["DATE"];
                    var tick = date.Ticks;
                    return(maildb.Messages.FolderQuery(Folder.Folder).Where(o => o.CreationTimeTick > tick).SelectAll());
                }
                else if (item.Name == "ON" || item.Name == "SENTON")
                {
                    var date = (DateTime)item.Parameters["DATE"];
                    // get the day before and after, then filter... I think this has better performance because there is an index on tick.
                    var before = date.AddDays(-1);
                    var after  = date.AddDays(1);

                    var allmessages = maildb.Messages.FolderQuery(Folder.Folder).Where(o => o.CreationTimeTick > before.Ticks && o.CreationTimeTick < after.Ticks).SelectAll();

                    if (allmessages != null && allmessages.Count() > 0)
                    {
                        return(allmessages.Where(o => o.CreationTime.DayOfYear == date.DayOfYear && o.CreationTime.Year == date.Year).ToList());
                    }
                }
                // string dateKey = "BEFORE SENTBEFORE SENTON SENTSINCE SINCE ON";
                // only those left...
            }

            // not collection found, loop all.
            return(maildb.Messages.FolderQuery(Folder.Folder).SelectAll());
        }
Example #2
0
        public SearchItem ReadItem()
        {
            var token = ConsumeNextToken();

            if (token != null)
            {
                var keyword = Keywords.Find(token);
                if (keyword == null)
                {
                    return(ReadItem());
                }

                SearchItem result = new SearchItem()
                {
                    Name = keyword.Name, AsCollection = keyword.UseAsCollection
                };

                if (keyword.Type == SearchType.NOT)
                {
                    result.Type = SearchType.NOT;
                    result.NOT  = this.ReadItem();
                    return(result);
                }
                else if (keyword.Type == SearchType.OR)
                {
                    result.Type  = SearchType.OR;
                    result.OROne = this.ReadItem();
                    result.ORTwo = this.ReadItem();
                    return(result);
                }
                else
                {
                    if (keyword.Arguments == null || keyword.Arguments.Count() == 0)
                    {
                        return(result);
                    }
                    else
                    {
                        foreach (var item in keyword.Arguments)
                        {
                            var next = this.ConsumeNextToken();

                            if (!item.Optional)
                            {
                                var rightvalue = ToRightDataType(next, item.DataType);
                                if (rightvalue != null)
                                {
                                    result.Parameters.Add(item.FieldName, rightvalue);
                                }
                            }
                            else
                            {
                                var find = Keywords.Find(next);
                                if (find != null)
                                {
                                    this.ReConsume(next);
                                    return(result);
                                }
                                else
                                {
                                    // convert to right DataType.
                                    var rightvalue = ToRightDataType(next, item.DataType);
                                    result.Parameters.Add(item.FieldName, rightvalue);
                                }
                            }
                        }

                        return(result);
                    }
                }
            }

            return(null);
        }
Example #3
0
        public static bool Check(MailDb maildb, SelectFolder folder, SearchItem item, Message message)
        {
            //ALL ANSWERED DELETED DRAFT FLAGGED NEW OLD RECENT SEEN UNANSWERED UNDELETED UNDRAFT UNFLAGGED UNSEEN
            switch (item.Name)
            {
            case "ALL":
            {
                return(true);
            }

            case "ANSWERED":
            {
                return(message.Answered);
            }

            case "DELETED":
            {
                return(message.Deleted);
            }

            case "DRAFT":
            {
                return(message.Draft);
            }

            case "FLAGGED":
            {
                return(message.Flagged);
            }

            case "NEW":
            {
                return(message.Recent);
            }

            case "OLD":
            {
                return(!message.Recent);
            }

            case "RECENT":
            {
                return(message.Recent);
            }

            case "SEEN":
            {
                return(message.Read);
            }

            ///UNANSWERED UNDELETED UNDRAFT UNFLAGGED UNSEEN
            case "UNANSWERED":
            {
                return(!message.Answered);
            }

            case "UNDELETED":
            {
                return(!message.Deleted);
            }

            case "UNDRAFT":
            {
                return(!message.Draft);
            }

            case "UNFLAGGED":
            {
                return(!message.Flagged);
            }

            case "UNSEEN":
            {
                return(!message.Read);
            }

            case "BCC":
            {
                return(CheckText(maildb, message, item.Name, item.Parameters.First().Value.ToString()));
            }

            ///"BCC BODY CC FROM SUBJECT TEXT TO";
            case "CC":
            {
                return(CheckText(maildb, message, item.Name, item.Parameters.First().Value.ToString()));
            }

            case "TO":
            {
                return(CheckText(maildb, message, item.Name, item.Parameters.First().Value.ToString()));
            }

            case "FROM":
            {
                return(CheckText(maildb, message, item.Name, item.Parameters.First().Value.ToString()));
            }

            case "SUBJECT":
            {
                return(CheckText(maildb, message, item.Name, item.Parameters.First().Value.ToString()));
            }

            case "TEXT":
            {
                return(CheckText(maildb, message, item.Name, item.Parameters.First().Value.ToString()));
            }

            case "BODY":
            {
                return(CheckText(maildb, message, item.Name, item.Parameters.First().Value.ToString()));
            }

            case "HEADER":
            {
                var    field = item.Parameters["FIELD-NAME"].ToString();
                string value = null;
                if (item.Parameters.ContainsKey("STRING"))
                {
                    value = item.Parameters["STRING"].ToString();
                }
                return(CheckText(maildb, message, field, value));
            }

            ///string dateKey = "BEFORE SENTBEFORE SENTON SENTSINCE SINCE ON";
            case "BEFORE":
            {
                return(CheckDate(maildb, message, item.Name, (DateTime)item.Parameters["DATE"]));
            }

            case "SENTBEFORE":
            {
                return(CheckDate(maildb, message, item.Name, (DateTime)item.Parameters["DATE"]));
            }

            case "SENTON":
            {
                return(CheckDate(maildb, message, item.Name, (DateTime)item.Parameters["DATE"]));
            }

            case "SENTSINCE":
            {
                return(CheckDate(maildb, message, item.Name, (DateTime)item.Parameters["DATE"]));
            }

            case "SINCE":
            {
                return(CheckDate(maildb, message, item.Name, (DateTime)item.Parameters["DATE"]));
            }

            case "ON":
            {
                return(CheckDate(maildb, message, item.Name, (DateTime)item.Parameters["DATE"]));
            }

            case "SMALLER":
            {
                if (item.SeqCompareUid == -1)
                {
                    int value = (int)item.Parameters["N"];
                    var msg   = maildb.Messages.GetBySeqNo(folder.Folder, folder.Stat.LastestMsgId, folder.Stat.Exists, value);
                    if (msg != null)
                    {
                        item.SeqCompareUid = msg.Id;
                    }
                    else
                    {
                        item.SeqCompareUid = 0;
                    }
                }
                return(message.Id < item.SeqCompareUid);
            }

            case "LARGER":
            {
                if (item.SeqCompareUid == -1)
                {
                    int value = (int)item.Parameters["N"];
                    var msg   = maildb.Messages.GetBySeqNo(folder.Folder, folder.Stat.LastestMsgId, folder.Stat.Exists, value);

                    if (msg != null)
                    {
                        item.SeqCompareUid = msg.Id;
                    }
                    else
                    {
                        item.SeqCompareUid = 0;
                    }
                }
                return(message.Id > item.SeqCompareUid);
            }

            case "NOT":
            {
                if (item.NOT != null)
                {
                    return(!Check(maildb, folder, item.NOT, message));
                }
                else
                {
                    return(true);
                }
            }

            case "OR":
            {
                if (item.OROne == null && item.ORTwo == null)
                {
                    return(false);
                }

                bool match = false;
                if (item.OROne != null)
                {
                    if (Check(maildb, folder, item.OROne, message))
                    {
                        match = true;
                    }
                }

                if (item.ORTwo != null)
                {
                    if (Check(maildb, folder, item.ORTwo, message))
                    {
                        match = true;
                    }
                }
                return(match);
            }

            case "KEYWORD":
            {
                var value = item.Parameters["FLAG"].ToString();

                foreach (var flag in maildb.Messages.GetFlags(message.Id))
                {
                    if (flag.ToUpper() == value)
                    {
                        return(true);
                    }
                }
                return(false);
            }

            default:
                break;
            }

            return(true);
        }