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
|
/* Copyright (C) 2020 C. McEnroe <june@causal.agency>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <err.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
#include <time.h>
#include "server.h"
const char *htmlStylesheet = "stylesheet.css";
enum kcgi_err htmlHead(struct khtmlreq *html, const char *title) {
return khtml_elem(html, KELEM_DOCTYPE)
|| khtml_attr(html, KELEM_META, KATTR_CHARSET, "utf-8", KATTR__MAX)
|| khtml_elem(html, KELEM_TITLE)
|| khtml_puts(html, title)
|| khtml_closeelem(html, 1)
|| khtml_attr(
html, KELEM_META,
KATTR_NAME, "viewport",
KATTR_CONTENT, "width=device-width, initial-scale=1.0",
KATTR__MAX
)
|| khtml_attr(
html, KELEM_LINK,
KATTR_REL, "stylesheet",
KATTR_HREF, htmlStylesheet,
KATTR__MAX
)
|| khtml_elem(html, KELEM_H1)
|| khtml_puts(html, title)
|| khtml_closeelem(html, 1);
}
enum kcgi_err
htmlNav(struct khtmlreq *html, const char *network, const char *context) {
enum kcgi_err error = 0
|| khtml_elem(html, KELEM_NAV)
|| khtml_elem(html, KELEM_OL)
|| khtml_elem(html, KELEM_LI)
|| khtml_attr(html, KELEM_A, KATTR_HREF, Pages[Networks], KATTR__MAX)
|| khtml_puts(html, "Networks")
|| khtml_closeelem(html, 2);
if (error) return error;
if (network) {
char *href = khttp_urlpart(
NULL, NULL, Pages[Contexts],
Keys[Network].name, network,
NULL
);
if (!href) err(EX_OSERR, "khttp_urlpart");
error = 0
|| khtml_elem(html, KELEM_LI)
|| khtml_attr(html, KELEM_A, KATTR_HREF, href, KATTR__MAX)
|| khtml_puts(html, network)
|| khtml_closeelem(html, 2);
if (error) return error;
}
if (network && context) {
char *href = khttp_urlpart(
NULL, NULL, Pages[Events],
Keys[Network].name, network,
Keys[Context].name, context,
NULL
);
if (!href) err(EX_OSERR, "khttp_urlpart");
error = 0
|| khtml_elem(html, KELEM_LI)
|| khtml_attr(html, KELEM_A, KATTR_HREF, href, KATTR__MAX)
|| khtml_puts(html, context)
|| khtml_closeelem(html, 2);
if (error) return error;
}
char label[256];
snprintf(
label, sizeof(label), "Search%s%s",
(network ? " " : ""), (context ? context : network ? network : "")
);
error = 0
|| khtml_closeelem(html, 1)
|| khtml_attr(
html, KELEM_FORM,
KATTR_METHOD, "get",
KATTR_ACTION, Pages[Search],
KATTR__MAX
)
|| khtml_attr(
html, KELEM_INPUT,
KATTR_TYPE, "search",
KATTR_NAME, Keys[Query].name,
KATTR__MAX
)
|| khtml_attr(
html, KELEM_INPUT,
KATTR_TYPE, "submit",
KATTR_VALUE, label,
KATTR__MAX
)
|| khtml_closeelem(html, 1);
if (error) return error;
if (network) {
error = khtml_attr(
html, KELEM_INPUT,
KATTR_TYPE, "hidden",
KATTR_NAME, Keys[Network].name,
KATTR_VALUE, network,
KATTR__MAX
);
if (error) return error;
}
if (context) {
error = khtml_attr(
html, KELEM_INPUT,
KATTR_TYPE, "hidden",
KATTR_NAME, Keys[Context].name,
KATTR_VALUE, context,
KATTR__MAX
);
if (error) return error;
}
return khtml_closeelem(html, 2);
}
static const char *SourceURL = "https://git.causal.agency/scooper";
static const char *SyntaxURL = {
"https://www.sqlite.org/fts5.html#full_text_query_syntax"
};
static const char *Columns = {
"network, channel, query, nick, user, target, message"
};
enum kcgi_err (struct khtmlreq *html) {
return 0
|| khtml_closeto(html, 0)
|| khtml_elem(html, KELEM_FOOTER)
|| khtml_elem(html, KELEM_SPAN)
|| khtml_attr(html, KELEM_A, KATTR_HREF, SourceURL, KATTR__MAX)
|| khtml_puts(html, "scooper is AGPLv3+")
|| khtml_closeelem(html, 2)
|| khtml_putc(html, ' ')
|| khtml_elem(html, KELEM_SPAN)
|| khtml_attr(html, KELEM_A, KATTR_HREF, SyntaxURL, KATTR__MAX)
|| khtml_puts(html, "Search syntax")
|| khtml_closeelem(html, 1)
|| khtml_putc(html, ' ')
|| khtml_attr(html, KELEM_SPAN, KATTR_TITLE, Columns, KATTR__MAX)
|| khtml_puts(html, "Columns")
|| khtml_closeto(html, 0);
}
static enum kcgi_err eventTime(struct khtmlreq *html, struct Event event) {
char time[sizeof("0000-00-00 00:00:00")];
strftime(time, sizeof(time), "%F %T", gmtime(&event.time));
char *base = NULL;
if (event.network && event.context) {
base = khttp_urlpart(
NULL, NULL, Pages[Events],
Keys[Network].name, event.network,
Keys[Context].name, event.context,
Keys[After].name, time,
NULL
);
if (!base) err(EX_OSERR, "khttp_urlpart");
}
char *href = NULL;
asprintf(&href, "%s#%" PRId64, (base ? base : ""), event.event);
if (!href) err(EX_OSERR, "asprintf");
if (base) free(base);
enum kcgi_err error = 0
|| khtml_attr(html, KELEM_TD, KATTR_CLASS, "time", KATTR__MAX)
|| khtml_attr(html, KELEM_A, KATTR_HREF, href, KATTR__MAX)
|| khtml_attr(html, KELEM_TIME, KATTR_DATETIME, time, KATTR__MAX)
|| khtml_puts(html, time)
|| khtml_closeelem(html, 3);
free(href);
return error;
}
static enum kcgi_err eventNetwork(struct khtmlreq *html, struct Event event) {
if (!event.network) return KCGI_OK;
char *href = khttp_urlpart(
NULL, NULL, Pages[Contexts],
Keys[Network].name, event.network,
NULL
);
if (!href) err(EX_OSERR, "khttp_urlpart");
enum kcgi_err error = 0
|| khtml_attr(html, KELEM_TD, KATTR_CLASS, "network", KATTR__MAX)
|| khtml_attr(html, KELEM_A, KATTR_HREF, href, KATTR__MAX)
|| khtml_puts(html, event.network)
|| khtml_closeelem(html, 2);
free(href);
return error;
}
static enum kcgi_err eventContext(struct khtmlreq *html, struct Event event) {
if (!event.network || !event.context) return KCGI_OK;
char *href = khttp_urlpart(
NULL, NULL, Pages[Events],
Keys[Network].name, event.network,
Keys[Context].name, event.context,
NULL
);
if (!href) err(EX_OSERR, "khttp_urlpart");
enum kcgi_err error = 0
|| khtml_attr(html, KELEM_TD, KATTR_CLASS, "context", KATTR__MAX)
|| khtml_attr(html, KELEM_A, KATTR_HREF, href, KATTR__MAX)
|| khtml_puts(html, event.context)
|| khtml_closeelem(html, 2);
free(href);
return error;
}
static int hash(const char *str) {
if (*str == '~') str++;
uint32_t hash = 0;
for (; *str; ++str) {
hash = (hash << 5) | (hash >> 27);
hash ^= *str;
hash *= 0x27220A95;
}
return 2 + hash % 74;
}
static enum kcgi_err eventNick(struct khtmlreq *html, struct Event event) {
char color[sizeof("fg99")];
snprintf(color, sizeof(color), "fg%02d", hash(event.user));
char *mask = NULL;
asprintf(&mask, "%s!%s@%s", event.nick, event.user, event.host);
if (!mask) err(EX_OSERR, "asprintf");
enum kcgi_err error = 0
|| khtml_attr(html, KELEM_TD, KATTR_CLASS, "nick", KATTR__MAX)
|| khtml_attr(
html, KELEM_SPAN,
KATTR_CLASS, color,
KATTR_TITLE, mask,
KATTR__MAX
)
|| khtml_puts(html, event.nick)
|| khtml_closeelem(html, 2);
free(mask);
return error;
}
static const char *Types[TypesLen] = {
#define X(id, name) [id] = name,
ENUM_TYPE
#undef X
};
enum kcgi_err htmlEvent(struct khtmlreq *html, struct Event event) {
const char *type = (event.type < TypesLen ? Types[event.type] : "unknown");
return 0
|| khtml_attrx(
html, KELEM_TR,
KATTR_ID, KATTRX_INT, event.event,
KATTR_CLASS, KATTRX_STRING, type,
KATTR__MAX
)
|| eventTime(html, event)
|| eventNetwork(html, event)
|| eventContext(html, event)
|| eventNick(html, event)
|| khtml_closeelem(html, 1);
}
|