buddy_status.c 7.34 KB
Newer Older
jehan's avatar
jehan committed
1 2

/*
jehan's avatar
jehan committed
3
buddy_status
4
Copyright (C) 2010  Belledonne Communications SARL
jehan's avatar
jehan committed
5 6 7 8 9 10 11 12 13 14 15 16 17

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
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
jehan's avatar
jehan committed
19 20 21 22 23
*/

/**
 * @defgroup buddy_tutorials Basic buddy status notification
 * @ingroup tutorials
jehan's avatar
jehan committed
24
 *This program is a _very_ simple usage example of liblinphone,
jehan's avatar
jehan committed
25
 *demonstrating 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>
28
 *ex budy_list sip:jehan@sip.linphone.org
jehan's avatar
jehan committed
29 30 31 32 33 34 35
 *<br>Subscription is cleared on SIGINT
 *<br>
 *@include buddy_status.c

 *
 */

36
#include "linphone/core.h"
jehan's avatar
jehan committed
37 38 39 40 41 42 43 44 45 46 47 48

#include <signal.h>

static bool_t running=TRUE;

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

/**
 * presence state change notification callback
 */
49
static void notify_presence_recv_updated (LinphoneCore *lc,  LinphoneFriend *friend) {
50
	const LinphoneAddress* friend_address = linphone_friend_get_address(friend);
51 52 53 54
	if (friend_address != NULL) {
		const LinphonePresenceModel* model = linphone_friend_get_presence_model(friend);
		LinphonePresenceActivity *activity = linphone_presence_model_get_activity(model);
		char *activity_str = linphone_presence_activity_to_string(activity);
55
		char *str = linphone_address_as_string (friend_address);
56 57
		printf("New state state [%s] for user id [%s] \n"
					,activity_str
58 59
					,str);
		ms_free(str);
60
	}
jehan's avatar
jehan committed
61
}
62
static void new_subscription_requested (LinphoneCore *lc,  LinphoneFriend *friend, const char* url) {
63 64
	const LinphoneAddress* friend_address = linphone_friend_get_address(friend);

65
	if (friend_address != NULL) {
66 67 68
		char *str = linphone_address_as_string (friend_address);
		printf(" [%s] wants to see your status, accepting\n", str);
		ms_free(str);
69
	}
70 71 72 73 74 75
	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*/

}
76 77 78 79
/**
 * Registration state notification callback
 */
static void registration_state_changed(struct _LinphoneCore *lc, LinphoneProxyConfig *cfg, LinphoneRegistrationState cstate, const char *message){
80
		printf("New registration state %s for user id [%s] at proxy [%s]"
81 82 83 84
				,linphone_registration_state_to_string(cstate)
				,linphone_proxy_config_get_identity(cfg)
				,linphone_proxy_config_get_addr(cfg));
}
jehan's avatar
jehan committed
85 86 87 88 89 90

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

	char* dest_friend=NULL;
91 92
	char* identity=NULL;
	char* password=NULL;
jehan's avatar
jehan committed
93

94 95
	LinphoneFriend* my_friend=NULL;

jehan's avatar
jehan committed
96 97 98 99
	/* takes   sip uri  identity from the command line arguments */
	if (argc>1){
		dest_friend=argv[1];
	}
100 101 102 103 104 105 106 107
	/* takes   sip uri  identity from the command line arguments */
	if (argc>2){
		identity=argv[2];
	}
	/* takes   password from the command line arguments */
	if (argc>3){
		password=argv[3];
	}
jehan's avatar
jehan committed
108
	signal(SIGINT,stop);
jehan's avatar
jehan committed
109
//#define DEBUG
jehan's avatar
jehan committed
110 111 112
#ifdef DEBUG
	linphone_core_enable_logs(NULL); /*enable liblinphone logs.*/
#endif
113
	/*
jehan's avatar
jehan committed
114
	 Fill the LinphoneCoreVTable with application callbacks.
115
	 All are optional. Here we only use the both notify_presence_received and new_subscription_requested callbacks
jehan's avatar
jehan committed
116
	 in order to get notifications about friend status.
jehan's avatar
jehan committed
117
	 */
