summaryrefslogtreecommitdiff
path: root/modules/presence/publisher.c
blob: f443852fd5b1c6402306f0ac01d73d151c013a5e (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/**
 * @file publisher.c Presence Publisher (RFC 3903)
 *
 * Copyright (C) 2010 Creytiv.com
 * Copyright (C) 2014 Juha Heinanen
 */

#include <string.h>
#include <re.h>
#include <baresip.h>
#include "presence.h"


struct publisher {
	struct le le;
	struct tmr tmr;
	unsigned failc;
	char *etag;
	unsigned int expires;
	unsigned int refresh;
	struct ua *ua;
};

static struct list publ = LIST_INIT;

static void tmr_handler(void *arg);
static int publish(struct publisher *pub);


static void response_handler(int err, const struct sip_msg *msg, void *arg)
{
	struct publisher *pub = arg;
	const struct sip_hdr *etag_hdr;

	if (err)
		return;

	if (msg->scode < 200) {
		return;
	}

	if (msg->scode < 300) {

		if (pub->expires == 0)
			return;

		etag_hdr = sip_msg_xhdr(msg, "SIP-ETag");
		if (etag_hdr) {
			mem_deref(pub->etag);
			pl_strdup(&(pub->etag), &(etag_hdr->val));
			pub->refresh = 1;
			tmr_start(&pub->tmr, pub->expires * 900,
				  tmr_handler, pub);
		}
		else {
			warning("%s: publisher got 200 OK without etag\n",
				ua_aor(pub->ua));
		}
	}
	else if (msg->scode == 412) {

		mem_deref(pub->etag);
		pub->etag = NULL;
		pub->refresh = 0;
		publish(pub);

	}
	else {
		warning("%s: publisher got error response %u %r\n",
			ua_aor(pub->ua), msg->scode, &msg->reason);
	}

	return;
}


/* move this to presence.c */
static const char *presence_status_str(enum presence_status st)
{
	switch (st) {

	case PRESENCE_OPEN:   return "open";
	case PRESENCE_CLOSED: return "closed";
	case PRESENCE_UNKNOWN: return "unknown";
	default: return "?";
	}
}


static int publish(struct publisher *pub)
{
	int err;
	const char *aor = ua_aor(pub->ua);
	struct mbuf *mb;

	mb = mbuf_alloc(1024);
	if (!mb)
		return ENOMEM;

	if (pub->expires && !pub->refresh)
		err = mbuf_printf(mb,
	"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n"
	"<presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\r\n"
	"    xmlns:dm=\"urn:ietf:params:xml:ns:pidf:data-model\"\r\n"
	"    xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\"\r\n"
	"    entity=\"%s\">\r\n"
	"  <dm:person id=\"p4159\"><rpid:activities/></dm:person>\r\n"
	"  <tuple id=\"t4109\">\r\n"
	"    <status>\r\n"
	"      <basic>%s</basic>\r\n"
	"    </status>\r\n"
	"    <contact>%s</contact>\r\n"
	"  </tuple>\r\n"
	"</presence>\r\n"
		  ,aor,
		  presence_status_str(ua_presence_status(pub->ua)), aor);
	else
		err = mbuf_printf(mb, "");
	if (err)
		goto out;

	mb->pos = 0;

	/* XXX: can be simplified with 1 function call, by adding a
	   print-handler that prints "SIP-If-Match: ETAG" */
	if (pub->etag)
		err = sip_req_send(pub->ua, "PUBLISH", aor,
				   pub->expires ? response_handler : NULL,
				   pub,
			   "%s"
			   "Event: presence\r\n"
			   "Expires: %u\r\n"
			   "SIP-If-Match: %s\r\n"
			   "Content-Length: %zu\r\n"
			   "\r\n"
			   "%b",
			   pub->expires
				   ? "Content-Type: application/pidf+xml\r\n"
				   : "",

			   pub->expires,
			   pub->etag,
			   mbuf_get_left(mb),
			   mbuf_buf(mb),
			   mbuf_get_left(mb));
	else
		err = sip_req_send(pub->ua, "PUBLISH", aor,
				   pub->expires ? response_handler : NULL,
				   pub,
			   "%s"
			   "Event: presence\r\n"
			   "Expires: %u\r\n"
			   "Content-Length: %zu\r\n"
			   "\r\n"
			   "%b",
			   pub->expires
				   ? "Content-Type: application/pidf+xml\r\n"
				   : "",
			   pub->expires,
			   mbuf_get_left(mb),
			   mbuf_buf(mb),
			   mbuf_get_left(mb));
	if (err) {
		warning("publisher: send PUBLISH: (%m)\n", err);
	}

out:
	mem_deref(mb);

	return err;
}


/* move to presence.c */
static uint32_t wait_fail(unsigned failc)
{
	switch (failc) {

	case 1:  return 30;
	case 2:  return 300;
	case 3:  return 3600;
	default: return 86400;
	}
}


static void tmr_handler(void *arg)
{
	struct publisher *pub = arg;

	if (publish(pub))
		tmr_start(&pub->tmr, wait_fail(++pub->failc) * 1000,
			  tmr_handler, pub);
	else
		pub->failc = 0;
}


static void destructor(void *arg)
{
	struct publisher *pub = arg;

	list_unlink(&pub->le);
	tmr_cancel(&pub->tmr);
	mem_deref(pub->ua);
	mem_deref(pub->etag);
}


void publisher_update_status(struct ua *ua)
{
	struct le *le;

	for (le = publ.head; le; le = le->next) {

		struct publisher *pub = le->data;

		if (pub->ua == ua) {
			pub->refresh = 0;
			publish(pub);
		}
	}
}


static int publisher_alloc(struct ua *ua)
{
	struct publisher *pub;

	pub = mem_zalloc(sizeof(*pub), destructor);
	if (!pub)
		return ENOMEM;

	pub->ua = mem_ref(ua);
	pub->expires = account_pubint(ua_account(ua));

	tmr_init(&pub->tmr);
	tmr_start(&pub->tmr, 10, tmr_handler, pub);

	list_append(&publ, &pub->le, pub);

	return 0;
}


static void pub_ua_event_handler(struct ua *ua,
				 enum ua_event ev,
				 struct call *call,
				 const char *prm,
				 void *arg )
{
	(void)call;
	(void)prm;
	(void)arg;

	if (account_pubint(ua_account(ua)) == 0)
		return;

	if (ev == UA_EVENT_REGISTER_OK) {
		if (ua_presence_status(ua) == PRESENCE_UNKNOWN) {
			ua_presence_status_set(ua, PRESENCE_OPEN);
			publisher_update_status(ua);
		}
	}
}


int publisher_init(void)
{
	struct le *le;
	int err = 0;

	uag_event_register(pub_ua_event_handler, NULL);

	for (le = list_head(uag_list()); le; le = le->next) {

		struct ua *ua = le->data;
		struct account *acc = ua_account(ua);

		if (account_pubint(acc) == 0)
			continue;

		err |= publisher_alloc(ua);
	}

	if (err)
		return err;

	return 0;
}


void publisher_close(void)
{
	struct le *le;

	uag_event_unregister(pub_ua_event_handler);

	for (le = list_head(&publ); le; le = le->next) {

		struct publisher *pub = le->data;

		ua_presence_status_set(pub->ua, PRESENCE_CLOSED);
		pub->expires = 0;
		publish(pub);
	}

	list_flush(&publ);
}