home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / openoffice / program / mailmerge.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-04-29  |  13.2 KB  |  426 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. import unohelper
  5. import uno
  6. from com.sun.star.mail import XMailServiceProvider
  7. from com.sun.star.mail import XMailService
  8. from com.sun.star.mail import XSmtpService
  9. from com.sun.star.mail import XConnectionListener
  10. from com.sun.star.mail import XAuthenticator
  11. from com.sun.star.mail import XMailMessage
  12. from com.sun.star.mail.MailServiceType import SMTP
  13. from com.sun.star.mail.MailServiceType import POP3
  14. from com.sun.star.mail.MailServiceType import IMAP
  15. from com.sun.star.uno import XCurrentContext
  16. from com.sun.star.lang import IllegalArgumentException
  17. from com.sun.star.lang import EventObject
  18. from com.sun.star.mail import SendMailMessageFailedException
  19. from email.MIMEBase import MIMEBase
  20. from email.Message import Message
  21. from email import Encoders
  22. from email.MIMEMultipart import MIMEMultipart
  23. import sys
  24. import smtplib
  25. import imaplib
  26. import poplib
  27. dbg = False
  28.  
  29. class PyMailSMTPService(unohelper.Base, XSmtpService):
  30.     
  31.     def __init__(self, ctx):
  32.         self.ctx = ctx
  33.         self.listeners = []
  34.         self.supportedtypes = ('Insecure', 'Ssl')
  35.         self.server = None
  36.         self.connectioncontext = None
  37.         self.notify = EventObject()
  38.         if dbg:
  39.             print >>sys.stderr, 'PyMailSMPTService init'
  40.         
  41.  
  42.     
  43.     def addConnectionListener(self, xListener):
  44.         if dbg:
  45.             print >>sys.stderr, 'PyMailSMPTService addConnectionListener'
  46.         
  47.         self.listeners.append(xListener)
  48.  
  49.     
  50.     def removeConnectionListener(self, xListener):
  51.         if dbg:
  52.             print >>sys.stderr, 'PyMailSMPTService removeConnectionListener'
  53.         
  54.         self.listeners.remove(xListener)
  55.  
  56.     
  57.     def getSupportedConnectionTypes(self):
  58.         if dbg:
  59.             print >>sys.stderr, 'PyMailSMPTService getSupportedConnectionTypes'
  60.         
  61.         return self.supportedtypes
  62.  
  63.     
  64.     def connect(self, xConnectionContext, xAuthenticator):
  65.         self.connectioncontext = xConnectionContext
  66.         if dbg:
  67.             print >>sys.stderr, 'PyMailSMPTService connect'
  68.         
  69.         server = xConnectionContext.getValueByName('ServerName')
  70.         if dbg:
  71.             print >>sys.stderr, server
  72.         
  73.         port = xConnectionContext.getValueByName('Port')
  74.         if dbg:
  75.             print >>sys.stderr, port
  76.         
  77.         self.server = smtplib.SMTP(server, port)
  78.         if dbg:
  79.             self.server.set_debuglevel(1)
  80.         
  81.         connectiontype = xConnectionContext.getValueByName('ConnectionType')
  82.         if dbg:
  83.             print >>sys.stderr, connectiontype
  84.         
  85.         if connectiontype == 'Ssl':
  86.             self.server.starttls()
  87.         
  88.         user = xAuthenticator.getUserName()
  89.         password = xAuthenticator.getPassword()
  90.         if user != '':
  91.             if dbg:
  92.                 print >>sys.stderr, 'Logging in, username of', user
  93.             
  94.             self.server.login(user, password)
  95.         
  96.         for listener in self.listeners:
  97.             listener.connected(self.notify)
  98.         
  99.  
  100.     
  101.     def disconnect(self):
  102.         if dbg:
  103.             print >>sys.stderr, 'PyMailSMPTService disconnect'
  104.         
  105.         if self.server:
  106.             self.server.quit()
  107.             self.server = None
  108.         
  109.         for listener in self.listeners:
  110.             listener.disconnected(self.notify)
  111.         
  112.  
  113.     
  114.     def isConnected(self):
  115.         if dbg:
  116.             print >>sys.stderr, 'PyMailSMPTService isConnected'
  117.         
  118.         return self.server != None
  119.  
  120.     
  121.     def getCurrentConnectionContext(self):
  122.         if dbg:
  123.             print >>sys.stderr, 'PyMailSMPTService getCurrentConnectionContext'
  124.         
  125.         return self.connectioncontext
  126.  
  127.     
  128.     def sendMailMessage(self, xMailMessage):
  129.         COMMASPACE = ', '
  130.         if dbg:
  131.             print >>sys.stderr, 'PyMailSMPTService sendMailMessage'
  132.         
  133.         recipients = xMailMessage.getRecipients()
  134.         sender = xMailMessage.SenderAddress
  135.         subject = xMailMessage.Subject
  136.         ccrecipients = xMailMessage.getCcRecipients()
  137.         bccrecipients = xMailMessage.getBccRecipients()
  138.         if dbg:
  139.             print >>sys.stderr, 'PyMailSMPTService subject', subject
  140.             print >>sys.stderr, 'PyMailSMPTService from', sender
  141.             print >>sys.stderr, 'PyMailSMPTService send to', recipients
  142.         
  143.         attachments = xMailMessage.getAttachments()
  144.         content = xMailMessage.Body
  145.         flavors = content.getTransferDataFlavors()
  146.         flavor = flavors[0]
  147.         if dbg:
  148.             print >>sys.stderr, 'PyMailSMPTService mimetype is', flavor.MimeType
  149.         
  150.         textbody = content.getTransferData(flavor)
  151.         textmsg = Message()
  152.         textmsg['Content-Type'] = flavor.MimeType
  153.         textmsg['MIME-Version'] = '1.0'
  154.         textmsg.set_payload(textbody.encode('utf-8'))
  155.         if len(attachments):
  156.             msg = MIMEMultipart()
  157.             msg.epilogue = ''
  158.             msg.attach(textmsg)
  159.         else:
  160.             msg = textmsg
  161.         msg['Subject'] = subject
  162.         msg['From'] = sender
  163.         msg['To'] = COMMASPACE.join(recipients)
  164.         if len(ccrecipients):
  165.             msg['Cc'] = COMMASPACE.join(ccrecipients)
  166.         
  167.         if xMailMessage.ReplyToAddress != '':
  168.             msg['Reply-To'] = xMailMessage.ReplyToAddress
  169.         
  170.         msg['X-Mailer'] = "OpenOffice.org 2.0 via Caolan's mailmerge component"
  171.         for attachment in attachments:
  172.             content = attachment.Data
  173.             flavors = content.getTransferDataFlavors()
  174.             flavor = flavors[0]
  175.             ctype = flavor.MimeType
  176.             (maintype, subtype) = ctype.split('/', 1)
  177.             msgattachment = MIMEBase(maintype, subtype)
  178.             data = content.getTransferData(flavor)
  179.             msgattachment.set_payload(data)
  180.             Encoders.encode_base64(msgattachment)
  181.             msgattachment.add_header('Content-Disposition', 'attachment', filename = attachment.ReadableName)
  182.             msg.attach(msgattachment)
  183.         
  184.         uniquer = { }
  185.         for key in recipients:
  186.             uniquer[key] = True
  187.         
  188.         if len(ccrecipients):
  189.             for key in ccrecipients:
  190.                 uniquer[key] = True
  191.             
  192.         
  193.         if len(bccrecipients):
  194.             for key in bccrecipients:
  195.                 uniquer[key] = True
  196.             
  197.         
  198.         truerecipients = uniquer.keys()
  199.         if dbg:
  200.             print >>sys.stderr, 'PyMailSMPTService recipients are', truerecipients
  201.         
  202.         self.server.sendmail(sender, recipients, msg.as_string())
  203.  
  204.  
  205.  
  206. class PyMailIMAPService(unohelper.Base, XMailService):
  207.     
  208.     def __init__(self, ctx):
  209.         self.ctx = ctx
  210.         self.listeners = []
  211.         self.supportedtypes = ('Insecure', 'Ssl')
  212.         self.server = None
  213.         self.connectioncontext = None
  214.         if dbg:
  215.             print >>sys.stderr, 'PyMailIMAPService init'
  216.         
  217.  
  218.     
  219.     def addConnectionListener(self, xListener):
  220.         if dbg:
  221.             print >>sys.stderr, 'PyMailIMAPService addConnectionListener'
  222.         
  223.         self.listeners.append(xListener)
  224.  
  225.     
  226.     def removeConnectionListener(self, xListener):
  227.         if dbg:
  228.             print >>sys.stderr, 'PyMailIMAPService removeConnectionListener'
  229.         
  230.         self.listeners.remove(xListener)
  231.  
  232.     
  233.     def getSupportedConnectionTypes(self):
  234.         if dbg:
  235.             print >>sys.stderr, 'PyMailIMAPService getSupportedConnectionTypes'
  236.         
  237.         return self.supportedtypes
  238.  
  239.     
  240.     def connect(self, xConnectionContext, xAuthenticator):
  241.         if dbg:
  242.             print >>sys.stderr, 'PyMailIMAPService connect'
  243.         
  244.         self.connectioncontext = xConnectionContext
  245.         server = xConnectionContext.getValueByName('ServerName')
  246.         if dbg:
  247.             print >>sys.stderr, server
  248.         
  249.         port = xConnectionContext.getValueByName('Port')
  250.         if dbg:
  251.             print >>sys.stderr, port
  252.         
  253.         connectiontype = xConnectionContext.getValueByName('ConnectionType')
  254.         if dbg:
  255.             print >>sys.stderr, connectiontype
  256.         
  257.         print >>sys.stderr, 'BEFORE'
  258.         if connectiontype == 'Ssl':
  259.             self.server = imaplib.IMAP4_SSL(server, port)
  260.         else:
  261.             self.server = imaplib.IMAP4(server, port)
  262.         print >>sys.stderr, 'AFTER'
  263.         user = xAuthenticator.getUserName()
  264.         password = xAuthenticator.getPassword()
  265.         if user != '':
  266.             if dbg:
  267.                 print >>sys.stderr, 'Logging in, username of', user
  268.             
  269.             self.server.login(user, password)
  270.         
  271.         for listener in self.listeners:
  272.             listener.connected(self.notify)
  273.         
  274.  
  275.     
  276.     def disconnect(self):
  277.         if dbg:
  278.             print >>sys.stderr, 'PyMailIMAPService disconnect'
  279.         
  280.         if self.server:
  281.             self.server.logout()
  282.             self.server = None
  283.         
  284.         for listener in self.listeners:
  285.             listener.disconnected(self.notify)
  286.         
  287.  
  288.     
  289.     def isConnected(self):
  290.         if dbg:
  291.             print >>sys.stderr, 'PyMailIMAPService isConnected'
  292.         
  293.         return self.server != None
  294.  
  295.     
  296.     def getCurrentConnectionContext(self):
  297.         if dbg:
  298.             print >>sys.stderr, 'PyMailIMAPService getCurrentConnectionContext'
  299.         
  300.         return self.connectioncontext
  301.  
  302.  
  303.  
  304. class PyMailPOP3Service(unohelper.Base, XMailService):
  305.     
  306.     def __init__(self, ctx):
  307.         self.ctx = ctx
  308.         self.listeners = []
  309.         self.supportedtypes = ('Insecure', 'Ssl')
  310.         self.server = None
  311.         self.connectioncontext = None
  312.         if dbg:
  313.             print >>sys.stderr, 'PyMailPOP3Service init'
  314.         
  315.  
  316.     
  317.     def addConnectionListener(self, xListener):
  318.         if dbg:
  319.             print >>sys.stderr, 'PyMailPOP3Service addConnectionListener'
  320.         
  321.         self.listeners.append(xListener)
  322.  
  323.     
  324.     def removeConnectionListener(self, xListener):
  325.         if dbg:
  326.             print >>sys.stderr, 'PyMailPOP3Service removeConnectionListener'
  327.         
  328.         self.listeners.remove(xListener)
  329.  
  330.     
  331.     def getSupportedConnectionTypes(self):
  332.         if dbg:
  333.             print >>sys.stderr, 'PyMailPOP3Service getSupportedConnectionTypes'
  334.         
  335.         return self.supportedtypes
  336.  
  337.     
  338.     def connect(self, xConnectionContext, xAuthenticator):
  339.         if dbg:
  340.             print >>sys.stderr, 'PyMailPOP3Service connect'
  341.         
  342.         self.connectioncontext = xConnectionContext
  343.         server = xConnectionContext.getValueByName('ServerName')
  344.         if dbg:
  345.             print >>sys.stderr, server
  346.         
  347.         port = xConnectionContext.getValueByName('Port')
  348.         if dbg:
  349.             print >>sys.stderr, port
  350.         
  351.         connectiontype = xConnectionContext.getValueByName('ConnectionType')
  352.         if dbg:
  353.             print >>sys.stderr, connectiontype
  354.         
  355.         print >>sys.stderr, 'BEFORE'
  356.         if connectiontype == 'Ssl':
  357.             self.server = poplib.POP3_SSL(server, port)
  358.         else:
  359.             self.server = poplib.POP3(server, port)
  360.         print >>sys.stderr, 'AFTER'
  361.         user = xAuthenticator.getUserName()
  362.         password = xAuthenticator.getPassword()
  363.         if dbg:
  364.             print >>sys.stderr, 'Logging in, username of', user
  365.         
  366.         self.server.user(user)
  367.         self.server.pass_(user, password)
  368.         for listener in self.listeners:
  369.             listener.connected(self.notify)
  370.         
  371.  
  372.     
  373.     def disconnect(self):
  374.         if dbg:
  375.             print >>sys.stderr, 'PyMailPOP3Service disconnect'
  376.         
  377.         if self.server:
  378.             self.server.quit()
  379.             self.server = None
  380.         
  381.         for listener in self.listeners:
  382.             listener.disconnected(self.notify)
  383.         
  384.  
  385.     
  386.     def isConnected(self):
  387.         if dbg:
  388.             print >>sys.stderr, 'PyMailPOP3Service isConnected'
  389.         
  390.         return self.server != None
  391.  
  392.     
  393.     def getCurrentConnectionContext(self):
  394.         if dbg:
  395.             print >>sys.stderr, 'PyMailPOP3Service getCurrentConnectionContext'
  396.         
  397.         return self.connectioncontext
  398.  
  399.  
  400.  
  401. class PyMailServiceProvider(unohelper.Base, XMailServiceProvider):
  402.     
  403.     def __init__(self, ctx):
  404.         if dbg:
  405.             print >>sys.stderr, 'PyMailServiceProvider init'
  406.         
  407.         self.ctx = ctx
  408.  
  409.     
  410.     def create(self, aType):
  411.         if dbg:
  412.             print >>sys.stderr, 'PyMailServiceProvider create with', aType
  413.         
  414.         if aType == SMTP:
  415.             return PyMailSMTPService(self.ctx)
  416.         elif aType == POP3:
  417.             return PyMailPOP3Service(self.ctx)
  418.         elif aType == IMAP:
  419.             return PyMailIMAPService(self.ctx)
  420.         else:
  421.             print >>sys.stderr, 'PyMailServiceProvider, unknown TYPE', aType
  422.  
  423.  
  424. g_ImplementationHelper = unohelper.ImplementationHelper()
  425. g_ImplementationHelper.addImplementation(PyMailServiceProvider, 'org.openoffice.pyuno.MailServiceProvider', ('com.sun.star.mail.MailServiceProvider',))
  426.