118 119
	vtable.notify_presence_received=notify_presence_recv_updated;
	vtable.new_subscription_requested=new_subscription_requested;
120
	vtable.registration_state_changed=registration_state_changed; /*just in case sip proxy is used*/
jehan's avatar
jehan committed
121 122

	/*
jehan's avatar
jehan committed
123
	 Instantiate a LinphoneCore object given the LinphoneCoreVTable
jehan's avatar
jehan committed
124 125
	*/
	lc=linphone_core_new(&vtable,NULL,NULL,NULL);
126 127 128 129 130 131
	/*sip proxy might be requested*/
	if (identity != NULL)  {
		/*create proxy config*/
		LinphoneProxyConfig* proxy_cfg = linphone_proxy_config_new();
		/*parse identity*/
		LinphoneAddress *from = linphone_address_new(identity);
132
		LinphoneAuthInfo *info;
133 134 135 136 137
		if (from==NULL){
			printf("%s not a valid sip uri, must be like sip:toto@sip.linphone.org \n",identity);
			goto end;
		}
		if (password!=NULL){
138
			info=linphone_auth_info_new(linphone_address_get_username(from),NULL,password,NULL,NULL,NULL); /*create authentication structure from identity*/
139 140 141 142 143 144 145 146
			linphone_core_add_auth_info(lc,info); /*add authentication info to LinphoneCore*/
		}

		// configure proxy entries
		linphone_proxy_config_set_identity(proxy_cfg,identity); /*set identity with user name and domain*/
		linphone_proxy_config_set_server_addr(proxy_cfg,linphone_address_get_domain(from)); /* we assume domain = proxy server address*/
		linphone_proxy_config_enable_register(proxy_cfg,TRUE); /*activate registration for this proxy config*/
		linphone_proxy_config_enable_publish(proxy_cfg,TRUE); /* enable presence satus publication for this proxy*/
Simon Morlat's avatar
Simon Morlat committed
147
		linphone_address_unref(from); /*release resource*/
148 149 150 151 152 153 154 155 156 157 158 159 160

		linphone_core_add_proxy_config(lc,proxy_cfg); /*add proxy config to linphone core*/
		linphone_core_set_default_proxy(lc,proxy_cfg); /*set to default proxy*/


		/* Loop until registration status is available */
		do {
			linphone_core_iterate(lc); /* first iterate initiates registration */
			ms_usleep(100000);
		}
		while(	running && linphone_proxy_config_get_state(proxy_cfg) == LinphoneRegistrationProgress);

	}
jehan's avatar
jehan committed
161 162

	if (dest_friend) {
163
		my_friend = linphone_core_create_friend_with_address(lc, dest_friend); /*creates friend object from dest*/
jehan's avatar
jehan committed
164 165 166 167 168 169
		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*/
170
		linphone_friend_set_inc_subscribe_policy(my_friend,LinphoneSPAccept); /* Accept incoming subscription request for this friend*/
jehan's avatar
jehan committed
171 172 173 174
		linphone_core_add_friend(lc,my_friend); /* add my friend to the buddy list, initiate SUBSCRIBE message*/

	}

175
	/*set my status to online*/
176
	linphone_core_set_presence_model(lc, linphone_presence_model_new_with_activity(LinphonePresenceActivityOnline, NULL));
177 178

	/* main loop for receiving notifications and doing background linphone core work: */
jehan's avatar
jehan committed
179
	while(running){
180
		linphone_core_iterate(lc); /* first iterate initiates subscription */
jehan's avatar
jehan committed
181 182 183
		ms_usleep(50000);
	}

184
	/* change my presence status to offline*/
185
	linphone_core_set_presence_model(lc, linphone_presence_model_new_with_activity(LinphonePresenceActivityOffline, NULL));
186 187
	linphone_core_iterate(lc); /* just to make sure new status is initiate message is issued */

jehan's avatar
jehan committed
188 189 190 191
	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*/

192
	linphone_core_iterate(lc); /* just to make sure unsubscribe message is issued */
jehan's avatar
jehan committed
193 194 195 196 197 198 199 200

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