sipsetup.c 6.52 KB
Newer Older
smorlat's avatar
smorlat committed
1 2
/*
linphone
3
Copyright (C) 2009  Simon MORLAT (simon.morlat@linphone.org)
smorlat's avatar
smorlat committed
4 5 6 7 8 9 10 11 12 13 14 15 16

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

20
#ifdef HAVE_CONFIG_H
21
#include "config.h"
22
#endif
smorlat's avatar
smorlat committed
23

24
#include "linphone/core.h"
smorlat's avatar
smorlat committed
25

26
extern SipSetup linphone_sip_login;
smorlat's avatar
smorlat committed
27
static SipSetup *all_sip_setups[]={
28
	&linphone_sip_login,
smorlat's avatar
smorlat committed
29 30 31
	NULL
};

32
static bctbx_list_t *registered_sip_setups=NULL;
smorlat's avatar
smorlat committed
33 34

void sip_setup_register(SipSetup *ss){
35
	registered_sip_setups=bctbx_list_append(registered_sip_setups,ss);
smorlat's avatar
smorlat committed
36
}
smorlat's avatar
smorlat committed
37

38
void sip_setup_register_all(MSFactory *factory){
smorlat's avatar
smorlat committed
39
	SipSetup **p=all_sip_setups;
40 41
	ms_factory_load_plugins(factory, LINPHONE_PLUGINS_DIR);
	//ms_load_plugins(LINPHONE_PLUGINS_DIR);
smorlat's avatar
smorlat committed
42
	while(*p!=NULL){
smorlat's avatar
smorlat committed
43
		sip_setup_register(*p);
44
		p++;
smorlat's avatar
smorlat committed
45 46 47
	}
}

48
const bctbx_list_t * linphone_core_get_sip_setups(LinphoneCore *lc){
smorlat's avatar
smorlat committed
49 50 51
	return registered_sip_setups;
}

smorlat's avatar
smorlat committed
52
SipSetup *sip_setup_lookup(const char *type_name){
53
	bctbx_list_t *elem;
smorlat's avatar
smorlat committed
54 55
	for(elem=registered_sip_setups;elem!=NULL;elem=elem->next){
		SipSetup *ss=(SipSetup*)elem->data;
smorlat's avatar
smorlat committed
56
		if ( strcasecmp(ss->name,type_name)==0){
smorlat's avatar
smorlat committed
57
			if (!ss->initialized){
smorlat's avatar
smorlat committed
58
				if (ss->init!=NULL) ss->init();
smorlat's avatar
smorlat committed
59 60 61
				ss->initialized=TRUE;
				if (ss->capabilities==0){
					ms_error("%s SipSetup isn't capable of anything ?",ss->name);
smorlat's avatar
smorlat committed
62
				}
smorlat's avatar
smorlat committed
63
			}
smorlat's avatar
smorlat committed
64
			return ss;
smorlat's avatar
smorlat committed
65 66 67 68 69 70
		}
	}
	ms_warning("no %s setup manager declared.",type_name);
	return NULL;
}

smorlat's avatar
smorlat committed
71
void sip_setup_unregister_all(void){
72
	bctbx_list_t *elem;
smorlat's avatar
smorlat committed
73 74 75
	for(elem=registered_sip_setups;elem!=NULL;elem=elem->next){
		SipSetup *ss=(SipSetup*)elem->data;
		if (ss->initialized){
smorlat's avatar
smorlat committed
76
			if (ss->exit) ss->exit();
smorlat's avatar
smorlat committed
77
			ss->initialized=FALSE;
smorlat's avatar
smorlat committed
78 79
		}
	}
80
	registered_sip_setups = bctbx_list_free(registered_sip_setups);
smorlat's avatar
smorlat committed
81 82
}

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
void buddy_lookup_request_set_key(BuddyLookupRequest *req, const char *key){
	if (req->key!=NULL) {
		ms_free(req->key);
		req->key=NULL;
	}
	if (key) req->key=ms_strdup(key);
}

void buddy_lookup_request_set_max_results(BuddyLookupRequest *req, int ncount){
	req->max_results=ncount;
}

void buddy_lookup_request_free(BuddyLookupRequest *req){
	if (req->key) ms_free(req->key);
	if (req->results){
98 99
		bctbx_list_for_each(req->results,(void (*)(void*))buddy_info_free);
		bctbx_list_free(req->results);
100 101 102 103
	}
	ms_free(req);
}

smorlat's avatar
smorlat committed
104 105 106
LinphoneProxyConfig *sip_setup_context_get_proxy_config(const SipSetupContext *ctx){
	return ctx->cfg;
}
smorlat's avatar
smorlat committed
107

smorlat's avatar
smorlat committed
108
SipSetupContext *sip_setup_context_new(SipSetup *s, struct _LinphoneProxyConfig *cfg){
smorlat's avatar
smorlat committed
109
	SipSetupContext *obj=(SipSetupContext*)ms_new0(SipSetupContext,1);
smorlat's avatar
smorlat committed
110 111
	obj->funcs=s;
	obj->data=NULL;
smorlat's avatar
smorlat committed
112
	obj->cfg=cfg;
smorlat's avatar
smorlat committed
113 114 115
	if (obj->funcs->init_instance){
		obj->funcs->init_instance(obj);
	}
smorlat's avatar
smorlat committed
116 117 118
	return obj;
}

smorlat's avatar
smorlat committed
119 120 121 122
unsigned int sip_setup_get_capabilities(SipSetup *s){
	return s->capabilities;
}

smorlat's avatar
smorlat committed
123
int sip_setup_context_get_capabilities(SipSetupContext *ctx){
Benjamin REIS's avatar
Benjamin REIS committed
124
	return (int)ctx->funcs->capabilities;
smorlat's avatar
smorlat committed
125 126
}

127
LinphoneStatus sip_setup_context_create_account(SipSetupContext * ctx, const char *uri, const char *passwd, const char *email, int suscribe){
128
	if (ctx->funcs->create_account)
Sylvain Berfini's avatar
Sylvain Berfini committed
129
		return ctx->funcs->create_account(ctx, uri, passwd, email, suscribe);
smorlat's avatar
smorlat committed
130 131 132
	else return -1;
}

133 134 135 136 137 138
int sip_setup_context_account_exists(SipSetupContext *ctx, const char *uri){
	if (ctx->funcs->account_exists)
		return ctx->funcs->account_exists(ctx,uri);
	return -1;
}

Sylvain Berfini's avatar
Sylvain Berfini committed
139 140 141 142 143 144
int sip_setup_context_account_validated(SipSetupContext *ctx, const char *uri){
	if (ctx->funcs->account_validated)
		return ctx->funcs->account_validated(ctx,uri);
	return -1;
}

145
LinphoneStatus sip_setup_context_login_account(SipSetupContext * ctx, const char *uri, const char *passwd, const char *userid){
146
	LinphoneAddress *from=linphone_address_new(uri);
147 148 149 150
	if (from==NULL) {
		ms_warning("Fail to parse %s",uri);
		return -1;
	}
151
	strncpy(ctx->domain,linphone_address_get_domain(from),sizeof(ctx->domain));
152
	ctx->domain[sizeof(ctx->domain) - 1] = '\0';
153
	strncpy(ctx->username,linphone_address_get_username(from),sizeof(ctx->username));
154
	ctx->username[sizeof(ctx->username) - 1] = '\0';
Simon Morlat's avatar
Simon Morlat committed
155
	linphone_address_unref(from);
smorlat's avatar
smorlat committed
156
	if (ctx->funcs->login_account)
157
		return ctx->funcs->login_account(ctx,uri,passwd,userid);
smorlat's avatar
smorlat committed
158 159 160
	return -1;
}

161
LinphoneStatus sip_setup_context_get_proxy(SipSetupContext *ctx, const char *domain, char *proxy, size_t sz){
smorlat's avatar
smorlat committed
162
	if (ctx->funcs->get_proxy)
smorlat's avatar
smorlat committed
163
		return ctx->funcs->get_proxy(ctx,domain ? domain : ctx->domain,proxy,sz);
smorlat's avatar
smorlat committed
164 165 166
	return -1;
}

167
LinphoneStatus sip_setup_context_get_stun_servers(SipSetupContext *ctx, char *stun1, char *stun2, size_t size){
smorlat's avatar
smorlat committed
168 169 170 171 172
	if (ctx->funcs->get_stun_servers)
		return ctx->funcs->get_stun_servers(ctx,stun1,stun2,size);
	return -1;
}

173
LinphoneStatus sip_setup_context_get_relay(SipSetupContext *ctx,char *relay, size_t size){
smorlat's avatar
smorlat committed
174 175 176 177 178
	if (ctx->funcs->get_relay)
		return ctx->funcs->get_relay(ctx,relay,size);
	return -1;
}

179 180 181 182
BuddyLookupRequest *sip_setup_context_create_buddy_lookup_request(SipSetupContext *ctx){
	if (ctx->funcs->buddy_lookup_funcs)
		return ctx->funcs->buddy_lookup_funcs->request_create(ctx);
	return NULL;
smorlat's avatar
smorlat committed
183 184
}

185
LinphoneStatus sip_setup_context_buddy_lookup_submit(SipSetupContext *ctx , BuddyLookupRequest *req){
186 187 188
	if (ctx->funcs->buddy_lookup_funcs)
		return ctx->funcs->buddy_lookup_funcs->request_submit(ctx,req);
	return -1;
smorlat's avatar
smorlat committed
189 190
}

191
LinphoneStatus sip_setup_context_buddy_lookup_free(SipSetupContext *ctx , BuddyLookupRequest *req){
192 193
	if (ctx->funcs->buddy_lookup_funcs)
		return ctx->funcs->buddy_lookup_funcs->request_free(ctx,req);
smorlat's avatar
smorlat committed
194 195 196
	return -1;
}

smorlat's avatar
smorlat committed
197 198 199 200 201 202 203 204 205 206 207 208 209
const char * sip_setup_context_get_notice(SipSetupContext *ctx){
	if (ctx->funcs->get_notice)
		return ctx->funcs->get_notice(ctx);
	return NULL;
}

const char ** sip_setup_context_get_domains(SipSetupContext *ctx){
	if (ctx->funcs->get_domains)
		return ctx->funcs->get_domains(ctx);
	return NULL;
}


210
LinphoneStatus sip_setup_context_logout(SipSetupContext *ctx){
smorlat's avatar
smorlat committed
211 212 213 214 215 216
	if (ctx->funcs->logout_account){
		return ctx->funcs->logout_account(ctx);
	}
	return -1;
}

smorlat's avatar
smorlat committed
217
void sip_setup_context_free(SipSetupContext *ctx){
smorlat's avatar
smorlat committed
218 219 220
	if (ctx->funcs->uninit_instance){
		ctx->funcs->uninit_instance(ctx);
	}
smorlat's avatar
smorlat committed
221 222
	ms_free(ctx);
}
223 224 225 226 227 228 229 230 231 232 233


BuddyInfo *buddy_info_new(){
	return ms_new0(BuddyInfo,1);
}

void buddy_info_free(BuddyInfo *info){
	if (info->image_data!=NULL)
		ms_free(info->image_data);
	ms_free(info);
}