1*9f22d7c2SAndrew Rist# *************************************************************
2*9f22d7c2SAndrew Rist#
3*9f22d7c2SAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
4*9f22d7c2SAndrew Rist#  or more contributor license agreements.  See the NOTICE file
5*9f22d7c2SAndrew Rist#  distributed with this work for additional information
6*9f22d7c2SAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
7*9f22d7c2SAndrew Rist#  to you under the Apache License, Version 2.0 (the
8*9f22d7c2SAndrew Rist#  "License"); you may not use this file except in compliance
9*9f22d7c2SAndrew Rist#  with the License.  You may obtain a copy of the License at
10*9f22d7c2SAndrew Rist#
11*9f22d7c2SAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
12*9f22d7c2SAndrew Rist#
13*9f22d7c2SAndrew Rist#  Unless required by applicable law or agreed to in writing,
14*9f22d7c2SAndrew Rist#  software distributed under the License is distributed on an
15*9f22d7c2SAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9f22d7c2SAndrew Rist#  KIND, either express or implied.  See the License for the
17*9f22d7c2SAndrew Rist#  specific language governing permissions and limitations
18*9f22d7c2SAndrew Rist#  under the License.
19*9f22d7c2SAndrew Rist#
20*9f22d7c2SAndrew Rist# *************************************************************
21*9f22d7c2SAndrew Rist
22cdf0e10cSrcweir# Caolan McNamara caolanm@redhat.com
23cdf0e10cSrcweir# a simple email mailmerge component
24cdf0e10cSrcweir
25cdf0e10cSrcweir# manual installation for hackers, not necessary for users
26cdf0e10cSrcweir# cp mailmerge.py /usr/lib/openoffice.org2.0/program
27cdf0e10cSrcweir# cd /usr/lib/openoffice.org2.0/program
28cdf0e10cSrcweir# ./unopkg add --shared mailmerge.py
29cdf0e10cSrcweir# edit ~/.openoffice.org2/user/registry/data/org/openoffice/Office/Writer.xcu
30cdf0e10cSrcweir# and change EMailSupported to as follows...
31cdf0e10cSrcweir#  <prop oor:name="EMailSupported" oor:type="xs:boolean">
32cdf0e10cSrcweir#   <value>true</value>
33cdf0e10cSrcweir#  </prop>
34cdf0e10cSrcweir
35cdf0e10cSrcweirimport unohelper
36cdf0e10cSrcweirimport uno
37cdf0e10cSrcweirimport re
38cdf0e10cSrcweir
39cdf0e10cSrcweir#to implement com::sun::star::mail::XMailServiceProvider
40cdf0e10cSrcweir#and
41cdf0e10cSrcweir#to implement com.sun.star.mail.XMailMessage
42cdf0e10cSrcweir
43cdf0e10cSrcweirfrom com.sun.star.mail import XMailServiceProvider
44cdf0e10cSrcweirfrom com.sun.star.mail import XMailService
45cdf0e10cSrcweirfrom com.sun.star.mail import XSmtpService
46cdf0e10cSrcweirfrom com.sun.star.mail import XConnectionListener
47cdf0e10cSrcweirfrom com.sun.star.mail import XAuthenticator
48cdf0e10cSrcweirfrom com.sun.star.mail import XMailMessage
49cdf0e10cSrcweirfrom com.sun.star.mail.MailServiceType import SMTP
50cdf0e10cSrcweirfrom com.sun.star.mail.MailServiceType import POP3
51cdf0e10cSrcweirfrom com.sun.star.mail.MailServiceType import IMAP
52cdf0e10cSrcweirfrom com.sun.star.uno import XCurrentContext
53cdf0e10cSrcweirfrom com.sun.star.lang import IllegalArgumentException
54cdf0e10cSrcweirfrom com.sun.star.lang import EventObject
55cdf0e10cSrcweirfrom com.sun.star.mail import SendMailMessageFailedException
56cdf0e10cSrcweir
57cdf0e10cSrcweirfrom email.MIMEBase import MIMEBase
58cdf0e10cSrcweirfrom email.Message import Message
59cdf0e10cSrcweirfrom email import Encoders
60cdf0e10cSrcweirfrom email.Header import Header
61cdf0e10cSrcweirfrom email.MIMEMultipart import MIMEMultipart
62cdf0e10cSrcweirfrom email.Utils import formatdate
63cdf0e10cSrcweirfrom email.Utils import parseaddr
64d3d1f4e0SAriel Constenla-Hailefrom socket import _GLOBAL_DEFAULT_TIMEOUT
65cdf0e10cSrcweir
66cdf0e10cSrcweirimport sys, smtplib, imaplib, poplib
67cdf0e10cSrcweir
68cdf0e10cSrcweirdbg = False
69cdf0e10cSrcweir
70cdf0e10cSrcweirclass PyMailSMTPService(unohelper.Base, XSmtpService):
71cdf0e10cSrcweir	def __init__( self, ctx ):
72cdf0e10cSrcweir		self.ctx = ctx
73cdf0e10cSrcweir		self.listeners = []
74cdf0e10cSrcweir		self.supportedtypes = ('Insecure', 'Ssl')
75cdf0e10cSrcweir		self.server = None
76cdf0e10cSrcweir		self.connectioncontext = None
77bb7facceSAriel Constenla-Haile		self.notify = EventObject(self)
78cdf0e10cSrcweir		if dbg:
79cdf0e10cSrcweir			print >> sys.stderr, "PyMailSMPTService init"
80cdf0e10cSrcweir	def addConnectionListener(self, xListener):
81cdf0e10cSrcweir		if dbg:
82cdf0e10cSrcweir			print >> sys.stderr, "PyMailSMPTService addConnectionListener"
83cdf0e10cSrcweir		self.listeners.append(xListener)
84cdf0e10cSrcweir	def removeConnectionListener(self, xListener):
85cdf0e10cSrcweir		if dbg:
86cdf0e10cSrcweir			print >> sys.stderr, "PyMailSMPTService removeConnectionListener"
87cdf0e10cSrcweir		self.listeners.remove(xListener)
88cdf0e10cSrcweir	def getSupportedConnectionTypes(self):
89cdf0e10cSrcweir		if dbg:
90cdf0e10cSrcweir			print >> sys.stderr, "PyMailSMPTService getSupportedConnectionTypes"
91cdf0e10cSrcweir		return self.supportedtypes
92cdf0e10cSrcweir	def connect(self, xConnectionContext, xAuthenticator):
93cdf0e10cSrcweir		self.connectioncontext = xConnectionContext
94cdf0e10cSrcweir		if dbg:
95cdf0e10cSrcweir			print >> sys.stderr, "PyMailSMPTService connect"
96d3d1f4e0SAriel Constenla-Haile
97cdf0e10cSrcweir		server = xConnectionContext.getValueByName("ServerName")
98cdf0e10cSrcweir		if dbg:
99d3d1f4e0SAriel Constenla-Haile			print >> sys.stderr, "ServerName: %s" % server
100d3d1f4e0SAriel Constenla-Haile
101cdf0e10cSrcweir		port = xConnectionContext.getValueByName("Port")
102cdf0e10cSrcweir		if dbg:
103d3d1f4e0SAriel Constenla-Haile			print >> sys.stderr, "Port: %d" % port
104d3d1f4e0SAriel Constenla-Haile
105d3d1f4e0SAriel Constenla-Haile		tout = xConnectionContext.getValueByName("Timeout")
106d3d1f4e0SAriel Constenla-Haile		if dbg:
107d3d1f4e0SAriel Constenla-Haile			print >> sys.stderr, isinstance(tout,int)
108d3d1f4e0SAriel Constenla-Haile		if not isinstance(tout,int):
109d3d1f4e0SAriel Constenla-Haile			tout = _GLOBAL_DEFAULT_TIMEOUT
110d3d1f4e0SAriel Constenla-Haile		if dbg:
111d3d1f4e0SAriel Constenla-Haile			print >> sys.stderr, "Timeout: %s" % str(tout)
112d3d1f4e0SAriel Constenla-Haile
113d3d1f4e0SAriel Constenla-Haile		self.server = smtplib.SMTP(server, port,timeout=tout)
114cdf0e10cSrcweir		if dbg:
115cdf0e10cSrcweir			self.server.set_debuglevel(1)
116d3d1f4e0SAriel Constenla-Haile
117cdf0e10cSrcweir		connectiontype = xConnectionContext.getValueByName("ConnectionType")
118cdf0e10cSrcweir		if dbg:
119d3d1f4e0SAriel Constenla-Haile			print >> sys.stderr, "ConnectionType: %s" % connectiontype
120d3d1f4e0SAriel Constenla-Haile
121bb7facceSAriel Constenla-Haile		if connectiontype.upper() == 'SSL':
122cdf0e10cSrcweir			self.server.ehlo()
123cdf0e10cSrcweir			self.server.starttls()
124cdf0e10cSrcweir			self.server.ehlo()
125cdf0e10cSrcweir
126cdf0e10cSrcweir		user = xAuthenticator.getUserName().encode('ascii')
127cdf0e10cSrcweir		password = xAuthenticator.getPassword().encode('ascii')
128cdf0e10cSrcweir		if user != '':
129cdf0e10cSrcweir			if dbg:
130cdf0e10cSrcweir				print >> sys.stderr, 'Logging in, username of', user
131cdf0e10cSrcweir			self.server.login(user, password)
132cdf0e10cSrcweir
133cdf0e10cSrcweir		for listener in self.listeners:
134cdf0e10cSrcweir			listener.connected(self.notify)
135cdf0e10cSrcweir	def disconnect(self):
136cdf0e10cSrcweir		if dbg:
137cdf0e10cSrcweir			print >> sys.stderr, "PyMailSMPTService disconnect"
138cdf0e10cSrcweir		if self.server:
139cdf0e10cSrcweir			self.server.quit()
140cdf0e10cSrcweir			self.server = None
141cdf0e10cSrcweir		for listener in self.listeners:
142cdf0e10cSrcweir			listener.disconnected(self.notify)
143cdf0e10cSrcweir	def isConnected(self):
144cdf0e10cSrcweir		if dbg:
145cdf0e10cSrcweir			print >> sys.stderr, "PyMailSMPTService isConnected"
146cdf0e10cSrcweir		return self.server != None
147cdf0e10cSrcweir	def getCurrentConnectionContext(self):
148cdf0e10cSrcweir		if dbg:
149cdf0e10cSrcweir			print >> sys.stderr, "PyMailSMPTService getCurrentConnectionContext"
150cdf0e10cSrcweir		return self.connectioncontext
151cdf0e10cSrcweir	def sendMailMessage(self, xMailMessage):
152cdf0e10cSrcweir		COMMASPACE = ', '
153cdf0e10cSrcweir
154cdf0e10cSrcweir		if dbg:
155cdf0e10cSrcweir			print >> sys.stderr, "PyMailSMPTService sendMailMessage"
156cdf0e10cSrcweir		recipients = xMailMessage.getRecipients()
157cdf0e10cSrcweir		sendermail = xMailMessage.SenderAddress
158cdf0e10cSrcweir		sendername = xMailMessage.SenderName
159cdf0e10cSrcweir		subject = xMailMessage.Subject
160cdf0e10cSrcweir		ccrecipients = xMailMessage.getCcRecipients()
161cdf0e10cSrcweir		bccrecipients = xMailMessage.getBccRecipients()
162cdf0e10cSrcweir		if dbg:
163cdf0e10cSrcweir			print >> sys.stderr, "PyMailSMPTService subject", subject
164cdf0e10cSrcweir			print >> sys.stderr, "PyMailSMPTService from", sendername.encode('utf-8')
165cdf0e10cSrcweir			print >> sys.stderr, "PyMailSMTPService from", sendermail
166cdf0e10cSrcweir			print >> sys.stderr, "PyMailSMPTService send to", recipients
167cdf0e10cSrcweir
168cdf0e10cSrcweir		attachments = xMailMessage.getAttachments()
169cdf0e10cSrcweir
170cdf0e10cSrcweir		textmsg = Message()
171cdf0e10cSrcweir
172cdf0e10cSrcweir		content = xMailMessage.Body
173cdf0e10cSrcweir		flavors = content.getTransferDataFlavors()
174cdf0e10cSrcweir		if dbg:
175cdf0e10cSrcweir			print >> sys.stderr, "PyMailSMPTService flavors len", len(flavors)
176cdf0e10cSrcweir
177cdf0e10cSrcweir		#Use first flavor that's sane for an email body
178cdf0e10cSrcweir		for flavor in flavors:
179cdf0e10cSrcweir			if flavor.MimeType.find('text/html') != -1 or flavor.MimeType.find('text/plain') != -1:
180cdf0e10cSrcweir				if dbg:
181cdf0e10cSrcweir					print >> sys.stderr, "PyMailSMPTService mimetype is", flavor.MimeType
182cdf0e10cSrcweir				textbody = content.getTransferData(flavor)
183cdf0e10cSrcweir				try:
184cdf0e10cSrcweir					textbody = textbody.value
185cdf0e10cSrcweir				except:
186cdf0e10cSrcweir					pass
187cdf0e10cSrcweir				textbody = textbody.encode('utf-8')
188cdf0e10cSrcweir
189cdf0e10cSrcweir				if len(textbody):
190cdf0e10cSrcweir					mimeEncoding = re.sub("charset=.*", "charset=UTF-8", flavor.MimeType)
191cdf0e10cSrcweir					if mimeEncoding.find('charset=UTF-8') == -1:
192cdf0e10cSrcweir						mimeEncoding = mimeEncoding + "; charset=UTF-8"
193cdf0e10cSrcweir					textmsg['Content-Type'] = mimeEncoding
194cdf0e10cSrcweir					textmsg['MIME-Version'] = '1.0'
195cdf0e10cSrcweir					textmsg.set_payload(textbody)
196cdf0e10cSrcweir
197cdf0e10cSrcweir				break
198cdf0e10cSrcweir
199cdf0e10cSrcweir		if (len(attachments)):
200cdf0e10cSrcweir			msg = MIMEMultipart()
201cdf0e10cSrcweir			msg.epilogue = ''
202cdf0e10cSrcweir			msg.attach(textmsg)
203cdf0e10cSrcweir		else:
204cdf0e10cSrcweir			msg = textmsg
205cdf0e10cSrcweir
206cdf0e10cSrcweir		hdr = Header(sendername, 'utf-8')
207cdf0e10cSrcweir		hdr.append('<'+sendermail+'>','us-ascii')
208cdf0e10cSrcweir		msg['Subject'] = subject
209cdf0e10cSrcweir		msg['From'] = hdr
210cdf0e10cSrcweir		msg['To'] = COMMASPACE.join(recipients)
211cdf0e10cSrcweir		if len(ccrecipients):
212cdf0e10cSrcweir			msg['Cc'] = COMMASPACE.join(ccrecipients)
213cdf0e10cSrcweir		if xMailMessage.ReplyToAddress != '':
214cdf0e10cSrcweir			msg['Reply-To'] = xMailMessage.ReplyToAddress
215cdf0e10cSrcweir
216cdf0e10cSrcweir		mailerstring = "OpenOffice.org 2.0 via Caolan's mailmerge component"
217cdf0e10cSrcweir		try:
218bb7facceSAriel Constenla-Haile			ctx = uno.getComponentContext()
219cdf0e10cSrcweir			aConfigProvider = ctx.ServiceManager.createInstance("com.sun.star.configuration.ConfigurationProvider")
220cdf0e10cSrcweir			prop = uno.createUnoStruct('com.sun.star.beans.PropertyValue')
221cdf0e10cSrcweir			prop.Name = "nodepath"
222cdf0e10cSrcweir			prop.Value = "/org.openoffice.Setup/Product"
223cdf0e10cSrcweir			aSettings = aConfigProvider.createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess",
224cdf0e10cSrcweir				(prop,))
225cdf0e10cSrcweir			mailerstring = aSettings.getByName("ooName") + " " + \
226cdf0e10cSrcweir				aSettings.getByName("ooSetupVersion") + " via Caolan's mailmerge component"
227cdf0e10cSrcweir		except:
228cdf0e10cSrcweir			pass
229bb7facceSAriel Constenla-Haile
230cdf0e10cSrcweir		msg['X-Mailer'] = mailerstring
231cdf0e10cSrcweir		msg['Date'] = formatdate(localtime=True)
232cdf0e10cSrcweir
233cdf0e10cSrcweir		for attachment in attachments:
234cdf0e10cSrcweir			content = attachment.Data
235cdf0e10cSrcweir			flavors = content.getTransferDataFlavors()
236cdf0e10cSrcweir			flavor = flavors[0]
237cdf0e10cSrcweir			ctype = flavor.MimeType
238cdf0e10cSrcweir			maintype, subtype = ctype.split('/', 1)
239cdf0e10cSrcweir			msgattachment = MIMEBase(maintype, subtype)
240cdf0e10cSrcweir			data = content.getTransferData(flavor)
241cdf0e10cSrcweir			msgattachment.set_payload(data)
242cdf0e10cSrcweir			Encoders.encode_base64(msgattachment)
243a0292563SAriel Constenla-Haile			fname = attachment.ReadableName
244a0292563SAriel Constenla-Haile			try:
245a0292563SAriel Constenla-Haile				fname.encode('ascii')
246a0292563SAriel Constenla-Haile			except:
247a0292563SAriel Constenla-Haile				fname = ('utf-8','',fname.encode('utf-8'))
248cdf0e10cSrcweir			msgattachment.add_header('Content-Disposition', 'attachment', \
249a0292563SAriel Constenla-Haile				filename=fname)
250cdf0e10cSrcweir			msg.attach(msgattachment)
251cdf0e10cSrcweir
252cdf0e10cSrcweir		uniquer = {}
253cdf0e10cSrcweir		for key in recipients:
254cdf0e10cSrcweir			uniquer[key] = True
255cdf0e10cSrcweir		if len(ccrecipients):
256cdf0e10cSrcweir			for key in ccrecipients:
257cdf0e10cSrcweir				uniquer[key] = True
258cdf0e10cSrcweir		if len(bccrecipients):
259cdf0e10cSrcweir			for key in bccrecipients:
260cdf0e10cSrcweir				uniquer[key] = True
261cdf0e10cSrcweir		truerecipients = uniquer.keys()
262cdf0e10cSrcweir
263cdf0e10cSrcweir		if dbg:
264cdf0e10cSrcweir			print >> sys.stderr, "PyMailSMPTService recipients are", truerecipients
265cdf0e10cSrcweir
266cdf0e10cSrcweir		self.server.sendmail(sendermail, truerecipients, msg.as_string())
267cdf0e10cSrcweir
268cdf0e10cSrcweirclass PyMailIMAPService(unohelper.Base, XMailService):
269cdf0e10cSrcweir	def __init__( self, ctx ):
270cdf0e10cSrcweir		self.ctx = ctx
271cdf0e10cSrcweir		self.listeners = []
272cdf0e10cSrcweir		self.supportedtypes = ('Insecure', 'Ssl')
273cdf0e10cSrcweir		self.server = None
274cdf0e10cSrcweir		self.connectioncontext = None
275bb7facceSAriel Constenla-Haile		self.notify = EventObject(self)
276cdf0e10cSrcweir		if dbg:
277cdf0e10cSrcweir			print >> sys.stderr, "PyMailIMAPService init"
278cdf0e10cSrcweir	def addConnectionListener(self, xListener):
279cdf0e10cSrcweir		if dbg:
280cdf0e10cSrcweir			print >> sys.stderr, "PyMailIMAPService addConnectionListener"
281cdf0e10cSrcweir		self.listeners.append(xListener)
282cdf0e10cSrcweir	def removeConnectionListener(self, xListener):
283cdf0e10cSrcweir		if dbg:
284cdf0e10cSrcweir			print >> sys.stderr, "PyMailIMAPService removeConnectionListener"
285cdf0e10cSrcweir		self.listeners.remove(xListener)
286cdf0e10cSrcweir	def getSupportedConnectionTypes(self):
287cdf0e10cSrcweir		if dbg:
288cdf0e10cSrcweir			print >> sys.stderr, "PyMailIMAPService getSupportedConnectionTypes"
289cdf0e10cSrcweir		return self.supportedtypes
290cdf0e10cSrcweir	def connect(self, xConnectionContext, xAuthenticator):
291cdf0e10cSrcweir		if dbg:
292cdf0e10cSrcweir			print >> sys.stderr, "PyMailIMAPService connect"
293cdf0e10cSrcweir
294cdf0e10cSrcweir		self.connectioncontext = xConnectionContext
295cdf0e10cSrcweir		server = xConnectionContext.getValueByName("ServerName")
296cdf0e10cSrcweir		if dbg:
297cdf0e10cSrcweir			print >> sys.stderr, server
298cdf0e10cSrcweir		port = xConnectionContext.getValueByName("Port")
299cdf0e10cSrcweir		if dbg:
300cdf0e10cSrcweir			print >> sys.stderr, port
301cdf0e10cSrcweir		connectiontype = xConnectionContext.getValueByName("ConnectionType")
302cdf0e10cSrcweir		if dbg:
303cdf0e10cSrcweir			print >> sys.stderr, connectiontype
304cdf0e10cSrcweir		print >> sys.stderr, "BEFORE"
305bb7facceSAriel Constenla-Haile		if connectiontype.upper() == 'SSL':
306cdf0e10cSrcweir			self.server = imaplib.IMAP4_SSL(server, port)
307cdf0e10cSrcweir		else:
308cdf0e10cSrcweir			self.server = imaplib.IMAP4(server, port)
309cdf0e10cSrcweir		print >> sys.stderr, "AFTER"
310bb7facceSAriel Constenla-Haile
311cdf0e10cSrcweir		user = xAuthenticator.getUserName().encode('ascii')
312cdf0e10cSrcweir		password = xAuthenticator.getPassword().encode('ascii')
313cdf0e10cSrcweir		if user != '':
314cdf0e10cSrcweir			if dbg:
315cdf0e10cSrcweir				print >> sys.stderr, 'Logging in, username of', user
316cdf0e10cSrcweir			self.server.login(user, password)
317cdf0e10cSrcweir
318cdf0e10cSrcweir		for listener in self.listeners:
319cdf0e10cSrcweir			listener.connected(self.notify)
320cdf0e10cSrcweir	def disconnect(self):
321cdf0e10cSrcweir		if dbg:
322cdf0e10cSrcweir			print >> sys.stderr, "PyMailIMAPService disconnect"
323cdf0e10cSrcweir		if self.server:
324cdf0e10cSrcweir			self.server.logout()
325cdf0e10cSrcweir			self.server = None
326cdf0e10cSrcweir		for listener in self.listeners:
327cdf0e10cSrcweir			listener.disconnected(self.notify)
328cdf0e10cSrcweir	def isConnected(self):
329cdf0e10cSrcweir		if dbg:
330cdf0e10cSrcweir			print >> sys.stderr, "PyMailIMAPService isConnected"
331cdf0e10cSrcweir		return self.server != None
332cdf0e10cSrcweir	def getCurrentConnectionContext(self):
333cdf0e10cSrcweir		if dbg:
334cdf0e10cSrcweir			print >> sys.stderr, "PyMailIMAPService getCurrentConnectionContext"
335cdf0e10cSrcweir		return self.connectioncontext
336cdf0e10cSrcweir
337cdf0e10cSrcweirclass PyMailPOP3Service(unohelper.Base, XMailService):
338cdf0e10cSrcweir	def __init__( self, ctx ):
339cdf0e10cSrcweir		self.ctx = ctx
340cdf0e10cSrcweir		self.listeners = []
341cdf0e10cSrcweir		self.supportedtypes = ('Insecure', 'Ssl')
342cdf0e10cSrcweir		self.server = None
343cdf0e10cSrcweir		self.connectioncontext = None
344bb7facceSAriel Constenla-Haile		self.notify = EventObject(self)
345cdf0e10cSrcweir		if dbg:
346cdf0e10cSrcweir			print >> sys.stderr, "PyMailPOP3Service init"
347cdf0e10cSrcweir	def addConnectionListener(self, xListener):
348cdf0e10cSrcweir		if dbg:
349cdf0e10cSrcweir			print >> sys.stderr, "PyMailPOP3Service addConnectionListener"
350cdf0e10cSrcweir		self.listeners.append(xListener)
351cdf0e10cSrcweir	def removeConnectionListener(self, xListener):
352cdf0e10cSrcweir		if dbg:
353cdf0e10cSrcweir			print >> sys.stderr, "PyMailPOP3Service removeConnectionListener"
354cdf0e10cSrcweir		self.listeners.remove(xListener)
355cdf0e10cSrcweir	def getSupportedConnectionTypes(self):
356cdf0e10cSrcweir		if dbg:
357cdf0e10cSrcweir			print >> sys.stderr, "PyMailPOP3Service getSupportedConnectionTypes"
358cdf0e10cSrcweir		return self.supportedtypes
359cdf0e10cSrcweir	def connect(self, xConnectionContext, xAuthenticator):
360cdf0e10cSrcweir		if dbg:
361cdf0e10cSrcweir			print >> sys.stderr, "PyMailPOP3Service connect"
362cdf0e10cSrcweir
363cdf0e10cSrcweir		self.connectioncontext = xConnectionContext
364cdf0e10cSrcweir		server = xConnectionContext.getValueByName("ServerName")
365cdf0e10cSrcweir		if dbg:
366cdf0e10cSrcweir			print >> sys.stderr, server
367cdf0e10cSrcweir		port = xConnectionContext.getValueByName("Port")
368cdf0e10cSrcweir		if dbg:
369cdf0e10cSrcweir			print >> sys.stderr, port
370cdf0e10cSrcweir		connectiontype = xConnectionContext.getValueByName("ConnectionType")
371cdf0e10cSrcweir		if dbg:
372cdf0e10cSrcweir			print >> sys.stderr, connectiontype
373cdf0e10cSrcweir		print >> sys.stderr, "BEFORE"
374bb7facceSAriel Constenla-Haile		if connectiontype.upper() == 'SSL':
375cdf0e10cSrcweir			self.server = poplib.POP3_SSL(server, port)
376cdf0e10cSrcweir		else:
377d3d1f4e0SAriel Constenla-Haile			tout = xConnectionContext.getValueByName("Timeout")
378d3d1f4e0SAriel Constenla-Haile			if dbg:
379d3d1f4e0SAriel Constenla-Haile				print >> sys.stderr, isinstance(tout,int)
380d3d1f4e0SAriel Constenla-Haile			if not isinstance(tout,int):
381d3d1f4e0SAriel Constenla-Haile				tout = _GLOBAL_DEFAULT_TIMEOUT
382d3d1f4e0SAriel Constenla-Haile			if dbg:
383d3d1f4e0SAriel Constenla-Haile				print >> sys.stderr, "Timeout: %s" % str(tout)
384d3d1f4e0SAriel Constenla-Haile			self.server = poplib.POP3(server, port, timeout=tout)
385cdf0e10cSrcweir		print >> sys.stderr, "AFTER"
386bb7facceSAriel Constenla-Haile
387cdf0e10cSrcweir		user = xAuthenticator.getUserName().encode('ascii')
388cdf0e10cSrcweir		password = xAuthenticator.getPassword().encode('ascii')
389cdf0e10cSrcweir		if dbg:
390cdf0e10cSrcweir			print >> sys.stderr, 'Logging in, username of', user
391cdf0e10cSrcweir		self.server.user(user)
392bb7facceSAriel Constenla-Haile		self.server.pass_(password)
393cdf0e10cSrcweir
394cdf0e10cSrcweir		for listener in self.listeners:
395cdf0e10cSrcweir			listener.connected(self.notify)
396cdf0e10cSrcweir	def disconnect(self):
397cdf0e10cSrcweir		if dbg:
398cdf0e10cSrcweir			print >> sys.stderr, "PyMailPOP3Service disconnect"
399cdf0e10cSrcweir		if self.server:
400cdf0e10cSrcweir			self.server.quit()
401cdf0e10cSrcweir			self.server = None
402cdf0e10cSrcweir		for listener in self.listeners:
403cdf0e10cSrcweir			listener.disconnected(self.notify)
404cdf0e10cSrcweir	def isConnected(self):
405cdf0e10cSrcweir		if dbg:
406cdf0e10cSrcweir			print >> sys.stderr, "PyMailPOP3Service isConnected"
407cdf0e10cSrcweir		return self.server != None
408cdf0e10cSrcweir	def getCurrentConnectionContext(self):
409cdf0e10cSrcweir		if dbg:
410cdf0e10cSrcweir			print >> sys.stderr, "PyMailPOP3Service getCurrentConnectionContext"
411cdf0e10cSrcweir		return self.connectioncontext
412cdf0e10cSrcweir
413cdf0e10cSrcweirclass PyMailServiceProvider(unohelper.Base, XMailServiceProvider):
414cdf0e10cSrcweir	def __init__( self, ctx ):
415cdf0e10cSrcweir		if dbg:
416cdf0e10cSrcweir			print >> sys.stderr, "PyMailServiceProvider init"
417cdf0e10cSrcweir		self.ctx = ctx
418cdf0e10cSrcweir	def create(self, aType):
419cdf0e10cSrcweir		if dbg:
420cdf0e10cSrcweir			print >> sys.stderr, "PyMailServiceProvider create with", aType
421cdf0e10cSrcweir		if aType == SMTP:
422cdf0e10cSrcweir			return PyMailSMTPService(self.ctx);
423cdf0e10cSrcweir		elif aType == POP3:
424cdf0e10cSrcweir			return PyMailPOP3Service(self.ctx);
425cdf0e10cSrcweir		elif aType == IMAP:
426cdf0e10cSrcweir			return PyMailIMAPService(self.ctx);
427cdf0e10cSrcweir		else:
428cdf0e10cSrcweir			print >> sys.stderr, "PyMailServiceProvider, unknown TYPE", aType
429cdf0e10cSrcweir
430cdf0e10cSrcweirclass PyMailMessage(unohelper.Base, XMailMessage):
431cdf0e10cSrcweir	def __init__( self, ctx, sTo='', sFrom='', Subject='', Body=None, aMailAttachment=None ):
432cdf0e10cSrcweir		if dbg:
433cdf0e10cSrcweir			print >> sys.stderr, "PyMailMessage init"
434cdf0e10cSrcweir		self.ctx = ctx
435cdf0e10cSrcweir
436bb7facceSAriel Constenla-Haile		self.recipients = [sTo]
437bb7facceSAriel Constenla-Haile		self.ccrecipients = []
438bb7facceSAriel Constenla-Haile		self.bccrecipients = []
439bb7facceSAriel Constenla-Haile		self.aMailAttachments = []
440cdf0e10cSrcweir		if aMailAttachment != None:
441bb7facceSAriel Constenla-Haile			self.aMailAttachments.append(aMailAttachment)
442cdf0e10cSrcweir
443cdf0e10cSrcweir		self.SenderName, self.SenderAddress = parseaddr(sFrom)
444cdf0e10cSrcweir		self.ReplyToAddress = sFrom
445cdf0e10cSrcweir		self.Subject = Subject
446cdf0e10cSrcweir		self.Body = Body
447cdf0e10cSrcweir		if dbg:
448cdf0e10cSrcweir			print >> sys.stderr, "post PyMailMessage init"
449cdf0e10cSrcweir	def addRecipient( self, recipient ):
450cdf0e10cSrcweir		if dbg:
451cdf0e10cSrcweir			print >> sys.stderr, "PyMailMessage.addRecipient", recipient
452bb7facceSAriel Constenla-Haile		self.recipients.append(recipient)
453cdf0e10cSrcweir	def addCcRecipient( self, ccrecipient ):
454cdf0e10cSrcweir		if dbg:
455cdf0e10cSrcweir			print >> sys.stderr, "PyMailMessage.addCcRecipient", ccrecipient
456bb7facceSAriel Constenla-Haile		self.ccrecipients.append(ccrecipient)
457cdf0e10cSrcweir	def addBccRecipient( self, bccrecipient ):
458cdf0e10cSrcweir		if dbg:
459cdf0e10cSrcweir			print >> sys.stderr, "PyMailMessage.addBccRecipient", bccrecipient
460bb7facceSAriel Constenla-Haile		self.bccrecipients.append(bccrecipient)
461cdf0e10cSrcweir	def getRecipients( self ):
462cdf0e10cSrcweir		if dbg:
463cdf0e10cSrcweir			print >> sys.stderr, "PyMailMessage.getRecipients", self.recipients
464bb7facceSAriel Constenla-Haile		return tuple(self.recipients)
465cdf0e10cSrcweir	def getCcRecipients( self ):
466cdf0e10cSrcweir		if dbg:
467cdf0e10cSrcweir			print >> sys.stderr, "PyMailMessage.getCcRecipients", self.ccrecipients
468bb7facceSAriel Constenla-Haile		return tuple(self.ccrecipients)
469cdf0e10cSrcweir	def getBccRecipients( self ):
470cdf0e10cSrcweir		if dbg:
471cdf0e10cSrcweir			print >> sys.stderr, "PyMailMessage.getBccRecipients", self.bccrecipients
472bb7facceSAriel Constenla-Haile		return tuple(self.bccrecipients)
473cdf0e10cSrcweir	def addAttachment( self, aMailAttachment ):
474cdf0e10cSrcweir		if dbg:
475cdf0e10cSrcweir			print >> sys.stderr, "PyMailMessage.addAttachment"
476bb7facceSAriel Constenla-Haile		self.aMailAttachments.append(aMailAttachment)
477cdf0e10cSrcweir	def getAttachments( self ):
478cdf0e10cSrcweir		if dbg:
479cdf0e10cSrcweir			print >> sys.stderr, "PyMailMessage.getAttachments"
480bb7facceSAriel Constenla-Haile		return tuple(self.aMailAttachments)
481bb7facceSAriel Constenla-Haile
482cdf0e10cSrcweir
483cdf0e10cSrcweir# pythonloader looks for a static g_ImplementationHelper variable
484cdf0e10cSrcweirg_ImplementationHelper = unohelper.ImplementationHelper()
485cdf0e10cSrcweirg_ImplementationHelper.addImplementation( \
486cdf0e10cSrcweir	PyMailServiceProvider, "org.openoffice.pyuno.MailServiceProvider",
487cdf0e10cSrcweir		("com.sun.star.mail.MailServiceProvider",),)
488cdf0e10cSrcweirg_ImplementationHelper.addImplementation( \
489cdf0e10cSrcweir	PyMailMessage, "org.openoffice.pyuno.MailMessage",
490cdf0e10cSrcweir		("com.sun.star.mail.MailMessage",),)
491