summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/extensions/bayesian-spam-filter/test/unit/resources/trainingfile.js
blob: b6d37e879b15dc76037791ffe71ed06ad1c60d1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// service class to manipulate the junk training.dat file
//  code is adapted from Mnehy Thunderbird Extension

/* exported TrainingData */
function TrainingData() {
  // local constants

  const CC = Components.Constructor;

  // public methods

  this.read = read;

  // public variables

  this.mGoodTokens = 0;
  this.mJunkTokens = 0;
  this.mGoodMessages = 0;
  this.mJunkMessages = 0;
  this.mGoodCounts = {};
  this.mJunkCounts = {};

  // helper functions

  function getJunkStatFile() {
    var sBaseDir = Services.dirsvc.get("ProfD", Ci.nsIFile);
    var CFileByFile = new CC(
      "@mozilla.org/file/local;1",
      "nsIFile",
      "initWithFile"
    );
    var oFile = new CFileByFile(sBaseDir);
    oFile.append("training.dat");
    return oFile;
  }

  function getBinStream(oFile) {
    if (oFile && oFile.exists()) {
      var oUri = Services.io.newFileURI(oFile);
      // open stream (channel)
      let channel = Services.io.newChannelFromURI(
        oUri,
        null,
        Services.scriptSecurityManager.getSystemPrincipal(),
        null,
        Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
        Ci.nsIContentPolicy.TYPE_OTHER
      );
      var oStream = channel.open();
      // buffer it
      var oBufStream = Cc[
        "@mozilla.org/network/buffered-input-stream;1"
      ].createInstance(Ci.nsIBufferedInputStream);
      oBufStream.init(oStream, oFile.fileSize);
      // read as binary
      var oBinStream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(
        Ci.nsIBinaryInputStream
      );
      oBinStream.setInputStream(oBufStream);
      // return it
      return oBinStream;
    }
    return null;
  }

  // method specifications

  function read() {
    var file = getJunkStatFile();

    // does the file exist?
    Assert.ok(file.exists());

    var fileStream = getBinStream(file);

    // check magic number
    var iMagicNumber = fileStream.read32();
    Assert.equal(iMagicNumber, 0xfeedface);

    // get ham'n'spam numbers
    this.mGoodMessages = fileStream.read32();
    this.mJunkMessages = fileStream.read32();

    // Read good tokens
    this.mGoodTokens = fileStream.read32();
    var iRefCount, iTokenLen, sToken;
    for (let i = 0; i < this.mGoodTokens; ++i) {
      iRefCount = fileStream.read32();
      iTokenLen = fileStream.read32();
      sToken = fileStream.readBytes(iTokenLen);
      this.mGoodCounts[sToken] = iRefCount;
    }

    // we have no further good tokens, so read junk tokens
    this.mJunkTokens = fileStream.read32();
    for (let i = 0; i < this.mJunkTokens; i++) {
      // read token data
      iRefCount = fileStream.read32();
      iTokenLen = fileStream.read32();
      sToken = fileStream.readBytes(iTokenLen);
      this.mJunkCounts[sToken] = iRefCount;
    }
  }
}