1 // Copyright (c) 2008 by Christian Dietrich <stettberger@dokucode.de>
3 // This work is licensed for reuse under an MIT license. Details are
4 // given in the LICENSE.txt file included with this file.
7 const Addressbook = Module("addressbook", {
11 // TODO: add option for a format specifier, like:
12 // :set displayname=%l, %f
13 generateDisplayName: function (firstName, lastName) {
14 if (firstName && lastName)
15 return lastName + ", " + firstName;
24 getDirectoryFromURI: function (uri) services.rdf.GetResource(uri).QueryInterface(Ci.nsIAbDirectory),
26 add: function (address, firstName, lastName, displayName) {
27 const personalAddressbookURI = "moz-abmdbdirectory://abook.mab";
28 let directory = this.getDirectoryFromURI(personalAddressbookURI);
29 let card = Cc["@mozilla.org/addressbook/cardproperty;1"].createInstance(Ci.nsIAbCard);
31 if (!address || !directory || !card)
34 card.primaryEmail = address;
35 card.firstName = firstName;
36 card.lastName = lastName;
37 card.displayName = displayName;
39 return directory.addCard(card);
42 // TODO: add telephone number support
43 list: function (filter, newMail) {
45 let dirs = services.abManager.directories;
46 let lowerFilter = filter.toLowerCase();
48 while (dirs.hasMoreElements()) {
49 let addrbook = dirs.getNext().QueryInterface(Ci.nsIAbDirectory);
50 let cards = addrbook.childCards;
51 while (cards.hasMoreElements()) {
52 let card = cards.getNext().QueryInterface(Ci.nsIAbCard);
53 //var mail = card.primaryEmail || ""; //XXX
54 let displayName = card.displayName;
56 displayName = this.generateDisplayName(card.firstName, card.lastName);
58 if (displayName.toLowerCase().indexOf(lowerFilter) > -1
59 || card.primaryEmail.toLowerCase().indexOf(lowerFilter) > -1)
60 addresses.push([displayName, card.primaryEmail]);
64 if (addresses.length < 1) {
66 dactyl.echoerr("Exxx: No contacts", commandline.FORCE_SINGLELINE);
68 dactyl.echoerr("Exxx: No contacts matching string '" + filter + "'", commandline.FORCE_SINGLELINE);
73 // Now we have to create a new message
75 args.to = addresses.map(
76 function (address) "\"" + address[0].replace(/"/g, "") + " <" + address[1] + ">\""
79 mail.composeNewMail(args);
82 let list = template.tabular(["Name", "Address"], [],
83 [[util.clip(address[0], 50), address[1]] for ([, address] in Iterator(addresses))]
85 commandline.echo(list, commandline.HL_NORMAL, commandline.FORCE_MULTILINE);
91 commands: function () {
92 commands.add(["con[tact]"],
93 "Add an address book entry",
95 let mailAddr = args[0]; // TODO: support more than one email address
96 let firstName = args["-firstname"] || null;
97 let lastName = args["-lastname"] || null;
98 let displayName = args["-name"] || null;
100 displayName = this.generateDisplayName(firstName, lastName);
102 if (addressbook.add(mailAddr, firstName, lastName, displayName))
103 dactyl.echomsg("Added address: " + displayName + " <" + mailAddr + ">", 1, commandline.FORCE_SINGLELINE);
105 dactyl.echoerr("Exxx: Could not add contact `" + mailAddr + "'", commandline.FORCE_SINGLELINE);
110 options: [{ names: ["-firstname", "-f"], type: CommandOption.STRING, description: "The first name of the contact" },
111 { names: ["-lastname", "-l"], type: CommandOption.STRING, description: "The last name of the contact" },
112 { names: ["-name", "-n"], type: CommandOption.STRING, description: "The display name of the contact" }]
115 commands.add(["contacts", "addr[essbook]"],
116 "List or open multiple addresses",
117 function (args) { addressbook.list(args.string, args.bang); },
120 mappings: function () {
121 var myModes = config.mailModes;
123 mappings.add(myModes, ["a"],
124 "Open a prompt to save a new addressbook entry for the sender of the selected message",
127 var to = gDBView.hdrForFirstSelectedMessage.mime2DecodedAuthor;
136 let address = to.substring(to.indexOf("<") + 1, to.indexOf(">"));
138 let displayName = to.substr(0, to.indexOf("<") - 1);
139 if (/^\S+\s+\S+\s*$/.test(displayName)) {
140 let names = displayName.split(/\s+/);
141 displayName = "-firstname=" + names[0].replace(/"/g, "")
142 + " -lastname=" + names[1].replace(/"/g, "");
145 displayName = "-name=\"" + displayName.replace(/"/g, "") + "\"";
147 CommandExMode().open("contact " + address + " " + displayName);
150 services: function initServices(dactyl, modules, window) {
151 services.add("abManager", "@mozilla.org/abmanager;1", Ci.nsIAbManager);
155 // vim: set fdm=marker sw=4 ts=4 et: