summaryrefslogtreecommitdiffstats
path: root/lurker/common/ConfigFile.h
blob: 81c3beaa5646079c27bc63c063404df57ff7166b (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*  $Id: ConfigFile.h 1649 2009-10-19 14:35:01Z terpstra $
 *  
 *  ConfigFile.h - Knows how to load the config file
 *  
 *  Copyright (C) 2002 - Wesley W. Terpstra
 *  
 *  License: GPL
 *  
 *  Authors: 'Wesley W. Terpstra' <wesley@terpstra.ca>
 *  
 *    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; version 2.1.
 *    
 *    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
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef CONFIG_H
#define CONFIG_H

#include <string>
#include <map>
#include <set>
#include <iostream>
#include <vector>

#if __GNUC__ == 2
#include <strstream>
#else
#include <sstream>
#endif

// get VERSION
#include "../config.h"

using std::string;
using std::map;
using std::set;
using std::ostream;
using std::vector;

// simplify a path by removing trailing '/'s and duplicate '/'s
string simplifyPath(string& path);

// localized string
class lstring
{
 protected:
 	map<string, string> s;
 	static map<string, string>* c; // conversion from one spelling to common
 	
 	static void prep_c();
 	
 public:
 	lstring(const string& fb);
 	lstring() { }
 	
 	// If the locale is funky, returns false
 	bool translate(const string& locale, const string& translation);
 	
 	string localize(const string& locale) const;
 	string operator () (const string& locale) const
 	{ return localize(locale); }
 	
 	bool is_set() const;
 	
 	// normalize the language to a common spelling - false if broken
 	static bool lang_normalize(string& lang);
 	static bool locale_normalize(string& locale);
};

struct List
{
	// filenames and addresses are not localized, nor are identifiers
	string mbox;
	string address;
	string group;
	set<string> languages;
	
	bool offline;
	bool allowed; // Set to true if the config file enabled this list
	
	// localize these
	lstring title;		// could make sense in some cases 
	lstring description;	// clearly should be translated
	lstring link;		// different language on different pages
	
 	struct SerializeMagic
 	{
 		const List& m;
 		string l;
 		SerializeMagic(const List& m_, const string& l_)
 		: m(m_), l(l_) { }
 	};
 	SerializeMagic operator () (const string& locale) const
 	{ return SerializeMagic(*this, locale); }
};

struct Frontend
{
 	enum Perm { ALLOW, DENY };
 	enum What { GROUP, LIST };
 	
 	struct Entry
	{
		Perm perm;
		What what;
		string key;
 	};
 	
 	vector<Entry>	entries;
 	
 	lstring		archive;
 	lstring		admin_name;
 	string		admin_address;
 	bool		hide_email;
 	bool		raw_email;
 	bool		web_cache;
};

class Config
{
 private:
 	List* list;
 	Frontend* frontend;
 	
 	string group;
#if __GNUC__ == 2
	strstream error;
#else
	std::stringstream error;
#endif
 	
 public:
 	typedef map<string, List> Lists;
 	typedef set<string>       Members;
 	
 	struct GroupData
 	{
 		lstring heading;
 		Members members;
 	};
 	
 	typedef map<string, GroupData> Groups;
 	typedef map<string, Frontend> Frontends;
 	
 	Lists lists;
 	Groups groups;
 	Frontends frontends;
        
        string file;  // the name of this config file
        
 	// never localize paths, commands, or addresses
 	string	dbdir;
 	int	db_umask;
 	string	xslt;
 	string  delete_message;
 	string	pgpv_mime;
 	string	pgpv_inline;
 	string	admin_address;
 	
 	// some names are spelt differently by locales
 	
 	lstring	archive;
 	lstring	admin_name;
 	
 	bool	web_cache;
 	bool	hide_email;
 	bool	raw_email;
 	time_t	modified; // the timestamp of modification for the config file
 	
 	// parameters specific for rendering
 	string	docUrl;
 	string	cgiUrl;
 	string	command;
 	mutable string options;
 	
 	// get the error string
 	string getError()
 	{
#if __GNUC__ == 2
		string out(error.str(), error.rdbuf()->pcount());
		return out;
#else
		return error.str();
#endif
 	}
 	
 	Config();
 	
 	// Open the config from this file.
 	int load(const string& file, bool toplevel = true);
 	int process_command(const string& file, int c, const string& key, const string& val, const string& dir);
 	
 	// Set the allowed flag on lists
 	void set_permissions(const Frontend& f);
 	
 	struct SerializeMagic
 	{
 		const Config& c;
 		string l;
 		SerializeMagic(const Config& c_, const string& l_)
 		: c(c_), l(l_) { }
 	};
 	SerializeMagic operator () (const string& locale) const
 	{ return SerializeMagic(*this, locale); }
};

ostream& operator << (ostream& o, const List::SerializeMagic& lm);
ostream& operator << (ostream& o, const Config::SerializeMagic& cm);

#endif