Skip to content

Commit dcdf0cd

Browse files
refactor(sieve_filters): Update sieve filters helpers
1 parent 007dce8 commit dcdf0cd

File tree

4 files changed

+64
-150
lines changed

4 files changed

+64
-150
lines changed

modules/core/site.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2752,7 +2752,8 @@ const Hm_Filters = (function (my) {
27522752
],
27532753
function (res) {
27542754
if (Object.keys(res.script_details).length === 0) {
2755-
window.location = window.location;
2755+
window.location.href = "?page=sieve_filters";
2756+
27562757
} else {
27572758
edit_script_modal.open();
27582759
$(".modal_sieve_script_textarea").val(res.script_details.gen_script);
@@ -2941,13 +2942,15 @@ class Hm_Filter_Modal extends Hm_Modal {
29412942
this.addFooterBtn("Save", "btn-primary ms-auto", async () => {
29422943
let result = save_filter(current_account);
29432944
if (result) {
2945+
Hm_Notices.show("Filter saved", "success");
29442946
this.hide();
29452947
}
29462948
});
29472949

29482950
this.addFooterBtn("Convert to code", "btn-warning", async () => {
29492951
let result = save_filter(current_account, true);
29502952
if (result) {
2953+
Hm_Notices.show("Filter saved", "success");
29512954
this.hide();
29522955
}
29532956
});

modules/imap/setup.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@
155155
add_handler('ajax_imap_message_content', 'save_imap_cache', true);
156156
add_handler('ajax_imap_message_content', 'save_imap_servers', true);
157157
add_handler('ajax_imap_message_content', 'close_session_early', true, 'core');
158-
// add_output('ajax_imap_message_content', 'filter_message_headers', true);
158+
add_output('ajax_imap_message_content', 'filter_message_headers', true);
159159
add_output('ajax_imap_message_content', 'filter_message_body', true);
160160
add_output('ajax_imap_message_content', 'filter_message_struct', true);
161161

@@ -337,6 +337,7 @@
337337
'do_not_flag_as_read_on_open' => array(FILTER_VALIDATE_BOOLEAN, false),
338338
'ajax_imap_folders_permissions' => array(FILTER_UNSAFE_RAW, FILTER_REQUIRE_ARRAY),
339339
'move_responses' => array(FILTER_UNSAFE_RAW, FILTER_REQUIRE_ARRAY),
340+
'mailbox_name' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
340341
),
341342

342343
'allowed_get' => array(

modules/imap/site.js

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -970,23 +970,21 @@ var imap_move_copy = function(e, action, context) {
970970
return false;
971971
};
972972

973-
974973
$(function () {
975974
$(document).on("submit", "#create-filter-form", function (e) {
976975
e.preventDefault();
977976
const current_account = $(this).attr("account");
978-
console.log("Current account mailbox:", current_account);
979-
980-
981-
const edit_filter_modal = new Hm_Filter_Modal();
977+
978+
const edit_filter_modal = new Hm_Filter_Modal(current_account);
982979
hm_sieve_button_events(edit_filter_modal);
983980
edit_filter_modal.setTitle("Add Filter for message like this");
981+
const add_filter_condition = Hm_Filters.add_filter_condition;
982+
const add_filter_action = Hm_Filters.add_filter_action;
984983

985984
const $form = $(this);
986985
const $btn = $form.find("#create_filter").prop("disabled", true);
987986
const data = {};
988987

989-
// collect checked conditions
990988
if ($form.find("#use_from").is(":checked"))
991989
data["from"] = $form.find('input[name="from"]').val();
992990
if ($form.find("#use_to").is(":checked"))
@@ -997,21 +995,58 @@ $(function () {
997995
data["reply-to"] = $form.find('input[name="reply-to"]').val();
998996

999997
if ($.isEmptyObject(data)) {
1000-
Hm_Notices.show("Please check at least one condition to create a filter.", "danger");
1001-
$btn.prop("disabled", false);
1002-
return;
1003-
}
998+
Hm_Notices.show(
999+
"Please check at least one condition to create a filter.",
1000+
"danger"
1001+
);
1002+
$btn.prop("disabled", false);
1003+
return;
1004+
}
10041005

1005-
function encodeParams(obj) {
1006-
return Object.entries(obj)
1007-
.map(([k, v]) => encodeURIComponent(k) + "=" + encodeURIComponent(v))
1008-
.join("&");
1009-
}
1006+
edit_filter_modal.open();
1007+
1008+
// After modal content is ready, auto-fill conditions
1009+
setTimeout(() => {
1010+
const allFields = hm_sieve_condition_fields();
1011+
const availableFields = [
1012+
...allFields.Message.map((f) => f.name),
1013+
...allFields.Header.map((f) => f.name),
1014+
];
10101015

1011-
const params = encodeParams(data);
1012-
edit_filter_modal.open();
1013-
1016+
for (const [key, value] of Object.entries(data)) {
1017+
// If key is not in available fields, skip it
1018+
if (!availableFields.includes(key)) {
1019+
console.warn("Skipping unrecognized field:", key);
1020+
continue;
1021+
}
10141022

1023+
add_filter_condition();
1024+
1025+
const $lastRow = $(".sieve_list_conditions_modal tr").last();
1026+
const $selectField = $lastRow.find(".add_condition_sieve_filters");
1027+
const $selectOp = $lastRow.find(".condition_options");
1028+
const $inputVal = $lastRow.find('input[name="sieve_selected_option_value[]"]');
1029+
$selectField.val(key);
1030+
$selectOp.val("Contains");
1031+
$inputVal.val(value);
1032+
1033+
}
1034+
1035+
if (data["reply-to"]) {
1036+
1037+
add_filter_action("autoreply");
1038+
1039+
const $lastRow = $(".filter_actions_modal_table tr").last();
1040+
const $select = $lastRow.find(".sieve_actions_select");
1041+
$select.val("autoreply").trigger("change");
1042+
1043+
// Focus the input field for the message
1044+
const $input = $lastRow.find('input[name="sieve_selected_action_value[]"]');
1045+
if ($input.length) {
1046+
$input.focus();
1047+
}
1048+
}
1049+
}, 250);
10151050
});
10161051
});
10171052

modules/sievefilters/site.js

Lines changed: 5 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -190,139 +190,14 @@ var hm_sieve_possible_actions = function() {
190190
];
191191
};
192192

193-
function add_filter_condition() {
194-
let header_fields = "";
195-
let message_fields = "";
196-
197-
hm_sieve_condition_fields().Message.forEach(function (value) {
198-
if (value.selected === true) {
199-
message_fields +=
200-
'<option selected value="' +
201-
value.name +
202-
'">' +
203-
value.description +
204-
"</option>";
205-
} else {
206-
message_fields +=
207-
'<option value="' +
208-
value.name +
209-
'">' +
210-
value.description +
211-
"</option>";
212-
}
213-
});
214-
hm_sieve_condition_fields().Header.forEach(function (value) {
215-
if (value.selected === true) {
216-
header_fields +=
217-
'<option selected value="' +
218-
value.name +
219-
'">' +
220-
value.description +
221-
"</option>";
222-
} else {
223-
header_fields +=
224-
'<option value="' +
225-
value.name +
226-
'">' +
227-
value.description +
228-
"</option>";
229-
}
230-
});
231-
let extra_options =
232-
'<td class="col-sm-3"><input type="hidden" class="condition_extra_value form-control form-control-sm" name="sieve_selected_extra_option_value[]" /></td>';
233-
$(".sieve_list_conditions_modal").append(
234-
" <tr>" +
235-
' <td class="col-sm-2">' +
236-
' <select class="add_condition_sieve_filters form-control form-control-sm" name="sieve_selected_conditions_field[]">' +
237-
' <optgroup label="Message">' +
238-
message_fields +
239-
" </optgroup>" +
240-
' <optgroup label="Header">' +
241-
header_fields +
242-
" </optgroup>" +
243-
" </select>" +
244-
" </td>" +
245-
extra_options +
246-
' <td class="col-sm-3">' +
247-
' <select class="condition_options form-control form-control-sm" name="sieve_selected_conditions_options[]">' +
248-
' <option value="Contains">' +
249-
" Contains" +
250-
" </option>" +
251-
' <option value="!Contains">' +
252-
" Not Contains" +
253-
" </option>" +
254-
' <option value="Matches">' +
255-
" Matches" +
256-
" </option>" +
257-
' <option value="!Matches">' +
258-
" Not Matches" +
259-
" </option>" +
260-
' <option value="Regex">' +
261-
" Regex" +
262-
" </option>" +
263-
' <option value="!Regex">' +
264-
" Not Regex" +
265-
" </option>" +
266-
" </select>" +
267-
" </td>" +
268-
' <td class="col-sm-3">' +
269-
' <input type="text" name="sieve_selected_option_value[]" class="form-control form-control-sm" />' +
270-
" </td>" +
271-
' <td class="col-sm-1 text-end align-middle">' +
272-
' <a href="#" class="delete_condition_modal_button btn btn-sm btn-secondary">Delete</a>' +
273-
" </td>" +
274-
" </tr>"
275-
);
276-
}
277-
278-
function add_filter_action(default_value = "") {
279-
let possible_actions_html = "";
280-
281-
hm_sieve_possible_actions().forEach(function (value) {
282-
if (value.selected === true) {
283-
possible_actions_html +=
284-
'<option selected value="' +
285-
value.name +
286-
'">' +
287-
value.description +
288-
"</option>";
289-
return;
290-
}
291-
possible_actions_html +=
292-
'<option value="' +
293-
value.name +
294-
'">' +
295-
value.description +
296-
"</option>";
297-
});
298-
let extra_options =
299-
'<td class="col-sm-3"><input type="hidden" class="condition_extra_action_value form-control form-control-sm" name="sieve_selected_extra_action_value[]" /></td>';
300-
$(".filter_actions_modal_table").append(
301-
'<tr class="border" default_value="' +
302-
default_value +
303-
'">' +
304-
' <td class="col-sm-3">' +
305-
' <select class="sieve_actions_select form-control form-control-sm" name="sieve_selected_actions[]">' +
306-
" " +
307-
possible_actions_html +
308-
" </select>" +
309-
" </td>" +
310-
extra_options +
311-
' <td class="col-sm-5">' +
312-
' <input type="hidden" name="sieve_selected_action_value[]" value="">' +
313-
" </input>" +
314-
' <td class="col-sm-1 text-end align-middle">' +
315-
' <a href="#" class="delete_action_modal_button btn btn-sm btn-secondary">Delete</a>' +
316-
" </td>" +
317-
"</tr>"
318-
);
319-
}
193+
const add_filter_condition = Hm_Filters.add_filter_condition;
194+
const add_filter_action = Hm_Filters.add_filter_action;
320195

321196

197+
/**************************************************************************************
198+
* MODAL EVENTS
199+
**************************************************************************************/
322200
var hm_sieve_button_events = function (edit_filter_modal, edit_script_modal) {
323-
/**************************************************************************************
324-
* MODAL EVENTS
325-
**************************************************************************************/
326201

327202
$(document)
328203
.off("click")

0 commit comments

Comments
 (0)