chat.c 26.5 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/***************************************************************************
 *            chat.c
 *
 *  Sun Jun  5 19:34:18 2005
 *  Copyright  2005  Simon Morlat
 *  Email simon dot morlat at linphone dot org
 ****************************************************************************/

/*
 *  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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
24

25 26 27 28
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlwriter.h>

29 30
#include <linphone/utils/utils.h>

31
#include "linphone/core.h"
32
#include "private.h"
33
#include "linphone/lpconfig.h"
34
#include "belle-sip/belle-sip.h"
johan's avatar
johan committed
35
#include "ortp/b64.h"
36
#include "linphone/wrapper_utils.h"
Simon Morlat's avatar
Simon Morlat committed
37

38
#include "c-wrapper/c-tools.h"
39
#include "chat/basic-chat-room.h"
40
#include "chat/client-group-chat-room.h"
Ronan's avatar
Ronan committed
41
#include "chat/real-time-text-chat-room.h"
42
#include "chat/real-time-text-chat-room-p.h"
Ronan's avatar
Ronan committed
43
#include "content/content-type.h"
44 45


46
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphoneChatMessage);
47

48
void linphone_chat_message_set_state(LinphoneChatMessage *msg, LinphoneChatMessageState state) {
49 50
	/* do not invoke callbacks on orphan messages */
	if (state != msg->state && msg->chat_room != NULL) {
51 52 53
		if (((msg->state == LinphoneChatMessageStateDisplayed) || (msg->state == LinphoneChatMessageStateDeliveredToUser))
			&& ((state == LinphoneChatMessageStateDeliveredToUser) || (state == LinphoneChatMessageStateDelivered) || (state == LinphoneChatMessageStateNotDelivered))) {
			/* If the message has been displayed or delivered to user we must not go back to the delivered or not delivered state. */
Ghislain MARY's avatar
Ghislain MARY committed
54 55
			return;
		}
56 57 58 59 60 61 62 63
		ms_message("Chat message %p: moving from state %s to %s", msg, linphone_chat_message_state_to_string(msg->state),
				   linphone_chat_message_state_to_string(state));
		msg->state = state;
		if (msg->message_state_changed_cb) {
			msg->message_state_changed_cb(msg, msg->state, msg->message_state_changed_user_data);
		}
		if (linphone_chat_message_cbs_get_msg_state_changed(msg->callbacks)) {
			linphone_chat_message_cbs_get_msg_state_changed(msg->callbacks)(msg, msg->state);
64 65 66 67
		}
	}
}

68 69 70 71 72
BELLE_SIP_INSTANCIATE_VPTR(LinphoneChatMessage, belle_sip_object_t,
						   (belle_sip_object_destroy_t)_linphone_chat_message_destroy,
						   NULL, // clone
						   NULL, // marshal
						   FALSE);
73

74 75
void linphone_core_disable_chat(LinphoneCore *lc, LinphoneReason deny_reason) {
	lc->chat_deny_code = deny_reason;
Simon Morlat's avatar
Simon Morlat committed
76 77
}

78 79
void linphone_core_enable_chat(LinphoneCore *lc) {
	lc->chat_deny_code = LinphoneReasonNone;
Simon Morlat's avatar
Simon Morlat committed
80 81
}

82 83
bool_t linphone_core_chat_enabled(const LinphoneCore *lc) {
	return lc->chat_deny_code != LinphoneReasonNone;
Simon Morlat's avatar
Simon Morlat committed
84
}
Simon Morlat's avatar
Simon Morlat committed
85

86
const bctbx_list_t *linphone_core_get_chat_rooms(LinphoneCore *lc) {
87 88 89
	return lc->chatrooms;
}

90
static LinphoneChatRoom *_linphone_core_create_chat_room(LinphoneCore *lc, const LinphoneAddress *addr) {
91
	LinphoneChatRoom *cr = linphone_chat_room_new(lc, addr);
92
	lc->chatrooms = bctbx_list_append(lc->chatrooms, (void *)cr);
93
	return cr;
94 95
}

96
LinphoneChatRoom *_linphone_core_create_chat_room_from_call(LinphoneCall *call){
97
	LinphoneChatRoom *cr = linphone_chat_room_new(linphone_call_get_core(call),
98
		linphone_address_clone(linphone_call_get_remote_address(call)));
Ghislain MARY's avatar
Ghislain MARY committed
99
	linphone_chat_room_set_call(cr, call);
100 101 102
	return cr;
}

