Skip to content

Instantly share code, notes, and snippets.

@daniel-barrows
Last active February 21, 2019 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daniel-barrows/c4345cfc22f3e8e6032d130ab11cbdf4 to your computer and use it in GitHub Desktop.
Save daniel-barrows/c4345cfc22f3e8e6032d130ab11cbdf4 to your computer and use it in GitHub Desktop.
The beginnings of a Thunderbird extension to convert an email to a RememberTheMilk task.
// License: GPLv3+
// Author: 2019 Daniel Barrows
// Status: pre-alpha
// Canonical URL: https://gist.github.com/daniel-barrows/c4345cfc22f3e8e6032d130ab11cbdf4
// require jquery
// Tested with jquery 1.12.4 and jquery-ui 1.12.1 at https://jqueryui.com/resources/demos/dialog/default.html
// Require Thunderlink
// Feature request:
// When any email is sent, tag: tasked-for-later should be removed, or perhaps
// prompt for removal
// These are options that should be set in a preferences dialog
rtm_thunderbird = {
prefs: {
default_task_entry: "Reply to %SENDER% re: %SUBJECT%",
hidden_task_additions: "#email %URL%",
rtm_task_email: 'username+abc123@rmilk.com',
archive_default: true,
}
};
// This should be pulled from the email metadata
email_meta = {
"%SENDER%": 'Joe Brown',
"%SUBJECT%": 'Meeting request',
'%URL%': 'thunderlink://messageid=81f59-1795-5c6df840@example.com',
};
// This is the actual script.
// Based on https://dxr.mozilla.org/thunderbird/source/mail/base/content/mailContextMenus.js#780
// This successfully opens a compose window with appropriate headers, but I wish
// it could send the message as well.
function compose_email_now(headers){
var fields = Components.classes["@mozilla.org/messengercompose/composefields;1"].createInstance(Components.interfaces.nsIMsgCompFields);
var params = Components.classes["@mozilla.org/messengercompose/composeparams;1"].createInstance(Components.interfaces.nsIMsgComposeParams);
for(var k in headers) fields[k]=headers[k];
params.type = Components.interfaces.nsIMsgCompType.New;
params.format = Components.interfaces.nsIMsgCompFormat.Default;
//params.identity = accountManager.getFirstIdentityForServer(GetLoadedMsgFolder().server);
params.identity = accountManager.getFirstIdentityForServer(this.GetSelectedMsgFolders()[0].server);
params.composeFields = fields;
var msgComposeService = Components.classes["@mozilla.org/messengercompose;1"].getService(Components.interfaces.nsIMsgComposeService);
return msgComposeService.OpenComposeWindowWithParams(null, params);
// See https://dxr.mozilla.org/thunderbird/source/mail/base/content/mailCommands.js#180
//ComposeMessage(Components.interfaces.nsIMsgCompType.New,
// Components.interfaces.nsIMsgCompFormat.Default,
// GetDefaultAccountRootFolder(),
// params); // params is ignored here.
//let mheader = Components.classes['@mozilla.org/messenger;1'].getService(Components.interfaces.nsIMessenger).msgHdrFromURI(?????)
}
// This should return true if and when the email is stored in the outbox.
// https://www.rememberthemilk.com/services/email/
rtm_thunderbird.add_task = function(task_str){
headers = {
to: rtm_thunderbird.prefs.rtm_task_email,
subject: task_str,
}
compose_email_now(headers);
};
// https://stackoverflow.com/a/7975025/5389585
rtm_thunderbird.replace_with_email_meta = function(str, email_meta){
return str.replace(/%\w+%/g, function(all) {
return email_meta[all] || all;
});
};
rtm_thunderbird.prompt_task_for_email = function(email_meta){
task_entry = rtm_thunderbird.replace_with_email_meta(rtm_thunderbird.prefs.default_task_entry, email_meta);
hidden_task_addition = rtm_thunderbird.replace_with_email_meta(rtm_thunderbird.prefs.hidden_task_additions, email_meta);
archive_checked = rtm_thunderbird.prefs.archive_default ? 'checked="checked"' : ''
dialog = jQuery('<div/>', {
id: 'rtm-taskmaker-dialog',
title: 'Create RememberTheMilk task from email',
});
dialog.append(`<label>Enter task: <input type="text" id="rtm-task-entry" required minlength="1" value="${task_entry}" size="96%" /></label><br/>
<label><input type="checkbox" id="rtm-email-archive" ${archive_checked}" /> Archive associated email?</label>`);
dialog.appendTo('body');
dialog.dialog({
'width': '60em',
buttons: [
{
text: "Add task",
icon: "ui-icon-plus",
click: function() {
task_entry = $('#rtm-task-entry').val() + ' ' + hidden_task_addition;
// Don't wait for mail to be sent, but do wait for mail to be stored.
if(rtm_thunderbird.add_task(task_entry)){
// email.addTag() and email.archive() are pseudo-code.
email.addTag('tasked-for-later');
if($('#rtm-email-archive').prop('checked')){email.archive()};
}
alert(task_entry);
$( this ).dialog( "close" );
}
},
{
text: "Cancel",
icon: "ui-icon-cancel",
click: function() {
$( this ).dialog( "close" );
}
},
]
});
return null;
};
//rtm_thunderbird.prompt_task_for_email(email_meta);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment