presence.c 4.97 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
linphone
Copyright (C) 2000  Simon MORLAT (simon.morlat@linphone.org)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "linphonecore.h"
#include "private.h"


extern const char *__policy_enum_to_str(LinphoneSubscribePolicy pol);


27
void linphone_core_add_subscriber(LinphoneCore *lc, const char *subscriber, SalOp *op){
aymeric's avatar
aymeric committed
28 29
	LinphoneFriend *fl=linphone_friend_new_with_addr(subscriber);
	if (fl==NULL) return ;
30
	fl->insub=op;
aymeric's avatar
aymeric committed
31 32 33
	linphone_friend_set_inc_subscribe_policy(fl,LinphoneSPAccept);
	fl->inc_subscribe_pending=TRUE;
	lc->subscribers=ms_list_append(lc->subscribers,(void *)fl);
jehan's avatar
jehan committed
34
	if (lc->vtable.new_subscription_request!=NULL) {
35
		char *tmp=linphone_address_as_string(fl->uri);
jehan's avatar
jehan committed
36
		lc->vtable.new_subscription_request(lc,fl,tmp);
37
		ms_free(tmp);
aymeric's avatar
aymeric committed
38 39 40 41 42 43 44 45
	}
}

void linphone_core_reject_subscriber(LinphoneCore *lc, LinphoneFriend *lf){
	linphone_friend_set_inc_subscribe_policy(lf,LinphoneSPDeny);
}

void linphone_core_notify_all_friends(LinphoneCore *lc, LinphoneOnlineStatus os){
46
	MSList *elem;
aymeric's avatar
aymeric committed
47
	ms_message("Notifying all friends that we are in status %i",os);
48 49 50 51 52 53
	for(elem=lc->friends;elem!=NULL;elem=elem->next){
		LinphoneFriend *lf=(LinphoneFriend *)elem->data;
		if (lf->insub){
			linphone_friend_notify(lf,os);
		}
	}
aymeric's avatar
aymeric committed
54 55
}

56
void linphone_subscription_new(LinphoneCore *lc, SalOp *op, const char *from){
aymeric's avatar
aymeric committed
57 58
	LinphoneFriend *lf=NULL;
	char *tmp;
59
	LinphoneAddress *uri;
60
	LinphoneProxyConfig *cfg;
61 62 63 64 65
	
	uri=linphone_address_new(from);
	linphone_address_clean(uri);
	tmp=linphone_address_as_string(uri);
	ms_message("Receiving new subscription from %s.",from);
66 67 68 69

	cfg=linphone_core_lookup_known_proxy(lc,uri);
	if (cfg!=NULL){
		if (cfg->op){
70 71 72
			if (sal_op_get_contact(cfg->op)) {
				sal_op_set_contact (op,sal_op_get_contact(cfg->op));
				ms_message("Contact for next subscribe answer has been fixed using proxy "/*to %s",fixed_contact*/);
73 74 75
			}
		}
	}
aymeric's avatar
aymeric committed
76
	/* check if we answer to this subscription */
77
	if (linphone_find_friend(lc->friends,uri,&lf)!=NULL){
78 79 80
		lf->insub=op;
		lf->inc_subscribe_pending=TRUE;
		sal_subscribe_accept(op);
aymeric's avatar
aymeric committed
81 82 83
		linphone_friend_done(lf);	/*this will do all necessary actions */
	}else{
		/* check if this subscriber is in our black list */
84
		if (linphone_find_friend(lc->subscribers,uri,&lf)){
aymeric's avatar
aymeric committed
85 86
			if (lf->pol==LinphoneSPDeny){
				ms_message("Rejecting %s because we already rejected it once.",from);
87
				sal_subscribe_decline(op);
aymeric's avatar
aymeric committed
88 89 90 91 92 93
			}
			else {
				/* else it is in wait for approval state, because otherwise it is in the friend list.*/
				ms_message("New subscriber found in friend list, in %s state.",__policy_enum_to_str(lf->pol));
			}
		}else {
94 95
			sal_subscribe_accept(op);
			linphone_core_add_subscriber(lc,tmp,op);
aymeric's avatar
aymeric committed
96 97
		}
	}
Simon Morlat's avatar
Simon Morlat committed
98
	linphone_address_destroy(uri);
99
	ms_free(tmp);
aymeric's avatar
aymeric committed
100 101
}

102
void linphone_notify_recv(LinphoneCore *lc, SalOp *op, SalSubscribeStatus ss, SalPresenceStatus sal_status){
aymeric's avatar
aymeric committed
103 104
	char *tmp;
	LinphoneFriend *lf;
105
	LinphoneAddress *friend=NULL;
106
	LinphoneOnlineStatus estatus=LinphoneStatusOffline;
107 108 109
	
	switch(sal_status){
		case SalPresenceOffline:
110
			estatus=LinphoneStatusOffline;
111 112
		break;
		case SalPresenceOnline:
113
			estatus=LinphoneStatusOnline;
114 115
		break;
		case SalPresenceBusy:
116
			estatus=LinphoneStatusBusy;
117 118
		break;
		case SalPresenceBerightback:
119
			estatus=LinphoneStatusBeRightBack;
120 121
		break;
		case SalPresenceAway:
122
			estatus=LinphoneStatusAway;
123 124
		break;
		case SalPresenceOnthephone:
125
			estatus=LinphoneStatusOnThePhone;
126 127
		break;
		case SalPresenceOuttolunch:
128
			estatus=LinphoneStatusOutToLunch;
129 130
		break;
		case SalPresenceDonotdisturb:
131
			estatus=LinphoneStatusDoNotDisturb;
132 133 134
		break;
		case SalPresenceMoved:
		case SalPresenceAltService:
135
			estatus=LinphoneStatusMoved;
136
		break;
aymeric's avatar
aymeric committed
137
	}
138
	lf=linphone_find_friend_by_out_subscribe(lc->friends,op);
aymeric's avatar
aymeric committed
139
	if (lf!=NULL){
140
		friend=lf->uri;
141
		tmp=linphone_address_as_string(friend);
aymeric's avatar
aymeric committed
142
		lf->status=estatus;
143
		lf->subscribe_active=TRUE;
Simon Morlat's avatar
Simon Morlat committed
144 145
		if (lc->vtable.notify_presence_recv)
			lc->vtable.notify_presence_recv(lc,(LinphoneFriend*)lf);
aymeric's avatar
aymeric committed
146 147 148 149
		ms_free(tmp);
	}else{
		ms_message("But this person is not part of our friend list, so we don't care.");
	}
150 151
	if (ss==SalSubscribeTerminated){
		sal_op_release(op);
152
		if (lf){
153
			lf->outsub=NULL;
154 155
			lf->subscribe_active=FALSE;
		}
aymeric's avatar
aymeric committed
156 157
	}
}
158 159

void linphone_subscription_closed(LinphoneCore *lc, SalOp *op){
aymeric's avatar
aymeric committed
160
	LinphoneFriend *lf;
161 162
	lf=linphone_find_friend_by_inc_subscribe(lc->friends,op);
	sal_op_release(op);
aymeric's avatar
aymeric committed
163
	if (lf!=NULL){
164
		lf->insub=NULL;
aymeric's avatar
aymeric committed
165
	}else{
166
		ms_warning("Receiving unsuscribe for unknown in-subscribtion from %s", sal_op_get_from(op));
aymeric's avatar
aymeric committed
167 168
	}
}