<commit_before>/**
 * Mailvelope - secure email with OpenPGP encryption for Webmail
 * Copyright (C) 2012-2015 Mailvelope GmbH
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Parts of the editor are based on Hoodiecrow (MIT License)
 * Copyright (c) 2014 Whiteout Networks GmbH.
 * See https://github.com/tanx/hoodiecrow/blob/master/LICENSE
 */

/**
 * @fileOverview This file implements the interface for encrypting and
 * signing user data in an sandboxed environment that is secured from
 * the webmail interface.
 */

'use strict';

var mvelo = mvelo || null;

/**
 * Angular controller for the editor UI.
 */
mvelo.Editor = function($scope, $timeout, $q) {
  this.setGlobal($scope, $timeout); // share globals in legacy closure code
  this.registerEventListeners(); // listen to incoming events
  this.initComplete(); // emit event to backend that editor has initialized

  /**
   * Verifies a recipient after input, gets their key, colors the
   * input tag accordingly and checks if encryption is possible.
   * @param  {Object} recipient   The recipient object
   */
  $scope.verify = function(recipient) {
    if (!recipient) {
      return;
    }

    if (recipient.email) {
      // display only email address after autocomplete
      recipient.displayId = recipient.email;
    } else {
      // set address after manual input
      recipient.email = recipient.displayId;
    }

    $scope.getKey(recipient);
    $scope.colorTag(recipient);
    $scope.checkEncryptStatus();
  };

  /**
   * Finds the recipient's corresponding public key and sets it
   * on the 'key' attribute on the recipient object.
   * @param  {Object} recipient   The recipient object
   * @return {Object}             The key object (undefined if none found)
   */
  $scope.getKey = function(recipient) {
    recipient.key = $scope.keys.find(function(key) {
      return key.email === recipient.email;
    });
    return recipient.key;
  };

  /**
   * Uses jQuery to color the recipient's input tag depending on
   * whether they have a key or not.
   * @param  {Object} recipient   The recipient object
   */
  $scope.colorTag = function(recipient) {
    $timeout(function() { // wait for html tag to appear
      $('tags-input li.tag-item').each(function() {
        if ($(this).text().indexOf(recipient.email) === -1) {
          return;
        }
        if (recipient.key) {
          $(this).addClass('tag-success');
        } else {
          $(this).addClass('tag-warning');
        }
      });
    });
  };

  /**
   * Checks if all recipients have a public key and prevents encryption
   * if one of them does not have a key.
   */
  $scope.checkEncryptStatus = function() {
    $scope.noEncrypt = $scope.recipients.some(function(r) { return !r.key; });
  };

  /**
   * Queries the local cache of key objects to find a matching user ID
   * @param  {String} query   The autocomplete query
   * @return {Array}          A list of filtered items that match the query
   */
  $scope.autocomplete = function(query) {
    var cache = $scope.keys.map(function(key) {
      return {
        email: key.email,
        displayId: key.userid
      };
    });
    return cache.filter(function(i) {
      return i.displayId.toLowerCase().indexOf(query.toLowerCase()) !== -1;
    });
  };
};

mvelo.Editor.prototype = Object.create(mvelo.EventHandler.prototype); // add event api

if (typeof angular !== 'undefined') { // do not use angular in unit tests
  var module = angular.module('editor', ['ngTagsInput']); // load module dependencies
  module.controller('EditorCtrl', mvelo.Editor); // attach controller to editor module
}


//
// Legacy code is contained in a closure and needs to be refactored to use angular
//


