/** * Roundcube Webmail Client Script * * This file is part of the Roundcube Webmail client * * @licstart The following is the entire license notice for the * JavaScript code in this file. * * Copyright (C) The Roundcube Dev Team * Copyright (C) Kolab Systems AG * * The JavaScript code in this page is free software: you can * redistribute it and/or modify it under the terms of the GNU * General Public License (GNU GPL) as published by the Free Software * Foundation, either version 3 of the License, or (at your option) * any later version. The code is distributed WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU GPL for more details. * * As additional permission under GNU GPL version 3 section 7, you * may distribute non-source (e.g., minimized or compacted) forms of * that code without the copy of the GNU GPL normally required by * section 4, provided you include this license notice and a URL * through which recipients can access the Corresponding Source. * * @licend The above is the entire license notice * for the JavaScript code in this file. * * @author Thomas Bruederli * @author Aleksander 'A.L.E.C' Machniak * @author Charles McNulty * * @requires jquery.js, common.js, list.js */ function rcube_webmail() { this.labels = {}; this.buttons = {}; this.buttons_sel = {}; this.gui_objects = {}; this.gui_containers = {}; this.commands = {}; this.command_handlers = {}; this.onloads = []; this.messages = {}; this.group2expand = {}; this.http_request_jobs = {}; this.menu_stack = []; this.menu_buttons = {}; this.entity_selectors = []; this.image_style = {}; this.uploads = {}; // webmail client settings this.dblclick_time = 500; this.message_time = 5000; this.preview_delay_select = 400; this.preview_delay_click = 60; this.identifier_expr = /[^0-9a-z_-]/gi; // environment defaults this.env = { request_timeout: 180, // seconds draft_autosave: 0, // seconds comm_path: './', recipients_separator: ',', // @deprecated recipients_delimiter: ', ', // @deprecated popup_width: 1150, popup_width_small: 900, thread_padding: '15px' }; // create protected reference to myself this.ref = 'rcmail'; var ref = this; // set jQuery ajax options $.ajaxSetup({ cache: false, timeout: this.env.request_timeout * 1000, error: function(request, status, err){ ref.http_error(request, status, err); }, beforeSend: function(xmlhttp){ xmlhttp.setRequestHeader('X-Roundcube-Request', ref.env.request_token); } }); // unload fix $(window).on('beforeunload', function() { ref.unload = true; }); // set environment variable(s) this.set_env = function(p, value) { if (p != null && typeof p === 'object' && !value) for (var n in p) this.env[n] = p[n]; else this.env[p] = value; }; // add a localized label to the client environment this.add_label = function(p, value) { if (typeof p == 'string') this.labels[p] = value; else if (typeof p == 'object') $.extend(this.labels, p); }; // add a button to the button list this.register_button = function(command, id, type, act, sel, over) { var button_prop = {id:id, type:type}; if (act) button_prop.act = act; if (sel) button_prop.sel = sel; if (over) button_prop.over = over; if (!this.buttons[command]) this.buttons[command] = []; this.buttons[command].push(button_prop); if (this.loaded) { this.init_button(command, button_prop); this.set_button(command, (this.commands[command] ? 'act' : 'pas')); } }; // register a button with popup menu, to set its state according to the state of all commands in the menu this.register_menu_button = function(button, menu_id) { if (this.menu_buttons[menu_id]) { this.menu_buttons[menu_id][0].push(button); } else { var commands = []; $('#' + menu_id).find('a').each(function() { var command, link = $(this), onclick = link.attr('onclick'); if (onclick && String(onclick).match(/rcmail\.command\(\'([^']+)/)) command = RegExp.$1; else command = function() { return link.is('.active'); }; commands.push(command); }); if (commands.length) this.menu_buttons[menu_id] = [[button], commands]; } this.set_menu_buttons(); }; // set state of a menu button according to state of all menu actions this.set_menu_buttons = function() { // Use timeouts to not block and set menu button states only once clearTimeout(this.menu_buttons_timeout); this.menu_buttons_timeout = setTimeout(function() { $.each(ref.menu_buttons, function() { var disabled = true; $.each(this[1], function() { var is_func = typeof(this) == 'function'; if ((is_func && this()) || (!is_func && ref.commands[this])) { return disabled = false; } }); $(this[0]).add($(this[0]).parent('.dropbutton')) .addClass(disabled ? 'disabled' : 'active') .removeClass(disabled ? 'active' : 'disabled'); }); }, 50); }; // register a specific gui object this.gui_object = function(name, id) { this.gui_objects[name] = this.loaded ? rcube_find_object(id) : id; }; // register a container object this.gui_container = function(name, id) { this.gui_containers[name] = id; }; // add a GUI element (html node) to a specified container this.add_element = function(elm, container) { if (this.gui_containers[container] && this.gui_containers[container].jquery) this.gui_containers[container].append(elm); }; // register an external handler for a certain command this.register_command = function(command, callback, enable) { this.command_handlers[command] = callback; if (enable) this.enable_command(command, true); }; // execute the given script on load this.add_onload = function(f) { this.onloads.push(f); }; // initialize webmail client this.init = function() { var n; this.task = this.env.task; // check browser capabilities (never use version checks here) if (this.env.server_error != 409 && (!bw.dom || !bw.xmlhttp_test())) { this.goto_url('error', '_code=0x199'); return; } if (!this.env.blankpage) this.env.blankpage = 'about:blank'; // find all registered gui containers for (n in this.gui_containers) this.gui_containers[n] = $('#'+this.gui_containers[n]); // find all registered gui objects for (n in this.gui_objects) this.gui_objects[n] = rcube_find_object(this.gui_objects[n]); // init registered buttons this.init_buttons(); // tell parent window that this frame is loaded if (this.is_framed()) { parent.rcmail.unlock_frame(); } // enable general commands this.enable_command('close', 'logout', 'mail', 'addressbook', 'settings', 'save-pref', 'compose', 'undo', 'about', 'switch-task', 'menu-open', 'menu-close', 'menu-save', true); // set active task button this.set_button(this.task, 'sel'); if (this.env.permaurl) this.enable_command('permaurl', 'extwin', true); switch (this.task) { case 'mail': // enable mail commands this.enable_command('list', 'checkmail', 'add-contact', 'search', 'reset-search', 'collapse-folder', 'import-messages', true); if (this.gui_objects.messagelist) { this.env.widescreen_list_template = [ {className: 'threads', cells: ['threads']}, {className: 'subject', cells: ['fromto', 'date', 'status', 'subject']}, {className: 'flags', cells: ['flag', 'attachment']} ]; this.message_list = new rcube_list_widget(this.gui_objects.messagelist, { multiselect:true, multiexpand:true, draggable:true, keyboard:true, column_movable:this.env.col_movable, dblclick_time:this.dblclick_time }); this.message_list .addEventListener('initrow', function(o) { ref.init_message_row(o); }) .addEventListener('dblclick', function(o) { ref.msglist_dbl_click(o); }) .addEventListener('keypress', function(o) { ref.msglist_keypress(o); }) .addEventListener('select', function(o) { ref.msglist_select(o); }) .addEventListener('dragstart', function(o) { ref.drag_start(o); }) .addEventListener('dragmove', function(e) { ref.drag_move(e); }) .addEventListener('dragend', function(e) { ref.drag_end(e); }) .addEventListener('expandcollapse', function(o) { ref.msglist_expand(o); }) .addEventListener('column_replace', function(o) { ref.msglist_set_coltypes(o); }) .init(); // TODO: this should go into the list-widget code $(this.message_list.thead).on('click', 'a.sortcol', function(e){ return ref.command('sort', $(this).attr('rel'), this); }); this.enable_command('toggle_status', 'toggle_flag', 'sort', true); this.enable_command('set-listmode', this.env.threads && !this.is_multifolder_listing()); // load messages var searchfilter = $(this.gui_objects.search_filter).val(); if (searchfilter && searchfilter != 'ALL') this.filter_mailbox(searchfilter); else this.command('list'); $(this.gui_objects.qsearchbox).val(this.env.search_text).focusin(function() { ref.message_list.blur(); }); } this.set_button_titles(); this.env.message_commands = ['show', 'reply', 'reply-all', 'reply-list', 'move', 'copy', 'delete', 'open', 'mark', 'edit', 'viewsource', 'bounce', 'print', 'load-attachment', 'download-attachment', 'show-headers', 'hide-headers', 'download', 'forward', 'forward-inline', 'forward-attachment', 'change-format']; if (this.env.action == 'show' || this.env.action == 'preview') { this.enable_command(this.env.message_commands, this.env.uid); this.enable_command('reply-list', this.env.list_post); if (this.env.action == 'show') { this.http_request('pagenav', {_uid: this.env.uid, _mbox: this.env.mailbox, _search: this.env.search_request}, this.display_message('', 'loading')); } if (this.env.mail_read_time > 0) setTimeout(function() { ref.http_post('mark', {_uid: ref.env.uid, _flag: 'read', _mbox: ref.env.mailbox, _quiet: 1}); }, this.env.mail_read_time * 1000); if (this.env.blockedobjects) { $(this.gui_objects.remoteobjectsmsg).show(); this.enable_command('load-remote', true); } // make preview/message frame visible if (this.env.action == 'preview' && this.is_framed()) { this.enable_command('compose', 'add-contact', false); parent.rcmail.show_contentframe(true); } if ($.inArray('flagged', this.env.message_flags) >= 0) { $(document.body).addClass('status-flagged'); } // initialize drag-n-drop on attachments, so they can e.g. // be dropped into mail compose attachments in another window if (this.gui_objects.attachments) $('li > a', this.gui_objects.attachments).not('.drop').on('dragstart', function(e) { var n, href = this.href, dt = e.originalEvent.dataTransfer; if (dt) { // inject username to the uri href = href.replace(/^https?:\/\//, function(m) { return m + urlencode(ref.env.username) + '@'}); // cleanup the node to get filename without the size test n = $(this).clone(); n.children().remove(); dt.setData('roundcube-uri', href); dt.setData('roundcube-name', n.text().trim()); } }); } else if (this.env.action == 'compose') { this.env.address_group_stack = []; this.env.compose_commands = ['send-attachment', 'remove-attachment', 'send', 'cancel', 'toggle-editor', 'list-addresses', 'pushgroup', 'search', 'reset-search', 'extwin', 'insert-response', 'save-response', 'menu-open', 'menu-close', 'load-attachment', 'download-attachment', 'open-attachment', 'rename-attachment']; if (this.env.drafts_mailbox) this.env.compose_commands.push('savedraft') this.enable_command(this.env.compose_commands, true); // add more commands (not enabled) $.merge(this.env.compose_commands, ['add-recipient', 'firstpage', 'previouspage', 'nextpage', 'lastpage']); if (window.googie) { this.env.editor_config.spellchecker = googie; this.env.editor_config.spellcheck_observer = function(s) { ref.spellcheck_state(); }; this.env.compose_commands.push('spellcheck') this.enable_command('spellcheck', true); } // initialize HTML editor this.editor_init(this.env.editor_config, this.env.composebody); // init canned response functions if (this.gui_objects.responseslist) { $('a.insertresponse', this.gui_objects.responseslist) .attr('unselectable', 'on') .mousedown(function(e) { return rcube_event.cancel(e); }) .on('mouseup keypress', function(e) { if (e.type == 'mouseup' || rcube_event.get_keycode(e) == 13) { ref.command('insert-response', $(this).attr('rel')); $(document.body).trigger('mouseup'); // hides the menu return rcube_event.cancel(e); } }); // avoid textarea loosing focus when hitting the save-response button/link $.each(this.buttons['save-response'] || [], function (i, v) { $('#' + v.id).mousedown(function(e){ return rcube_event.cancel(e); }) }); } // init message compose form this.init_messageform(); } else if (this.env.action == 'bounce') { this.init_messageform_inputs(); this.env.compose_commands = []; } else if (this.env.action == 'get') { this.enable_command('download', true); this.enable_command('image-scale', 'image-rotate', !!/^image\//.test(this.env.mimetype)); // Mozilla's PDF.js viewer does not allow printing from host page (#5125) // to minimize user confusion we disable the Print button on Firefox < 75 this.enable_command('print', this.env.mimetype != 'application/pdf' || !bw.mz || bw.vendver >= 75); if (this.env.is_message) { this.enable_command('reply', 'reply-all', 'edit', 'viewsource', 'forward', 'forward-inline', 'forward-attachment', 'bounce', true); if (this.env.list_post) this.enable_command('reply-list', true); } // center and scale the image in preview frame // TODO: Find a better way. Onload is late, also we could use embed.css if (this.env.mimetype.startsWith('image/')) $(this.gui_objects.messagepartframe).on('load', function() { var contents = $(this).contents(); // do not apply styles to an error page (with no image) if (contents.find('img').length) contents.find('head').append( '' ); }); } // show printing dialog else if (this.env.action == 'print' && this.env.uid && !this.env.is_pgp_content && !this.env.pgp_mime_part ) { this.print_dialog(); } // get unread count for each mailbox if (this.gui_objects.mailboxlist) { this.env.unread_counts = {}; this.gui_objects.folderlist = this.gui_objects.mailboxlist; this.http_request('getunread', {_page: this.env.current_page}); } // init address book widget if (this.gui_objects.contactslist) { this.contact_list = new rcube_list_widget(this.gui_objects.contactslist, { multiselect:true, draggable:false, keyboard:true }); this.contact_list .addEventListener('initrow', function(o) { ref.triggerEvent('insertrow', { cid:o.uid, row:o }); }) .addEventListener('select', function(o) { ref.compose_recipient_select(o); }) .addEventListener('dblclick', function(o) { ref.compose_add_recipient(); }) .addEventListener('keypress', function(o) { if (o.key_pressed == o.ENTER_KEY) { if (!ref.compose_add_recipient()) { // execute link action on if not a recipient entry if (o.last_selected && String(o.last_selected).charAt(0) == 'G') { $(o.rows[o.last_selected].obj).find('a').first().click(); } } } }) .init(); // remember last focused address field $('#_to,#_cc,#_bcc').focus(function() { ref.env.focused_field = this; }); } if (this.gui_objects.addressbookslist) { this.gui_objects.folderlist = this.gui_objects.addressbookslist; this.enable_command('list-addresses', true); } // ask user to send MDN if (this.env.mdn_request && this.env.uid) { var postact = 'sendmdn', postdata = {_uid: this.env.uid, _mbox: this.env.mailbox}; if (!confirm(this.get_label('mdnrequest'))) { postdata._flag = 'mdnsent'; postact = 'mark'; } this.http_post(postact, postdata); } this.check_mailvelope(this.env.action); // detect browser capabilities if (!this.is_framed() && !this.env.extwin) this.browser_capabilities_check(); break; case 'addressbook': this.env.address_group_stack = []; if (this.gui_objects.folderlist) this.env.contactfolders = $.extend($.extend({}, this.env.address_sources), this.env.contactgroups); this.enable_command('add', 'import', this.env.writable_source); this.enable_command('list', 'listgroup', 'pushgroup', 'popgroup', 'listsearch', 'search', 'reset-search', 'advanced-search', true); if (this.gui_objects.contactslist) { this.contact_list = new rcube_list_widget(this.gui_objects.contactslist, {multiselect:true, draggable:this.gui_objects.folderlist?true:false, keyboard:true}); this.contact_list .addEventListener('initrow', function(o) { ref.triggerEvent('insertrow', { cid:o.uid, row:o }); }) .addEventListener('keypress', function(o) { ref.list_keypress(o); }) .addEventListener('select', function(o) { ref.contactlist_select(o); }) .addEventListener('dragstart', function(o) { ref.drag_start(o); }) .addEventListener('dragmove', function(e) { ref.drag_move(e); }) .addEventListener('dragend', function(e) { ref.drag_end(e); }) .init(); $(this.gui_objects.qsearchbox).focusin(function() { ref.contact_list.blur(); }); this.update_group_commands(); this.command('list'); } if (this.gui_objects.savedsearchlist) { this.savedsearchlist = new rcube_treelist_widget(this.gui_objects.savedsearchlist, { id_prefix: 'rcmli', id_encode: this.html_identifier_encode, id_decode: this.html_identifier_decode }); this.savedsearchlist.addEventListener('select', function(node) { ref.triggerEvent('selectfolder', { folder:node.id, prefix:'rcmli' }); }); } this.set_page_buttons(); if (this.env.cid) { this.enable_command('show', 'edit', 'qrcode', true); // register handlers for group assignment via checkboxes if (this.gui_objects.editform) { $('input.groupmember').change(function() { ref.group_member_change(this.checked ? 'add' : 'del', ref.env.cid, ref.env.source, this.value); }); } } if (this.gui_objects.editform) { this.enable_command('save', true); if (this.env.action == 'add' || this.env.action == 'edit' || this.env.action == 'search') this.init_contact_form(); } else if (this.env.action == 'print') { this.print_dialog(); } break; case 'settings': this.enable_command('show', 'save', true); if (this.env.action == 'identities') { this.enable_command('add', this.env.identities_level < 2); } else if (this.env.action == 'edit-identity' || this.env.action == 'add-identity') { this.enable_command('save', 'edit', 'toggle-editor', true); this.enable_command('delete', this.env.identities_level < 2); // initialize HTML editor this.editor_init(this.env.editor_config, 'rcmfd_signature'); this.check_mailvelope(this.env.action); } else if (this.env.action == 'folders') { this.enable_command('subscribe', 'unsubscribe', 'create-folder', 'rename-folder', true); } else if (this.env.action == 'edit-folder' && this.gui_objects.editform) { this.enable_command('save', 'folder-size', true); parent.rcmail.env.exists = this.env.messagecount; parent.rcmail.enable_command('purge', this.env.messagecount); } else if (this.env.action == 'responses') { this.enable_command('add', true); } if (this.gui_objects.identitieslist) { this.identity_list = new rcube_list_widget(this.gui_objects.identitieslist, {multiselect:false, draggable:false, keyboard:true}); this.identity_list .addEventListener('select', function(o) { ref.identity_select(o); }) .addEventListener('keypress', function(o) { ref.list_keypress(o); }) .init() .focus(); } else if (this.gui_objects.sectionslist) { this.sections_list = new rcube_list_widget(this.gui_objects.sectionslist, {multiselect:false, draggable:false, keyboard:true}); this.sections_list .addEventListener('select', function(o) { ref.section_select(o); }) .init() .focus(); } else if (this.gui_objects.subscriptionlist) { this.init_subscription_list(); } else if (this.gui_objects.responseslist) { this.responses_list = new rcube_list_widget(this.gui_objects.responseslist, {multiselect:false, draggable:false, keyboard:true}); this.responses_list .addEventListener('select', function(o) { ref.response_select(o); }) .addEventListener('keypress', function(o) { ref.list_keypress(o); }) .init() .focus(); } break; case 'login': var tz, tz_name, input_user = $('#rcmloginuser'), input_tz = $('#rcmlogintz'); if (input_user.val() == '') input_user.focus(); else $('#rcmloginpwd').focus(); // detect client timezone if (window.jstz && (tz = jstz.determine())) tz_name = tz.name(); input_tz.val(tz_name ? tz_name : (new Date().getStdTimezoneOffset() / -60)); // display 'loading' message on form submit, lock submit button $('form').submit(function () { $('[type=submit]', this).prop('disabled', true); ref.clear_messages(); ref.display_message('', 'loading'); }); break; } // select first input field in an edit form if (this.gui_objects.editform) $("input,select,textarea", this.gui_objects.editform) .not(':hidden').not(':disabled').first().select().focus(); // prevent from form submit with Enter key in file input fields if (bw.ie) $('input[type=file]').keydown(function(e) { if (e.keyCode == '13') e.preventDefault(); }); // flag object as complete this.loaded = true; this.env.lastrefresh = new Date(); // show message if (this.pending_message) this.display_message.apply(this, this.pending_message); // init treelist widget if (this.gui_objects.folderlist && window.rcube_treelist_widget // some plugins may load rcube_treelist_widget and there's one case // when this will cause problems - addressbook widget in compose, // which already has been initialized using rcube_list_widget && this.gui_objects.folderlist != this.gui_objects.addressbookslist ) { this.treelist = new rcube_treelist_widget(this.gui_objects.folderlist, { selectable: true, id_prefix: 'rcmli', parent_focus: true, id_encode: this.html_identifier_encode, id_decode: this.html_identifier_decode, check_droptarget: function(node) { return !node.virtual && ref.check_droptarget(node.id) } }); this.treelist .addEventListener('collapse', function(node) { ref.folder_collapsed(node) }) .addEventListener('expand', function(node) { ref.folder_collapsed(node) }) .addEventListener('beforeselect', function(node) { return !ref.busy; }) .addEventListener('select', function(node) { ref.triggerEvent('selectfolder', { folder:node.id, prefix:'rcmli' }); ref.mark_all_read_state(); }); } // activate html5 file drop feature (if browser supports it and if configured) if (this.gui_objects.filedrop && this.env.filedrop && window.FormData) { $(document.body).on('dragover dragleave drop', function(e) { return ref.document_drag_hover(e, e.type == 'dragover'); }); $(this.gui_objects.filedrop).addClass('droptarget') .on('dragover dragleave', function(e) { return ref.file_drag_hover(e, e.type == 'dragover'); }) .get(0).addEventListener('drop', function(e) { return ref.file_dropped(e); }, false); } // catch document (and iframe) mouse clicks var body_mouseup = function(e) { return ref.doc_mouse_up(e); }; $(document.body) .mouseup(body_mouseup) .keydown(function(e) { return ref.doc_keypress(e); }); rcube_webmail.set_iframe_events({mouseup: body_mouseup}); // trigger init event hook this.triggerEvent('init', { task:this.task, action:this.env.action }); // execute all foreign onload scripts // @deprecated for (n in this.onloads) { if (typeof this.onloads[n] === 'string') eval(this.onloads[n]); else if (typeof this.onloads[n] === 'function') this.onloads[n](); } // register menu buttons $('[data-popup]').each(function() { ref.register_menu_button(this, $(this).data('popup')); }); // start keep-alive and refresh intervals this.start_refresh(); this.start_keepalive(); }; this.log = function(msg) { if (this.env.devel_mode && window.console && console.log) console.log(msg); }; /*********************************************************/ /********* client command interface *********/ /*********************************************************/ // execute a specific command on the web client this.command = function(command, props, obj, event, allow_disabled) { var ret; if (obj && obj.blur && !(event && rcube_event.is_keyboard(event))) obj.blur(); // do nothing if interface is locked by another command // with exception for searching reset and menu if (this.busy && !(command == 'reset-search' && this.last_command == 'search') && !command.match(/^menu-/)) return false; // let the browser handle this click (shift/ctrl usually opens the link in a new window/tab) if ((obj && obj.href && String(obj.href).indexOf('#') < 0) && rcube_event.get_modifier(event)) { return true; } // command not supported or allowed if (!allow_disabled && !this.commands[command]) { // pass command to parent window if (this.is_framed()) parent.rcmail.command(command, props); return false; } // check input before leaving compose step if (this.task == 'mail' && this.env.action == 'compose' && !this.env.server_error && command != 'save-pref' && $.inArray(command, this.env.compose_commands) < 0 && !this.compose_skip_unsavedcheck ) { if (!this.env.is_sent && this.cmp_hash != this.compose_field_hash()) { this.confirm_dialog(this.get_label('notsentwarning'), 'discard', function() { // remove copy from local storage if compose screen is left intentionally ref.remove_compose_data(ref.env.compose_id); ref.compose_skip_unsavedcheck = true; ref.command(command, props, obj, event); }); return false; } } this.last_command = command; this.command_aborted = false; // trigger plugin hooks this.triggerEvent('actionbefore', {props: props, action: command, originalEvent: event}); if ((ret = this.triggerEvent('before' + command, props || event)) !== undefined) { // abort if one of the handlers returned false if (ret === false) return false; props = ret; } // process external commands if (typeof this.command_handlers[command] === 'function') { ret = this.command_handlers[command](props, obj, event); } else if (typeof this.command_handlers[command] === 'string') { ret = window[this.command_handlers[command]](props, obj, event); } // process internal commands else { ret = this.command_handler(command, props, obj, event); } if (!this.command_aborted && this.triggerEvent('after' + command, props) === false) ret = false; this.triggerEvent('actionafter', {props: props, action: command, aborted: this.command_aborted, ret: ret, originalEvent: event}); if (ret === false) return false; if ((obj && ret !== true) || this.command_aborted === true) return false; return true; }; // execute a specific known command this.command_handler = function(command, props, obj, event) { var uid, cid, url, flag; // process internal command switch (command) { // commands to switch task case 'logout': case 'mail': case 'addressbook': case 'settings': this.switch_task(command); break; case 'about': this.redirect('?_task=settings&_action=about', false); break; case 'permaurl': if (obj && obj.href && obj.target) return true; if (this.env.permaurl) parent.location.href = this.env.permaurl; break; case 'extwin': if (this.env.action == 'compose') { var form = this.gui_objects.messageform, win = this.open_window(''); if (win) { this.save_compose_form_local(); this.compose_skip_unsavedcheck = true; $("[name='_action']", form).val('compose'); form.action = this.url('mail/compose', { _id: this.env.compose_id, _extwin: 1 }); form.target = win.name; form.submit(); } } else { this.open_window(this.env.permaurl, true); } break; case 'change-format': url = this.env.permaurl + '&_format=' + props; if (this.env.action == 'preview') url = url.replace(/_action=show/, '_action=preview') + '&_framed=1'; if (this.env.extwin) url += '&_extwin=1'; location.href = url; break; case 'menu-open': if (props && props.menu == 'attachmentmenu') { var mimetype = this.env.attachments[props.id]; if (mimetype && mimetype.mimetype) // in compose format is different mimetype = mimetype.mimetype; this.enable_command('open-attachment', mimetype && this.env.mimetypes && $.inArray(mimetype, this.env.mimetypes) >= 0); } this.show_menu(props, props.show || undefined, event); break; case 'menu-close': this.hide_menu(props, event); break; case 'menu-save': this.triggerEvent(command, {props:props, originalEvent:event}); return false; case 'open': if (uid = this.get_single_uid()) { obj.href = this.url('show', this.params_from_uid(uid, {_extwin: 1})); return true; } break; case 'close': if (this.env.extwin) window.close(); break; case 'list': if (props && props != '') { this.reset_qsearch(true); } if (this.env.action == 'compose' && this.env.extwin) { window.close(); } else if (this.task == 'mail') { this.list_mailbox(props); this.set_button_titles(); } else if (this.task == 'addressbook') this.list_contacts(props); break; case 'set-listmode': this.set_list_options(null, undefined, undefined, props == 'threads' ? 1 : 0); break; case 'sort': var sort_order = this.env.sort_order, sort_col = !this.env.disabled_sort_col ? props : this.env.sort_col; if (!this.env.disabled_sort_order) sort_order = this.env.sort_col == sort_col && sort_order == 'ASC' ? 'DESC' : 'ASC'; // set table header and update env this.set_list_sorting(sort_col, sort_order); // reload message list this.list_mailbox('', '', sort_col+'_'+sort_order); break; case 'nextpage': this.list_page('next'); break; case 'lastpage': this.list_page('last'); break; case 'previouspage': this.list_page('prev'); break; case 'firstpage': this.list_page('first'); break; case 'expunge': if (this.env.exists) this.expunge_mailbox(this.env.mailbox); break; case 'purge': case 'empty-mailbox': if (this.env.exists) this.purge_mailbox(this.env.mailbox); break; // common commands used in multiple tasks case 'show': if (this.task == 'mail') { uid = this.get_single_uid(); if (uid && (!this.env.uid || uid != this.env.uid)) { var mbox = this.get_message_mailbox(uid); if (mbox == this.env.drafts_mailbox) this.open_compose_step({_draft_uid: uid, _mbox: mbox}); else this.show_message(uid); } } else if (this.task == 'addressbook') { cid = props ? props : this.get_single_cid(); if (cid && !(this.env.action == 'show' && cid == this.env.cid)) this.load_contact(cid, 'show'); } else if (this.task == 'settings') { this.goto_url('settings/' + props, {_framed: 0}); } break; case 'add': if (this.task == 'addressbook') this.load_contact(0, 'add'); else if (this.task == 'settings' && this.env.action == 'responses') this.load_response(0, 'add-response'); else if (this.task == 'settings') this.load_identity(0, 'add-identity'); break; case 'edit': if (this.task == 'addressbook' && (cid = this.get_single_cid())) this.load_contact(cid, 'edit'); else if (this.task == 'mail' && (uid = this.get_single_uid())) { url = { _mbox: this.get_message_mailbox(uid) }; url[url._mbox == this.env.drafts_mailbox && props != 'new' ? '_draft_uid' : '_uid'] = uid; this.open_compose_step(url); } break; case 'save': var input, form = this.gui_objects.editform; if (form) { // user prefs if ((input = $("[name='_pagesize']", form)) && input.length && isNaN(parseInt(input.val()))) { this.alert_dialog(this.get_label('nopagesizewarning'), function() { input.focus(); }); break; } // contacts/identities else { // reload form if (props == 'reload') { form.action += '&_reload=1'; } else if (this.task == 'settings' && (this.env.identities_level % 2) == 0 && (input = $("[name='_email']", form)) && input.length && !rcube_check_email(input.val()) ) { this.alert_dialog(this.get_label('noemailwarning'), function() { input.focus(); }); break; } } // add selected source (on the list) if (parent.rcmail && parent.rcmail.env.source) form.action = this.add_url(form.action, '_orig_source', parent.rcmail.env.source); form.submit(); } break; case 'delete': // mail task if (this.task == 'mail') this.delete_messages(event); // addressbook task else if (this.task == 'addressbook') this.delete_contacts(); // settings: canned response else if (this.task == 'settings' && this.env.action == 'responses') this.delete_response(); // settings: user identities else if (this.task == 'settings') this.delete_identity(); break; // mail task commands case 'move': case 'moveto': // deprecated if (this.task == 'mail') this.move_messages(props, event); else if (this.task == 'addressbook') this.move_contacts(props, event); break; case 'copy': if (this.task == 'mail') this.copy_messages(props, event); else if (this.task == 'addressbook') this.copy_contacts(props, event); break; case 'mark': if (props) this.mark_message(props); break; case 'toggle_status': case 'toggle_flag': flag = command == 'toggle_flag' ? 'flagged' : 'read'; if (uid = props) { // toggle flagged/unflagged if (flag == 'flagged') { if (this.message_list.rows[uid].flagged) flag = 'unflagged'; } // toggle read/unread else if (this.message_list.rows[uid].deleted) flag = 'undelete'; else if (!this.message_list.rows[uid].unread) flag = 'unread'; this.mark_message(flag, uid); } break; case 'add-contact': this.add_contact(props); break; case 'load-remote': if (this.env.uid) { if (props && this.env.sender) { this.add_contact(this.env.sender, true); break; } this.show_message(this.env.uid, true, this.env.action == 'preview'); } break; case 'load-attachment': case 'open-attachment': case 'download-attachment': var params, mimetype = this.env.attachments[props]; if (this.env.action == 'compose') { params = {_file: props, _id: this.env.compose_id}; mimetype = mimetype ? mimetype.mimetype : ''; } else { params = {_mbox: this.env.mailbox, _uid: this.env.uid, _part: props}; } // open attachment in frame if it's of a supported mimetype if (command != 'download-attachment' && mimetype && this.env.mimetypes && $.inArray(mimetype, this.env.mimetypes) >= 0) { // Note: We disable _framed for proper X-Frame-Options:deny support (#6688) if (this.open_window(this.url('get', $.extend({_frame: 1, _framed: 0}, params)))) break; } params._download = 1; // prevent from page unload warning in compose this.compose_skip_unsavedcheck = 1; this.goto_url('get', params, false, true); this.compose_skip_unsavedcheck = 0; break; case 'select-all': this.select_all_mode = props ? false : true; this.dummy_select = true; // prevent msg opening if there's only one msg on the list var list = this[this.task == 'addressbook' ? 'contact_list' : 'message_list']; if (props == 'invert') list.invert_selection(); else list.select_all(props == 'page' ? '' : props); this.dummy_select = null; break; case 'select-none': this.select_all_mode = false; this[this.task == 'addressbook' ? 'contact_list' : 'message_list'].clear_selection(); break; case 'expand-all': this.env.autoexpand_threads = 1; this.message_list.expand_all(); break; case 'expand-unread': this.env.autoexpand_threads = 2; this.message_list.collapse_all(); this.expand_unread(); break; case 'collapse-all': this.env.autoexpand_threads = 0; this.message_list.collapse_all(); break; case 'nextmessage': if (this.env.next_uid) this.show_message(this.env.next_uid, false, this.env.action == 'preview'); break; case 'lastmessage': if (this.env.last_uid) this.show_message(this.env.last_uid); break; case 'previousmessage': if (this.env.prev_uid) this.show_message(this.env.prev_uid, false, this.env.action == 'preview'); break; case 'firstmessage': if (this.env.first_uid) this.show_message(this.env.first_uid); break; case 'compose': url = {}; if (this.task == 'mail') { url = {_mbox: this.env.mailbox, _search: this.env.search_request}; if (props) url._to = props; } // modify url if we're in addressbook else if (this.task == 'addressbook') { // switch to mail compose step directly if (props && props.indexOf('@') > 0) { url._to = props; } else { var a_cids = []; // use contact id passed as command parameter if (props) a_cids.push(props); // get selected contacts else if (this.contact_list) a_cids = this.contact_list.get_selection(); if (a_cids.length) { this.http_post('mailto', { _cid: a_cids.join(','), _source: this.env.source }, true); break; } else if (this.env.group && this.env.pagecount) { this.http_post('mailto', { _gid: this.env.group, _source: this.env.source }, true); break; } } } else if (props && typeof props == 'string') { url._to = props; } else if (props && typeof props == 'object') { $.extend(url, props); } this.open_compose_step(url); break; case 'spellcheck': if (this.spellcheck_state()) { this.editor.spellcheck_stop(); } else { this.editor.spellcheck_start(); } break; case 'savedraft': // Reset the auto-save timer clearTimeout(this.save_timer); // compose form did not change (and draft wasn't saved already) if (this.env.draft_id && this.cmp_hash == this.compose_field_hash()) { this.auto_save_start(); break; } this.submit_messageform(true); break; case 'send': if (!props.nocheck && !this.env.is_sent && !this.check_compose_input(command)) break; // Reset the auto-save timer clearTimeout(this.save_timer); this.submit_messageform(); break; case 'send-attachment': // Reset the auto-save timer clearTimeout(this.save_timer); if (!(flag = this.upload_file(props || this.gui_objects.uploadform, 'upload'))) { if (flag !== false) this.alert_dialog(this.get_label('selectimportfile')); aborted = true; } break; case 'insert-sig': this.change_identity($("[name='_from']")[0], true); break; case 'list-addresses': this.list_contacts(props); this.enable_command('add-recipient', false); break; case 'add-recipient': this.compose_add_recipient(props); break; case 'reply-all': case 'reply-list': case 'reply': if (uid = this.get_single_uid()) { url = {_reply_uid: uid, _mbox: this.get_message_mailbox(uid), _search: this.env.search_request}; if (command == 'reply-all') // do reply-list, when list is detected and popup menu wasn't used url._all = (!props && this.env.reply_all_mode == 1 && this.commands['reply-list'] ? 'list' : 'all'); else if (command == 'reply-list') url._all = 'list'; this.open_compose_step(url); } break; case 'forward-attachment': case 'forward-inline': case 'forward': var uids = this.env.uid ? [this.env.uid] : (this.message_list ? this.message_list.get_selection() : []); if (uids.length) { url = { _forward_uid: this.uids_to_list(uids), _mbox: this.env.mailbox, _search: this.env.search_request }; if (command == 'forward-attachment' || (!props && this.env.forward_attachment) || uids.length > 1) url._attachment = 1; this.open_compose_step(url); } break; case 'print': if (this.task == 'addressbook') { if (uid = this.get_single_cid()) { url = '&_action=print&_cid=' + uid; if (this.env.source) url += '&_source=' + urlencode(this.env.source); this.open_window(this.env.comm_path + url, true, true); } } else if (this.env.action == 'get' && !this.env.is_message) { this.gui_objects.messagepartframe.contentWindow.print(); } else if (uid = this.get_single_uid()) { url = this.url('print', this.params_from_uid(uid, {_safe: this.env.safemode ? 1 : 0})); if (this.open_window(url, true, true)) { if (this.env.action != 'show' && this.env.action != 'get') this.mark_message('read', uid); } } break; case 'viewsource': if (uid = this.get_single_uid()) this.open_window(this.url('viewsource', this.params_from_uid(uid)), true, true); break; case 'download': if (this.env.action == 'get') { location.href = this.secure_url(location.href.replace(/_frame=/, '_download=')); } else if (uid = this.get_single_uid()) { this.goto_url('viewsource', this.params_from_uid(uid, {_save: 1}), false, true); } break; // quicksearch case 'search': return this.qsearch(props); // reset quicksearch case 'reset-search': var n, s = this.env.search_request || this.env.qsearch; this.reset_qsearch(true); if (s && this.env.action == 'compose') { if (this.contact_list) this.list_contacts_clear(); } else if (s && this.env.mailbox) { this.list_mailbox(this.env.mailbox, 1); } else if (s && this.task == 'addressbook') { if (this.env.source == '') { for (n in this.env.address_sources) break; this.env.source = n; this.env.group = ''; } this.list_contacts(this.env.source, this.env.group, 1); } break; case 'pushgroup': // add group ID and current search to stack var group = { id: props.id, search_request: this.env.search_request, page: this.env.current_page, search: this.env.search_request && this.gui_objects.qsearchbox ? this.gui_objects.qsearchbox.value : null }; this.env.address_group_stack.push(group); if (obj && event) rcube_event.cancel(event); // FIXME: no break? case 'listgroup': this.reset_qsearch(); this.list_contacts(props.source, props.id, 1, group); break; case 'popgroup': if (this.env.address_group_stack.length) { var old = this.env.address_group_stack.pop(); this.reset_qsearch(); if (old.search_request) { // this code is executed when going back to the search result if (old.search && this.gui_objects.qsearchbox) $(this.gui_objects.qsearchbox).val(old.search); this.env.search_request = old.search_request; this.list_contacts_remote(null, null, this.env.current_page = old.page); } else this.list_contacts(props.source, this.env.address_group_stack[this.env.address_group_stack.length-1].id); } break; case 'import-messages': var form = props || this.gui_objects.importform, importlock = this.set_busy(true, 'importwait'); if (!(flag = this.upload_file(form, 'import', importlock))) { this.set_busy(false, null, importlock); if (flag !== false) this.alert_dialog(this.get_label('selectimportfile')); this.command_aborted = true; } break; case 'import': var dialog = $('