Beispiel #1
0
        public void Search(string accessingUser, string folderOwnerUser, string folder, IMAP_e_Search e)
        {
            ArgsValidator.ValidateUserName(folderOwnerUser);
            ArgsValidator.ValidateFolder(folder);
            ArgsValidator.ValidateNotNull(e);
            if (!this.UserExists(folderOwnerUser))
            {
                throw new Exception("User '" + folderOwnerUser + "' doesn't exist !");
            }
            folder = API_Utlis.NormalizeFolder(folder);
            string text = folder;
            xml_API.SharedFolderMapInfo sharedFolderMapInfo = this.MapSharedFolder(text);
            if (sharedFolderMapInfo.IsSharedFolder)
            {
                folderOwnerUser = sharedFolderMapInfo.FolderOnwer;
                folder = sharedFolderMapInfo.Folder;
                if (folderOwnerUser == "" || folder == "")
                {
                    throw new ArgumentException("Specified root folder '" + text + "' isn't accessible !");
                }
            }
            if (!this.FolderExists(folderOwnerUser + "/" + folder))
            {
                throw new Exception("Folder '" + folder + "' doesn't exist !");
            }
            if (accessingUser.ToLower() != "system")
            {
                IMAP_ACL_Flags userACL = this.GetUserACL(folderOwnerUser, folder, accessingUser);
                if ((userACL & IMAP_ACL_Flags.r) == IMAP_ACL_Flags.None)
                {
                    throw new InsufficientPermissionsException(string.Concat(new string[]
					{
						"Insufficient permissions for folder '",
						accessingUser,
						"/",
						folder,
						"' !"
					}));
                }
            }
            using (SQLiteConnection messagesInfoSqlCon = this.GetMessagesInfoSqlCon(folderOwnerUser, folder))
            {
                Dictionary<long, long> dictionary = new Dictionary<long, long>();
                using (SQLiteCommand sQLiteCommand = messagesInfoSqlCon.CreateCommand())
                {
                    sQLiteCommand.CommandText = "select UID from MessagesInfo ORDER by UID ASC;";
                    using (SQLiteDataAdapter sQLiteDataAdapter = new SQLiteDataAdapter(sQLiteCommand))
                    {
                        DataSet dataSet = new DataSet();
                        sQLiteDataAdapter.Fill(dataSet);
                        for (int i = 0; i < dataSet.Tables[0].Rows.Count; i++)
                        {
                            dictionary.Add((long)(i + 1), Convert.ToInt64(dataSet.Tables[0].Rows[i]["UID"]));
                        }
                    }
                    using (SQLiteCommand sQLiteCommand2 = messagesInfoSqlCon.CreateCommand())
                    {
                        sQLiteCommand2.CommandText = "select UID from MessagesInfo where " + this.SearchCriteriaToSql(e.Criteria, dictionary) + ";";
                        DataSet dataSet2 = new DataSet("dsMessagesInfo");
                        using (SQLiteDataAdapter sQLiteDataAdapter2 = new SQLiteDataAdapter(sQLiteCommand2))
                        {
                            sQLiteDataAdapter2.Fill(dataSet2);
                        }
                        foreach (DataRow dataRow in dataSet2.Tables[0].Rows)
                        {
                            e.AddMessage(Convert.ToInt64(dataRow["UID"]));
                        }
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Searhes specified folder messages which match to search criteria.
        /// </summary>
        /// <param name="accessingUser">User who accesses this method. 
        /// User needs r permission to call this method or Exception is thrown. 
        /// There is special user 'system' for which permission check is skipped.</param>
        /// <param name="folderOwnerUser">User who's folder it is.</param>
        /// <param name="folder">Folder what messages info to get. For example: Inbox,Public Folders/Documnets .</param>
        /// <param name="e">IMAP search event data.</param>
        public void Search(string accessingUser,string folderOwnerUser,string folder,IMAP_e_Search e)
        {
            /* Implementation notes:
                *) Validate values. Throw ArgumnetExcetion if invalid values.
                *) Ensure that user exists.
                *) Normalize folder. Remove '/' from folder start and end, ... .
                *) Do Shared Folders mapping.
                *) Ensure that folder exists. Throw Exception if don't.
                *) See if user has sufficient permissions. User requires 'r' permission.
                    There is builtin user system, skip ACL for it.
                *) Search messages.
            */

            //--- Validate values -------------------//
            ArgsValidator.ValidateUserName(folderOwnerUser);
            ArgsValidator.ValidateFolder(folder);
            ArgsValidator.ValidateNotNull(e);
            //---------------------------------------//
          			
            // Ensure that user exists.
            if(!UserExists(folderOwnerUser)){
                throw new Exception("User '" + folderOwnerUser + "' doesn't exist !");
            }

            // Normalize folder. Remove '/' from folder start and end.
            folder = API_Utlis.NormalizeFolder(folder);

            // Do Shared Folders mapping.
            string originalFolder = folder;
            SharedFolderMapInfo mappedFolder = MapSharedFolder(originalFolder);
            if(mappedFolder.IsSharedFolder){
                folderOwnerUser = mappedFolder.FolderOnwer;
                folder = mappedFolder.Folder;

                if(folderOwnerUser == "" || folder == ""){
                    throw new ArgumentException("Specified root folder '" + originalFolder + "' isn't accessible !");
                }
            }

            // Ensure that folder exists. Throw Exception if don't.
            if(!FolderExists(folderOwnerUser + "/" + folder)){
                throw new Exception("Folder '" + folder + "' doesn't exist !");
            }

            // See if user has sufficient permissions. User requires 'r' permission.
            //  There is builtin user system, skip ACL for it.
            if(accessingUser.ToLower() != "system"){
                IMAP_ACL_Flags acl = GetUserACL(folderOwnerUser,folder,accessingUser);
                if((acl & IMAP_ACL_Flags.r) == 0){
                    throw new InsufficientPermissionsException("Insufficient permissions for folder '" + accessingUser + "/" + folder + "' !");
                }
            }

            using(SQLiteConnection sqlCon = GetMessagesInfoSqlCon(folderOwnerUser,folder)){
                // -------- Create sequence-number to UI map table. -------------------------
                Dictionary<long,long> seqNo_to_UID = new Dictionary<long,long>();
                using(SQLiteCommand cmd = sqlCon.CreateCommand()){
                    cmd.CommandText = "select UID from MessagesInfo ORDER by UID ASC;";
                                
                    using(SQLiteDataAdapter ad = new SQLiteDataAdapter(cmd)){
                        DataSet dsUids = new DataSet();
                        ad.Fill(dsUids); 

                        for(int i=0;i<dsUids.Tables[0].Rows.Count;i++){
                            seqNo_to_UID.Add(i + 1,Convert.ToInt64(dsUids.Tables[0].Rows[i]["UID"]));
                        }
                    }                
                    //----------------------------------------------------------------------------

                    using(SQLiteCommand cmd2 = sqlCon.CreateCommand()){
                        cmd2.CommandText = "select UID from MessagesInfo where " + SearchCriteriaToSql(e.Criteria,seqNo_to_UID) + ";";

                        DataSet ds = new DataSet("dsMessagesInfo");                
                        using(SQLiteDataAdapter ad = new SQLiteDataAdapter(cmd2)){
                            ad.Fill(ds); 
                        }
                                
                        foreach(DataRow dr in ds.Tables[0].Rows){
                            e.AddMessage(Convert.ToInt64(dr["UID"]));
                        }
                    }
                }
            }
        }