103
static LinphoneChatRoom *_linphone_core_create_chat_room_from_url(LinphoneCore *lc, const char *to) {
104 105 106
	LinphoneAddress *parsed_url = NULL;
	if ((parsed_url = linphone_core_interpret_url(lc, to)) != NULL) {
		return _linphone_core_create_chat_room(lc, parsed_url);
aymeric's avatar
aymeric committed
107 108
	}
	return NULL;
Simon Morlat's avatar
Simon Morlat committed
109
}
110

111 112 113 114 115 116 117
static bool_t linphone_chat_room_matches(LinphoneChatRoom *cr, const LinphoneAddress *from) {
	LinphoneAddress *addr = linphone_address_new(L_GET_CPP_PTR_FROM_C_STRUCT(cr, ChatRoom, ChatRoom)->getPeerAddress().asString().c_str());
	bool_t result = linphone_address_weak_equal(addr, from);
	linphone_address_unref(addr);
	return result;
}

118 119
LinphoneChatRoom *_linphone_core_get_chat_room(LinphoneCore *lc, const LinphoneAddress *addr) {
	LinphoneChatRoom *cr = NULL;
120 121
	bctbx_list_t *elem;
	for (elem = lc->chatrooms; elem != NULL; elem = bctbx_list_next(elem)) {
122 123
		cr = (LinphoneChatRoom *)elem->data;
		if (linphone_chat_room_matches(cr, addr)) {
124 125
			break;
		}
126
		cr = NULL;
127 128
	}
	return cr;
129 130
}

131 132
static LinphoneChatRoom *_linphone_core_get_or_create_chat_room(LinphoneCore *lc, const char *to) {
	LinphoneAddress *to_addr = linphone_core_interpret_url(lc, to);
133
	LinphoneChatRoom *ret;
134

135 136
	if (to_addr == NULL) {
		ms_error("linphone_core_get_or_create_chat_room(): Cannot make a valid address with %s", to);
137 138
		return NULL;
	}
139
	ret = _linphone_core_get_chat_room(lc, to_addr);
Simon Morlat's avatar
Simon Morlat committed
140
	linphone_address_unref(to_addr);
141 142
	if (!ret) {
		ret = _linphone_core_create_chat_room_from_url(lc, to);
143 144
	}
	return ret;
145 146
}

147
LinphoneChatRoom *linphone_core_get_chat_room(LinphoneCore *lc, const LinphoneAddress *addr) {
148 149
	LinphoneChatRoom *ret = _linphone_core_get_chat_room(lc, addr);
	if (!ret) {
150
		ret = _linphone_core_create_chat_room(lc, addr);
151
	}
152
	return ret;
153
}
154

155 156
LinphoneChatRoom * linphone_core_create_client_group_chat_room(LinphoneCore *lc, const bctbx_list_t *addresses) {
	return linphone_client_group_chat_room_new(lc, addresses);
157 158
}

159
void linphone_core_delete_chat_room(LinphoneCore *lc, LinphoneChatRoom *cr) {
160
	if (bctbx_list_find(lc->chatrooms, cr)) {
Ghislain MARY's avatar
Ghislain MARY committed
161
		lc->chatrooms = bctbx_list_remove(lc->chatrooms, cr);
Simon Morlat's avatar
Simon Morlat committed
162
		linphone_chat_room_delete_history(cr);
Simon Morlat's avatar
Simon Morlat committed
163
		linphone_chat_room_unref(cr);
164 165
	} else {
		ms_error("linphone_core_delete_chat_room(): chatroom [%p] isn't part of LinphoneCore.", cr);
Simon Morlat's avatar
Simon Morlat committed
166 167 168
	}
}

169
LinphoneChatRoom *linphone_core_get_chat_room_from_uri(LinphoneCore *lc, const char *to) {
170
	return _linphone_core_get_or_create_chat_room(lc, to);
171
}
172

173 174 175
void linphone_chat_message_update_state(LinphoneChatMessage *msg, LinphoneChatMessageState new_state) {
	linphone_chat_message_set_state(msg, new_state);
	linphone_chat_message_store_state(msg);
176

177
	if (msg->state == LinphoneChatMessageStateDelivered || msg->state == LinphoneChatMessageStateNotDelivered) {
178
		L_GET_PRIVATE_FROM_C_STRUCT(msg->chat_room, ChatRoom, ChatRoom)->moveTransientMessageToWeakMessages(msg);
179 180 181
	}
}

