Package svnmailer :: Package notifier :: Module mail

[hide private]

# Source Code for Module svnmailer.notifier.mail

      1  # -*- coding: utf-8 -*-
      2  #
      3  # Copyright 2004-2006 André Malo or his licensors, as applicable
      4  #
      5  # Licensed under the Apache License, Version 2.0 (the "License");
      6  # you may not use this file except in compliance with the License.
      7  # You may obtain a copy of the License at
      8  #
      9  #     http://www.apache.org/licenses/LICENSE-2.0
     10  #
     11  # Unless required by applicable law or agreed to in writing, software
     12  # distributed under the License is distributed on an "AS IS" BASIS,
     13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  # See the License for the specific language governing permissions and
     15  # limitations under the License.
     16  """
     17  Text based email notifiers (either piped to a program or via SMTP)
     18  """
     19  __author__    = "André Malo"
     20  __docformat__ = "epytext en"
     21  __all__       = ['getNotifier']
     22
     23
     24 -def getNotifier(config, groupset):
     25      """ Returns an initialized notifier or nothing
     26
     27          @param config: The svnmailer config
     28          @type config: C{svnmailer.settings.Settings}
     29
     30          @param groupset: The groupset to process
     31          @type groupset: C{list}
     32
     33          @return: The list of notifiers (containing 0 or 1 member)
     34          @rtype: C{list}
     35      """
     36      from svnmailer import settings
     37      from svnmailer.notifier import _textmail, _multimail
     38
     39      cls = None
     40      if config.general.sendmail_command:
     41          cls = SendmailSubmitter
     42      elif config.general.smtp_host:
     43          cls = SMTPSubmitter
     44
     45      if cls:
     46          mtype = (groupset.groups[0].mail_type or u'single').split()[0].lower()
     47          is_commit = (config.runtime.mode == settings.modes.commit)
     48          mod = (is_commit and mtype == u'multipart') and \
     49              _multimail or _textmail
     50          return mod.getNotifier(cls, config, groupset)
     51
     52      return []
     53
     54
     55 -class SMTPSubmitter(object):
     56      """ Use SMTP to submit the mail """
     57      _settings = None
     58
     59 -    def sendMail(self, sender, to_addr, mail):
     60          """ Sends the mail via SMTP """
     61          import smtplib, cStringIO
     62
     63          fp = cStringIO.StringIO()
     64          mail.dump(fp)
     65          mail = fp.getvalue()
     66          fp.close()
     67
     68          general = self._settings.general
     69          conn = smtplib.SMTP(general.smtp_host)
     70          if general.smtp_user:
     71              conn.login(general.smtp_user, general.smtp_pass)
     72
     73          conn.sendmail(sender, to_addr, mail)
     74          conn.quit()
     75
     76
     77 -class SendmailSubmitter(object):
     78      """ Pipe all stuff to a mailer """
     79      _settings = None
     80
     81 -    def sendMail(self, sender, to_addr, mail):
     82          """ Sends the mail via a piped mailer """
     83          from svnmailer import util
     84
     85          pipe = util.getPipe2(self._getMailCommand(sender, to_addr))
     86          pipe.fromchild.close() # we don't expect something
     87          mail.dump(pipe.tochild)
     88          pipe.tochild.close()
     89
     90          # what do we do with the return code?
     91          pipe.wait()
     92
     93
     94 -    def _getMailCommand(self, sender, to_addr):
     95          """ Returns the mailer command
     96
     97              The command is created using sendmail conventions.
     98              If you want another commandline, override this method.
     99
    100              @param sender: The sender address
    101              @type sender: C{str}
    102
    103              @param to_addr: The receivers
    104              @type to_addr: C{list}
    105
    106              @return: The command
    107              @rtype: C{list}
    108          """
    109          cmd = list(self._settings.general.sendmail_command)
    110          cmd[1:] = [(isinstance(arg, unicode) and
    111              [arg.encode("utf-8")] or [arg])[0] for arg in cmd[1:]
    112          ]
    113          cmd.extend(['-f', sender])
    114          cmd.extend(to_addr)
    115
    116          return cmd
    117