]> sjero.net Git - linphone/blob - linphone/coreapi/presence.c
remove mediastreamer2 and add it as a submodule instead.
[linphone] / linphone / coreapi / presence.c
1 /*
2 linphone
3 Copyright (C) 2000  Simon MORLAT (simon.morlat@linphone.org)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19
20 #include "linphonecore.h"
21 #include <eXosip2/eXosip.h>
22 #include <osipparser2/osip_message.h>
23 #include "private.h"
24
25
26 extern const char *__policy_enum_to_str(LinphoneSubscribePolicy pol);
27
28
29 void linphone_core_add_subscriber(LinphoneCore *lc, const char *subscriber, int did, int nid){
30         LinphoneFriend *fl=linphone_friend_new_with_addr(subscriber);
31         if (fl==NULL) return ;
32         fl->in_did=did;
33         linphone_friend_set_nid(fl,nid);
34         linphone_friend_set_inc_subscribe_policy(fl,LinphoneSPAccept);
35         fl->inc_subscribe_pending=TRUE;
36         lc->subscribers=ms_list_append(lc->subscribers,(void *)fl);
37         if (lc->vtable.new_unknown_subscriber!=NULL) {
38                 char *subscriber=linphone_address_as_string(fl->uri);
39                 lc->vtable.new_unknown_subscriber(lc,fl,subscriber);
40                 ms_free(subscriber);
41         }
42 }
43
44 void linphone_core_reject_subscriber(LinphoneCore *lc, LinphoneFriend *lf){
45         linphone_friend_set_inc_subscribe_policy(lf,LinphoneSPDeny);
46 }
47
48 static void __do_notify(void * data, void * user_data){
49         int *tab=(int*)user_data;
50         LinphoneFriend *lf=(LinphoneFriend*)data;
51         linphone_friend_notify(lf,tab[0],tab[1]);
52 }
53
54 void __linphone_core_notify_all_friends(LinphoneCore *lc, int ss, int os){
55         int tab[2];
56         tab[0]=ss;
57         tab[1]=os;
58         ms_list_for_each2(lc->friends,__do_notify,(void *)tab);
59 }
60
61 void linphone_core_notify_all_friends(LinphoneCore *lc, LinphoneOnlineStatus os){
62         ms_message("Notifying all friends that we are in status %i",os);
63         __linphone_core_notify_all_friends(lc,EXOSIP_SUBCRSTATE_ACTIVE,os);
64 }
65
66 /* check presence state before answering to call; returns TRUE if we can proceed, else answer the appropriate answer
67 to close the dialog*/
68 bool_t linphone_core_check_presence(LinphoneCore *lc){
69         return TRUE;
70 }
71
72 void linphone_subscription_new(LinphoneCore *lc, eXosip_event_t *ev){
73         LinphoneFriend *lf=NULL;
74         osip_from_t *from=ev->request->from;
75         char *tmp;
76         osip_message_t *msg=NULL;
77         LinphoneAddress *uri;
78         osip_from_to_str(ev->request->from,&tmp);
79         uri=linphone_address_new(tmp);
80         ms_message("Receiving new subscription from %s.",tmp);
81         /* check if we answer to this subscription */
82         if (linphone_find_friend(lc->friends,uri,&lf)!=NULL){
83                 lf->in_did=ev->did;
84                 linphone_friend_set_nid(lf,ev->nid);
85                 eXosip_insubscription_build_answer(ev->tid,202,&msg);
86                 eXosip_insubscription_send_answer(ev->tid,202,msg);
87                 __eXosip_wakeup_event();
88                 linphone_friend_done(lf);       /*this will do all necessary actions */
89         }else{
90                 /* check if this subscriber is in our black list */
91                 if (linphone_find_friend(lc->subscribers,uri,&lf)){
92                         if (lf->pol==LinphoneSPDeny){
93                                 ms_message("Rejecting %s because we already rejected it once.",from);
94                                 eXosip_insubscription_send_answer(ev->tid,401,NULL);
95                         }
96                         else {
97                                 /* else it is in wait for approval state, because otherwise it is in the friend list.*/
98                                 ms_message("New subscriber found in friend list, in %s state.",__policy_enum_to_str(lf->pol));
99                         }
100                 }else {
101                         eXosip_insubscription_build_answer(ev->tid,202,&msg);
102                         eXosip_insubscription_send_answer(ev->tid,202,msg);
103                         linphone_core_add_subscriber(lc,tmp,ev->did,ev->nid);
104                 }
105         }
106         osip_free(tmp);
107 }
108
109 void linphone_notify_recv(LinphoneCore *lc, eXosip_event_t *ev)
110 {
111         const char *status=_("Gone");
112         const char *img="sip-closed.png";
113         char *tmp;
114         LinphoneFriend *lf;
115         LinphoneAddress *friend=NULL;
116         osip_from_t *from=NULL;
117         osip_body_t *body=NULL;
118         LinphoneOnlineStatus estatus=LINPHONE_STATUS_UNKNOWN;
119         ms_message("Receiving notify with sid=%i,nid=%i",ev->sid,ev->nid);
120         if (ev->request!=NULL){
121                 from=ev->request->from;
122                 osip_message_get_body(ev->request,0,&body);
123                 if (body==NULL){
124                         ms_error("No body in NOTIFY");
125                         return;
126                 }
127                 if (strstr(body->body,"pending")!=NULL){
128                         status=_("Waiting for Approval");
129                         img="sip-wfa.png";
130                         estatus=LINPHONE_STATUS_PENDING;
131                 }else if ((strstr(body->body,"online")!=NULL) || (strstr(body->body,"open")!=NULL)) {
132                         status=_("Online");
133                         img="sip-online.png";
134                         estatus=LINPHONE_STATUS_ONLINE;
135                 }else if (strstr(body->body,"busy")!=NULL){
136                         status=_("Busy");
137                         img="sip-busy.png";
138                         estatus=LINPHONE_STATUS_BUSY;
139                 }else if (strstr(body->body,"berightback")!=NULL
140                                 || strstr(body->body,"in-transit")!=NULL ){
141                         status=_("Be Right Back");
142                         img="sip-bifm.png";
143                         estatus=LINPHONE_STATUS_BERIGHTBACK;
144                 }else if (strstr(body->body,"away")!=NULL){
145                         status=_("Away");
146                         img="sip-away.png";
147                         estatus=LINPHONE_STATUS_AWAY;
148                 }else if (strstr(body->body,"onthephone")!=NULL
149                         || strstr(body->body,"on-the-phone")!=NULL){
150                         status=_("On The Phone");
151                         img="sip-otp.png";
152                         estatus=LINPHONE_STATUS_ONTHEPHONE;
153                 }else if (strstr(body->body,"outtolunch")!=NULL
154                                 || strstr(body->body,"meal")!=NULL){
155                         status=_("Out To Lunch");
156                         img="sip-otl.png";
157                         estatus=LINPHONE_STATUS_OUTTOLUNCH;
158                 }else if (strstr(body->body,"closed")!=NULL){
159                         status=_("Closed");
160                         img="sip-away.png";
161                         estatus=LINPHONE_STATUS_CLOSED;
162                 }else{
163                         status=_("Gone");
164                         img="sip-closed.png";
165                         estatus=LINPHONE_STATUS_OFFLINE;
166                 }
167                 ms_message("We are notified that sip:%s@%s has online status %s",from->url->username,from->url->host,status);
168         }
169         lf=linphone_find_friend_by_sid(lc->friends,ev->sid);
170         if (lf!=NULL){
171                 friend=lf->uri;
172                 tmp=linphone_address_as_string(friend);
173                 lf->status=estatus;
174                 lc->vtable.notify_recv(lc,(LinphoneFriend*)lf,tmp,status,img);
175                 ms_free(tmp);
176                 if (ev->ss_status==EXOSIP_SUBCRSTATE_TERMINATED) {
177                         lf->sid=-1;
178                         lf->out_did=-1;
179                         ms_message("Outgoing subscription terminated by remote.");
180                 }
181         }else{
182                 ms_message("But this person is not part of our friend list, so we don't care.");
183         }
184 }
185
186 void linphone_subscription_answered(LinphoneCore *lc, eXosip_event_t *ev){
187         LinphoneFriend *lf;
188         osip_from_t *from=ev->response->to;
189         char *tmp;
190         osip_from_to_str(from,&tmp);
191         LinphoneAddress *uri=linphone_address_new(tmp);
192         linphone_find_friend(lc->friends,uri,&lf);
193         if (lf!=NULL){
194                 lf->out_did=ev->did;
195                 linphone_friend_set_sid(lf,ev->sid);
196         }else{
197                 ms_warning("Receiving answer for unknown subscribe sip:%s@%s", from->url->username,from->url->host);
198         }
199         ms_free(tmp);
200 }
201 void linphone_subscription_closed(LinphoneCore *lc,eXosip_event_t *ev){
202         LinphoneFriend *lf;
203         osip_from_t *from=ev->request->from;
204         lf=linphone_find_friend_by_nid(lc->friends,ev->nid);
205         if (lf!=NULL){
206                 lf->in_did=-1;
207                 linphone_friend_set_nid(lf,-1);
208         }else{
209                 ms_warning("Receiving unsuscribe for unknown in-subscribtion from sip:%s@%s", from->url->username, from->url->host);
210         }
211 }