summaryrefslogtreecommitdiffstats
path: root/src/mwi/mwi_subscription.cpp
blob: d25cea3fbf3c269d167797bb851353a7ff52084b (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
/*
    Copyright (C) 2005-2009  Michel de Boer <michel@twinklephone.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    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 <https://www.gnu.org/licenses/>.
*/

#include "mwi_subscription.h"

#include <cassert>

#include "userintf.h"
#include "audits/memman.h"
#include "parser/hdr_event.h"

t_request *t_mwi_subscription::create_subscribe(unsigned long expires) const {
	t_request *r = t_subscription::create_subscribe(expires);
	SET_MWI_HDR_ACCEPT(r->hdr_accept);
	
	return r;
}

t_mwi_subscription::t_mwi_subscription(t_mwi_dialog *_dialog, t_mwi *_mwi) :
	t_subscription(_dialog, SR_SUBSCRIBER, SIP_EVENT_MSG_SUMMARY),
	mwi(_mwi)
{}

bool t_mwi_subscription::recv_notify(t_request *r, t_tuid tuid, t_tid tid) {
	if (t_subscription::recv_notify(r, tuid, tid)) return true;
	
	bool unsupported_body = false;
	
	// NOTE: if the subscription is still pending (RFC 3265 3.2.4), then the
	//       information in the body has no meaning.
	if (r->body && r->body->get_type() == BODY_SIMPLE_MSG_SUM &&
	    !is_pending()) 
	{
		t_simple_msg_sum_body *body = dynamic_cast<t_simple_msg_sum_body *>(r->body);
		assert(body);
		mwi->set_msg_waiting(body->get_msg_waiting());
		
		t_msg_summary summary;
		if (body->get_msg_summary(MSG_CONTEXT_VOICE, summary)) {
			mwi->set_voice_msg_summary(summary);
		} else {
			mwi->clear_voice_msg_summary();
		}
		
		mwi->set_status(t_mwi::MWI_KNOWN);
	}
	
	// Verify if there is an usupported body.
	if (r->body && r->body->get_type() != BODY_SIMPLE_MSG_SUM) {
		unsupported_body = true;
	}
	
	if (state == SS_TERMINATED && !may_resubscribe) {
		// The MWI server ended the subscription and indicated
		// that resubscription is not possible. So no MWI status
		// can be retrieved anymore. This should not happen, so
		// present it as a failure to the user.
		mwi->set_status(t_mwi::MWI_FAILED);
		ui->cb_mwi_terminated(user_config, get_reason_termination());
	}
	
	t_response *resp;
	if (unsupported_body) {
		resp = r->create_response(R_415_UNSUPPORTED_MEDIA_TYPE);
		SET_MWI_HDR_ACCEPT(r->hdr_accept);
	} else {
		resp = r->create_response(R_200_OK);
	}
	send_response(user_config, resp, 0, tid);
	MEMMAN_DELETE(resp);
	delete resp;
	
	ui->cb_update_mwi();
	return true;
}

bool t_mwi_subscription::recv_subscribe_response(t_response *r, t_tuid tuid, t_tid tid) {
	// Parent handles the SUBSCRIBE response
	(void)t_subscription::recv_subscribe_response(r, tuid, tid);
	
	// If the subscription is terminated after the SUBSCRIBE response, it means
	// that subscription failed.
	if (state == SS_TERMINATED) {
		ui->cb_mwi_subscribe_failed(user_config, r, mwi->get_status() != t_mwi::MWI_FAILED);
		mwi->set_status(t_mwi::MWI_FAILED);
	}
	
	ui->cb_update_mwi();
	return true;
}