internal ThunderbirdEmailMessage(ThunderbirdFolder folder,
                                  string mailId,
                                  bool read,
                                  bool starred,
                                  long initialMessagePosition,
                                  long finalMessagePosition,
                                  Encoding encoding,
                                  int carriageReturnSize) {
   this.folder = folder;
   this.mailId = mailId;
   this.read = read;
   this.starred = starred;
   this.initialMessagePosition = initialMessagePosition;
   this.finalMessagePosition = finalMessagePosition;
   this.messageSize = 
       (uint)(this.finalMessagePosition - this.initialMessagePosition);
   this.encoding = encoding;
   this.carriageReturnSize = carriageReturnSize;
 }
    public ThunderbirdStore(string storePath,
                            ThunderbirdClient client) {
      this.storePath = Path.GetDirectoryName(storePath);
      this.persistName = Path.GetFileName(this.storePath);
      this.displayName = this.persistName;
      this.profile = null;
      this.client = client;
      this.contacts = new ArrayList();
      this.folders = new ArrayList();

      string folderName = Path.GetFileName(storePath);
      FolderKind folderKind = this.GetFolderKind(folderName);
      ThunderbirdFolder folder = new ThunderbirdFolder(
          folderKind,
          folderName,
          storePath,
          null,
          this);
      this.folders.Add(folder);
    }
    // For every folder check whether a correspoding .sbd is present. If it
    // is present just populate it and add it to subfolder's list.
    private void PopulateFolder() {
      string subFoldersPath = Path.GetFullPath(
          this.folderPath + ThunderbirdConstants.ThunderbirdDir);
      
      bool doesSubFolderExist = Directory.Exists(subFoldersPath);
      if (!doesSubFolderExist) {
        return;
      }

      foreach (string filePath in
          Directory.GetFiles(subFoldersPath,
                             ThunderbirdConstants.StarDotMSF)) {
        string fileName = Path.GetFileName(filePath);

        int msfIndex =
            fileName.LastIndexOf(ThunderbirdConstants.ThunderbirdMSFExtension);
        int fileLength = fileName.Length;

        // Check to see if there is an msf file present. If it is, check to see
        // if the corresponding msf file is present. This is introduced  because
        // there were cases in which msf files were present but mbox files were
        // not present which was causing scav to crash.
        if (msfIndex != -1 &&
            fileLength != ThunderbirdConstants.ThunderbirdMSFLen &&
            (msfIndex + ThunderbirdConstants.ThunderbirdMSFLen == fileLength)) {
          // Strip out the msf extension and check whether the file exists.
          string tempFileName = fileName.Substring(0, msfIndex);
          bool doesFileExist = false;
          doesFileExist = File.Exists(Path.Combine(subFoldersPath,
                                                   tempFileName));
          if (!doesFileExist) {
            continue;
          }

          FolderKind folderKind = this.GetFolderKind(tempFileName);
          ThunderbirdFolder folder = new ThunderbirdFolder(
              folderKind,
              tempFileName,
              Path.Combine(subFoldersPath, tempFileName),
              this,
              this.store);
      
          this.subFolders.Add(folder);
        }
      }
    }
    // Checks whether the folder is a subfolder of the folder.
    private bool IsFolderPresentInFolder(ThunderbirdFolder folder,
                                        string filePath) {
      if (folder.FolderPath.Equals(filePath)) {
        return true;
      }

      foreach (ThunderbirdFolder childFolder in folder.SubFolders) {
        if (IsFolderPresentInFolder(childFolder, filePath)) {
          return true;
        }
      }
      return false;
    }
    internal ThunderbirdEmailEnumerator(ThunderbirdFolder folder) {
      this.folder = folder;
      this.isRead = false;
      this.isStarred = false;
      this.hasFileReadError = false;

      try {
        this.currentPositionInFile = 0;
        this.initialMessagePosition = 0;
        this.finalMessagePosition = 0;
        this.initialFileSeekPosition = 0;
        this.carriageReturnSize = 0;

        this.fileStream = new FileStream(this.folder.FolderPath,
                                         FileMode.Open,
                                         FileAccess.Read,
                                         FileShare.Read);
        if (this.fileStream.CanSeek) {
          // Get the byte order mark, if there is one.
          byte[] bom = new byte[4];
          this.fileStream.Read(bom, 0, 4);
          if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf) {
            this.initialFileSeekPosition = 3;
            this.fileStream.Seek(3, SeekOrigin.Begin);
            this.encoding = Encoding.UTF8;
          } else if ((bom[0] == 0xff && bom[1] == 0xfe)) {
            this.initialFileSeekPosition = 2;
            this.fileStream.Seek(2, SeekOrigin.Begin);
            this.encoding = Encoding.Unicode;
          } else if (bom[0] == 0xfe && bom[1] == 0xff) {
            this.initialFileSeekPosition = 2;
            this.fileStream.Seek(2, SeekOrigin.Begin);
            this.encoding = Encoding.BigEndianUnicode;
          } else if (bom[0] == 0 &&
                     bom[1] == 0 &&
                     bom[2] == 0xfe &&
                     bom[3] == 0xff) {
            // Encoding.UTF32 is not supported in VS2003. We will be returning
            // has fileReadErrors = true in this case. If you are using VS2005,
            // please comment the line "this.hasFileReadError = true;" and
            // uncomment both the lines following it.
            this.hasFileReadError = true;

            // this.initialFileSeekPosition = 4;
            // this.encoding = Encoding.UTF32;
          } else if ((bom[0] == 0x2b && bom[1] == 0x2f && bom[2] == 76) &&
                     (bom[3] == 38 ||
                      bom[3] == 39 ||
                      bom[3] == 0x2b ||
                      bom[3] == 0x2f)) {
            this.initialFileSeekPosition = 4;
            this.encoding = Encoding.UTF7;
          } else {
            this.initialFileSeekPosition = 0;
            this.encoding = Encoding.ASCII;
            this.fileStream.Seek(0, SeekOrigin.Begin);
          }

          this.fileStream.Seek(this.initialFileSeekPosition, SeekOrigin.Begin);
          this.currentPositionInFile = this.initialFileSeekPosition;
          this.fileReader = new StreamReader(fileStream, this.encoding);
          this.carriageReturnSize =
              this.encoding.GetByteCount(ThunderbirdConstants.CarriageReturn);

          // Before proceeding with reading the file check if the file reader
          // exists.
          if (!this.hasFileReadError) {
            // Move the file_reader_ to the first "From - " if it exists.
            while (this.fileReader.Peek() != -1) {
              string line = fileReader.ReadLine();

              // We need to add the size of "\r\n" to position depending upon
              // the current encoding. The line read using ReadLine() does not
              // include the carriage return, thus we need to add its size also.
              this.currentPositionInFile +=
                  (encoding.GetByteCount(line) + this.carriageReturnSize);
              if (line.StartsWith(ThunderbirdConstants.MboxMailStart)) {
                break;
              }
            }
          }
        } else {
          this.hasFileReadError = true;
        }
      } catch (IOException) {
        // There might be 2 reasons for the program to come here.
        // 1. The file does not exist.
        // 2. The file is beign read by some other program.
        this.hasFileReadError = true;
      }
    }
 internal ThunderbirdEmailEnumerable(ThunderbirdFolder folder) {
   this.folder = folder;
 }