 #{t 'compose forward to'} #{addresses}
 
 """
-                message.to = []
-                message.cc = []
-                message.bcc = []
-                message.subject = """
-                    #{t 'compose forward prefix'}#{inReplyTo.get 'subject'}
-                    """
-                message.text = separator + text
-                htmlSeparator = separator.replace /(\n)+/g, '<br />'
-                html = """
-#{style}
+        textSeparator = separator.replace('&lt;', '<').replace('&gt;', '>')
+        htmlSeparator = separator.replace /(\n)+/g, '<br />'
+
+        @setMessageAsDefault message
+        message.subject = """
+            #{t 'compose forward prefix'}#{inReplyTo.get 'subject'}
+            """
+        message.text = textSeparator + text
+        message.html = """
+#{COMPOSE_STYLE}
 
 <p>#{htmlSeparator}</p><p><br /></p>#{html}
 """
-                message.html = html
-
-                # Add original message attachments
-                message.attachments = inReplyTo.get 'attachments'
-
-            when null
-                message.to      = []
-                message.cc      = []
-                message.bcc     = []
-                message.subject = ''
-                message.text    = ''
-                message.html    = ''
-
-        # remove my address from dests
-        notMe = (dest) -> return dest.address isnt myAddress
-        message.to = message.to.filter notMe
-        message.cc = message.cc.filter notMe
+        message.attachments = inReplyTo.get 'attachments'
+
         return message
 
+
+
+    # Clear all fields of the message object.
+    setMessageAsDefault: (message) ->
+        message.to = []
+        message.cc = []
+        message.bcc = []
+        message.subject = ''
+        message.text = ''
+        message.html = ''
+        return message
+
+
+    # Generate reply text by adding `>` before each line of the given text.
     generateReplyText: (text) ->
         text = text.split '\n'
         res  = []

MessageActionCreator      = require '../actions/message_action_creator'
LayoutActionCreator       = require '../actions/layout_action_creator'

module.exports = MessageUtils =


    displayAddress: (address, full = false) ->
        if full
            if address.name? and address.name isnt ""

            message.references = inReplyTo.get('references') or []
            message.references = message.references.concat message.inReplyTo

        options = {
            message
            inReplyTo
            dateHuman
            sender
            text
            html
        }

        switch action

            when ComposeActions.REPLY
                @setMessageAsReply options

            when ComposeActions.REPLY_ALL
                @setMessageAsReplyAll options

            when ComposeActions.FORWARD
                @setMessageAsForward options

            when null
                @setMessgeAsDefault message

        # remove my address from dests
        notMe = (dest) -> return dest.address isnt myAddress
        message.to = message.to.filter notMe
        message.cc = message.cc.filter notMe
        return message


    # Build message to display in composer in case of a reply to a message:
    # * set subject automatically (Re: previous subject)
    # * Set recipient based on sender
    # * add a style header for proper display
    # * Add quote of the previous message at the beginning of the message
    setMessageAsReply: (options) ->
        {
            message
            inReplyTo
            dateHuman
            sender
            text
            html
        } = options

        params = date: dateHuman, sender: sender
        separator = t 'compose reply separator', params
        message.to = @getReplyToAddress inReplyTo
        message.cc = []
        message.bcc = []
        message.subject = @getReplySubject inReplyTo
        message.text = separator + @generateReplyText(text) + "\n"
        message.html = """
            #{COMPOSE_STYLE}
            <p>#{separator}<span class="originalToggle"> … </span></p>
            <blockquote style="#{QUOTE_STYLE}">#{html}</blockquote>
            <p><br /></p>
            """

    # Build message to display in composer in case of a reply to all message:
    # * set subject automatically (Re: previous subject)
    # * Set recipients based on all people set in the conversation.
    # * add a style header for proper display
    # * Add quote of the previous message at the beginning of the message
    setMessageAsReplyAll: (options) ->
        {
            message
            inReplyTo
            dateHuman
            sender
            text
            html
        } = options

        params = date: dateHuman, sender: sender
        separator = t 'compose reply separator', params
        message.to = @getReplyToAddress inReplyTo
        # filter to don't have same address twice
        toAddresses = message.to.map (dest) -> return dest.address

        message.cc = [].concat(
            inReplyTo.get('from'),
            inReplyTo.get('to'),
            inReplyTo.get('cc')
        ).filter (dest) ->
            return dest? and toAddresses.indexOf(dest.address) is -1
        message.bcc = []

        message.subject = @getReplySubject inReplyTo
        message.text = separator + @generateReplyText(text) + "\n"
        message.html = """
            #{COMPOSE_STYLE}
            <p>#{separator}<span class="originalToggle"> … </span></p>
            <blockquote style="#{QUOTE_STYLE}">#{html}</blockquote>
            <p><br /></p>
            """

    # Build message to display in composer in case of a message forwarding:
    # * set subject automatically (fwd: previous subject)
    # * add a style header for proper display
    # * Add forward information at the beginning of the message
    setMessageAsForward: (options) ->
        {
            message
            inReplyTo
            dateHuman
            sender
            text
            html
        } = options

        addresses = inReplyTo.get('to')
        .map (address) -> address.address
        .join ', '

        senderInfos = @getReplyToAddress inReplyTo
        senderName = ""

        senderAddress =
        if senderInfos.length > 0
            senderName = senderInfos[0].name
            senderAddress = senderInfos[0].address

        if senderName.length > 0
            fromField = "#{senderName} &lt;#{senderAddress}&gt;"
        else
            fromField = senderAddress

        separator = """

----- #{t 'compose forward header'} ------
#{t 'compose forward subject'} #{inReplyTo.get 'subject'}
