<commit_before>/* global $, _, singleMessage:true */
/* global phiQ, phiTitle, phiBar, setup, JAM, beforeSpace, FidoHTML */
/* will be used: GUI, nwClipboard */

singleMessage = function(echotag, parsedURL){ /* jshint indent:false */

var echobase;

var lcEchotag = echotag.toLowerCase();
var echoNames = setup.areas.group('EchoArea').names();
var foundNames = echoNames.filter(function(echoName){
   return echoName.toLowerCase() === lcEchotag;
});

if( foundNames.length === 0 ){
   return phiBar.reportErrorHTML([
      'Sorry, the echomail area <b>',
      echotag,
      '</b> is not found on the system.'
   ].join(''));
}

var setupEchotag = foundNames[0];
var echoPath = beforeSpace(
   setup.areas.group('EchoArea').first(setupEchotag)
);
echobase = JAM( echoPath );

var arrDesc = /-d "([^"]+?)"/.exec(
   setup.areas.group('EchoArea').first(setupEchotag)
);
var echoDesc;
if( arrDesc === null ){
   echoDesc = setupEchotag;
   phiTitle(echotag + ' - message');
} else {
   echoDesc = arrDesc[1];
   phiTitle(echoDesc + ' [' + echotag + '] messages');
}

var arrMSGID = parsedURL.optionalParams.filter(function(param){
   return param.name === 'msgid';
}).map(function(param){
   return param.value;
});

var outputMessageText = function($message, header, callback){
   echobase.decodeMessage(header, function(error, messageText){
      $message.find('.messageText').html(
         error ? _.escapeHTML('' + error) : FidoHTML.fromText(messageText)
      );
      callback();
   });
};

var outputSingleMessage = function(header, callback){
   var defaultAvatarSize = 140;
   var decoded = echobase.decodeHeader(header);
   header.decoded = decoded;
   var $curr = $(['<table class="table table-bordered table-condensed">',
      '<tr>',
         '<th rowspan=4 class="avatar inverse" width=1>',
            '<div style="width: '+defaultAvatarSize+'px;">',
               '&nbsp;',
            '</div>',
         '</th>',
         '<th class="inverse">Msg</th>',
         '<td colspan=3>',
            header.MessageIndex + ' of ' + echobase.size(),
         '</td>',
      '</tr>',
      '<tr>',
         '<th class="inverse">From</th>',
         '<td width="100%">',
            decoded.from || '',
         '</td>',
         '<td width=1>',
            decoded.origAddr || '<i class="fa fa-spinner fa-spin"></i>',
         '</td>',
         '<td>',
            '<nobr>',
               decoded.origTime[0], '-',
               _(decoded.origTime[1]).pad(2, '0'), '-',
               _(decoded.origTime[2]).pad(2, '0'),
            ' ',
               _(decoded.origTime[3]).pad(2, '0'), ':',
               _(decoded.origTime[4]).pad(2, '0'), ':',
               _(decoded.origTime[5]).pad(2, '0'),
            '</nobr>',
         '</td>',
      '</tr>',
      '<tr>',
         '<th class="inverse">To</th>',
         '<td>' + (decoded.to     ||'') + '</td>',
         '<td width=1>' + (decoded.toAddr ||'') + '</td>',
         '<td>',
            '<nobr>',
               decoded.procTime[0], '-',
               _(decoded.procTime[1]).pad(2, '0'), '-',
               _(decoded.procTime[2]).pad(2, '0'),
            ' ',
               _(decoded.procTime[3]).pad(2, '0'), ':',
               _(decoded.procTime[4]).pad(2, '0'), ':',
               _(decoded.procTime[5]).pad(2, '0'),
            '</nobr>',
         '</td>',
      '</tr>',
      '<tr>',
         '<th class="inverse">Subj</th>',
         '<td colspan=3>',
            decoded.subj || '',
         '</td>',
      '</tr>',
      '<tr>',
         '<td colspan=5 class="messageText">',
            '<p style="text-align: center;">',
               '<i class="fa fa-spinner fa-spin"></i>',
            '</p>',
         '</td>',
      '</tr>',
   '</table>'].join('')).appendTo('#content');
   $curr.find('.avatar').each(function(){
      var $avatar = $(this);

      var avatarSize;
      var height = $avatar.height();
      if ( height + 1 < defaultAvatarSize ){
         avatarSize = height + 1;
      } else {
         avatarSize = defaultAvatarSize;
      }
      $avatar.find('div').width( avatarSize );

      var avatars = echobase.getAvatarsForHeader(header, ['https', 'http'], {
         size: avatarSize
      });
      if( avatars.length < 1 ) avatars = [
         'https://secure.gravatar.com/avatar/?f=y&d=mm&s=' + avatarSize
      ];

      $avatar.css('background-image', 'url(' + avatars[0] + ')');
   });
   phiQ.push(function(qNext){
      outputMessageText($curr, header, qNext);
   });
   callback();
};

