summaryrefslogtreecommitdiff
path: root/parseconfig.c
blob: 0bfda05c2468049cd6363d75f8f2f30d200aa868 (plain)
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
/*
 * This file is part of GetHT
 *
 * See COPYING file for copyright, license and warranty details.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "getht.h"

#include <curl/curl.h>

int loadconfig(char * getht_path, struct config * options)
/*	Loads variables from config file to extern and passed
 *	variables. */
{
	FILE * config_file;
	char filepath[STR_MAX];

	snprintf(filepath, STR_MAX, "%s/config.ini", getht_path);

	if((config_file = fopen(filepath,"r")) == NULL)
	{
		fprintf(stderr,"Cannot open file %s for reading.\n",filepath);
		return 1;
	}

	char parameter[80], option[80];
	while(!feof(config_file))
	{
		fscanf(config_file, "%s = %s", option, parameter);

		if(option[0] == '#');	/* ignore lines beginning with a hash */
		else if(!strcmp(option, "savepath"))
			strncpy(options->save_path, parameter, STR_MAX);
		else if(!strcmp(option, "startup_check"))
			options->startup_check = atoi(parameter);
		else if(!strcmp(option, "toc_uri"))
			strncpy(options->issue_uri, parameter, STR_MAX);
		else if(!strcmp(option, "proxy_type"))
		{
			if(!strcmp(parameter, "http"))
				options->proxy.type = CURLPROXY_HTTP;
			else if(!strcmp(parameter, "socks4"))
				options->proxy.type = CURLPROXY_SOCKS4;
			else if(!strcmp(parameter, "socks5"))
				options->proxy.type = CURLPROXY_SOCKS5;
			else
				fprintf(stderr,
					"Proxy type %s not known, please use either http, socks4 or socks5\n",
					parameter);
		}
		else if(!strcmp(option, "proxy_address"))
			strncpy(options->proxy.address, parameter, STR_MAX);
		else if(!strcmp(option, "proxy_port"))
			options->proxy.port = (long) atoi(parameter);
		else if(!strcmp(option, "proxy_auth"))
		{
			if(!strcmp(parameter, "basic"))
				options->proxy.auth = CURLAUTH_BASIC;
			else if(!strcmp(parameter, "digest"))
				options->proxy.auth = CURLAUTH_DIGEST;
			else if(!strcmp(parameter, "ntlm"))
				options->proxy.auth = CURLAUTH_NTLM;
			else
				fprintf(stderr,
					"config.ini: Proxy authentication method %s not known, please use basic, digest or ntlm",
					parameter);
		}
		else if(!strcmp(option, "proxy_user"))
			strncpy(options->proxy.user, parameter, STR_MAX);
		else if(!strcmp(option, "proxy_pass"))
			strncpy(options->proxy.pass, parameter, STR_MAX);
		else
			fprintf(stderr, "config.ini: Option '%s' not recognised, ignoring\n", option);
	}

	fclose(config_file);

	return 0;
}

int writefreshconfig(char * getht_path, struct config * options)
/*	Write a new config file according to extern and passed variables. */
{
	FILE * config_file;
	char filepath[STR_MAX];

	snprintf(filepath, STR_MAX, "%s/config.ini", getht_path);

	if((config_file = fopen(filepath,"w")) == NULL)
	{
		fprintf(stderr,"Cannot open file %s for writing.\n",filepath);
		return 1;
	}
	else
		fprintf(stdout,"Writing a fresh config file to %s.\n",filepath);

	if(options->save_path[0])
		fprintf(config_file, "%s = %s\n", "savepath", options->save_path);
	if(options->startup_check)
		fprintf(config_file, "%s = %i\n", "startup_check", options->startup_check);
	if(options->issue_uri[0])
		fprintf(config_file, "%s = %s\n", "toc_uri", options->issue_uri);
	if(options->proxy.type)
	{
		if(options->proxy.type == CURLPROXY_HTTP)
			fprintf(config_file, "%s = %s\n", "proxy_type", "http");
		else if(options->proxy.type == CURLPROXY_SOCKS4)
			fprintf(config_file, "%s = %s\n", "proxy_type", "socks4");
		else if(options->proxy.type == CURLPROXY_SOCKS5)
			fprintf(config_file, "%s = %s\n", "proxy_type", "socks5");
	}
	if(options->proxy.address[0])
		fprintf(config_file, "%s = %s\n", "proxy_address", options->proxy.address);
	if(options->proxy.port)
		fprintf(config_file, "%s = %li\n", "proxy_port", options->proxy.port);
	if(options->proxy.auth)
	{
		if(options->proxy.auth == CURLAUTH_BASIC)
			fprintf(config_file, "%s = %s\n", "proxy_auth", "basic");
		else if(options->proxy.auth == CURLAUTH_DIGEST)
			fprintf(config_file, "%s = %s\n", "proxy_auth", "digest");
		else if(options->proxy.auth == CURLAUTH_NTLM)
			fprintf(config_file, "%s = %s\n", "proxy_auth", "ntlm");
	}
	if(options->proxy.user[0])
		fprintf(config_file, "%s = %s\n", "proxy_user", options->proxy.user);
	if(options->proxy.pass[0])
		fprintf(config_file, "%s = %s\n", "proxy_pass", options->proxy.pass);
		
	fclose(config_file);

	return 0;
}

int updateconfig(char * getht_path, struct config * options)
/*	Read existing config file, and rewrite any variables which differ
 *	in memory. */
{
	FILE * config_file;
	char filepath[STR_MAX];

	snprintf(filepath, STR_MAX, "%s/config.ini", getht_path);

	if((config_file = fopen(filepath,"rw")) == NULL)
	{
		fprintf(stderr,"Cannot open file %s for reading/writing.\n",filepath);
		return 1;
	}

	/* Not yet implemented */

	fclose(config_file);

	return 1;
}