NeoMutt  2024-10-02-58-g7720cc
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
email.c
Go to the documentation of this file.
1
31#include "config.h"
32#include <stdbool.h>
33#include <string.h>
34#include "mutt/lib.h"
35#include "email.h"
36#include "body.h"
37#include "envelope.h"
38#include "tags.h"
39
40void nm_edata_free(void **ptr);
41
46void email_free(struct Email **ptr)
47{
48 if (!ptr || !*ptr)
49 return;
50
51 struct Email *e = *ptr;
52
53 mutt_debug(LL_NOTIFY, "NT_EMAIL_DELETE: %p\n", (void *) e);
54 struct EventEmail ev_e = { 1, &e };
56
57 if (e->edata_free && e->edata)
58 e->edata_free(&e->edata);
59
60 mutt_env_free(&e->env);
62 FREE(&e->tree);
63 FREE(&e->path);
64#ifdef USE_NOTMUCH
66#endif
69
70 FREE(ptr);
71}
72
77struct Email *email_new(void)
78{
79 static size_t sequence = 0;
80
81 struct Email *e = mutt_mem_calloc(1, sizeof(struct Email));
82 STAILQ_INIT(&e->tags);
83 e->visible = true;
84 e->sequence = sequence++;
85 e->notify = notify_new();
86
87 return e;
88}
89
96bool email_cmp_strict(const struct Email *e1, const struct Email *e2)
97{
98 if (e1 && e2)
99 {
100 if ((e1->received != e2->received) || (e1->date_sent != e2->date_sent) ||
101 (e1->body->length != e2->body->length) || (e1->lines != e2->lines) ||
102 (e1->zhours != e2->zhours) || (e1->zminutes != e2->zminutes) ||
103 (e1->zoccident != e2->zoccident) || (e1->mime != e2->mime) ||
104 !mutt_env_cmp_strict(e1->env, e2->env) || !mutt_body_cmp_strict(e1->body, e2->body))
105 {
106 return false;
107 }
108 return true;
109 }
110 else
111 {
112 return (!e1 && !e2);
113 }
114}
115
121size_t email_size(const struct Email *e)
122{
123 if (!e || !e->body)
124 return 0;
125 return e->body->length + e->body->offset - e->body->hdr_offset;
126}
127
137struct ListNode *header_find(const struct ListHead *hdrlist, const char *header)
138{
139 const char *key_end = strchr(header, ':');
140 if (!key_end)
141 return NULL;
142
143 const int keylen = key_end - header + 1;
144
145 struct ListNode *n = NULL;
146 STAILQ_FOREACH(n, hdrlist, entries)
147 {
148 if (mutt_istrn_equal(n->data, header, keylen))
149 return n;
150 }
151 return n;
152}
153
160struct ListNode *header_add(struct ListHead *hdrlist, const char *header)
161{
162 struct ListNode *n = mutt_list_insert_tail(hdrlist, NULL);
163 n->data = mutt_str_dup(header);
164
165 return n;
166}
167
174struct ListNode *header_update(struct ListNode *hdr, const char *header)
175{
176 FREE(&hdr->data);
177 hdr->data = mutt_str_dup(header);
178
179 return hdr;
180}
181
190struct ListNode *header_set(struct ListHead *hdrlist, const char *header)
191{
192 struct ListNode *n = header_find(hdrlist, header);
193
194 return n ? header_update(n, header) : header_add(hdrlist, header);
195}
196
202void header_free(struct ListHead *hdrlist, struct ListNode *target)
203{
204 STAILQ_REMOVE(hdrlist, target, ListNode, entries);
205 FREE(&target->data);
206 FREE(&target);
207}
void mutt_body_free(struct Body **ptr)
Free a Body.
Definition: body.c:58
bool mutt_body_cmp_strict(const struct Body *b1, const struct Body *b2)
Strictly compare two email Body's.
Definition: body.c:110
void nm_edata_free(void **ptr)
Free data attached to an Email.
Definition: edata.c:42
bool email_cmp_strict(const struct Email *e1, const struct Email *e2)
Strictly compare message emails.
Definition: email.c:96
void header_free(struct ListHead *hdrlist, struct ListNode *target)
Free and remove a header from a header list.
Definition: email.c:202
size_t email_size(const struct Email *e)
Compute the size of an email.
Definition: email.c:121
struct Email * email_new(void)
Create a new Email.
Definition: email.c:77
struct ListNode * header_set(struct ListHead *hdrlist, const char *header)
Set a header value in a list.
Definition: email.c:190
struct ListNode * header_add(struct ListHead *hdrlist, const char *header)
Add a header to a list.
Definition: email.c:160
struct ListNode * header_update(struct ListNode *hdr, const char *header)
Update an existing header.
Definition: email.c:174
void email_free(struct Email **ptr)
Free an Email.
Definition: email.c:46
struct ListNode * header_find(const struct ListHead *hdrlist, const char *header)
Find a header, matching on its field, in a list of headers.
Definition: email.c:137
Representation of an email.
@ NT_EMAIL_DELETE
Email is about to be deleted.
Definition: email.h:184
void mutt_env_free(struct Envelope **ptr)
Free an Envelope.
Definition: envelope.c:126
bool mutt_env_cmp_strict(const struct Envelope *e1, const struct Envelope *e2)
Strictly compare two Envelopes.
Definition: envelope.c:285
Representation of an email header (envelope)
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
struct ListNode * mutt_list_insert_tail(struct ListHead *h, char *s)
Append a string to the end of a List.
Definition: list.c:65
@ LL_NOTIFY
Log of notifications.
Definition: logging2.h:48
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:51
#define FREE(x)
Definition: memory.h:45
Convenience wrapper for the library headers.
struct Notify * notify_new(void)
Create a new notifications handler.
Definition: notify.c:62
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition: notify.c:173
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition: notify.c:75
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
bool mutt_istrn_equal(const char *a, const char *b, size_t num)
Check for equality of two strings ignoring case (to a maximum), safely.
Definition: string.c:453
@ NT_EMAIL
Email has changed, NotifyEmail, EventEmail.
Definition: notify_type.h:44
#define STAILQ_REMOVE(head, elm, type, field)
Definition: queue.h:402
#define STAILQ_INIT(head)
Definition: queue.h:372
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
Convenience wrapper for the send headers.
LOFF_T offset
offset where the actual data begins
Definition: body.h:52
LOFF_T length
length (in bytes) of attachment
Definition: body.h:53
long hdr_offset
Offset in stream where the headers begin.
Definition: body.h:81
The envelope/body of an email.
Definition: email.h:39
unsigned int zminutes
Minutes away from UTC.
Definition: email.h:57
bool visible
Is this message part of the view?
Definition: email.h:121
struct Envelope * env
Envelope information.
Definition: email.h:68
bool mime
Has a MIME-Version header?
Definition: email.h:48
void * edata
Driver-specific data.
Definition: email.h:74
int lines
How many lines in the body of this message?
Definition: email.h:62
struct Body * body
List of MIME parts.
Definition: email.h:69
void * nm_edata
Notmuch private data.
Definition: email.h:93
char * tree
Character string to print thread tree.
Definition: email.h:125
void(* edata_free)(void **ptr)
Definition: email.h:90
bool zoccident
True, if west of UTC, False if east.
Definition: email.h:58
unsigned int zhours
Hours away from UTC.
Definition: email.h:56
time_t date_sent
Time when the message was sent (UTC)
Definition: email.h:60
struct TagList tags
For drivers that support server tagging.
Definition: email.h:72
struct Notify * notify
Notifications: NotifyEmail, EventEmail.
Definition: email.h:73
char * path
Path of Email (for local Mailboxes)
Definition: email.h:70
size_t sequence
Sequence number assigned on creation.
Definition: email.h:67
time_t received
Time when the message was placed in the mailbox.
Definition: email.h:61
An Event that happened to an Email.
Definition: email.h:196
A List node for strings.
Definition: list.h:37
char * data
String.
Definition: list.h:38
void driver_tags_free(struct TagList *tl)
Free tags from a header.
Definition: tags.c:131
Driver based email tags.