summaryrefslogtreecommitdiffstats
path: root/mailnews/base/src/nsMailNewsCommandLineHandler.js
blob: 3a85ec33642c6d5fe3edb224a00dc2cc66d1aadc (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
/* 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 Cc = Components.classes;
var Ci = Components.interfaces;
var Cr = Components.results;

Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
Components.utils.import("resource://gre/modules/AppConstants.jsm");

var MAPI_STARTUP_ARG = "MapiStartup";
var MESSAGE_ID_PARAM = "?messageid=";

var CMDLINEHANDLER_CID = Components.ID("{2f86d554-f9d9-4e76-8eb7-243f047333ee}");
var CMDLINEHANDLER_CONTRACTID = "@mozilla.org/commandlinehandler/general-startup;1?type=mail";

var nsMailNewsCommandLineHandler =
{
  get _messenger() {
    delete this._messenger;
    return this._messenger = Cc["@mozilla.org/messenger;1"]
                               .createInstance(Ci.nsIMessenger);
  },

  /* nsICommandLineHandler */

  /**
   * Handles the following command line arguments:
   * - -mail: opens the mail folder view
   * - -MapiStartup: indicates that this startup is due to MAPI.
   *   Don't do anything for now.
   */
  handle: function nsMailNewsCommandLineHandler_handle(aCommandLine) {
    // Do this here because xpcshell isn't too happy with this at startup
    Components.utils.import("resource:///modules/MailUtils.js");
    // -mail <URL>
    let mailURL = null;
    try {
      mailURL = aCommandLine.handleFlagWithParam("mail", false);
    }
    catch (e) {
      // We're going to cover -mail without a parameter later
    }

    if (mailURL && mailURL.length > 0) {
      let msgHdr = null;
      if (/^(mailbox|imap|news)-message:\/\//.test(mailURL)) {
        // This might be a standard message URI, or one with a messageID
        // parameter. Handle both cases.
        let messageIDIndex = mailURL.toLowerCase().indexOf(MESSAGE_ID_PARAM);
        if (messageIDIndex != -1) {
          // messageID parameter
          // Convert the message URI into a folder URI
          let folderURI = mailURL.slice(0, messageIDIndex)
                                 .replace("-message", "");
          // Get the message ID
          let messageID = mailURL.slice(messageIDIndex + MESSAGE_ID_PARAM.length);
          // Make sure the folder tree is initialized
          MailUtils.discoverFolders();

          let folder = MailUtils.getFolderForURI(folderURI, true);
          // The folder might not exist, so guard against that
          if (folder && messageID.length > 0)
            msgHdr = folder.msgDatabase.getMsgHdrForMessageID(messageID);
        }
        else {
          // message URI
          msgHdr = this._messenger.msgHdrFromURI(mailURL);
        }
      }
      else {
        // Necko URL, so convert it into a message header
        let neckoURL = null;
        try {
          neckoURL = Services.io.newURI(mailURL, null, null);
        }
        catch (e) {
          // We failed to convert the URI. Oh well.
        }

        if (neckoURL instanceof Ci.nsIMsgMessageUrl)
          msgHdr = neckoURL.messageHeader;
      }

      if (msgHdr) {
        aCommandLine.preventDefault = true;
        MailUtils.displayMessage(msgHdr);
      }
      else if (AppConstants.MOZ_APP_NAME == "seamonkey" &&
               /\.(eml|msg)$/i.test(mailURL)) {
        try {
          let file = aCommandLine.resolveFile(mailURL);
          // No point in trying to open a file if it doesn't exist or is empty
          if (file.exists() && file.fileSize > 0) {
            // Get the URL for this file
            let fileURL = Services.io.newFileURI(file)
                                  .QueryInterface(Ci.nsIFileURL);
            fileURL.query = "?type=application/x-message-display";
            // Open this file in a new message window.
            Services.ww.openWindow(null,
                                   "chrome://messenger/content/messageWindow.xul",
                                   "_blank", "all,chrome,dialog=no,status,toolbar",
                                   fileURL);
            aCommandLine.preventDefault = true;
          }
        }
        catch (e) {
        }
      }
      else {
        dump("Unrecognized URL: " + mailURL + "\n");
        Services.console.logStringMessage("Unrecognized URL: " + mailURL);
      }
    }

    // -mail (no parameter)
    let mailFlag = aCommandLine.handleFlag("mail", false);
    if (mailFlag) {
      // Focus the 3pane window if one is present, else open one
      let mail3PaneWindow = Services.wm.getMostRecentWindow("mail:3pane");
      if (mail3PaneWindow) {
        mail3PaneWindow.focus();
      }
      else {
        Services.ww.openWindow(null, "chrome://messenger/content/", "_blank",
            "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar,dialog=no",
            null);
      }
      aCommandLine.preventDefault = true;
    }

    // -MapiStartup
    aCommandLine.handleFlag(MAPI_STARTUP_ARG, false);
  },

  helpInfo: "  -mail              Open the mail folder view.\n" +
            "  -mail <URL>        Open the message specified by this URL.\n",

  classInfo: XPCOMUtils.generateCI({classID: CMDLINEHANDLER_CID,
                                    contractID: CMDLINEHANDLER_CONTRACTID,
                                    interfaces: [Ci.nsICommandLineHandler],
                                    flags: Ci.nsIClassInfo.SINGLETON}),

  /* nsIFactory */
  createInstance: function(outer, iid) {
    if (outer != null)
      throw Cr.NS_ERROR_NO_AGGREGATION;

    return this.QueryInterface(iid);
  },

  QueryInterface: XPCOMUtils.generateQI([Ci.nsICommandLineHandler,
                                         Ci.nsIFactory])
};

function mailNewsCommandLineHandlerModule() {}
mailNewsCommandLineHandlerModule.prototype =
{
  // XPCOM registration
  classID: CMDLINEHANDLER_CID,

  QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIModule]),

  _xpcom_factory: nsMailNewsCommandLineHandler
};

var components = [mailNewsCommandLineHandlerModule];
var NSGetFactory = XPCOMUtils.generateNSGetFactory(components);