echobase.headersForMSGID(arrMSGID, function(err, headers){
   if( err ) return phiBar.reportErrorHTML( _.escapeHTML('' + err) );

   $('#content').empty();
   if( headers.length < 1 ){
      $('#content').html(<commit_msg>do not trust `decoded.origAddr` (use `.getOrigAddr` instead of it)<commit_after>/* global $, _, singleMessage:true */
/* global phiQ, phiTitle, phiBar, setup, JAM, beforeSpace, FidoHTML */
/* will be used: GUI, nwClipboard */

singleMessage = function(echotag, parsedURL){ /* jshint indent:false */

var echobase;

var lcEchotag = echotag.toLowerCase();
var echoNames = setup.areas.group('EchoArea').names();
var foundNames = echoNames.filter(function(echoName){
   return echoName.toLowerCase() === lcEchotag;
});

if( foundNames.length === 0 ){
   return phiBar.reportErrorHTML([
      'Sorry, the echomail area <b>',
      echotag,
      '</b> is not found on the system.'
   ].join(''));
}

var setupEchotag = foundNames[0];
var echoPath = beforeSpace(
   setup.areas.group('EchoArea').first(setupEchotag)
);
echobase = JAM( echoPath );

var arrDesc = /-d "([^"]+?)"/.exec(
   setup.areas.group('EchoArea').first(setupEchotag)
);
var echoDesc;
if( arrDesc === null ){
   echoDesc = setupEchotag;
   phiTitle(echotag + ' - message');
} else {
   echoDesc = arrDesc[1];
   phiTitle(echoDesc + ' [' + echotag + '] messages');
}

var arrMSGID = parsedURL.optionalParams.filter(function(param){
   return param.name === 'msgid';
}).map(function(param){
   return param.value;
});

var outputMessageText = function($message, header, callback){
   echobase.decodeMessage(header, function(error, messageText){
      $message.find('.messageText').html(
         error ? _.escapeHTML('' + error) : FidoHTML.fromText(messageText)
      );
      callback();
   });
};

var outputSingleMessage = function(header, callback){
   var defaultAvatarSize = 140;
   var decoded = echobase.decodeHeader(header);
   header.decoded = decoded;
   var $curr = $(['<table class="table table-bordered table-condensed">',
      '<tr>',
         '<th rowspan=4 class="avatar inverse" width=1>',
            '<div style="width: '+defaultAvatarSize+'px;">',
               '&nbsp;',
            '</div>',
         '</th>',
         '<th class="inverse">Msg</th>',
         '<td colspan=3>',
            header.MessageIndex + ' of ' + echobase.size(),
         '</td>',
      '</tr>',
      '<tr>',
         '<th class="inverse">From</th>',
         '<td width="100%">',
            decoded.from || '',
         '</td>',
         '<td width=1 class="origAddr">',
            '<i class="fa fa-spinner fa-spin"></i>',
         '</td>',
         '<td>',
            '<nobr>',
               decoded.origTime[0], '-',
               _(decoded.origTime[1]).pad(2, '0'), '-',
               _(decoded.origTime[2]).pad(2, '0'),
            ' ',
               _(decoded.origTime[3]).pad(2, '0'), ':',
               _(decoded.origTime[4]).pad(2, '0'), ':',
               _(decoded.origTime[5]).pad(2, '0'),
            '</nobr>',
         '</td>',
      '</tr>',
      '<tr>',
         '<th class="inverse">To</th>',
         '<td>' + (decoded.to     ||'') + '</td>',
         '<td width=1>' + (decoded.toAddr ||'') + '</td>',
         '<td>',
            '<nobr>',
               decoded.procTime[0], '-',
               _(decoded.procTime[1]).pad(2, '0'), '-',
               _(decoded.procTime[2]).pad(2, '0'),
            ' ',
               _(decoded.procTime[3]).pad(2, '0'), ':',
               _(decoded.procTime[4]).pad(2, '0'), ':',
               _(decoded.procTime[5]).pad(2, '0'),
            '</nobr>',
         '</td>',
      '</tr>',
      '<tr>',
         '<th class="inverse">Subj</th>',
         '<td colspan=3>',
            decoded.subj || '',
         '</td>',
      '</tr>',
      '<tr>',
         '<td colspan=5 class="messageText">',
            '<p style="text-align: center;">',
               '<i class="fa fa-spinner fa-spin"></i>',
            '</p>',
         '</td>',
      '</tr>',
   '</table>'].join('')).appendTo('#content');

   echobase.getOrigAddr(header, function(err, origAddr){
      if( err ){
         $curr.find('.origAddr').html('ERROR');
         origAddr = void 0;
      } else {
         $curr.find('.origAddr').html( _.escapeHTML(origAddr) );
      }

      $curr.find('.avatar').each(function(){
         var $avatar = $(this);

         var avatarSize;
         var height = $avatar.height();
         if ( height + 1 < defaultAvatarSize ){
            avatarSize = height + 1;
         } else {
            avatarSize = defaultAvatarSize;
         }
         $avatar.find('div').width( avatarSize );

         var avatars = echobase.getAvatarsForHeader(
            header, ['https', 'http'], {
               size: avatarSize,
               origAddr: origAddr
         });
         if( avatars.length < 1 ) avatars = [
            'https://secure.gravatar.com/avatar/?f=y&d=mm&s=' + avatarSize
         ];

         $avatar.css('background-image', 'url(' + avatars[0] + ')');
      });
   });
   phiQ.push(function(qNext){
      outputMessageText($curr, header, qNext);
   });
   callback();
};

echobase.headersForMSGID(arrMSGID, function(err, headers){
   if( err ) return phiBar.reportErrorHTML( _.escapeHTML('' + err) );

   $('#content').empty();
   if( headers.length < 1 ){
      $('#content').html(
{'fraction_non_alphanumeric': 0.20818107998375965, 'fraction_numerical': 0.010860738936256598, 'mean_word_length': 2.24967018469657, 'pattern_counts': {'":': 0, '<': 117, '<?xml version=': 0, '>': 111, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1503238', 'n_tokens_mistral': 3282, 'n_tokens_neox': 3355, 'n_words': 586}