NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
email.c File Reference

Representation of an email. More...

#include "config.h"
#include <stdbool.h>
#include <string.h>
#include "mutt/lib.h"
#include "email.h"
#include "body.h"
#include "envelope.h"
#include "tags.h"
+ Include dependency graph for email.c:

Go to the source code of this file.

Functions

void nm_edata_free (void **ptr)
 Free data attached to an Email. More...
 
void email_free (struct Email **ptr)
 Free an Email. More...
 
struct Emailemail_new (void)
 Create a new Email. More...
 
bool email_cmp_strict (const struct Email *e1, const struct Email *e2)
 Strictly compare message emails. More...
 
size_t email_size (const struct Email *e)
 compute the size of an email More...
 
void emaillist_clear (struct EmailList *el)
 Drop a private list of Emails. More...
 
int emaillist_add_email (struct EmailList *el, struct Email *e)
 Add an Email to a list. More...
 
struct ListNodeheader_find (const struct ListHead *hdrlist, const char *header)
 Find a header, matching on its field, in a list of headers. More...
 
struct ListNodeheader_add (struct ListHead *hdrlist, const char *header)
 Add a header to a list. More...
 
struct ListNodeheader_update (struct ListNode *hdr, const char *header)
 Update an existing header. More...
 
struct ListNodeheader_set (struct ListHead *hdrlist, const char *header)
 Set a header value in a list. More...
 
void header_free (struct ListHead *hdrlist, struct ListNode *target)
 Free and remove a header from a header list. More...
 

Detailed Description

Representation of an email.

Authors
  • Michael R. Elkins

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file email.c.

Function Documentation

◆ nm_edata_free()

void nm_edata_free ( void **  ptr)

Free data attached to an Email.

Parameters
[out]ptrEmail data

Each email has an attached NmEmailData, which contains things like the tags (labels).

Definition at line 42 of file edata.c.

43 {
44  if (!ptr || !*ptr)
45  return;
46 
47  struct NmEmailData *edata = *ptr;
48 
49  mutt_debug(LL_DEBUG2, "nm: freeing email %p\n", (void *) edata);
50  FREE(&edata->folder);
51  FREE(&edata->oldpath);
52  FREE(&edata->virtual_id);
53 
54  FREE(ptr);
55 }
char * oldpath
Definition: edata.h:36
Log at debug level 2.
Definition: logging.h:41
char * virtual_id
Unique Notmuch Id.
Definition: edata.h:37
char * folder
Location of the Email.
Definition: edata.h:35
Notmuch-specific Email data -.
Definition: edata.h:33
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
void * edata
Driver-specific data.
Definition: email.h:111
#define FREE(x)
Definition: memory.h:40
+ Here is the caller graph for this function:

◆ email_free()

void email_free ( struct Email **  ptr)

Free an Email.

Parameters
[out]ptrEmail to free

Definition at line 44 of file email.c.

45 {
46  if (!ptr || !*ptr)
47  return;
48 
49  struct Email *e = *ptr;
50 
51  mutt_debug(LL_NOTIFY, "NT_EMAIL_DELETE: %p\n", e);
52  struct EventEmail ev_e = { 1, &e };
54 
55  if (e->edata_free && e->edata)
56  e->edata_free(&e->edata);
57 
58  mutt_env_free(&e->env);
59  mutt_body_free(&e->body);
60  FREE(&e->tree);
61  FREE(&e->path);
62 #ifdef MIXMASTER
63  mutt_list_free(&e->chain);
64 #endif
65 #ifdef USE_NOTMUCH
67 #endif
69  notify_free(&e->notify);
70 
71  FREE(ptr);
72 }
An Event that happened to an Email.
Definition: email.h:155
void mutt_env_free(struct Envelope **ptr)
Free an Envelope.
Definition: envelope.c:96
The envelope/body of an email.
Definition: email.h:37
struct Body * body
List of MIME parts.
Definition: email.h:91
Log of notifications.
Definition: logging.h:45
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition: notify.c:73
void driver_tags_free(struct TagList *list)
Free tags from a header.
Definition: tags.c:107
void mutt_list_free(struct ListHead *h)
Free a List AND its strings.
Definition: list.c:122
Email is about to be deleted.
Definition: email.h:147
struct Envelope * env
Envelope information.
Definition: email.h:90
struct TagList tags
For drivers that support server tagging.
Definition: email.h:109
struct ListHead chain
Mixmaster chain.
Definition: email.h:102
void(* edata_free)(void **ptr)
Free the private data attached to the Email.
Definition: email.h:121
void mutt_body_free(struct Body **ptr)
Free a Body.
Definition: body.c:57
char * tree
Character string to print thread tree.
Definition: email.h:94
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
struct Notify * notify
Notifications: NotifyEmail, EventEmail.
Definition: email.h:123
Email has changed, NotifyEmail, EventEmail.
Definition: notify_type.h:44
void * edata
Driver-specific data.
Definition: email.h:111
char * path
Path of Email (for local Mailboxes)
Definition: email.h:92
#define FREE(x)
Definition: memory.h:40
void nm_edata_free(void **ptr)
Free data attached to an Email.
Definition: edata.c:42
void * nm_edata
Notmuch private data.
Definition: email.h:106
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition: notify.c:171
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ email_new()

