]> sjero.net Git - linphone/blob - coreapi/chat.c
cc36d730566c80c4b2b6835e2804f8c4c4aa43c8
[linphone] / coreapi / chat.c
1 /***************************************************************************
2  *            chat.c
3  *
4  *  Sun Jun  5 19:34:18 2005
5  *  Copyright  2005  Simon Morlat
6  *  Email simon dot morlat at linphone dot org
7  ****************************************************************************/
8
9 /*
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  */
24  
25  #include "linphonecore.h"
26  #include "private.h"
27  
28  LinphoneChatRoom * linphone_core_create_chat_room(LinphoneCore *lc, const char *to){
29         LinphoneAddress *parsed_url=NULL;
30
31         if ((parsed_url=linphone_core_interpret_url(lc,to))!=NULL){
32                 LinphoneChatRoom *cr=ms_new0(LinphoneChatRoom,1);
33                 cr->lc=lc;
34                 cr->peer=linphone_address_as_string(parsed_url);
35                 cr->peer_url=parsed_url;
36                 lc->chatrooms=ms_list_append(lc->chatrooms,(void *)cr);
37                 return cr;
38         }
39         return NULL;
40  }
41  
42  
43  void linphone_chat_room_destroy(LinphoneChatRoom *cr){
44         LinphoneCore *lc=cr->lc;
45         lc->chatrooms=ms_list_remove(lc->chatrooms,(void *) cr);
46         linphone_address_destroy(cr->peer_url);
47         ms_free(cr->peer);
48         if (cr->op)
49                  sal_op_release(cr->op);
50  }
51
52
53 static void _linphone_chat_room_send_message(LinphoneChatRoom *cr, LinphoneChatMessage* msg){
54         const char *route=NULL;
55         const char *identity=linphone_core_find_best_identity(cr->lc,cr->peer_url,&route);
56         SalOp *op=NULL;
57         LinphoneCall *call;
58         if((call = linphone_core_get_call_by_remote_address(cr->lc,cr->peer))!=NULL){
59                 if (call->state==LinphoneCallConnected ||
60                     call->state==LinphoneCallStreamsRunning ||
61                     call->state==LinphoneCallPaused ||
62                     call->state==LinphoneCallPausing ||
63                     call->state==LinphoneCallPausedByRemote){
64                         ms_message("send SIP message through the existing call.");
65                         op = call->op;
66                         call->pending_message=msg;
67                 }
68         }
69         if (op==NULL){
70                 /*sending out of calls*/
71                 op = sal_op_new(cr->lc->sal);
72                 sal_op_set_route(op,route);
73                 if (cr->op!=NULL){
74                         sal_op_release (cr->op);
75                         cr->op=NULL;
76                 }
77                 cr->op=op;
78                 sal_op_set_user_pointer(op, msg); /*if out of call, directly store msg*/
79         }
80         sal_text_send(op,identity,cr->peer,msg->message);
81 }
82
83 void linphone_chat_room_send_message(LinphoneChatRoom *cr, const char *msg) {
84         _linphone_chat_room_send_message(cr,linphone_chat_room_create_message(cr,msg));
85 }
86 bool_t linphone_chat_room_matches(LinphoneChatRoom *cr, const LinphoneAddress *from){
87         if (linphone_address_get_username(cr->peer_url) && linphone_address_get_username(from) && 
88                 strcmp(linphone_address_get_username(cr->peer_url),linphone_address_get_username(from))==0) return TRUE;
89         return FALSE;
90 }
91
92 void linphone_chat_room_text_received(LinphoneChatRoom *cr, LinphoneCore *lc, const LinphoneAddress *from, const char *msg){
93         if (lc->vtable.text_received!=NULL) lc->vtable.text_received(lc, cr, from, msg);
94 }
95
96 void linphone_core_text_received(LinphoneCore *lc, const char *from, const char *msg){
97         MSList *elem;
98         LinphoneChatRoom *cr=NULL;
99         LinphoneAddress *addr;
100         char *cleanfrom;
101
102         addr=linphone_address_new(from);
103         linphone_address_clean(addr);
104         for(elem=lc->chatrooms;elem!=NULL;elem=ms_list_next(elem)){
105                 cr=(LinphoneChatRoom*)elem->data;
106                 if (linphone_chat_room_matches(cr,addr)){
107                         break;
108                 }
109                 cr=NULL;
110         }
111         cleanfrom=linphone_address_as_string(addr);
112         if (cr==NULL){
113                 /* create a new chat room */
114                 cr=linphone_core_create_chat_room(lc,cleanfrom);
115         }
116
117         linphone_address_destroy(addr);
118         linphone_chat_room_text_received(cr,lc,cr->peer_url,msg);
119         ms_free(cleanfrom);
120 }
121
122 LinphoneCore* linphone_chat_room_get_lc(LinphoneChatRoom *cr){
123         return cr->lc;
124 }
125
126 LinphoneChatRoom* linphone_chat_message_get_chat_room(LinphoneChatMessage *msg){
127         return msg->chat_room;
128 }
129
130 void linphone_chat_room_set_user_data(LinphoneChatRoom *cr, void * ud){
131         cr->user_data=ud;
132 }
133 void * linphone_chat_room_get_user_data(LinphoneChatRoom *cr){
134         return cr->user_data;
135 }
136 const LinphoneAddress* linphone_chat_room_get_peer_address(LinphoneChatRoom *cr) {
137         return cr->peer_url;
138 }
139
140 LinphoneChatMessage* linphone_chat_room_create_message(const LinphoneChatRoom *cr,const char* message) {
141         LinphoneChatMessage* msg = ms_new0(LinphoneChatMessage,1);
142         msg->chat_room=(LinphoneChatRoom*)cr;
143         msg->message=ms_strdup(message);
144         return msg;
145 }
146
147 void linphone_chat_message_destroy(LinphoneChatMessage* msg) {
148         if (msg->message) ms_free(msg->message);
149         ms_free(msg);
150 }
151
152 void linphone_chat_room_send_message2(LinphoneChatRoom *cr, LinphoneChatMessage* msg,LinphoneChatMessageStateChangeCb status_cb,void* ud) {
153         msg->cb=status_cb;
154         msg->cb_ud=ud;
155         _linphone_chat_room_send_message(cr, msg);
156 }
157
158 const char* linphone_chat_message_state_to_string(const LinphoneChatMessageState state) {
159         switch (state) {
160                 case LinphoneChatMessageStateIdle:return "LinphoneChatMessageStateIdle"; 
161                 case LinphoneChatMessageStateInProgress:return "LinphoneChatMessageStateInProgress";
162                 case LinphoneChatMessageStateDelivered:return "LinphoneChatMessageStateDelivered";
163                 case  LinphoneChatMessageStateNotDelivered:return "LinphoneChatMessageStateNotDelivered";
164                 default: return "Unknown state";
165         }
166         
167 }
168
169 char* linphone_chat_message_get_message(LinphoneChatMessage* msg) {
170         return msg->message;
171 }
172
173 const LinphoneAddress* linphone_chat_message_get_peer_address(LinphoneChatMessage *msg) {
174         return linphone_chat_room_get_peer_address(msg->chat_room);
175 }
176
177 /**
178  * user pointer set function
179  */
180 void linphone_chat_message_set_user_data(LinphoneChatMessage* message,void* ud) {
181         message->message_userdata=ud;
182 }
183 /**
184  * user pointer get function
185  */
186 void* linphone_chat_message_get_user_data(const LinphoneChatMessage* message) {
187         return message->message_userdata;
188 }