Skip to content

Commit

Permalink
Added snooze functionnality to imap
Browse files Browse the repository at this point in the history
  • Loading branch information
josaphatim committed Jul 3, 2023
1 parent 27494ff commit ac7d8ab
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 36 deletions.
8 changes: 8 additions & 0 deletions lib/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,14 @@ public function module_is_supported($name) {
return in_array(strtolower($name), $this->config->get_modules(true), true);
}

public function save_hm_msgs() {
$msgs = Hm_Msgs::get();
if (!empty($msgs)) {
Hm_Msgs::flush();
$this->session->secure_cookie($this->request, 'hm_msgs', base64_encode(json_encode($msgs)));
}
}

/**
* Handler modules need to override this method to do work
*/
Expand Down
3 changes: 1 addition & 2 deletions modules/github/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ public function process() {
else {
Hm_Msgs::add('ERRAn Error Occurred');
}
$msgs = Hm_Msgs::get();
$this->session->secure_cookie($this->request, 'hm_msgs', base64_encode(serialize($msgs)));
$this->save_hm_msgs();
Hm_Dispatch::page_redirect('?page=servers');
}
}
Expand Down
12 changes: 7 additions & 5 deletions modules/imap/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ function parse_snooze_header($snooze_header)
{
$snooze_header = str_replace('X-Snoozed: ', '', $snooze_header);
$result = [];
foreach (explode(';', str_replace("\r\n", " ", $snooze_header)) as $kv)
foreach (explode(';', $snooze_header) as $kv)
{
$kv = trim($kv);
$spacePos = strpos($kv, ' ');
Expand Down Expand Up @@ -1265,22 +1265,24 @@ function get_snooze_date($format, $only_label = false) {
*/
if (!hm_exists('snooze_formats')) {
function snooze_formats() {
return array(
$values = array(
'tomorrow',
'next_weekend',
'next_week',
'next_month'
);
if (date('H') <= 16) {
$values = array_push($values, 'later_in_day');
}
return $values;
}}

/**
* @subpackage imap/functions
*/
if (!hm_exists('snooze_dropdown')) {
function snooze_dropdown($output, $unsnooze = false) {
if (date('H') <= 16) {
$values = array_merge(['later_in_day'], snooze_formats());
}
$values = snooze_formats();
$txt = '<div style="display: inline-block;">';
$txt .= '<a class="snooze_link hlink" id="snooze_message" href="#">'.$output->trans('Snooze').'</a>';
$txt .= '<div class="snooze_dropdown" style="display:none;">';
Expand Down
37 changes: 17 additions & 20 deletions modules/imap/handler_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,10 @@ public function process() {
}
}
elseif (count($moved) == 0) {
Hm_Msgs::add('ERR Unable to move/copy selected messages');
Hm_Msgs::add('ERRUnable to move/copy selected messages');
}
if ($form['imap_move_action'] == 'move' && $form['imap_move_page'] == 'message') {
$msgs = Hm_Msgs::get();
Hm_Msgs::flush();
$this->session->secure_cookie($this->request, 'hm_msgs', base64_encode(json_encode($msgs)));
$this->save_hm_msgs();
}
$this->out('move_count', $moved);
}
Expand Down Expand Up @@ -828,9 +826,7 @@ public function process() {
Hm_Msgs::add('Message deleted');
$this->out('imap_delete_error', false);
}
$msgs = Hm_Msgs::get();
Hm_Msgs::flush();
$this->session->secure_cookie($this->request, 'hm_msgs', base64_encode(json_encode($msgs)));
$this->save_hm_msgs();
}
}
}
Expand Down Expand Up @@ -895,9 +891,7 @@ public function process() {
Hm_Msgs::add('ERRAn error occurred archiving the message');
}
}
$msgs = Hm_Msgs::get();
Hm_Msgs::flush();
$this->session->secure_cookie($this->request, 'hm_msgs', base64_encode(json_encode($msgs)));
$this->save_hm_msgs();
}
}

