chat.c 7.2 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"
35
#include "ortp/b64.h"
36
#include "linphone/wrapper_utils.h"
37

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

Ronan's avatar
Ronan committed
48 49
using namespace std;

50 51
void linphone_core_disable_chat(LinphoneCore *lc, LinphoneReason deny_reason) {
	lc->chat_deny_code = deny_reason;
52 53
}

54 55
void linphone_core_enable_chat(LinphoneCore *lc) {
	lc->chat_deny_code = LinphoneReasonNone;
56 57
}

58 59
bool_t linphone_core_chat_enabled(const LinphoneCore *lc) {
	return lc->chat_deny_code != LinphoneReasonNone;
60
}
Simon Morlat's avatar
Simon Morlat committed
61

62 63 64
const bctbx_list_t *linphone_core_get_chat_rooms (LinphoneCore *lc) {
	if (lc->chat_rooms)
		bctbx_list_free_with_data(lc->chat_rooms, (bctbx_list_free_func)linphone_chat_room_unref);
65
	lc->chat_rooms = L_GET_RESOLVED_C_LIST_FROM_CPP_LIST(L_GET_CPP_PTR_FROM_C_OBJECT(lc)->getChatRooms());
66
	return lc->chat_rooms;
67 68
}

69
static LinphoneChatRoom *linphone_chat_room_new (LinphoneCore *core, const LinphoneAddress *addr) {
70
	return L_GET_C_BACK_PTR(L_GET_CPP_PTR_FROM_C_OBJECT(core)->getOrCreateBasicChatRoom(
71
		*L_GET_CPP_PTR_FROM_C_OBJECT(addr),
72
		!!linphone_core_realtime_text_enabled(core)
73 74 75
	));
}

76
LinphoneChatRoom *_linphone_core_create_chat_room_from_call(LinphoneCall *call){
77
	LinphoneChatRoom *cr = linphone_chat_room_new(linphone_call_get_core(call),
78
		linphone_address_clone(linphone_call_get_remote_address(call)));
Ghislain MARY's avatar
Ghislain MARY committed
79
	linphone_chat_room_set_call(cr, call);
80 81 82
	return cr;
}

83
LinphoneChatRoom *linphone_core_get_chat_room (LinphoneCore *lc, const LinphoneAddress *addr) {
84
	return L_GET_C_BACK_PTR(L_GET_CPP_PTR_FROM_C_OBJECT(lc)->getOrCreateBasicChatRoom(*L_GET_CPP_PTR_FROM_C_OBJECT(addr)));
85 86
}

87 88 89 90 91
LinphoneChatRoom *linphone_core_get_chat_room_2 (
	LinphoneCore *lc,
	const LinphoneAddress *peer_addr,
	const LinphoneAddress *local_addr
) {
92
	return L_GET_C_BACK_PTR(L_GET_CPP_PTR_FROM_C_OBJECT(lc)->getOrCreateBasicChatRoom(LinphonePrivate::ConferenceId(
93 94 95 96 97
		LinphonePrivate::IdentityAddress(*L_GET_CPP_PTR_FROM_C_OBJECT(peer_addr)),
		LinphonePrivate::IdentityAddress(*L_GET_CPP_PTR_FROM_C_OBJECT(local_addr))
	)));
}

98 99
LinphoneChatRoom *linphone_core_create_client_group_chat_room (LinphoneCore *lc, const char *subject, bool_t fallback) {
	return L_GET_C_BACK_PTR(L_GET_CPP_PTR_FROM_C_OBJECT(lc)->createClientGroupChatRoom(L_C_TO_STRING(subject), !!fallback));
100 101
}

102 103 104 105
LinphoneChatRoom *linphone_core_create_client_group_chat_room_2 (LinphoneCore *lc, const char *subject, bool_t fallback, bool_t encrypted) {
	return L_GET_C_BACK_PTR(L_GET_CPP_PTR_FROM_C_OBJECT(lc)->createClientGroupChatRoom(L_C_TO_STRING(subject), !!fallback, !!encrypted));
}

106
LinphoneChatRoom *_linphone_core_create_server_group_chat_room (LinphoneCore *lc, LinphonePrivate::SalCallOp *op) {
107
	return _linphone_server_group_chat_room_new(lc, op);
108 109
}

110
void linphone_core_delete_chat_room (LinphoneCore *, LinphoneChatRoom *cr) {
111
	L_GET_CPP_PTR_FROM_C_OBJECT(cr)->deleteFromDb();
Simon Morlat's avatar
Simon Morlat committed
112 113
}

