summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/src/mailstoreConverter.jsm
blob: 6e5be5ebe17590b7270706de3fa691b1647fb6c2 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* 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/. */

var EXPORTED_SYMBOLS = ["convertMailStoreTo", "terminateWorkers"];

const { FileUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/FileUtils.sys.mjs"
);

let log = console.createInstance({
  prefix: "mail.mailstoreconverter",
  maxLogLevel: "Warn",
  maxLogLevelPref: "mail.mailstoreconverter.loglevel",
});

let gConverterWorker = null;

/**
 * Sets a server to use a different type of mailstore, converting
 * all the existing data.
 *
 * @param {string} aMailstoreContractId - XPCOM id of new mailstore type.
 * @param {nsIMsgServer} aServer - server to migrate.
 * @param {?Element} aEventTarget - If set, element to send progress events.
 *
 * @returns {Promise<string>} - Resolves with a string containing the new root
 *   directory for the migrated server.
 *   Rejects with an error message.
 */
function convertMailStoreTo(aMailstoreContractId, aServer, aEventTarget) {
  let accountRootFolder = aServer.rootFolder.filePath;

  let srcType = null;
  let destType = null;
  if (aMailstoreContractId == "@mozilla.org/msgstore/maildirstore;1") {
    srcType = "maildir";
    destType = "mbox";
  } else {
    srcType = "mbox";
    destType = "maildir";
  }

  // Go offline before conversion, so there aren't messages coming in during
  // the process.
  Services.io.offline = true;
  let destDir = createTmpConverterFolder(
    accountRootFolder,
    aMailstoreContractId
  );

  // Return a promise that will complete once the worker is done.
  return new Promise(function (resolve, reject) {
    let worker = new ChromeWorker("resource:///modules/converterWorker.js");
    gConverterWorker = worker;

    // Helper to log error, clean up and reject with error message.
    let bailout = function (errmsg) {
      log.error("bailing out (" + errmsg + ")");
      // Cleanup.
      log.info("Trying to remove converter folder: " + destDir.path);
      destDir.remove(true);
      reject(errmsg);
    };

    // Handle exceptions thrown by the worker thread.
    worker.addEventListener("error", function (e) {
      // (e is type ErrorEvent)

      // if we're lucky, the error will contain location info
      if (e.filename && e.lineno) {
        bailout(e.filename + ":" + e.lineno + ": " + e.message);
      } else {
        bailout(e.message);
      }
    });

    // Handle updates from the worker thread.
    worker.addEventListener("message", function (e) {
      let response = e.data;
      // log.debug("WORKER SAYS: " + JSON.stringify(response) + "\n");
      if (response.msg == "progress") {
        let val = response.val;
        let total = response.total;

        // Send the percentage completion to the GUI.
        // XXX TODO: should probably check elapsed time, and throttle
        // the events to avoid spending all our time drawing!
        let ev = new Event("progress");
        ev.detail = parseInt((val / total) * 100);
        if (aEventTarget) {
          aEventTarget.dispatchEvent(ev);
        }
      }
      if (response.msg == "success") {
        // If we receive this, the worker has completed, without errors.
        let storeTypeIDs = {
          mbox: "@mozilla.org/msgstore/berkeleystore;1",
          maildir: "@mozilla.org/msgstore/maildirstore;1",
        };
        let newStoreTypeID = storeTypeIDs[destType];

        try {
          let finalRoot = installNewRoot(aServer, destDir, newStoreTypeID);
          log.info(
            "Conversion complete. Converted dir installed as: " + finalRoot
          );
          resolve(finalRoot);
        } catch (e) {
          bailout("installNewRoot() failed");
        }
      }
    });

    // Kick off the worker.
    worker.postMessage({
      srcType,
      destType,
      srcRoot: accountRootFolder.path,
      destRoot: destDir.path,
    });
  });
}