Ghislain MARY's avatar
Ghislain MARY committed
182
void create_file_transfer_information_from_vnd_gsma_rcs_ft_http_xml(LinphoneChatMessage *msg) {
Ghislain MARY's avatar
Ghislain MARY committed
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
	xmlChar *file_url = NULL;
	xmlDocPtr xmlMessageBody;
	xmlNodePtr cur;
	/* parse the msg body to get all informations from it */
	xmlMessageBody = xmlParseDoc((const xmlChar *)msg->message);
	msg->file_transfer_information = linphone_content_new();

	cur = xmlDocGetRootElement(xmlMessageBody);
	if (cur != NULL) {
		cur = cur->xmlChildrenNode;
		while (cur != NULL) {
			if (!xmlStrcmp(cur->name, (const xmlChar *)"file-info")) {
				/* we found a file info node, check if it has a type="file" attribute */
				xmlChar *typeAttribute = xmlGetProp(cur, (const xmlChar *)"type");
				if (!xmlStrcmp(typeAttribute, (const xmlChar *)"file")) { /* this is the node we are looking for */
					cur = cur->xmlChildrenNode; /* now loop on the content of the file-info node */
					while (cur != NULL) {
						if (!xmlStrcmp(cur->name, (const xmlChar *)"file-size")) {
							xmlChar *fileSizeString = xmlNodeListGetString(xmlMessageBody, cur->xmlChildrenNode, 1);
							linphone_content_set_size(msg->file_transfer_information, strtol((const char *)fileSizeString, NULL, 10));
							xmlFree(fileSizeString);
						}

						if (!xmlStrcmp(cur->name, (const xmlChar *)"file-name")) {
							xmlChar *filename = xmlNodeListGetString(xmlMessageBody, cur->xmlChildrenNode, 1);
							linphone_content_set_name(msg->file_transfer_information, (char *)filename);
							xmlFree(filename);
						}
						if (!xmlStrcmp(cur->name, (const xmlChar *)"content-type")) {
							xmlChar *contentType = xmlNodeListGetString(xmlMessageBody, cur->xmlChildrenNode, 1);
							int contentTypeIndex = 0;
							char *type;
							char *subtype;
							while (contentType[contentTypeIndex] != '/' && contentType[contentTypeIndex] != '\0') {
								contentTypeIndex++;
							}
							type = ms_strndup((char *)contentType, contentTypeIndex);
							subtype = ms_strdup(((char *)contentType + contentTypeIndex + 1));
							linphone_content_set_type(msg->file_transfer_information, type);
							linphone_content_set_subtype(msg->file_transfer_information, subtype);
							ms_free(subtype);
							ms_free(type);
							xmlFree(contentType);
						}
						if (!xmlStrcmp(cur->name, (const xmlChar *)"data")) {
							file_url = xmlGetProp(cur, (const xmlChar *)"url");
						}

231
						if (!xmlStrcmp(cur->name, (const xmlChar *)"file-key")) {
Ghislain MARY's avatar
Ghislain MARY committed
232 233 234
							/* there is a key in the msg: file has been encrypted */
							/* convert the key from base 64 */
							xmlChar *keyb64 = xmlNodeListGetString(xmlMessageBody, cur->xmlChildrenNode, 1);
Ghislain MARY's avatar
Ghislain MARY committed
235
							size_t keyLength = b64::b64_decode((char *)keyb64, strlen((char *)keyb64), NULL, 0);
Ghislain MARY's avatar
Ghislain MARY committed
236 237
							uint8_t *keyBuffer = (uint8_t *)malloc(keyLength);
							/* decode the key into local key buffer */
Ghislain MARY's avatar
Ghislain MARY committed
238
							b64::b64_decode((char *)keyb64, strlen((char *)keyb64), keyBuffer, keyLength);
239
							linphone_content_set_key(msg->file_transfer_information, (char *)keyBuffer, keyLength);
Ghislain MARY's avatar
Ghislain MARY committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
							/* duplicate key value into the linphone content private structure */
							xmlFree(keyb64);
							free(keyBuffer);
						}

						cur = cur->next;
					}
					xmlFree(typeAttribute);
					break;
				}
				xmlFree(typeAttribute);
			}
			cur = cur->next;
		}
	}
	xmlFreeDoc(xmlMessageBody);

	linphone_chat_message_set_external_body_url(msg, (const char *)file_url);
	xmlFree(file_url);
}

Ghislain MARY's avatar
Ghislain MARY committed
261 262
int linphone_core_message_received(LinphoneCore *lc, SalOp *op, const SalMessage *sal_msg) {
	LinphoneAddress *addr = linphone_address_new(sal_msg->from);
263
	linphone_address_clean(addr);
Ghislain MARY's avatar
Ghislain MARY committed
264
	LinphoneChatRoom *cr = linphone_core_get_chat_room(lc, addr);
265
	LinphoneReason reason = L_GET_PRIVATE_FROM_C_STRUCT(cr, ChatRoom, ChatRoom)->messageReceived(op, sal_msg);
Simon Morlat's avatar
Simon Morlat committed
266
	linphone_address_unref(addr);
267
	return reason;
aymeric's avatar
aymeric committed
268 269
}

