Документ взят из кэша поисковой машины. Адрес
оригинального документа
: http://www.sao.ru/cats/~satr/JS/Guide/mail.htm
Дата изменения: Fri Dec 12 11:19:08 1997 Дата индексирования: Tue Oct 2 05:40:04 2012 Кодировка: Поисковые слова: р п р п р п р п р п р п р п р п р п |
filters.js
file. This file contains the JavaScript functions that perform the mail filtering. These functions can use all features of client-side JavaScript. The location of this file depends on your platform, as shown in the following table:
The following is an example of a simple mail filter file. It files all messages from my_mom into the "FromMom" folder, and marks them as high priority. It also sends all messages from my_sister to the trash folder.
// filters.js file.
function MomFilter(message) {
if (message.from.indexOf("my_mom@mothers.net") != -1) {
message.priority = "High";
message.folder = "mailbox:FromMom";
}
else if (message.subject.indexOf("my_sister@sisters.net") != -1) {
message.trash();
}
}
NOTE:
There is no way to specify an IMAP folder using the mailbox:
syntax. So, if
you refile things using IMAP, they all end up on your local machine.
Once you've written the JavaScript filter function, you add a reference to the filter in your mail rules file. The location of your rules file is also platform dependent, as shown in the following table:
This file is normally only written by the filter system in Netscape Messenger. If you've got a rules file already, add the following lines to it:
name="filterName"
Where
enabled="yes"
type="2"
scriptName="scriptName"
The appropriate entry for the example above would be:
name="Filter for Mom"
You can add multiple groups of the above lines to your rules file to add multiple filters. They are executed in the order listed in the file until one of them performs an action on the message (sets a property or calls a method).
If you don't already have a mail rule file, you'll need to add the following two lines at the top (before any filter references):
enabled="yes"
type="2"
scriptName="MomFilter"version="6"
logging="no" News filters
The above discussion about adding filters to your mail rule file applies to news filters as well. The only difference between news filters and mail filters is the type
line. With mail filters, you use type="2"
. For news filters, you use type="8"
.
Message Object Reference
Filter functions take one argument, a message object. For news filters it is a News Message object and for mail filters it is a Mail Message object.
Mail Messages
A Mail Message object has the following methods:
Method | Description |
---|---|
killThread() | Mark a thread as ignored. |
watchThread() | Mark a thread as watched. |
trash() | Mark the message read and move it to the trash folder. |
A Mail Message object has the following properties:
Property | Description |
---|---|
folder | Reflects the folder containing the message. |
read | Reflects whether or not the message has been read. |
priority | Reflects the priority of the message. |
To refile a mail message, you set the folder
property of the message object. You can use either a full path or the mailbox:
URL syntax to specify the destination.
The priority property can be set using either an integer or a string. The possible values are:
message.subject
and the CC list as message.cc
. Headers with hyphens in their names (such as Resent-from
) cannot be retrieved with the dot syntax. Instead, retrieve them using the array syntax for a property value (such as message["Resent-from"]
).
Method | Description |
---|---|
killThread() | Mark a thread as ignored. |
watchThread() | Mark a thread as watched. |
A News Message object has the following properties:
Debugging your filters
If there is a problem with your JavaScript filters, you'll get the standard JavaScript alert telling you the nature of the error. Any filters affected by the problems are not used to filter your messages. Consequently, if you've got problems, all the mail remains unchanged in your Inbox.
A more complex example
This filter file lets you easily perform one of several changes to a message. First, it uses object initializers to create an array of objects. Each of those objects represents a set of messages and what the function will do with messages in that set. The objects can have the following properties:
Once it has the array of filters, the code creates regular expressions from those filters to use in matching individual messages. When Messenger calls ApplyFilters
for a message, it searches for a match in the MyFilters
array. If it finds one, the function either puts the message in the trash, moves it to a new folder, or changes its priority.
var MyFilters = [
{field:"From", probe:"cltbld@netscape.com", folder:"mailbox:Client Build"},
{field:"From", probe:"scopus@netscape.com", folder:"mailbox:Scopus"},
{field:"Resent-From", probe:"bonsai-hook@warp.mcom.com", trash:true"},
{field:"Resent-From", probe:"xheads@netscape.com", folder:"mailbox:X Heads"},
{field:"Resent-From", probe:"layers@netscape.com", priority:"High"}
];// Initialize by compiling a regular expression for each filter
for (var i = 0; i < MyFilters.length; i++) {
var f = MyFilters[i];
f.regexp = new RegExp("^" + f.field + " *:.*" + f.probe, "i");
}function ApplyFilters(message)
{
trace("Applying mail filters"); for (var i = 0; i < MyFilters.length; i++) {
var f = MyFilters[i];
if (f.regexp.test()) {
if (f.trash) {
message.trash();
} else if (f.folder) {
message.folder = f.folder;
} else {
message.priority = f.priority;
continue;
}
break;
}
}
}
Last Updated: 11/26/97 09:26:12