Source code for zbot.plugs.scan

# zbot/plugs/directory.py
#
#

""" mail input plugin. """

## IMPORTS

from zbot.utils import strip_html, j, error, need_skip
from zbot import kernel, Object

import email.message
import datetime
import logging
import email
import time
import os

[docs]def scan_dir(*args, **kwargs): target = args[0] teller = 0 for fn in os.listdir(target): ff = target + os.sep + fn logging.warn("reading %s" % ff) if os.path.isdir(ff): scan_dir(ff) try: o = Object() try: data = open(ff, "r").read() except Exception as ex: logging.warn("problem scanning %s - %s" % (ff, str(ex))) ; continue message = email.message_from_string(data) ; o.update(message.items()) o.text = "" o.filename = ff payload = message.get_payload() if not payload: continue go = True for load in payload: if not load: continue if need_skip(load, [], ["text/plain", ],): go = False ; continue else: o.text += str(load) if not go: continue if "Date" in o: o.mail_date = o["Date"] o.save() except IsADirectoryError: continue except Exception: error() ; continue teller += 1 logging.warn("at %s" % teller) ## COMMANDS
[docs]def do_scan(event): if not os.path.isdir(event.rest): logging.warn("scan <directory>") ; return scan_dir(event.rest) event.reply("done")
kernel.cmnds.register("scan", do_scan)