imdn.cpp 8.03 KB
Newer Older
1 2
/*
 * imdn.cpp
3
 * Copyright (C) 2010-2018 Belledonne Communications SARL
4
 *
Ghislain MARY's avatar
Ghislain MARY committed
5 6 7 8
 * 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.
9 10 11 12 13 14 15
 *
 * 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
Ghislain MARY's avatar
Ghislain MARY committed
16 17
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 19
 */

20
#include "chat/chat-message/chat-message-p.h"
21
#include "chat/chat-room/chat-room.h"
22 23 24 25
#include "core/core.h"
#include "logger/logger.h"

#include "imdn.h"
26

Ronan's avatar
Ronan committed
27
// =============================================================================
28 29 30 31 32 33 34

using namespace std;

LINPHONE_BEGIN_NAMESPACE

const string Imdn::imdnPrefix = "/imdn:imdn";

35
string Imdn::createXml (const string &id, time_t time, Imdn::Type imdnType, LinphoneReason reason) {
36 37 38 39 40 41 42
	xmlBufferPtr buf;
	xmlTextWriterPtr writer;
	int err;
	string content;
	char *datetime = nullptr;

	// Check that the chat message has a message id.
43 44
	if (id.empty())
		return content;
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

	buf = xmlBufferCreate();
	if (buf == nullptr) {
		lError() << "Error creating the XML buffer";
		return content;
	}
	writer = xmlNewTextWriterMemory(buf, 0);
	if (writer == nullptr) {
		lError() << "Error creating the XML writer";
		return content;
	}

	datetime = linphone_timestamp_to_rfc3339_string(time);
	err = xmlTextWriterStartDocument(writer, "1.0", "UTF-8", nullptr);
	if (err >= 0) {
		err = xmlTextWriterStartElementNS(writer, nullptr, (const xmlChar *)"imdn",
				(const xmlChar *)"urn:ietf:params:xml:ns:imdn");
	}
	if ((err >= 0) && (reason != LinphoneReasonNone)) {
		err = xmlTextWriterWriteAttributeNS(writer, (const xmlChar *)"xmlns", (const xmlChar *)"linphoneimdn", nullptr, (const xmlChar *)"http://www.linphone.org/xsds/imdn.xsd");
	}
	if (err >= 0) {
		err = xmlTextWriterWriteElement(writer, (const xmlChar *)"message-id", (const xmlChar *)id.c_str());
	}
	if (err >= 0) {
		err = xmlTextWriterWriteElement(writer, (const xmlChar *)"datetime", (const xmlChar *)datetime);
	}
	if (err >= 0) {
		if (imdnType == Imdn::Type::Delivery) {
			err = xmlTextWriterStartElement(writer, (const xmlChar *)"delivery-notification");
		} else {
			err = xmlTextWriterStartElement(writer, (const xmlChar *)"display-notification");
		}
	}
	if (err >= 0) {
		err = xmlTextWriterStartElement(writer, (const xmlChar *)"status");
	}
	if (err >= 0) {
		if (reason == LinphoneReasonNone) {
			if (imdnType == Imdn::Type::Delivery) {
				err = xmlTextWriterStartElement(writer, (const xmlChar *)"delivered");
			} else {
				err = xmlTextWriterStartElement(writer, (const xmlChar *)"displayed");
			}
		} else {
			err = xmlTextWriterStartElement(writer, (const xmlChar *)"error");
		}
	}
	if (err >= 0) {
		// Close the "delivered", "displayed" or "error" element.
		err = xmlTextWriterEndElement(writer);
	}
	if ((err >= 0) && (reason != LinphoneReasonNone)) {
		err = xmlTextWriterStartElementNS(writer, (const xmlChar *)"linphoneimdn", (const xmlChar *)"reason", nullptr);
		if (err >= 0) {
			char codestr[16];
			snprintf(codestr, 16, "%d", linphone_reason_to_error_code(reason));
			err = xmlTextWriterWriteAttribute(writer, (const xmlChar *)"code", (const xmlChar *)codestr);
		}
		if (err >= 0) {
			err = xmlTextWriterWriteString(writer, (const xmlChar *)linphone_reason_to_string(reason));
		}
		if (err >= 0) {
			err = xmlTextWriterEndElement(writer);
		}
	}
	if (err >= 0) {
		// Close the "status" element.
		err = xmlTextWriterEndElement(writer);
	}
	if (err >= 0) {
		// Close the "delivery-notification" or "display-notification" element.
		err = xmlTextWriterEndElement(writer);
	}
	if (err >= 0) {
		// Close the "imdn" element.
		err = xmlTextWriterEndElement(writer);
	}
	if (err >= 0) {
		err = xmlTextWriterEndDocument(writer);
	}
	if (err > 0) {
		// xmlTextWriterEndDocument returns the size of the content.
		content = string((char *)buf->content);
	}
	xmlFreeTextWriter(writer);
	xmlBufferFree(buf);
	ms_free(datetime);
	return content;
}

