1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
/* $Id$ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "fdm.h"
int tzlookup(const char *, int *);
char *
rfc822time(time_t t, char *buf, size_t len)
{
struct tm *tm;
size_t n;
tm = localtime(&t);
if ((n = strftime(buf, len, "%a, %d %b %Y %H:%M:%S %z", tm)) == 0)
return (NULL);
if (n == len)
return (NULL);
return (buf);
}
/*
* Some mailers, notably AOL's, use the timezone string instead of an offset
* from UTC. A limited set of these are permitted by RFC822, but it is still
* highly annoying: others can appear, and since there are duplicate
* abbreviations it cannot be converted with absolute certainty. As it is only
* a few clients do this anyway, don't even try particularly hard, just try to
* look it up using tzset, which catches the few most common abbreviations.
*/
int
tzlookup(const char *tz, int *off)
{
char *saved_tz;
struct tm *tm;
time_t t;
saved_tz = getenv("TZ");
if (saved_tz != NULL)
saved_tz = xstrdup(saved_tz);
/* Set the new timezone. */
if (setenv("TZ", tz, 1) != 0)
goto error;
tzset();
/* Get the time at epoch + one year. */
t = TIME_YEAR;
tm = localtime(&t);
/* And work out the timezone. */
if (strcmp(tz, tm->tm_zone) != 0)
goto error;
*off = tm->tm_gmtoff;
/* Restore the old timezone. */
if (saved_tz != NULL) {
if (setenv("TZ", saved_tz, 1) != 0)
goto error;
xfree(saved_tz);
} else
unsetenv("TZ");
tzset();
return (0);
error:
if (saved_tz != NULL)
xfree(saved_tz);
return (-1);
}
int
mailtime(struct mail *m, time_t *tim)
{
char *s, *ptr, *endptr, *hdr;
const char *errstr;
size_t len;
struct tm tm;
int tz;
hdr = find_header(m, "date", &len, 1);
if (hdr == NULL || len == 0)
return (-1);
/* Make a copy of the header. */
s = xmalloc(len + 1);
strlcpy(s, hdr, len + 1);
/* Skip spaces. */
ptr = s;
while (*ptr != '\0' && isspace((u_char) *ptr))
ptr++;
/* Parse the date. */
memset(&tm, 0, sizeof tm);
endptr = strptime(ptr, "%a, %d %b %Y %H:%M:%S", &tm);
if (endptr == NULL)
endptr = strptime(ptr, "%d %b %Y %H:%M:%S", &tm);
if (endptr == NULL)
goto invalid;
*tim = mktime(&tm);
/* Skip spaces. */
while (*endptr != '\0' && isspace((u_char) *endptr))
endptr++;
/* Terminate the timezone. */
ptr = endptr;
while (*ptr != '\0' && !isspace((u_char) *ptr))
ptr++;
*ptr = '\0';
tz = strtonum(endptr, -2359, 2359, &errstr);
if (errstr != NULL) {
/* Try it using tzset. */
if (tzlookup(endptr, &tz) != 0)
goto invalid;
}
*tim -= (tz / 100) * TIME_HOUR + (tz % 100) * TIME_MINUTE;
if (*tim < 0)
goto invalid;
xfree(s);
return (0);
invalid:
xfree(s);
return (-1);
}
|