<div id="message-{{mid}}" class="thread-message has-mid {% for tid in metadata.tag_tids %}{% if result.data.tags[tid].slug == "new" %}{{result.data.tags[tid].slug}}{% endif %}{% endfor %}" data-mid="{{mid}}">
  {% set from = metadata.from %}
  {% set url_danger = metadata.flags.spam %}
  {% include("partials/thread_metadata.html") %}
  {% set encryption_status = [message.crypto.encryption.status] %}
  {% set signature_status = [message.crypto.signature.status] %}
  {% set special_text_parts = [] %}
  <div class="thread-message-body">
{#### If no text_part, show html_part }
    {% if not message.text_parts %}
      {% for part in message.html_parts %}
      {% if part.data %}
      Yay, I am some part.data: {{part.data}}
      <iframe class="message-part-html" onload="mailpile.thread_html_iframe(this)" sandbox="allow-popups allow-popups-to-escape-sandbox allow-top-navigation" seamless target="_blank" srcdoc="{{part.data}}"></iframe>
      {% else %}
      <div class="message-part-text"><em>{{_("Message content is empty")}}</em></div>
      {% endif %}
      {% endfor %}
    {% endif
#####}
    {% if message.text_parts %}
      {% set last_part = message.text_parts|length - 1 %}
      {% for part in message.text_parts %}
        {# Parts that do not have own crypto attribute inherit from message itself
         # Watch for changes to the "status" which is present in each encryption and
         # signature section - if either has changed, then we have moved from
         # one security context to the next, and need to let the user know.
         #}
        {% if part.data %}
          {% set part_encryption = (part.crypto and part.crypto.encryption or message.crypto.encryption) %}
          {% set part_signature = (part.crypto and part.crypto.signature or message.crypto.signature) %}
          {% if part_encryption or part_signature %}
          <div class="message-inline-crypto clearfix">
              {% if part_encryption %}
                {% if encryption_status and part_encryption.status != encryption_status[-1] %}
                {% set encryption = show_text_part_encryption(part_encryption.status) %}
                <span class="message-inline-crypto-info" title="{{ encryption.text }}" data-crypto_color="{{ encryption.color }}" data-crypto_icon="{{ encryption.icon }}" data-crypto_message="{{ encryption.message }}">
                  <span class="icon {{ encryption.color + ' ' + encryption.icon }}"></span> <span class="text {{ encryption.color }}">{{ encryption.text }}</span>
                </span>
                {% endif %}
                {% do encryption_status.append(part_encryption.status) %}
              {% endif %}
              {% if part_signature %}
                {% if signature_status and part_signature.status != signature_status[-1] %}
                {% set signature = show_text_part_signature(part_signature.status) %}
                <span class="message-inline-crypto-info" title="{{ signature.text }}" data-crypto_color="{{ signature.color }}" data-crypto_icon="{{ signature.icon }}" data-crypto_message="{{ signature.message }}">
                  <span class="icon {{ signature.color + ' ' + signature.icon }}"></span> <span class="text {{ signature.color }}">{{ signature.text }}</span>
                </span>
                {% endif %}
                {% do signature_status.append(part_signature.status) %}
              {% endif %}
            <div class="message-inline-crypto-line left"></div>
          </div>
          {% endif %}
          {% autoescape false %}
          {% set part_text = part.data|nice_text|e|urlize|fix_urls(40, url_danger) %}
          {% if part.type in ("text", "pgpsignedtext") %}
            <div class="message-part-text">{{ part_text|to_br }}</div>
          {% elif part.type in ("pgptext",) %}
            {% if part.crypto.encryption.status in ("lockedkey", "missingkey", "error") %}
              {%- set failed_crypto = part.crypto.encryption %}
              {%- include("partials/thread_message_cryptofail.html") %}
            {% else %}
            <div class="message-part-text">{{ part_text|to_br }}</div>
            {% endif %}
          {% elif part.type in ("pgpbegin", "pgpend") %}
            <div class="message-part-{{part.type}} hide">{{part.data}}</div>
          {% elif part.type == "quote" %}
            {# If this is the 2nd-to-last part, and the LAST part is a quote
               or signature, we shall also hide. #}
            {%- if loop.index == (loop.length - 1) and
                   message.text_parts[last_part].type in ("signature", "quote") -%}
              {% do special_text_parts.append("quote") %}
              <div class="message-part-quote hide">{{ part_text|to_br }}</div>
            {# If this part is a quote at end of message, hide #}
            {% elif loop.last %}
              {% do special_text_parts.append("quote") %}
              <div class="message-part-quote hide">{{ part_text|to_br }}</div>
            {% else %}
              <div class="message-part-quote">{{ part_text|to_br }}</div>
            {%- endif -%}
          {% elif part.type == "signature" %}
            {% do special_text_parts.append("signature") %}
            <div class="message-part-signature hide">{{ part_text|to_br }}</div>
          {% else %}
            <div class="message-part-text"><em>{{_("Unknown Text Part")}}</em></div>
          {% endif %}
          {% endautoescape %}
        {% endif %}
      {% endfor %}
    {% else %}
      {% if message.crypto.encryption.status in ("lockedkey", "missingkey", "error") %}
        {%- set failed_crypto = message.crypto.encryption %}
        {%- include("partials/thread_message_cryptofail.html") %}
      {% elif not message.attachments %}
      <div class="message-part-text"><em>{{_("Message content is empty")}}</em></div>
      {% endif %}
    {% endif %}
    {% if message.attachments %}
    <div class="thread-message-attachments">
      <ul class="thread-message-attachments horizontal clearfix">
        {% for att in message.attachments %}
        <li class="left">
          {% set type = attachment_type(att.mimetype) %}
        {% if (att.filename) %}
          {% set filename = att.filename %}
        {% else %}
          {% set filename = _("Untitled") %}
        {% endif %}
        {% if att.mimetype in ["image/bmp", "image/gif", "image/jpg", "image/jpeg", "image/pjpeg", "image/svg+xml", "image/x-png", "image/png", "application/vnd.google-apps.photo"] %}
        <a href="{{ U('/message/download/=', mid, '/part:', att.count, '/') }}" class="attachment-image" title="{{filename}}" data-size="{{att.length|friendly_bytes}}" type="{{att.mimetype}}">
          <div class="preview" style="background-image: url('{{ U('/message/download/preview/=', mid, '/part:', att.count, '/') }}');"></div>
        </a>
        {% else %}
        <a href="{{ U('/message/download/=', mid, '/part:', att.count, '/') }}" class="attachment" title="{{filename}}" data-size="{{att.length|friendly_bytes}}">
          <div class="preview">
            <span class="icon-mime" type="{{att.mimetype}}"></span>
            {% set file_parts = filename.split(".") %}
            {% set file_parts_length = file_parts|length %}
            {% if filename != _("Untitled") %}
            <span class="extension">
              {{ file_parts[file_parts_length - 1] }}
            </span>
            {% endif %}
          </div>
          <div class="filename">
            {% if att.crypto.encryption.status == "decrypted" %}
            <span class="icon-lock-closed crypto-encrypted"></span>
            {% elif att.crypto.encryption.status == "encrypted" %}
            <span class="icon-lock-closed crypto-error"></span>
            {% endif %}
            {% if file_parts_length > 2 or filename|length > 20 %}
              {{ att.filename[0:15] }}...
            {% elif file_parts[0] %}
              {{ file_parts[0] }}
            {% endif %}
          </div>
        </a>
        {% endif %}
        </li>
        {% endfor %}
      </ul>
    </div>
  {% endif %}
  </div>
  <div class="message-actions clearfix">
    <ul class="message-actions horizontal left">
      {% if "quote" in special_text_parts or "signature" in special_text_parts %}
      <li class="action"><a href="#" class="message-actions-quote">&middot;&middot;&middot;&middot;</a></li>
      {% endif %}
      {% if not from.flags.contact and not from.flags.profile %}
      <li class="action"><a class="message-action-add-contact" href="#" data-name="{{from.fn}}" data-address="{{from.address}}"><span class="icon-user"></span> {{_("Add Contact")}}</a></li>
      {% endif %}
    </ul>
    <ul class="message-actions horizontal right">
      {% if show_reply %}
      <li class="action"><a class="message-action-reply-all" href="#"><span class="icon-reply-all"></span> {{_("Reply")}}</a></li>
      {% endif %}
      <li class="action"><a class="message-action-forward" href="#"><span class="icon-forward"></span> {{_("Forward")}}</a></li>
      <li class="dropdown action">
        <a class="dropdown-toggle" data-toggle="dropdown" href="#"><span class="icon-move"></span> {{_("Move")}}</a>
        <ul id="menu1" class="dropdown-menu dropdown-menu-right" role="menu" aria-labelledby="thread-message-move">
          <li role="presentation"><a class="message-action-archive" role="menuitem" tabindex="-1" href="#"><span class="icon-archive"></span> {{_("Archive Message")}}</a></li>
          <li role="presentation"><a class="message-action-inbox" role="menuitem" tabindex="-1" href="#"><span class="icon-inbox"></span> {{_("Move to Inbox")}}</a></li>
          <li role="presentation"><a class="message-action-spam" role="menuitem" tabindex="-1" href="#"><span class="icon-spam"></span> {{_("Flag as Spam")}}</a></li>
          {% if result.data.metadata|length > 1 %}
          <li role="presentation"><a class="message-action-unthread" role="menuitem" tabindex="-1" href="#"><span class="icon-circle-x"></span> {{_("Remove from Thread")}}</a></li>
          {% endif %}
          {% for header in message.header_list %}
          {% if 'List-unsubscribe' == header[0] %}
          <li role="presentation"><a class="message-action-unsubscribe" role="menuitem" tabindex="-1" data-unsubscribe="{{header[1]}}" href="#"><span class="icon-compose"></span> {{_("Unsubscribe")}}</a></li>
          {% endif %}
          {% endfor %}
        </ul>
      </li>
      <li class="action"><a class="message-action-trash" href="#"><span class="icon-trash"></span> {{_("Trash")}}</a></li>
    </ul>
  </div>
</div>
<script>
$(document).ready(function() {
  Mailpile.Message.AnalyzeMessageInline('{{mid}}');
  $('#message-{{mid}}').data('html', {{ message.html_parts|json|safe }});
});
</script>
