Skip to content

Instantly share code, notes, and snippets.

@irq0
Created November 6, 2012 21:04
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 irq0/4027531 to your computer and use it in GitHub Desktop.
Save irq0/4027531 to your computer and use it in GitHub Desktop.
Script to capture org-mode TODOs from mutt
?? build/
#!/usr/bin/env python
import sys
import email.parser
from subprocess import call
import urllib
def make_org_link(**kwargs):
return "[[{link}][{title}]]".format(**kwargs)
def message_id_link(message):
if 'Message-ID' in message:
return "message://{mid}".format(
mid=message["Message-ID"].replace("<","").replace(">",""))
else:
return "message://"
def message_body(msg):
result = ""
for part in msg.walk():
if part.get_content_type() == "text/plain":
result = part.get_payload()
break
return result
def message_to_org_capture(msg):
return """\
Subject: {subj}
From: {frm}
Message: {link}
{body}
""".format(subj=msg["Subject"],
frm=msg["From"],
link=make_org_link(title=msg["Subject"], link=message_id_link(msg)),
body=message_body(msg))
def url_encode(part):
return urllib.quote(part, safe='~()*!.\'')
def org_capture(cap, msg):
cmd = ["emacsclient",
"-c",
"--no-wait",
"org-protocol://capture://m/{url}/{subject}/{body}".format(
url=url_encode(message_id_link(msg)),
subject=url_encode(msg["Subject"]),
body=url_encode(cap))]
call(cmd)
msg = email.parser.Parser().parsestr(sys.stdin.read())
org_capture(message_to_org_capture(msg), msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment