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