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

https://github.com/google/end-to-end · JavaScript · 137 lines · 67 code · 23 blank · 47 comment · 9 complexity · cd27bb3b2d616a066cecae2ecaeba38b 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. * @return {constants.Actions} The requested PGP action.
  63. */
  64. utils.getPgpAction = function(content) {
  65. if (/^-----BEGIN PGP (?:SIGNED )?MESSAGE-----/.test(content)) {
  66. return constants.Actions.DECRYPT_VERIFY;
  67. }
  68. if (/^-----BEGIN PGP PUBLIC KEY BLOCK-----/.test(content)) {
  69. return constants.Actions.IMPORT_KEY;
  70. }
  71. if (/^-----BEGIN PGP PRIVATE KEY BLOCK-----/.test(content)) {
  72. return constants.Actions.IMPORT_KEY;
  73. }
  74. return constants.Actions.ENCRYPT_SIGN;
  75. };
  76. /**
  77. * Extract a valid e-mail address from 'user id <email>' string. If no
  78. * valid e-mail address can be extracted, returns null. Uses
  79. * {@link goog.format.EmailAddress}, but also enforces stricter rules.
  80. * @param {string} recipient "username <email>" string.
  81. * @return {?string} Valid email address or null
  82. */
  83. utils.extractValidEmail = function(recipient) {
  84. var emailAddress = goog.format.EmailAddress.parse(recipient);
  85. if (!emailAddress.isValid()) {
  86. return null;
  87. }
  88. var email = emailAddress.getAddress();
  89. if (!constants.EMAIL_ADDRESS_REGEXP.exec(emailAddress.getAddress())) {
  90. return null;
  91. }
  92. return email;
  93. };
  94. /**
  95. * Checks whether a URI is an HTTPS ymail origin.
  96. * @param {!string} uri
  97. * @return {boolean}
  98. */
  99. utils.isYmailOrigin = function(uri) {
  100. var googUri = new goog.Uri(uri);
  101. return (googUri.getScheme() === 'https' &&
  102. goog.string.endsWith(googUri.getDomain(), '.mail.yahoo.com'));
  103. };
  104. /**
  105. * Checks whether a URI is an HTTPS Gmail origin.
  106. * @param {!string} uri
  107. * @return {boolean}
  108. */
  109. utils.isGmailOrigin = function(uri) {
  110. var googUri = new goog.Uri(uri);
  111. return (googUri.getScheme() === 'https' &&
  112. googUri.getDomain() === 'mail.google.com');
  113. };
  114. }); // goog.scope