1 module dcc.engine.conf;
2 
3 private import dcc.engine.tribune;
4 private import ini.dini;
5 
6 private import std.file;
7 private import std.conv;
8 private import std.algorithm : map;
9 
10 private import std.array;
11 private import std.string;
12 
13 class Config {
14 	Tribune[] tribunes;
15 
16 	string default_ua = "DCoinCoin/%v";
17 	int default_refresh = 30;
18 	string[] default_ignorelist;
19 
20 	string default_color = "white";
21 
22 	this(string file) {
23 		if (!exists(file)) {
24 			std.file.write(file, []);
25 		}
26 
27 		auto ini = Ini.Parse(file);
28 
29 		foreach (IniSection section ; ini.sections) {
30 			if (section.name == "global") {
31 				this.default_ua = section.getKey("ua");
32 				this.default_refresh = to!int(section.getKey("refresh"));
33 				this.default_ignorelist = section.getKey("ignore").split(",");
34 				foreach (int i, string s; this.default_ignorelist) { this.default_ignorelist[i] = s.strip(); }
35 			} else {
36 				tribunes ~= this.tribune_from_section(section);
37 			}
38 		}
39 	}
40 
41 	Tribune tribune_from_section(IniSection section) {
42 		string name = section.getKey("name");
43 		string[] aliases = section.getKey("aliases").split(",");
44 		// Why can't I just use map! here? I don't understand.
45 		foreach (int i, string a; aliases) { aliases[i] = a.strip(); }
46 		string post_url = section.getKey("post_url");
47 		string post_format = section.getKey("post_format");
48 		string xml_url = section.getKey("xml_url");
49 		string cookie = section.getKey("cookie");
50 		string ua = section.getKey("ua");
51 		if (!ua.length) {
52 			ua = this.default_ua;
53 		}
54 		int refresh;
55 		string refresh_string = section.getKey("refresh");
56 		try {
57 			refresh = parse!int(refresh_string);
58 		} catch (Exception e) {
59 			refresh = this.default_refresh;
60 		}
61 		bool tags_encoded;
62 		string tags_encoded_string = section.getKey("tags_encoded");
63 		try {
64 			tags_encoded = parse!bool(tags_encoded_string);
65 		} catch (Exception e) {
66 			tags_encoded = false;
67 		}
68 		string color = section.getKey("color");
69 		if (!color.length) {
70 			color = this.default_color;
71 		}
72 		string login = section.getKey("login");
73 		Tribune tribune = new Tribune(name, aliases, post_url, post_format, xml_url, cookie, ua, refresh, tags_encoded, color, login);
74 
75 		return tribune;
76 	}
77 }