struct Email* email_new ( void  )

Create a new Email.

Return values
ptrNewly created Email

Definition at line 78 of file email.c.

79 {
80  static size_t sequence = 0;
81 
82  struct Email *e = mutt_mem_calloc(1, sizeof(struct Email));
83 #ifdef MIXMASTER
84  STAILQ_INIT(&e->chain);
85 #endif
86  STAILQ_INIT(&e->tags);
87  e->visible = true;
88  e->sequence = sequence++;
89  e->notify = notify_new();
90 
91  return e;
92 }
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
The envelope/body of an email.
Definition: email.h:37
#define STAILQ_INIT(head)
Definition: queue.h:372
struct TagList tags
For drivers that support server tagging.
Definition: email.h:109
struct Notify * notify_new(void)
Create a new notifications handler.
Definition: notify.c:60
bool visible
Is this message part of the view?
Definition: email.h:74
struct ListHead chain
Mixmaster chain.
Definition: email.h:102
size_t sequence
Sequence number assigned on creation.
Definition: email.h:99
struct Notify * notify
Notifications: NotifyEmail, EventEmail.
Definition: email.h:123
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ email_cmp_strict()

bool email_cmp_strict ( const struct Email e1,
const struct Email e2 
)

Strictly compare message emails.

Parameters
e1First Email
e2Second Email
Return values
trueEmails are strictly identical

Definition at line 100 of file email.c.

101 {
102  if (e1 && e2)
103  {
104  if ((e1->received != e2->received) || (e1->date_sent != e2->date_sent) ||
105  (e1->body->length != e2->body->length) || (e1->lines != e2->lines) ||
106  (e1->zhours != e2->zhours) || (e1->zminutes != e2->zminutes) ||
107  (e1->zoccident != e2->zoccident) || (e1->mime != e2->mime) ||
108  !mutt_env_cmp_strict(e1->env, e2->env) || !mutt_body_cmp_strict(e1->body, e2->body))
109  {
110  return false;
111  }
112  return true;
113  }
114  else
115  {
116  return (!e1 && !e2);
117  }
118 }
int lines
How many lines in the body of this message?
Definition: email.h:85
struct Body * body
List of MIME parts.
Definition: email.h:91
bool mutt_env_cmp_strict(const struct Envelope *e1, const struct Envelope *e2)
Strictly compare two Envelopes.
Definition: envelope.c:241
bool zoccident
True, if west of UTC, False if east.
Definition: email.h:65
bool mutt_body_cmp_strict(const struct Body *b1, const struct Body *b2)
Strictly compare two email Body&#39;s.
Definition: body.c:108
bool mime
Has a MIME-Version header?
Definition: email.h:42
struct Envelope * env
Envelope information.
Definition: email.h:90
time_t date_sent
Time when the message was sent (UTC)
Definition: email.h:82
LOFF_T length
length (in bytes) of attachment
Definition: body.h:45
unsigned int zhours
Hours away from UTC.
Definition: email.h:63
unsigned int zminutes
Minutes away from UTC.
Definition: email.h:64
time_t received
Time when the message was placed in the mailbox.
Definition: email.h:83
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ email_size()

size_t email_size ( const struct Email e)

compute the size of an email

Parameters
eEmail
Return values
numSize of the email, in bytes

Definition at line 125 of file email.c.

126 {
127  if (!e || !e->body)
128  return 0;
129  return e->body->length + e->body->offset - e->body->hdr_offset;
130 }
struct Body * body
List of MIME parts.
Definition: email.h:91
LOFF_T offset
offset where the actual data begins
Definition: body.h:44
LOFF_T length
length (in bytes) of attachment
Definition: body.h:45
long hdr_offset
Offset in stream where the headers begin.
Definition: body.h:42
+ Here is the caller graph for this function:

◆ emaillist_clear()

void emaillist_clear ( struct EmailList *  el)

Drop a private list of Emails.

Parameters
elEmailList to empty

The Emails are not freed.

Definition at line 138 of file email.c.

139 {
140  if (!el)
141  return;
142 
143  struct EmailNode *en = NULL, *tmp = NULL;
144  STAILQ_FOREACH_SAFE(en, el, entries, tmp)
145  {
146  STAILQ_REMOVE(el, en, EmailNode, entries);
147  FREE(&en);
148  }
149  STAILQ_INIT(el);
150 }
#define STAILQ_REMOVE(head, elm, type, field)
Definition: queue.h:402
#define STAILQ_INIT(head)
Definition: queue.h:372
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:362
#define FREE(x)
Definition: memory.h:40
List of Emails.
Definition: email.h:129
+ Here is the caller graph for this function:

◆ emaillist_add_email()

int emaillist_add_email ( struct EmailList *  el,
struct Email e 
)

Add an Email to a list.

Parameters
eEmail to add
elEmailList to add to
Return values
0Success
-1Error

Definition at line 159 of file email.c.

160 {
161  if (!el || !e)
162  return -1;
163 
164  struct EmailNode *en = mutt_mem_calloc(1, sizeof(*en));
165  en->email = e;
166  STAILQ_INSERT_TAIL(el, en, entries);
167 
168  return 0;
169 }
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:389
struct Email * email
Email in the list.
Definition: email.h:131
List of Emails.
Definition: email.h:129
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ header_find()

struct ListNode* header_find ( const struct ListHead *  hdrlist,
const char *  header 
)

Find a header, matching on its field, in a list of headers.

Parameters
hdrlistList of headers to search
headerThe header to search for
Return values
nodeThe node in the list matching the header
NULLNo matching header is found

The header should either of the form "X-Header:" or "X-Header: value"

Definition at line 180 of file email.c.

181 {
182  const char *key_end = strchr(header, ':');
183  if (!key_end)
184  return NULL;
185 
186  const int keylen = key_end - header + 1;
187 
188  struct ListNode *n = NULL;
189  STAILQ_FOREACH(n, hdrlist, entries)
190  {
191  if (mutt_istrn_equal(n->data, header, keylen))
192  return n;
193  }
194  return n;
195 }
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:621
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
char * data
String.
Definition: list.h:36
A List node for strings.
Definition: list.h:34
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ header_add()

struct ListNode* header_add ( struct ListHead *  hdrlist,
const char *  header 
)

Add a header to a list.

Parameters
hdrlistList of headers to search
headerString to set as the header
Return values
nodeThe created header

Definition at line 203 of file email.c.

204 {
205  struct ListNode *n = mutt_list_insert_tail(hdrlist, NULL);
206  n->data = mutt_str_dup(header);
207 
208  return n;
209 }
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:370
struct ListNode * mutt_list_insert_tail(struct ListHead *h, char *s)
Append a string to the end of a List.
Definition: list.c:64
char * data
String.
Definition: list.h:36
A List node for strings.
Definition: list.h:34
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ header_update()

struct ListNode* header_update ( struct ListNode hdr,
const char *  header 
)

Update an existing header.

Parameters
hdrThe header to update
headerString to update the header with
Return values
nodeThe updated header

Definition at line 217 of file email.c.

218 {
219  FREE(&hdr->data);
220  hdr->data = mutt_str_dup(header);
221 
222  return hdr;
223 }
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:370
char * data
String.
Definition: list.h:36
#define FREE(x)
Definition: memory.h:40
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ header_set()

struct ListNode* header_set ( struct ListHead *  hdrlist,
const char *  header 
)

Set a header value in a list.

Parameters
hdrlistList of headers to search
headerString to set the value of the header to
Return values
nodeThe updated or created header

If a header exists with the same field, update it, otherwise add a new header.

Definition at line 233 of file email.c.

234 {
235  struct ListNode *n = header_find(hdrlist, header);
236 
237  return n ? header_update(n, header) : header_add(hdrlist, header);
238 }
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:180
struct ListNode * header_add(struct ListHead *hdrlist, const char *header)
Add a header to a list.
Definition: email.c:203
struct ListNode * header_update(struct ListNode *hdr, const char *header)
Update an existing header.
Definition: email.c:217
A List node for strings.
Definition: list.h:34
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ header_free()

void header_free ( struct ListHead *  hdrlist,
struct ListNode target 
)

Free and remove a header from a header list.

Parameters
hdrlistList to free the header from
targetThe header to free

Definition at line 245 of file email.c.

246 {
247  STAILQ_REMOVE(hdrlist, target, ListNode, entries);
248  FREE(&target->data);
249  FREE(&target);
250 }
#define STAILQ_REMOVE(head, elm, type, field)
Definition: queue.h:402
char * data
String.
Definition: list.h:36
#define FREE(x)
Definition: memory.h:40
A List node for strings.
Definition: list.h:34
+ Here is the caller graph for this function: