ms_free(cr->peer);
}
+
+
static void _linphone_chat_room_send_message(LinphoneChatRoom *cr, LinphoneChatMessage* msg){
const char *route=NULL;
const char *identity=linphone_core_find_best_identity(cr->lc,cr->peer_url,&route);
SalOp *op=NULL;
LinphoneCall *call;
char* content_type;
+ time_t t=time(NULL);
if (lp_config_get_int(cr->lc->config,"sip","chat_use_call_dialogs",0)){
if((call = linphone_core_get_call_by_remote_address(cr->lc,cr->peer))!=NULL){
}
}
}
+ msg->time=t;
if (op==NULL){
/*sending out of calls*/
op = sal_op_new(cr->lc->sal);
} else {
sal_text_send(op, identity, cr->peer,msg->message);
}
+ msg->dir=LinphoneChatMessageOutgoing;
+ msg->from=linphone_address_new(identity);
+ linphone_chat_message_store(msg);
}
/**
}
-void linphone_core_message_received(LinphoneCore *lc, SalOp *op, const SalMessage *sal_msg){
+/**
+ * Retrieve an existing chat room whose peer is the supplied address, if exists.
+ * @param lc the linphone core
+ * @param add a linphone address.
+ * @returns the matching chatroom, or NULL if no such chatroom exists.
+**/
+LinphoneChatRoom *linphone_core_get_chat_room(LinphoneCore *lc, const LinphoneAddress *addr){
+ LinphoneChatRoom *cr=NULL;
MSList *elem;
+ for(elem=lc->chatrooms;elem!=NULL;elem=ms_list_next(elem)){
+ cr=(LinphoneChatRoom*)elem->data;
+ if (linphone_chat_room_matches(cr,addr)){
+ break;
+ }
+ cr=NULL;
+ }
+ return cr;
+}
+
+void linphone_core_message_received(LinphoneCore *lc, SalOp *op, const SalMessage *sal_msg){
+
LinphoneChatRoom *cr=NULL;
LinphoneAddress *addr;
char *cleanfrom;
+ char *from;
LinphoneChatMessage* msg;
const SalCustomHeader *ch;
addr=linphone_address_new(sal_msg->from);
linphone_address_clean(addr);
- for(elem=lc->chatrooms;elem!=NULL;elem=ms_list_next(elem)){
- cr=(LinphoneChatRoom*)elem->data;
- if (linphone_chat_room_matches(cr,addr)){
- break;
- }
- cr=NULL;
- }
+ cr=linphone_core_get_chat_room(lc,addr);
cleanfrom=linphone_address_as_string(addr);
+ from=linphone_address_as_string_uri_only(addr);
if (cr==NULL){
/* create a new chat room */
cr=linphone_core_create_chat_room(lc,cleanfrom);
}
msg = linphone_chat_room_create_message(cr, sal_msg->text);
linphone_chat_message_set_from(msg, cr->peer_url);
+
+ {
+ LinphoneAddress *to;
+ to=sal_op_get_to(op) ? linphone_address_new(sal_op_get_to(op)) : linphone_address_new(linphone_core_get_identity(lc));
+ msg->to=to;
+ }
+
msg->time=sal_msg->time;
+ msg->state=LinphoneChatMessageStateDelivered;
+ msg->is_read=FALSE;
ch=sal_op_get_custom_header(op);
if (ch) msg->custom_headers=sal_custom_header_clone(ch);
linphone_chat_message_set_external_body_url(msg, sal_msg->url);
}
linphone_address_destroy(addr);
+ linphone_chat_message_store(msg);
linphone_chat_room_message_received(cr,lc,msg);
ms_free(cleanfrom);
+ ms_free(from);
}
/**
LinphoneChatMessage* msg = ms_new0(LinphoneChatMessage,1);
msg->chat_room=(LinphoneChatRoom*)cr;
msg->message=message?ms_strdup(message):NULL;
+ msg->is_read=TRUE;
return msg;
}
void linphone_chat_room_send_message2(LinphoneChatRoom *cr, LinphoneChatMessage* msg,LinphoneChatMessageStateChangeCb status_cb, void* ud) {
msg->cb=status_cb;
msg->cb_ud=ud;
+ msg->state=LinphoneChatMessageStateInProgress;
_linphone_chat_room_send_message(cr, msg);
}
*@param message #LinphoneChatMessage obj
*@return #LinphoneAddress
*/
-LinphoneAddress* linphone_chat_message_get_from(const LinphoneChatMessage* message) {
+const LinphoneAddress* linphone_chat_message_get_from(const LinphoneChatMessage* message) {
return message->from;
}
+/**
+ * Get destination of the message
+ *@param message #LinphoneChatMessage obj
+ *@return #LinphoneAddress
+ */
+const LinphoneAddress* linphone_chat_message_get_to(const LinphoneChatMessage* message){
+ if (message->to) return message->to;
+ if (message->dir==LinphoneChatMessageOutgoing){
+ return message->chat_room->peer_url;
+ }
+ return NULL;
+}
+
+/**
+ * Returns the origin address of a message if it was a outgoing message, or the destination address if it was an incoming message.
+ *@param message #LinphoneChatMessage obj
+ *@return #LinphoneAddress
+ */
+LinphoneAddress *linphone_chat_message_get_local_address(const LinphoneChatMessage* message){
+ return message->dir==LinphoneChatMessageOutgoing ? message->from : message->to;
+}
+
/**
* Get the time the message was sent.
*/
return message->time;
}
+/**
+ * Get the state of the message
+ *@param message #LinphoneChatMessage obj
+ *@return #LinphoneChatMessageState
+ */
+LinphoneChatMessageState linphone_chat_message_get_state(const LinphoneChatMessage* message) {
+ return message->state;
+}
+
/**
* Get text part of this message
* @return text or NULL if no text.
void* message_userdata;
char* external_body_url;
LinphoneAddress* from;
+ time_t time;
+ SalCustomHeader *custom_headers;
+ LinphoneChatMessageState state;
};*/
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);
new_message->cb_ud=msg->cb_ud;
new_message->message_userdata=msg->message_userdata;
new_message->cb=msg->cb;
+ new_message->time=msg->time;
+ new_message->state=msg->state;
if (msg->from) new_message->from=linphone_address_clone(msg->from);
return new_message;
}
if (msg->message) ms_free(msg->message);
if (msg->external_body_url) ms_free(msg->external_body_url);
if (msg->from) linphone_address_destroy(msg->from);
+ if (msg->to) linphone_address_destroy(msg->to);
if (msg->custom_headers) sal_custom_header_free(msg->custom_headers);
ms_free(msg);
}