136
void Imdn::parse (const shared_ptr<ChatMessage> &chatMessage) {
137 138
	xmlparsing_context_t *xmlCtx = linphone_xmlparsing_context_new();
	xmlSetGenericErrorFunc(xmlCtx, linphone_xmlparsing_genericxml_error);
139
	xmlCtx->doc = xmlReadDoc((const unsigned char *)chatMessage->getPrivate()->getText().c_str(), 0, nullptr, 0);
140
	if (xmlCtx->doc)
141
		parse(chatMessage, xmlCtx);
142 143 144 145 146
	else
		lWarning() << "Wrongly formatted IMDN XML: " << xmlCtx->errorBuffer;
	linphone_xmlparsing_context_destroy(xmlCtx);
}

147
void Imdn::parse (const shared_ptr<ChatMessage> &imdnMessage, xmlparsing_context_t *xmlCtx) {
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
	char xpathStr[MAX_XPATH_LENGTH];
	char *messageIdStr = nullptr;
	char *datetimeStr = nullptr;
	if (linphone_create_xml_xpath_context(xmlCtx) < 0)
		return;

	xmlXPathRegisterNs(xmlCtx->xpath_ctx, (const xmlChar *)"imdn", (const xmlChar *)"urn:ietf:params:xml:ns:imdn");
	xmlXPathObjectPtr imdnObject = linphone_get_xml_xpath_object_for_node_list(xmlCtx, imdnPrefix.c_str());
	if (imdnObject) {
		if (imdnObject->nodesetval && (imdnObject->nodesetval->nodeNr >= 1)) {
			snprintf(xpathStr, sizeof(xpathStr), "%s[1]/imdn:message-id", imdnPrefix.c_str());
			messageIdStr = linphone_get_xml_text_content(xmlCtx, xpathStr);
			snprintf(xpathStr, sizeof(xpathStr), "%s[1]/imdn:datetime", imdnPrefix.c_str());
			datetimeStr = linphone_get_xml_text_content(xmlCtx, xpathStr);
		}
		xmlXPathFreeObject(imdnObject);
	}

	if (messageIdStr && datetimeStr) {
167 168 169
		shared_ptr<AbstractChatRoom> cr = imdnMessage->getChatRoom();
		shared_ptr<ChatMessage> cm = cr->findChatMessage(messageIdStr, ChatMessage::Direction::Outgoing);
		const IdentityAddress &participantAddress = imdnMessage->getFromAddress().getAddressWithoutGruu();
170 171 172
		if (!cm) {
			lWarning() << "Received IMDN for unknown message " << messageIdStr;
		} else {
173
			time_t imdnTime = imdnMessage->getTime();
174
			LinphoneImNotifPolicy *policy = linphone_core_get_im_notif_policy(cr->getCore()->getCCore());
175 176 177 178 179 180 181 182 183
			snprintf(xpathStr, sizeof(xpathStr), "%s[1]/imdn:delivery-notification/imdn:status", imdnPrefix.c_str());
			xmlXPathObjectPtr deliveryStatusObject = linphone_get_xml_xpath_object_for_node_list(xmlCtx, xpathStr);
			snprintf(xpathStr, sizeof(xpathStr), "%s[1]/imdn:display-notification/imdn:status", imdnPrefix.c_str());
			xmlXPathObjectPtr displayStatusObject = linphone_get_xml_xpath_object_for_node_list(xmlCtx, xpathStr);
			if (deliveryStatusObject && linphone_im_notif_policy_get_recv_imdn_delivered(policy)) {
				if (deliveryStatusObject->nodesetval && (deliveryStatusObject->nodesetval->nodeNr >= 1)) {
					xmlNodePtr node = deliveryStatusObject->nodesetval->nodeTab[0];
					if (node->children && node->children->name) {
						if (strcmp((const char *)node->children->name, "delivered") == 0) {
184
							cm->getPrivate()->setParticipantState(participantAddress, ChatMessage::State::DeliveredToUser, imdnTime);
185
						} else if (strcmp((const char *)node->children->name, "error") == 0) {
186
							cm->getPrivate()->setParticipantState(participantAddress, ChatMessage::State::NotDelivered, imdnTime);
187 188 189 190 191 192 193 194 195 196
						}
					}
				}
				xmlXPathFreeObject(deliveryStatusObject);
			}
			if (displayStatusObject && linphone_im_notif_policy_get_recv_imdn_displayed(policy)) {
				if (displayStatusObject->nodesetval && (displayStatusObject->nodesetval->nodeNr >= 1)) {
					xmlNodePtr node = displayStatusObject->nodesetval->nodeTab[0];
					if (node->children && node->children->name) {
						if (strcmp((const char *)node->children->name, "displayed") == 0) {
197
							cm->getPrivate()->setParticipantState(participantAddress, ChatMessage::State::Displayed, imdnTime);
198 199 200 201 202 203 204 205 206 207 208 209 210 211
						}
					}
				}
				xmlXPathFreeObject(displayStatusObject);
			}
		}
	}
	if (messageIdStr)
		linphone_free_xml_text_content(messageIdStr);
	if (datetimeStr)
		linphone_free_xml_text_content(datetimeStr);
}

LINPHONE_END_NAMESPACE