Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

338 lines
8.8 KiB

  1. /*
  2. Copyright 2015 Kyle E. Mitchell
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. var parse = require('spdx-expression-parse')
  14. var spdxLicenseIds = require('spdx-license-ids')
  15. function valid (string) {
  16. try {
  17. parse(string)
  18. return true
  19. } catch (error) {
  20. return false
  21. }
  22. }
  23. // Common transpositions of license identifier acronyms
  24. var transpositions = [
  25. ['APGL', 'AGPL'],
  26. ['Gpl', 'GPL'],
  27. ['GLP', 'GPL'],
  28. ['APL', 'Apache'],
  29. ['ISD', 'ISC'],
  30. ['GLP', 'GPL'],
  31. ['IST', 'ISC'],
  32. ['Claude', 'Clause'],
  33. [' or later', '+'],
  34. [' International', ''],
  35. ['GNU', 'GPL'],
  36. ['GUN', 'GPL'],
  37. ['+', ''],
  38. ['GNU GPL', 'GPL'],
  39. ['GNU/GPL', 'GPL'],
  40. ['GNU GLP', 'GPL'],
  41. ['GNU General Public License', 'GPL'],
  42. ['Gnu public license', 'GPL'],
  43. ['GNU Public License', 'GPL'],
  44. ['GNU GENERAL PUBLIC LICENSE', 'GPL'],
  45. ['MTI', 'MIT'],
  46. ['Mozilla Public License', 'MPL'],
  47. ['WTH', 'WTF'],
  48. ['-License', '']
  49. ]
  50. var TRANSPOSED = 0
  51. var CORRECT = 1
  52. // Simple corrections to nearly valid identifiers.
  53. var transforms = [
  54. // e.g. 'mit'
  55. function (argument) {
  56. return argument.toUpperCase()
  57. },
  58. // e.g. 'MIT '
  59. function (argument) {
  60. return argument.trim()
  61. },
  62. // e.g. 'M.I.T.'
  63. function (argument) {
  64. return argument.replace(/\./g, '')
  65. },
  66. // e.g. 'Apache- 2.0'
  67. function (argument) {
  68. return argument.replace(/\s+/g, '')
  69. },
  70. // e.g. 'CC BY 4.0''
  71. function (argument) {
  72. return argument.replace(/\s+/g, '-')
  73. },
  74. // e.g. 'LGPLv2.1'
  75. function (argument) {
  76. return argument.replace('v', '-')
  77. },
  78. // e.g. 'Apache 2.0'
  79. function (argument) {
  80. return argument.replace(/,?\s*(\d)/, '-$1')
  81. },
  82. // e.g. 'GPL 2'
  83. function (argument) {
  84. return argument.replace(/,?\s*(\d)/, '-$1.0')
  85. },
  86. // e.g. 'Apache Version 2.0'
  87. function (argument) {
  88. return argument
  89. .replace(/,?\s*(V\.|v\.|V|v|Version|version)\s*(\d)/, '-$2')
  90. },
  91. // e.g. 'Apache Version 2'
  92. function (argument) {
  93. return argument
  94. .replace(/,?\s*(V\.|v\.|V|v|Version|version)\s*(\d)/, '-$2.0')
  95. },
  96. // e.g. 'ZLIB'
  97. function (argument) {
  98. return argument[0].toUpperCase() + argument.slice(1)
  99. },
  100. // e.g. 'MPL/2.0'
  101. function (argument) {
  102. return argument.replace('/', '-')
  103. },
  104. // e.g. 'Apache 2'
  105. function (argument) {
  106. return argument
  107. .replace(/\s*V\s*(\d)/, '-$1')
  108. .replace(/(\d)$/, '$1.0')
  109. },
  110. // e.g. 'GPL-2.0', 'GPL-3.0'
  111. function (argument) {
  112. if (argument.indexOf('3.0') !== -1) {
  113. return argument + '-or-later'
  114. } else {
  115. return argument + '-only'
  116. }
  117. },
  118. // e.g. 'GPL-2.0-'
  119. function (argument) {
  120. return argument + 'only'
  121. },
  122. // e.g. 'GPL2'
  123. function (argument) {
  124. return argument.replace(/(\d)$/, '-$1.0')
  125. },
  126. // e.g. 'BSD 3'
  127. function (argument) {
  128. return argument.replace(/(-| )?(\d)$/, '-$2-Clause')
  129. },
  130. // e.g. 'BSD clause 3'
  131. function (argument) {
  132. return argument.replace(/(-| )clause(-| )(\d)/, '-$3-Clause')
  133. },
  134. // e.g. 'BY-NC-4.0'
  135. function (argument) {
  136. return 'CC-' + argument
  137. },
  138. // e.g. 'BY-NC'
  139. function (argument) {
  140. return 'CC-' + argument + '-4.0'
  141. },
  142. // e.g. 'Attribution-NonCommercial'
  143. function (argument) {
  144. return argument
  145. .replace('Attribution', 'BY')
  146. .replace('NonCommercial', 'NC')
  147. .replace('NoDerivatives', 'ND')
  148. .replace(/ (\d)/, '-$1')
  149. .replace(/ ?International/, '')
  150. },
  151. // e.g. 'Attribution-NonCommercial'
  152. function (argument) {
  153. return 'CC-' +
  154. argument
  155. .replace('Attribution', 'BY')
  156. .replace('NonCommercial', 'NC')
  157. .replace('NoDerivatives', 'ND')
  158. .replace(/ (\d)/, '-$1')
  159. .replace(/ ?International/, '') +
  160. '-4.0'
  161. }
  162. ]
  163. var licensesWithVersions = spdxLicenseIds
  164. .map(function (id) {
  165. var match = /^(.*)-\d+\.\d+$/.exec(id)
  166. return match
  167. ? [match[0], match[1]]
  168. : [id, null]
  169. })
  170. .reduce(function (objectMap, item) {
  171. var key = item[1]
  172. objectMap[key] = objectMap[key] || []
  173. objectMap[key].push(item[0])
  174. return objectMap
  175. }, {})
  176. var licensesWithOneVersion = Object.keys(licensesWithVersions)
  177. .map(function makeEntries (key) {
  178. return [key, licensesWithVersions[key]]
  179. })
  180. .filter(function identifySoleVersions (item) {
  181. return (
  182. // Licenses has just one valid version suffix.
  183. item[1].length === 1 &&
  184. item[0] !== null &&
  185. // APL will be considered Apache, rather than APL-1.0
  186. item[0] !== 'APL'
  187. )
  188. })
  189. .map(function createLastResorts (item) {
  190. return [item[0], item[1][0]]
  191. })
  192. licensesWithVersions = undefined
  193. // If all else fails, guess that strings containing certain substrings
  194. // meant to identify certain licenses.
  195. var lastResorts = [
  196. ['UNLI', 'Unlicense'],
  197. ['WTF', 'WTFPL'],
  198. ['2 CLAUSE', 'BSD-2-Clause'],
  199. ['2-CLAUSE', 'BSD-2-Clause'],
  200. ['3 CLAUSE', 'BSD-3-Clause'],
  201. ['3-CLAUSE', 'BSD-3-Clause'],
  202. ['AFFERO', 'AGPL-3.0-or-later'],
  203. ['AGPL', 'AGPL-3.0-or-later'],
  204. ['APACHE', 'Apache-2.0'],
  205. ['ARTISTIC', 'Artistic-2.0'],
  206. ['Affero', 'AGPL-3.0-or-later'],
  207. ['BEER', 'Beerware'],
  208. ['BOOST', 'BSL-1.0'],
  209. ['BSD', 'BSD-2-Clause'],
  210. ['CDDL', 'CDDL-1.1'],
  211. ['ECLIPSE', 'EPL-1.0'],
  212. ['FUCK', 'WTFPL'],
  213. ['GNU', 'GPL-3.0-or-later'],
  214. ['LGPL', 'LGPL-3.0-or-later'],
  215. ['GPLV1', 'GPL-1.0-only'],
  216. ['GPL-1', 'GPL-1.0-only'],
  217. ['GPLV2', 'GPL-2.0-only'],
  218. ['GPL-2', 'GPL-2.0-only'],
  219. ['GPL', 'GPL-3.0-or-later'],
  220. ['MIT +NO-FALSE-ATTRIBS', 'MITNFA'],
  221. ['MIT', 'MIT'],
  222. ['MPL', 'MPL-2.0'],
  223. ['X11', 'X11'],
  224. ['ZLIB', 'Zlib']
  225. ].concat(licensesWithOneVersion)
  226. var SUBSTRING = 0
  227. var IDENTIFIER = 1
  228. var validTransformation = function (identifier) {
  229. for (var i = 0; i < transforms.length; i++) {
  230. var transformed = transforms[i](identifier).trim()
  231. if (transformed !== identifier && valid(transformed)) {
  232. return transformed
  233. }
  234. }
  235. return null
  236. }
  237. var validLastResort = function (identifier) {
  238. var upperCased = identifier.toUpperCase()
  239. for (var i = 0; i < lastResorts.length; i++) {
  240. var lastResort = lastResorts[i]
  241. if (upperCased.indexOf(lastResort[SUBSTRING]) > -1) {
  242. return lastResort[IDENTIFIER]
  243. }
  244. }
  245. return null
  246. }
  247. var anyCorrection = function (identifier, check) {
  248. for (var i = 0; i < transpositions.length; i++) {
  249. var transposition = transpositions[i]
  250. var transposed = transposition[TRANSPOSED]
  251. if (identifier.indexOf(transposed) > -1) {
  252. var corrected = identifier.replace(
  253. transposed,
  254. transposition[CORRECT]
  255. )
  256. var checked = check(corrected)
  257. if (checked !== null) {
  258. return checked
  259. }
  260. }
  261. }
  262. return null
  263. }
  264. module.exports = function (identifier) {
  265. var validArugment = (
  266. typeof identifier === 'string' &&
  267. identifier.trim().length !== 0
  268. )
  269. if (!validArugment) {
  270. throw Error('Invalid argument. Expected non-empty string.')
  271. }
  272. identifier = identifier.trim()
  273. if (valid(identifier)) {
  274. return upgradeGPLs(identifier)
  275. }
  276. var noPlus = identifier.replace(/\+$/, '').trim()
  277. if (valid(noPlus)) {
  278. return upgradeGPLs(noPlus)
  279. }
  280. var transformed = validTransformation(identifier)
  281. if (transformed !== null) {
  282. return upgradeGPLs(transformed)
  283. }
  284. transformed = anyCorrection(identifier, function (argument) {
  285. if (valid(argument)) {
  286. return argument
  287. }
  288. return validTransformation(argument)
  289. })
  290. if (transformed !== null) {
  291. return upgradeGPLs(transformed)
  292. }
  293. transformed = validLastResort(identifier)
  294. if (transformed !== null) {
  295. return upgradeGPLs(transformed)
  296. }
  297. transformed = anyCorrection(identifier, validLastResort)
  298. if (transformed !== null) {
  299. return upgradeGPLs(transformed)
  300. }
  301. return null
  302. }
  303. function upgradeGPLs (value) {
  304. if ([
  305. 'GPL-1.0', 'LGPL-1.0', 'AGPL-1.0',
  306. 'GPL-2.0', 'LGPL-2.0', 'AGPL-2.0',
  307. 'LGPL-2.1'
  308. ].indexOf(value) !== -1) {
  309. return value + '-only'
  310. } else if ([
  311. 'GPL-1.0+', 'GPL-2.0+', 'GPL-3.0+',
  312. 'LGPL-2.0+', 'LGPL-2.1+', 'LGPL-3.0+',
  313. 'AGPL-1.0+', 'AGPL-3.0+'
  314. ].indexOf(value) !== -1) {
  315. return value.replace(/\+$/, '-or-later')
  316. } else if (['GPL-3.0', 'LGPL-3.0', 'AGPL-3.0'].indexOf(value) !== -1) {
  317. return value + '-or-later'
  318. } else {
  319. return value
  320. }
  321. }