diff --git a/src/apps/storetool/bongo/storetool/MailCommands.py b/src/apps/storetool/bongo/storetool/MailCommands.py index 0202c5c..309ed2f 100644 --- a/src/apps/storetool/bongo/storetool/MailCommands.py +++ b/src/apps/storetool/bongo/storetool/MailCommands.py @@ -42,8 +42,7 @@ class MailImportCommand(Command): mailstore = mailbox.Maildir(file, email.message_from_file) for msg in mailstore: - data = msg.as_string(True) - store.Write(collection, DocTypes.Mail, data) + store.Write(collection, DocTypes.Mail, msg.as_string(True)) if fd: fd.close() @@ -112,3 +111,52 @@ class MailExportCommand(Command): print str(e) finally: store.Quit() + +class MailImapCommand(Command): + log = logging.getLogger("Bongo.StoreTool") + + def __init__(self): + Command.__init__(self,"mail-importimap", aliases=["mii"], + summary="Imports mail from an IMAP server", + usage="%prog %cmd") + + self.add_option("", "--imap_username", type="string", default="", help=_("IMAP server username")) + self.add_option("", "--imap_password", type="string", default="", help=_("IMAP server password")) + self.add_option("", "--imap_folder", type="string", default="INBOX", help=_("IMAP server folder")) + self.add_option("", "--imap_server", type="string", default="", help=_("IMAP server hostname")) + self.add_option("", "--imap_port", type="int", default=143, help=_("IMAP server port")) + + def Run(self, options, args): + if (options.imap_username == "" or + options.imap_server == ""): + self.print_help() + self.exit() + + store = StoreClient(options.user, options.store) + try: + self.ImportMail(store, options.imap_username, options.imap_password, options.imap_folder, options.imap_server, options.imap_port) + finally: + store.Quit() + + def ImportMail(self, store, username, password, folder, server, port): + import imaplib + + collection = "/mail/" + folder + store.Create(collection, existingOk=True) + + try: + M=imaplib.IMAP4(server, port) + M.login(username, password) + M.select(folder) + + try: + typ, data = M.search(None, 'ALL') + for num in data[0].split(): + store.WriteImap(collection, DocTypes.Mail, M, num) + + finally: + M.close() + M.logout() + finally: + return + diff --git a/src/libs/python/bongo/store/StoreClient.py b/src/libs/python/bongo/store/StoreClient.py index e90129e..faacffd 100644 --- a/src/libs/python/bongo/store/StoreClient.py +++ b/src/libs/python/bongo/store/StoreClient.py @@ -788,6 +788,47 @@ class StoreClient: self.user = name return r + def WriteImap(self, collection, type, imap, num, filename=None, index=None, guid=None, timeCreated=None, flags=None, link=None): + + typ, data = imap.fetch(num, '(RFC822.SIZE)') + cnt = int(re.findall(r'RFC822\.SIZE (\d+)', data[0])[0]) + + command = "WRITE \"%s\" %d %d" % (collection, type, cnt) + + if index is not None: + command = command + " I%s" % index + + if filename is not None: + command = command + " \"F%s\"" % filename + + if guid is not None: + command = command + " G%s" % guid + + if timeCreated is not None: + command += " T" + timeCreated + + if flags is not None: + command = command + " Z%d" % flags + + if link is not None: + command = command + " \"L%s\"" % link + + self.stream.Write(command) + + + r = self.stream.GetResponse() + if r.code != 2002: + raise CommandError(r) + + self.stream.WriteRaw(imap.fetch(num, '(RFC822)')[1][0][1]) + + r = self.stream.GetResponse() + if r.code != 1000: + raise CommandError(r) + + (guid, junk) = r.message.split(" ", 1) + return guid + def Write(self, collection, type, data, filename=None, index=None, guid=None, timeCreated=None, flags=None, link=None): if data is None: data = ""