summaryrefslogtreecommitdiff
path: root/teledactyl/content/mail.js
blob: fa81dfa59e2a11166f2a84f0936d69ccc00042c4 (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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
// Copyright (c) 2006-2009 by Martin Stubenschrott <stubenschrott@vimperator.org>
//
// This work is licensed for reuse under an MIT license. Details are
// given in the LICENSE.txt file included with this file.
"use strict";

const Mail = Module("mail", {
    init: function () {
        services.add("smtpService", "@mozilla.org/messengercompose/smtp;1", Ci.nsISmtpService);

        // used for asynchronously selecting messages after wrapping folders
        this._selectMessageKeys = [];
        this._selectMessageCount = 1;
        this._selectMessageReverse = false;

        this._mailSession = Cc["@mozilla.org/messenger/services/session;1"].getService(Ci.nsIMsgMailSession);
        this._notifyFlags = Ci.nsIFolderListener.intPropertyChanged | Ci.nsIFolderListener.event;
        this._mailSession.AddFolderListener(this._folderListener, this._notifyFlags);
    },

    _folderListener: {
        OnItemAdded: function (parentItem, item) {},
        OnItemRemoved: function (parentItem, item) {},
        OnItemPropertyChanged: function (item, property, oldValue, newValue) {},
        OnItemIntPropertyChanged: function (item, property, oldValue, newValue) {},
        OnItemBoolPropertyChanged: function (item, property, oldValue, newValue) {},
        OnItemUnicharPropertyChanged: function (item, property, oldValue, newValue) {},
        OnItemPropertyFlagChanged: function (item, property, oldFlag, newFlag) {},

        OnItemEvent: function (folder, event) {
            let eventType = event.toString();
            if (eventType == "FolderLoaded") {
                if (folder) {
                    let msgFolder = folder.QueryInterface(Ci.nsIMsgFolder);
                    autocommands.trigger("FolderLoaded", { url: msgFolder });

                    // Jump to a message when requested
                    let indices = [];
                    if (mail._selectMessageKeys.length > 0) {
                        for (let j = 0; j < mail._selectMessageKeys.length; j++)
                            indices.push([gDBView.findIndexFromKey(mail._selectMessageKeys[j], true), mail._selectMessageKeys[j]]);

                        indices.sort();
                        let index = mail._selectMessageCount - 1;
                        if (mail._selectMessageReverse)
                            index = mail._selectMessageKeys.length - 1 - index;

                        gDBView.selectMsgByKey(indices[index][1]);
                        mail._selectMessageKeys = [];
                    }
                }
            }
            /*else if (eventType == "ImapHdrDownloaded") {}
            else if (eventType == "DeleteOrMoveMsgCompleted") {}
            else if (eventType == "DeleteOrMoveMsgFailed") {}
            else if (eventType == "AboutToCompact") {}
            else if (eventType == "CompactCompleted") {}
            else if (eventType == "RenameCompleted") {}
            else if (eventType == "JunkStatusChanged") {}*/
        }
    },

    _getCurrentFolderIndex: function () {
        // for some reason, the index is interpreted as a string, therefore the parseInt
        return parseInt(gFolderTreeView.getIndexOfFolder(gFolderTreeView.getSelectedFolders()[0]));
    },

    _getRSSUrl: function () {
        return gDBView.hdrForFirstSelectedMessage.messageId.replace(/(#.*)?@.*$/, "");
    },

    _moveOrCopy: function (copy, destinationFolder, operateOnThread) {
        let folders = mail.getFolders(destinationFolder);
        if (folders.length == 0)
            return void dactyl.echoerr("Exxx: No matching folder for " + destinationFolder);
        else if (folders.length > 1)
            return dactyl.echoerr("Exxx: More than one match for " + destinationFolder);

        let count = gDBView.selection.count;
        if (!count)
            return void dactyl.beep();

        (copy ? MsgCopyMessage : MsgMoveMessage)(folders[0]);
        util.timeout(function () {
            dactyl.echomsg(count + " message(s) " + (copy ? "copied" : "moved") + " to " + folders[0].prettyName, 1);
        }, 100);
    },

    _parentIndex: function (index) {
        let parent = index;
        let tree = GetThreadTree();

        while (true) {
            let tmp = tree.view.getParentIndex(parent);
            if (tmp >= 0)
                parent = tmp;
            else
                break;
        }
        return parent;
    },

    // does not wrap yet, intentional?
    _selectUnreadFolder: function (backwards, count) {
        count = Math.max(1, count);
        let direction = backwards ? -1 : 1;
        let c = this._getCurrentFolderIndex();
        let i = direction;
        let folder;
        while (count > 0 && (c + i) < gFolderTreeView.rowCount && (c + i) >= 0) {
            let resource = gFolderTreeView._rowMap[c + i]._folder;
            if (!resource.isServer && resource.getNumUnread(false)) {
                count -= 1;
                folder = i;
            }
            i += direction;
        }
        if (!folder || count > 0)
            dactyl.beep();
        else
            gFolderTreeView.selection.timedSelect(c + folder, 500);
    },

    _escapeRecipient: function (recipient) {
        // strip all ":
        recipient = recipient.replace(/"/g, "");
        return "\"" + recipient + "\"";
    },

    get currentAccount() this.currentFolder.rootFolder,

    get currentFolder() gFolderTreeView.getSelectedFolders()[0],

    /** @property {nsISmtpServer[]} The list of configured SMTP servers. */
    get smtpServers() {
        let servers = services.smtpService.smtpServers;
        let ret = [];

        while (servers.hasMoreElements()) {
            let server = servers.getNext();
            if (server instanceof Ci.nsISmtpServer)
                ret.push(server);
        }

        return ret;
    },

    composeNewMail: function (args) {
        let params = Cc["@mozilla.org/messengercompose/composeparams;1"].createInstance(Ci.nsIMsgComposeParams);
        params.composeFields = Cc["@mozilla.org/messengercompose/composefields;1"].createInstance(Ci.nsIMsgCompFields);

        if (args) {
            if (args.originalMsg)
                params.originalMsgURI = args.originalMsg;
            if (args.to)
                params.composeFields.to = args.to;
            if (args.cc)
                params.composeFields.cc = args.cc;
            if (args.bcc)
                params.composeFields.bcc = args.bcc;
            if (args.newsgroups)
                params.composeFields.newsgroups = args.newsgroups;
            if (args.subject)
                params.composeFields.subject = args.subject;
            if (args.body)
                params.composeFields.body = args.body;

            if (args.attachments) {
                while (args.attachments.length > 0) {
                    let url = args.attachments.pop();
                    let file = io.getFile(url);
                    if (!file.exists())
                        return void dactyl.echoerr("Exxx: Could not attach file `" + url + "'", commandline.FORCE_SINGLELINE);

                    attachment = Cc["@mozilla.org/messengercompose/attachment;1"].createInstance(Ci.nsIMsgAttachment);
                    attachment.url = "file://" + file.path;
                    params.composeFields.addAttachment(attachment);
                }
            }
        }

        params.type = Ci.nsIMsgCompType.New;

        const msgComposeService = Cc["@mozilla.org/messengercompose;1"].getService();
        msgComposeService = msgComposeService.QueryInterface(Ci.nsIMsgComposeService);
        msgComposeService.OpenComposeWindowWithParams(null, params);
    },

    // returns an array of nsIMsgFolder objects
    getFolders: function (filter, includeServers, includeMsgFolders) {
        let folders = [];
        if (!filter)
            filter = "";
        else
            filter = filter.toLowerCase();

        if (includeServers === undefined)
            includeServers = false;
        if (includeMsgFolders === undefined)
            includeMsgFolders = true;

        for (let i = 0; i < gFolderTreeView.rowCount; i++) {
            let resource = gFolderTreeView._rowMap[i]._folder;
            if ((resource.isServer && !includeServers) || (!resource.isServer && !includeMsgFolders))
                continue;

            let folderString = resource.server.prettyName + ": " + resource.name;

            if (resource.prettiestName.toLowerCase().indexOf(filter) >= 0)
                folders.push(resource);
            else if (folderString.toLowerCase().indexOf(filter) >= 0)
                folders.push(resource);
        }
        return folders;
    },

    getNewMessages: function (currentAccountOnly) {
        if (currentAccountOnly)
            MsgGetMessagesForAccount();
        else
            GetMessagesForAllAuthenticatedAccounts();
    },

    getStatistics: function (currentAccountOnly) {
        let accounts = currentAccountOnly ? [this.currentAccount]
                                          : this.getFolders("", true, false);

        let unreadCount = 0, totalCount = 0, newCount = 0;
        for (let i = 0; i < accounts.length; i++) {
            let account = accounts[i];
            unreadCount += account.getNumUnread(true); // true == deep (includes subfolders)
            totalCount  += account.getTotalMessages(true);
            newCount    += account.getNumUnread(true);
        }

        return { numUnread: unreadCount, numTotal: totalCount, numNew: newCount };
    },

    collapseThread: function () {
        let tree = GetThreadTree();
        if (tree) {
            let parent = this._parentIndex(tree.currentIndex);
            if (tree.changeOpenState(parent, false)) {
                tree.view.selection.select(parent);
                tree.treeBoxObject.ensureRowIsVisible(parent);
                return true;
            }
        }
        return false;
    },

    expandThread: function () {
        let tree = GetThreadTree();
        if (tree) {
            let row = tree.currentIndex;
            if (row >= 0 && tree.changeOpenState(row, true))
               return true;
        }
        return false;
    },

    /**
     * General-purpose method to find messages.
     *
     * @param {function(nsIMsgDBHdr):boolean} validatorFunc Return
     *     true/false whether msg should be selected or not.
     * @param {boolean} canWrap When true, wraps around folders.
     * @param {boolean} openThreads Should we open closed threads?
     * @param {boolean} reverse Change direction of searching.
     */
    selectMessage: function (validatorFunc, canWrap, openThreads, reverse, count) {
        function currentIndex() {
            let index = gDBView.selection.currentIndex;
            if (index < 0)
                index = 0;
            return index;
        }

        function closedThread(index) {
            if (!(gDBView.viewFlags & nsMsgViewFlagsType.kThreadedDisplay))
                return false;

            index = (typeof index == "number") ? index : currentIndex();
            return !gDBView.isContainerOpen(index) && !gDBView.isContainerEmpty(index);
        }

        if (typeof validatorFunc != "function")
            return;

        if (typeof count != "number" || count < 1)
            count = 1;

        // first try to find in current folder
        if (gDBView) {
            for (let i = currentIndex() + (reverse ? -1 : (openThreads && closedThread() ? 0 : 1));
                    reverse ? (i >= 0) : (i < gDBView.rowCount);
                    reverse ? i-- : i++) {
                let key = gDBView.getKeyAt(i);
                let msg = gDBView.db.GetMsgHdrForKey(key);

                // a closed thread
                if (openThreads && closedThread(i)) {
                    let thread = gDBView.db.GetThreadContainingMsgHdr(msg);
                    let originalCount = count;

                    for (let j = (i == currentIndex() && !reverse) ? 1 : (reverse ? thread.numChildren - 1 : 0);
                             reverse ? (j >= 0) : (j < thread.numChildren);
                             reverse ? j-- : j++) {
                        msg = thread.getChildAt(j);
                        if (validatorFunc(msg) && --count == 0) {
                            // this hack is needed to get the correct message, because getChildAt() does not
                            // necessarily return the messages in the order they are displayed
                            gDBView.selection.timedSelect(i, GetThreadTree()._selectDelay || 500);
                            GetThreadTree().treeBoxObject.ensureRowIsVisible(i);
                            if (j > 0) {
                                GetThreadTree().changeOpenState(i, true);
                                this.selectMessage(validatorFunc, false, false, false, originalCount);
                            }
                            return;
                        }
                    }
                }
                else { // simple non-threaded message
                    if (validatorFunc(msg) && --count == 0) {
                        gDBView.selection.timedSelect(i, GetThreadTree()._selectDelay || 500);
                        GetThreadTree().treeBoxObject.ensureRowIsVisible(i);
                        return;
                    }
                }
            }
        }

        // then in other folders
        if (canWrap) {
            this._selectMessageReverse = reverse;

            let folders = this.getFolders("", true, true);
            let ci = this._getCurrentFolderIndex();
            for (let i = 1; i < folders.length; i++) {
                let index = (i + ci) % folders.length;
                if (reverse)
                    index = folders.length - 1 - index;

                let folder = folders[index];
                if (folder.isServer)
                    continue;

                this._selectMessageCount = count;
                this._selectMessageKeys = [];

                // sometimes folder.getMessages can fail with an exception
                // TODO: find out why, and solve the problem
                try {
                    var msgs = folder.messages;
                }
                catch (e) {
                    msgs = folder.getMessages(msgWindow); // for older thunderbirds
                    dactyl.dump("WARNING: " + folder.prettyName + " failed to getMessages, trying old API");
                    //continue;
                }

                while (msgs.hasMoreElements()) {
                    let msg = msgs.getNext().QueryInterface(Ci.nsIMsgDBHdr);
                    if (validatorFunc(msg)) {
                        count--;
                        this._selectMessageKeys.push(msg.messageKey);
                    }
                }

                if (count <= 0) {
                    // SelectFolder is asynchronous, message is selected in this._folderListener
                    SelectFolder(folder.URI);
                    return;
                }
            }
        }

        // TODO: finally for the "rest" of the current folder

        dactyl.beep();
    },

    setHTML: function (value) {
        let values = [[true,  1, gDisallow_classes_no_html],  // plaintext
                      [false, 0, 0],                          // HTML
                      [false, 3, gDisallow_classes_no_html]]; // sanitized/simple HTML

        if (typeof value != "number" || value < 0 || value > 2)
            value = 1;

        gPrefBranch.setBoolPref("mailnews.display.prefer_plaintext", values[value][0]);
        gPrefBranch.setIntPref("mailnews.display.html_as", values[value][1]);
        gPrefBranch.setIntPref("mailnews.display.disallow_mime_handlers", values[value][2]);
        ReloadMessage();
    }
}, {
}, {
    commands: function () {
        commands.add(["go[to]"],
            "Select a folder",
            function (args) {
                let count = Math.max(0, args.count - 1);
                let arg = args.literalArg || "Inbox";

                let folder = mail.getFolders(arg, true, true)[count];
                if (!folder)
                    dactyl.echoerr("Exxx: Folder \"" + arg + "\" does not exist");
                else if (dactyl.forceNewTab)
                    MsgOpenNewTabForFolder(folder.URI);
                else
                    SelectFolder(folder.URI);
            },
            {
                argCount: "?",
                completer: function (context) completion.mailFolder(context),
                count: true,
                literal: 0
            });

        commands.add(["m[ail]"],
            "Write a new message",
            function (args) {
                let mailargs = {};
                mailargs.to =          args.join(", ");
                mailargs.subject =     args["-subject"];
                mailargs.bcc =         args["-bcc"] || [];
                mailargs.cc =          args["-cc"] || [];
                mailargs.body =        args["-text"];
                mailargs.attachments = args["-attachment"] || [];

                let addresses = args;
                if (mailargs.bcc)
                    addresses = addresses.concat(mailargs.bcc);
                if (mailargs.cc)
                    addresses = addresses.concat(mailargs.cc);

                // TODO: is there a better way to check for validity?
                if (addresses.some(function (recipient) !(/\S@\S+\.\S/.test(recipient))))
                    return void dactyl.echoerr("Exxx: Invalid e-mail address");

                mail.composeNewMail(mailargs);
            },
            {
                // TODO: completers, validators - whole shebang. Do people actually use this? --djk
                options: [
                    { names: ["-subject", "-s"],     type: CommandOption.STRING, description: "Subject line"},
                    { names: ["-attachment", "-a"],  type: CommandOption.LIST,   description: "List of attachments"},
                    { names: ["-bcc", "-b"],         type: CommandOption.LIST,   description: "Blind Carbon Copy addresses"},
                    { names: ["-cc", "-c"],          type: CommandOption.LIST,   description: "Carbon Copy addresses"},
                    { names: ["-text", "-t"],        type: CommandOption.STRING, description: "Message body"}
                ]
            });

        commands.add(["copy[to]"],
            "Copy selected messages",
            function (args) { this._moveOrCopy(true, args.literalArg); },
            {
                argCount: 1,
                completer: function (context) completion.mailFolder(context),
                literal: 0
            });

        commands.add(["move[to]"],
            "Move selected messages",
            function (args) { this._moveOrCopy(false, args.literalArg); },
            {
                argCount: 1,
                completer: function (context) completion.mailFolder(context),
                literal: 0
            });

        commands.add(["empty[trash]"],
            "Empty trash of the current account",
            function () { window.goDoCommand("cmd_emptyTrash"); },
            { argCount: "0" });

        commands.add(["get[messages]"],
            "Check for new messages",
            function (args) mail.getNewMessages(!args.bang),
            {
                argCount: "0",
                bang: true,
            });
    },
    completion: function () {
        completion.mailFolder = function mailFolder(context) {
            let folders = mail.getFolders(context.filter);
            context.anchored = false;
            context.quote = false;
            context.completions = folders.map(function (folder)
                    [folder.server.prettyName + ": " + folder.name,
                     "Unread: " + folder.getNumUnread(false)]);
        };
    },
    mappings: function () {
        var myModes = config.mailModes;

        mappings.add(myModes, ["<Return>", "i"],
            "Inspect (focus) message",
            function () { content.focus(); });

        mappings.add(myModes, ["I"],
            "Open the message in new tab",
            function () {
                if (gDBView && gDBView.selection.count < 1)
                    return void dactyl.beep();

                MsgOpenNewTabForMessage();
            });

        /*mappings.add([modes.NORMAL],
            ["o"], "Open a message",
            function () { commandline.open(":", "open ", modes.EX); });*/

        mappings.add(myModes, ["<Space>"],
            "Scroll message or select next unread one",
            function () true,
            { route: true });

        mappings.add(myModes, ["t"],
            "Select thread",
            function () { gDBView.ExpandAndSelectThreadByIndex(GetThreadTree().currentIndex, false); });

        mappings.add(myModes, ["d", "<Del>"],
            "Move mail to Trash folder",
            function () { window.goDoCommand("cmd_delete"); });

        mappings.add(myModes, ["j", "<Right>"],
            "Select next message",
            function (count) { mail.selectMessage(function (msg) true, false, false, false, count); },
            { count: true });

        mappings.add(myModes, ["gj"],
            "Select next message, including closed threads",
            function (count) { mail.selectMessage(function (msg) true, false, true, false, count); },
            { count: true });

        mappings.add(myModes, ["J", "<Tab>"],
            "Select next unread message",
            function (count) { mail.selectMessage(function (msg) !msg.isRead, true, true, false, count); },
            { count: true });

        mappings.add(myModes, ["k", "<Left>"],
            "Select previous message",
            function (count) { mail.selectMessage(function (msg) true, false, false, true, count); },
            { count: true });

        mappings.add(myModes, ["gk"],
            "Select previous message",
            function (count) { mail.selectMessage(function (msg) true, false, true, true, count); },
            { count: true });

        mappings.add(myModes, ["K"],
            "Select previous unread message",
            function (count) { mail.selectMessage(function (msg) !msg.isRead, true, true, true, count); },
            { count: true });

        mappings.add(myModes, ["*"],
            "Select next message from the same sender",
            function (count) {
                try {
                    let author = gDBView.hdrForFirstSelectedMessage.mime2DecodedAuthor.toLowerCase();
                    mail.selectMessage(function (msg) msg.mime2DecodedAuthor.toLowerCase().indexOf(author) == 0, true, true, false, count);
                }
                catch (e) { dactyl.beep(); }
            },
            { count: true });

        mappings.add(myModes, ["#"],
            "Select previous message from the same sender",
            function (count) {
                try {
                    let author = gDBView.hdrForFirstSelectedMessage.mime2DecodedAuthor.toLowerCase();
                    mail.selectMessage(function (msg) msg.mime2DecodedAuthor.toLowerCase().indexOf(author) == 0, true, true, true, count);
                }
                catch (e) { dactyl.beep(); }
            },
            { count: true });

        // SENDING MESSAGES
        mappings.add(myModes, ["m"],
            "Compose a new message",
            function () { commandline.open(":", "mail -subject=", modes.EX); });

        mappings.add(myModes, ["M"],
            "Compose a new message to the sender of selected mail",
            function () {
              try {
                  let to = this._escapeRecipient(gDBView.hdrForFirstSelectedMessage.mime2DecodedAuthor);
                  commandline.open(":", "mail " + to + " -subject=", modes.EX);
              }
              catch (e) {
                  dactyl.beep();
              }
            });

        mappings.add(myModes, ["r"],
            "Reply to sender",
            function () { window.goDoCommand("cmd_reply"); });

        mappings.add(myModes, ["R"],
            "Reply to all",
            function () { window.goDoCommand("cmd_replyall"); });

        mappings.add(myModes, ["f"],
            "Forward message",
            function () { window.goDoCommand("cmd_forward"); });

        mappings.add(myModes, ["F"],
            "Forward message inline",
            function () { window.goDoCommand("cmd_forwardInline"); });

        // SCROLLING
        mappings.add(myModes, ["<Down>"],
            "Scroll message down",
            function (count) { buffer.scrollLines(Math.max(count, 1)); },
            { count: true });

        mappings.add(myModes, ["<Up>"],
            "Scroll message up",
            function (count) { buffer.scrollLines(-Math.max(count, 1)); },
            { count: true });

        mappings.add([modes.MESSAGE], ["<Left>"],
            "Select previous message",
            function (count) { mail.selectMessage(function (msg) true, false, false, true, count); },
            { count: true });

        mappings.add([modes.MESSAGE], ["<Right>"],
            "Select next message",
            function (count) { mail.selectMessage(function (msg) true, false, false, false, count); },
            { count: true });

        // UNDO/REDO
        mappings.add(myModes, ["u"],
            "Undo",
            function () {
                if (messenger.canUndo())
                    messenger.undo(msgWindow);
                else
                    dactyl.beep();
            });
        mappings.add(myModes, ["<C-r>"],
            "Redo",
            function () {
                if (messenger.canRedo())
                    messenger.redo(msgWindow);
                else
                    dactyl.beep();
            });

        // GETTING MAIL
        mappings.add(myModes, ["gm"],
            "Get new messages",
            function () { mail.getNewMessages(); });

        mappings.add(myModes, ["gM"],
            "Get new messages for current account only",
            function () { mail.getNewMessages(true); });

        // MOVING MAIL
        mappings.add(myModes, ["c"],
            "Change folders",
            function () { commandline.open(":", "goto ", modes.EX); });

        mappings.add(myModes, ["s"],
            "Move selected messages",
            function () { commandline.open(":", "moveto ", modes.EX); });

        mappings.add(myModes, ["S"],
            "Copy selected messages",
            function () { commandline.open(":", "copyto ", modes.EX); });

        mappings.add(myModes, ["<C-s>"],
            "Archive message",
            function () { this._moveOrCopy(false, options["archivefolder"]); });

        mappings.add(myModes, ["]s"],
            "Select next starred message",
            function (count) { mail.selectMessage(function (msg) msg.isFlagged, true, true, false, count); },
            { count: true });

        mappings.add(myModes, ["[s"],
            "Select previous starred message",
            function (count) { mail.selectMessage(function (msg) msg.isFlagged, true, true, true, count); },
            { count: true });

        mappings.add(myModes, ["]a"],
            "Select next message with an attachment",
            function (count) { mail.selectMessage(function (msg) gDBView.db.HasAttachments(msg.messageKey), true, true, false, count); },
            { count: true });

        mappings.add(myModes, ["[a"],
            "Select previous message with an attachment",
            function (count) { mail.selectMessage(function (msg) gDBView.db.HasAttachments(msg.messageKey), true, true, true, count); },
            { count: true });

        // FOLDER SWITCHING
        mappings.add(myModes, ["gi"],
            "Go to inbox",
            function (count) {
                let folder = mail.getFolders("Inbox", false, true)[(count > 0) ? (count - 1) : 0];
                if (folder)
                    SelectFolder(folder.URI);
                else
                    dactyl.beep();
            },
            { count: true });

        mappings.add(myModes, ["<C-n>"],
            "Select next folder",
            function (count) {
                count = Math.max(1, count);
                let newPos = this._getCurrentFolderIndex() + count;
                if (newPos >= gFolderTreeView.rowCount) {
                    newPos = newPos % gFolderTreeView.rowCount;
                    commandline.echo("search hit BOTTOM, continuing at TOP", commandline.HL_WARNINGMSG, commandline.APPEND_TO_MESSAGES);
                }
                gFolderTreeView.selection.timedSelect(newPos, 500);
            },
            { count: true });

        mappings.add(myModes, ["<C-N>"],
            "Go to next mailbox with unread messages",
            function (count) {
                this._selectUnreadFolder(false, count);
            },
            { count: true });

        mappings.add(myModes, ["<C-p>"],
            "Select previous folder",
            function (count) {
                count = Math.max(1, count);
                let newPos = this._getCurrentFolderIndex() - count;
                if (newPos < 0) {
                    newPos = (newPos % gFolderTreeView.rowCount) + gFolderTreeView.rowCount;
                    commandline.echo("search hit TOP, continuing at BOTTOM", commandline.HL_WARNINGMSG, commandline.APPEND_TO_MESSAGES);
                }
                gFolderTreeView.selection.timedSelect(newPos, 500);
            },
            { count: true });

        mappings.add(myModes, ["<C-P>"],
            "Go to previous mailbox with unread messages",
            function (count) {
                this._selectUnreadFolder(true, count);
            },
            { count: true });

        // THREADING
        mappings.add(myModes, ["za"],
            "Toggle thread collapsed/expanded",
            function () { if (!mail.expandThread()) mail.collapseThread(); });

        mappings.add(myModes, ["zc"],
            "Collapse thread",
            function () { mail.collapseThread(); });

        mappings.add(myModes, ["zo"],
            "Open thread",
            function () { mail.expandThread(); });

        mappings.add(myModes, ["zr", "zR"],
            "Expand all threads",
            function () { window.goDoCommand("cmd_expandAllThreads"); });

        mappings.add(myModes, ["zm", "zM"],
            "Collapse all threads",
            function () { window.goDoCommand("cmd_collapseAllThreads"); });

        mappings.add(myModes, ["<C-i>"],
            "Go forward",
            function (count) { if (count < 1) count = 1; while (count--) GoNextMessage(nsMsgNavigationType.forward, true); },
            { count: true });

        mappings.add(myModes, ["<C-o>"],
            "Go back",
            function (count) { if (count < 1) count = 1; while (count--) GoNextMessage(nsMsgNavigationType.back, true); },
            { count: true });

        mappings.add(myModes, ["gg"],
            "Select first message",
            function (count) { if (count < 1) count = 1; while (count--) GoNextMessage(nsMsgNavigationType.firstMessage, true); },
            { count: true });

        mappings.add(myModes, ["G"],
            "Select last message",
            function (count) { if (count < 1) count = 1; while (count--) GoNextMessage(nsMsgNavigationType.lastMessage, false); },
            { count: true });

        // tagging messages
        mappings.add(myModes, ["l"],
            "Label message",
            function (arg) {
                if (!GetSelectedMessages())
                    return void dactyl.beep();

                switch (arg) {
                    case "r": MsgMarkMsgAsRead(); break;
                    case "s": MsgMarkAsFlagged(); break;
                    case "i": ToggleMessageTagKey(1); break; // Important
                    case "w": ToggleMessageTagKey(2); break; // Work
                    case "p": ToggleMessageTagKey(3); break; // Personal
                    case "t": ToggleMessageTagKey(4); break; // TODO
                    case "l": ToggleMessageTagKey(5); break; // Later
                    default:  dactyl.beep();
                }
            },
            {
                arg: true
            });

        // TODO: change binding?
        mappings.add(myModes, ["T"],
            "Mark current folder as read",
            function () {
                if (mail.currentFolder.isServer)
                    return dactyl.beep();

                mail.currentFolder.markAllMessagesRead(msgWindow);
            });

        mappings.add(myModes, ["<C-t>"],
            "Mark all messages as read",
            function () {
                mail.getFolders("", false).forEach(function (folder) { folder.markAllMessagesRead(msgWindow); });
            });

        // DISPLAY OPTIONS
        mappings.add(myModes, ["h"],
            "Toggle displayed headers",
            function () {
                let value = gPrefBranch.getIntPref("mail.show_headers", 2);
                gPrefBranch.setIntPref("mail.show_headers", value == 2 ? 1 : 2);
                ReloadMessage();
            });

        mappings.add(myModes, ["x"],
            "Toggle HTML message display",
            function () {
                let wantHtml = (gPrefBranch.getIntPref("mailnews.display.html_as", 1) == 1);
                mail.setHTML(wantHtml ? 1 : 0);
            });

        // YANKING TEXT
        mappings.add(myModes, ["Y"],
            "Yank subject",
            function () {
                try {
                    let subject = gDBView.hdrForFirstSelectedMessage.mime2DecodedSubject;
                    dactyl.clipboardWrite(subject, true);
                }
                catch (e) { dactyl.beep(); }
            });

        mappings.add(myModes, ["y"],
            "Yank sender or feed URL",
            function () {
                try {
                    if (mail.currentAccount.server.type == "rss")
                        dactyl.clipboardWrite(this._getRSSUrl(), true);
                    else
                        dactyl.clipboardWrite(gDBView.hdrForFirstSelectedMessage.mime2DecodedAuthor, true);
                }
                catch (e) { dactyl.beep(); }
            });

        // RSS specific mappings
        mappings.add(myModes, ["p"],
            "Open RSS message in browser",
            function () {
                try {
                    if (mail.currentAccount.server.type == "rss")
                        messenger.launchExternalURL(this._getRSSUrl());
                    // TODO: what to do for non-rss message?
                }
                catch (e) {
                    dactyl.beep();
                }
            });
    },
    options: function () {
        // FIXME: why does this default to "Archive", I don't have one? The default
        // value won't validate now. mst please fix. --djk
        options.add(["archivefolder"],
            "Set the archive folder",
            "string", "Archive",
            {
                completer: function (context) completion.mailFolder(context)
            });

        // TODO: generate the possible values dynamically from the menu
        options.add(["layout"],
            "Set the layout of the mail window",
            "string", "inherit",
            {
                setter: function (value) {
                    switch (value) {
                        case "classic":  ChangeMailLayout(0); break;
                        case "wide":     ChangeMailLayout(1); break;
                        case "vertical": ChangeMailLayout(2); break;
                        // case "inherit" just does nothing
                    }

                    return value;
                },
                completer: function (context) [
                    ["inherit",  "Default View"], // FIXME: correct description?
                    ["classic",  "Classic View"],
                    ["wide",     "Wide View"],
                    ["vertical", "Vertical View"]
                ]
            });

        options.add(["smtpserver", "smtp"],
            "Set the default SMTP server",
            "string", services.smtpService.defaultServer.key, // TODO: how should we handle these persistent external defaults - "inherit" or null?
            {
                getter: function () services.smtpService.defaultServer.key,
                setter: function (value) {
                    let server = mail.smtpServers.filter(function (s) s.key == value)[0];
                    services.smtpService.defaultServer = server;
                    return value;
                },
                completer: function (context) [[s.key, s.serverURI] for ([, s] in Iterator(mail.smtpServers))]
            });

        /*options.add(["threads"],
            "Use threading to group messages",
            "boolean", true,
            {
                setter: function (value) {
                    if (value)
                        MsgSortThreaded();
                    else
                        MsgSortUnthreaded();

                    return value;
                }
            });*/
    }
});

// vim: set fdm=marker sw=4 ts=4 et: