NeoMutt
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
from.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <stdbool.h>
31#include <stdio.h>
32#include <time.h>
33#include "mutt/lib.h"
34#include "from.h"
35
48bool is_from(const char *s, char *path, size_t pathlen, time_t *tp)
49{
50 bool lax = false;
51
52 const regmatch_t *match = mutt_prex_capture(PREX_MBOX_FROM, s);
53 if (!match)
54 {
56 if (!match)
57 {
58 mutt_debug(LL_DEBUG1, "Could not parse From line: <%s>\n", s);
59 return false;
60 }
61 lax = true;
62 mutt_debug(LL_DEBUG2, "Fallback regex for From line: <%s>\n", s);
63 }
64
65 if (path)
66 {
67 const regmatch_t *msender = &match[lax ? PREX_MBOX_FROM_LAX_MATCH_ENVSENDER : PREX_MBOX_FROM_MATCH_ENVSENDER];
68 const size_t dsize = MIN(pathlen, mutt_regmatch_len(msender) + 1);
69 mutt_str_copy(path, s + mutt_regmatch_start(msender), dsize);
70 }
71
72 if (tp)
73 {
74 // clang-format off
75 const regmatch_t *mmonth = &match[lax ? PREX_MBOX_FROM_LAX_MATCH_MONTH : PREX_MBOX_FROM_MATCH_MONTH];
76 const regmatch_t *mday = &match[lax ? PREX_MBOX_FROM_LAX_MATCH_DAY : PREX_MBOX_FROM_MATCH_DAY];
77 const regmatch_t *mtime = &match[lax ? PREX_MBOX_FROM_LAX_MATCH_TIME : PREX_MBOX_FROM_MATCH_TIME];
78 const regmatch_t *myear = &match[lax ? PREX_MBOX_FROM_LAX_MATCH_YEAR : PREX_MBOX_FROM_MATCH_YEAR];
79 // clang-format on
80
81 struct tm tm = { 0 };
82 tm.tm_isdst = -1;
83 tm.tm_mon = mutt_date_check_month(s + mutt_regmatch_start(mmonth));
84 sscanf(s + mutt_regmatch_start(mday), " %d", &tm.tm_mday);
85 sscanf(s + mutt_regmatch_start(mtime), "%d:%d:%d", &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
86 int year = 0;
87 sscanf(s + mutt_regmatch_start(myear), "%d", &year);
88 if (year > 1900)
89 tm.tm_year = year - 1900;
90 else if (year < 70)
91 tm.tm_year = year + 100;
92 else
93 tm.tm_year = year;
94 *tp = mutt_date_make_time(&tm, false);
95 }
96
97 return true;
98}
bool is_from(const char *s, char *path, size_t pathlen, time_t *tp)
Is a string a 'From' header line?
Definition: from.c:48
Determine who the email is from.
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
#define MIN(a, b)
Definition: memory.h:32
time_t mutt_date_make_time(struct tm *t, bool local)
Convert struct tm to time_t
Definition: date.c:232
int mutt_date_check_month(const char *s)
Is the string a valid month name.
Definition: date.c:422
Convenience wrapper for the library headers.
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:653
regmatch_t * mutt_prex_capture(enum Prex which, const char *str)
Match a precompiled regex against a string.
Definition: prex.c:289
@ PREX_MBOX_FROM_LAX_MATCH_MONTH
From god@heaven.af.mil Sat [Jan] 3 01:05:34 1996
Definition: prex.h:201
@ PREX_MBOX_FROM_LAX_MATCH_ENVSENDER
From [god at heaven.af.mil] Sat Jan 3 01:05:34 1996
Definition: prex.h:198
@ PREX_MBOX_FROM_LAX_MATCH_TIME
From god@heaven.af.mil Sat Jan 10 [01:05:34] 1996
Definition: prex.h:205
@ PREX_MBOX_FROM_LAX_MATCH_DAY
From god@heaven.af.mil Sat Jan [ 3] 01:05:34 1996
Definition: prex.h:202
@ PREX_MBOX_FROM_LAX_MATCH_YEAR
From god@heaven.af.mil Sat Jan 10 01:05:34 [1996]
Definition: prex.h:211
@ PREX_MBOX_FROM_MATCH_DAY
From god@heaven.af.mil Sat Jan [ 3] 01:05:34 1996
Definition: prex.h:182
@ PREX_MBOX_FROM_MATCH_MONTH
From god@heaven.af.mil Sat [Jan] 3 01:05:34 1996
Definition: prex.h:181
@ PREX_MBOX_FROM_MATCH_YEAR
From god@heaven.af.mil Sat Jan 10 01:05:34 [1996]
Definition: prex.h:186
@ PREX_MBOX_FROM_MATCH_ENVSENDER
From [god@heaven.af.mil] Sat Jan 3 01:05:34 1996
Definition: prex.h:179
@ PREX_MBOX_FROM_MATCH_TIME
From god@heaven.af.mil Sat Jan 10 [01:05:34] 1996
Definition: prex.h:185
@ PREX_MBOX_FROM_LAX
[From god@heaven.af.mil Sat Jan 3 01:05:34 1996]
Definition: prex.h:40
@ PREX_MBOX_FROM
[From god@heaven.af.mil Sat Jan 3 01:05:34 1996]
Definition: prex.h:39
static size_t mutt_regmatch_len(const regmatch_t *match)
Return the length of a match.
Definition: regex3.h:80
static regoff_t mutt_regmatch_start(const regmatch_t *match)
Return the start of a match.
Definition: regex3.h:60