filter that check other mails if positive

User Help for Mozilla Thunderbird
Post Reply
ElPazzo
Posts: 21
Joined: May 29th, 2005, 3:16 am
Location: Wien
Contact:

filter that check other mails if positive

Post by ElPazzo »

Hi,

Is it possible either by filtering or with an addon to run the filter on mail to look for a specific subject:

e.g. if subject starts with "Project closed" (the subject contains a number at the end)

and if that subject is found than use the number from that subject and run another filter that marks all mails from the same sender where subject contains that number as read?

Thanks,
Pascal
morat
Posts: 6421
Joined: February 3rd, 2009, 6:29 pm

Re: filter that check other mails if positive

Post by morat »

I got a two part manual filter working with the FiltaQuilla extension.

http://addons.mozilla.org/thunderbird/a ... ltaquilla/

http://mesquilla.com/extensions/filtaquilla/
http://mesquilla.com/forum/filtaquilla/
http://mesquilla.com/category/extensions/filtaquilla/

http://kb.mozillazine.org/Message_Filters

Remember to check Javascript search term in FiltaQuilla options, then restart.

Filter name: FiltaQuilla Javascript Search Term Part 1 of 2

Javascript:

Code: Select all

var win = Services.wm.getMostRecentWindow("mail:3pane");
var uid = "__unique_identifier_FiltaQuilla";
var subject = message.mime2DecodedSubject;
var author = message.mime2DecodedAuthor;
var matches = subject.match(/\d+$/);
if (/^Project closed/.test(subject) && matches) win[uid] = {author: author, project: matches[0]};
(false);
Filter name: FiltaQuilla Javascript Search Term Part 2 of 2

Javascript:

Code: Select all

var win = Services.wm.getMostRecentWindow("mail:3pane");
var uid = "__unique_identifier_FiltaQuilla";
var subject = message.mime2DecodedSubject;
var author = message.mime2DecodedAuthor;
var project = null;
var matches = subject.match(/\d+$/);
if (matches) project = matches[0];
(author == (win[uid] && win[uid].author) && project == (win[uid] && win[uid].project));
ElPazzo
Posts: 21
Joined: May 29th, 2005, 3:16 am
Location: Wien
Contact:

Re: filter that check other mails if positive

Post by ElPazzo »

Hi Morat,

Thanks for the hint. I already use FiltaQuilla for regex filters but am not very proficient in the other capabilities.

Where do I enter the javascript you showed?

Just setting up a new filter and enter it in the field showing up? And with 2 part filter, do you mean with 2 conditions in one filter, or is it 2 separate filters?

regards,
Pascal
morat
Posts: 6421
Joined: February 3rd, 2009, 6:29 pm

Re: filter that check other mails if positive

Post by morat »

It works for me with a simple test case. I tagged 2 messages as important in the draft folder.

https://s29.postimg.cc/narc1xvxz/filter1.png
ElPazzo wrote:Where do I enter the javascript you showed?
There is an "Edit Javascript" button in the Filter Rules dialog.

https://s29.postimg.cc/lwzp6mwon/filter2.png
Last edited by morat on August 21st, 2018, 8:15 am, edited 1 time in total.
ElPazzo
Posts: 21
Joined: May 29th, 2005, 3:16 am
Location: Wien
Contact:

Re: filter that check other mails if positive

Post by ElPazzo »

Thanks,

Yes, found the box when chosing the right option. ;)

So, in fact there are 2 filters.

edit: … for some reason it does not work over here.

filter 1:

Code: Select all

var win = Services.wm.getMostRecentWindow("mail:3pane");
var uid = "__unique_identifier_FiltaQuilla";
var subject = message.mime2DecodedSubject;
var author = message.mime2DecodedAuthor;
var matches = subject.match(/\b(\d{7}-\d{2})\b$/);
if (/^Projet attribué à un autre prestataire/.test(subject) && matches) win[uid] = {author: author, project: matches[0]};
(false);
filter 2:

Code: Select all

