19f22d7c2SAndrew Rist# *************************************************************
2*4861d9f0Smseidel#
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
10*4861d9f0Smseidel#
119f22d7c2SAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
12*4861d9f0Smseidel#
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.
19*4861d9f0Smseidel#
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
69bc42e70bSAriel Constenla-Haileout = sys.stderr
70cdf0e10cSrcweir
71cdf0e10cSrcweirclass PyMailSMTPService(unohelper.Base, XSmtpService):
7213cfd8dfSPedro Giffuni    def __init__( self, ctx ):
7313cfd8dfSPedro Giffuni        self.ctx = ctx
7413cfd8dfSPedro Giffuni        self.listeners = []
7513cfd8dfSPedro Giffuni        self.supportedtypes = ('Insecure', 'Ssl')
7613cfd8dfSPedro Giffuni        self.server = None
7713cfd8dfSPedro Giffuni        self.connectioncontext = None
7813cfd8dfSPedro Giffuni        self.notify = EventObject(self)
7913cfd8dfSPedro Giffuni        if dbg:
80*4861d9f0Smseidel            out.write("PyMailSMTPService init\n")
8113cfd8dfSPedro Giffuni    def addConnectionListener(self, xListener):
8213cfd8dfSPedro Giffuni        if dbg:
83*4861d9f0Smseidel            out.write("PyMailSMTPService addConnectionListener\n")
8413cfd8dfSPedro Giffuni        self.listeners.append(xListener)
8513cfd8dfSPedro Giffuni    def removeConnectionListener(self, xListener):
8613cfd8dfSPedro Giffuni        if dbg:
87*4861d9f0Smseidel            out.write("PyMailSMTPService removeConnectionListener\n")
8813cfd8dfSPedro Giffuni        self.listeners.remove(xListener)
8913cfd8dfSPedro Giffuni    def getSupportedConnectionTypes(self):
9013cfd8dfSPedro Giffuni        if dbg:
91*4861d9f0Smseidel            out.write("PyMailSMTPService getSupportedConnectionTypes\n")
9213cfd8dfSPedro Giffuni        return self.supportedtypes
9313cfd8dfSPedro Giffuni    def connect(self, xConnectionContext, xAuthenticator):
9413cfd8dfSPedro Giffuni        self.connectioncontext = xConnectionContext
9513cfd8dfSPedro Giffuni        if dbg:
96*4861d9f0Smseidel            out.write("PyMailSMTPService connect\n")
9713cfd8dfSPedro Giffuni
9813cfd8dfSPedro Giffuni        server = xConnectionContext.getValueByName("ServerName")
9913cfd8dfSPedro Giffuni        if dbg:
100bc42e70bSAriel Constenla-Haile            out.write("ServerName: %s\n" % server)
10113cfd8dfSPedro Giffuni
10213cfd8dfSPedro Giffuni        port = xConnectionContext.getValueByName("Port")
10313cfd8dfSPedro Giffuni        if dbg:
104bc42e70bSAriel Constenla-Haile            out.write("Port: %d\n" % port)
10513cfd8dfSPedro Giffuni
10613cfd8dfSPedro Giffuni        tout = xConnectionContext.getValueByName("Timeout")
10713cfd8dfSPedro Giffuni        if dbg:
108bc42e70bSAriel Constenla-Haile            out.write("Timeout is instance of int? %s\n" % isinstance(tout,int))
10913cfd8dfSPedro Giffuni        if not isinstance(tout,int):
11013cfd8dfSPedro Giffuni            tout = _GLOBAL_DEFAULT_TIMEOUT
11113cfd8dfSPedro Giffuni        if dbg:
112bc42e70bSAriel Constenla-Haile            out.write("Timeout: %s\n" % str(tout))
11313cfd8dfSPedro Giffuni
11413cfd8dfSPedro Giffuni        self.server = smtplib.SMTP(server, port,timeout=tout)
11513cfd8dfSPedro Giffuni        if dbg:
11613cfd8dfSPedro Giffuni            self.server.set_debuglevel(1)
11713cfd8dfSPedro Giffuni
11813cfd8dfSPedro Giffuni        connectiontype = xConnectionContext.getValueByName("ConnectionType")
11913cfd8dfSPedro Giffuni        if dbg:
120bc42e70bSAriel Constenla-Haile            out.write("ConnectionType: %s\n" % str(connectiontype))
12113cfd8dfSPedro Giffuni
12213cfd8dfSPedro Giffuni        if connectiontype.upper() == 'SSL':
12313cfd8dfSPedro Giffuni            self.server.ehlo()
12413cfd8dfSPedro Giffuni            self.server.starttls()
12513cfd8dfSPedro Giffuni            self.server.ehlo()
12613cfd8dfSPedro Giffuni
12713cfd8dfSPedro Giffuni        user = xAuthenticator.getUserName().encode('ascii')
12813cfd8dfSPedro Giffuni        password = xAuthenticator.getPassword().encode('ascii')
12913cfd8dfSPedro Giffuni        if user != '':
13013cfd8dfSPedro Giffuni            if dbg:
131bc42e70bSAriel Constenla-Haile                out.write('Logging in, username of %s\n' % user)
13213cfd8dfSPedro Giffuni            self.server.login(user, password)
13313cfd8dfSPedro Giffuni
13413cfd8dfSPedro Giffuni        for listener in self.listeners:
13513cfd8dfSPedro Giffuni            listener.connected(self.notify)
13613cfd8dfSPedro Giffuni    def disconnect(self):
13713cfd8dfSPedro Giffuni        if dbg:
138*4861d9f0Smseidel            out.write("PyMailSMTPService disconnect\n")
13913cfd8dfSPedro Giffuni        if self.server:
14013cfd8dfSPedro Giffuni            self.server.quit()
14113cfd8dfSPedro Giffuni            self.server = None
14213cfd8dfSPedro Giffuni        for listener in self.listeners:
14313cfd8dfSPedro Giffuni            listener.disconnected(self.notify)
14413cfd8dfSPedro Giffuni    def isConnected(self):
14513cfd8dfSPedro Giffuni        if dbg:
146*4861d9f0Smseidel            out.write("PyMailSMTPService isConnected\n")
14713cfd8dfSPedro Giffuni        return self.server != None
14813cfd8dfSPedro Giffuni    def getCurrentConnectionContext(self):
14913cfd8dfSPedro Giffuni        if dbg:
150*4861d9f0Smseidel            out.write("PyMailSMTPService getCurrentConnectionContext\n")
15113cfd8dfSPedro Giffuni        return self.connectioncontext
15213cfd8dfSPedro Giffuni    def sendMailMessage(self, xMailMessage):
15313cfd8dfSPedro Giffuni        COMMASPACE = ', '
15413cfd8dfSPedro Giffuni
15513cfd8dfSPedro Giffuni        if dbg:
156*4861d9f0Smseidel            out.write("PyMailSMTPService sendMailMessage\n")
15713cfd8dfSPedro Giffuni        recipients = xMailMessage.getRecipients()
15813cfd8dfSPedro Giffuni        sendermail = xMailMessage.SenderAddress
15913cfd8dfSPedro Giffuni        sendername = xMailMessage.SenderName
16013cfd8dfSPedro Giffuni        subject = xMailMessage.Subject
16113cfd8dfSPedro Giffuni        ccrecipients = xMailMessage.getCcRecipients()
16213cfd8dfSPedro Giffuni        bccrecipients = xMailMessage.getBccRecipients()
16313cfd8dfSPedro Giffuni        if dbg:
164*4861d9f0Smseidel            out.write("PyMailSMTPService subject %s\n" % subject)
165*4861d9f0Smseidel            out.write("PyMailSMTPService from %s\n" % sendername.encode('utf-8'))
166bc42e70bSAriel Constenla-Haile            out.write("PyMailSMTPService from %s\n" % sendermail)
167*4861d9f0Smseidel            out.write("PyMailSMTPService send to %s\n" % str(recipients))
16813cfd8dfSPedro Giffuni
16913cfd8dfSPedro Giffuni        attachments = xMailMessage.getAttachments()
17013cfd8dfSPedro Giffuni
17113cfd8dfSPedro Giffuni        textmsg = Message()
17213cfd8dfSPedro Giffuni
17313cfd8dfSPedro Giffuni        content = xMailMessage.Body
17413cfd8dfSPedro Giffuni        flavors = content.getTransferDataFlavors()
17513cfd8dfSPedro Giffuni        if dbg:
176*4861d9f0Smseidel            out.write("PyMailSMTPService flavors len %d\n" % len(flavors))
17713cfd8dfSPedro Giffuni
17813cfd8dfSPedro Giffuni        #Use first flavor that's sane for an email body
17913cfd8dfSPedro Giffuni        for flavor in flavors:
18013cfd8dfSPedro Giffuni            if flavor.MimeType.find('text/html') != -1 or flavor.MimeType.find('text/plain') != -1:
18113cfd8dfSPedro Giffuni                if dbg:
182*4861d9f0Smseidel                    out.write("PyMailSMTPService mimetype is %s\n" % flavor.MimeType)
18313cfd8dfSPedro Giffuni                textbody = content.getTransferData(flavor)
18413cfd8dfSPedro Giffuni                try:
18513cfd8dfSPedro Giffuni                    textbody = textbody.value
18613cfd8dfSPedro Giffuni                except:
18713cfd8dfSPedro Giffuni                    pass
18813cfd8dfSPedro Giffuni                textbody = textbody.encode('utf-8')
18913cfd8dfSPedro Giffuni
19013cfd8dfSPedro Giffuni                if len(textbody):
19113cfd8dfSPedro Giffuni                    mimeEncoding = re.sub("charset=.*", "charset=UTF-8", flavor.MimeType)
19213cfd8dfSPedro Giffuni                    if mimeEncoding.find('charset=UTF-8') == -1:
19313cfd8dfSPedro Giffuni                        mimeEncoding = mimeEncoding + "; charset=UTF-8"
19413cfd8dfSPedro Giffuni                    textmsg['Content-Type'] = mimeEncoding
19513cfd8dfSPedro Giffuni                    textmsg['MIME-Version'] = '1.0'
19613cfd8dfSPedro Giffuni                    textmsg.set_payload(textbody)
19713cfd8dfSPedro Giffuni
19813cfd8dfSPedro Giffuni                break
19913cfd8dfSPedro Giffuni
20013cfd8dfSPedro Giffuni        if (len(attachments)):
20113cfd8dfSPedro Giffuni            msg = MIMEMultipart()
20213cfd8dfSPedro Giffuni            msg.epilogue = ''
20313cfd8dfSPedro Giffuni            msg.attach(textmsg)
20413cfd8dfSPedro Giffuni        else:
20513cfd8dfSPedro Giffuni            msg = textmsg
20613cfd8dfSPedro Giffuni
20713cfd8dfSPedro Giffuni        hdr = Header(sendername, 'utf-8')
20813cfd8dfSPedro Giffuni        hdr.append('<'+sendermail+'>','us-ascii')
20913cfd8dfSPedro Giffuni        msg['Subject'] = subject
21013cfd8dfSPedro Giffuni        msg['From'] = hdr
21113cfd8dfSPedro Giffuni        msg['To'] = COMMASPACE.join(recipients)
21213cfd8dfSPedro Giffuni        if len(ccrecipients):
21313cfd8dfSPedro Giffuni            msg['Cc'] = COMMASPACE.join(ccrecipients)
21413cfd8dfSPedro Giffuni        if xMailMessage.ReplyToAddress != '':
21513cfd8dfSPedro Giffuni            msg['Reply-To'] = xMailMessage.ReplyToAddress
21613cfd8dfSPedro Giffuni
217*4861d9f0Smseidel        mailerstring = "OpenOffice via Caolan's mailmerge component"
21813cfd8dfSPedro Giffuni        try:
21913cfd8dfSPedro Giffuni            ctx = uno.getComponentContext()
22013cfd8dfSPedro Giffuni            aConfigProvider = ctx.ServiceManager.createInstance("com.sun.star.configuration.ConfigurationProvider")
22113cfd8dfSPedro Giffuni            prop = uno.createUnoStruct('com.sun.star.beans.PropertyValue')
22213cfd8dfSPedro Giffuni            prop.Name = "nodepath"
22313cfd8dfSPedro Giffuni            prop.Value = "/org.openoffice.Setup/Product"
22413cfd8dfSPedro Giffuni            aSettings = aConfigProvider.createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess",
22513cfd8dfSPedro Giffuni                    (prop,))
22613cfd8dfSPedro Giffuni            mailerstring = aSettings.getByName("ooName") + " " + \
22713cfd8dfSPedro Giffuni                    aSettings.getByName("ooSetupVersion") + " via Caolan's mailmerge component"
22813cfd8dfSPedro Giffuni        except:
22913cfd8dfSPedro Giffuni            pass
23013cfd8dfSPedro Giffuni
23113cfd8dfSPedro Giffuni        msg['X-Mailer'] = mailerstring
23213cfd8dfSPedro Giffuni        msg['Date'] = formatdate(localtime=True)
23313cfd8dfSPedro Giffuni
23413cfd8dfSPedro Giffuni        for attachment in attachments:
23513cfd8dfSPedro Giffuni            content = attachment.Data
23613cfd8dfSPedro Giffuni            flavors = content.getTransferDataFlavors()
23713cfd8dfSPedro Giffuni            flavor = flavors[0]
23813cfd8dfSPedro Giffuni            ctype = flavor.MimeType
23913cfd8dfSPedro Giffuni            maintype, subtype = ctype.split('/', 1)
24013cfd8dfSPedro Giffuni            msgattachment = MIMEBase(maintype, subtype)
24113cfd8dfSPedro Giffuni            data = content.getTransferData(flavor)
24213cfd8dfSPedro Giffuni            msgattachment.set_payload(data)
24313cfd8dfSPedro Giffuni            Encoders.encode_base64(msgattachment)
24413cfd8dfSPedro Giffuni            fname = attachment.ReadableName
24513cfd8dfSPedro Giffuni            try:
24613cfd8dfSPedro Giffuni                fname.encode('ascii')
24713cfd8dfSPedro Giffuni            except:
24813cfd8dfSPedro Giffuni                fname = ('utf-8','',fname.encode('utf-8'))
24913cfd8dfSPedro Giffuni            msgattachment.add_header('Content-Disposition', 'attachment', \
25013cfd8dfSPedro Giffuni                    filename=fname)
25113cfd8dfSPedro Giffuni            msg.attach(msgattachment)
25213cfd8dfSPedro Giffuni
25313cfd8dfSPedro Giffuni        uniquer = {}
25413cfd8dfSPedro Giffuni        for key in recipients:
25513cfd8dfSPedro Giffuni            uniquer[key] = True
25613cfd8dfSPedro Giffuni        if len(ccrecipients):
25713cfd8dfSPedro Giffuni            for key in ccrecipients:
25813cfd8dfSPedro Giffuni                uniquer[key] = True
25913cfd8dfSPedro Giffuni        if len(bccrecipients):
26013cfd8dfSPedro Giffuni            for key in bccrecipients:
26113cfd8dfSPedro Giffuni                uniquer[key] = True
2627d9fa7c3SPedro Giffuni        truerecipients = list(uniquer.keys())
26313cfd8dfSPedro Giffuni
26413cfd8dfSPedro Giffuni        if dbg:
265*4861d9f0Smseidel            out.write("PyMailSMTPService recipients are %s\n" % str(truerecipients))
26613cfd8dfSPedro Giffuni
26713cfd8dfSPedro Giffuni        self.server.sendmail(sendermail, truerecipients, msg.as_string())
268cdf0e10cSrcweir
269cdf0e10cSrcweirclass PyMailIMAPService(unohelper.Base, XMailService):
27013cfd8dfSPedro Giffuni    def __init__( self, ctx ):
27113cfd8dfSPedro Giffuni        self.ctx = ctx
27213cfd8dfSPedro Giffuni        self.listeners = []
27313cfd8dfSPedro Giffuni        self.supportedtypes = ('Insecure', 'Ssl')
27413cfd8dfSPedro Giffuni        self.server = None
27513cfd8dfSPedro Giffuni        self.connectioncontext = None
27613cfd8dfSPedro Giffuni        self.notify = EventObject(self)
27713cfd8dfSPedro Giffuni        if dbg:
278bc42e70bSAriel Constenla-Haile            out.write("PyMailIMAPService init\n")
27913cfd8dfSPedro Giffuni    def addConnectionListener(self, xListener):
28013cfd8dfSPedro Giffuni        if dbg:
281bc42e70bSAriel Constenla-Haile            out.write("PyMailIMAPService addConnectionListener\n")
28213cfd8dfSPedro Giffuni        self.listeners.append(xListener)
28313cfd8dfSPedro Giffuni    def removeConnectionListener(self, xListener):
28413cfd8dfSPedro Giffuni        if dbg:
285bc42e70bSAriel Constenla-Haile            out.write("PyMailIMAPService removeConnectionListener\n")
28613cfd8dfSPedro Giffuni        self.listeners.remove(xListener)
28713cfd8dfSPedro Giffuni    def getSupportedConnectionTypes(self):
28813cfd8dfSPedro Giffuni        if dbg:
289bc42e70bSAriel Constenla-Haile            out.write("PyMailIMAPService getSupportedConnectionTypes\n")
29013cfd8dfSPedro Giffuni        return self.supportedtypes
29113cfd8dfSPedro Giffuni    def connect(self, xConnectionContext, xAuthenticator):
29213cfd8dfSPedro Giffuni        if dbg:
293bc42e70bSAriel Constenla-Haile            out.write("PyMailIMAPService connect\n")
29413cfd8dfSPedro Giffuni
29513cfd8dfSPedro Giffuni        self.connectioncontext = xConnectionContext
29613cfd8dfSPedro Giffuni        server = xConnectionContext.getValueByName("ServerName")
29713cfd8dfSPedro Giffuni        if dbg:
298bc42e70bSAriel Constenla-Haile            out.write("Server: %s\n" % server)
29913cfd8dfSPedro Giffuni        port = xConnectionContext.getValueByName("Port")
30013cfd8dfSPedro Giffuni        if dbg:
301bc42e70bSAriel Constenla-Haile            out.write("Port: %d\n" % port)
30213cfd8dfSPedro Giffuni        connectiontype = xConnectionContext.getValueByName("ConnectionType")
30313cfd8dfSPedro Giffuni        if dbg:
304bc42e70bSAriel Constenla-Haile            out.write("Connection type: %s\n" % connectiontype)
305bc42e70bSAriel Constenla-Haile        out.write("BEFORE\n")
30613cfd8dfSPedro Giffuni        if connectiontype.upper() == 'SSL':
30713cfd8dfSPedro Giffuni            self.server = imaplib.IMAP4_SSL(server, port)
30813cfd8dfSPedro Giffuni        else:
30913cfd8dfSPedro Giffuni            self.server = imaplib.IMAP4(server, port)
310bc42e70bSAriel Constenla-Haile        out.write("AFTER\n")
31113cfd8dfSPedro Giffuni
31213cfd8dfSPedro Giffuni        user = xAuthenticator.getUserName().encode('ascii')
31313cfd8dfSPedro Giffuni        password = xAuthenticator.getPassword().encode('ascii')
31413cfd8dfSPedro Giffuni        if user != '':
31513cfd8dfSPedro Giffuni            if dbg:
316bc42e70bSAriel Constenla-Haile                out.write('Logging in, username of %s\n' % user)
31713cfd8dfSPedro Giffuni            self.server.login(user, password)
31813cfd8dfSPedro Giffuni
31913cfd8dfSPedro Giffuni        for listener in self.listeners:
32013cfd8dfSPedro Giffuni            listener.connected(self.notify)
32113cfd8dfSPedro Giffuni    def disconnect(self):
32213cfd8dfSPedro Giffuni        if dbg:
323bc42e70bSAriel Constenla-Haile            out.write("PyMailIMAPService disconnect\n")
32413cfd8dfSPedro Giffuni        if self.server:
32513cfd8dfSPedro Giffuni            self.server.logout()
32613cfd8dfSPedro Giffuni            self.server = None
32713cfd8dfSPedro Giffuni        for listener in self.listeners:
32813cfd8dfSPedro Giffuni            listener.disconnected(self.notify)
32913cfd8dfSPedro Giffuni    def isConnected(self):
33013cfd8dfSPedro Giffuni        if dbg:
331bc42e70bSAriel Constenla-Haile            out.write("PyMailIMAPService isConnected\n")
33213cfd8dfSPedro Giffuni        return self.server != None
33313cfd8dfSPedro Giffuni    def getCurrentConnectionContext(self):
33413cfd8dfSPedro Giffuni        if dbg:
335bc42e70bSAriel Constenla-Haile            out.write("PyMailIMAPService getCurrentConnectionContext\n")
33613cfd8dfSPedro Giffuni        return self.connectioncontext
337cdf0e10cSrcweir
338cdf0e10cSrcweirclass PyMailPOP3Service(unohelper.Base, XMailService):
33913cfd8dfSPedro Giffuni    def __init__( self, ctx ):
34013cfd8dfSPedro Giffuni        self.ctx = ctx
34113cfd8dfSPedro Giffuni        self.listeners = []
34213cfd8dfSPedro Giffuni        self.supportedtypes = ('Insecure', 'Ssl')
34313cfd8dfSPedro Giffuni        self.server = None
34413cfd8dfSPedro Giffuni        self.connectioncontext = None
34513cfd8dfSPedro Giffuni        self.notify = EventObject(self)
34613cfd8dfSPedro Giffuni        if dbg:
347bc42e70bSAriel Constenla-Haile            out.write("PyMailPOP3Service init\n")
34813cfd8dfSPedro Giffuni    def addConnectionListener(self, xListener):
34913cfd8dfSPedro Giffuni        if dbg:
350bc42e70bSAriel Constenla-Haile            out.write("PyMailPOP3Service addConnectionListener\n")
35113cfd8dfSPedro Giffuni        self.listeners.append(xListener)
35213cfd8dfSPedro Giffuni    def removeConnectionListener(self, xListener):
35313cfd8dfSPedro Giffuni        if dbg:
354bc42e70bSAriel Constenla-Haile            out.write("PyMailPOP3Service removeConnectionListener\n")
35513cfd8dfSPedro Giffuni        self.listeners.remove(xListener)
35613cfd8dfSPedro Giffuni    def getSupportedConnectionTypes(self):
35713cfd8dfSPedro Giffuni        if dbg:
358bc42e70bSAriel Constenla-Haile            out.write("PyMailPOP3Service getSupportedConnectionTypes\n")
35913cfd8dfSPedro Giffuni        return self.supportedtypes
36013cfd8dfSPedro Giffuni    def connect(self, xConnectionContext, xAuthenticator):
36113cfd8dfSPedro Giffuni        if dbg:
362bc42e70bSAriel Constenla-Haile            out.write("PyMailPOP3Service connect\n")
36313cfd8dfSPedro Giffuni
36413cfd8dfSPedro Giffuni        self.connectioncontext = xConnectionContext
36513cfd8dfSPedro Giffuni        server = xConnectionContext.getValueByName("ServerName")
36613cfd8dfSPedro Giffuni        if dbg:
367bc42e70bSAriel Constenla-Haile            out.write("Server: %s\n" % server)
36813cfd8dfSPedro Giffuni        port = xConnectionContext.getValueByName("Port")
36913cfd8dfSPedro Giffuni        if dbg:
370bc42e70bSAriel Constenla-Haile            out.write("Port: %s\n" % port)
37113cfd8dfSPedro Giffuni        connectiontype = xConnectionContext.getValueByName("ConnectionType")
37213cfd8dfSPedro Giffuni        if dbg:
373bc42e70bSAriel Constenla-Haile            out.write("Connection type: %s\n" % str(connectiontype))
374bc42e70bSAriel Constenla-Haile        out.write("BEFORE\n")
37513cfd8dfSPedro Giffuni        if connectiontype.upper() == 'SSL':
37613cfd8dfSPedro Giffuni            self.server = poplib.POP3_SSL(server, port)
37713cfd8dfSPedro Giffuni        else:
37813cfd8dfSPedro Giffuni            tout = xConnectionContext.getValueByName("Timeout")
37913cfd8dfSPedro Giffuni            if dbg:
380bc42e70bSAriel Constenla-Haile                out.write("Timeout is instance of int? %s\n" % isinstance(tout,int))
38113cfd8dfSPedro Giffuni            if not isinstance(tout,int):
38213cfd8dfSPedro Giffuni                tout = _GLOBAL_DEFAULT_TIMEOUT
38313cfd8dfSPedro Giffuni            if dbg:
384bc42e70bSAriel Constenla-Haile                out.write("Timeout: %s\n" % str(tout))
38513cfd8dfSPedro Giffuni            self.server = poplib.POP3(server, port, timeout=tout)
386bc42e70bSAriel Constenla-Haile        out.write("AFTER\n")
38713cfd8dfSPedro Giffuni
38813cfd8dfSPedro Giffuni        user = xAuthenticator.getUserName().encode('ascii')
38913cfd8dfSPedro Giffuni        password = xAuthenticator.getPassword().encode('ascii')
39013cfd8dfSPedro Giffuni        if dbg:
391bc42e70bSAriel Constenla-Haile            out.write('Logging in, username of %s\n' % user)
39213cfd8dfSPedro Giffuni        self.server.user(user)
39313cfd8dfSPedro Giffuni        self.server.pass_(password)
39413cfd8dfSPedro Giffuni
39513cfd8dfSPedro Giffuni        for listener in self.listeners:
39613cfd8dfSPedro Giffuni            listener.connected(self.notify)
39713cfd8dfSPedro Giffuni    def disconnect(self):
39813cfd8dfSPedro Giffuni        if dbg:
399bc42e70bSAriel Constenla-Haile            out.write("PyMailPOP3Service disconnect\n")
40013cfd8dfSPedro Giffuni        if self.server:
40113cfd8dfSPedro Giffuni            self.server.quit()
40213cfd8dfSPedro Giffuni            self.server = None
40313cfd8dfSPedro Giffuni        for listener in self.listeners:
40413cfd8dfSPedro Giffuni            listener.disconnected(self.notify)
40513cfd8dfSPedro Giffuni    def isConnected(self):
40613cfd8dfSPedro Giffuni        if dbg:
407bc42e70bSAriel Constenla-Haile            out.write("PyMailPOP3Service isConnected\n")
40813cfd8dfSPedro Giffuni        return self.server != None
40913cfd8dfSPedro Giffuni    def getCurrentConnectionContext(self):
41013cfd8dfSPedro Giffuni        if dbg:
411bc42e70bSAriel Constenla-Haile            out.write("PyMailPOP3Service getCurrentConnectionContext\n")
41213cfd8dfSPedro Giffuni        return self.connectioncontext
413cdf0e10cSrcweir
414cdf0e10cSrcweirclass PyMailServiceProvider(unohelper.Base, XMailServiceProvider):
41513cfd8dfSPedro Giffuni    def __init__( self, ctx ):
41613cfd8dfSPedro Giffuni        if dbg:
417bc42e70bSAriel Constenla-Haile            out.write("PyMailServiceProvider init\n")
41813cfd8dfSPedro Giffuni        self.ctx = ctx
41913cfd8dfSPedro Giffuni    def create(self, aType):
42013cfd8dfSPedro Giffuni        if dbg:
421bc42e70bSAriel Constenla-Haile            out.write("PyMailServiceProvider create with %s\n" % aType)
42213cfd8dfSPedro Giffuni        if aType == SMTP:
42313cfd8dfSPedro Giffuni            return PyMailSMTPService(self.ctx);
42413cfd8dfSPedro Giffuni        elif aType == POP3:
42513cfd8dfSPedro Giffuni            return PyMailPOP3Service(self.ctx);
42613cfd8dfSPedro Giffuni        elif aType == IMAP:
42713cfd8dfSPedro Giffuni            return PyMailIMAPService(self.ctx);
42813cfd8dfSPedro Giffuni        else:
429bc42e70bSAriel Constenla-Haile            out.write("PyMailServiceProvider, unknown TYPE %s\n" % aType)
430cdf0e10cSrcweir
431cdf0e10cSrcweirclass PyMailMessage(unohelper.Base, XMailMessage):
43213cfd8dfSPedro Giffuni    def __init__( self, ctx, sTo='', sFrom='', Subject='', Body=None, aMailAttachment=None ):
43313cfd8dfSPedro Giffuni        if dbg:
434bc42e70bSAriel Constenla-Haile            out.write("PyMailMessage init\n")
43513cfd8dfSPedro Giffuni        self.ctx = ctx
43613cfd8dfSPedro Giffuni
43713cfd8dfSPedro Giffuni        self.recipients = [sTo]
43813cfd8dfSPedro Giffuni        self.ccrecipients = []
43913cfd8dfSPedro Giffuni        self.bccrecipients = []
44013cfd8dfSPedro Giffuni        self.aMailAttachments = []
44113cfd8dfSPedro Giffuni        if aMailAttachment != None:
44213cfd8dfSPedro Giffuni            self.aMailAttachments.append(aMailAttachment)
44313cfd8dfSPedro Giffuni
44413cfd8dfSPedro Giffuni        self.SenderName, self.SenderAddress = parseaddr(sFrom)
44513cfd8dfSPedro Giffuni        self.ReplyToAddress = sFrom
44613cfd8dfSPedro Giffuni        self.Subject = Subject
44713cfd8dfSPedro Giffuni        self.Body = Body
44813cfd8dfSPedro Giffuni        if dbg:
449bc42e70bSAriel Constenla-Haile            out.write("post PyMailMessage init\n")
45013cfd8dfSPedro Giffuni    def addRecipient( self, recipient ):
45113cfd8dfSPedro Giffuni        if dbg:
452bc42e70bSAriel Constenla-Haile            out.write("PyMailMessage.addRecipient%s\n" % recipient)
45313cfd8dfSPedro Giffuni        self.recipients.append(recipient)
45413cfd8dfSPedro Giffuni    def addCcRecipient( self, ccrecipient ):
45513cfd8dfSPedro Giffuni        if dbg:
456bc42e70bSAriel Constenla-Haile            out.write("PyMailMessage.addCcRecipient%s\n" % ccrecipient)
45713cfd8dfSPedro Giffuni        self.ccrecipients.append(ccrecipient)
45813cfd8dfSPedro Giffuni    def addBccRecipient( self, bccrecipient ):
45913cfd8dfSPedro Giffuni        if dbg:
460bc42e70bSAriel Constenla-Haile            out.write("PyMailMessage.addBccRecipient%s\n" % bccrecipient)
46113cfd8dfSPedro Giffuni        self.bccrecipients.append(bccrecipient)
46213cfd8dfSPedro Giffuni    def getRecipients( self ):
46313cfd8dfSPedro Giffuni        if dbg:
464bc42e70bSAriel Constenla-Haile            out.write("PyMailMessage.getRecipients%s\n" % self.recipients)
46513cfd8dfSPedro Giffuni        return tuple(self.recipients)
46613cfd8dfSPedro Giffuni    def getCcRecipients( self ):
46713cfd8dfSPedro Giffuni        if dbg:
468bc42e70bSAriel Constenla-Haile            out.write("PyMailMessage.getCcRecipients%s\n" % self.ccrecipients)
46913cfd8dfSPedro Giffuni        return tuple(self.ccrecipients)
47013cfd8dfSPedro Giffuni    def getBccRecipients( self ):
47113cfd8dfSPedro Giffuni        if dbg:
472bc42e70bSAriel Constenla-Haile            out.write("PyMailMessage.getBccRecipients%s\n" % self.bccrecipients)
47313cfd8dfSPedro Giffuni        return tuple(self.bccrecipients)
47413cfd8dfSPedro Giffuni    def addAttachment( self, aMailAttachment ):
47513cfd8dfSPedro Giffuni        if dbg:
476bc42e70bSAriel Constenla-Haile            out.write("PyMailMessage.addAttachment\n")
47713cfd8dfSPedro Giffuni        self.aMailAttachments.append(aMailAttachment)
47813cfd8dfSPedro Giffuni    def getAttachments( self ):
47913cfd8dfSPedro Giffuni        if dbg:
480bc42e70bSAriel Constenla-Haile            out.write("PyMailMessage.getAttachments\n")
48113cfd8dfSPedro Giffuni        return tuple(self.aMailAttachments)
482bb7facceSAriel Constenla-Haile
483cdf0e10cSrcweir
484cdf0e10cSrcweir# pythonloader looks for a static g_ImplementationHelper variable
485cdf0e10cSrcweirg_ImplementationHelper = unohelper.ImplementationHelper()
486cdf0e10cSrcweirg_ImplementationHelper.addImplementation( \
48713cfd8dfSPedro Giffuni        PyMailServiceProvider, "org.openoffice.pyuno.MailServiceProvider",
48813cfd8dfSPedro Giffuni                ("com.sun.star.mail.MailServiceProvider",),)
489cdf0e10cSrcweirg_ImplementationHelper.addImplementation( \
49013cfd8dfSPedro Giffuni        PyMailMessage, "org.openoffice.pyuno.MailMessage",
49113cfd8dfSPedro Giffuni                ("com.sun.star.mail.MailMessage",),)
492