270 271 272 273 274 275 276 277 278 279 280
void _linphone_chat_message_resend(LinphoneChatMessage *msg, bool_t ref_msg) {
	LinphoneChatMessageState state = linphone_chat_message_get_state(msg);
	LinphoneChatRoom *cr;

	if (state != LinphoneChatMessageStateNotDelivered) {
		ms_warning("Cannot resend chat message in state %s", linphone_chat_message_state_to_string(state));
		return;
	}

	cr = linphone_chat_message_get_chat_room(msg);
	if (ref_msg) linphone_chat_message_ref(msg);
281
	L_GET_CPP_PTR_FROM_C_STRUCT(cr, ChatRoom, ChatRoom)->sendMessage(msg);
282 283 284 285 286 287 288 289 290 291
}

void linphone_chat_message_resend(LinphoneChatMessage *msg) {
	_linphone_chat_message_resend(msg, FALSE);
}

void linphone_chat_message_resend_2(LinphoneChatMessage *msg) {
	_linphone_chat_message_resend(msg, TRUE);
}

Ghislain MARY's avatar
Ghislain MARY committed
292
static char *linphone_chat_message_create_imdn_xml(LinphoneChatMessage *cm, ImdnType imdn_type, LinphoneReason reason) {
Ghislain MARY's avatar
Ghislain MARY committed
293 294 295 296 297
	xmlBufferPtr buf;
	xmlTextWriterPtr writer;
	int err;
	char *content = NULL;
	char *datetime = NULL;
298 299 300 301 302
	const char *message_id;

	/* Check that the chat message has a message id */
	message_id = linphone_chat_message_get_message_id(cm);
	if (message_id == NULL) return NULL;
Ghislain MARY's avatar
Ghislain MARY committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320

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

	datetime = linphone_timestamp_to_rfc3339_string(linphone_chat_message_get_time(cm));
	err = xmlTextWriterStartDocument(writer, "1.0", "UTF-8", NULL);
	if (err >= 0) {
		err = xmlTextWriterStartElementNS(writer, NULL, (const xmlChar *)"imdn",
										  (const xmlChar *)"urn:ietf:params:xml:ns:imdn");
	}
321 322 323
	if ((err >= 0) && (reason != LinphoneReasonNone)) {
		err = xmlTextWriterWriteAttributeNS(writer, (const xmlChar *)"xmlns", (const xmlChar *)"linphoneimdn", NULL, (const xmlChar *)"http://www.linphone.org/xsds/imdn.xsd");
	}
Ghislain MARY's avatar
Ghislain MARY committed
324
	if (err >= 0) {
325
		err = xmlTextWriterWriteElement(writer, (const xmlChar *)"message-id", (const xmlChar *)message_id);
Ghislain MARY's avatar
Ghislain MARY committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
	}
	if (err >= 0) {
		err = xmlTextWriterWriteElement(writer, (const xmlChar *)"datetime", (const xmlChar *)datetime);
	}
	if (err >= 0) {
		if (imdn_type == ImdnTypeDelivery) {
			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) {
341 342 343 344 345 346
		if (reason == LinphoneReasonNone) {
			if (imdn_type == ImdnTypeDelivery) {
				err = xmlTextWriterStartElement(writer, (const xmlChar *)"delivered");
			} else {
				err = xmlTextWriterStartElement(writer, (const xmlChar *)"displayed");
			}
Ghislain MARY's avatar
Ghislain MARY committed
347
		} else {
348
			err = xmlTextWriterStartElement(writer, (const xmlChar *)"error");
Ghislain MARY's avatar
Ghislain MARY committed
349 350 351
		}
	}
	if (err >= 0) {
352
		/* Close the "delivered", "displayed" or "error" element. */
Ghislain MARY's avatar
Ghislain MARY committed
353 354
		err = xmlTextWriterEndElement(writer);
	}
355 356 357 358 359 360 361 362 363 364 365 366 367 368
	if ((err >= 0) && (reason != LinphoneReasonNone)) {
		err = xmlTextWriterStartElementNS(writer, (const xmlChar *)"linphoneimdn", (const xmlChar *)"reason", NULL);
		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);
		}
	}
Ghislain MARY's avatar
Ghislain MARY committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
	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 = ms_strdup((char *)buf->content);
	}
	xmlFreeTextWriter(writer);
	xmlBufferFree(buf);
	ms_free(datetime);
	return content;
}