Expand Down Expand Up @@ -954,7 +948,7 @@ public function process() {
if ($form['imap_snooze_until'] != 'unsnooze') {
$at = date('D, d M Y H:i:s O');
$until = get_snooze_date($form['imap_snooze_until']);
$snooze_tag = "X-Snoozed: at $at;\n \tuntil $until";
$snooze_tag = "X-Snoozed: at $at; until $until";
}
$ids = explode(',', $form['imap_snooze_ids']);
foreach ($ids as $msg_part) {
Expand All @@ -977,9 +971,7 @@ public function process() {
$msg = 'ERRFailed to snooze selected messages';
}
Hm_Msgs::add($msg);
$msgs = Hm_Msgs::get();
Hm_Msgs::flush();
$this->session->secure_cookie($this->request, 'hm_msgs', base64_encode(json_encode($msgs)));
$this->save_hm_msgs();
}
}

Expand All @@ -999,16 +991,21 @@ public function process() {
$imap = Hm_IMAP_List::connect($server_id, $cache);
if (imap_authed($imap)) {
$folder = 'Snoozed';
if (!count($imap->get_mailbox_status($folder))) {
continue;
}
$ret = $imap->get_mailbox_page($folder, 'DATE', false, 'ALL');
foreach ($ret[1] as $msg) {
$msg_headers = $imap->get_message_headers($msg['uid']);
try {
$snooze_headers = parse_snooze_header($msg_headers['X-Snoozed']);
if (new DateTime($snooze_headers['until']) <= new DateTime()) {
snooze_message($imap, $msg['uid'], $folder, null);
if (isset($msg_headers['X-Snoozed'])) {
try {
$snooze_headers = parse_snooze_header($msg_headers['X-Snoozed']);
if (new DateTime($snooze_headers['until']) <= new DateTime()) {
snooze_message($imap, $msg['uid'], $folder, null);
}
} catch (Exception $e) {
Hm_Debug::add(sprintf('ERR Cannot unsnooze message: %s', $msg_headers['subject']));
}
} catch (Exception $e) {
Hm_Debug::add(sprintf('ERR Cannot unsnooze message: %s', $msg_headers['subject']));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion modules/imap/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ var imap_setup_snooze = function() {
if (res.snoozed_messages > 0) {
Hm_Folders.reload_folders(true);
var path = hm_list_parent()? hm_list_parent(): hm_list_path();
window.location.href = '?page=message_list&list_path='+path;
window.location.replace('?page=message_list&list_path='+path);
}
}
);
Expand Down
8 changes: 2 additions & 6 deletions modules/nux/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ public function process() {
else {
Hm_Msgs::add('ERRAn Error Occurred');
}
$msgs = Hm_Msgs::get();
$this->session->secure_cookie($this->request, 'hm_msgs', base64_encode(serialize($msgs)));
$this->save_hm_msgs();
Hm_Dispatch::page_redirect('?page=servers');
}
}
Expand Down Expand Up @@ -213,10 +212,7 @@ public function process() {
$this->session->record_unsaved('SMTP server added');
$this->session->secure_cookie($this->request, 'hm_reload_folders', '1');
Hm_Msgs::add('E-mail account successfully added');
$msgs = Hm_Msgs::get();
if (!empty($msgs)) {
$this->session->secure_cookie($this->request, 'hm_msgs', base64_encode(serialize($msgs)));
}
$this->save_hm_msgs();
$this->session->close_early();
$this->out('nux_account_added', true);
$this->out('nux_server_id', $new_id);
Expand Down
3 changes: 1 addition & 2 deletions modules/wordpress/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ public function process() {
else {
Hm_Msgs::add('ERRAn Error Occured');
}
$msgs = Hm_Msgs::get();
$this->session->secure_cookie($this->request, 'hm_msgs', base64_encode(serialize($msgs)));
$this->save_hm_msgs();
Hm_Dispatch::page_redirect('?page=servers');
}
}
Expand Down

0 comments on commit ac7d8ab

Please sign in to comment.