114
LinphoneChatRoom *linphone_core_get_chat_room_from_uri(LinphoneCore *lc, const char *to) {
115
	return L_GET_C_BACK_PTR(L_GET_CPP_PTR_FROM_C_OBJECT(lc)->getOrCreateBasicChatRoomFromUri(L_C_TO_STRING(to)));
116
}
117

118 119
LinphoneChatRoom *linphone_core_find_chat_room(
	const LinphoneCore *lc,
120 121
	const LinphoneAddress *peer_addr,
	const LinphoneAddress *local_addr
122
) {
123
	return L_GET_C_BACK_PTR(L_GET_CPP_PTR_FROM_C_OBJECT(lc)->findChatRoom(LinphonePrivate::ConferenceId(
124 125
		LinphonePrivate::IdentityAddress(*L_GET_CPP_PTR_FROM_C_OBJECT(peer_addr)),
		LinphonePrivate::IdentityAddress(*L_GET_CPP_PTR_FROM_C_OBJECT(local_addr))
126 127 128
	)));
}

129 130 131 132 133
LinphoneChatRoom *linphone_core_find_one_to_one_chat_room (
	const LinphoneCore *lc,
	const LinphoneAddress *local_addr,
	const LinphoneAddress *participant_addr
) {
134
	return L_GET_C_BACK_PTR(L_GET_CPP_PTR_FROM_C_OBJECT(lc)->findOneToOneChatRoom(
135
		LinphonePrivate::IdentityAddress(*L_GET_CPP_PTR_FROM_C_OBJECT(local_addr)),
136 137 138 139 140 141 142 143 144 145 146 147 148 149
		LinphonePrivate::IdentityAddress(*L_GET_CPP_PTR_FROM_C_OBJECT(participant_addr)),
		false)
	);
}

LinphoneChatRoom *linphone_core_find_one_to_one_chat_room_2 (
	const LinphoneCore *lc,
	const LinphoneAddress *local_addr,
	const LinphoneAddress *participant_addr,
	bool_t encrypted
) {
	return L_GET_C_BACK_PTR(L_GET_CPP_PTR_FROM_C_OBJECT(lc)->findOneToOneChatRoom(
		LinphonePrivate::IdentityAddress(*L_GET_CPP_PTR_FROM_C_OBJECT(local_addr)),
		LinphonePrivate::IdentityAddress(*L_GET_CPP_PTR_FROM_C_OBJECT(participant_addr)),
150
		!!encrypted)
151
	);
152 153
}

154
int linphone_core_message_received(LinphoneCore *lc, LinphonePrivate::SalOp *op, const SalMessage *sal_msg) {
155
	LinphoneReason reason = LinphoneReasonNotAcceptable;
156 157
	std::string peerAddress;
	std::string localAddress;
158
	if (linphone_core_conference_server_enabled(lc)) {
159
		localAddress = peerAddress = op->getTo();
160
	} else {
161 162
		peerAddress = op->getFrom();
		localAddress = op->getTo();
163
	}
164

165
	LinphonePrivate::ConferenceId conferenceId{
166 167 168
		LinphonePrivate::IdentityAddress(peerAddress),
		LinphonePrivate::IdentityAddress(localAddress)
	};
169
	shared_ptr<LinphonePrivate::AbstractChatRoom> chatRoom = L_GET_CPP_PTR_FROM_C_OBJECT(lc)->findChatRoom(conferenceId);
170
	if (chatRoom)
171
		reason = L_GET_PRIVATE(chatRoom)->onSipMessageReceived(op, sal_msg);
172
	else if (!linphone_core_conference_server_enabled(lc)) {
173
		chatRoom = L_GET_CPP_PTR_FROM_C_OBJECT(lc)->getOrCreateBasicChatRoom(conferenceId);
174 175
		if (chatRoom)
			reason = L_GET_PRIVATE(chatRoom)->onSipMessageReceived(op, sal_msg);
176
	}
177
	return reason;
aymeric's avatar
aymeric committed
178 179
}

Sylvain Berfini's avatar
Sylvain Berfini committed
180
void linphone_core_real_time_text_received(LinphoneCore *lc, LinphoneChatRoom *cr, uint32_t character, LinphoneCall *call) {
181 182 183
	if (!(L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getCapabilities() & LinphonePrivate::ChatRoom::Capabilities::RealTimeText))
		return;
	L_GET_PRIVATE_FROM_C_OBJECT(cr, RealTimeTextChatRoom)->realtimeTextReceived(character, L_GET_CPP_PTR_FROM_C_OBJECT(call));
184
}
185 186 187 188 189

unsigned int linphone_chat_message_store(LinphoneChatMessage *msg) {
	// DO nothing, just for old JNI compat...
	return 1;
}