(function() {

  if (typeof angular !== 'undefined') { // do not init in unit tests
    angular.element(document).ready(init); // do manual angular bootstraping after init
  }

  /**
   * Register the event handlers for the editor.
   */
  mvelo.Editor.prototype.registerEventListeners = function() {
    this.on('public-key-userids', this.setRecipients);
    this.on('set-text', onSetText);
    this.on('set-init-data', this._onSetInitData);
    this.on('set-attachment', this._onSetAttachment);
    this.on('decrypt-in-progress', this.showWaitingModal);
    this.on('encrypt-in-progress', this.showWaitingModal);
    this.on('decrypt-end', this.hideWaitingModal);
    this.on('encrypt-end', this.hideWaitingModal);
    this.on('encrypt-failed', this.hideWaitingModal);
    this.on('decrypt-failed', this._decryptFailed);
    this.on('show-pwd-dialog', this._onShowPwdDialog);
    this.on('hide-pwd-dialog', hidePwdDialog);<commit_msg>Fix jshint error<commit_after>/**
 * Mailvelope - secure email with OpenPGP encryption for Webmail
 * Copyright (C) 2012-2015 Mailvelope GmbH
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Parts of the editor are based on Hoodiecrow (MIT License)
 * Copyright (c) 2014 Whiteout Networks GmbH.
 * See https://github.com/tanx/hoodiecrow/blob/master/LICENSE
 */

/**
 * @fileOverview This file implements the interface for encrypting and
 * signing user data in an sandboxed environment that is secured from
 * the webmail interface.
 */

'use strict';

var mvelo = mvelo || null;

/**
 * Angular controller for the editor UI.
 */
mvelo.Editor = function($scope, $timeout, $q) {
  this.setGlobal($scope, $timeout); // share globals in legacy closure code
  this.registerEventListeners(); // listen to incoming events
  this.initComplete(); // emit event to backend that editor has initialized

  /**
   * Verifies a recipient after input, gets their key, colors the
   * input tag accordingly and checks if encryption is possible.
   * @param  {Object} recipient   The recipient object
   */
  $scope.verify = function(recipient) {
    if (!recipient) {
      return;
    }

    if (recipient.email) {
      // display only email address after autocomplete
      recipient.displayId = recipient.email;
    } else {
      // set address after manual input
      recipient.email = recipient.displayId;
    }

    $scope.getKey(recipient);
    $scope.colorTag(recipient);
    $scope.checkEncryptStatus();
  };

  /**
   * Finds the recipient's corresponding public key and sets it
   * on the 'key' attribute on the recipient object.
   * @param  {Object} recipient   The recipient object
   * @return {Object}             The key object (undefined if none found)
   */
  $scope.getKey = function(recipient) {
    recipient.key = $scope.keys.find(function(key) {
      return key.email === recipient.email;
    });
    return recipient.key;
  };

  /**
   * Uses jQuery to color the recipient's input tag depending on
   * whether they have a key or not.
   * @param  {Object} recipient   The recipient object
   */
  $scope.colorTag = function(recipient) {
    $timeout(function() { // wait for html tag to appear
      $('tags-input li.tag-item').each(function() {
        if ($(this).text().indexOf(recipient.email) === -1) {
          return;
        }
        if (recipient.key) {
          $(this).addClass('tag-success');
        } else {
          $(this).addClass('tag-warning');
        }
      });
    });
  };

  /**
   * Checks if all recipients have a public key and prevents encryption
   * if one of them does not have a key.
   */
  $scope.checkEncryptStatus = function() {
    $scope.noEncrypt = $scope.recipients.some(function(r) { return !r.key; });
  };

  /**
   * Queries the local cache of key objects to find a matching user ID
   * @param  {String} query   The autocomplete query
   * @return {Array}          A list of filtered items that match the query
   */
  $scope.autocomplete = function(query) {
    var cache = $scope.keys.map(function(key) {
      return {
        email: key.email,
        displayId: key.userid
      };
    });
    return cache.filter(function(i) {
      return i.displayId.toLowerCase().indexOf(query.toLowerCase()) !== -1;
    });
  };
};

mvelo.Editor.prototype = Object.create(mvelo.EventHandler.prototype); // add event api

if (typeof angular !== 'undefined') { // do not use angular in unit tests
  var ngModule = angular.module('editor', ['ngTagsInput']); // load module dependencies
  ngModule.controller('EditorCtrl', mvelo.Editor); // attach controller to editor module
}


//
// Legacy code is contained in a closure and needs to be refactored to use angular
//


(function() {

  if (typeof angular !== 'undefined') { // do not init in unit tests
    angular.element(document).ready(init); // do manual angular bootstraping after init
  }

  /**
   * Register the event handlers for the editor.
   */
  mvelo.Editor.prototype.registerEventListeners = function() {
    this.on('public-key-userids', this.setRecipients);
    this.on('set-text', onSetText);
    this.on('set-init-data', this._onSetInitData);
    this.on('set-attachment', this._onSetAttachment);
    this.on('decrypt-in-progress', this.showWaitingModal);
    this.on('encrypt-in-progress', this.showWaitingModal);
    this.on('decrypt-end', this.hideWaitingModal);
    this.on('encrypt-end', this.hideWaitingModal);
    this.on('encrypt-failed', this.hideWaitingModal);
    this.on('decrypt-failed', this._decryptFailed);
    this.on('show-pwd-dialog', this._onShowPwdDialog);
    this.on('hide-pwd-dialog', hidePwdDialog);
{'fraction_non_alphanumeric': 0.12002684306394401, 'fraction_numerical': 0.0028760425654299686, 'mean_word_length': 3.0861731296513906, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '4708128', 'n_tokens_mistral': 3071, 'n_tokens_neox': 2982, 'n_words': 973}