var win = Services.wm.getMostRecentWindow("mail:3pane");
var uid = "__unique_identifier_FiltaQuilla";
var subject = message.mime2DecodedSubject;
var author = message.mime2DecodedAuthor;
var project = null;
var matches = subject.match(/\b(\d{7}-\d{2})\b$/);
if (matches) project = matches[0];
(author == (win[uid] && win[uid].author) && project == (win[uid] && win[uid].project));
I only changed the regex and added condition if mail status unread
morat
Posts: 6421
Joined: February 3rd, 2009, 6:29 pm

Re: filter that check other mails if positive

Post by morat »

I can't really check the regular expressions without knowing the subjects.

Code: Select all

alert("Example 1234567-89".match(/\b(\d{7}-\d{2})\b$/)[0]); // 1234567-89
The dash could be problem.

e.g. subject uses U+2013 instead of U+002D

Unicode Characters in the 'Punctuation, Dash' Category
http://www.fileformat.info/info/unicode ... d/list.htm

Unicode code converter
http://r12a.github.io/apps/conversion/

Here is how to check the global variable.

1. tools > developer tools > error console
2. paste code in text field
3. press return

Code: Select all

JSON.stringify(Services.wm.getMostRecentWindow("mail:3pane"). __unique_identifier_FiltaQuilla);
ElPazzo
Posts: 21
Joined: May 29th, 2005, 3:16 am
Location: Wien
Contact:

Re: filter that check other mails if positive

Post by ElPazzo »

Hi Morat,

examples of subject to be checked first:

Projet attribué à un autre prestataire 1708164-01

if that one is true then mark all mails from that sender containing that number as read:

e.g.
Demande de disponibilité | 1708164-01-03 | DE » fr-FR | Verhaltenskodex
ElPazzo
Posts: 21
Joined: May 29th, 2005, 3:16 am
Location: Wien
Contact:

Re: filter that check other mails if positive

Post by ElPazzo »

I just managed to check only the first part of the filter by removing the false part at the end.

That one works fine. so it seems it's the second part of the filter (2/2) that generates the problem but I cannot find where.

This is the code I use to match the second part as the number is not at the end here:

Code: Select all

var win = Services.wm.getMostRecentWindow("mail:3pane");
var uid = "__unique_identifier_FiltaQuilla";
var subject = message.mime2DecodedSubject;
var author = message.mime2DecodedAuthor;
var project = null;
var matches = subject.match(/(\d{7}-\d{2})/);
if (matches) project = matches[0];
(author == (win[uid] && win[uid].author) && project == (win[uid] && win[uid].project));
morat
Posts: 6421
Joined: February 3rd, 2009, 6:29 pm

Re: filter that check other mails if positive

Post by morat »

Is the sender different?

I don't see the problem with the code.

Code: Select all

var s1 = "Projet attribué à un autre prestataire 1708164-01";
var s2 = "Demande de disponibilité | 1708164-01-03 | DE » fr-FR | Verhaltenskodex";

alert(/^Projet attribué à un autre prestataire/.test(s1)); // true
alert(s1.match(/\b(\d{7}-\d{2})\b$/)[0]);                  // 1708164-01
alert(s2.match(/(\d{7}-\d{2})/)[0]);                       // 1708164-01
morat
Posts: 6421
Joined: February 3rd, 2009, 6:29 pm

Re: filter that check other mails if positive

Post by morat »

I only store a single project number in the global variable.

http://s27.postimg.cc/ob54j4nkz/filter3.png

That's why the 2 messages with 789 aren't tagged as important.
Last edited by morat on August 21st, 2018, 8:15 am, edited 1 time in total.
ElPazzo
Posts: 21
Joined: May 29th, 2005, 3:16 am
Location: Wien
Contact:

Re: filter that check other mails if positive

Post by ElPazzo »