Ghislain MARY's avatar
Ghislain MARY committed
394 395 396
void linphone_chat_message_send_imdn(LinphoneChatMessage *cm, ImdnType imdn_type, LinphoneReason reason) {
	char *content = linphone_chat_message_create_imdn_xml(cm, imdn_type, reason);
	if (content) {
397
		L_GET_PRIVATE_FROM_C_STRUCT(linphone_chat_message_get_chat_room(cm), ChatRoom, ChatRoom)->sendImdn(content, reason);
Ghislain MARY's avatar
Ghislain MARY committed
398 399 400 401
		ms_free(content);
	}
}

402
void linphone_chat_message_send_delivery_notification(LinphoneChatMessage *cm, LinphoneReason reason) {
Ghislain MARY's avatar
Ghislain MARY committed
403 404 405 406
	LinphoneChatRoom *cr = linphone_chat_message_get_chat_room(cm);
	LinphoneCore *lc = linphone_chat_room_get_core(cr);
	LinphoneImNotifPolicy *policy = linphone_core_get_im_notif_policy(lc);
	if (linphone_im_notif_policy_get_send_imdn_delivered(policy) == TRUE) {
407
		linphone_chat_message_send_imdn(cm, ImdnTypeDelivery, reason);
Ghislain MARY's avatar
Ghislain MARY committed
408
	}
Ghislain MARY's avatar
Ghislain MARY committed
409 410 411
}

void linphone_chat_message_send_display_notification(LinphoneChatMessage *cm) {
Ghislain MARY's avatar
Ghislain MARY committed
412 413 414 415
	LinphoneChatRoom *cr = linphone_chat_message_get_chat_room(cm);
	LinphoneCore *lc = linphone_chat_room_get_core(cr);
	LinphoneImNotifPolicy *policy = linphone_core_get_im_notif_policy(lc);
	if (linphone_im_notif_policy_get_send_imdn_displayed(policy) == TRUE) {
416
		linphone_chat_message_send_imdn(cm, ImdnTypeDisplay, LinphoneReasonNone);
Ghislain MARY's avatar
Ghislain MARY committed
417
	}
Ghislain MARY's avatar
Ghislain MARY committed
418 419
}

Sylvain Berfini's avatar
Sylvain Berfini committed
420
void linphone_core_real_time_text_received(LinphoneCore *lc, LinphoneChatRoom *cr, uint32_t character, LinphoneCall *call) {
421 422 423 424 425 426
	if (linphone_core_realtime_text_enabled(lc)) {
		std::shared_ptr<LinphonePrivate::RealTimeTextChatRoom> rttcr =
			std::static_pointer_cast<LinphonePrivate::RealTimeTextChatRoom>(L_GET_CPP_PTR_FROM_C_STRUCT(cr, ChatRoom, ChatRoom));
		L_GET_PRIVATE(rttcr)->realtimeTextReceived(character, call);
		//L_GET_PRIVATE(std::static_pointer_cast<LinphonePrivate::RealTimeTextChatRoom>(L_GET_CPP_PTR_FROM_C_STRUCT(cr, ChatRoom, ChatRoom)))->realtimeTextReceived(character, call);
	}
427 428
}

429
LinphoneStatus linphone_chat_message_put_char(LinphoneChatMessage *msg, uint32_t character) {
430
	LinphoneChatRoom *cr = linphone_chat_message_get_chat_room(msg);
431
	if (linphone_core_realtime_text_enabled(linphone_chat_room_get_core(cr))) {
432 433 434 435
		std::shared_ptr<LinphonePrivate::RealTimeTextChatRoom> rttcr =
			std::static_pointer_cast<LinphonePrivate::RealTimeTextChatRoom>(L_GET_CPP_PTR_FROM_C_STRUCT(cr, ChatRoom, ChatRoom));
		LinphoneCall *call = rttcr->getCall();
		LinphoneCore *lc = rttcr->getCore();
436 437 438 439 440 441
		const uint32_t new_line = 0x2028;
		const uint32_t crlf = 0x0D0A;
		const uint32_t lf = 0x0A;

		if (!call || !linphone_call_get_stream(call, LinphoneStreamTypeText)) {
			return -1;
442
		}
443

444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
		if (character == new_line || character == crlf || character == lf) {
			if (lc && lp_config_get_int(lc->config, "misc", "store_rtt_messages", 1) == 1) {
				ms_debug("New line sent, forge a message with content %s", msg->message);
				msg->time = ms_time(0);
				msg->state = LinphoneChatMessageStateDisplayed;
				msg->dir = LinphoneChatMessageOutgoing;
				if (msg->from) linphone_address_unref(msg->from);
				msg->from = linphone_address_new(linphone_core_get_identity(lc));
				msg->storage_id = linphone_chat_message_store(msg);
				ms_free(msg->message);
				msg->message = NULL;
			}
		} else {
			char *value = LinphonePrivate::Utils::utf8ToChar(character);
			msg->message = ms_strcat_printf(msg->message, value);
			ms_debug("Sent RTT character: %s (%lu), pending text is %s", value, (unsigned long)character, msg->message);
			delete value;
		}

		text_stream_putchar32(reinterpret_cast<TextStream *>(linphone_call_get_stream(call, LinphoneStreamTypeText)), character);
		return 0;
	}
	return -1;
467
}
Sylvain Berfini's avatar
Sylvain Berfini committed
468

