]> sjero.net Git - linphone/blob - coreapi/help/buddy_status.c
Aac-eld add missing header according to RFC3640 3.3.6
[linphone] / coreapi / help / buddy_status.c
1
2 /*
3 buddy_status
4 Copyright (C) 2010  Belledonne Communications SARL 
5
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.
10
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.
15
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.
19 */
20
21 /**
22  * @defgroup buddy_tutorials Basic buddy status notification
23  * @ingroup tutorials
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 .
27  *<br>
28  *ex budy_list sip:jehan@sip.linphone.org
29  *<br>Subscription is cleared on SIGINT
30  *<br>
31  *@include buddy_status.c
32
33  *
34  */
35
36 #ifdef IN_LINPHONE
37 #include "linphonecore.h"
38 #else
39 #include "linphone/linphonecore.h"
40 #endif
41
42 #include <signal.h>
43
44 static bool_t running=TRUE;
45
46 static void stop(int signum){
47         running=FALSE;
48 }
49
50 /**
51  * presence state change notification callback
52  */
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));
58 }
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*/
67
68 }
69 /**
70  * Registration state notification callback
71  */
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));
77 }
78
79 LinphoneCore *lc;
80 int main(int argc, char *argv[]){
81         LinphoneCoreVTable vtable={0};
82
83         char* dest_friend=NULL;
84         char* identity=NULL;
85         char* password=NULL;
86
87         /* takes   sip uri  identity from the command line arguments */
88         if (argc>1){
89                 dest_friend=argv[1];
90         }
91         /* takes   sip uri  identity from the command line arguments */
92         if (argc>2){
93                 identity=argv[2];
94         }
95         /* takes   password from the command line arguments */
96         if (argc>3){
97                 password=argv[3];
98         }
99         signal(SIGINT,stop);
100 //#define DEBUG
101 #ifdef DEBUG
102         linphone_core_enable_logs(NULL); /*enable liblinphone logs.*/
103 #endif
104         /* 
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.
108          */
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*/
112
113         /*
114          Instantiate a LinphoneCore object given the LinphoneCoreVTable
115         */
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();
121                 /*parse identity*/
122                 LinphoneAddress *from = linphone_address_new(identity);
123                 if (from==NULL){
124                         printf("%s not a valid sip uri, must be like sip:toto@sip.linphone.org \n",identity);
125                         goto end;
126                 }
127                 LinphoneAuthInfo *info;
128                 if (password!=NULL){
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*/
131                 }
132
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*/
139
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*/
142
143
144                 /* Loop until registration status is available */
145                 do {
146                         linphone_core_iterate(lc); /* first iterate initiates registration */
147                         ms_usleep(100000);
148                 }
149                 while(  running && linphone_proxy_config_get_state(proxy_cfg) == LinphoneRegistrationProgress);
150
151         }
152         LinphoneFriend* my_friend=NULL;
153
154         if (dest_friend) {
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);
158                         goto end;
159                 }
160
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*/
164
165         }
166
167         linphone_core_set_presence_info(lc,0,NULL,LinphoneStatusOnline); /*set my status to online*/
168
169         /* main loop for receiving notifications and doing background linphone core work: */
170         while(running){
171                 linphone_core_iterate(lc); /* first iterate initiates subscription */
172                 ms_usleep(50000);
173         }
174
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 */
177
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*/
181
182         linphone_core_iterate(lc); /* just to make sure unsubscribe message is issued */
183
184 end:
185         printf("Shutting down...\n");
186         linphone_core_destroy(lc);
187         printf("Exited\n");
188         return 0;
189 }
190