1 /* $Id: mail.c,v 1.4 2006-01-21 22:56:04 bjk Exp $ */
3 Copyright (C) 2001-2005 Ben Kibbey <bjk@arbornet.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sys/types.h>
37 #include "../strsep.c"
40 void ui_module_init(int *chainable
)
43 fprintf(stderr
, "%s: ui_module_init()\n", __FILE__
);
53 fprintf(stderr
, "%s: ui_module_exit()\n", __FILE__
);
57 munmap(aliasbuf
, strlen(aliasbuf
));
63 /* Remove characters (rm) from string (str). */
64 static char *stripstr(char *str
, char *rm
)
66 static char buf
[LINE_MAX
];
70 if (rm
== NULL
|| str
== NULL
)
92 /* Return a string of mail aliases for the user. Looks in /etc/aliases (or
93 * whatever was specified at compile-time). The file is read into a buffer
94 * only once (mmap(2)). */
95 static char *mail_aliases(const char *user
, const int multi
)
98 static char aliases
[LINE_MAX
], *p
;
102 char m
[2] = { multi
, '\0' };
107 if ((!aliasbuf
&& firstrun
) || aliasbuf
== MAP_FAILED
)
113 if (stat(ALIAS_FILE
, &st
) == -1)
116 if ((fd
= open(ALIAS_FILE
, O_RDONLY
)) == -1)
119 if ((aliasbuf
= mmap(0, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
,
121 warn("%s", "mmap()");
128 for (i
= n
= 0; aliasbuf
[i
]; i
++) {
129 char *last
, *name
, *tmp
;
131 while (aliasbuf
[i
] != '\n')
132 t
[n
++] = aliasbuf
[i
++];
137 if (t
[0] == '#' || t
[0] == '\0')
142 if ((name
= strsep(&last
, ":")) == NULL
)
145 if (strcmp(user
, name
) == 0) {
146 while ((tmp
= strsep(&last
, ",")) != NULL
) {
147 tmp
= stripstr(tmp
, " \n\t");
149 strncat(aliases
, tmp
, sizeof(aliases
));
150 strncat(aliases
, m
, sizeof(aliases
));
156 while ((tmp
= strsep(&last
, ",")) != NULL
) {
157 tmp
= stripstr(tmp
, " \n\t");
159 if (strcmp(user
, tmp
) == 0) {
160 strncat(aliases
, name
, sizeof(aliases
));
161 strncat(aliases
, m
, sizeof(aliases
));
166 if (aliases
[0] == '\0')
169 aliases
[strlen(aliases
) - 1] = '\0';
175 /* Returns a string of forward aliases for the user. Reads ~/.forward if it
176 * exists and is readable. */
177 static char *forwards(const char *dir
, const int multi
)
180 char buf
[LINE_MAX
], *s
;
181 static char buf2
[FILENAME_MAX
];
183 char m
[2] = { multi
, '\0' };
185 buf
[0] = buf2
[0] = '\0';
186 snprintf(buf2
, sizeof(buf2
), "%s/.forward", dir
);
188 if ((fp
= fopen(buf2
, "r")) == NULL
) {
197 while ((s
= fgets(buf
, sizeof(buf
), fp
)) != NULL
) {
201 if (buf
[strlen(buf
) - 1] == '\n')
202 buf
[strlen(buf
) - 1] = '\0';
205 strncat(buf2
, m
, sizeof(buf2
));
207 strncat(buf2
, buf
, sizeof(buf2
));
219 /* /var/mail/username folder size in bytes. */
220 static char *foldersize(struct stat st
)
222 static char str
[33], *p
;
226 snprintf(str
, sizeof(str
), "%lu", (unsigned long) st
.st_size
);
231 /* This is output if the -h command line option is passed to the main program.
233 void ui_module_help()
236 fprintf(stderr
, "%s: ui_module_help()\n", __FILE__
);
239 printf(" Mail information [-M (-%s)]:\n", MAIL_OPTION_ORDER
);
240 printf("\t-f forwarding addresses\t");
241 printf("-a mail aliases\n");
242 printf("\t-r folder access (read) time\t");
243 printf("-m folder modification time\n");
244 printf("\t-s folder size\n\n");
248 /* This is the equivalent to main() only without argc and argv available. */
249 int ui_module_exec(char ***s
, const struct passwd
*pw
, const int multi_char
,
250 const int verbose
, char *tf
)
255 char folder
[FILENAME_MAX
];
259 snprintf(folder
, sizeof(folder
), "%s/%s", _PATH_MAILDIR
, pw
->pw_name
);
261 if (stat(folder
, &st
) != -1)
267 add_string(&strings
, (gotstat
) ? foldersize(st
) : "!");
270 add_string(&strings
, (gotstat
) ? stamp(st
.st_atime
, tf
) : "!");
273 add_string(&strings
, (gotstat
) ? stamp(st
.st_mtime
, tf
) : "!");
276 add_string(&strings
, forwards(pw
->pw_dir
, multi_char
));
279 add_string(&strings
, mail_aliases(pw
->pw_name
, multi_char
));
292 char *ui_module_options_init(char **defaults
)
295 return MAIL_OPTION_STRING
;
298 /* Check module option validity. */
299 int ui_module_options(int argc
, char **argv
)
305 fprintf(stderr
, "%s: ui_module_options()\n", __FILE__
);
308 while ((opt
= getopt(argc
, argv
, MAIL_OPTION_STRING
)) != -1) {
318 warnx("mail: invalid option -- %c", optopt
);
324 * This option '-M' sets all available options for this module.
327 strncpy(options
, MAIL_OPTION_ORDER
, sizeof(options
));