Ghislain MARY's avatar
Ghislain MARY committed
469 470 471 472
const char* linphone_chat_message_get_message_id(const LinphoneChatMessage *cm) {
	return cm->message_id;
}

473
const char *linphone_chat_message_state_to_string(const LinphoneChatMessageState state) {
jehan's avatar
jehan committed
474
	switch (state) {
475 476 477 478 479 480 481 482 483 484 485 486
	case LinphoneChatMessageStateIdle:
		return "LinphoneChatMessageStateIdle";
	case LinphoneChatMessageStateInProgress:
		return "LinphoneChatMessageStateInProgress";
	case LinphoneChatMessageStateDelivered:
		return "LinphoneChatMessageStateDelivered";
	case LinphoneChatMessageStateNotDelivered:
		return "LinphoneChatMessageStateNotDelivered";
	case LinphoneChatMessageStateFileTransferError:
		return "LinphoneChatMessageStateFileTransferError";
	case LinphoneChatMessageStateFileTransferDone:
		return "LinphoneChatMessageStateFileTransferDone ";
Ghislain MARY's avatar
Ghislain MARY committed
487 488 489 490
	case LinphoneChatMessageStateDeliveredToUser:
		return "LinphoneChatMessageStateDeliveredToUser";
	case LinphoneChatMessageStateDisplayed:
		return "LinphoneChatMessageStateDisplayed";
jehan's avatar
jehan committed
491
	}
Ghislain MARY's avatar
Ghislain MARY committed
492
	return NULL;
jehan's avatar
jehan committed
493
}
494

495
LinphoneChatRoom *linphone_chat_message_get_chat_room(LinphoneChatMessage *msg) {
Simon Morlat's avatar
Simon Morlat committed
496
	return msg->chat_room;
497 498
}

499
const LinphoneAddress *linphone_chat_message_get_peer_address(LinphoneChatMessage *msg) {
500 501 502
	return linphone_chat_room_get_peer_address(msg->chat_room);
}

503 504
const char *linphone_chat_message_get_external_body_url(const LinphoneChatMessage *msg) {
	return msg->external_body_url;
505 506
}

507 508 509
void linphone_chat_message_set_external_body_url(LinphoneChatMessage *msg, const char *url) {
	if (msg->external_body_url) {
		ms_free(msg->external_body_url);
510
	}
511
	msg->external_body_url = url ? ms_strdup(url) : NULL;
512
}
Simon Morlat's avatar
Simon Morlat committed
513

Ghislain MARY's avatar
Ghislain MARY committed
514 515 516 517 518 519 520 521 522 523 524
const char * linphone_chat_message_get_content_type(const LinphoneChatMessage *msg) {
	return msg->content_type;
}

void linphone_chat_message_set_content_type(LinphoneChatMessage *msg, const char *content_type) {
	if (msg->content_type) {
		ms_free(msg->content_type);
	}
	msg->content_type = content_type ? ms_strdup(content_type) : NULL;
}

525
bool_t linphone_chat_message_is_file_transfer(const LinphoneChatMessage *msg) {
Ghislain MARY's avatar
Ghislain MARY committed
526
	return LinphonePrivate::ContentType::isFileTransfer(msg->content_type);
527 528
}

529
bool_t linphone_chat_message_is_text(const LinphoneChatMessage *msg) {
Ghislain MARY's avatar
Ghislain MARY committed
530
	return LinphonePrivate::ContentType::isText(msg->content_type);
531 532 533 534 535 536 537 538 539 540
}

bool_t linphone_chat_message_get_to_be_stored(const LinphoneChatMessage *msg) {
	return msg->to_be_stored;
}

void linphone_chat_message_set_to_be_stored(LinphoneChatMessage *msg, bool_t to_be_stored) {
	msg->to_be_stored = to_be_stored;
}

