]> sjero.net Git - linphone/blob - coreapi/siplogin.c
Merge branch 'master' of belledonne-communications.com:linphone-private into dev_sal
[linphone] / coreapi / siplogin.c
1 /*
2 linphone
3 Copyright (C) 2009  Simon MORLAT (simon.morlat@linphone.org)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "../config.h"
22 #endif
23
24 #include "linphonecore.h"
25 #include "private.h"
26 #include <ctype.h>
27
28 static void sip_login_init_instance(SipSetupContext *ctx){
29         LinphoneProxyConfig *cfg=sip_setup_context_get_proxy_config(ctx);
30         /*disable registration until the user logs in*/
31         linphone_proxy_config_enable_register(cfg,FALSE);
32 }
33
34 static void guess_display_name(LinphoneAddress *from){
35         char *dn=(char*)ms_malloc(strlen(linphone_address_get_username(from))+3);
36         const char *it;
37         char *wptr=dn;
38         bool_t begin=TRUE;
39         bool_t surname=0;
40         for(it=linphone_address_get_username(from);*it!='\0';++it){
41                 if (begin){
42                         *wptr=toupper(*it);
43                         begin=FALSE;
44                 }else if (*it=='.'){
45                         if (surname) break;
46                         *wptr=' ';
47                         begin=TRUE;
48                         surname=TRUE;
49                 }else *wptr=*it;
50                 wptr++;
51         }
52         linphone_address_set_display_name(from,dn);
53         ms_free(dn);
54 }
55
56 static int sip_login_do_login(SipSetupContext * ctx, const char *uri, const char *passwd){
57         LinphoneProxyConfig *cfg=sip_setup_context_get_proxy_config(ctx);
58         LinphoneCore *lc=linphone_proxy_config_get_core(cfg);
59         LinphoneAuthInfo *auth;
60         LinphoneAddress *parsed_uri;
61         char *tmp;
62
63         parsed_uri=linphone_address_new(uri);
64         if (parsed_uri==NULL){
65                 return -1;
66         }
67         if (linphone_address_get_display_name(parsed_uri)!=NULL){
68                 guess_display_name(parsed_uri);
69         }
70         tmp=linphone_address_as_string(parsed_uri);
71         linphone_proxy_config_set_identity(cfg,tmp);
72         if (passwd ) {
73                 auth=linphone_auth_info_new(linphone_address_get_username(parsed_uri),NULL,passwd,NULL,NULL);
74                 linphone_core_add_auth_info(lc,auth);
75         }
76         linphone_proxy_config_enable_register(cfg,TRUE);
77         linphone_proxy_config_done(cfg);
78         ms_free(tmp);
79         linphone_address_destroy(parsed_uri);
80         ms_message("SipLogin: done");
81         return 0;
82 }
83
84 static int sip_login_do_logout(SipSetupContext * ctx){
85         LinphoneProxyConfig *cfg=sip_setup_context_get_proxy_config(ctx);
86         linphone_proxy_config_enable_register(cfg,FALSE);
87         linphone_proxy_config_done(cfg);
88         return 0;
89 }
90
91 /* a simple SipSetup built-in plugin to allow specify the user/password for proxy config at runtime*/
92
93 #ifndef _MSC_VER
94
95 SipSetup linphone_sip_login={
96         .name="SipLogin",
97         .capabilities=SIP_SETUP_CAP_LOGIN,
98         .init_instance=sip_login_init_instance,
99         .login_account=sip_login_do_login,
100         .logout_account=sip_login_do_logout
101 };
102
103 #else
104 SipSetup linphone_sip_login={
105         "SipLogin",
106         SIP_SETUP_CAP_LOGIN,
107         0,
108         NULL,
109         NULL,
110         sip_login_init_instance,
111         NULL,
112         NULL,
113         NULL,
114         sip_login_do_login,
115         NULL,
116         NULL,
117         NULL,
118         NULL,
119         NULL,
120         NULL,
121         NULL,
122         NULL,
123         sip_login_do_logout
124 };
125
126
127
128 #endif