aboutsummaryrefslogtreecommitdiff
path: root/src/espik_config.c
blob: 410fcf0cf918c2ea5695ce9f251bcd283af15d38 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#include <string.h>

#include <Ecore_Config.h>

#include "espik_config.h"
#include "espik_error.h"
#include "espik_debug.h"

static int ret;		/* For debug */

t_servinfo*	espik_server_config_set (char* server, unsigned short port)
{
	t_servinfo*	s_info;
	espik_enter();

	s_info = malloc (sizeof (t_servinfo));

	s_info->host = strdup (server);
	s_info->port = port;

	espik_free (server);
	espik_leave();
	return (s_info);
}

t_userinfo*	espik_user_sysinfo_get (void)
{
	struct passwd*	pw;
	t_userinfo*	u_info;
	espik_enter();

	pw = getpwuid (getuid ());

	u_info = malloc (sizeof (t_userinfo));

	//u_info->nickname = pw->pw_name;
	u_info->nickname = strdup ("beber_espik_test");
	u_info->username = strdup (pw->pw_name);
	
	if (! (pw->pw_gecos[0]) )
		u_info->realname = strdup ("espik user");
	else
		u_info->realname = strdup (pw->pw_gecos);

	espik_free (u_info);
	espik_free (pw);

	espik_leave();
	return (u_info);
}

void	espik_config_default_set (void)
{
	t_userinfo*	user_sysinfo;
	espik_enter();

	ecore_config_string_default ("/config/server/0/host", "beber.melee");
	ecore_config_int_default    ("/config/server/0/port", 6667);

	user_sysinfo = espik_user_sysinfo_get ();

	ecore_config_string_default ("/config/user/nickname",
				     user_sysinfo->nickname);
	ecore_config_string_default ("/config/user/username",
			             user_sysinfo->username);
	ecore_config_string_default ("/config/user/realname",
			             user_sysinfo->realname);

	ecore_config_string_default ("/espik/version", ESPIK_VERSION);

	espik_free (user_sysinfo->nickname);
	espik_free (user_sysinfo->username);
	espik_free (user_sysinfo->realname);
	espik_free (user_sysinfo);
	espik_leave();
}

void	espik_config_init (void)
{
	espik_enter();

	if ((ret = ecore_config_init (APPS_NAME)) != ECORE_CONFIG_ERR_SUCC)
	{
		espik_debug_print ("Init ecore_config failed (%d)", ret);
		//kill_me (2);
		//exit (ret);
	}

	if ((ret = ecore_config_load ()) != ECORE_CONFIG_ERR_SUCC)
	{
		/* If an config option is not present,
		 * this will force it to a value */
		espik_debug_print ("Load ecore_config failed (%d)", ret);
		espik_config_default_set ();
		/*
		kill_me (2);
		exit (ret);
		*/
	}
	espik_leave();
}

void espik_config_shutdown (t_info s_info)
{
	espik_free (s_info.server);
	espik_free (s_info.client);
//	free (s_info);

	ecore_config_save();
	ecore_config_shutdown();
}

void	espik_config_get (t_info *s_info)
{
	espik_enter();

	s_info->server = malloc (sizeof (t_servinfo));
	s_info->client = malloc (sizeof (t_userinfo));
	
	s_info->server->host =
		ecore_config_string_get ("/config/server/0/host");
	s_info->server->port =
		(unsigned short)ecore_config_int_get ("/config/server/0/port");

	s_info->client->nickname =
		ecore_config_string_get ("/config/user/nickname");
	s_info->client->username =
		ecore_config_string_get ("/config/user/username");
	s_info->client->realname =
		ecore_config_string_get ("/config/user/realname");

	if (ecore_config_save () != ECORE_CONFIG_ERR_SUCC)
	{
		espik_debug_print ("Save ecore_config failed");
		exit (-1);
	}
	espik_leave();
}

void	espik_config_set (t_info config)
{
	espik_enter();

	ecore_config_int_set	("/config/server/len", 1);
	ecore_config_string_set	("/config/server/0/host", config.server->host);
	ecore_config_int_set	("/config/server/0/port",
			(int)config.server->port);
	ecore_config_int_set	("/config/server/0/autoconnect", 1);

	ecore_config_string_set	("/config/user/nickname",
			config.client->nickname);
	ecore_config_string_set	("/config/user/username",
			config.client->username);
	ecore_config_string_set	("/config/user/realname",
			config.client->realname);

	if (ecore_config_save () != ECORE_CONFIG_ERR_SUCC)
	{
		espik_debug_print ("Save ecore_config failed");
		exit (-1);
	}
	espik_leave();
}