541 542
const char *linphone_chat_message_get_appdata(const LinphoneChatMessage *msg) {
	return msg->appdata;
543 544
}

545 546 547
void linphone_chat_message_set_appdata(LinphoneChatMessage *msg, const char *data) {
	if (msg->appdata) {
		ms_free(msg->appdata);
548
	}
549 550
	msg->appdata = data ? ms_strdup(data) : NULL;
	linphone_chat_message_store_appdata(msg);
551 552
}

553 554
void linphone_chat_message_set_from_address(LinphoneChatMessage *msg, const LinphoneAddress *from) {
	if (msg->from)
Simon Morlat's avatar
Simon Morlat committed
555
		linphone_address_unref(msg->from);
556
	msg->from = linphone_address_clone(from);
557
}
Simon Morlat's avatar
Simon Morlat committed
558

559 560
const LinphoneAddress *linphone_chat_message_get_from_address(const LinphoneChatMessage *msg) {
	return msg->from;
561
}
562

563 564
void linphone_chat_message_set_to_address(LinphoneChatMessage *msg, const LinphoneAddress *to) {
	if (msg->to)
Simon Morlat's avatar
Simon Morlat committed
565
		linphone_address_unref(msg->to);
566
	msg->to = linphone_address_clone(to);
567 568
}

569 570 571 572
const LinphoneAddress *linphone_chat_message_get_to_address(const LinphoneChatMessage *msg) {
	if (msg->to)
		return msg->to;
	if (msg->dir == LinphoneChatMessageOutgoing) {
573
		return linphone_chat_room_get_peer_address(msg->chat_room);
Simon Morlat's avatar
Simon Morlat committed
574 575 576 577
	}
	return NULL;
}

578 579 580 581 582
void linphone_chat_message_set_is_secured(LinphoneChatMessage *msg, bool_t secured) {
	msg->is_secured = secured;
}

bool_t linphone_chat_message_is_secured(LinphoneChatMessage *msg) {
Ghislain MARY's avatar
Ghislain MARY committed
583
	return msg->is_secured;
584 585
}

586 587
LinphoneAddress *linphone_chat_message_get_local_address(const LinphoneChatMessage *msg) {
	return msg->dir == LinphoneChatMessageOutgoing ? msg->from : msg->to;
Simon Morlat's avatar
Simon Morlat committed
588 589
}

590 591
time_t linphone_chat_message_get_time(const LinphoneChatMessage *msg) {
	return msg->time;
592 593
}

594 595
LinphoneChatMessageState linphone_chat_message_get_state(const LinphoneChatMessage *msg) {
	return msg->state;
Margaux Clerc's avatar
Margaux Clerc committed
596 597
}

598 599
const char *linphone_chat_message_get_text(const LinphoneChatMessage *msg) {
	return msg->message;
600
}
601

602 603 604 605 606 607 608
int linphone_chat_message_set_text(LinphoneChatMessage *msg, const char* text) {
	if (msg->message)
		ms_free(msg->message);
	if (text)
		msg->message = ms_strdup(text);
	else
		msg->message = NULL;
609

610 611 612
	return 0;
}

613 614 615
void linphone_chat_message_add_custom_header(LinphoneChatMessage *msg, const char *header_name,
											 const char *header_value) {
	msg->custom_headers = sal_custom_header_append(msg->custom_headers, header_name, header_value);
616 617
}

618 619
const char *linphone_chat_message_get_custom_header(LinphoneChatMessage *msg, const char *header_name) {
	return sal_custom_header_find(msg->custom_headers, header_name);
620 621
}

622 623 624 625
void linphone_chat_message_remove_custom_header(LinphoneChatMessage *msg, const char *header_name) {
	msg->custom_headers = sal_custom_header_remove(msg->custom_headers, header_name);
}

626
bool_t linphone_chat_message_is_read(LinphoneChatMessage *msg) {
627 628 629 630
	LinphoneChatRoom *cr = linphone_chat_message_get_chat_room(msg);
	LinphoneCore *lc = linphone_chat_room_get_core(cr);
	LinphoneImNotifPolicy *policy = linphone_core_get_im_notif_policy(lc);
	if ((linphone_im_notif_policy_get_recv_imdn_displayed(policy) == TRUE) && (msg->state == LinphoneChatMessageStateDisplayed)) return TRUE;
631 632
	if ((linphone_im_notif_policy_get_recv_imdn_delivered(policy) == TRUE) && (msg->state == LinphoneChatMessageStateDeliveredToUser || msg->state == LinphoneChatMessageStateDisplayed)) return TRUE;
	return (msg->state == LinphoneChatMessageStateDelivered || msg->state == LinphoneChatMessageStateDisplayed || msg->state == LinphoneChatMessageStateDeliveredToUser);
633 634
}

