--- /dev/null
+function junk_keyword_message_set(junk_uids, nonjunk_uids) {
+ var i;
+ for (i=0; i<junk_uids.length; i++) {
+ var uid = junk_uids[i];
+ var message = rcmail.env.messages[uid];
+ var row = rcmail.message_list.rows[uid];
+ var rowobj = $(row.junk_icon);
+ if (!rowobj.hasClass('junk')) {
+ rowobj.addClass('junk');
+ message.flags.junk = true;
+ }
+ rcmail.set_message(uid, 'unread', false);
+ rcmail.update_thread_root(uid, 'read');
+ if (rcmail.env.unread_counts[rcmail.env.mailbox]) {
+ rcmail.env.unread_counts[rcmail.env.mailbox] -= 1;
+ rcmail.set_unread_count(rcmail.env.mailbox
+ , rcmail.env.unread_counts[rcmail.env.mailbox]
+ , rcmail.env.mailbox == 'INBOX');
+ }
+ if (rcmail.env.preview_pane_mark_read > 0)
+ rcmail.http_post('mark', '_uid='+id+'&_flag=read&_quiet=1');
+ }
+ for (i=0; i<nonjunk_uids.length; i++) {
+ var uid = nonjunk_uids[i];
+ var message = rcmail.env.messages[uid];
+ var row = rcmail.message_list.rows[uid];
+ var rowobj = $(row.junk_icon);
+ if (rowobj.hasClass('junk')) {
+ rowobj.removeClass('junk');
+ message.flags.junk = false;
+ }
+ }
+ var lock = rcmail.set_busy(true, 'loading');
+ var str_junk_uids = junk_uids.join(',');
+ var str_nonjunk_uids = nonjunk_uids.join(',');
+ rcmail.http_request('plugin.junk_keyword.set_keywords'
+ , '&_junk_uids=' + str_junk_uids
+ + '&_nonjunk_uids=' + str_nonjunk_uids
+ + '&_mbox=' + urlencode(rcmail.env.mailbox)
+ //+ '&_search='+ (this.env.search_request ? this.env.search_request : '')
+ , lock);
+ };
+function junk_keyword_message_toggle(uid) {
+ var uids = [], junk_uids = [], nonjunk_uids = []
+ , i, id
+ , list = rcmail.message_list;
+
+ if (uid)
+ uids[0] = uid;
+ else if (rcmail.env.uid)
+ uids[0] = rcmail.env.uid;
+ else if (list)
+ uids = list.get_selection();
+
+ list.focus();
+ for (i=0; i<uids.length; i++) {
+ id = uids[i];
+ if (list.rows[id].flags.junk)
+ nonjunk_uids.push(id);
+ else
+ junk_uids.push(id);
+ }
+ junk_keyword_message_set(junk_uids, nonjunk_uids);
+ };
+function junk_keyword_icon_toggle(props) {
+ if (props && !props._row)
+ return false;
+ junk_keyword_message_toggle(props._row.uid);
+ return true;
+ };
+
+function junk_keyword_menu() {
+ $('#rcmjunk_keyword').html(rcmail.env.junk_keywordicon);
+ };
+if (window.rcmail) {
+ rcmail.addEventListener('init', function(evt) {
+ rcmail.register_command('plugin.junk_keyword.icon_toggle', junk_keyword_icon_toggle, true);
+ });
+
+ rcmail.addEventListener('listupdate','junk_keyword_menu');
+ rcmail.addEventListener('insertrow', function(evt) {
+ var row = evt.row, uid = evt.uid;
+ var message = rcmail.env.messages[uid];
+ //if (message.flags) {
+ // var rowobj = $(row.obj);
+ // if (message.flags.junk)
+ // rowobj.addClass('junk');
+ // }
+ if ((found = $.inArray('junk_keyword', rcmail.env.coltypes)) >= 0) {
+ rcmail.set_env('junk_keyword_col', found);
+ }
+ if (rcmail.env.junk_keyword_col != null) {
+ var junk_icon = 'rcmjunk_keyword' + row.uid;
+ if (row.junk_icon = document.getElementById(junk_icon)) {
+ row.junk_icon._row = row.obj;
+ row.junk_icon.onmousedown = function(e) {
+ rcmail.command('plugin.junk_keyword.icon_toggle', this);
+ rcube_event.cancel(e);
+ };
+ }
+ }
+ });
+ };
+$(document).ready(function() {
+ junk_keyword_menu();
+ var li = '<li><input type="checkbox" name="list_col[]" value="junk_keyword" id="cols_junk_keyword" /><label for="cols_junk_keyword">'+rcmail.get_label('junk_keyword.junk_keyword')+'</label></li>';
+ $("#listmenu fieldset ul input#cols_threads").parent().after(li);
+ });
--- /dev/null
+<?php
+
+class junk_keyword extends rcube_plugin {
+ public $task = 'mail';
+
+ function init() {
+ #write_log($this->name, "init: task=" . print_r($rcmail->task, true) . " action=" . print_r($rcmail->action, true));
+ $rcmail = rcmail::get_instance();
+ $this->name = get_class($this);
+ if (($rcmail->task == 'mail') && ($rcmail->action == '')) {
+ $this->add_hook('render_page', array($this, 'render_page'));
+ $this->include_script('junk_keyword.js');
+ }
+ $this->add_junk_flags = array
+ ( 'JUNK' => 'Junk'
+ , 'NONJUNK' => 'NonJunk'
+ );
+ $this->register_action('plugin.junk_keyword.set_keywords', array($this, 'set_keywords'));
+ $this->add_hook('messages_list', array($this, 'message_list'));
+ $this->add_texts('localization');
+ }
+ public function render_page($args) {
+ #write_log($this->name, "render_page: args=" . print_r($args, true));
+ $rcmail = rcmail::get_instance();
+ $icon = 'plugins/junk_keyword/' .$this->local_skin_path(). '/junk.png';
+ $junk_keywordicon = html::img(array
+ ( 'alt' => $this->gettext('junk_keyword')
+ , 'id' => 'junk_keyword_menulink'
+ , 'src' => $icon
+ , 'title' => $this->gettext('junk_keyword')));
+ $rcmail->output->add_label('junk_keyword.junk_keyword');
+ $rcmail->output->set_env('junk_keywordicon', $junk_keywordicon);
+ $this->include_stylesheet($this->local_skin_path(). '/junk.css');
+
+ return $args;
+ }
+ public function message_list($args) {
+ #write_log($this->name, "message_list: args=" . print_r($args, true));
+ if (!isset($args['messages']) or !is_array($args['messages']))
+ return $args;
+ foreach($args['messages'] as $message) {
+ #write_log($this->name, "message_list: foreach: begin: message=" . print_r($message, true));
+ $message->list_flags['extra_flags']['junk_keywords'] = array(); # always set extra_flags, needed for javascript later!
+ if (is_array($message->flags)) {
+ $uid = $message->uid;
+ $class = '';
+ foreach ($message->flags as $flagname => $flagvalue) {
+ $flag = is_numeric("$flagvalue") ? $flagname : $flagvalue; // for compatibility with < 0.5.4
+ $flag = strtolower($flag);
+ #write_log($this->name, "message_list: foreach: flag=" . print_r($flag, true));
+ $message->list_flags['extra_flags']['junk'] = false;
+ if (preg_match('/^junk$/', $flag)) {
+ $class = "junk";
+ $message->list_flags['extra_flags']['junk'] = true;
+ }
+ if (preg_match('/^nonjunk$/', $flag)) {
+ $class = "nonjunk";
+ $message->list_flags['extra_flags']['junk'] = false;
+ }
+ }
+ if (!empty($uid))
+ $message->list_cols['junk_keyword'] = '<div id="rcmjunk_keyword'.$uid.'" name="rcmjunk_keyword'.$uid.'" class="'.$class.'"> </div>';
+ }
+ }
+ return $args;
+ }
+ public function set_keywords() {
+ #write_log($this->name, "set_keywords: _GET=" . print_r($_GET, true));
+
+ $rcmail = rcmail::get_instance();
+ $imap = $rcmail->imap;
+ //$cbox = get_input_value('_cur', RCUBE_INPUT_GET);
+ $mbox = get_input_value('_mbox', RCUBE_INPUT_GET);
+ $junk_uids = get_input_value('_junk_uids', RCUBE_INPUT_GET);
+ $junk_uids = array_filter(explode(',', $junk_uids));
+ $nonjunk_uids = get_input_value('_nonjunk_uids', RCUBE_INPUT_GET);
+ $nonjunk_uids = array_filter(explode(',', $nonjunk_uids));
+
+ $imap->conn->flags = array_merge($imap->conn->flags, $this->add_junk_flags);
+
+ if (!is_array($junk_uids) || !is_array($nonjunk_uids))
+ return false;
+ //write_log($this->name, "set_keywords: junk_uids=" . print_r($junk_uids, true));
+ //write_log($this->name, "set_keywords: nonjunk_uids=" . print_r($nonjunk_uids, true));
+
+ $error = "";
+ $imap->conn->error = "";
+ if (!empty($junk_uids)) {
+ if (!$imap->set_flag($junk_uids, 'SEEN', $mbox))
+ $error = "set SEEN: " . $imap->conn->error;
+ if (!$imap->unset_flag($junk_uids, 'NONJUNK', $mbox))
+ $error = "unset NONJUNK: " . $imap->conn->error;
+ if (!$imap->set_flag($junk_uids, 'JUNK', $mbox))
+ $error = "set JUNK: " . $imap->conn->error;
+ }
+ if (!empty($nonjunk_uids)) {
+ if (!$imap->unset_flag($nonjunk_uids, 'JUNK', $mbox))
+ $error = "unset JUNK: " . $imap->conn->error;
+ if (!$imap->set_flag($nonjunk_uids, 'NONJUNK', $mbox))
+ $error = "set NONJUNK: " . $imap->conn->error;
+ }
+ if (empty($error))
+ $rcmail->output->command('display_message', $this->gettext('classification_done'), 'confirmation');
+ else
+ $rcmail->output->command('display_message', $this->gettext('classification_failed'). ': ' . $error, 'error');
+ $this->api->output->send();
+ return empty($error);
+ }
+ }
+?>