3 Copyright (C) 2000 Simon MORLAT (simon.morlat@linphone.org)
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.
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.
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.
20 #include "linphonecore.h"
21 #include <eXosip2/eXosip.h>
22 #include <osipparser2/osip_message.h>
26 extern const char *__policy_enum_to_str(LinphoneSubscribePolicy pol);
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 ;
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);
44 void linphone_core_reject_subscriber(LinphoneCore *lc, LinphoneFriend *lf){
45 linphone_friend_set_inc_subscribe_policy(lf,LinphoneSPDeny);
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]);
54 void __linphone_core_notify_all_friends(LinphoneCore *lc, int ss, int os){
58 ms_list_for_each2(lc->friends,__do_notify,(void *)tab);
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);
66 /* check presence state before answering to call; returns TRUE if we can proceed, else answer the appropriate answer
68 bool_t linphone_core_check_presence(LinphoneCore *lc){
72 void linphone_subscription_new(LinphoneCore *lc, eXosip_event_t *ev){
73 LinphoneFriend *lf=NULL;
74 osip_from_t *from=ev->request->from;
76 osip_message_t *msg=NULL;
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){
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 */
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);
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));
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);
109 void linphone_notify_recv(LinphoneCore *lc, eXosip_event_t *ev)
111 const char *status=_("Gone");
112 const char *img="sip-closed.png";
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);
124 ms_error("No body in NOTIFY");
127 if (strstr(body->body,"pending")!=NULL){
128 status=_("Waiting for Approval");
130 estatus=LINPHONE_STATUS_PENDING;
131 }else if ((strstr(body->body,"online")!=NULL) || (strstr(body->body,"open")!=NULL)) {
133 img="sip-online.png";
134 estatus=LINPHONE_STATUS_ONLINE;
135 }else if (strstr(body->body,"busy")!=NULL){
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");
143 estatus=LINPHONE_STATUS_BERIGHTBACK;
144 }else if (strstr(body->body,"away")!=NULL){
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");
152 estatus=LINPHONE_STATUS_ONTHEPHONE;
153 }else if (strstr(body->body,"outtolunch")!=NULL
154 || strstr(body->body,"meal")!=NULL){
155 status=_("Out To Lunch");
157 estatus=LINPHONE_STATUS_OUTTOLUNCH;
158 }else if (strstr(body->body,"closed")!=NULL){
161 estatus=LINPHONE_STATUS_CLOSED;
164 img="sip-closed.png";
165 estatus=LINPHONE_STATUS_OFFLINE;
167 ms_message("We are notified that sip:%s@%s has online status %s",from->url->username,from->url->host,status);
169 lf=linphone_find_friend_by_sid(lc->friends,ev->sid);
172 tmp=linphone_address_as_string(friend);
174 lc->vtable.notify_recv(lc,(LinphoneFriend*)lf,tmp,status,img);
176 if (ev->ss_status==EXOSIP_SUBCRSTATE_TERMINATED) {
179 ms_message("Outgoing subscription terminated by remote.");
182 ms_message("But this person is not part of our friend list, so we don't care.");
186 void linphone_subscription_answered(LinphoneCore *lc, eXosip_event_t *ev){
188 osip_from_t *from=ev->response->to;
190 osip_from_to_str(from,&tmp);
191 LinphoneAddress *uri=linphone_address_new(tmp);
192 linphone_find_friend(lc->friends,uri,&lf);
195 linphone_friend_set_sid(lf,ev->sid);
197 ms_warning("Receiving answer for unknown subscribe sip:%s@%s", from->url->username,from->url->host);
201 void linphone_subscription_closed(LinphoneCore *lc,eXosip_event_t *ev){
203 osip_from_t *from=ev->request->from;
204 lf=linphone_find_friend_by_nid(lc->friends,ev->nid);
207 linphone_friend_set_nid(lf,-1);
209 ms_warning("Receiving unsuscribe for unknown in-subscribtion from sip:%s@%s", from->url->username, from->url->host);