PageRenderTime 79ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/javascript/crypto/e2e/extension/utils/text.js

https://gitlab.com/reyjmc03/end-to-end
JavaScript | 143 lines | 70 code | 24 blank | 49 comment | 10 complexity | 495da861393cafdbc5b41d7538ad2e3c MD5 | raw file
  1. /**
  2. * @license
  3. * Copyright 2014 Google Inc. All rights reserved.
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * @fileoverview Utility methods for working with free-text and PGP messages.
  18. */
  19. goog.provide('e2e.ext.utils.text');
  20. goog.require('e2e.ext.constants');
  21. goog.require('e2e.ext.constants.Actions');
  22. goog.require('goog.Uri');
  23. goog.require('goog.array');
  24. goog.require('goog.format.EmailAddress');
  25. goog.require('goog.string');
  26. goog.require('goog.string.format');
  27. goog.scope(function() {
  28. var constants = e2e.ext.constants;
  29. var utils = e2e.ext.utils.text;
  30. /**
  31. * Formats a body of text such that all lines do not exceed a given length.
  32. * @param {string} str The body of text to wrap around.
  33. * @param {number} maxlen The maximum length of a line.
  34. * @return {string} The formatted text.
  35. */
  36. utils.prettyTextWrap = function(str, maxlen) {
  37. var regexp = new RegExp(goog.string.format('(.{%d})', maxlen), 'g');
  38. str = str.trim().replace(regexp, '$1\n');
  39. var carry = '';
  40. var lines = goog.array.map(str.split('\n'), function(line) {
  41. var newline = goog.string.format('%s%s', carry, line).trim();
  42. carry = '';
  43. if (newline.length >= maxlen && !goog.string.endsWith(newline, ' ')) {
  44. var lastSpace = newline.lastIndexOf(' ');
  45. if (lastSpace > -1 &&
  46. goog.string.isAlphaNumeric(newline.substring(newline.length - 1))) {
  47. carry = newline.substring(lastSpace + 1);
  48. return newline.substring(0, lastSpace);
  49. }
  50. }
  51. return newline;
  52. });
  53. if (carry) {
  54. goog.array.extend(lines, utils.prettyTextWrap(carry, maxlen).split('\n'));
  55. }
  56. return lines.join('\n');
  57. };
  58. /**
  59. * Determines the requested PGP action based on the content that the user has
  60. * selected.
  61. * @param {string} content The content that the user has selected.
  62. * @param {boolean=} opt_enableSniffing Optional. True if action sniffing is to
  63. * be enabled. Defaults to false.
  64. * @return {constants.Actions} The requested PGP action.
  65. */
  66. utils.getPgpAction = function(content, opt_enableSniffing) {
  67. if (!Boolean(opt_enableSniffing)) {
  68. return constants.Actions.USER_SPECIFIED;
  69. }
  70. if (/^-----BEGIN PGP MESSAGE-----/.test(content)) {
  71. return constants.Actions.DECRYPT_VERIFY;
  72. }
  73. if (/^-----BEGIN PGP PUBLIC KEY BLOCK-----/.test(content)) {
  74. return constants.Actions.IMPORT_KEY;
  75. }
  76. if (/^-----BEGIN PGP PRIVATE KEY BLOCK-----/.test(content)) {
  77. return constants.Actions.IMPORT_KEY;
  78. }
  79. return constants.Actions.ENCRYPT_SIGN;
  80. };
  81. /**
  82. * Extract a valid e-mail address from 'user id <email>' string. If no valid
  83. * e-mail address can be extracted, returns null. Uses
  84. * goog.format.EmailAddress, but also enforces stricter rules.
  85. * @param {string} recipient "username <email> string".
  86. * @return {?string} Valid email address or null
  87. */
  88. utils.extractValidEmail = function(recipient) {
  89. var emailAddress = goog.format.EmailAddress.parse(recipient);
  90. if (!emailAddress.isValid()) {
  91. return null;
  92. }
  93. var email = emailAddress.getAddress();
  94. if (!constants.EMAIL_ADDRESS_REGEXP.exec(emailAddress.getAddress())) {
  95. return null;
  96. }
  97. return email;
  98. };
  99. /**
  100. * Checks whether a URI is an HTTPS ymail origin.
  101. * @param {!string} uri
  102. * @return {boolean}
  103. */
  104. utils.isYmailOrigin = function(uri) {
  105. var googUri = new goog.Uri(uri);
  106. return (googUri.getScheme() === 'https' &&
  107. goog.string.endsWith(googUri.getDomain(), '.mail.yahoo.com'));
  108. };
  109. /**
  110. * Checks whether a URI is an HTTPS Gmail origin.
  111. * @param {!string} uri
  112. * @return {boolean}
  113. */
  114. utils.isGmailOrigin = function(uri) {
  115. var googUri = new goog.Uri(uri);
  116. return (googUri.getScheme() === 'https' &&
  117. googUri.getDomain() === 'mail.google.com');
  118. };
  119. }); // goog.scope