]> sjero.net Git - linphone/blob - coreapi/sipsetup.c
d6b459c5fa9d10e4e4acb70a70926ccc51cb2a6c
[linphone] / coreapi / sipsetup.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
26 extern SipSetup linphone_sip_login;
27 #if defined(ANDROID) || defined(IOS)
28 static SipSetup *all_sip_setups[]={
29         &linphone_sip_login,
30         NULL
31 };
32 #else
33 extern SipSetup linphone_sip_wizard;
34 static SipSetup *all_sip_setups[]={
35         &linphone_sip_login,
36         &linphone_sip_wizard,
37         NULL
38 };
39 #endif
40
41 static MSList *registered_sip_setups=NULL;
42
43 void sip_setup_register(SipSetup *ss){
44         registered_sip_setups=ms_list_append(registered_sip_setups,ss);
45 }
46
47 void sip_setup_register_all(void){
48         SipSetup **p=all_sip_setups;
49         ms_load_plugins(LINPHONE_PLUGINS_DIR);
50         while(*p!=NULL){
51                 sip_setup_register(*p);
52                 p++;
53         }
54 }
55
56 const MSList * linphone_core_get_sip_setups(LinphoneCore *lc){
57         return registered_sip_setups;
58 }
59
60 SipSetup *sip_setup_lookup(const char *type_name){
61         MSList *elem;
62         for(elem=registered_sip_setups;elem!=NULL;elem=elem->next){
63                 SipSetup *ss=(SipSetup*)elem->data;
64                 if ( strcasecmp(ss->name,type_name)==0){
65                         if (!ss->initialized){
66                                 if (ss->init!=NULL) ss->init();
67                                 ss->initialized=TRUE;
68                                 if (ss->capabilities==0){
69                                         ms_error("%s SipSetup isn't capable of anything ?",ss->name);
70                                 }
71                         }
72                         return ss;
73                 }
74         }
75         ms_warning("no %s setup manager declared.",type_name);
76         return NULL;
77 }
78
79 void sip_setup_unregister_all(void){
80         MSList *elem;
81         for(elem=registered_sip_setups;elem!=NULL;elem=elem->next){
82                 SipSetup *ss=(SipSetup*)elem->data;
83                 if (ss->initialized){
84                         if (ss->exit) ss->exit();
85                         ss->initialized=FALSE;
86                 }
87         }
88 }
89
90 void buddy_lookup_request_set_key(BuddyLookupRequest *req, const char *key){
91         if (req->key!=NULL) {
92                 ms_free(req->key);
93                 req->key=NULL;
94         }
95         if (key) req->key=ms_strdup(key);
96 }
97
98 void buddy_lookup_request_set_max_results(BuddyLookupRequest *req, int ncount){
99         req->max_results=ncount;
100 }
101
102 void buddy_lookup_request_free(BuddyLookupRequest *req){
103         if (req->key) ms_free(req->key);
104         if (req->results){
105                 ms_list_for_each(req->results,(void (*)(void*))buddy_info_free);
106                 ms_list_free(req->results);
107         }
108         ms_free(req);
109 }
110
111 LinphoneProxyConfig *sip_setup_context_get_proxy_config(const SipSetupContext *ctx){
112         return ctx->cfg;
113 }
114
115 SipSetupContext *sip_setup_context_new(SipSetup *s, struct _LinphoneProxyConfig *cfg){
116         SipSetupContext *obj=(SipSetupContext*)ms_new0(SipSetupContext,1);
117         obj->funcs=s;
118         obj->data=NULL;
119         obj->cfg=cfg;
120         if (obj->funcs->init_instance){
121                 obj->funcs->init_instance(obj);
122         }
123         return obj;
124 }
125
126 unsigned int sip_setup_get_capabilities(SipSetup *s){
127         return s->capabilities;
128 }
129
130 int sip_setup_context_get_capabilities(SipSetupContext *ctx){
131         return ctx->funcs->capabilities;
132 }
133
134 int sip_setup_context_create_account(SipSetupContext * ctx, const char *uri, const char *passwd, const char *email, int suscribe){
135         if (ctx->funcs->create_account)
136                 return ctx->funcs->create_account(ctx, uri, passwd, email, suscribe);
137         else return -1;
138 }
139
140 int sip_setup_context_account_exists(SipSetupContext *ctx, const char *uri){
141         if (ctx->funcs->account_exists)
142                 return ctx->funcs->account_exists(ctx,uri);
143         return -1;
144 }
145
146 int sip_setup_context_account_validated(SipSetupContext *ctx, const char *uri){
147         if (ctx->funcs->account_validated)
148                 return ctx->funcs->account_validated(ctx,uri);
149         return -1;
150 }
151
152 int sip_setup_context_login_account(SipSetupContext * ctx, const char *uri, const char *passwd){
153         LinphoneAddress *from=linphone_address_new(uri);
154         if (from==NULL) {
155                 ms_warning("Fail to parse %s",uri);
156                 return -1;
157         }
158         strncpy(ctx->domain,linphone_address_get_domain(from),sizeof(ctx->domain));
159         strncpy(ctx->username,linphone_address_get_username(from),sizeof(ctx->username));
160         linphone_address_destroy(from);
161         if (ctx->funcs->login_account)
162                 return ctx->funcs->login_account(ctx,uri,passwd);
163         return -1;
164 }
165
166 int sip_setup_context_get_proxy(SipSetupContext *ctx, const char *domain, char *proxy, size_t sz){
167         if (ctx->funcs->get_proxy)
168                 return ctx->funcs->get_proxy(ctx,domain ? domain : ctx->domain,proxy,sz);
169         return -1;
170 }
171
172 int sip_setup_context_get_stun_servers(SipSetupContext *ctx, char *stun1, char *stun2, size_t size){
173         if (ctx->funcs->get_stun_servers)
174                 return ctx->funcs->get_stun_servers(ctx,stun1,stun2,size);
175         return -1;
176 }
177
178 int sip_setup_context_get_relay(SipSetupContext *ctx,char *relay, size_t size){
179         if (ctx->funcs->get_relay)
180                 return ctx->funcs->get_relay(ctx,relay,size);
181         return -1;
182 }
183
184 BuddyLookupRequest *sip_setup_context_create_buddy_lookup_request(SipSetupContext *ctx){
185         if (ctx->funcs->buddy_lookup_funcs)
186                 return ctx->funcs->buddy_lookup_funcs->request_create(ctx);
187         return NULL;
188 }
189
190 int sip_setup_context_buddy_lookup_submit(SipSetupContext *ctx , BuddyLookupRequest *req){
191         if (ctx->funcs->buddy_lookup_funcs)
192                 return ctx->funcs->buddy_lookup_funcs->request_submit(ctx,req);
193         return -1;
194 }
195
196 int sip_setup_context_buddy_lookup_free(SipSetupContext *ctx , BuddyLookupRequest *req){
197         if (ctx->funcs->buddy_lookup_funcs)
198                 return ctx->funcs->buddy_lookup_funcs->request_free(ctx,req);
199         return -1;
200 }
201
202 const char * sip_setup_context_get_notice(SipSetupContext *ctx){
203         if (ctx->funcs->get_notice)
204                 return ctx->funcs->get_notice(ctx);
205         return NULL;
206 }
207
208 const char ** sip_setup_context_get_domains(SipSetupContext *ctx){
209         if (ctx->funcs->get_domains)
210                 return ctx->funcs->get_domains(ctx);
211         return NULL;
212 }
213
214
215 int sip_setup_context_logout(SipSetupContext *ctx){
216         if (ctx->funcs->logout_account){
217                 return ctx->funcs->logout_account(ctx);
218         }
219         return -1;
220 }
221
222 void sip_setup_context_free(SipSetupContext *ctx){
223         if (ctx->funcs->uninit_instance){
224                 ctx->funcs->uninit_instance(ctx);
225         }
226         ms_free(ctx);
227 }
228
229
230 BuddyInfo *buddy_info_new(){
231         return ms_new0(BuddyInfo,1);
232 }
233
234 void buddy_info_free(BuddyInfo *info){
235         if (info->image_data!=NULL)
236                 ms_free(info->image_data);
237         ms_free(info);
238 }