summaryrefslogtreecommitdiffstats
path: root/mailnews/base/util/mailnewsMigrator.js
blob: 2e3be690676018f9a9b0637fda46ff2dfe909c5f (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
/* 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/. */

/**
 * Migrate profile (prefs and other files) from older versions of Mailnews to
 * current.
 * This should be run at startup. It migrates as needed: each migration
 * function should be written to be a no-op when the value is already migrated
 * or was never used in the old version.
 */

this.EXPORTED_SYMBOLS = [ "migrateMailnews" ];

Components.utils.import("resource:///modules/errUtils.js");
Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource:///modules/mailServices.js");
var Ci = Components.interfaces;
var kServerPrefVersion = 1;
var kSmtpPrefVersion = 1;
var kABRemoteContentPrefVersion = 1;
var kDefaultCharsetsPrefVersion = 1;

function migrateMailnews()
{
  try {
    MigrateServerAuthPref();
  } catch (e) { logException(e); }

  try {
    MigrateABRemoteContentSettings();
  } catch (e) { logException(e); }

  try {
    MigrateDefaultCharsets();
  } catch (e) { logException(e); }
}

/**
 * Migrates from pref useSecAuth to pref authMethod
 */
function MigrateServerAuthPref()
{
  try {
    // comma-separated list of all accounts.
    var accounts = Services.prefs.getCharPref("mail.accountmanager.accounts")
        .split(",");
    for (let i = 0; i < accounts.length; i++)
    {
      let accountKey = accounts[i]; // e.g. "account1"
      if (!accountKey)
        continue;
      let serverKey = Services.prefs.getCharPref("mail.account." + accountKey +
         ".server");
      let server = "mail.server." + serverKey + ".";
      if (Services.prefs.prefHasUserValue(server + "authMethod"))
        continue;
      if (!Services.prefs.prefHasUserValue(server + "useSecAuth") &&
          !Services.prefs.prefHasUserValue(server + "auth_login"))
        continue;
      if (Services.prefs.prefHasUserValue(server + "migrated"))
        continue;
      // auth_login = false => old-style auth
      // else: useSecAuth = true => "secure auth"
      // else: cleartext pw
      let auth_login = true;
      let useSecAuth = false; // old default, default pref now removed
      try {
        auth_login = Services.prefs.getBoolPref(server + "auth_login");
      } catch (e) {}
      try {
        useSecAuth = Services.prefs.getBoolPref(server + "useSecAuth");
      } catch (e) {}

      Services.prefs.setIntPref(server + "authMethod",
          auth_login ? (useSecAuth ?
                           Ci.nsMsgAuthMethod.secure :
                           Ci.nsMsgAuthMethod.passwordCleartext) :
                       Ci.nsMsgAuthMethod.old);
      Services.prefs.setIntPref(server + "migrated", kServerPrefVersion);
    }

    // same again for SMTP servers
    var smtpservers = Services.prefs.getCharPref("mail.smtpservers").split(",");
    for (let i = 0; i < smtpservers.length; i++)
    {
      if (!smtpservers[i])
        continue;
      let server = "mail.smtpserver." + smtpservers[i] + ".";
      if (Services.prefs.prefHasUserValue(server + "authMethod"))
        continue;
      if (!Services.prefs.prefHasUserValue(server + "useSecAuth") &&
          !Services.prefs.prefHasUserValue(server + "auth_method"))
        continue;
      if (Services.prefs.prefHasUserValue(server + "migrated"))
        continue;
      // auth_method = 0 => no auth
      // else: useSecAuth = true => "secure auth"
      // else: cleartext pw
      let auth_method = 1;
      let useSecAuth = false;
      try {
        auth_method = Services.prefs.getIntPref(server + "auth_method");
      } catch (e) {}
      try {
        useSecAuth = Services.prefs.getBoolPref(server + "useSecAuth");
      } catch (e) {}

      Services.prefs.setIntPref(server + "authMethod",
          auth_method ? (useSecAuth ?
                            Ci.nsMsgAuthMethod.secure :
                            Ci.nsMsgAuthMethod.passwordCleartext) :
                        Ci.nsMsgAuthMethod.none);
      Services.prefs.setIntPref(server + "migrated", kSmtpPrefVersion);
    }
  } catch(e) { logException(e); }
}

/**
 * The address book used to contain information about wheather to allow remote
 * content for a given contact. Now we use the permission manager for that.
 * Do a one-time migration for it.
 */
function MigrateABRemoteContentSettings()
{
  if (Services.prefs.prefHasUserValue("mail.ab_remote_content.migrated"))
    return;

  // Search through all of our local address books looking for a match.
  let enumerator = MailServices.ab.directories;
  while (enumerator.hasMoreElements())
  {
    let migrateAddress = function(aEmail) {
      let uri = Services.io.newURI(
        "chrome://messenger/content/email=" + aEmail, null, null);
      Services.perms.add(uri, "image", Services.perms.ALLOW_ACTION);
    }

    let addrbook = enumerator.getNext()
      .QueryInterface(Components.interfaces.nsIAbDirectory);
    try {
      // If it's a read-only book, don't try to find a card as we we could never
      // have set the AllowRemoteContent property.
      if (addrbook.readOnly)
        continue;

      let childCards = addrbook.childCards;
      while (childCards.hasMoreElements())
      {
        let card = childCards.getNext()
                             .QueryInterface(Components.interfaces.nsIAbCard);

        if (card.getProperty("AllowRemoteContent", false) == false)
          continue; // not allowed for this contact

        if (card.primaryEmail)
          migrateAddress(card.primaryEmail);

        if (card.getProperty("SecondEmail", ""))
          migrateAddress(card.getProperty("SecondEmail", ""));
      }
    } catch (e) { logException(e); }
  }

  Services.prefs.setIntPref("mail.ab_remote_content.migrated",
                            kABRemoteContentPrefVersion);
}

/**
 * If the default sending or viewing charset is one that is no longer available,
 * change it back to the default.
 */
function MigrateDefaultCharsets()
{
  if (Services.prefs.prefHasUserValue("mail.default_charsets.migrated"))
    return;

  let charsetConvertManager = Components.classes['@mozilla.org/charset-converter-manager;1']
    .getService(Components.interfaces.nsICharsetConverterManager);

  let sendCharsetStr = Services.prefs.getComplexValue(
      "mailnews.send_default_charset",
      Components.interfaces.nsIPrefLocalizedString).data;

  try {
    charsetConvertManager.getCharsetTitle(sendCharsetStr);
  } catch (e) {
    Services.prefs.clearUserPref("mailnews.send_default_charset");
  }

  let viewCharsetStr = Services.prefs.getComplexValue(
      "mailnews.view_default_charset",
      Components.interfaces.nsIPrefLocalizedString).data;

  try {
    charsetConvertManager.getCharsetTitle(viewCharsetStr);
  } catch (e) {
    Services.prefs.clearUserPref("mailnews.view_default_charset");
  }

  Services.prefs.setIntPref("mail.default_charsets.migrated",
                            kDefaultCharsetsPrefVersion);
}