Fix double release of IMAP connection object

This commit fixes the case when we're invoking releaseconnection()
for a given imapobj twice.

This bug manifests itself as
{{{
ValueError: list.remove(x): x not in list

  File "[...]/offlineimap/folder/IMAP.py", line 615, in savemessage
    self.imapserver.releaseconnection(imapobj)
  File "[...]/offlineimap/imapserver.py", line 130, in releaseconnection
    self.assignedconnections.remove(connection)
}}}

Signed-off-by: Eygene Ryabinkin <rea@codelabs.ru>
Introduce dynamic filtering for repository folders

Allow people who want folder filtering to depend on the external
conditions or to make it dynamic for other reasons, to do what
they want.

New repository configuration knob 'dynamic_folderfilter' was
introduced; it defaults to 'False' that matches historical behaviour.

GitHub: #73
Signed-off-by: Eygene Ryabinkin <rea@codelabs.ru>
Fix: Undefined variable 'this' should be 'self'

Pointyhat-to: Eygene Ryabinkin
Signed-off-by: Eygene Ryabinkin <rea@codelabs.ru>
change_message_uid did not update messagelist's filename

This broke code that relied on the filename being up to date in memory after
messages are copied.
Changed NotImplementedException to NotImplementedError

It seems NotImplementedException does not exist. It must be a relic from old
python...
Enable compressed connections to the IMAP server

Added the configuration setting usecompression for the IMAP repositories.
When enabled, the data from and to the IMAP server is compressed.
Restructured folder.IMAP code

Preparing for a sequence of commits implementing gmail label sync, I have split
some functions so I will be able to change some functionality in folder.Gmail,
with less code repetition.

Also added some functions to folder.base and imaputil I will later need.
Adds labels and mtime to LocalStatus Sqlite table

Adds two columns to the LocalStatus Sqlite table:
  * labels: A comma separated list of labels, to be used by Gmail folder.
  * mtime: The POSIX modification time for the message in a local Maildir.

The interface for the class LocalStatusSQLite remains compatible with what it
was (i.e. new arguments to functions are optional with default values).
Make GmailFolder sync gmail labels

When synclabels config flag is set to "yes" for the gmail repo, offlineimap
fetches the message labels along with the messages, and embeds them into the
body under the header X-Keywords, as a comma separated list.

The configuration option labelsheader allows to change that header under which
labels are stored. X-Keywords is a useful choice as some mail programs may
recognize it.

It also adds an extra pass to savemessageto, that performs label synchronization
on existing messages from gmail to local, the same way it is done with flags.

The ignorelabels configuration seting contains is a list of comma separated
labels that will be left alone. They will not be added nor removed from any
message.
Implements a GmailMaildir repository

The GmailMaildir repository adds functionality to change message labels. Also
keeps track of messages modification time, so that it can quickly detect when
the labels may have changed.
Added a filterheaders option

When filterheaders is set to a comma-separated list of headers, offlineimap
removes those headers from messages before uploading them to the server.
Update documentation with the recent gmail changes
fixed re.sub call, that was using python 2.7 features.
fixes setting labels on new local messages

This fixes a bug, that would crash offlineimap when:
  - synclabels is enabled
  - there is a new local message not present on gmail
  - the new local message has embedded labels

In this case, the label sync would use the wrong uid and crash. In particular,
this happens when moving messages between different folders "the IMAP way",
instead of changing labels.
Better handling of gmail labels header

In particular, now formats X-Label and Keywords as a space separated list
instead of comma separated list.
fixes header manipulation function

Corrects behaviour of addheader when the message has no '\n\n' separating
headers from content (it may happen with empty content).
fix message_addheader again

A newline was missing when there is no '\n\n' and adds the header at the
beginning.
Don't use gmail's IMAP extensions for the IMAP folder

A query for X-GM-LABELS had slipped in commit 6ecdfc4c993b9003.
initialize an empty messagelist for empty remote folders'

This produced a TypeError because self.messagelist remained None,
instead of {}.
fixes confusion between uids and message sequential number

cachemessagelist from Gmail folder mixed up uid's with sequential
message numbers. This effectively broke Gmail folder when synclabels is
enabled and some filtering on messages is performed, like maxage.

Now we use make sure to use sequential numbers in cachemessagelist, as
it compactifies into ranges better than uid's.