presence.c 4.52 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 34
	linphone_friend_set_inc_subscribe_policy(fl,LinphoneSPAccept);
	fl->inc_subscribe_pending=TRUE;
	lc->subscribers=ms_list_append(lc->subscribers,(void *)fl);
	if (lc->vtable.new_unknown_subscriber!=NULL) {
35 36 37
		char *tmp=linphone_address_as_string(fl->uri);
		lc->vtable.new_unknown_subscriber(lc,fl,tmp);
		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 61 62 63 64
	
	uri=linphone_address_new(from);
	linphone_address_clean(uri);
	tmp=linphone_address_as_string(uri);
	ms_message("Receiving new subscription from %s.",from);
aymeric's avatar
aymeric committed
65
	/* check if we answer to this subscription */
66
	if (linphone_find_friend(lc->friends,uri,&lf)!=NULL){
67 68 69
		lf->insub=op;
		lf->inc_subscribe_pending=TRUE;
		sal_subscribe_accept(op);
aymeric's avatar
aymeric committed
70 71 72
		linphone_friend_done(lf);	/*this will do all necessary actions */
	}else{
		/* check if this subscriber is in our black list */
73
		if (linphone_find_friend(lc->subscribers,uri,&lf)){
aymeric's avatar
aymeric committed
74 75
			if (lf->pol==LinphoneSPDeny){
				ms_message("Rejecting %s because we already rejected it once.",from);
76
				sal_subscribe_decline(op);
aymeric's avatar
aymeric committed
77 78 79 80 81 82
			}
			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 {
83 84
			sal_subscribe_accept(op);
			linphone_core_add_subscriber(lc,tmp,op);
aymeric's avatar
aymeric committed
85 86
		}
	}
87
	ms_free(tmp);
aymeric's avatar
aymeric committed
88 89
}

90
void linphone_notify_recv(LinphoneCore *lc, SalOp *op, SalSubscribeState ss, SalPresenceStatus sal_status){
aymeric's avatar
aymeric committed
91 92
	char *tmp;
	LinphoneFriend *lf;
93
	LinphoneAddress *friend=NULL;
94 95 96 97 98 99 100
	LinphoneOnlineStatus estatus=LINPHONE_STATUS_OFFLINE;
	
	switch(sal_status){
		case SalPresenceOffline:
			estatus=LINPHONE_STATUS_OFFLINE;
		break;
		case SalPresenceOnline:
aymeric's avatar
aymeric committed
101
			estatus=LINPHONE_STATUS_ONLINE;
102 103
		break;
		case SalPresenceBusy:
aymeric's avatar
aymeric committed
104
			estatus=LINPHONE_STATUS_BUSY;
105 106
		break;
		case SalPresenceBerightback:
aymeric's avatar
aymeric committed
107
			estatus=LINPHONE_STATUS_AWAY;
108 109 110 111 112
		break;
		case SalPresenceAway:
			estatus=LINPHONE_STATUS_AWAY;
		break;
		case SalPresenceOnthephone:
aymeric's avatar
aymeric committed
113
			estatus=LINPHONE_STATUS_ONTHEPHONE;
114 115
		break;
		case SalPresenceOuttolunch:
aymeric's avatar
aymeric committed
116
			estatus=LINPHONE_STATUS_OUTTOLUNCH;
117 118 119 120 121 122 123 124
		break;
		case SalPresenceDonotdisturb:
			estatus=LINPHONE_STATUS_BUSY;
		break;
		case SalPresenceMoved:
		case SalPresenceAltService:
			estatus=LINPHONE_STATUS_AWAY;
		break;
aymeric's avatar
aymeric committed
125
	}
126
	lf=linphone_find_friend_by_out_subscribe(lc->friends,op);
aymeric's avatar
aymeric committed
127
	if (lf!=NULL){
128
		friend=lf->uri;
129
		tmp=linphone_address_as_string(friend);
aymeric's avatar
aymeric committed
130
		lf->status=estatus;
131
		lc->vtable.notify_recv(lc,(LinphoneFriend*)lf);
aymeric's avatar
aymeric committed
132 133 134 135
		ms_free(tmp);
	}else{
		ms_message("But this person is not part of our friend list, so we don't care.");
	}
136 137 138 139
	if (ss==SalSubscribeTerminated){
		sal_op_release(op);
		if (lf)
			lf->outsub=NULL;
aymeric's avatar
aymeric committed
140 141
	}
}
142 143

void linphone_subscription_closed(LinphoneCore *lc, SalOp *op){
aymeric's avatar
aymeric committed
144
	LinphoneFriend *lf;
145 146
	lf=linphone_find_friend_by_inc_subscribe(lc->friends,op);
	sal_op_release(op);
aymeric's avatar
aymeric committed
147
	if (lf!=NULL){
148
		lf->insub=NULL;
aymeric's avatar
aymeric committed
149
	}else{
150
		ms_warning("Receiving unsuscribe for unknown in-subscribtion from %s", sal_op_get_from(op));
aymeric's avatar
aymeric committed
151 152
	}
}