buddy_status.c 4.61 KB
Newer Older
jehan's avatar
jehan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

/*
linphone
Copyright (C) 2010  Belledonne Communications SARL 

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.
*/

/**
 * @defgroup buddy_tutorials Basic buddy status notification
 * @ingroup tutorials
jehan's avatar
jehan committed
24 25
 *This program is a _very_ simple usage example of liblinphone,
 *desmonstrating how to initiate  SIP subscriptions and receive notifications from a sip uri identity passed from the command line.
jehan's avatar
jehan committed
26 27
 *<br>Argument must be like sip:jehan@sip.linphone.org .
 *<br>
jehan's avatar
jehan committed
28
 *ex budy_list sip:jehan@sip.linphone.org
jehan's avatar
jehan committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
 *<br>Subscription is cleared on SIGINT
 *<br>
 *@include buddy_status.c

 *
 */

#include "linphonecore.h"

#include <signal.h>

static bool_t running=TRUE;

static void stop(int signum){
	running=FALSE;
}

/**
 * presence state change notification callback
 */
static void notify_presence_recv_updated (struct _LinphoneCore *lc,  LinphoneFriend *friend) {
	const LinphoneAddress* friend_address = linphone_friend_get_address(friend);
	printf("New state state [%s] for user id [%s] \n"
				,linphone_online_status_to_string(linphone_friend_get_status(friend))
				,linphone_address_as_string (friend_address));
}
jehan's avatar
jehan committed
55 56 57 58 59 60 61 62 63 64
static void new_subscription_request (struct _LinphoneCore *lc,  LinphoneFriend *friend, const char* url) {
	const LinphoneAddress* friend_address = linphone_friend_get_address(friend);
	printf(" [%s] wants to see your status, accepting\n"
				,linphone_address_as_string (friend_address));
	linphone_friend_edit(friend); /* start editing friend */
	linphone_friend_set_inc_subscribe_policy(friend,LinphoneSPAccept); /* Accept incoming subscription request for this friend*/
	linphone_friend_done(friend); /*commit change*/
	linphone_core_add_friend(lc,friend); /* add this new friend to the buddy list*/

}
jehan's avatar
jehan committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78

LinphoneCore *lc;
int main(int argc, char *argv[]){
	LinphoneCoreVTable vtable={0};

	char* dest_friend=NULL;


	/* takes   sip uri  identity from the command line arguments */
	if (argc>1){
		dest_friend=argv[1];
	}

	signal(SIGINT,stop);
jehan's avatar
jehan committed
79
//#define DEBUG
jehan's avatar
jehan committed
80 81 82 83 84 85 86 87 88
#ifdef DEBUG
	linphone_core_enable_logs(NULL); /*enable liblinphone logs.*/
#endif
	/* 
	 Fill the LinphoneCoreVTable with application callbacks.
	 All are optional. Here we only use the registration_state_changed callbacks
	 in order to get notifications about the progress of the registration.
	 */
	vtable.notify_presence_recv=notify_presence_recv_updated;
jehan's avatar
jehan committed
89
	vtable.new_subscription_request=new_subscription_request;
jehan's avatar
jehan committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

	/*
	 Instanciate a LinphoneCore object given the LinphoneCoreVTable
	*/
	lc=linphone_core_new(&vtable,NULL,NULL,NULL);
	LinphoneFriend* my_friend=NULL;

	if (dest_friend) {
		my_friend = linphone_friend_new_with_addr(dest_friend); /*creates friend object from dest*/
		if (my_friend == NULL) {
			printf("bad destination uri for friend [%s]\n",dest_friend);
			goto end;
		}

		linphone_friend_enable_subscribes(my_friend,TRUE); /*configure this friend to emit SUBSCRIBE message after being added to LinphoneCore*/
jehan's avatar
jehan committed
105
		linphone_friend_set_inc_subscribe_policy(my_friend,LinphoneSPAccept); /* Accept incoming subscription request for this friend*/
jehan's avatar
jehan committed
106 107 108 109
		linphone_core_add_friend(lc,my_friend); /* add my friend to the buddy list, initiate SUBSCRIBE message*/

	}

jehan's avatar
jehan committed
110 111 112
	linphone_core_set_presence_info(lc,0,NULL,LinphoneStatusOnline); /*set my status to online*/

	/* main loop for receiving notifications and doing background linphone core work: */
jehan's avatar
jehan committed
113
	while(running){
jehan's avatar
jehan committed
114
		linphone_core_iterate(lc); /* first iterate initiates subscription */
jehan's avatar
jehan committed
115 116 117
		ms_usleep(50000);
	}

jehan's avatar
jehan committed
118 119 120
	linphone_core_set_presence_info(lc,0,NULL,LinphoneStatusOffline); /* change my presence status to offline*/
	linphone_core_iterate(lc); /* just to make sure new status is initiate message is issued */

jehan's avatar
jehan committed
121 122 123 124
	linphone_friend_edit(my_friend); /* start editing friend */
	linphone_friend_enable_subscribes(my_friend,FALSE); /*disable subscription for this friend*/
	linphone_friend_done(my_friend); /*commit changes triggering an UNSUBSCRIBE message*/

jehan's avatar
jehan committed
125
	linphone_core_iterate(lc); /* just to make sure unsubscribe message is issued */
jehan's avatar
jehan committed
126 127 128 129 130 131 132 133

end:
	printf("Shutting down...\n");
	linphone_core_destroy(lc);
	printf("Exited\n");
	return 0;
}