<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>costela.net &#187; Home</title>
	<atom:link href="http://costela.net/category/home/feed/" rel="self" type="application/rss+xml" />
	<link>http://costela.net</link>
	<description>rhetorically impaired, ironically despaired</description>
	<lastBuildDate>Sun, 08 Apr 2012 13:30:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Importing an Outlook PST into IMAP</title>
		<link>http://costela.net/2011/06/importing-an-outlook-pst-into-imap/</link>
		<comments>http://costela.net/2011/06/importing-an-outlook-pst-into-imap/#comments</comments>
		<pubDate>Sun, 19 Jun 2011 16:16:09 +0000</pubDate>
		<dc:creator>Leo Antunes</dc:creator>
				<category><![CDATA[Home]]></category>
		<category><![CDATA[debian]]></category>
		<category><![CDATA[debian planet]]></category>
		<category><![CDATA[dovecot]]></category>
		<category><![CDATA[imap]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[libpst]]></category>
		<category><![CDATA[outlook]]></category>
		<category><![CDATA[patch]]></category>
		<category><![CDATA[pst]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[readpst]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://costela.net/?p=691</guid>
		<description><![CDATA[Well, every once in a while we&#8217;re forced to do something that isn&#8217;t particularly interesting or pleasant. Last week it happened again: I had to import a few pretty big PSTs (most 2Gig, one 10Gig, with about 100.000 emails) into our dovecot IMAP. Doing this with Outlook itself was out of the question: it took [...]]]></description>
			<content:encoded><![CDATA[<p>Well, every once in a while we&#8217;re forced to do something that isn&#8217;t particularly interesting or pleasant. Last week it happened again: I had to import a few pretty big PSTs (most 2Gig, one 10Gig, with about 100.000 emails) into our dovecot IMAP.</p>
<p>Doing this with Outlook itself was out of the question: it took <em>way</em> too long, even on the local network (many hours for a 1G file) and was prone to hanging and crashes, which were obviously a pain to debug and start over.<br />
Thunderbird was unfortunately not much better, since &#8211; at least in our tests &#8211; it didn&#8217;t import the read status of the emails (they were all marked as unread) and also wasn&#8217;t particularly good at handling folders with strange names, containing &#8220;.&#8221;, &#8220;/&#8221; or some more obscure characters. We had used it before for smaller files, where manually dealing with the problems was acceptable, but this time it required something a bit more elaborate, if we were to keep our sanity.</p>
<p>Enter <a href="http://www.five-ten-sg.com/libpst/">libpst</a>. It includes the handy <em>readpst</em> utility which dumps all emails in usable formats in a directory tree, one directory per folder. Unfortunately the Debian version is somewhat outdated and doesn&#8217;t support the newer Outlook formats, so I did <a href="http://anonscm.debian.org/gitweb/?p=collab-maint/libpst.git">some packaging</a> and even a little bit of <a href="http://anonscm.debian.org/gitweb/?p=collab-maint/libpst.git;a=blob;f=debian/patches/mbox_flags.patch">patching</a>. It seems Thunderbird also uses this library, which would explain why it didn&#8217;t handle the Read-Status (haven&#8217;t confirmed this though; just read it somewhere).</p>
<p>The last step was this not-so-little script, which uses the dumped directories from readpst and imports them in IMAP. It would have probably been a bit more elegant to use libpst directly, but I unfortunately didn&#8217;t have the time to mess around with that. I did have to mess around a lot with encodings though, ergo the unholy chaos with <em>unicode()</em>s and <em>str.encode()</em>s thrown around like rice at a wedding (I could never really wrap my head around charset problems; the subject boggles my mind to this very day).</p>
<p><small>code after the jump</small><br />
<span id="more-691"></span></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python2.7</span>
<span style="color: #808080; font-style: italic;">#-*- encoding: utf-8 -*-</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">sys</span>, <span style="color: #dc143c;">time</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> argparse <span style="color: #ff7700;font-weight:bold;">import</span> ArgumentParser
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">getpass</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">getpass</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">imaplib</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">email</span>.<span style="color: black;">header</span> <span style="color: #ff7700;font-weight:bold;">import</span> decode_header
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">mailbox</span> <span style="color: #ff7700;font-weight:bold;">import</span> mbox
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">mail</span> <span style="color: #ff7700;font-weight:bold;">import</span> imap4 <span style="color: #808080; font-style: italic;"># for their imap4-utf7 implementation</span>
&nbsp;
<span style="color: #dc143c;">parser</span> = ArgumentParser<span style="color: black;">&#40;</span>description=<span style="color: #483d8b;">&quot;&quot;&quot;Recursively import mbox files in a directory
                                        structure to an IMAP server.<span style="color: #000099; font-weight: bold;">\n</span>
                                        The expected structure is that generated by
                                        'readpst -r'.&quot;&quot;&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_argument</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'-s'</span>, dest=<span style="color: #483d8b;">'imap_server'</span>, default=<span style="color: #483d8b;">'localhost'</span>, <span style="color: #008000;">help</span>=<span style="color: #483d8b;">'IMAP server to import emails to'</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_argument</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'-u'</span>, dest=<span style="color: #483d8b;">'imap_user'</span>, required=<span style="color: #008000;">True</span>, <span style="color: #008000;">help</span>=<span style="color: #483d8b;">'user for logging in to IMAP'</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_argument</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'-p'</span>, dest=<span style="color: #483d8b;">'imap_passwd'</span>, <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;will be prompted for if not provided&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_argument</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'-c'</span>, dest=<span style="color: #483d8b;">'charset'</span>, default=<span style="color: #483d8b;">'utf8'</span>, <span style="color: #008000;">help</span>=<span style="color: #483d8b;">'charset in which the folders are stored (for versions older than 2003)'</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_argument</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'-f'</span>, dest=<span style="color: #483d8b;">'force'</span>, action=<span style="color: #483d8b;">'store_true'</span>, <span style="color: #008000;">help</span>=<span style="color: #483d8b;">'import mail even if we think it might be a duplicate'</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_argument</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'-m'</span>, dest=<span style="color: #483d8b;">'mappings'</span>, <span style="color: #008000;">help</span>=<span style="color: #483d8b;">'a JSON file with mappings between folder names and mailbox names (no slashes or dots)'</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_argument</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'folder'</span>, nargs=<span style="color: #483d8b;">'+'</span>, <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;the base folders to import&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
args = <span style="color: #dc143c;">parser</span>.<span style="color: black;">parse_args</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> args.<span style="color: black;">imap_passwd</span>:
    args.<span style="color: black;">imap_passwd</span> = <span style="color: #dc143c;">getpass</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> args.<span style="color: black;">mappings</span>:
    <span style="color: #ff7700;font-weight:bold;">import</span> json
    folderToMailbox = json.<span style="color: black;">load</span><span style="color: black;">&#40;</span><span style="color: #008000;">open</span><span style="color: black;">&#40;</span>args.<span style="color: black;">mappings</span>,<span style="color: #483d8b;">'r'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">else</span>:
    folderToMailbox = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> mailboxFromPath<span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>:
    paths = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> p <span style="color: #ff7700;font-weight:bold;">in</span> path.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">sep</span><span style="color: black;">&#41;</span>:
        p = folderToMailbox.<span style="color: black;">get</span><span style="color: black;">&#40;</span>p, p<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># only other invalid char besides '/', which can't be created by readpst anyway</span>
        p = p.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span>,<span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>
&nbsp;
        paths.<span style="color: black;">append</span><span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">'.'</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>paths<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> imapFlagsFromMbox<span style="color: black;">&#40;</span>flags<span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># libpst only sets R and O</span>
    f = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">'R'</span> <span style="color: #ff7700;font-weight:bold;">in</span> flags <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #483d8b;">'O'</span> <span style="color: #ff7700;font-weight:bold;">in</span> flags:
        f.<span style="color: black;">append</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\S</span>een'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">'D'</span> <span style="color: #ff7700;font-weight:bold;">in</span> flags:
        f.<span style="color: black;">append</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\D</span>eleted'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">'A'</span> <span style="color: #ff7700;font-weight:bold;">in</span> flags:
        f.<span style="color: black;">append</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\A</span>nswered'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">'F'</span> <span style="color: #ff7700;font-weight:bold;">in</span> flags:
        f.<span style="color: black;">append</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\F</span>lagged'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">'('</span>+<span style="color: #483d8b;">' '</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>f<span style="color: black;">&#41;</span>+<span style="color: #483d8b;">')'</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> utf7encode<span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> imap4.<span style="color: black;">encoder</span><span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> headerToUnicode<span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span>:
    h = decode_header<span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> h<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>: <span style="color: #808080; font-style: italic;"># charset != None</span>
            <span style="color: #ff7700;font-weight:bold;">try</span>:
                <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">unicode</span><span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>h<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">LookupError</span>:
                <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">unicode</span><span style="color: black;">&#40;</span>h<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>,<span style="color: #483d8b;">'utf8'</span>,<span style="color: #483d8b;">'replace'</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">unicode</span><span style="color: black;">&#40;</span>h<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, <span style="color: #483d8b;">'utf8'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">UnicodeDecodeError</span>:
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">unicode</span><span style="color: black;">&#40;</span>h<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, <span style="color: #483d8b;">'cp1252'</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># the usual culprits for malformed headers</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">UnicodeDecodeError</span>:
            <span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">unicode</span><span style="color: black;">&#40;</span>h<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, <span style="color: #483d8b;">'latin1'</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># the usual culprits for malformed headers</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">UnicodeDecodeError</span>:
            <span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">unicode</span><span style="color: black;">&#40;</span>h<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, <span style="color: #483d8b;">'ascii'</span>, <span style="color: #483d8b;">'ignore'</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># give up...</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    imap = <span style="color: #dc143c;">imaplib</span>.<span style="color: black;">IMAP4_SSL</span><span style="color: black;">&#40;</span>args.<span style="color: black;">imap_server</span><span style="color: black;">&#41;</span>
    imap.<span style="color: black;">login</span><span style="color: black;">&#40;</span>args.<span style="color: black;">imap_user</span>, args.<span style="color: black;">imap_passwd</span><span style="color: black;">&#41;</span>
&nbsp;
    imap.<span style="color: #dc143c;">select</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> base <span style="color: #ff7700;font-weight:bold;">in</span> args.<span style="color: black;">folder</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;importing folder &quot;</span>+base
        <span style="color: #ff7700;font-weight:bold;">for</span> root, dirs, files <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">walk</span><span style="color: black;">&#40;</span>base<span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">'mbox'</span> <span style="color: #ff7700;font-weight:bold;">in</span> files:
                folder = <span style="color: #008000;">unicode</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">relpath</span><span style="color: black;">&#40;</span>root, base<span style="color: black;">&#41;</span>, args.<span style="color: black;">charset</span><span style="color: black;">&#41;</span>
                <span style="color: #dc143c;">mailbox</span> = mailboxFromPath<span style="color: black;">&#40;</span>folder<span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> u<span style="color: #483d8b;">'importing mbox in {0} to {1}'</span>.<span style="color: black;">format</span><span style="color: black;">&#40;</span>folder, <span style="color: #dc143c;">mailbox</span><span style="color: black;">&#41;</span>
                mailbox_encoded = utf7encode<span style="color: black;">&#40;</span><span style="color: #dc143c;">mailbox</span><span style="color: black;">&#41;</span>
                m = mbox<span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>root, <span style="color: #483d8b;">'mbox'</span><span style="color: black;">&#41;</span>, create=<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">for</span> msg <span style="color: #ff7700;font-weight:bold;">in</span> m:
                    <span style="color: #ff7700;font-weight:bold;">if</span> imap.<span style="color: #dc143c;">select</span><span style="color: black;">&#40;</span>mailbox_encoded<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">'OK'</span>:
                        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;creating mailbox &quot;</span>+<span style="color: #dc143c;">mailbox</span>
                        r = imap.<span style="color: black;">create</span><span style="color: black;">&#40;</span>mailbox_encoded<span style="color: black;">&#41;</span>
                        <span style="color: #ff7700;font-weight:bold;">if</span> r<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">'OK'</span>:
                            <span style="color: #dc143c;">sys</span>.<span style="color: black;">stderr</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Could not create mailbox: &quot;</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>r<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                            <span style="color: #ff7700;font-weight:bold;">continue</span>
                        imap.<span style="color: black;">subscribe</span><span style="color: black;">&#40;</span>mailbox_encoded<span style="color: black;">&#41;</span>
                        imap.<span style="color: #dc143c;">select</span><span style="color: black;">&#40;</span>mailbox_encoded<span style="color: black;">&#41;</span>
&nbsp;
                    <span style="color: #808080; font-style: italic;"># skip possibly duplicated msgs</span>
                    query = <span style="color: #483d8b;">'FROM &quot;{0}&quot; SUBJECT &quot;{1}&quot;'</span>.<span style="color: black;">format</span><span style="color: black;">&#40;</span>
                                utf7encode<span style="color: black;">&#40;</span>headerToUnicode<span style="color: black;">&#40;</span>msg<span style="color: black;">&#91;</span><span style="color: #483d8b;">'from'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'&quot;'</span>,<span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,
                                utf7encode<span style="color: black;">&#40;</span>headerToUnicode<span style="color: black;">&#40;</span>msg<span style="color: black;">&#91;</span><span style="color: #483d8b;">'subject'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'&quot;'</span>,r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                            <span style="color: black;">&#41;</span>
                    <span style="color: #ff7700;font-weight:bold;">if</span> msg.<span style="color: black;">has_key</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'date'</span><span style="color: black;">&#41;</span>:
                        query += <span style="color: #483d8b;">' HEADER DATE &quot;{0}&quot;'</span>.<span style="color: black;">format</span><span style="color: black;">&#40;</span>utf7encode<span style="color: black;">&#40;</span>msg<span style="color: black;">&#91;</span><span style="color: #483d8b;">'date'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                    <span style="color: #ff7700;font-weight:bold;">if</span> msg.<span style="color: black;">has_key</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'message-id'</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> msg<span style="color: black;">&#91;</span><span style="color: #483d8b;">'message-id'</span><span style="color: black;">&#93;</span>:
                        query += <span style="color: #483d8b;">' HEADER MESSAGE-ID &quot;{0}&quot;'</span>.<span style="color: black;">format</span><span style="color: black;">&#40;</span>utf7encode<span style="color: black;">&#40;</span>msg<span style="color: black;">&#91;</span><span style="color: #483d8b;">'message-id'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
                    r = imap.<span style="color: black;">search</span><span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, <span style="color: #483d8b;">'({0})'</span>.<span style="color: black;">format</span><span style="color: black;">&#40;</span>query<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                    <span style="color: #ff7700;font-weight:bold;">if</span> r<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #ff7700;font-weight:bold;">not</span> args.<span style="color: black;">force</span>:
                        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;skipping &quot;</span>+<span style="color: #dc143c;">mailbox</span>+<span style="color: #483d8b;">&quot;: '&quot;</span>+headerToUnicode<span style="color: black;">&#40;</span>msg<span style="color: black;">&#91;</span><span style="color: #483d8b;">'subject'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>:<span style="color: #ff4500;">20</span><span style="color: black;">&#93;</span>+<span style="color: #483d8b;">&quot;' (mid: &quot;</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>msg<span style="color: black;">&#91;</span><span style="color: #483d8b;">'message-id'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>+<span style="color: #483d8b;">&quot;)&quot;</span>
                        <span style="color: #ff7700;font-weight:bold;">continue</span>
&nbsp;
                    r = imap.<span style="color: black;">append</span><span style="color: black;">&#40;</span>mailbox_encoded, <span style="color: #483d8b;">''</span>, <span style="color: #dc143c;">imaplib</span>.<span style="color: black;">Time2Internaldate</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>, <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>msg<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                    <span style="color: #ff7700;font-weight:bold;">if</span> r<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">'OK'</span>:
                        <span style="color: #dc143c;">sys</span>.<span style="color: black;">stderr</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;failed to import {0} ({1}): {2}&quot;</span>.<span style="color: black;">format</span><span style="color: black;">&#40;</span>msg<span style="color: black;">&#91;</span><span style="color: #483d8b;">'message-id'</span><span style="color: black;">&#93;</span>, msg<span style="color: black;">&#91;</span><span style="color: #483d8b;">'date'</span><span style="color: black;">&#93;</span>, r<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                        <span style="color: #ff7700;font-weight:bold;">continue</span>
                    num = <span style="color: #dc143c;">re</span>.<span style="color: black;">sub</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'.*APPENDUID <span style="color: #000099; font-weight: bold;">\d</span>+ (<span style="color: #000099; font-weight: bold;">\d</span>+).*'</span>, r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\1</span>'</span>, r<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
                    r = imap.<span style="color: black;">uid</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'STORE'</span>, <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>num<span style="color: black;">&#41;</span>, <span style="color: #483d8b;">&quot;FLAGS&quot;</span>, imapFlagsFromMbox<span style="color: black;">&#40;</span>msg.<span style="color: black;">get_flags</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
                    <span style="color: #ff7700;font-weight:bold;">if</span> r<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">'OK'</span>:
                        <span style="color: #dc143c;">sys</span>.<span style="color: black;">stderr</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;failed to set flags for msg {0} in {1}&quot;</span>.<span style="color: black;">format</span><span style="color: black;">&#40;</span>num, <span style="color: #dc143c;">mailbox</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    imap.<span style="color: black;">logout</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Another useful automation is using the mappings file, which looks something like</p>

<div class="wp_syntax"><div class="code"><pre class="json" style="font-family:monospace;">{
&quot;Deleted items&quot;: &quot;Trash&quot;,
&quot;Sent items&quot;: &quot;Sent&quot;
}</pre></div></div>

<p>to automatically import in the right destinations, in case you have a standardized structure.</p>
]]></content:encoded>
			<wfw:commentRss>http://costela.net/2011/06/importing-an-outlook-pst-into-imap/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>The Imperial March as it should ALWAYS be played.</title>
		<link>http://costela.net/2011/04/the-imperial-march-as-it-should-always-be-played/</link>
		<comments>http://costela.net/2011/04/the-imperial-march-as-it-should-always-be-played/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 16:07:43 +0000</pubDate>
		<dc:creator>Leo Antunes</dc:creator>
				<category><![CDATA[Home]]></category>
		<category><![CDATA[darth vader]]></category>
		<category><![CDATA[imperial march]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[osb]]></category>
		<category><![CDATA[star wars]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://costela.net/?p=689</guid>
		<description><![CDATA[I may not be the biggest Star Wars geek out there (by far), but I do enjoy almost all soundtracks by John Williams. Specially when it&#8217;s being conducted by none other than Darth Vader himself! This is the Brazilian Symphonic Orchestra (Osquestra Sinfônica Brasileira &#8211; OSB) in one of the many songs in a tribute [...]]]></description>
			<content:encoded><![CDATA[<p><object width="640" height="390"><param name="movie" value="http://www.youtube-nocookie.com/v/7moTZBEVtRc?fs=1&amp;hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube-nocookie.com/v/7moTZBEVtRc?fs=1&amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="390"></embed></object></p>
<p>I may not be the biggest Star Wars geek out there (by far), but I do enjoy almost all soundtracks by John Williams. Specially when it&#8217;s being conducted by none other than Darth Vader himself!<br />
This is the Brazilian Symphonic Orchestra (<em>Osquestra Sinfônica Brasileira &#8211; OSB</em>) in one of the many songs in a <a href="http://www.osb.com.br/concertoseingressos/concerto.aspx?c=1744">tribute to John Williams</a> <small>(link in Portuguese)</small> last year.<br />
Also see <a href="http://www.youtube.com/watch?v=V8wD8ZqZ8fQ">this one</a> for a better introduction, but worse view.</p>
<p>Oh, I so wish I&#8217;d been there! Huge kudos to the many who had the presence of mind to record this.</p>
]]></content:encoded>
			<wfw:commentRss>http://costela.net/2011/04/the-imperial-march-as-it-should-always-be-played/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I really shouldn&#8217;t, but&#8230;</title>
		<link>http://costela.net/2011/01/i-really-shouldnt-but/</link>
		<comments>http://costela.net/2011/01/i-really-shouldnt-but/#comments</comments>
		<pubDate>Thu, 20 Jan 2011 22:31:10 +0000</pubDate>
		<dc:creator>Leo Antunes</dc:creator>
				<category><![CDATA[Home]]></category>
		<category><![CDATA[debian planet]]></category>
		<category><![CDATA[fosdem]]></category>

		<guid isPermaLink="false">http://costela.net/?p=682</guid>
		<description><![CDATA[See you there!]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.fosdem.org"><img src="http://www.fosdem.org/promo/going-to" alt="I'm going to FOSDEM, the Free and Open Source Software Developers' European Meeting" /></a></p>
<p>See you there!</p>
]]></content:encoded>
			<wfw:commentRss>http://costela.net/2011/01/i-really-shouldnt-but/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Screenshots on unrooted Android</title>
		<link>http://costela.net/2010/12/screenshots-on-unrooted-android/</link>
		<comments>http://costela.net/2010/12/screenshots-on-unrooted-android/#comments</comments>
		<pubDate>Wed, 01 Dec 2010 22:26:57 +0000</pubDate>
		<dc:creator>Leo Antunes</dc:creator>
				<category><![CDATA[Home]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[debian planet]]></category>
		<category><![CDATA[feature]]></category>
		<category><![CDATA[screenshot]]></category>

		<guid isPermaLink="false">http://costela.net/?p=666</guid>
		<description><![CDATA[A few weeks ago I noticed an image on my Froyo phone which appeared to be a screenshot. This came as a surprise, since I don&#8217;t have any screenshot applications installed and am generally economic when it comes to the amount of fluff on my phone. I did a few searches and there doesn&#8217;t seem [...]]]></description>
			<content:encoded><![CDATA[<p>A few weeks ago I noticed an image on my Froyo phone which appeared to be a screenshot. This came as a surprise, since I don&#8217;t have any screenshot applications installed and am generally economic when it comes to the amount of fluff on my phone. I did a few searches and there doesn&#8217;t seem to be any visible mention of this feature out there, only mentions of how to do it with the SDK or with specific apps.</p>
<p>Since today I finally managed to discover how to actually do it again (holding the Back button and pressing Home), I took these screenshots using different launchers to be sure it wasn&#8217;t some feature of ADW.Laucher. </p>
<p><a href="/files/SC20101201-224338.png"><img src="/files/SC20101201-224338-400x321.png" alt="" title="" width="400" height="321" class="aligncenter size-medium wp-image-667" /></a></p>
<p>I still haven&#8217;t beat my laziness to actually reset to factory settings and make sure it&#8217;s not some other random app (which would be a bit unsettling, actually) and it could also be a Samsung specific feature, implemented as one of those pesky unremovable apps.</p>
<p>Can anybody out there confirm this (not particularly useful) feature? Did I miss it being published somewhere? </p>
<p><strong>[UPDATE]</strong>: I found a couple of &#8220;hidden&#8221; apps that probably have something to do with this (through Settings→Applications→Manage applications→All).</p>
<p><a href="/files/SC20101202-120607.png"><img src="/files/SC20101202-120607-328x400.png" alt="" title="" width="328" height="400" class="aligncenter size-medium wp-image-671" /></a></p>
<p>The first one is an obvious suspect, but since <del datetime="2010-12-03T12:30:51+00:00">it isn&#8217;t named after Samsung (which admittedly doesn&#8217;t mean much) I figured it might be activated by the second one, which in my overly-creative and not-really-investigative mind could be a sort of hotkey-daemon.</del> [<strong>UPDATE 2</strong>: well, that's ignoring the obvious fact that SEC stands for "Samsung Electronics Corporation". D'oh!]<br />
I don&#8217;t think the feature is important or interesting enough for me to investigate much further, but still I thought I might leave some pointers if people want to get this on their ROMs.</p>
]]></content:encoded>
			<wfw:commentRss>http://costela.net/2010/12/screenshots-on-unrooted-android/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>My kingdom for a VGA cable</title>
		<link>http://costela.net/2010/11/my-kingdom-for-a-vga-cable/</link>
		<comments>http://costela.net/2010/11/my-kingdom-for-a-vga-cable/#comments</comments>
		<pubDate>Fri, 26 Nov 2010 21:44:24 +0000</pubDate>
		<dc:creator>Leo Antunes</dc:creator>
				<category><![CDATA[Home]]></category>
		<category><![CDATA[cat5]]></category>
		<category><![CDATA[debian planet]]></category>
		<category><![CDATA[geek]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[hardware]]></category>
		<category><![CDATA[stargate]]></category>
		<category><![CDATA[vga]]></category>

		<guid isPermaLink="false">http://costela.net/?p=650</guid>
		<description><![CDATA[So you have two geeks in a university room after a relatively late and (at least for one of them) unproductive learning session. It&#8217;s just natural that they decide to kick back and watch some mind-numbingly stupid geek series, which in this case happened to be Stargate SG-1 (so absurdly shitty its actually very entertaining). [...]]]></description>
			<content:encoded><![CDATA[<p>So you have <a href="http://costela.net">two</a> <a href="http://sponc.de">geeks</a> in a university room after a relatively late and (at least for one of them) unproductive learning session. It&#8217;s just natural that they decide to kick back and watch some mind-numbingly stupid geek series, which in this case happened to be <a href="http://en.wikipedia.org/wiki/Stargate_SG-1">Stargate SG-1</a> (so absurdly shitty its actually very entertaining).<br />
The first lazy geek instinct is to just watch it on the laptop that has the file, which &#8211; with its 11&#8243; display and shitty speakers &#8211; doesn&#8217;t turn out to be a great idea. The next try involves the other laptop, but a 13&#8243; screen isn&#8217;t that big of an improvement.<br />
Since the room our intrepid heroes are in happens to have a pretty decent built-in projector and a couple of small &#8211; but still a lot better than a laptop&#8217;s &#8211; Bose speakers, the obvious next step would be using it. The only problem is the lack of a VGA cable.</p>
<p>Inspired by the brief sight of <a href="http://www.imdb.com/name/nm0000760/">MacGyver</a> on the 11&#8243; screen, <a href="http://sponc.de">one particularly enterprising geek</a> comes up with the challenge of making a VGA cable out of the only material available at the time: one horribly yellow <a href="http://en.wikipedia.org/wiki/Category_5_cable">cat5 ethernet cable</a>.<br />
Being the helpful little extra-dimensional entity that it is, the internet happily provided <a href="http://www.instructables.com/id/Make-your-own-VGA-cord-of-CAT5-cable/">all the needed information</a> and after some slight problems trying to appropriately deprive the cat5 of its connectors (no scissors and no blades of any kind in sight) and some annoying and manual sticking-cable-to-socket action&#8230;</p>
<p><img src="/files/2010-11-12-23.35.07-400x300.jpg" alt="what did you mother tell you about sticking things in sockets?" title="what did you mother tell you about sticking things in sockets?" width="400" height="300" class="aligncenter size-medium wp-image-651" /></p>
<p>our reluctant hackers get it right:</p>
<p><img src="/files/2010-11-12-23.54.07-400x300.jpg" alt="it&#039;s ALIIIIIVEEEE!!!" title="it&#039;s ALIIIIIVEEEE!!!" width="400" height="300" class="aligncenter size-medium wp-image-652" /></p>
<p>The final solution looked like this:</p>
<p><a href="/files/2010-11-13-03.36.38.jpg"><img src="/files/2010-11-13-03.36.38-300x400.jpg" alt="nothing like cable salad for dinner" title="nothing like cable salad for dinner" width="300" height="400" class="aligncenter size-medium wp-image-653" /></a></p>
<p>And if you&#8217;re wondering where those white wires came from, one final touch of über-hackerdom:</p>
<p><img src="/files/2010-11-13-00.21.33-400x300.jpg" alt="notepads have never been more useful" title="notepads have never been more useful" width="400" height="300" class="aligncenter size-medium wp-image-654" /></p>
<p>This might seem like overkill, but after a nice nice 4 hour movies and series marathon, we can safely say it was totally worth it (but no, we didn&#8217;t stand 4 hours of Stargate; even geeks have their limits).</p>
<p>&nbsp;</p>
<p><small>Just in case the Instructables page gets hosed at some point, here&#8217;s <a href="/files/vga-cat5.jpg">the invaluable connection diagram</a>, originally scraped off of a since dead Geocities page.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://costela.net/2010/11/my-kingdom-for-a-vga-cable/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

