summaryrefslogtreecommitdiffstats
path: root/src/plugins/push-notification/push-notification-event-message-common.c
blob: 7965039905f3eb69d55d893887d9b7d5297aea14 (plain)
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
/* Copyright (c) 2015-2019 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "str.h"
#include "message-address.h"
#include "message-header-decode.h"
#include "mail-storage.h"
#include "push-notification-event-message-common.h"

static void
decode_address_header(pool_t pool, const char *hdr,
		      const char **address_r, const char **name_r)
{
	struct message_address *addr;
	const char *display_name;

	if (hdr == NULL)
		return;

	addr = message_address_parse(pool_datastack_create(),
		(const unsigned char *)hdr, strlen(hdr), 1, 0);
	if (addr == NULL)
		return;

	display_name = addr->name;
	if (addr->domain == NULL) {
		/* group */
		display_name = addr->mailbox;
	} else if (addr->domain[0] != '\0')
		*address_r = p_strdup_printf(pool, "%s@%s", addr->mailbox,
					     addr->domain);
	else if (addr->mailbox != NULL && addr->mailbox[0] != '\0')
		*address_r = p_strdup(pool, addr->mailbox);

	if (display_name != NULL && display_name[0] != '\0') {
		string_t *name_utf8 = t_str_new(128);

		message_header_decode_utf8((const unsigned char *)display_name,
					   strlen(display_name), name_utf8, NULL);
		*name_r = p_strdup(pool, str_c(name_utf8));
	}
}

void push_notification_message_fill(
	struct mail *mail, pool_t pool,
	enum push_notification_event_message_flags event_flags,
	const char **from, const char **to, const char **subject, time_t *date,
	int *date_tz, const char **message_id, enum mail_flags *flags,
	bool *flags_set, const char *const **keywords, const char **snippet,
	struct push_notification_message_ext *ext)
{
	const char *value;
	time_t tmp_date;
	int tmp_tz;

	if ((*from == NULL) &&
	    (event_flags & PUSH_NOTIFICATION_MESSAGE_HDR_FROM) != 0 &&
	    (mail_get_first_header(mail, "From", &value) >= 0)) {
		*from = p_strdup(pool, value);
		decode_address_header(pool, value, &ext->from_address,
				      &ext->from_display_name_utf8);
	}

	if ((*to == NULL) &&
	    (event_flags & PUSH_NOTIFICATION_MESSAGE_HDR_TO) != 0 &&
	    (mail_get_first_header(mail, "To", &value) >= 0)) {
		*to = p_strdup(pool, value);
		decode_address_header(pool, value, &ext->to_address,
				      &ext->to_display_name_utf8);
	}

	if ((*subject == NULL) &&
	    (event_flags & PUSH_NOTIFICATION_MESSAGE_HDR_SUBJECT) != 0 &&
	    (mail_get_first_header(mail, "Subject", &value) >= 0)) {
		string_t *subject_utf8 = t_str_new(128);

		*subject = p_strdup(pool, value);
		if (value != NULL) {
			message_header_decode_utf8(
				(const unsigned char *)value,
				strlen(value), subject_utf8, NULL);
			ext->subject_utf8 = p_strdup(pool, str_c(subject_utf8));
		}
	}

	if ((*date == -1) &&
	    (event_flags & PUSH_NOTIFICATION_MESSAGE_HDR_DATE) != 0 &&
	    (mail_get_date(mail, &tmp_date, &tmp_tz) >= 0)) {
		*date = tmp_date;
		*date_tz = tmp_tz;
	}

	if ((*message_id == NULL) &&
	    (event_flags & PUSH_NOTIFICATION_MESSAGE_HDR_MESSAGE_ID) != 0 &&
	    (mail_get_first_header(mail, "Message-ID", &value) >= 0)) {
		*message_id = p_strdup(pool, value);
	}

	if (!*flags_set &&
	    (event_flags & PUSH_NOTIFICATION_MESSAGE_FLAGS) != 0) {
		*flags = mail_get_flags(mail);
		*flags_set = TRUE;
	}

	if ((*keywords == NULL) &&
	    (event_flags & PUSH_NOTIFICATION_MESSAGE_KEYWORDS) != 0) {
		*keywords = p_strarray_dup(pool, mail_get_keywords(mail));
	}

	if ((*snippet == NULL) &&
	    (event_flags & PUSH_NOTIFICATION_MESSAGE_BODY_SNIPPET) != 0 &&
	    (mail_get_special(mail, MAIL_FETCH_BODY_SNIPPET, &value) >= 0)) {
		/* [0] contains the snippet algorithm, skip over it */
		i_assert(value[0] != '\0');
		*snippet = p_strdup(pool, value + 1);
	}
}