/**
 * Checks if Converter folder exists in tmp dir, removes it and creates a new
 * "Converter" folder.
 *
 * @param {nsIFile} aFolder - account root folder.
 * @param {string} aMailstoreContractId - XPCOM id of dest mailstore type
 *
 * @returns {nsIFile} - the new tmp directory to use as converter dest.
 */
function createTmpConverterFolder(aFolder, aMailstoreContractId) {
  let tmpDir = FileUtils.getDir("TmpD", [], false);
  let tmpFolder;
  switch (aMailstoreContractId) {
    case "@mozilla.org/msgstore/maildirstore;1": {
      if (aFolder.leafName.substr(-8) == "-maildir") {
        tmpFolder = new FileUtils.File(
          PathUtils.join(
            tmpDir.path,
            aFolder.leafName.substr(0, aFolder.leafName.length - 8) + "-mbox"
          )
        );
      } else {
        tmpFolder = new FileUtils.File(
          PathUtils.join(tmpDir.path, aFolder.leafName + "-mbox")
        );
      }

      if (tmpFolder.exists()) {
        log.info(
          "Temporary Converter folder " +
            tmpFolder.path +
            " exists in tmp dir. Removing it"
        );
        tmpFolder.remove(true);
      }
      return FileUtils.getDir("TmpD", [tmpFolder.leafName], true);
    }

    case "@mozilla.org/msgstore/berkeleystore;1": {
      if (aFolder.leafName.substr(-5) == "-mbox") {
        tmpFolder = new FileUtils.File(
          PathUtils.join(
            tmpDir.path,
            aFolder.leafName.substr(0, aFolder.leafName.length - 5) + "-maildir"
          )
        );
      } else {
        tmpFolder = new FileUtils.File(
          PathUtils.join(tmpDir.path, aFolder.leafName + "-maildir")
        );
      }

      if (tmpFolder.exists()) {
        log.info(
          "Temporary Converter folder " +
            tmpFolder.path +
            "exists in tmp dir. Removing it"
        );
        tmpFolder.remove(true);
      }
      return FileUtils.getDir("TmpD", [tmpFolder.leafName], true);
    }

    default: {
      throw new Error(
        "Unexpected mailstoreContractId: " + aMailstoreContractId
      );
    }
  }
}

/**
 * Switch server over to use the newly-converted directory tree.
 * Moves the converted directory into an appropriate place for the server.
 *
 * @param {nsIMsgServer} server - server to migrate.
 * @param {string} dir - dir of converted mailstore to install
 *                                  (will be moved by this function).
 * @param {string} newStoreTypeID - XPCOM id of new mailstore type.
 * @returns {string} new location of dir.
 */
function installNewRoot(server, dir, newStoreTypeID) {
  let accountRootFolder = server.rootFolder.filePath;

  // Migration is complete, get path of parent of account root
  // folder into "parentPath" check if Converter folder already
  // exists in "parentPath". If yes, remove it.
  let lastSlash = accountRootFolder.path.lastIndexOf("/");
  let parentPath = accountRootFolder.parent.path;
  log.info("Path to parent folder of account root folder: " + parentPath);

  let parent = new FileUtils.File(parentPath);
  log.info("Path to parent folder of account root folder: " + parent.path);

  let converterFolder = new FileUtils.File(
    PathUtils.join(parent.path, dir.leafName)
  );
  if (converterFolder.exists()) {
    log.info(
      "Converter folder exists in " +
        parentPath +
        ". Removing already existing folder"
    );
    converterFolder.remove(true);
  }

  // Move Converter folder into the parent of account root folder.
  try {
    dir.moveTo(parent, dir.leafName);
    // {nsIFile} new account root folder.
    log.info("Path to new account root folder: " + converterFolder.path);
  } catch (e) {
    // Cleanup.
    log.error(e);
    log.error("Trying to remove converter folder: " + converterFolder.path);
    converterFolder.remove(true);
    throw e;
  }

  // If the account is imap then copy the msf file for the original
  // root folder and rename the copy with the name of the new root
  // folder.
  if (server.type != "pop3" && server.type != "none") {
    let converterFolderMsf = new FileUtils.File(
      PathUtils.join(parent.path, dir.leafName + ".msf")
    );
    if (converterFolderMsf.exists()) {
      converterFolderMsf.remove(true);
    }

    let oldRootFolderMsf = new FileUtils.File(
      PathUtils.join(parent.path, accountRootFolder.leafName + ".msf")
    );
    if (oldRootFolderMsf.exists()) {
      oldRootFolderMsf.copyTo(parent, converterFolderMsf.leafName);
    }
  }

  if (server.type == "nntp") {
    let converterFolderNewsrc = new FileUtils.File(
      PathUtils.join(parent.path, "newsrc-" + dir.leafName)
    );
    if (converterFolderNewsrc.exists()) {
      converterFolderNewsrc.remove(true);
    }
    let oldNewsrc = new FileUtils.File(
      PathUtils.join(parent.path, "newsrc-" + accountRootFolder.leafName)
    );
    if (oldNewsrc.exists()) {
      oldNewsrc.copyTo(parent, converterFolderNewsrc.leafName);
    }
  }

  server.rootFolder.filePath = converterFolder;
  server.localPath = converterFolder;
  log.info("Path to account root folder: " + server.rootFolder.filePath.path);

  // Set various preferences.
  let p1 = "mail.server." + server.key + ".directory";
  let p2 = "mail.server." + server.key + ".directory-rel";
  let p3 = "mail.server." + server.key + ".newsrc.file";
  let p4 = "mail.server." + server.key + ".newsrc.file-rel";
  let p5 = "mail.server." + server.key + ".storeContractID";

  Services.prefs.setCharPref(p1, converterFolder.path);
  log.info(p1 + ": " + converterFolder.path);

  // The directory-rel pref is of the form "[ProfD]Mail/pop.gmail.com
  // " (pop accounts) or "[ProfD]ImapMail/imap.gmail.com" (imap
  // accounts) ie the last slash "/" is followed by the root folder
  // name. So, replace the old root folder name that follows the last
  // slash with the new root folder name to set the correct value of
  // directory-rel pref.
  let directoryRel = Services.prefs.getCharPref(p2);
  lastSlash = directoryRel.lastIndexOf("/");
  directoryRel =
    directoryRel.slice(0, lastSlash) + "/" + converterFolder.leafName;
  Services.prefs.setCharPref(p2, directoryRel);
  log.info(p2 + ": " + directoryRel);

  if (server.type == "nntp") {
    let newNewsrc = FileUtils.File(
      PathUtils.join(parent.path, "newsrc-" + converterFolder.leafName)
    );
    Services.prefs.setCharPref(p3, newNewsrc.path);

    // The newsrc.file-rel pref is of the form "[ProfD]News/newsrc-
    // news.mozilla.org" ie the last slash "/" is followed by the
    // newsrc file name. So, replace the old newsrc file name that
    // follows the last slash with the new newsrc file name to set
    // the correct value of newsrc.file-rel pref.
    let newsrcRel = Services.prefs.getCharPref(p4);
    lastSlash = newsrcRel.lastIndexOf("/");
    newsrcRel = newsrcRel.slice(0, lastSlash) + "/" + newNewsrc.leafName;
    Services.prefs.setCharPref(p4, newsrcRel);
    log.info(p4 + ": " + newsrcRel);
  }

  Services.prefs.setCharPref(p5, newStoreTypeID);

  Services.prefs.savePrefFile(null);

  return converterFolder.path;
}

/**
 * Terminate any workers involved in the conversion process.
 */
function terminateWorkers() {
  // We're only using a single worker right now.
  if (gConverterWorker !== null) {
    gConverterWorker.terminate();
    gConverterWorker = null;
  }
}