From 5bfe771ab9c762bef8f67ab3bcdfe293cb28ab7a Mon Sep 17 00:00:00 2001 From: scrat Date: Sat, 17 May 2008 10:31:02 +0000 Subject: [PATCH] moved codepages.py and the handling of outlook dialoges to system/windows/ writing outlook attachments to disk with mkstemp (NamedTemporaryFile would delete file after closing the handle) git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@2602 fd906abe-77d9-0310-91a1-e0d9ade77398 --- agent/crawl/outlook.py | 77 +++++++-------------- agent/{util => system/windows}/codepages.py | 0 agent/system/windows/outlookdialog.py | 56 +++++++++++++++ 3 files changed, 82 insertions(+), 51 deletions(-) rename agent/{util => system/windows}/codepages.py (100%) create mode 100644 agent/system/windows/outlookdialog.py diff --git a/agent/crawl/outlook.py b/agent/crawl/outlook.py index 0e80188..b2a60e0 100644 --- a/agent/crawl/outlook.py +++ b/agent/crawl/outlook.py @@ -24,6 +24,7 @@ $Id$ import re from email import MIMEMultipart +import tempfile from zope.interface import implements from twisted.internet import defer @@ -37,10 +38,11 @@ from twisted.internet import defer from cybertools.agent.base.agent import Agent, Master from cybertools.agent.crawl.mail import MailCrawler from cybertools.agent.crawl.mail import MailResource +from cybertools.agent.crawl.filesystem import FileResource from cybertools.agent.components import agents from cybertools.agent.system.windows import api from cybertools.agent.util.task import coiterate -from cybertools.agent.util.codepages import codepages +from cybertools.agent.system.windows.codepages import codepages # some constants COMMASPACE = ', ' @@ -125,48 +127,14 @@ class OutlookCrawler(MailCrawler): if isinstance(value, (int, str, unicode, bool)): record[key] = value else: - record[key] = "Invalid data format" + record[key] = None except: - record[key] = "Requested attribute not available" - metadata = self.assembleMetadata(record) + pass + metadata = self.assembleMetadata(folder, record) # Create a resource and append it to the result list self.createResource(mail, folder, metadata) yield None - def login(self): - pass - - def handleOutlookDialog(self): - """ - This function handles the outlook dialog, which appears if someone - tries to access to MS Outlook. - """ - hwnd = None - while True: - hwnd = api.ctypes.windll.user32.FindWindowExA(None, hwnd, None, None) - if hwnd == None: - break - else: - val = u"\0" * 1024 - api.ctypes.windll.user32.GetWindowTextW(hwnd, val, len(val)) - val = val.replace(u"\000", u"") - if val and repr(val) == "u'Microsoft Office Outlook'": - print repr(val) - # get the Main Control - form = api.findTopWindow(wantedText='Microsoft Office Outlook') - controls = findControls(form) - # get the check box - checkBox = findControl(form, wantedText='Zugriff') - setCheckBox(checkBox, 1) - # get the combo box - comboBox = findControl(form, wantedClass='ComboBox') - items = getComboboxItems(comboBox) - selectComboboxItem(comboBox, items[3])#'10 Minuten' - # finally get the button and click it - button = findControl(form, wantedText = 'Erteilen') - clickButton(button) - break - def findOutlook(self): outlookFound = False try: @@ -177,7 +145,7 @@ class OutlookCrawler(MailCrawler): pass return outlookFound - def assembleMetadata(self, mailAttr): + def assembleMetadata(self, folder, mailAttr): meta = {} for key in mailAttr.keys(): if isinstance(mailAttr[key], (str, unicode))\ @@ -190,14 +158,15 @@ class OutlookCrawler(MailCrawler): meta[key] = COMMASPACE.join(lst) else: meta[key] = mailAttr[key] + meta["path"] = folder metadata = self.createMetadata(meta) return metadata def createResource(self, mail, folder, metadata): - enc = "not available" - textType = "not available" + enc = None + textType = "application/octet-stream" attachments = [] - ident = "EntryID not available" + ident = None if (hasattr(mail, 'BodyFormat')): value = getattr(mail, 'BodyFormat') if value == 1: @@ -208,7 +177,7 @@ class OutlookCrawler(MailCrawler): mailContent = getattr(mail, 'Body') textType = "text/plain" else: - mailContent = "Could not retrieve mail body" + mailContent = "" textType = "text/plain" elif value == 2: #2: it is a HTML mail @@ -233,14 +202,20 @@ class OutlookCrawler(MailCrawler): if hasattr(mail, 'Attachments'): attachedElems = getattr(mail, 'Attachments') for item in range(1, len(attachedElems)+1): - attachments.append(attachedElems.Item(item).FileName) - resource = MailResource(data=mailContent,\ - contentType=textType,\ - encoding=enc,\ - path=folder,\ - application='outlook',\ - identifier=ident,\ - metadata=metadata,\ + fileHandle, filePath = tempfile.mkstemp(prefix="outlook") + item.SaveAsFile(fileHandle.name) + fileHandle.close() + fileRes = FileResource(data=None, + path=filePath, + metadata=self.createMetadata(filename=filePath)) + attachments.append(fileRes) + resource = MailResource(data=mailContent, + contentType=textType, + encoding=enc, + path=None, + application='outlook', + identifier=ident, + metadata=metadata, subResources=attachments) self.result.append(resource) diff --git a/agent/util/codepages.py b/agent/system/windows/codepages.py similarity index 100% rename from agent/util/codepages.py rename to agent/system/windows/codepages.py diff --git a/agent/system/windows/outlookdialog.py b/agent/system/windows/outlookdialog.py new file mode 100644 index 0000000..dce390a --- /dev/null +++ b/agent/system/windows/outlookdialog.py @@ -0,0 +1,56 @@ +# +# Copyright (c) 2008 Helmut Merz helmutm@cy55.de +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +""" +Module for handling Outlook dialoges +$Id$ +""" + +def login(self): + pass + +def handleOutlookDialog(self): + """ + This function handles the outlook dialog, which appears if someone + tries to access to MS Outlook. + """ + hwnd = None + while True: + hwnd = api.ctypes.windll.user32.FindWindowExA(None, hwnd, None, None) + if hwnd == None: + break + else: + val = u"\0" * 1024 + api.ctypes.windll.user32.GetWindowTextW(hwnd, val, len(val)) + val = val.replace(u"\000", u"") + if val and repr(val) == "u'Microsoft Office Outlook'": + print repr(val) + # get the Main Control + form = api.findTopWindow(wantedText='Microsoft Office Outlook') + controls = findControls(form) + # get the check box + checkBox = findControl(form, wantedText='Zugriff') + setCheckBox(checkBox, 1) + # get the combo box + comboBox = findControl(form, wantedClass='ComboBox') + items = getComboboxItems(comboBox) + selectComboboxItem(comboBox, items[3])#'10 Minuten' + # finally get the button and click it + button = findControl(form, wantedText = 'Erteilen') + clickButton(button) + break \ No newline at end of file