##
## Copyright (c) 2006 Jason Dillon
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
##     http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##

##
## $Id: message.py 79 2009-08-25 12:34:09Z mindwanderer $
##

from email.MIMEText import MIMEText
from email.header import Header

from p4spam import log, config
from p4spam.version import VERSION

##
## MessageBuffer
##

from cStringIO import StringIO

class MessageBuffer:
    NEWLINE = '\n'
    
    def __init__(this):
        this.buff = StringIO()
    
    def close(this):
        this.buff.close()
    
    def write(this, *args):
        for arg in args:
            this.buff.write(arg)
    
    def writeln(this, *args):
        for arg in args:
            this.buff.write(arg)
        this.buff.write(this.NEWLINE)
    
    def toString(this):
        return this.buff.getvalue()

##
## MessageBuilder
##

class MessageBuilder:
    def __init__(this, info):
        this.log = log.getLogger(this)
        this.info = info
        this.p4 = info.p4
        this.contentType = 'plain'
    
    def getTitle(this):
        return this.getSubject()
    
    def getSubject(this):
        change = this.info.getChange()
        comment = this.info.getComment()
        
        # Special handling of the subject regaurding newlines
        if config.SUBJECT_NEW_LINES == 'strip':
            comment = comment.replace("\n", ', ')
        elif config.SUBJECT_NEW_LINES == 'first':
            comment = comment.split("\n")[0]
        
        subject = config.SUBJECT_FORMAT % (change, comment)
        
        l = len(subject)
        m = config.MAX_SUBJECT_LINE_LENGTH
        if l > m:
            this.log.warning("Subject line longer than %s; truncating %s remaining characters" % (m, l - m))
            subject = subject[:m]
        
        return subject
    
    def writeDocument(this, buff):
        raise "Abstract method"
    
    def getPayload(this, recipient):
        buff = MessageBuffer()
        
        this.writeDocument(buff, recipient)
        
        payload = buff.toString()
        buff.close()
        
        return payload
        
    def getMessage(this, recipient):
        this.log.info("Building email message...")
        
        payload = this.getPayload(recipient)
        msg = MIMEText(payload, this.contentType, 'iso-8859-1')
        
        # Use email header class so that the header will look well on Outlook
        msg['Subject'] = Header(this.getSubject(), 'iso-8859-1')
        
        # Set X-Mailer to allow better filtering
        msg['X-Mailer'] = "P4Spam %s: http://p4spam.sourceforge.net" % VERSION
        
        return msg