6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Copyright (c) 2000 by Michael Huttinger
8 * Licensed under the GNU GPL. For full terms see the file COPYING.
10 * Quite a hack -- but my first attempt at a plugin. We were
11 * looking for a way to play a sound when there was unseen
12 * messages to look at. Nice for users who keep the squirrel
13 * mail window up for long periods of time and want to know
16 * Basically, I hacked much of left_main.php into a plugin that
17 * goes through each mail folder and increments a flag if
18 * there are unseen messages. If the final count of unseen
19 * folders is > 0, then we play a sound (using the HTML at the
20 * far end of this script).
22 * This was tested with IE5.0 - but I hear Netscape works well,
23 * too (with a plugin).
28 function CheckNewMailboxSound($imapConnection, $mailbox, $real_box, $delimeter, $unseen, &$total_unseen) {
29 global $folder_prefix, $trash_folder, $sent_folder;
30 global $color, $move_to_sent, $move_to_trash;
31 global $unseen_notify, $unseen_type, $newmail_allbox, $newmail_recent;
32 global $newmail_changetitle;
34 $mailboxURL = urlencode($real_box);
37 // Skip folders for Sent and Trash
39 if ($real_box == $sent_folder ||
40 $real_box == $trash_folder) {
44 if (($unseen_notify == 2 && $real_box == 'INBOX') ||
45 ($unseen_notify == 3 && ($newmail_allbox == 'on' ||
46 $real_box == 'INBOX'))) {
47 $unseen = sqimap_unseen_messages($imapConnection, $real_box);
48 $total_unseen +
= $unseen;
50 if($newmail_recent == 'on') {
51 $unseen = sqimap_mailbox_select( $imapConnection, $real_box, TRUE, TRUE);
58 return( $unseen_found );
61 function squirrelmail_plugin_init_newmail() {
62 global $squirrelmail_plugin_hooks;
64 $squirrelmail_plugin_hooks['left_main_before']['newmail'] = 'newmail_plugin';
65 $squirrelmail_plugin_hooks['optpage_register_block']['newmail'] = 'newmail_optpage_register_block';
66 $squirrelmail_plugin_hooks['options_save']['newmail'] = 'newmail_sav';
67 $squirrelmail_plugin_hooks['loading_prefs']['newmail'] = 'newmail_pref';
70 function newmail_optpage_register_block() {
71 // Gets added to the user's OPTIONS page.
72 global $optpage_blocks;
76 /* Register Squirrelspell with the $optionpages array. */
77 $optpage_blocks[] = array(
78 'name' => _("NewMail Options"),
79 'url' => '../plugins/newmail/newmail_opt.php',
80 'desc' => _("This configures settings for playing sounds and/or showing popup windows when new mail arrives."),
86 function newmail_sav() {
88 global $username,$data_dir;
89 global $submit_newmail,$media_file,$media_reset,$media_enable,$media_popup;
90 global $media_recent,$media_sel;
91 global $media_allbox, $media_changetitle;
93 if ( isset($submit_newmail) ) {
95 if(isset($media_enable)) {
96 setPref($data_dir,$username,'newmail_enable',$media_enable);
98 setPref($data_dir,$username,'newmail_enable','');
100 if(isset($media_popup)) {
101 setPref($data_dir,$username,'newmail_popup',$media_popup);
103 setPref($data_dir,$username,'newmail_popup','');
105 if(isset($media_allbox)) {
106 setPref($data_dir,$username,'newmail_allbox',$media_allbox);
108 setPref($data_dir,$username,'newmail_allbox','');
110 if(isset($media_recent)) {
111 setPref($data_dir,$username,'newmail_recent',$media_recent);
113 setPref($data_dir,$username,'newmail_recent','');
115 if(isset($media_changetitle)) {
116 setPref($data_dir,$username,'newmail_changetitle',$media_changetitle);
118 setPref($data_dir,$username,'newmail_changetitle','');
120 if(isset($media_sel)) {
121 if($media_sel == '(local media)') {
122 setPref($data_dir,$username,'newmail_media',StripSlashes($media_file));
124 setPref($data_dir,$username,'newmail_media',$media_sel);
127 setPref($data_dir,$username,'newmail_media','');
129 echo html_tag( 'p', _("New Mail Notification options saved"), 'center' );
133 function newmail_pref() {
135 global $username,$data_dir;
136 global $newmail_media,$newmail_enable,$newmail_popup,$newmail_allbox;
137 global $newmail_recent, $newmail_changetitle;
139 $newmail_recent = getPref($data_dir,$username,'newmail_recent');
140 $newmail_enable = getPref($data_dir,$username,'newmail_enable');
141 $newmail_media = getPref($data_dir, $username, 'newmail_media', '../plugins/newmail/sounds/Notify.wav');
142 $newmail_popup = getPref($data_dir, $username, 'newmail_popup');
143 $newmail_allbox = getPref($data_dir, $username, 'newmail_allbox');
144 $newmail_changetitle = getPref($data_dir, $username, 'newmail_changetitle');
148 function newmail_plugin() {
150 global $username,$key,$imapServerAddress,$imapPort;
151 global $newmail_media,$newmail_enable,$newmail_popup,$newmail_recent;
152 global $newmail_changetitle;
153 global $imapConnection;
155 if ($newmail_enable == 'on' ||
156 $newmail_popup == 'on' ||
157 $newmail_changetitle) {
159 // open a connection on the imap port (143)
161 $boxes = sqimap_mailbox_list($imapConnection);
162 $delimeter = sqimap_get_delimiter($imapConnection);
167 for ($i = 0;$i < count($boxes); $i++
) {
170 $mailbox = $boxes[$i]['formatted'];
172 if (! isset($boxes[$i]['unseen']))
173 $boxes[$i]['unseen'] = '';
174 if ($boxes[$i]['flags']) {
176 for ($h = 0; $h < count($boxes[$i]['flags']); $h++
) {
177 if (strtolower($boxes[$i]["flags"][$h]) == 'noselect')
181 $status = $status +
CheckNewMailboxSound($imapConnection, $mailbox,
182 $boxes[$i]['unformatted'], $delimeter, $boxes[$i]['unseen'],
186 $status = $status +
CheckNewMailboxSound($imapConnection, $mailbox, $boxes[$i]['unformatted'],
187 $delimeter, $boxes[$i]['unseen'], $totalNew);
192 // sqimap_logout($imapConnection);
194 // If we found unseen messages, then we
195 // will play the sound as follows:
197 if ($newmail_changetitle) {
198 echo "<script language=\"javascript\">\n" .
199 "function ChangeTitleLoad() {\n";
200 if( $totalNew > 1 ||
$totalNew == 0 ) {
201 echo 'window.parent.document.title = "' .
202 sprintf(_("%s New Messages"), $totalNew ) .
205 echo 'window.parent.document.title = "' .
206 sprintf(_("%s New Message"), $totalNew ) .
209 echo "if (BeforeChangeTitle != null)\n".
210 "BeforeChangeTitle();\n".
212 "BeforeChangeTitle = window.onload;\n".
213 "window.onload = ChangeTitleLoad;\n".
217 if ($status > 0 && $newmail_enable == 'on') {
218 echo "<EMBED SRC=\"$newmail_media\" HIDDEN=TRUE AUTOSTART=TRUE>\n";
220 if ($status >0 && $newmail_popup == 'on') {
221 echo "<SCRIPT LANGUAGE=\"JavaScript\">\n".
223 "function PopupScriptLoad() {\n".
224 'window.open("../plugins/newmail/newmail.php", "SMPopup",'.
225 "\"width=200,height=130,scrollbars=no\");\n".
226 "if (BeforePopupScript != null)\n".
227 "BeforePopupScript();\n".
229 "BeforePopupScript = window.onload;\n".
230 "window.onload = PopupScriptLoad;\n".
231 // Idea by: Nic Wolfe (Nic@TimelapseProductions.com)
232 // Web URL: http://fineline.xs.mw
233 // More code from Tyler Akins