]> sjero.net Git - linphone/blob - coreapi/presence.c
sal in progress, near to code complete.
[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 "private.h"
22
23
24 extern const char *__policy_enum_to_str(LinphoneSubscribePolicy pol);
25
26
27 void linphone_core_add_subscriber(LinphoneCore *lc, const char *subscriber, SalOp *op){
28         LinphoneFriend *fl=linphone_friend_new_with_addr(subscriber);
29         if (fl==NULL) return ;
30         fl->insub=op;
31         linphone_friend_set_inc_subscribe_policy(fl,LinphoneSPAccept);
32         fl->inc_subscribe_pending=TRUE;
33         lc->subscribers=ms_list_append(lc->subscribers,(void *)fl);
34         if (lc->vtable.new_unknown_subscriber!=NULL) {
35                 char *tmp=linphone_address_as_string(fl->uri);
36                 lc->vtable.new_unknown_subscriber(lc,fl,tmp);
37                 ms_free(tmp);
38         }
39 }
40
41 void linphone_core_reject_subscriber(LinphoneCore *lc, LinphoneFriend *lf){
42         linphone_friend_set_inc_subscribe_policy(lf,LinphoneSPDeny);
43 }
44
45 void linphone_core_notify_all_friends(LinphoneCore *lc, LinphoneOnlineStatus os){
46         MSList *elem;
47         ms_message("Notifying all friends that we are in status %i",os);
48         for(elem=lc->friends;elem!=NULL;elem=elem->next){
49                 LinphoneFriend *lf=(LinphoneFriend *)elem->data;
50                 if (lf->insub){
51                         linphone_friend_notify(lf,os);
52                 }
53         }
54 }
55
56 void linphone_subscription_new(LinphoneCore *lc, SalOp *op, const char *from){
57         LinphoneFriend *lf=NULL;
58         char *tmp;
59         LinphoneAddress *uri;
60         
61         uri=linphone_address_new(from);
62         linphone_address_clean(uri);
63         tmp=linphone_address_as_string(uri);
64         ms_message("Receiving new subscription from %s.",from);
65         /* check if we answer to this subscription */
66         if (linphone_find_friend(lc->friends,uri,&lf)!=NULL){
67                 lf->insub=op;
68                 lf->inc_subscribe_pending=TRUE;
69                 sal_subscribe_accept(op);
70                 linphone_friend_done(lf);       /*this will do all necessary actions */
71         }else{
72                 /* check if this subscriber is in our black list */
73                 if (linphone_find_friend(lc->subscribers,uri,&lf)){
74                         if (lf->pol==LinphoneSPDeny){
75                                 ms_message("Rejecting %s because we already rejected it once.",from);
76                                 sal_subscribe_decline(op);
77                         }
78                         else {
79                                 /* else it is in wait for approval state, because otherwise it is in the friend list.*/
80                                 ms_message("New subscriber found in friend list, in %s state.",__policy_enum_to_str(lf->pol));
81                         }
82                 }else {
83                         sal_subscribe_accept(op);
84                         linphone_core_add_subscriber(lc,tmp,op);
85                 }
86         }
87         ms_free(tmp);
88 }
89
90 void linphone_notify_recv(LinphoneCore *lc, SalOp *op, SalSubscribeState ss, SalPresenceStatus sal_status){
91         const char *status=_("Gone");
92         const char *img="sip-closed.png";
93         char *tmp;
94         LinphoneFriend *lf;
95         LinphoneAddress *friend=NULL;
96         LinphoneOnlineStatus estatus=LINPHONE_STATUS_OFFLINE;
97         
98         switch(sal_status){
99                 case SalPresenceOffline:
100                         status=_("Gone");
101                         img="sip-closed.png";
102                         estatus=LINPHONE_STATUS_OFFLINE;
103                 break;
104                 case SalPresenceOnline:
105                         status=_("Online");
106                         img="sip-online.png";
107                         estatus=LINPHONE_STATUS_ONLINE;
108                 break;
109                 case SalPresenceBusy:
110                         status=_("Busy");
111                         img="sip-busy.png";
112                         estatus=LINPHONE_STATUS_BUSY;
113                 break;
114                 case SalPresenceBerightback:
115                         status=_("Away");
116                         img="sip-away.png";
117                         estatus=LINPHONE_STATUS_AWAY;
118                 break;
119                 case SalPresenceAway:
120                         status=_("Away");
121                         img="sip-away.png";
122                         estatus=LINPHONE_STATUS_AWAY;
123                 break;
124                 case SalPresenceOnthephone:
125                         status=_("On The Phone");
126                         img="sip-otp.png";
127                         estatus=LINPHONE_STATUS_ONTHEPHONE;
128                 break;
129                 case SalPresenceOuttolunch:
130                         status=_("Out To Lunch");
131                         img="sip-otl.png";
132                         estatus=LINPHONE_STATUS_OUTTOLUNCH;
133                 break;
134                 case SalPresenceDonotdisturb:
135                         status=_("Busy");
136                         img="sip-busy.png";
137                         estatus=LINPHONE_STATUS_BUSY;
138                 break;
139                 case SalPresenceMoved:
140                 case SalPresenceAltService:
141                         status=_("Away");
142                         img="sip-away.png";
143                         estatus=LINPHONE_STATUS_AWAY;
144                 break;
145         }
146         lf=linphone_find_friend_by_out_subscribe(lc->friends,op);
147         if (lf!=NULL){
148                 friend=lf->uri;
149                 tmp=linphone_address_as_string(friend);
150                 lf->status=estatus;
151                 lc->vtable.notify_recv(lc,(LinphoneFriend*)lf,tmp,status,img);
152                 ms_free(tmp);
153         }else{
154                 ms_message("But this person is not part of our friend list, so we don't care.");
155         }
156         if (ss==SalSubscribeTerminated){
157                 sal_op_release(op);
158                 if (lf)
159                         lf->outsub=NULL;
160         }
161 }
162
163 void linphone_subscription_closed(LinphoneCore *lc, SalOp *op){
164         LinphoneFriend *lf;
165         lf=linphone_find_friend_by_inc_subscribe(lc->friends,op);
166         sal_op_release(op);
167         if (lf!=NULL){
168                 lf->insub=NULL;
169         }else{
170                 ms_warning("Receiving unsuscribe for unknown in-subscribtion from %s", sal_op_get_from(op));
171         }
172 }