No, it's exactly the same sender. Glad I'm not the only one not seeing any problem with the code.
Yes, it should be only one number per run. But of course it could be that in rare cases there are two rejection mails in the inbox.
But here when testing, there is only one with unread status. So it should only get that one. Or should I rather check for unread status in javascript field instead of adding a second condition for status?
ElPazzo
Posts: 21
Joined: May 29th, 2005, 3:16 am
Location: Wien
Contact:

Re: filter that check other mails if positive

Post by ElPazzo »

yes, that seems to be the problem. When I remove the other mails marked as read, it works fine.
How do I check for read/unread status with javascript?
ElPazzo
Posts: 21
Joined: May 29th, 2005, 3:16 am
Location: Wien
Contact:

Re: filter that check other mails if positive

Post by ElPazzo »

Ok, i found out, by removing the second condition about status adding this to javascript everything works: !(/read/.test(status)) &&

Thanks a lot for your time morat. ;)
morat
Posts: 6421
Joined: February 3rd, 2009, 6:29 pm

Re: filter that check other mails if positive

Post by morat »

I got it working by storing all the project numbers in the global variable.

http://s2.postimg.cc/8txcp1i09/filter4.png

Filter name: FiltaQuilla Javascript Search Term Part 1 of 3

Javascript:

Code: Select all

var win = Services.wm.getMostRecentWindow("mail:3pane");
var uid = "__unique_identifier_FiltaQuilla";
var subject = message.mime2DecodedSubject;
var author = message.mime2DecodedAuthor;
var project = null;
var matches = subject.match(/\d+$/);
if (matches) project = matches[0];
if (/^Project closed/.test(subject) && matches) {
  if (!(uid in win)) win[uid] = [];
  var bool = false;
  for (var i = 0; i < win[uid].length; i++) {
    bool = (author == (win[uid][i] && win[uid][i].author) &&
      project == (win[uid][i] && win[uid][i].project));
    if (bool) break;
  }
  if (!bool) win[uid].push({author: author, project: project});
}
(false);
Filter name: FiltaQuilla Javascript Search Term Part 2 of 3

Javascript:

Code: Select all

var win = Services.wm.getMostRecentWindow("mail:3pane");
var uid = "__unique_identifier_FiltaQuilla";
var subject = message.mime2DecodedSubject;
var author = message.mime2DecodedAuthor;
var project = null;
var matches = subject.match(/\d+$/);
if (matches) project = matches[0];
var bool = false;
if (uid in win) {
  for (var i = 0; i < win[uid].length; i++) {
    bool = (author == (win[uid][i] && win[uid][i].author) &&
      project == (win[uid][i] && win[uid][i].project));
    if (bool) break;
  }
}
(bool);
Filter name: FiltaQuilla Javascript Search Term Part 3 of 3

Javascript:

Code: Select all

var win = Services.wm.getMostRecentWindow("mail:3pane");
var uid = "__unique_identifier_FiltaQuilla";
if (uid in win) delete win[uid];
(false);
Last edited by morat on August 21st, 2018, 8:16 am, edited 2 times in total.
morat
Posts: 6421
Joined: February 3rd, 2009, 6:29 pm

Re: filter that check other mails if positive

Post by morat »

ElPazzo wrote:How do I check for read/unread status with javascript?
Try these:

Code: Select all

(message.flags & Components.interfaces.nsMsgMessageFlags.Read);

Code: Select all

(!(message.flags & Components.interfaces.nsMsgMessageFlags.Read));
Here is how to send a string to the error console for troubleshooting.

Code: Select all

var win = Services.wm.getMostRecentWindow("mail:3pane");
var uid = "__unique_identifier_FiltaQuilla";
var subject = message.mime2DecodedSubject;
var author = message.mime2DecodedAuthor;
Application.console.log("FiltaQuilla: " + [subject, author].join(", "));
Application.console.log("FiltaQuilla: " + JSON.stringify(win[uid]));
if (message.flags & Components.interfaces.nsMsgMessageFlags.Read) {
  Application.console.log("FiltaQuilla: message is read");
} else {
  Application.console.log("FiltaQuilla: message is unread");
}
(false);
Post Reply