635 636
bool_t linphone_chat_message_is_outgoing(LinphoneChatMessage *msg) {
	return msg->dir == LinphoneChatMessageOutgoing;
637 638
}

639 640
unsigned int linphone_chat_message_get_storage_id(LinphoneChatMessage *msg) {
	return msg->storage_id;
641 642
}

643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
LinphoneChatMessage *linphone_chat_message_clone(const LinphoneChatMessage *msg) {
	LinphoneChatMessage *new_message = linphone_chat_room_create_message(msg->chat_room, msg->message);
	if (msg->external_body_url)
		new_message->external_body_url = ms_strdup(msg->external_body_url);
	if (msg->appdata)
		new_message->appdata = ms_strdup(msg->appdata);
	new_message->message_state_changed_cb = msg->message_state_changed_cb;
	new_message->message_state_changed_user_data = msg->message_state_changed_user_data;
	new_message->message_userdata = msg->message_userdata;
	new_message->time = msg->time;
	new_message->state = msg->state;
	new_message->storage_id = msg->storage_id;
	if (msg->from)
		new_message->from = linphone_address_clone(msg->from);
	if (msg->file_transfer_filepath)
		new_message->file_transfer_filepath = ms_strdup(msg->file_transfer_filepath);
	if (msg->file_transfer_information)
		new_message->file_transfer_information = linphone_content_copy(msg->file_transfer_information);
661
	return new_message;
662
}
663

664
void linphone_chat_message_destroy(LinphoneChatMessage *msg) {
665 666 667
	belle_sip_object_unref(msg);
}

668 669 670
static void _linphone_chat_message_destroy(LinphoneChatMessage *msg) {
	if (msg->op)
		sal_op_release(msg->op);
671 672
	if (msg->ei)
		linphone_error_info_unref(msg->ei);
673 674 675 676 677 678 679
	if (msg->message)
		ms_free(msg->message);
	if (msg->external_body_url)
		ms_free(msg->external_body_url);
	if (msg->appdata)
		ms_free(msg->appdata);
	if (msg->from)
Simon Morlat's avatar
Simon Morlat committed
680
		linphone_address_unref(msg->from);
681
	if (msg->to)
Simon Morlat's avatar
Simon Morlat committed
682
		linphone_address_unref(msg->to);
Ghislain MARY's avatar
Ghislain MARY committed
683 684
	if (msg->message_id)
		ms_free(msg->message_id);
685 686 687 688
	if (msg->custom_headers)
		sal_custom_header_free(msg->custom_headers);
	if (msg->content_type)
		ms_free(msg->content_type);
johan's avatar
johan committed
689
	if (msg->file_transfer_information) {
690
		linphone_content_unref(msg->file_transfer_information);
johan's avatar
johan committed
691
	}
692 693 694
	if (msg->file_transfer_filepath != NULL) {
		ms_free(msg->file_transfer_filepath);
	}
695 696 697
	if (msg->callbacks) {
		linphone_chat_message_cbs_unref(msg->callbacks);
	}
698 699
}

Ghislain MARY's avatar
Ghislain MARY committed
700
void linphone_chat_message_deactivate(LinphoneChatMessage *msg){
701
	if (msg->file_transfer_information != NULL) {
702
		_linphone_chat_message_cancel_file_transfer(msg, FALSE);
703
	}
704 705
	/*mark the chat msg as orphan (it has no chat room anymore)*/
	msg->chat_room = NULL;
706 707
}

Ghislain MARY's avatar
Ghislain MARY committed
708
void linphone_chat_message_release(LinphoneChatMessage *msg) {
709
	linphone_chat_message_deactivate(msg);
Simon Morlat's avatar
Simon Morlat committed
710 711 712
	linphone_chat_message_unref(msg);
}

713
const LinphoneErrorInfo *linphone_chat_message_get_error_info(const LinphoneChatMessage *msg) {
714 715 716
	if (!msg->ei) ((LinphoneChatMessage*)msg)->ei = linphone_error_info_new(); /*let's do it mutable*/
	linphone_error_info_from_sal_op(msg->ei, msg->op);
	return msg->ei;
717
}
718

719
LinphoneReason linphone_chat_message_get_reason(LinphoneChatMessage *msg) {
720
	return linphone_error_info_get_reason(linphone_chat_message_get_error_info(msg));
721 722
}

723
LinphoneChatMessageCbs *linphone_chat_message_get_callbacks(const LinphoneChatMessage *msg) {
724 725
	return msg->callbacks;
}