Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | /* mailcheck.c -- The check is in the mail... */ |
| 2 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 3 | /* Copyright (C) 1987-2002 Free Software Foundation, Inc. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 4 | |
| 5 | This file is part of GNU Bash, the Bourne Again SHell. |
| 6 | |
| 7 | Bash is free software; you can redistribute it and/or modify it under |
| 8 | the terms of the GNU General Public License as published by the Free |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 9 | Software Foundation; either version 2, or (at your option) any later |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 10 | version. |
| 11 | |
| 12 | Bash is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 | for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License along |
| 18 | with Bash; see the file COPYING. If not, write to the Free Software |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 19 | Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 21 | #include "config.h" |
| 22 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include "bashtypes.h" |
| 25 | #include "posixstat.h" |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 26 | #ifndef _MINIX |
| 27 | # include <sys/param.h> |
| 28 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 29 | #if defined (HAVE_UNISTD_H) |
| 30 | # include <unistd.h> |
| 31 | #endif |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 32 | #include "posixtime.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 33 | #include "bashansi.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 34 | #include "bashintl.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 35 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 36 | #include "shell.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 37 | #include "execute_cmd.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 38 | #include "mailcheck.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 39 | #include <tilde/tilde.h> |
| 40 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 41 | extern int mailstat __P((const char *, struct stat *)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 42 | |
| 43 | typedef struct { |
| 44 | char *name; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 45 | char *msg; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 46 | time_t access_time; |
| 47 | time_t mod_time; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 48 | off_t file_size; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 49 | } FILEINFO; |
| 50 | |
| 51 | /* The list of remembered mail files. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 52 | static FILEINFO **mailfiles = (FILEINFO **)NULL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 53 | |
| 54 | /* Number of mail files that we have. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 55 | static int mailfiles_count; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 56 | |
| 57 | /* The last known time that mail was checked. */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 58 | static time_t last_time_mail_checked; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 59 | |
| 60 | /* Non-zero means warn if a mail file has been read since last checked. */ |
| 61 | int mail_warning; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 62 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 63 | static int find_mail_file __P((char *)); |
| 64 | static void update_mail_file __P((int)); |
| 65 | static int add_mail_file __P((char *, char *)); |
| 66 | |
| 67 | static int file_mod_date_changed __P((int)); |
| 68 | static int file_access_date_changed __P((int)); |
| 69 | static int file_has_grown __P((int)); |
| 70 | |
| 71 | static char *parse_mailpath_spec __P((char *)); |
| 72 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 73 | /* Returns non-zero if it is time to check mail. */ |
| 74 | int |
| 75 | time_to_check_mail () |
| 76 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 77 | char *temp; |
| 78 | time_t now; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 79 | intmax_t seconds; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 80 | |
| 81 | temp = get_string_value ("MAILCHECK"); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 82 | |
| 83 | /* Negative number, or non-numbers (such as empty string) cause no |
| 84 | checking to take place. */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 85 | if (temp == 0 || legal_number (temp, &seconds) == 0 || seconds < 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 86 | return (0); |
| 87 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 88 | now = NOW; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 89 | /* Time to check if MAILCHECK is explicitly set to zero, or if enough |
| 90 | time has passed since the last check. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 91 | return (seconds == 0 || ((now - last_time_mail_checked) >= seconds)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* Okay, we have checked the mail. Perhaps I should make this function |
| 95 | go away. */ |
| 96 | void |
| 97 | reset_mail_timer () |
| 98 | { |
| 99 | last_time_mail_checked = NOW; |
| 100 | } |
| 101 | |
| 102 | /* Locate a file in the list. Return index of |
| 103 | entry, or -1 if not found. */ |
| 104 | static int |
| 105 | find_mail_file (file) |
| 106 | char *file; |
| 107 | { |
| 108 | register int i; |
| 109 | |
| 110 | for (i = 0; i < mailfiles_count; i++) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 111 | if (STREQ (mailfiles[i]->name, file)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 112 | return i; |
| 113 | |
| 114 | return -1; |
| 115 | } |
| 116 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 117 | #define RESET_MAIL_FILE(i) \ |
| 118 | do \ |
| 119 | { \ |
| 120 | mailfiles[i]->access_time = mailfiles[i]->mod_time = 0; \ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 121 | mailfiles[i]->file_size = 0; \ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 122 | } \ |
| 123 | while (0) |
| 124 | |
| 125 | static void |
| 126 | update_mail_file (i) |
| 127 | int i; |
| 128 | { |
| 129 | char *file; |
| 130 | struct stat finfo; |
| 131 | |
| 132 | file = mailfiles[i]->name; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 133 | if (mailstat (file, &finfo) == 0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 134 | { |
| 135 | mailfiles[i]->access_time = finfo.st_atime; |
| 136 | mailfiles[i]->mod_time = finfo.st_mtime; |
| 137 | mailfiles[i]->file_size = finfo.st_size; |
| 138 | } |
| 139 | else |
| 140 | RESET_MAIL_FILE (i); |
| 141 | } |
| 142 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 143 | /* Add this file to the list of remembered files and return its index |
| 144 | in the list of mail files. */ |
| 145 | static int |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 146 | add_mail_file (file, msg) |
| 147 | char *file, *msg; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 148 | { |
| 149 | struct stat finfo; |
| 150 | char *filename; |
| 151 | int i; |
| 152 | |
| 153 | filename = full_pathname (file); |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 154 | i = find_mail_file (filename); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 155 | if (i >= 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 156 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 157 | if (mailstat (filename, &finfo) == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 158 | { |
| 159 | mailfiles[i]->mod_time = finfo.st_mtime; |
| 160 | mailfiles[i]->access_time = finfo.st_atime; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 161 | mailfiles[i]->file_size = finfo.st_size; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 162 | } |
| 163 | free (filename); |
| 164 | return i; |
| 165 | } |
| 166 | |
| 167 | i = mailfiles_count++; |
| 168 | mailfiles = (FILEINFO **)xrealloc |
| 169 | (mailfiles, mailfiles_count * sizeof (FILEINFO *)); |
| 170 | |
| 171 | mailfiles[i] = (FILEINFO *)xmalloc (sizeof (FILEINFO)); |
| 172 | mailfiles[i]->name = filename; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 173 | mailfiles[i]->msg = msg ? savestring (msg) : (char *)NULL; |
| 174 | update_mail_file (i); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 175 | return i; |
| 176 | } |
| 177 | |
| 178 | /* Reset the existing mail files access and modification times to zero. */ |
| 179 | void |
| 180 | reset_mail_files () |
| 181 | { |
| 182 | register int i; |
| 183 | |
| 184 | for (i = 0; i < mailfiles_count; i++) |
| 185 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 186 | RESET_MAIL_FILE (i); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | /* Free the information that we have about the remembered mail files. */ |
| 191 | void |
| 192 | free_mail_files () |
| 193 | { |
| 194 | register int i; |
| 195 | |
| 196 | for (i = 0; i < mailfiles_count; i++) |
| 197 | { |
| 198 | free (mailfiles[i]->name); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 199 | FREE (mailfiles[i]->msg); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 200 | free (mailfiles[i]); |
| 201 | } |
| 202 | |
| 203 | if (mailfiles) |
| 204 | free (mailfiles); |
| 205 | |
| 206 | mailfiles_count = 0; |
| 207 | mailfiles = (FILEINFO **)NULL; |
| 208 | } |
| 209 | |
| 210 | /* Return non-zero if FILE's mod date has changed and it has not been |
| 211 | accessed since modified. */ |
| 212 | static int |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 213 | file_mod_date_changed (i) |
| 214 | int i; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 215 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 216 | time_t mtime; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 217 | struct stat finfo; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 218 | char *file; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 219 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 220 | file = mailfiles[i]->name; |
| 221 | mtime = mailfiles[i]->mod_time; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 222 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 223 | if ((mailstat (file, &finfo) == 0) && (finfo.st_size > 0)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 224 | return (mtime != finfo.st_mtime); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 225 | |
| 226 | return (0); |
| 227 | } |
| 228 | |
| 229 | /* Return non-zero if FILE's access date has changed. */ |
| 230 | static int |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 231 | file_access_date_changed (i) |
| 232 | int i; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 233 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 234 | time_t atime; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 235 | struct stat finfo; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 236 | char *file; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 237 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 238 | file = mailfiles[i]->name; |
| 239 | atime = mailfiles[i]->access_time; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 240 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 241 | if ((mailstat (file, &finfo) == 0) && (finfo.st_size > 0)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 242 | return (atime != finfo.st_atime); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 243 | |
| 244 | return (0); |
| 245 | } |
| 246 | |
| 247 | /* Return non-zero if FILE's size has increased. */ |
| 248 | static int |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 249 | file_has_grown (i) |
| 250 | int i; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 251 | { |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 252 | off_t size; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 253 | struct stat finfo; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 254 | char *file; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 255 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 256 | file = mailfiles[i]->name; |
| 257 | size = mailfiles[i]->file_size; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 258 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 259 | return ((mailstat (file, &finfo) == 0) && (finfo.st_size > size)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 262 | /* Take an element from $MAILPATH and return the portion from |
| 263 | the first unquoted `?' or `%' to the end of the string. This is the |
| 264 | message to be printed when the file contents change. */ |
| 265 | static char * |
| 266 | parse_mailpath_spec (str) |
| 267 | char *str; |
| 268 | { |
| 269 | char *s; |
| 270 | int pass_next; |
| 271 | |
| 272 | for (s = str, pass_next = 0; s && *s; s++) |
| 273 | { |
| 274 | if (pass_next) |
| 275 | { |
| 276 | pass_next = 0; |
| 277 | continue; |
| 278 | } |
| 279 | if (*s == '\\') |
| 280 | { |
| 281 | pass_next++; |
| 282 | continue; |
| 283 | } |
| 284 | if (*s == '?' || *s == '%') |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 285 | return s; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 286 | } |
| 287 | return ((char *)NULL); |
| 288 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 289 | |
| 290 | char * |
| 291 | make_default_mailpath () |
| 292 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 293 | #if defined (DEFAULT_MAIL_DIRECTORY) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 294 | char *mp; |
| 295 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 296 | get_current_user_info (); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 297 | mp = (char *)xmalloc (2 + sizeof (DEFAULT_MAIL_DIRECTORY) + strlen (current_user.user_name)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 298 | strcpy (mp, DEFAULT_MAIL_DIRECTORY); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 299 | mp[sizeof(DEFAULT_MAIL_DIRECTORY) - 1] = '/'; |
| 300 | strcpy (mp + sizeof (DEFAULT_MAIL_DIRECTORY), current_user.user_name); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 301 | return (mp); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 302 | #else |
| 303 | return ((char *)NULL); |
| 304 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 307 | /* Remember the dates of the files specified by MAILPATH, or if there is |
| 308 | no MAILPATH, by the file specified in MAIL. If neither exists, use a |
| 309 | default value, which we randomly concoct from using Unix. */ |
| 310 | void |
| 311 | remember_mail_dates () |
| 312 | { |
| 313 | char *mailpaths; |
| 314 | char *mailfile, *mp; |
| 315 | int i = 0; |
| 316 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 317 | mailpaths = get_string_value ("MAILPATH"); |
| 318 | |
| 319 | /* If no $MAILPATH, but $MAIL, use that as a single filename to check. */ |
| 320 | if (mailpaths == 0 && (mailpaths = get_string_value ("MAIL"))) |
| 321 | { |
| 322 | add_mail_file (mailpaths, (char *)NULL); |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | if (mailpaths == 0) |
| 327 | { |
| 328 | mailpaths = make_default_mailpath (); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 329 | if (mailpaths) |
| 330 | { |
| 331 | add_mail_file (mailpaths, (char *)NULL); |
| 332 | free (mailpaths); |
| 333 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 334 | return; |
| 335 | } |
| 336 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 337 | while (mailfile = extract_colon_unit (mailpaths, &i)) |
| 338 | { |
| 339 | mp = parse_mailpath_spec (mailfile); |
| 340 | if (mp && *mp) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 341 | *mp++ = '\0'; |
| 342 | add_mail_file (mailfile, mp); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 343 | free (mailfile); |
| 344 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /* check_mail () is useful for more than just checking mail. Since it has |
| 348 | the paranoids dream ability of telling you when someone has read your |
| 349 | mail, it can just as easily be used to tell you when someones .profile |
| 350 | file has been read, thus letting one know when someone else has logged |
| 351 | in. Pretty good, huh? */ |
| 352 | |
| 353 | /* Check for mail in some files. If the modification date of any |
| 354 | of the files in MAILPATH has changed since we last did a |
| 355 | remember_mail_dates () then mention that the user has mail. |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 356 | Special hack: If the variable MAIL_WARNING is non-zero and the |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 357 | mail file has been accessed since the last time we remembered, then |
| 358 | the message "The mail in <mailfile> has been read" is printed. */ |
| 359 | void |
| 360 | check_mail () |
| 361 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 362 | char *current_mail_file, *message; |
| 363 | int i, use_user_notification; |
| 364 | char *dollar_underscore, *temp; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 365 | |
| 366 | dollar_underscore = get_string_value ("_"); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 367 | if (dollar_underscore) |
| 368 | dollar_underscore = savestring (dollar_underscore); |
| 369 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 370 | for (i = 0; i < mailfiles_count; i++) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 371 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 372 | current_mail_file = mailfiles[i]->name; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 373 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 374 | if (*current_mail_file == '\0') |
| 375 | continue; |
| 376 | |
| 377 | if (file_mod_date_changed (i)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 378 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 379 | int file_is_bigger; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 380 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 381 | use_user_notification = mailfiles[i]->msg != (char *)NULL; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 382 | message = mailfiles[i]->msg ? mailfiles[i]->msg : _("You have mail in $_"); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 383 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 384 | bind_variable ("_", current_mail_file); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 385 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 386 | #define atime mailfiles[i]->access_time |
| 387 | #define mtime mailfiles[i]->mod_time |
| 388 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 389 | /* Have to compute this before the call to update_mail_file, which |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 390 | resets all the information. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 391 | file_is_bigger = file_has_grown (i); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 392 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 393 | update_mail_file (i); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 394 | |
| 395 | /* If the user has just run a program which manipulates the |
| 396 | mail file, then don't bother explaining that the mail |
| 397 | file has been manipulated. Since some systems don't change |
| 398 | the access time to be equal to the modification time when |
| 399 | the mail in the file is manipulated, check the size also. If |
| 400 | the file has not grown, continue. */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 401 | if ((atime >= mtime) && !file_is_bigger) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 402 | continue; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 403 | |
| 404 | /* If the mod time is later than the access time and the file |
| 405 | has grown, note the fact that this is *new* mail. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 406 | if (use_user_notification == 0 && (atime < mtime) && file_is_bigger) |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 407 | message = _("You have new mail in $_"); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 408 | #undef atime |
| 409 | #undef mtime |
| 410 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 411 | if (temp = expand_string_to_string (message, Q_DOUBLE_QUOTES)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 412 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 413 | puts (temp); |
| 414 | free (temp); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 415 | } |
| 416 | else |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 417 | putchar ('\n'); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 420 | if (mail_warning && file_access_date_changed (i)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 421 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 422 | update_mail_file (i); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame^] | 423 | printf (_("The mail in %s has been read\n"), current_mail_file); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 424 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 425 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 426 | |
| 427 | if (dollar_underscore) |
| 428 | { |
| 429 | bind_variable ("_", dollar_underscore); |
| 430 | free (dollar_underscore); |
| 431 | } |
| 432 | else |
| 433 | unbind_variable ("_"); |
| 434 | } |