-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcb_config.c
More file actions
315 lines (272 loc) · 6.4 KB
/
cb_config.c
File metadata and controls
315 lines (272 loc) · 6.4 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* This file implements `Config` command. It accepts a command string
* such as `C chrome` and outputs JSON like this:
*
* {"status":"OK", "config": {"CloseEmptyTab":1, ...}}
*
* Typycally, each browser addon calls this program every 1 minute,
* to sync with the latest redirect rules.
*
* NOTE: You need to set up `HKLM\SOFTWARE\RepostConfirmationCanceler\Rulefile` before
* using this program.
*/
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "internal.h"
/*
* First, we define structs to store configuration. The "section" struct
* represents each section block. The "config" struct holds the entire
* INI file.
*/
struct section {
char name[16];
struct strbuf patterns;
struct strbuf excludes;
struct section *next; /* linked list */
};
struct config {
int warning_when_close_dialog;
int close_err_cache_miss_page;
struct section *section;
};
/*
* Allocate a section object. `line` is the first line of the section
* block, containing a browser name in brackets ("[IE]").
*/
static struct section *new_section(char *line)
{
struct section *section;
char *p;
char *q;
section = xmalloc(sizeof(struct section));
memset(section, 0, sizeof(struct section));
p = line + 1;
q = section->name;
while (*p && *p != ']') {
*q++ = tolower(*p++);
}
return section;
}
/*
* The format of RepostConfirmationCanceler config uses an INI-file like syntax.
* You can define variables/URLs in each section like this:
*
* [GLOBAL]
* @WARNING_WHEN_CLOSE_DIALOG
*
* [TARGETS]
* https://example.com*
* -https://test.example.com*
*
* "@" lines are properties. Normal lines are URL patterns.
* "-" lines are exclude patterns.
*/
static void parse_conf(char *data, struct config *conf)
{
char *line;
int global;
struct section *section;
struct section **indirect = &conf->section;
line = strtok(data, "\r\n");
while (line) {
switch (line[0]) {
case ';':
case '#':
break;
case '[':
global = 0;
if (strcmp(line, "[GLOBAL]") == 0) {
global = 1;
}
else {
section = new_section(line);
*indirect = section;
indirect = §ion->next;
}
break;
case '@':
if (global) {
if (strcmp(line, "@WARNING_WHEN_CLOSE_DIALOG") == 0)
{
conf->warning_when_close_dialog = 1;
}
if (strcmp(line, "@CLOSE_ERR_CACHE_MISS_PAGE") == 0)
{
conf->close_err_cache_miss_page = 1;
}
}
break;
case '-':
if (section && strstr(line, "-#") != line) {
strbuf_concat(§ion->excludes, line);
strbuf_putchar(§ion->excludes, '\n');
}
break;
default:
if (section) {
strbuf_concat(§ion->patterns, line);
strbuf_putchar(§ion->patterns, '\n');
}
break;
}
line = strtok(NULL, "\r\n");
}
}
static char *dump_section(struct section *section)
{
struct strbuf sb = {0};
char *ptr;
char *end;
int need_comma;
strbuf_concat(&sb, "{\"Name\":");
strbuf_concat_jsonstr(&sb, section->name, strlen(section->name));
strbuf_putchar(&sb, ',');
/* URLPatterns */
strbuf_concat(&sb, "\"Patterns\":[");
if (section->patterns.buf) {
ptr = section->patterns.buf;
need_comma = 0;
while (*ptr) {
if (need_comma)
strbuf_putchar(&sb, ',');
end = strchr(ptr, '\n');
strbuf_concat_jsonstr(&sb, ptr, end - ptr);
need_comma = 1;
ptr = end + 1;
}
}
strbuf_concat(&sb, "],");
/* URLExcludePatterns */
strbuf_concat(&sb, "\"Excludes\":[");
if (section->excludes.buf) {
ptr = section->excludes.buf;
need_comma = 0;
while (*ptr) {
if (need_comma)
strbuf_putchar(&sb, ',');
end = strchr(ptr, '\n');
strbuf_concat_jsonstr(&sb, ptr + 1, end - ptr - 1);
need_comma = 1;
ptr = end + 1;
}
}
strbuf_concat(&sb, "]}");
return sb.buf;
}
static char *dump_json(struct config *conf)
{
struct strbuf sb = {0};
struct section *section;
char buf[64];
char *json;
int need_comma;
/* WarningWhenCloseDialog */
strbuf_concat(&sb, "{\"WarningWhenCloseDialog\":");
strbuf_concat(&sb, _itoa(conf->warning_when_close_dialog, buf, 10));
strbuf_putchar(&sb, ',');
/* CloseErrCacheMissPage */
strbuf_concat(&sb, "\"CloseErrCacheMissPage\":");
strbuf_concat(&sb, _itoa(conf->close_err_cache_miss_page, buf, 10));
strbuf_putchar(&sb, ',');
/* Sections */
strbuf_concat(&sb, "\"Sections\":[");
section = conf->section;
need_comma = 0;
while (section) {
/* no pattern defined */
if (section->patterns.buf == NULL) {
section = section->next;
continue;
}
if (need_comma)
strbuf_putchar(&sb, ',');
json = dump_section(section);
strbuf_concat(&sb, json);
free(json);
need_comma = 1;
section = section->next;
}
strbuf_concat(&sb, "]}");
return sb.buf;
}
/*
* Utils to locate & read RepostConfirmationCanceler.ini.
*/
static int get_RepostConfirmationCancelerRulefile(char *buf, DWORD size)
{
int ret;
DWORD len = size;
memset(buf, 0, size);
ret = RegGetValueA(HKEY_LOCAL_MACHINE,"SOFTWARE\\RepostConfirmationCanceler","Rulefile",
RRF_RT_REG_SZ, NULL, buf, &size);
if (ret != ERROR_SUCCESS) {
fprintf(stderr, "cannot read %s (%i)","SOFTWARE\\RepostConfirmationCanceler", ret);
return -1;
}
buf[len - 1] = '\0';
return 0;
}
static char *read_file(const char *path)
{
int ret;
FILE *fp;
char *buf;
struct stat st;
fp = fopen(path, "rb");
if (fp == NULL)
return NULL;
ret = fstat(_fileno(fp), &st);
if (ret == -1) {
fclose(fp);
return NULL;
}
if (!(st.st_mode & S_IFREG)) {
fclose(fp);
return NULL;
}
buf = calloc(st.st_size + 1, 1);
if (!buf) {
fclose(fp);
return NULL;
}
ret = fread(buf, st.st_size, 1, fp);
if (ret != 1) {
free(buf);
fclose(fp);
return NULL;
}
fclose(fp);
return buf;
}
int cb_config(char *cmd)
{
char *data;
char *json;
struct config conf = {0};
struct section *section, *next;
char path[MAX_PATH] = {0};
if (get_RepostConfirmationCancelerRulefile(path, MAX_PATH) < 0)
return -1;
data = read_file(path);
if (data == NULL) {
fprintf(stderr, "cannot read %s", path);
return -1;
}
parse_conf(data, &conf);
json = dump_json(&conf);
talk_response("{\"status\":\"OK\",\"config\":%s}", json);
free(data);
free(json);
section = conf.section;
while (section) {
next = section->next;
free(section->patterns.buf);
free(section->excludes.buf);
free(section);
section = next;
}
return 0;
}