preprompt for master password if user has it protecting passwords and fetches new mail on startup, r=standard8, 437635
authorDavid Bienvenu <bienvenu@nventure.com>
Tue, 25 Nov 2008 08:39:45 -0800
changeset 1216 9da34e2932d1ea92f3c225a22180190f6dacb5ed
parent 1215 8355895c81bd95484ede15d59e546ef1ab95c3a9
child 1217 a1c7621565a6a1cb1ded52c3a937566bc5efa49a
push idunknown
push userunknown
push dateunknown
reviewersstandard8, 437635
bugs437635
preprompt for master password if user has it protecting passwords and fetches new mail on startup, r=standard8, 437635
mail/base/content/msgMail3PaneWindow.js
--- a/mail/base/content/msgMail3PaneWindow.js
+++ b/mail/base/content/msgMail3PaneWindow.js
@@ -864,16 +864,36 @@ function HandleAppCommandEvent(evt)
       break;
     case "Home":
     case "Reload":
     default:
       break;
   }
 }
 
+/**
+ * Returns true if the user has a master password set and false otherwise.
+ * Shamelessly stolen from FF - browser/components/preferences/security.js
+ */
+function masterPasswordSet()
+{
+  const Ci = Components.interfaces;
+  let slot = Components.classes["@mozilla.org/security/pkcs11moduledb;1"]
+                   .getService(Ci.nsIPKCS11ModuleDB).findSlotByName("");
+  if (slot) {
+    let status = slot.status;
+    return status != Ci.nsIPKCS11Slot.SLOT_UNINITIALIZED
+                  && status != Ci.nsIPKCS11Slot.SLOT_READY;
+  } 
+  else {
+    // XXX I have no bloody idea what this means
+    return false;
+  }
+}
+
 function OnUnloadMessenger()
 {
   OnLeavingFolder(gMsgFolderSelected);  // mark all read in current folder
   accountManager.removeIncomingServerListener(gThreePaneIncomingServerListener);
   gPrefBranch.QueryInterface(Components.interfaces.nsIPrefBranch2);
   gPrefBranch.removeObserver("mail.pane_config.dynamic", MailPrefObserver);
   document.getElementById('tabmail').closeTabs();
 
@@ -930,31 +950,54 @@ function loadStartFolder(initialUri)
                 startFolder = inboxFolder;
             }
         }
 
         // it is possible we were given an initial uri and we need to subscribe or try to add
         // the folder. i.e. the user just clicked on a news folder they aren't subscribed to from a browser
         // the news url comes in here.
 
+        if (isLoginAtStartUpEnabled && masterPasswordSet()) {
+          let passwordMgr = Components.classes["@mozilla.org/passwordmanager;1"]
+                              .getService(Components.interfaces.nsIPasswordManagerInternal);
+          if (passwordMgr)
+          {
+            let hostFound = new Object;
+            let userNameFound = new Object;
+            let passwordFound = new Object;
+
+            // Get password entry corresponding to the default server.
+            // This will block the UI until the use enters something.
+            // This throws an exception if the user cancels. For now, I'm
+            // just letting everything proceed as before, but conceivably,
+            // we could loop waiting for the user to enter the right password,
+            // and exit the app if they don't. But this assumes the
+            // default server password is stored.
+            try {
+                passwordMgr.findPasswordEntry(defaultServer.serverURI, "", "",
+                                           hostFound, userNameFound, 
+                                           passwordFound);
+            } catch(ex) {};
+          }
+        }
         // Perform biff on the server to check for new mail, except for imap
         // or a pop3 account that is deferred or deferred to,
         // or the case where initialUri is non-null (non-startup)
         if (!initialUri && isLoginAtStartUpEnabled && gLoadStartFolder
             && !defaultServer.isDeferredTo &&
             defaultServer.rootFolder == defaultServer.rootMsgFolder)
           defaultServer.performBiff(msgWindow);
         try {
           gFolderTreeView.selectFolder(startFolder);
         } catch(ex) {
           // This means we tried to select a folder that isn't in the current
           // view. Just select the first one in the view then.
           if (gFolderTreeView._rowMap.length)
             gFolderTreeView.selectFolder(gFolderTreeView._rowMap[0]._folder);
-         }
+        }
     }
     catch(ex)
     {
       // this is the case where we're trying to auto-subscribe to a folder.
       if (initialUri && !startFolder.parent)
       {
         // hack to force display of thread pane.
         ShowingThreadPane();