-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.php
More file actions
147 lines (137 loc) · 3.8 KB
/
convert.php
File metadata and controls
147 lines (137 loc) · 3.8 KB
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
<?php
// ZNC to JSON Config Conversion by Thomas Edwards
// Use commandline -o to overwrite regardless.
// Created around ZNC Config v1.0
// The Config we're reading, This can simply be a path.
$config = "znc.conf";
// Where are we writing this to. Something like /var/www/myznc/config.json would be fine.
$dest = "zncconfig.json";
// You dont need to bother with anything below this line.
$force = false;
if ($argc >= 2) {
if ($argv[1] == "-o") {
$force = true;
}
}
echo "ZNC to JSON Parser by Thomas Edwards\n";
// Check if either files exist.
if (!file_exists($config)) {
die("The ZNC Config at '{$config}' could not be found.\n");
}
if (file_exists($dest) && !$force) {
die("The JSON File {$dest} already exists.\n");
}
// Read the config into this variable and explode it into an array.
$src_data = explode("\n",file_get_contents($config));
// Setup the primary Structure.
$json = array();
$json['Listeners'] = array();
$json['Users'] = array();
$json['Modules'] = array();
// Setup some parsing variables.
$in_user = false;
$in_listener = false;
$in_network = false;
$in_pass = false;
$in_chan = false;
// Some Template arrays.
$user_temp = array();
$user_temp['Networks'] = array();
$user_temp['Modules'] = array();
$user_temp['Channels'] = array();
// Setup some temp arrays.
$listener = array();
$user = $user_temp;
$network = array();
$chan = array();
$password = array();
// Cycle each line of the configuration.
foreach ($src_data as $line) {
// Explode the line by its spaces and trim extra characters and whitespace.
$data = explode(chr(32),trim($line));
if ($data[0] == "<Listener") {
$in_listener = true;
$listener['Name'] = substr($data[1],0,-1);
}
else if ($data[0] == "</Listener>") {
$json['Listeners'][] = $listener;
$listener = array();
$in_listener = false;
}
else if ($data[0] == "<User") {
$in_user = true; // Nothing kinkeh here :o!
$user['Username'] = substr($data[1],0,-1);
}
else if ($data[0] == "</User>") {
$in_user = false;
$json['Users'][] = $user;
$user = $user_temp;
}
else if ($data[0] == "<Network") {
if ($in_user) {
$in_network = true;
$network['Name'] = substr($data[1],0,-1);
}
}
else if ($data[0] == "</Network>") {
if ($in_user) {
$user['Networks'][] = $network;
$in_network = false;
$network = array();
}
}
else if ($data[0] == "<Chan") {
if ($in_user) {
$in_chan = true;
$chan['Name'] = substr($data[1],0,-1);
}
}
else if ($data[0] == "</Chan>") {
if ($in_user) {
$in_chan = false;
$user['Channels'][] = $chan;
$chan = array();
}
}
else if ($data[0] == "<Pass") {
if ($in_user) {
$in_pass = true;
}
}
else if ($data[0] == "</Pass>") {
if ($in_user) {
$in_pass = false;
$user['Password'] = $password;
$password = array();
}
}
else if ($data[0] != "//" && $data[0] != "") {
// It's not a comment its a variable being set.
if ($in_user && !$in_chan && !$in_network && !$in_pass) {
if ($data[0] != "LoadModule") {
$user[$data[0]] = implode(chr(32),array_slice($data,2));
}
else {
$user['Modules'][] = $user[$data[0]] = implode(chr(32),array_slice($data,2));
}
}
else if ($in_network) {
$network[$data[0]] = implode(chr(32),array_slice($data,2));
}
else if ($in_chan) {
$channel[$data[0]] = implode(chr(32),array_slice($data,2));
}
else if ($in_pass) {
$password[$data[0]] = implode(chr(32),array_slice($data,2));
}
else if ($in_listener) {
$listener[$data[0]] = implode(chr(32),array_slice($data,2));
}
else if ($data[0] == "LoadModule" && !$in_user) {
$json['Modules'][] = implode(chr(32),array_slice($data,2));
}
}
}
file_put_contents($dest,json_encode($json));
echo "Config parsed. ".count($src_data)." lines were parsed into JSON and wrote to '{$dest}'\n";
?>