Ejemplo n.º 1
0
        /// <summary>
        /// Private method for manually searching the cache when in offline mode
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        private IMAPSearchResult OfflineSearch(IMAPSearchQuery query)
        {
            IMAPSearchResult result = new IMAPSearchResult();
            result.Query = query;

            foreach (IMAPMessage msg in _messages)
            {
                if (msg == null)
                    continue;

                #region To
                if (query.To.Count > 0)
                {
                    bool containsAll = true;
                    foreach (IMAPMailAddress a in query.To)
                    {
                        if (!AddressListContains(msg.To, a))
                        {
                            containsAll = false;
                            break;
                        }


                    }

                    if (containsAll)
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion

                #region From
                if (query.From.Count > 0)
                {
                    bool containsAll = true;
                    foreach (IMAPMailAddress a in query.From)
                    {
                        if (!AddressListContains(msg.From, a))
                        {
                            containsAll = false;
                            break;
                        }


                    }

                    if (containsAll)
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion

                #region CC
                if (query.CC.Count > 0)
                {
                    bool containsAll = true;
                    foreach (IMAPMailAddress a in query.CC)
                    {
                        if (!AddressListContains(msg.Cc, a))
                        {
                            containsAll = false;
                            break;
                        }


                    }

                    if (containsAll)
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion

                #region BCC
                if (query.BCC.Count > 0)
                {
                    bool containsAll = true;
                    foreach (IMAPMailAddress a in query.BCC)
                    {
                        if (!AddressListContains(msg.Bcc, a))
                        {
                            containsAll = false;
                            break;
                        }


                    }

                    if (containsAll)
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion

                #region Subject
                if (query.Subject != String.Empty)
                {
                    if (msg.Subject.Contains(query.Subject))
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion

                #region Content
                if (query.Content != String.Empty)
                {
                    if (msg.TextData.TextData.Contains(query.Content))
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion

                #region Answered
                if (query.Answered)
                {
                    if (msg.Flags.Answered)
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion

                #region Deleted
                if (query.Deleted)
                {
                    if (msg.Flags.Deleted)
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion

                #region Draft
                if (query.Draft)
                {
                    if (msg.Flags.Draft)
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion

                #region New
                if (query.New)
                {
                    if (msg.Flags.New)
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion

                #region Recent
                if (query.Recent)
                {
                    if (msg.Flags.Recent)
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion

                #region AfterDate
                if (query.AfterDate != String.Empty)
                {
                    DateTime afterDate = new DateTime();
                    if (DateTime.TryParse(query.AfterDate, out afterDate))
                    {
                        if (msg.Date >= afterDate)
                        {
                            if (!result.Messages.Contains(msg))
                                result.Messages.Add(msg);
                        }
                        else
                        {
                            if (result.Messages.Contains(msg))
                                result.Messages.Remove(msg);
                        }
                    }


                }
                #endregion

                #region BeforeDate
                if (query.BeforeDate != String.Empty)
                {
                    DateTime beforeDate = new DateTime();
                    if (DateTime.TryParse(query.BeforeDate, out beforeDate))
                    {
                        if (msg.Date < beforeDate)
                        {
                            if (!result.Messages.Contains(msg))
                                result.Messages.Add(msg);
                        }
                        else
                        {
                            if (result.Messages.Contains(msg))
                                result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region Date
                if (query.Date != String.Empty)
                {
                    DateTime date = new DateTime();
                    if (DateTime.TryParse(query.Date, out date))
                    {
                        if (msg.Date == date)
                        {
                            if (!result.Messages.Contains(msg))
                                result.Messages.Add(msg);
                        }
                        else
                        {
                            if (result.Messages.Contains(msg))
                                result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region Date Range
                if (query.Range != null)
                {
                    if (query.Range.DateWithinRange(msg.Date))
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion
            }

            return result;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Private method for manually searching the cache when in offline mode
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        private IMAPSearchResult OfflineSearch(IMAPSearchQuery query)
        {
            IMAPSearchResult result = new IMAPSearchResult();

            result.Query = query;

            foreach (IMAPMessage msg in _messages)
            {
                if (msg == null)
                {
                    continue;
                }

                #region To
                if (query.To.Count > 0)
                {
                    bool containsAll = true;
                    foreach (IMAPMailAddress a in query.To)
                    {
                        if (!AddressListContains(msg.To, a))
                        {
                            containsAll = false;
                            break;
                        }
                    }

                    if (containsAll)
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region From
                if (query.From.Count > 0)
                {
                    bool containsAll = true;
                    foreach (IMAPMailAddress a in query.From)
                    {
                        if (!AddressListContains(msg.From, a))
                        {
                            containsAll = false;
                            break;
                        }
                    }

                    if (containsAll)
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region CC
                if (query.CC.Count > 0)
                {
                    bool containsAll = true;
                    foreach (IMAPMailAddress a in query.CC)
                    {
                        if (!AddressListContains(msg.Cc, a))
                        {
                            containsAll = false;
                            break;
                        }
                    }

                    if (containsAll)
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region BCC
                if (query.BCC.Count > 0)
                {
                    bool containsAll = true;
                    foreach (IMAPMailAddress a in query.BCC)
                    {
                        if (!AddressListContains(msg.Bcc, a))
                        {
                            containsAll = false;
                            break;
                        }
                    }

                    if (containsAll)
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region Subject
                if (query.Subject != String.Empty)
                {
                    if (msg.Subject.Contains(query.Subject))
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region Content
                if (query.Content != String.Empty)
                {
                    if (msg.TextData.TextData.Contains(query.Content))
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region Answered
                if (query.Answered)
                {
                    if (msg.Flags.Answered)
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region Deleted
                if (query.Deleted)
                {
                    if (msg.Flags.Deleted)
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region Draft
                if (query.Draft)
                {
                    if (msg.Flags.Draft)
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region New
                if (query.New)
                {
                    if (msg.Flags.New)
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region Recent
                if (query.Recent)
                {
                    if (msg.Flags.Recent)
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region AfterDate
                if (query.AfterDate != String.Empty)
                {
                    DateTime afterDate = new DateTime();
                    if (DateTime.TryParse(query.AfterDate, out afterDate))
                    {
                        if (msg.Date >= afterDate)
                        {
                            if (!result.Messages.Contains(msg))
                            {
                                result.Messages.Add(msg);
                            }
                        }
                        else
                        {
                            if (result.Messages.Contains(msg))
                            {
                                result.Messages.Remove(msg);
                            }
                        }
                    }
                }
                #endregion

                #region BeforeDate
                if (query.BeforeDate != String.Empty)
                {
                    DateTime beforeDate = new DateTime();
                    if (DateTime.TryParse(query.BeforeDate, out beforeDate))
                    {
                        if (msg.Date < beforeDate)
                        {
                            if (!result.Messages.Contains(msg))
                            {
                                result.Messages.Add(msg);
                            }
                        }
                        else
                        {
                            if (result.Messages.Contains(msg))
                            {
                                result.Messages.Remove(msg);
                            }
                        }
                    }
                }
                #endregion

                #region Date
                if (query.Date != String.Empty)
                {
                    DateTime date = new DateTime();
                    if (DateTime.TryParse(query.Date, out date))
                    {
                        if (msg.Date == date)
                        {
                            if (!result.Messages.Contains(msg))
                            {
                                result.Messages.Add(msg);
                            }
                        }
                        else
                        {
                            if (result.Messages.Contains(msg))
                            {
                                result.Messages.Remove(msg);
                            }
                        }
                    }
                }
                #endregion

                #region Date Range
                if (query.Range != null)
                {
                    if (query.Range.DateWithinRange(msg.Date))
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Searches this folder, and optionally the sub-folders for the specified query
        /// </summary>
        /// <param name="query">IMAPSearchQuery object containing the search options</param>
        /// <returns>A new IMAPSearchResult object containing the results of the query</returns>
        public IMAPSearchResult Search(IMAPSearchQuery query)
        {
            if (_client.OfflineMode)
                return OfflineSearch(query);
            
            IMAPSearchResult result = new IMAPSearchResult();
            result.Query = query;
            result.Folder = this;
            string cmd = "UID SEARCH {0}\r\n";
            StringBuilder q = new StringBuilder();
            ArrayList searchTerms = new ArrayList();
            // first we need to analyze the query and build our search command
            if (query.To.Count > 0)
            {
                foreach (IMAPMailAddress a in query.To)
                {
                    if (a.DisplayName != String.Empty)
                        searchTerms.Add(String.Format("TO \"{0}\"", a.DisplayName));

                    if (a.Address != String.Empty)
                        searchTerms.Add(String.Format("TO \"{0}\"", a.Address));

                }
            }

            if (query.From.Count > 0)
            {
                foreach (IMAPMailAddress a in query.From)
                {
                    if (a.DisplayName != String.Empty)
                        searchTerms.Add(String.Format("FROM \"{0}\"", a.DisplayName));

                    if (a.Address != String.Empty)
                        searchTerms.Add(String.Format("FROM \"{0}\"", a.Address));
                }
            }

            if (query.CC.Count > 0)
            {
                foreach (IMAPMailAddress a in query.CC)
                {
                    if (a.DisplayName != String.Empty)
                        searchTerms.Add(String.Format("CC \"{0}\"", a.DisplayName));

                    if (a.Address != String.Empty)
                        searchTerms.Add(String.Format("CC \"{0}\"", a.Address));
                }
            }

            if (query.BCC.Count > 0)
            {
                foreach (IMAPMailAddress a in query.BCC)
                {
                    if (a.DisplayName != String.Empty)
                        searchTerms.Add(String.Format("BCC \"{0}\"", a.DisplayName));

                    if (a.Address != String.Empty)
                        searchTerms.Add(String.Format("BCC \"{0}\"", a.Address));
                }
            }

            if (query.Subject != String.Empty)
            {
                searchTerms.Add(String.Format("SUBJECT \"{0}\"", query.Subject));
            }

            if (query.Content != String.Empty)
            {
                searchTerms.Add(String.Format("BODY {0}", query.Content));
            }

            if (query.Date != String.Empty)
            {
                searchTerms.Add(String.Format("ON {0}", FormatDateToServer(query.Date)));
            }            

            if (query.BeforeDate != String.Empty)
            {
                searchTerms.Add(String.Format("SENTBEFORE {0}", FormatDateToServer(query.BeforeDate)));
            }

            if (query.AfterDate != String.Empty)
            {
                searchTerms.Add(String.Format("SENTSINCE {0}", FormatDateToServer(query.AfterDate)));
            }

            if (query.Range != null)
            {                
                searchTerms.Add(String.Format("SENTBEFORE {0}", FormatDateToServer(query.Range.EndDate)));
                searchTerms.Add(String.Format("SENTSINCE {0}", FormatDateToServer(query.Range.StartDate)));
                searchTerms.Add(String.Format("SENTON {0}", FormatDateToServer(query.Range.StartDate)));
                searchTerms.Add(String.Format("SENTON {0}", FormatDateToServer(query.Range.EndDate)));
            }

            if (query.Answered)
                searchTerms.Add("ANSWERED");

            if (query.Deleted)
                searchTerms.Add("DELETED");

            if (query.Draft)
                searchTerms.Add("DRAFT");

            if (query.New)
                searchTerms.Add("NEW");

            if (query.Recent)
                searchTerms.Add("RECENT");

            if (query.LargerThan > -1)
                searchTerms.Add(String.Format("LARGER {0}", query.LargerThan));

            if (query.SmallerThan > -1)
                searchTerms.Add(String.Format("SMALLER {0}", query.SmallerThan));

            foreach (string s in searchTerms)
            {
                q.Append(s);
                q.Append(" ");
            }

            if (!this.IsCurrentlyExamined || !this.IsCurrentlySelected)
                this.Examine();

            ArrayList cmdResult = new ArrayList();
            cmd = String.Format(cmd, q.ToString().Trim());
            if (searchTerms.Count == 0)
                throw new ArgumentNullException("No search terms were found");

            _client._imap.SearchMessage(new string[] { q.ToString().Trim() }, true, cmdResult);

            foreach (string id in cmdResult)
            {
                if (id == String.Empty)
                    continue;

                foreach (IMAPMessage msg in _messages)
                {
                    if (msg.Uid == Convert.ToInt32(id))
                    {
                        result.Messages.Add(msg);
                    }
                }
            }

            

            return result;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Searches this folder, and optionally the sub-folders for the specified query
        /// </summary>
        /// <param name="query">IMAPSearchQuery object containing the search options</param>
        /// <returns>A new IMAPSearchResult object containing the results of the query</returns>
        public IMAPSearchResult Search(IMAPSearchQuery query)
        {
            if (_client.OfflineMode)
            {
                return(OfflineSearch(query));
            }

            IMAPSearchResult result = new IMAPSearchResult();

            result.Query  = query;
            result.Folder = this;
            string        cmd         = "UID SEARCH {0}\r\n";
            StringBuilder q           = new StringBuilder();
            ArrayList     searchTerms = new ArrayList();

            // first we need to analyze the query and build our search command
            if (query.To.Count > 0)
            {
                foreach (IMAPMailAddress a in query.To)
                {
                    if (a.DisplayName != String.Empty)
                    {
                        searchTerms.Add(String.Format("TO \"{0}\"", a.DisplayName));
                    }

                    if (a.Address != String.Empty)
                    {
                        searchTerms.Add(String.Format("TO \"{0}\"", a.Address));
                    }
                }
            }

            if (query.From.Count > 0)
            {
                foreach (IMAPMailAddress a in query.From)
                {
                    if (a.DisplayName != String.Empty)
                    {
                        searchTerms.Add(String.Format("FROM \"{0}\"", a.DisplayName));
                    }

                    if (a.Address != String.Empty)
                    {
                        searchTerms.Add(String.Format("FROM \"{0}\"", a.Address));
                    }
                }
            }

            if (query.CC.Count > 0)
            {
                foreach (IMAPMailAddress a in query.CC)
                {
                    if (a.DisplayName != String.Empty)
                    {
                        searchTerms.Add(String.Format("CC \"{0}\"", a.DisplayName));
                    }

                    if (a.Address != String.Empty)
                    {
                        searchTerms.Add(String.Format("CC \"{0}\"", a.Address));
                    }
                }
            }

            if (query.BCC.Count > 0)
            {
                foreach (IMAPMailAddress a in query.BCC)
                {
                    if (a.DisplayName != String.Empty)
                    {
                        searchTerms.Add(String.Format("BCC \"{0}\"", a.DisplayName));
                    }

                    if (a.Address != String.Empty)
                    {
                        searchTerms.Add(String.Format("BCC \"{0}\"", a.Address));
                    }
                }
            }

            if (query.Subject != String.Empty)
            {
                searchTerms.Add(String.Format("SUBJECT \"{0}\"", query.Subject));
            }

            if (query.Content != String.Empty)
            {
                searchTerms.Add(String.Format("BODY {0}", query.Content));
            }

            if (query.Date != String.Empty)
            {
                searchTerms.Add(String.Format("ON {0}", FormatDateToServer(query.Date)));
            }

            if (query.BeforeDate != String.Empty)
            {
                searchTerms.Add(String.Format("SENTBEFORE {0}", FormatDateToServer(query.BeforeDate)));
            }

            if (query.AfterDate != String.Empty)
            {
                searchTerms.Add(String.Format("SENTSINCE {0}", FormatDateToServer(query.AfterDate)));
            }

            if (query.Range != null)
            {
                searchTerms.Add(String.Format("SENTBEFORE {0}", FormatDateToServer(query.Range.EndDate)));
                searchTerms.Add(String.Format("SENTSINCE {0}", FormatDateToServer(query.Range.StartDate)));
                searchTerms.Add(String.Format("SENTON {0}", FormatDateToServer(query.Range.StartDate)));
                searchTerms.Add(String.Format("SENTON {0}", FormatDateToServer(query.Range.EndDate)));
            }

            if (query.Answered)
            {
                searchTerms.Add("ANSWERED");
            }

            if (query.Deleted)
            {
                searchTerms.Add("DELETED");
            }

            if (query.Draft)
            {
                searchTerms.Add("DRAFT");
            }

            if (query.New)
            {
                searchTerms.Add("NEW");
            }

            if (query.Recent)
            {
                searchTerms.Add("RECENT");
            }

            if (query.LargerThan > -1)
            {
                searchTerms.Add(String.Format("LARGER {0}", query.LargerThan));
            }

            if (query.SmallerThan > -1)
            {
                searchTerms.Add(String.Format("SMALLER {0}", query.SmallerThan));
            }

            foreach (string s in searchTerms)
            {
                q.Append(s);
                q.Append(" ");
            }

            if (!this.IsCurrentlyExamined || !this.IsCurrentlySelected)
            {
                this.Examine();
            }

            ArrayList cmdResult = new ArrayList();

            cmd = String.Format(cmd, q.ToString().Trim());
            if (searchTerms.Count == 0)
            {
                throw new ArgumentNullException("No search terms were found");
            }

            _client._imap.SearchMessage(new string[] { q.ToString().Trim() }, true, cmdResult);

            foreach (string id in cmdResult)
            {
                if (id == String.Empty)
                {
                    continue;
                }

                foreach (IMAPMessage msg in _messages)
                {
                    if (msg.Uid == Convert.ToInt32(id))
                    {
                        result.Messages.Add(msg);
                    }
                }
            }



            return(result);
        }