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
This commit is contained in:
scrat 2008-05-17 10:31:02 +00:00
parent a9c73b5361
commit 5bfe771ab9
3 changed files with 82 additions and 51 deletions

View file

@ -24,6 +24,7 @@ $Id$
import re import re
from email import MIMEMultipart from email import MIMEMultipart
import tempfile
from zope.interface import implements from zope.interface import implements
from twisted.internet import defer 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.base.agent import Agent, Master
from cybertools.agent.crawl.mail import MailCrawler from cybertools.agent.crawl.mail import MailCrawler
from cybertools.agent.crawl.mail import MailResource from cybertools.agent.crawl.mail import MailResource
from cybertools.agent.crawl.filesystem import FileResource
from cybertools.agent.components import agents from cybertools.agent.components import agents
from cybertools.agent.system.windows import api from cybertools.agent.system.windows import api
from cybertools.agent.util.task import coiterate from cybertools.agent.util.task import coiterate
from cybertools.agent.util.codepages import codepages from cybertools.agent.system.windows.codepages import codepages
# some constants # some constants
COMMASPACE = ', ' COMMASPACE = ', '
@ -125,48 +127,14 @@ class OutlookCrawler(MailCrawler):
if isinstance(value, (int, str, unicode, bool)): if isinstance(value, (int, str, unicode, bool)):
record[key] = value record[key] = value
else: else:
record[key] = "Invalid data format" record[key] = None
except: except:
record[key] = "Requested attribute not available" pass
metadata = self.assembleMetadata(record) metadata = self.assembleMetadata(folder, record)
# Create a resource and append it to the result list # Create a resource and append it to the result list
self.createResource(mail, folder, metadata) self.createResource(mail, folder, metadata)
yield None 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): def findOutlook(self):
outlookFound = False outlookFound = False
try: try:
@ -177,7 +145,7 @@ class OutlookCrawler(MailCrawler):
pass pass
return outlookFound return outlookFound
def assembleMetadata(self, mailAttr): def assembleMetadata(self, folder, mailAttr):
meta = {} meta = {}
for key in mailAttr.keys(): for key in mailAttr.keys():
if isinstance(mailAttr[key], (str, unicode))\ if isinstance(mailAttr[key], (str, unicode))\
@ -190,14 +158,15 @@ class OutlookCrawler(MailCrawler):
meta[key] = COMMASPACE.join(lst) meta[key] = COMMASPACE.join(lst)
else: else:
meta[key] = mailAttr[key] meta[key] = mailAttr[key]
meta["path"] = folder
metadata = self.createMetadata(meta) metadata = self.createMetadata(meta)
return metadata return metadata
def createResource(self, mail, folder, metadata): def createResource(self, mail, folder, metadata):
enc = "not available" enc = None
textType = "not available" textType = "application/octet-stream"
attachments = [] attachments = []
ident = "EntryID not available" ident = None
if (hasattr(mail, 'BodyFormat')): if (hasattr(mail, 'BodyFormat')):
value = getattr(mail, 'BodyFormat') value = getattr(mail, 'BodyFormat')
if value == 1: if value == 1:
@ -208,7 +177,7 @@ class OutlookCrawler(MailCrawler):
mailContent = getattr(mail, 'Body') mailContent = getattr(mail, 'Body')
textType = "text/plain" textType = "text/plain"
else: else:
mailContent = "Could not retrieve mail body" mailContent = ""
textType = "text/plain" textType = "text/plain"
elif value == 2: elif value == 2:
#2: it is a HTML mail #2: it is a HTML mail
@ -233,14 +202,20 @@ class OutlookCrawler(MailCrawler):
if hasattr(mail, 'Attachments'): if hasattr(mail, 'Attachments'):
attachedElems = getattr(mail, 'Attachments') attachedElems = getattr(mail, 'Attachments')
for item in range(1, len(attachedElems)+1): for item in range(1, len(attachedElems)+1):
attachments.append(attachedElems.Item(item).FileName) fileHandle, filePath = tempfile.mkstemp(prefix="outlook")
resource = MailResource(data=mailContent,\ item.SaveAsFile(fileHandle.name)
contentType=textType,\ fileHandle.close()
encoding=enc,\ fileRes = FileResource(data=None,
path=folder,\ path=filePath,
application='outlook',\ metadata=self.createMetadata(filename=filePath))
identifier=ident,\ attachments.append(fileRes)
metadata=metadata,\ resource = MailResource(data=mailContent,
contentType=textType,
encoding=enc,
path=None,
application='outlook',
identifier=ident,
metadata=metadata,
subResources=attachments) subResources=attachments)
self.result.append(resource) self.result.append(resource)

View file

@ -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