This repository was archived by the owner on Aug 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNlconverterLib.py
283 lines (235 loc) · 9.8 KB
/
NlconverterLib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# -*- coding: utf-8 -*-
import os
#COM/DDE
import win32com.client #NB : Calls to COM are starting with an uppercase
#Mime dependencies
import email.mime.multipart
import email.mime.text
import email.mime.base
import email.header
import mimetypes
from email import encoders
import re #in order to parse addresses
import tempfile #required for dealing with attachment
#icalendar / time
import icalendar
import datetime
import time
#mailbox
import mailbox
#Regexp
addressNotesDomainTable = { 'dgi.finances.gouv.fr' : 'dgfip.finances.gouv.fr', }
reGenericAddressNotes = re.compile(r'CN=(.*?)\s+(.*?)\/(.*?)O=(\w*?)\/C=(\w*)', re.IGNORECASE)
reOU = re.compile(r'OU=(\w+?)\/', re.IGNORECASE)
reAddressMail = re.compile(r'([a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,6})', re.IGNORECASE)
#this list should be extended to match regular install path
notesDllPathList = ['c:/notes', 'd:/notes']
def registerNotesDll():
for p in notesDllPathList :
fp = os.path.join(p, 'nlsxbe.dll')
if os.path.exists(fp) and os.system("regsvr32 /s %s" % fp) == 0:
return True
return False
def getNotesDb(notesNsfPath, notesPasswd):
"""Connect to notes and open the nsf file"""
session = win32com.client.Dispatch(r'Lotus.NotesSession')
session.Initialize(notesPasswd)
return session.GetDatabase("", notesNsfPath)
class NotesDocumentReader(object):
"""Base class for all documents"""
def __init__(self):
#compute a name for temporary files (attachments)
self.tempname = os.path.join(tempfile.gettempdir(), 'nlc.tmp')
def get(self, doc, itemname):
"""Helper to get an Item value in a document"""
return doc.GetItemValue(itemname)
def get1(self, doc, itemname):
"""Helper to get the first item value"""
itemvalue = self.get(doc, itemname)
if len(itemvalue):
return itemvalue[0]
else :
return u''
def fullDebug(self, doc):
"""Debug method : print all items values"""
self.debugItems(doc, doc.Items)
def debug(self, doc):
"""Debug method : print message identifiers"""
self.debugItems(doc, ["Subject", "From", "To", "PostedDate", "DeliveredDate"])
def debugItems(self, doc, itemlist):
"""Generic debug method"""
self.log(20*'-')
for it in itemlist:
try:
self.log("--%s = %s" % (it, doc.GetItemValue(it)) )
except:
self.log("--%s = !! can't display item value !!" % it)
self.log(20*'-')
def matchAddress(self, value):
"""Convert Notes Address Name Space into emails"""
res = reGenericAddressNotes.search(value)
if res == None:
res = reAddressMail.search(value)
if res == None:
return value.lower()
else :
return res.group(1)
else :
mail = u"%s.%s@" % ( res.group(1).lower(), res.group(2).lower() )
subs = reOU.findall(res.group(3))
subs += res.groups()[3:]
suffix = ('.'.join(subs)).lower()
if addressNotesDomainTable.has_key(suffix):
suffix = addressNotesDomainTable[suffix]
mail += suffix
return mail.lower()
def listAttachments(self, doc):
"""Return the list of the attachments, striping None and void names"""
return filter(lambda x : x != None and x != u'', self.get(doc, "$FILE"))
def hasAttachments(self, doc):
"""True if theyre are any attachments"""
return len(self.listAttachments) > 0
def extractAttachment(self, doc, f):
"""Extract an attachment from the document"""
a = doc.GetAttachment(f)
#FIXME : bug when there is \xa0 (non breaking space) in the filename. What to do then ?
if a == None :
self.log("ERROR: Can't get attachment for this message :")
self.debug(doc)
return None
a.ExtractFile(self.tempname)
return self.tempname
def dateitem2datetime(self, doc, itemname):
datetuple = time.gmtime(int(self.get1(doc, itemname)) )[:5]
return datetime.datetime(*datetuple )
def log(self, message = ""):
print message
class NotesDocumentConverter(NotesDocumentReader):
"""Base class for all converters"""
formWhiteList = None
formBlackList = []
def addDocument(self, doc):
"""Generic add of a document which does nothing"""
"""Check if this form type is allowed"""
fname = self.get1(doc, 'Form')
return (
(self.formWhiteList == None or fname in self.formWhiteList)
and (fname not in self.formBlackList) #OK for blacklist
)
def close(self):
pass
class NotesToIcalConverter(NotesDocumentConverter):
formWhiteList = ['Appointment']
cal = None
filedescriptor = None
def __init__(self, icalfilename):
"""open/init icalfilename"""
super(NotesToIcalConverter, self).__init__()
self.cal = icalendar.Calendar()
self.filedescriptor = open(icalfilename, 'wb')
def addDocument(self, doc):
if not super(NotesToIcalConverter, self).addDocument(doc):
return False
if self.filedescriptor == None :
self.log("ERROR: destination file not defined !!")
m = self.buildMessage(doc)
self.cal.add_component(m)
#self.debug(doc)
return True
def close(self):
self.filedescriptor.write(self.cal.as_string())
self.filedescriptor.close()
def matchAddress2vcal(self, address):
return icalendar.vCalAddress("MAILTO:%s" % self.matchAddress(address) )
def buildMessage(self, doc):
event = icalendar.Event()
event['uid'] = self.get1(doc, "ApptUNID")
event.add('summary', self.get1(doc, 'Subject'))
event.add('dtstart', self.dateitem2datetime(doc, "StartDate"))
event.add('dtend', self.dateitem2datetime(doc, "EndDate"))
event.add('dtstamp', self.dateitem2datetime(doc, "StartDate"))
organizer = self.matchAddress2vcal(self.get1(doc, "From") )
#FIXME : encoding is not correctly handled...
event.add('organizer' , organizer, encode='iso-8859-15')
for att in self.get(doc, "SendTo"):
attendee = self.matchAddress2vcal( att )
attendee.params['ROLE'] = icalendar.vText('REQ-PARTICIPANT')
event.add('attendee', attendee, encode='iso-8859-15')
return event
class NotesToMimeConverter(NotesDocumentConverter):
"""Convert a Memo Document to a Mime Message"""
charset = 'iso-8859-15' #default charset
charsetAttachment = 'utf-8' #attachment filename charset. Because Linux and Windows seems to use Utf-8 for filenames...
def stringToHeader(self, value):
"""Build a Mail header value from a string"""
return email.header.Header(value, self.charset)
def header(self, doc, itemname):
return self.stringToHeader(self.get1(doc, itemname))
def addressHeader(self, doc, item):
items = self.get(doc, item)
return self.stringToHeader(",".join(map(self.matchAddress, items)))
def messageHeaders(self, doc, m):
m['Subject'] = self.header(doc, "Subject")
m['From'] = self.addressHeader(doc, "From")
m['To'] = self.addressHeader(doc, "sendto")
m['Cc'] = self.addressHeader(doc, "copyto")
m['Date'] = self.get1(doc, "PostedDate")
if m['Date'] == u'':
m['Date'] = self.get1(doc, "DeliveredDate")
ccc = self.addressHeader(doc, "BlindCopyTo")
if ccc != u'':
m['Ccc'] = ccc
m['User-Agent'] = self.header(doc, "$Mailer")
m['Message-ID'] = self.header(doc, "$MessageID")
def buildAttachment(self, doc, f):
"""Build Mime Attachment 'f' from doc"""
tmp = self.extractAttachment(doc, f)
msg = email.mime.base.MIMEBase('application', 'octet-stream')
if tmp != None :
fp = open(self.tempname, 'rb')
msg.set_payload(fp.read())
fp.close()
encoders.encode_base64(msg)
try:
fname = f.encode(self.charsetAttachment)
except :
fname = f.encode(self.charset)
msg.add_header('Content-Disposition', 'attachment', filename=fname)
return msg
def buildMessage(self, doc):
"""Build a message from doc"""
main = email.mime.text.MIMEText(self.get1(doc, 'Body'), _charset=self.charset)
files = self.listAttachments(doc)
if len(files) > 0 :
m = email.mime.multipart.MIMEMultipart(charset=self.charset)
m.set_charset(self.charset)
self.messageHeaders(doc, m)
m.attach(main)
for it in doc.Items :
#FIXME: should be in listAttachments
#FIXME: should use NotesEmbedded plutôt que de scanner les items
if it.type == 1084:
f = it.Values[0]
msg = self.buildAttachment(doc, f)
m.attach(msg)
else:
m = main
self.messageHeaders(doc, m)
return m
class NotesToMboxConverter(NotesToMimeConverter):
"""Notes to mbox format converter"""
mbox = None
def __init__(self, filename):
super(NotesToMboxConverter, self).__init__()
self.filename = filename
self.mbox = mailbox.mbox(filename, None, True)
def addDocument(self, doc):
"""Add a notes document to the mbox storage"""
super(NotesToMboxConverter, self).addDocument(doc)
m = self.buildMessage(doc)
self.mbox.add(m)
def close(self):
"""Close the mbox file"""
self.log("Writing %s ... please wait." % self.filename)
self.mbox.close()
self.log("INFO: mbox file %s completed" % self.filename)