Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /* Enigma Plugin */
  2. window.rcmail && rcmail.addEventListener('init', function(evt) {
  3. if (rcmail.env.task == 'settings') {
  4. if (rcmail.gui_objects.keyslist) {
  5. rcmail.keys_list = new rcube_list_widget(rcmail.gui_objects.keyslist,
  6. {multiselect:true, draggable:false, keyboard:false});
  7. rcmail.keys_list
  8. .addEventListener('select', function(o) { rcmail.enigma_keylist_select(o); })
  9. .addEventListener('keypress', function(o) { rcmail.enigma_keylist_keypress(o); })
  10. .init()
  11. .focus();
  12. rcmail.enigma_list();
  13. rcmail.register_command('firstpage', function(props) { return rcmail.enigma_list_page('first'); });
  14. rcmail.register_command('previouspage', function(props) { return rcmail.enigma_list_page('previous'); });
  15. rcmail.register_command('nextpage', function(props) { return rcmail.enigma_list_page('next'); });
  16. rcmail.register_command('lastpage', function(props) { return rcmail.enigma_list_page('last'); });
  17. }
  18. if (rcmail.env.action == 'plugin.enigmakeys') {
  19. rcmail.register_command('search', function(props) {return rcmail.enigma_search(props); }, true);
  20. rcmail.register_command('reset-search', function(props) {return rcmail.enigma_search_reset(props); }, true);
  21. rcmail.register_command('plugin.enigma-import', function() { rcmail.enigma_import(); }, true);
  22. rcmail.register_command('plugin.enigma-key-export', function() { rcmail.enigma_export(); });
  23. rcmail.register_command('plugin.enigma-key-export-selected', function() { rcmail.enigma_export(true); });
  24. rcmail.register_command('plugin.enigma-key-import', function() { rcmail.enigma_key_import(); }, true);
  25. rcmail.register_command('plugin.enigma-key-delete', function(props) { return rcmail.enigma_delete(); });
  26. rcmail.register_command('plugin.enigma-key-create', function(props) { return rcmail.enigma_key_create(); }, true);
  27. rcmail.register_command('plugin.enigma-key-save', function(props) { return rcmail.enigma_key_create_save(); }, true);
  28. rcmail.addEventListener('responseafterplugin.enigmakeys', function() {
  29. rcmail.enable_command('plugin.enigma-key-export', rcmail.env.rowcount > 0);
  30. });
  31. }
  32. }
  33. else if (rcmail.env.task == 'mail') {
  34. if (rcmail.env.action == 'compose') {
  35. rcmail.addEventListener('beforesend', function(props) { rcmail.enigma_beforesend_handler(props); })
  36. .addEventListener('beforesavedraft', function(props) { rcmail.enigma_beforesavedraft_handler(props); });
  37. $('input,label', $('#enigmamenu')).mouseup(function(e) {
  38. // don't close the menu on mouse click inside
  39. e.stopPropagation();
  40. });
  41. }
  42. $.each(['encrypt', 'sign'], function() {
  43. if (rcmail.env['enigma_force_' + this])
  44. $('[name="_enigma_' + this + '"]').prop('checked', true);
  45. });
  46. if (rcmail.env.enigma_password_request) {
  47. rcmail.enigma_password_request(rcmail.env.enigma_password_request);
  48. }
  49. }
  50. });
  51. /*********************************************************/
  52. /********* Enigma Settings/Keys/Certs UI *********/
  53. /*********************************************************/
  54. // Display key(s) import form
  55. rcube_webmail.prototype.enigma_key_import = function()
  56. {
  57. this.enigma_loadframe('&_action=plugin.enigmakeys&_a=import');
  58. };
  59. // Display key(s) generation form
  60. rcube_webmail.prototype.enigma_key_create = function()
  61. {
  62. this.enigma_loadframe('&_action=plugin.enigmakeys&_a=create');
  63. };
  64. // Generate key(s) and submit them
  65. rcube_webmail.prototype.enigma_key_create_save = function()
  66. {
  67. var options, lock,
  68. user = $('#key-ident > option').filter(':selected').text(),
  69. password = $('#key-pass').val(),
  70. confirm = $('#key-pass-confirm').val(),
  71. size = $('#key-size').val();
  72. // validate the form
  73. if (!password || !confirm)
  74. return alert(this.get_label('enigma.formerror'));
  75. if (password != confirm)
  76. return alert(this.get_label('enigma.passwordsdiffer'));
  77. if (user.match(/^<[^>]+>$/))
  78. return alert(this.get_label('enigma.nonameident'));
  79. // generate keys
  80. // use OpenPGP.js if browser supports required features
  81. if (window.openpgp && window.crypto && (window.crypto.getRandomValues || window.crypto.subtle)) {
  82. lock = this.set_busy(true, 'enigma.keygenerating');
  83. options = {
  84. numBits: size,
  85. userId: user,
  86. passphrase: password
  87. };
  88. openpgp.generateKeyPair(options).then(function(keypair) {
  89. // success
  90. var post = {_a: 'import', _keys: keypair.privateKeyArmored};
  91. // send request to server
  92. rcmail.http_post('plugin.enigmakeys', post, lock);
  93. }, function(error) {
  94. // failure
  95. rcmail.set_busy(false, null, lock);
  96. rcmail.display_message(rcmail.get_label('enigma.keygenerateerror'), 'error');
  97. });
  98. }
  99. // generate keys on the server
  100. else if (rcmail.env.enigma_keygen_server) {
  101. lock = this.set_busy(true, 'enigma.keygenerating');
  102. options = {_a: 'generate', _user: user, _password: password, _size: size};
  103. rcmail.http_post('plugin.enigmakeys', options, lock);
  104. }
  105. else {
  106. rcmail.display_message(rcmail.get_label('enigma.keygennosupport'), 'error');
  107. }
  108. };
  109. // Action executed after successful key generation and import
  110. rcube_webmail.prototype.enigma_key_create_success = function()
  111. {
  112. parent.rcmail.enigma_list(1);
  113. };
  114. // Delete key(s)
  115. rcube_webmail.prototype.enigma_delete = function()
  116. {
  117. var keys = this.keys_list.get_selection();
  118. if (!keys.length || !confirm(this.get_label('enigma.keyremoveconfirm')))
  119. return;
  120. var lock = this.display_message(this.get_label('enigma.keyremoving'), 'loading'),
  121. post = {_a: 'delete', _keys: keys};
  122. // send request to server
  123. this.http_post('plugin.enigmakeys', post, lock);
  124. };
  125. // Export key(s)
  126. rcube_webmail.prototype.enigma_export = function(selected)
  127. {
  128. var priv = false,
  129. list = this.keys_list,
  130. keys = selected ? list.get_selection().join(',') : '*',
  131. args = {_a: 'export', _keys: keys};
  132. if (!keys.length)
  133. return;
  134. // find out wether selected keys are private
  135. if (keys == '*')
  136. priv = true;
  137. else
  138. $.each(list.get_selection(), function() {
  139. flags = $(list.rows[this].obj).data('flags');
  140. if (flags && flags.indexOf('p') >= 0) {
  141. priv = true;
  142. return false;
  143. }
  144. });
  145. // ask the user about including private key in the export
  146. if (priv)
  147. return this.show_popup_dialog(
  148. this.get_label('enigma.keyexportprompt'),
  149. this.get_label('enigma.exportkeys'),
  150. [{
  151. text: this.get_label('enigma.onlypubkeys'),
  152. click: function(e) {
  153. rcmail.goto_url('plugin.enigmakeys', args, false, true);
  154. $(this).remove();
  155. }
  156. },
  157. {
  158. text: this.get_label('enigma.withprivkeys'),
  159. click: function(e) {
  160. args._priv = 1;
  161. rcmail.goto_url('plugin.enigmakeys', args, false, true);
  162. $(this).remove();
  163. }
  164. }],
  165. {width: 400}
  166. );
  167. this.goto_url('plugin.enigmakeys', args, false, true);
  168. };
  169. // Submit key(s) import form
  170. rcube_webmail.prototype.enigma_import = function()
  171. {
  172. var form, file;
  173. if (form = this.gui_objects.importform) {
  174. file = document.getElementById('rcmimportfile');
  175. if (file && !file.value) {
  176. alert(this.get_label('selectimportfile'));
  177. return;
  178. }
  179. var lock = this.set_busy(true, 'importwait');
  180. form.action = this.add_url(form.action, '_unlock', lock);
  181. form.submit();
  182. this.lock_form(form, true);
  183. }
  184. };
  185. // list row selection handler
  186. rcube_webmail.prototype.enigma_keylist_select = function(list)
  187. {
  188. var id = list.get_single_selection(), url;
  189. if (id)
  190. url = '&_action=plugin.enigmakeys&_a=info&_id=' + id;
  191. this.enigma_loadframe(url);
  192. this.enable_command('plugin.enigma-key-delete', 'plugin.enigma-key-export-selected', list.selection.length > 0);
  193. };
  194. rcube_webmail.prototype.enigma_keylist_keypress = function(list)
  195. {
  196. if (list.modkey == CONTROL_KEY)
  197. return;
  198. if (list.key_pressed == list.DELETE_KEY || list.key_pressed == list.BACKSPACE_KEY)
  199. this.command('plugin.enigma-key-delete');
  200. else if (list.key_pressed == 33)
  201. this.command('previouspage');
  202. else if (list.key_pressed == 34)
  203. this.command('nextpage');
  204. };
  205. // load key frame
  206. rcube_webmail.prototype.enigma_loadframe = function(url)
  207. {
  208. var frm, win;
  209. if (this.env.contentframe && window.frames && (frm = window.frames[this.env.contentframe])) {
  210. if (!url && (win = window.frames[this.env.contentframe])) {
  211. if (win.location && win.location.href.indexOf(this.env.blankpage) < 0)
  212. win.location.href = this.env.blankpage;
  213. return;
  214. }
  215. this.env.frame_lock = this.set_busy(true, 'loading');
  216. frm.location.href = this.env.comm_path + '&_framed=1&' + url;
  217. }
  218. };
  219. // Search keys/certs
  220. rcube_webmail.prototype.enigma_search = function(props)
  221. {
  222. if (!props && this.gui_objects.qsearchbox)
  223. props = this.gui_objects.qsearchbox.value;
  224. if (props || this.env.search_request) {
  225. var params = {'_a': 'search', '_q': urlencode(props)},
  226. lock = this.set_busy(true, 'searching');
  227. // if (this.gui_objects.search_filter)
  228. // addurl += '&_filter=' + this.gui_objects.search_filter.value;
  229. this.env.current_page = 1;
  230. this.enigma_loadframe();
  231. this.enigma_clear_list();
  232. this.http_post('plugin.enigmakeys', params, lock);
  233. }
  234. return false;
  235. }
  236. // Reset search filter and the list
  237. rcube_webmail.prototype.enigma_search_reset = function(props)
  238. {
  239. var s = this.env.search_request;
  240. this.reset_qsearch();
  241. if (s) {
  242. this.enigma_loadframe();
  243. this.enigma_clear_list();
  244. // refresh the list
  245. this.enigma_list();
  246. }
  247. return false;
  248. }
  249. // Keys/certs listing
  250. rcube_webmail.prototype.enigma_list = function(page)
  251. {
  252. var params = {'_a': 'list'},
  253. lock = this.set_busy(true, 'loading');
  254. this.env.current_page = page ? page : 1;
  255. if (this.env.search_request)
  256. params._q = this.env.search_request;
  257. if (page)
  258. params._p = page;
  259. this.enigma_clear_list();
  260. this.http_post('plugin.enigmakeys', params, lock);
  261. }
  262. // Change list page
  263. rcube_webmail.prototype.enigma_list_page = function(page)
  264. {
  265. if (page == 'next')
  266. page = this.env.current_page + 1;
  267. else if (page == 'last')
  268. page = this.env.pagecount;
  269. else if (page == 'prev' && this.env.current_page > 1)
  270. page = this.env.current_page - 1;
  271. else if (page == 'first' && this.env.current_page > 1)
  272. page = 1;
  273. this.enigma_list(page);
  274. }
  275. // Remove list rows
  276. rcube_webmail.prototype.enigma_clear_list = function()
  277. {
  278. this.enigma_loadframe();
  279. if (this.keys_list)
  280. this.keys_list.clear(true);
  281. this.enable_command('plugin.enigma-key-delete', 'plugin.enigma-key-delete-selected', false);
  282. }
  283. // Adds a row to the list
  284. rcube_webmail.prototype.enigma_add_list_row = function(r)
  285. {
  286. if (!this.gui_objects.keyslist || !this.keys_list)
  287. return false;
  288. var list = this.keys_list,
  289. tbody = this.gui_objects.keyslist.tBodies[0],
  290. rowcount = tbody.rows.length,
  291. even = rowcount%2,
  292. css_class = 'message'
  293. + (even ? ' even' : ' odd'),
  294. // for performance use DOM instead of jQuery here
  295. row = document.createElement('tr'),
  296. col = document.createElement('td');
  297. row.id = 'rcmrow' + r.id;
  298. row.className = css_class;
  299. if (r.flags) $(row).data('flags', r.flags);
  300. col.innerHTML = r.name;
  301. row.appendChild(col);
  302. list.insert_row(row);
  303. }
  304. /*********************************************************/
  305. /********* Enigma Message methods *********/
  306. /*********************************************************/
  307. // handle message send/save action
  308. rcube_webmail.prototype.enigma_beforesend_handler = function(props)
  309. {
  310. this.env.last_action = 'send';
  311. this.enigma_compose_handler(props);
  312. }
  313. rcube_webmail.prototype.enigma_beforesavedraft_handler = function(props)
  314. {
  315. this.env.last_action = 'savedraft';
  316. this.enigma_compose_handler(props);
  317. }
  318. rcube_webmail.prototype.enigma_compose_handler = function(props)
  319. {
  320. var form = this.gui_objects.messageform;
  321. // copy inputs from enigma menu to the form
  322. $('#enigmamenu input').each(function() {
  323. var id = this.id + '_cpy', input = $('#' + id);
  324. if (!input.length) {
  325. input = $(this).clone();
  326. input.prop({id: id, type: 'hidden'}).appendTo(form);
  327. }
  328. input.val(this.checked ? '1' : '');
  329. });
  330. // disable signing when saving drafts
  331. if (this.env.last_action == 'savedraft') {
  332. $('input[name="_enigma_sign"]', form).val(0);
  333. }
  334. }
  335. // Import attached keys/certs file
  336. rcube_webmail.prototype.enigma_import_attachment = function(mime_id)
  337. {
  338. var lock = this.set_busy(true, 'loading'),
  339. post = {_uid: this.env.uid, _mbox: this.env.mailbox, _part: mime_id};
  340. this.http_post('plugin.enigmaimport', post, lock);
  341. return false;
  342. }
  343. // password request popup
  344. rcube_webmail.prototype.enigma_password_request = function(data)
  345. {
  346. if (!data || !data.keyid) {
  347. return;
  348. }
  349. var ref = this,
  350. msg = this.get_label('enigma.enterkeypass'),
  351. myprompt = $('<div class="prompt">'),
  352. myprompt_content = $('<div class="message">')
  353. .appendTo(myprompt),
  354. myprompt_input = $('<input>').attr({type: 'password', size: 30})
  355. .keypress(function(e) {
  356. if (e.which == 13)
  357. (ref.is_framed() ? window.parent.$ : $)('.ui-dialog-buttonpane button.mainaction:visible').click();
  358. })
  359. .appendTo(myprompt);
  360. data.key = data.keyid;
  361. if (data.keyid.length > 8)
  362. data.keyid = data.keyid.substr(data.keyid.length - 8);
  363. $.each(['keyid', 'user'], function() {
  364. msg = msg.replace('$' + this, data[this]);
  365. });
  366. myprompt_content.text(msg);
  367. this.show_popup_dialog(myprompt, this.get_label('enigma.enterkeypasstitle'),
  368. [{
  369. text: this.get_label('save'),
  370. 'class': 'mainaction',
  371. click: function(e) {
  372. e.stopPropagation();
  373. var jq = ref.is_framed() ? window.parent.$ : $;
  374. data.password = myprompt_input.val();
  375. if (!data.password) {
  376. myprompt_input.focus();
  377. return;
  378. }
  379. ref.enigma_password_submit(data);
  380. jq(this).remove();
  381. }
  382. },
  383. {
  384. text: this.get_label('cancel'),
  385. click: function(e) {
  386. var jq = ref.is_framed() ? window.parent.$ : $;
  387. e.stopPropagation();
  388. jq(this).remove();
  389. }
  390. }], {width: 400});
  391. if (this.is_framed() && parent.rcmail.message_list) {
  392. // this fixes bug when pressing Enter on "Save" button in the dialog
  393. parent.rcmail.message_list.blur();
  394. }
  395. }
  396. // submit entered password
  397. rcube_webmail.prototype.enigma_password_submit = function(data)
  398. {
  399. if (this.env.action == 'compose' && !data['compose-init']) {
  400. return this.enigma_password_compose_submit(data);
  401. }
  402. var lock = this.set_busy(true, 'loading'),
  403. form = $('<form>').attr({method: 'post', action: data.action || location.href, style: 'display:none'})
  404. .append($('<input>').attr({type: 'hidden', name: '_keyid', value: data.key}))
  405. .append($('<input>').attr({type: 'hidden', name: '_passwd', value: data.password}))
  406. .append($('<input>').attr({type: 'hidden', name: '_token', value: this.env.request_token}))
  407. .append($('<input>').attr({type: 'hidden', name: '_unlock', value: lock}));
  408. // Additional form fields for request parameters
  409. $.each(data, function(i, v) {
  410. if (i.indexOf('input') == 0)
  411. form.append($('<input>').attr({type: 'hidden', name: i.substring(5), value: v}))
  412. });
  413. if (data.iframe) {
  414. var name = 'enigma_frame_' + (new Date()).getTime(),
  415. frame = $('<iframe>').attr({style: 'display:none', name: name}).appendTo(document.body);
  416. form.attr('target', name);
  417. }
  418. form.appendTo(document.body).submit();
  419. }
  420. // submit entered password - in mail compose page
  421. rcube_webmail.prototype.enigma_password_compose_submit = function(data)
  422. {
  423. var form = this.gui_objects.messageform;
  424. if (!$('input[name="_keyid"]', form).length) {
  425. $(form).append($('<input>').attr({type: 'hidden', name: '_keyid', value: data.key}))
  426. .append($('<input>').attr({type: 'hidden', name: '_passwd', value: data.password}));
  427. }
  428. else {
  429. $('input[name="_keyid"]', form).val(data.key);
  430. $('input[name="_passwd"]', form).val(data.password);
  431. }
  432. this.submit_messageform(this.env.last_action == 'savedraft');
  433. }