3 * Message and Spam Filter Plugin
4 * By Luke Ehresman <luke@squirrelmail.org>
7 * (c) 2000 (GNU GPL - see ../../COPYING)
9 * This plugin filters your inbox into different folders based upon given
10 * criteria. It is most useful for people who are subscibed to mailing lists
11 * to help organize their messages. The argument stands that filtering is
12 * not the place of the client, which is why this has been made a plugin for
13 * SquirrelMail. You may be better off using products such as Sieve or
14 * Procmail to do your filtering so it happens even when SquirrelMail isn't
17 * If you need help with this, or see improvements that can be made, please
18 * email me directly at the address above. I definately welcome suggestions
19 * and comments. This plugin, as is the case with all SquirrelMail plugins,
20 * is not directly supported by the developers. Please come to me off the
21 * mailing list if you have trouble with it.
23 * Also view plugins/README.plugins for more information.
27 require_once('../src/validate.php');
28 require_once('../functions/page_header.php');
29 require_once('../functions/imap.php');
30 require_once('../src/load_prefs.php');
32 global $AllowSpamFilters;
34 displayPageHeader($color, 'None');
36 if (isset($filter_submit)) {
37 if (!isset($theid)) $theid = 0;
38 $filter_what = str_replace(',', ' ', $filter_what);
39 $filter_what = str_replace("\\\\", "\\", $filter_what);
40 $filter_what = str_replace("\\\"", "\"", $filter_what);
41 $filter_what = str_replace("\"", """, $filter_what);
43 setPref($data_dir, $username, 'filter'.$theid, $filter_where.','.$filter_what.','.$filter_folder);
44 $filters[$theid]['where'] = $filter_where;
45 $filters[$theid]['what'] = $filter_what;
46 $filters[$theid]['folder'] = $filter_folder;
47 } elseif (isset($spam_submit) && $AllowSpamFilters) {
48 $spam_filters = load_spam_filters();
49 setPref($data_dir, $username, 'filters_spam_folder', $filters_spam_folder_set);
50 setPref($data_dir, $username, 'filters_spam_scan', $filters_spam_scan_set);
51 foreach ($spam_filters as $Key => $Value)
53 $input = $spam_filters[$Key]['prefname'] . '_set';
54 setPref($data_dir, $username, $spam_filters[$Key]['prefname'],
57 } elseif (isset($action) && $action == 'delete') {
58 remove_filter($theid);
59 } elseif (isset($action) && $action == 'move_up') {
60 filter_swap($theid, $theid - 1);
61 } elseif (isset($action) && $action == 'move_down') {
62 filter_swap($theid, $theid +
1);
65 if ($AllowSpamFilters) {
66 $filters_spam_folder = getPref($data_dir, $username, 'filters_spam_folder');
67 $filters_spam_scan = getPref($data_dir, $username, 'filters_spam_scan');
69 $filters = load_filters();
72 '<table width=95% align=center border=0 cellpadding=2 cellspacing=0>'.
73 "<tr><td bgcolor=\"$color[0]\">".
74 '<center><b>' . _("Options") . ' - ' . _("Message Filtering") . '</b></center>'.
76 '<br><center>[<a href="options.php?action=add">' . _("New") .
77 '</a>] - [<a href="../../src/options.php">' . _("Done") . '</a>]</center><br>' .
78 '<table border=0 cellpadding=3 cellspacing=0 align=center>';
80 for ($i=0; $i < count($filters); $i++
) {
87 $fdr = ($folder_prefix)?
str_replace($folder_prefix, "", $filters[$i]["folder"]):$filters[$i]["folder"];
89 echo "<tr bgcolor=\"$clr\"><td><small>".
90 "[<a href=\"options.php?theid=$i&action=edit\">" . _("Edit") . '</a>]'.
91 '</small></td><td><small>'.
92 "[<a href=\"options.php?theid=$i&action=delete\">" . _("Delete") . '</a>]'.
93 '</small></td><td align=center><small>';
95 if (isset($filters[$i +
1])) {
96 echo "<a href=\"options.php?theid=$i&action=move_down\">" . _("Down") . '</a>';
102 echo "<a href=\"options.php?theid=$i&action=move_up\">" . _("Up") . '</a>';
104 echo ']</small></td><td> - ';
105 printf( _("If <b>%s</b> contains <b>%s</b> then move to <b>%s</b>"), $filters[$i]['where'], $filters[$i]['what'], $fdr );
111 '<table width=80% align=center border=0 cellpadding=2 cellspacing=0">'.
112 '<tr><td> </td></tr>'.
115 if ($AllowSpamFilters) {
117 echo "<table width=95% align=center border=0 cellpadding=2 cellspacing=0 bgcolor=\"$color[0]\">".
118 '<tr><th align=center>' . _("Spam Filtering") . '</th></tr>'.
120 if (! isset($action) ||
$action != 'spam') {
122 echo '<p align=center>[<a href="options.php?action=spam">' . _("Edit") . '</a>]<br>';
123 printf( _("Spam is sent to <b>%s</b>"), ($filters_spam_folder?
$filters_spam_folder:_("[<i>not set yet</i>]") ) );
125 printf( _("Spam scan is limited to <b>%s</b>"), (($filters_spam_scan == 'new')?
_("New Messages Only"):_("All Messages") ) );
127 "<table border=0 cellpadding=3 cellspacing=0 align=center bgcolor=\"$color[0]\">";
129 $spam_filters = load_spam_filters();
131 foreach ($spam_filters as $Key => $Value) {
132 echo '<tr><th align=center>';
134 if ($spam_filters[$Key]['enabled']) {
140 echo '</th><td> - </td><td>';
142 if ($spam_filters[$Key]['link']) {
144 $spam_filters[$Key]['link'] .
145 '" target="_blank">';
148 echo $spam_filters[$Key]['name'];
149 if ($spam_filters[$Key]['link']) {
158 if (isset($action) && ($action == 'add' ||
$action == 'edit')) {
159 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
160 $boxes = sqimap_mailbox_list($imapConnection);
161 sqimap_logout($imapConnection);
162 if ( !isset($theid) ) {
163 $theid = count($filters);
166 '<form action="options.php" method=post>'.
167 '<br><table cellpadding=2 cellspacing=0 border=0>'.
171 '<select name=filter_where>';
173 $L = isset($filters[$theid]['where']);
175 $sel = (($L && $filters[$theid]['where'] == 'From')?
'selected':'');
176 echo "<option value=\"From\" $sel>" . _ ("From") . '</option>';
178 $sel = (($L && $filters[$theid]['where'] == 'To')?
'selected':'');
179 echo "<option value=\"To\" $sel>" . _ ("To") . '</option>';
181 $sel = (($L && $filters[$theid]['where'] == 'Cc')?
'selected':'');
182 echo "<option value=\"Cc\" $sel>" . _ ("Cc") . '</option>';
184 $sel = (($L && $filters[$theid]['where'] == 'To or Cc')?
'selected':'');
185 echo "<option value=\"To or Cc\" $sel>" . _ ("To or Cc") . '</option>';
187 $sel = (($L && $filters[$theid]['where'] == 'Subject')?
'selected':'');
188 echo "<option value=\"Subject\" $sel>" . _ ("Subject") . '</option>';
198 '<input type=text size=32 name=filter_what value="';
199 if (isset($filters[$theid]['what'])) {
200 echo $filters[$theid]["what"];
211 '<select name=filter_folder>';
213 for ($i = 0; $i < count($boxes); $i++
) {
214 if (! in_array('noselect', $boxes[$i]['flags'])) {
215 $box = $boxes[$i]['unformatted'];
216 $box2 = str_replace(' ', ' ', $boxes[$i]['formatted']);
217 if (isset($filters[$theid]['folder']) &&
218 $filters[$theid]['folder'] == $box)
219 echo "<OPTION VALUE=\"$box\" SELECTED>$box2</option>";
221 echo "<OPTION VALUE=\"$box\">$box2</option>";
229 '<input type=submit name=filter_submit value=' . _("Submit") . '>'.
230 "<input type=hidden name=theid value=$theid>".
234 } else if (isset($action) && $action == 'spam' && $AllowSpamFilters) {
235 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
236 $boxes = sqimap_mailbox_list($imapConnection);
237 sqimap_logout($imapConnection);
238 for ($i = 0; $i < count($boxes) && $filters_spam_folder == ''; $i++
) {
239 if ($boxes[$i]['flags'][0] != 'noselect' &&
240 $boxes[$i]['flags'][1] != 'noselect' &&
241 $boxes[$i]['flags'][2] != 'noselect') {
242 $filters_spam_folder = $boxes[$i]['unformatted'];
246 echo '<form method=post action="options.php">'.
248 '<table width=85% cellpadding=2 cellspacing=0 border=0>'.
250 '<th align=right nowrap>' . _("Move spam to:") . '</th>'.
251 '<td><select name="filters_spam_folder_set">';
253 for ($i = 0; $i < count($boxes); $i++
) {
254 if (! in_array('noselect', $boxes[$i]['flags'])) {
255 $box = $boxes[$i]['unformatted'];
256 $box2 = str_replace(' ', ' ', $boxes[$i]['formatted']);
257 if ($filters_spam_folder == $box)
258 echo "<OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n";
260 echo "<OPTION VALUE=\"$box\">$box2</OPTION>\n";
266 '<tr><td></td><td>' .
267 _("Moving spam directly to the trash may not be a good idea at first, since messages from friends and mailing lists might accidentally be marked as spam. Whatever folder you set this to, make sure that it gets cleaned out periodically, so that you don't have an excessively large mailbox hanging around.") .
270 '<th align=right nowrap>' . _("What to Scan:") . '</th>'.
271 '<td><select name="filters_spam_scan_set">'.
273 if ($filters_spam_scan == '')
275 echo '>' . _("All messages") . '</option>'.
276 '<option value="new"';
277 if ($filters_spam_scan == 'new')
279 echo '>' . _("Only unread messages") . '</option>' .
285 _("The more messages you scan, the longer it takes. I would suggest that you scan only new messages. If you make a change to your filters, I would set it to scan all messages, then go view my INBOX, then come back and set it to scan only new messages. That way, your new spam filters will be applied and you'll scan even the spam you read with the new filters.").
288 $spam_filters = load_spam_filters();
290 foreach ($spam_filters as $Key => $Value) {
291 echo "<tr><th align=right nowrap>$Key</th>\n" .
292 '<td><input type=checkbox name="' .
293 $spam_filters[$Key]['prefname'] .
295 if ($spam_filters[$Key]['enabled'])
298 if ($spam_filters[$Key]['link']) {
300 $spam_filters[$Key]['link'] .
301 '" target="_blank">';
303 echo $spam_filters[$Key]['name'];
304 if ($spam_filters[$Key]['link']) {
307 echo '</td></tr><tr><td></td><td>' .
308 $spam_filters[$Key]['comment'] .
311 echo '<tr><td colspan=2 align=center><input type=submit name="spam_submit" value="' . _("Save") . '"></td></tr>'.
316 sqimap_logout($imapConnection);