NeoMutt  2024-10-02-37-gfa9146
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
from.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <stdbool.h>
32#include <stdio.h>
33#include <time.h>
34#include "mutt/lib.h"
35#include "from.h"
36
49bool is_from(const char *s, char *path, size_t pathlen, time_t *tp)
50{
51 bool lax = false;
52
53 const regmatch_t *match = mutt_prex_capture(PREX_MBOX_FROM, s);
54 if (!match)
55 {
57 if (!match)
58 {
59 mutt_debug(LL_DEBUG1, "Could not parse From line: <%s>\n", s);
60 return false;
61 }
62 lax = true;
63 mutt_debug(LL_DEBUG2, "Fallback regex for From line: <%s>\n", s);
64 }
65
66 if (path)
67 {
68 const regmatch_t *msender = &match[lax ? PREX_MBOX_FROM_LAX_MATCH_ENVSENDER : PREX_MBOX_FROM_MATCH_ENVSENDER];
69 const size_t dsize = MIN(pathlen, mutt_regmatch_len(msender) + 1);
70 mutt_str_copy(path, s + mutt_regmatch_start(msender), dsize);
71 }
72
73 if (tp)
74 {
75 // clang-format off
76 const regmatch_t *mmonth = &match[lax ? PREX_MBOX_FROM_LAX_MATCH_MONTH : PREX_MBOX_FROM_MATCH_MONTH];
77 const regmatch_t *mday = &match[lax ? PREX_MBOX_FROM_LAX_MATCH_DAY : PREX_MBOX_FROM_MATCH_DAY];
78 const regmatch_t *mtime = &match[lax ? PREX_MBOX_FROM_LAX_MATCH_TIME : PREX_MBOX_FROM_MATCH_TIME];
79 const regmatch_t *myear = &match[lax ? PREX_MBOX_FROM_LAX_MATCH_YEAR : PREX_MBOX_FROM_MATCH_YEAR];
80 // clang-format on
81
82 struct tm tm = { 0 };
83 tm.tm_isdst = -1;
84 tm.tm_mon = mutt_date_check_month(s + mutt_regmatch_start(mmonth));
85 sscanf(s + mutt_regmatch_start(mday), " %d", &tm.tm_mday);
86 sscanf(s + mutt_regmatch_start(mtime), "%d:%d:%d", &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
87 int year = 0;
88 sscanf(s + mutt_regmatch_start(myear), "%d", &year);
89 if (year > 1900)
90 tm.tm_year = year - 1900;
91 else if (year < 70)
92 tm.tm_year = year + 100;
93 else
94 tm.tm_year = year;
95 *tp = mutt_date_make_time(&tm, false);
96 }
97
98 return true;
99}
bool is_from(const char *s, char *path, size_t pathlen, time_t *tp)
Is a string a 'From' header line?
Definition: from.c:49
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:242
int mutt_date_check_month(const char *s)
Is the string a valid month name.
Definition: date.c:432
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:581
regmatch_t * mutt_prex_capture(enum Prex which, const char *str)
Match a precompiled regex against a string.
Definition: prex.c:298
@ PREX_MBOX_FROM_LAX_MATCH_MONTH
From god@heaven.af.mil Sat [Jan] 3 01:05:34 1996
Definition: prex.h:203
@ PREX_MBOX_FROM_LAX_MATCH_ENVSENDER
From [god at heaven.af.mil] Sat Jan 3 01:05:34 1996
Definition: prex.h:200
@ PREX_MBOX_FROM_LAX_MATCH_TIME
From god@heaven.af.mil Sat Jan 10 [01:05:34] 1996
Definition: prex.h:207
@ PREX_MBOX_FROM_LAX_MATCH_DAY
From god@heaven.af.mil Sat Jan [ 3] 01:05:34 1996
Definition: prex.h:204
@ PREX_MBOX_FROM_LAX_MATCH_YEAR
From god@heaven.af.mil Sat Jan 10 01:05:34 [1996]
Definition: prex.h:213
@ PREX_MBOX_FROM_MATCH_DAY
From god@heaven.af.mil Sat Jan [ 3] 01:05:34 1996
Definition: prex.h:184
@ PREX_MBOX_FROM_MATCH_MONTH
From god@heaven.af.mil Sat [Jan] 3 01:05:34 1996
Definition: prex.h:183
@ PREX_MBOX_FROM_MATCH_YEAR
From god@heaven.af.mil Sat Jan 10 01:05:34 [1996]
Definition: prex.h:188
@ PREX_MBOX_FROM_MATCH_ENVSENDER
From [god@heaven.af.mil] Sat Jan 3 01:05:34 1996
Definition: prex.h:181
@ PREX_MBOX_FROM_MATCH_TIME
From god@heaven.af.mil Sat Jan 10 [01:05:34] 1996
Definition: prex.h:187
@ PREX_MBOX_FROM_LAX
[From god@heaven.af.mil Sat Jan 3 01:05:34 1996]
Definition: prex.h:41
@ PREX_MBOX_FROM
[From god@heaven.af.mil Sat Jan 3 01:05:34 1996]
Definition: prex.h:40
static size_t mutt_regmatch_len(const regmatch_t *match)
Return the length of a match.
Definition: regex3.h:77
static regoff_t mutt_regmatch_start(const regmatch_t *match)
Return the start of a match.
Definition: regex3.h:57