4 Copyright (C) 2010 Belledonne Communications SARL
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * @defgroup buddy_tutorials Basic buddy status notification
24 *This program is a _very_ simple usage example of liblinphone,
25 *demonstrating how to initiate SIP subscriptions and receive notifications from a sip uri identity passed from the command line.
26 *<br>Argument must be like sip:jehan@sip.linphone.org .
28 *ex budy_list sip:jehan@sip.linphone.org
29 *<br>Subscription is cleared on SIGINT
31 *@include buddy_status.c
37 #include "linphonecore.h"
39 #include "linphone/linphonecore.h"
44 static bool_t running=TRUE;
46 static void stop(int signum){
51 * presence state change notification callback
53 static void notify_presence_recv_updated (LinphoneCore *lc, LinphoneFriend *friend) {
54 const LinphoneAddress* friend_address = linphone_friend_get_address(friend);
55 printf("New state state [%s] for user id [%s] \n"
56 ,linphone_online_status_to_string(linphone_friend_get_status(friend))
57 ,linphone_address_as_string (friend_address));
59 static void new_subscription_request (LinphoneCore *lc, LinphoneFriend *friend, const char* url) {
60 const LinphoneAddress* friend_address = linphone_friend_get_address(friend);
61 printf(" [%s] wants to see your status, accepting\n"
62 ,linphone_address_as_string (friend_address));
63 linphone_friend_edit(friend); /* start editing friend */
64 linphone_friend_set_inc_subscribe_policy(friend,LinphoneSPAccept); /* Accept incoming subscription request for this friend*/
65 linphone_friend_done(friend); /*commit change*/
66 linphone_core_add_friend(lc,friend); /* add this new friend to the buddy list*/
70 * Registration state notification callback
72 static void registration_state_changed(struct _LinphoneCore *lc, LinphoneProxyConfig *cfg, LinphoneRegistrationState cstate, const char *message){
73 printf("New registration state %s for user id [%s] at proxy [%s]\n"
74 ,linphone_registration_state_to_string(cstate)
75 ,linphone_proxy_config_get_identity(cfg)
76 ,linphone_proxy_config_get_addr(cfg));
80 int main(int argc, char *argv[]){
81 LinphoneCoreVTable vtable={0};
83 char* dest_friend=NULL;
87 /* takes sip uri identity from the command line arguments */
91 /* takes sip uri identity from the command line arguments */
95 /* takes password from the command line arguments */
102 linphone_core_enable_logs(NULL); /*enable liblinphone logs.*/
105 Fill the LinphoneCoreVTable with application callbacks.
106 All are optional. Here we only use the both notify_presence_recv and new_subscription_request callbacks
107 in order to get notifications about friend status.
109 vtable.notify_presence_recv=notify_presence_recv_updated;
110 vtable.new_subscription_request=new_subscription_request;
111 vtable.registration_state_changed=registration_state_changed; /*just in case sip proxy is used*/
114 Instantiate a LinphoneCore object given the LinphoneCoreVTable
116 lc=linphone_core_new(&vtable,NULL,NULL,NULL);
117 /*sip proxy might be requested*/
118 if (identity != NULL) {
119 /*create proxy config*/
120 LinphoneProxyConfig* proxy_cfg = linphone_proxy_config_new();
122 LinphoneAddress *from = linphone_address_new(identity);
124 printf("%s not a valid sip uri, must be like sip:toto@sip.linphone.org \n",identity);
127 LinphoneAuthInfo *info;
129 info=linphone_auth_info_new(linphone_address_get_username(from),NULL,password,NULL,NULL); /*create authentication structure from identity*/
130 linphone_core_add_auth_info(lc,info); /*add authentication info to LinphoneCore*/
133 // configure proxy entries
134 linphone_proxy_config_set_identity(proxy_cfg,identity); /*set identity with user name and domain*/
135 linphone_proxy_config_set_server_addr(proxy_cfg,linphone_address_get_domain(from)); /* we assume domain = proxy server address*/
136 linphone_proxy_config_enable_register(proxy_cfg,TRUE); /*activate registration for this proxy config*/
137 linphone_proxy_config_enable_publish(proxy_cfg,TRUE); /* enable presence satus publication for this proxy*/
138 linphone_address_destroy(from); /*release resource*/
140 linphone_core_add_proxy_config(lc,proxy_cfg); /*add proxy config to linphone core*/
141 linphone_core_set_default_proxy(lc,proxy_cfg); /*set to default proxy*/
144 /* Loop until registration status is available */
146 linphone_core_iterate(lc); /* first iterate initiates registration */
149 while( running && linphone_proxy_config_get_state(proxy_cfg) == LinphoneRegistrationProgress);
152 LinphoneFriend* my_friend=NULL;
155 my_friend = linphone_friend_new_with_addr(dest_friend); /*creates friend object from dest*/
156 if (my_friend == NULL) {
157 printf("bad destination uri for friend [%s]\n",dest_friend);
161 linphone_friend_enable_subscribes(my_friend,TRUE); /*configure this friend to emit SUBSCRIBE message after being added to LinphoneCore*/
162 linphone_friend_set_inc_subscribe_policy(my_friend,LinphoneSPAccept); /* Accept incoming subscription request for this friend*/
163 linphone_core_add_friend(lc,my_friend); /* add my friend to the buddy list, initiate SUBSCRIBE message*/
167 linphone_core_set_presence_info(lc,0,NULL,LinphoneStatusOnline); /*set my status to online*/
169 /* main loop for receiving notifications and doing background linphone core work: */
171 linphone_core_iterate(lc); /* first iterate initiates subscription */
175 linphone_core_set_presence_info(lc,0,NULL,LinphoneStatusOffline); /* change my presence status to offline*/
176 linphone_core_iterate(lc); /* just to make sure new status is initiate message is issued */
178 linphone_friend_edit(my_friend); /* start editing friend */
179 linphone_friend_enable_subscribes(my_friend,FALSE); /*disable subscription for this friend*/
180 linphone_friend_done(my_friend); /*commit changes triggering an UNSUBSCRIBE message*/
182 linphone_core_iterate(lc); /* just to make sure unsubscribe message is issued */
185 printf("Shutting down...\n");
186 linphone_core_destroy(lc);