2 5565365c 2024-03-27 op * Copyright (c) 2024 Omar Polo <op@openbsd.org>
4 5565365c 2024-03-27 op * Permission to use, copy, modify, and distribute this software for any
5 5565365c 2024-03-27 op * purpose with or without fee is hereby granted, provided that the above
6 5565365c 2024-03-27 op * copyright notice and this permission notice appear in all copies.
8 5565365c 2024-03-27 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 5565365c 2024-03-27 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 5565365c 2024-03-27 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 5565365c 2024-03-27 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 5565365c 2024-03-27 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 5565365c 2024-03-27 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 5565365c 2024-03-27 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 5565365c 2024-03-27 op #include <sys/time.h>
18 5565365c 2024-03-27 op #include <sys/types.h>
19 5565365c 2024-03-27 op #include <sys/socket.h>
21 5565365c 2024-03-27 op #include <err.h>
22 5565365c 2024-03-27 op #include <errno.h>
23 5565365c 2024-03-27 op #include <fcntl.h>
24 5565365c 2024-03-27 op #include <limits.h>
25 5565365c 2024-03-27 op #include <netdb.h>
26 5565365c 2024-03-27 op #include <poll.h>
27 cb29e255 2024-04-18 stsp #include <stdarg.h>
28 02dab75a 2024-04-18 stsp #include <stdio.h>
29 5565365c 2024-03-27 op #include <stdlib.h>
30 5565365c 2024-03-27 op #include <string.h>
31 cb29e255 2024-04-18 stsp #include <syslog.h>
32 5565365c 2024-03-27 op #include <unistd.h>
34 5565365c 2024-03-27 op #include "got_opentemp.h"
35 5565365c 2024-03-27 op #include "got_version.h"
37 5565365c 2024-03-27 op #include "bufio.h"
38 cb29e255 2024-04-18 stsp #include "log.h"
39 02dab75a 2024-04-18 stsp #include "utf8d.h"
41 5565365c 2024-03-27 op #define USERAGENT "got-notify-http/" GOT_VERSION_STR
43 5565365c 2024-03-27 op static int http_timeout = 300; /* 5 minutes in seconds */
45 5565365c 2024-03-27 op __dead static void
48 c1003102 2024-04-15 op fprintf(stderr, "usage: %s [-c] -r repo -h host -p port path\n",
49 5565365c 2024-03-27 op getprogname());
54 5565365c 2024-03-27 op dial(const char *host, const char *port)
56 5565365c 2024-03-27 op struct addrinfo hints, *res, *res0;
57 5565365c 2024-03-27 op const char *cause = NULL;
58 5565365c 2024-03-27 op int s, error, save_errno;
60 5565365c 2024-03-27 op memset(&hints, 0, sizeof(hints));
61 5565365c 2024-03-27 op hints.ai_family = AF_UNSPEC;
62 5565365c 2024-03-27 op hints.ai_socktype = SOCK_STREAM;
63 5565365c 2024-03-27 op error = getaddrinfo(host, port, &hints, &res0);
65 5565365c 2024-03-27 op errx(1, "failed to resolve %s:%s: %s", host, port,
66 5565365c 2024-03-27 op gai_strerror(error));
69 5565365c 2024-03-27 op for (res = res0; res; res = res->ai_next) {
70 5565365c 2024-03-27 op s = socket(res->ai_family, res->ai_socktype,
71 5565365c 2024-03-27 op res->ai_protocol);
72 5565365c 2024-03-27 op if (s == -1) {
73 5565365c 2024-03-27 op cause = "socket";
77 5565365c 2024-03-27 op if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
78 5565365c 2024-03-27 op cause = "connect";
79 5565365c 2024-03-27 op save_errno = errno;
81 5565365c 2024-03-27 op errno = save_errno;
89 5565365c 2024-03-27 op freeaddrinfo(res0);
91 5565365c 2024-03-27 op err(1, "%s", cause);
96 5565365c 2024-03-27 op escape(FILE *fp, const uint8_t *s)
98 ea5e974d 2024-03-28 op uint32_t codepoint, state;
99 ea5e974d 2024-03-28 op const uint8_t *start = s;
102 ea5e974d 2024-03-28 op for (; *s; ++s) {
103 ea5e974d 2024-03-28 op switch (decode(&state, &codepoint, *s)) {
104 ea5e974d 2024-03-28 op case UTF8_ACCEPT:
105 ea5e974d 2024-03-28 op switch (codepoint) {
108 ea5e974d 2024-03-28 op fprintf(fp, "\\%c", *s);
111 ea5e974d 2024-03-28 op fprintf(fp, "\\b");
114 ea5e974d 2024-03-28 op fprintf(fp, "\\f");
117 ea5e974d 2024-03-28 op fprintf(fp, "\\n");
120 ea5e974d 2024-03-28 op fprintf(fp, "\\r");
123 ea5e974d 2024-03-28 op fprintf(fp, "\\t");
126 ea5e974d 2024-03-28 op /* other control characters */
127 ea5e974d 2024-03-28 op if (codepoint < ' ' || codepoint == 0x7F) {
128 ea5e974d 2024-03-28 op fprintf(fp, "\\u%04x", codepoint);
131 ea5e974d 2024-03-28 op fwrite(start, 1, s - start + 1, fp);
134 ea5e974d 2024-03-28 op start = s + 1;
137 ea5e974d 2024-03-28 op case UTF8_REJECT:
138 ea5e974d 2024-03-28 op /* bad UTF-8 sequence; try to recover */
139 ea5e974d 2024-03-28 op fputs("\\uFFFD", fp);
140 ea5e974d 2024-03-28 op state = UTF8_ACCEPT;
141 ea5e974d 2024-03-28 op start = s + 1;
148 5565365c 2024-03-27 op json_field(FILE *fp, const char *key, const char *val, int comma)
150 5565365c 2024-03-27 op fprintf(fp, "\"%s\":\"", key);
151 5565365c 2024-03-27 op escape(fp, val);
152 5565365c 2024-03-27 op fprintf(fp, "\"%s", comma ? "," : "");
156 ac0a4dfc 2024-03-28 op json_author(FILE *fp, const char *type, char *address, int comma)
158 ac0a4dfc 2024-03-28 op char *gt, *lt, *at, *email, *endname;
160 ac0a4dfc 2024-03-28 op fprintf(fp, "\"%s\":{", type);
162 ac0a4dfc 2024-03-28 op gt = strchr(address, '<');
163 ac0a4dfc 2024-03-28 op if (gt != NULL) {
164 ac0a4dfc 2024-03-28 op /* long format, e.g. "Omar Polo <op@openbsd.org>" */
166 ac0a4dfc 2024-03-28 op json_field(fp, "full", address, 1);
168 ac0a4dfc 2024-03-28 op endname = gt;
169 ac0a4dfc 2024-03-28 op while (endname > address && endname[-1] == ' ')
172 ac0a4dfc 2024-03-28 op *endname = '\0';
173 ac0a4dfc 2024-03-28 op json_field(fp, "name", address, 1);
175 ac0a4dfc 2024-03-28 op email = gt + 1;
176 ac0a4dfc 2024-03-28 op lt = strchr(email, '>');
180 ac0a4dfc 2024-03-28 op json_field(fp, "mail", email, 1);
182 ac0a4dfc 2024-03-28 op at = strchr(email, '@');
186 ac0a4dfc 2024-03-28 op json_field(fp, "user", email, 0);
188 ac0a4dfc 2024-03-28 op /* short format only shows the username */
189 ac0a4dfc 2024-03-28 op json_field(fp, "user", address, 0);
192 ac0a4dfc 2024-03-28 op fprintf(fp, "}%s", comma ? "," : "");
196 c1003102 2024-04-15 op jsonify_branch_rm(FILE *fp, char *line, const char *repo)
198 d6057084 2024-03-28 op char *ref, *id;
200 d6057084 2024-03-28 op line = strchr(line, ' ');
201 d6057084 2024-03-28 op if (line == NULL)
202 d6057084 2024-03-28 op errx(1, "invalid branch rm line");
203 d6057084 2024-03-28 op line += strspn(line, " ");
207 d6057084 2024-03-28 op line = strchr(line, ':');
208 d6057084 2024-03-28 op if (line == NULL)
209 d6057084 2024-03-28 op errx(1, "invalid branch rm line");
210 d6057084 2024-03-28 op *line++ = '\0';
211 d6057084 2024-03-28 op id = line + strspn(line, " ");
213 d6057084 2024-03-28 op fputc('{', fp);
214 d6057084 2024-03-28 op json_field(fp, "type", "branch-deleted", 1);
215 c1003102 2024-04-15 op json_field(fp, "repo", repo, 1);
216 d6057084 2024-03-28 op json_field(fp, "ref", ref, 1);
217 d6057084 2024-03-28 op json_field(fp, "id", id, 0);
218 d6057084 2024-03-28 op fputc('}', fp);
224 c1003102 2024-04-15 op jsonify_commit_short(FILE *fp, char *line, const char *repo)
226 5565365c 2024-03-27 op char *t, *date, *id, *author, *message;
230 ec405b99 2024-03-28 op if ((t = strchr(t, ' ')) == NULL)
231 ec405b99 2024-03-28 op errx(1, "malformed line");
235 ec405b99 2024-03-28 op if ((t = strchr(t, ' ')) == NULL)
236 ec405b99 2024-03-28 op errx(1, "malformed line");
240 ec405b99 2024-03-28 op if ((t = strchr(t, ' ')) == NULL)
241 ec405b99 2024-03-28 op errx(1, "malformed line");
246 93623901 2024-03-28 op fprintf(fp, "{\"type\":\"commit\",\"short\":true,");
247 c1003102 2024-04-15 op json_field(fp, "repo", repo, 1);
248 ec405b99 2024-03-28 op json_field(fp, "id", id, 1);
249 ec405b99 2024-03-28 op json_author(fp, "committer", author, 1);
250 ec405b99 2024-03-28 op json_field(fp, "date", date, 1);
251 ec405b99 2024-03-28 op json_field(fp, "short_message", message, 0);
252 ec405b99 2024-03-28 op fprintf(fp, "}");
258 c1003102 2024-04-15 op jsonify_commit(FILE *fp, const char *repo, char **line, ssize_t *linesize)
260 5565365c 2024-03-27 op const char *errstr;
261 ac0a4dfc 2024-03-28 op char *author = NULL;
262 763b7f49 2024-03-28 op char *filename, *t;
264 5565365c 2024-03-27 op ssize_t linelen;
265 5565365c 2024-03-27 op int parent = 0;
266 5565365c 2024-03-27 op int msglen = 0, msgwrote = 0;
267 763b7f49 2024-03-28 op int n, files = 0;
268 ec405b99 2024-03-28 op int done = 0;
278 ec405b99 2024-03-28 op } phase = P_FROM;
281 ec405b99 2024-03-28 op if (strncmp(l, "commit ", 7) != 0)
282 ec405b99 2024-03-28 op errx(1, "%s: unexpected line: %s", __func__, l);
285 93623901 2024-03-28 op fprintf(fp, "{\"type\":\"commit\",\"short\":false,");
286 c1003102 2024-04-15 op json_field(fp, "repo", repo, 1);
287 ec405b99 2024-03-28 op json_field(fp, "id", l, 1);
289 ec405b99 2024-03-28 op while (!done) {
290 ec405b99 2024-03-28 op if ((linelen = getline(line, linesize, stdin)) == -1)
293 ec405b99 2024-03-28 op if ((*line)[linelen - 1] == '\n')
294 ec405b99 2024-03-28 op (*line)[--linelen] = '\0';
297 ec405b99 2024-03-28 op switch (phase) {
299 5565365c 2024-03-27 op if (strncmp(l, "from: ", 6) != 0)
300 5565365c 2024-03-27 op errx(1, "unexpected from line");
303 ac0a4dfc 2024-03-28 op author = strdup(l);
304 ac0a4dfc 2024-03-28 op if (author == NULL)
305 ac0a4dfc 2024-03-28 op err(1, "strdup");
307 ac0a4dfc 2024-03-28 op json_author(fp, "author", l, 1);
309 5565365c 2024-03-27 op phase = P_VIA;
313 5565365c 2024-03-27 op /* optional */
314 5565365c 2024-03-27 op if (!strncmp(l, "via: ", 5)) {
316 ac0a4dfc 2024-03-28 op json_author(fp, "committer", l, 1);
317 5565365c 2024-03-27 op phase = P_DATE;
321 ac0a4dfc 2024-03-28 op if (author == NULL) /* impossible */
322 ac0a4dfc 2024-03-28 op err(1, "from not specified");
323 ac0a4dfc 2024-03-28 op json_author(fp, "committer", author, 1);
324 ac0a4dfc 2024-03-28 op free(author);
325 ac0a4dfc 2024-03-28 op author = NULL;
327 5565365c 2024-03-27 op phase = P_DATE;
328 5565365c 2024-03-27 op /* fallthrough */
331 5565365c 2024-03-27 op /* optional */
332 5565365c 2024-03-27 op if (!strncmp(l, "date: ", 6)) {
334 5565365c 2024-03-27 op json_field(fp, "date", l, 1);
335 5565365c 2024-03-27 op phase = P_PARENT;
338 5565365c 2024-03-27 op phase = P_PARENT;
339 5565365c 2024-03-27 op /* fallthough */
341 5565365c 2024-03-27 op case P_PARENT:
342 5565365c 2024-03-27 op /* optional - more than one */
343 5565365c 2024-03-27 op if (!strncmp(l, "parent ", 7)) {
345 5565365c 2024-03-27 op l += strcspn(l, ":");
346 5565365c 2024-03-27 op l += strspn(l, " ");
348 5565365c 2024-03-27 op if (parent == 0) {
350 5565365c 2024-03-27 op fprintf(fp, "\"parents\":[");
353 5565365c 2024-03-27 op fputc('"', fp);
354 5565365c 2024-03-27 op escape(fp, l);
355 5565365c 2024-03-27 op fputc('"', fp);
359 5565365c 2024-03-27 op if (parent != 0) {
360 5565365c 2024-03-27 op fprintf(fp, "],");
363 5565365c 2024-03-27 op phase = P_MSGLEN;
364 5565365c 2024-03-27 op /* fallthrough */
366 5565365c 2024-03-27 op case P_MSGLEN:
367 5565365c 2024-03-27 op if (strncmp(l, "messagelen: ", 12) != 0)
368 5565365c 2024-03-27 op errx(1, "unexpected messagelen line");
370 5565365c 2024-03-27 op msglen = strtonum(l, 1, INT_MAX, &errstr);
372 5565365c 2024-03-27 op errx(1, "message len is %s: %s", errstr, l);
376 5565365c 2024-03-27 op phase = P_MSG;
381 5565365c 2024-03-27 op * The commit message is indented with one extra
382 5565365c 2024-03-27 op * space which is not accounted for in messagelen,
383 5565365c 2024-03-27 op * but we also strip the trailing \n so that
384 5565365c 2024-03-27 op * accounts for it.
386 5565365c 2024-03-27 op * Since we read line-by-line and there is always
387 5565365c 2024-03-27 op * a \n added at the end of the message,
388 5565365c 2024-03-27 op * tolerate one byte less than advertised.
390 763b7f49 2024-03-28 op if (*l != ' ')
391 763b7f49 2024-03-28 op errx(1, "unexpected line in commit message");
393 763b7f49 2024-03-28 op l++; /* skip leading space */
396 763b7f49 2024-03-28 op if (msgwrote == 0 && linelen != 0) {
397 763b7f49 2024-03-28 op json_field(fp, "short_message", l, 1);
398 763b7f49 2024-03-28 op fprintf(fp, "\"message\":\"");
399 763b7f49 2024-03-28 op escape(fp, l);
400 763b7f49 2024-03-28 op escape(fp, "\n");
401 763b7f49 2024-03-28 op msgwrote += linelen;
402 763b7f49 2024-03-28 op } else if (msgwrote != 0) {
403 763b7f49 2024-03-28 op escape(fp, l);
404 763b7f49 2024-03-28 op escape(fp, "\n");
407 ac0a4dfc 2024-03-28 op msglen -= linelen + 1;
408 5565365c 2024-03-27 op if (msglen <= 1) {
409 5565365c 2024-03-27 op fprintf(fp, "\",");
410 5565365c 2024-03-27 op phase = P_DST;
416 763b7f49 2024-03-28 op if (files == 0 && !strcmp(l, " "))
419 763b7f49 2024-03-28 op if (files == 0)
420 763b7f49 2024-03-28 op fputs("\"diffstat\":{\"files\":[", fp);
422 ec405b99 2024-03-28 op if (*l == '\0') {
423 763b7f49 2024-03-28 op fputs("],", fp);
424 5565365c 2024-03-27 op phase = P_SUM;
428 763b7f49 2024-03-28 op if (*l != ' ')
429 763b7f49 2024-03-28 op errx(1, "bad diffstat line");
432 763b7f49 2024-03-28 op if (files != 0)
433 763b7f49 2024-03-28 op fputc(',', fp);
434 763b7f49 2024-03-28 op fputc('{', fp);
436 763b7f49 2024-03-28 op switch (*l) {
438 763b7f49 2024-03-28 op json_field(fp, "action", "added", 1);
441 763b7f49 2024-03-28 op json_field(fp, "action", "deleted", 1);
444 763b7f49 2024-03-28 op json_field(fp, "action", "modified", 1);
447 763b7f49 2024-03-28 op json_field(fp, "action", "mode changed", 1);
450 763b7f49 2024-03-28 op json_field(fp, "action", "unknown", 1);
455 763b7f49 2024-03-28 op while (*l == ' ')
457 763b7f49 2024-03-28 op if (*l == '\0')
458 763b7f49 2024-03-28 op errx(1, "invalid diffstat: no filename");
460 763b7f49 2024-03-28 op filename = l;
461 763b7f49 2024-03-28 op l = strrchr(l, '|');
462 763b7f49 2024-03-28 op if (l == NULL)
463 763b7f49 2024-03-28 op errx(1, "invalid diffstat: no separator");
465 763b7f49 2024-03-28 op while (t > filename && *t == ' ')
467 763b7f49 2024-03-28 op json_field(fp, "file", filename, 1);
470 763b7f49 2024-03-28 op while (*l == ' ')
473 763b7f49 2024-03-28 op t = strchr(l, '+');
474 763b7f49 2024-03-28 op if (t == NULL)
475 763b7f49 2024-03-28 op errx(1, "invalid diffstat: no added counter");
478 763b7f49 2024-03-28 op n = strtonum(l, 0, INT_MAX, &errstr);
480 763b7f49 2024-03-28 op errx(1, "added counter is %s: %s", errstr, l);
481 763b7f49 2024-03-28 op fprintf(fp, "\"added\":%d,", n);
484 763b7f49 2024-03-28 op while (*l == ' ')
487 763b7f49 2024-03-28 op t = strchr(l, '-');
488 763b7f49 2024-03-28 op if (t == NULL)
489 763b7f49 2024-03-28 op errx(1, "invalid diffstat: no del counter");
492 763b7f49 2024-03-28 op n = strtonum(l, 0, INT_MAX, &errstr);
494 763b7f49 2024-03-28 op errx(1, "del counter is %s: %s", errstr, l);
495 763b7f49 2024-03-28 op fprintf(fp, "\"removed\":%d", n);
497 763b7f49 2024-03-28 op fputc('}', fp);
504 763b7f49 2024-03-28 op fputs("\"total\":{", fp);
507 763b7f49 2024-03-28 op l = strchr(l, ' ');
508 763b7f49 2024-03-28 op if (l == NULL)
509 763b7f49 2024-03-28 op errx(1, "missing number of additions");
512 763b7f49 2024-03-28 op n = strtonum(t, 0, INT_MAX, &errstr);
514 763b7f49 2024-03-28 op errx(1, "add counter is %s: %s", errstr, t);
515 763b7f49 2024-03-28 op fprintf(fp, "\"added\":%d,", n);
517 763b7f49 2024-03-28 op l = strchr(l, ',');
518 763b7f49 2024-03-28 op if (l == NULL)
519 763b7f49 2024-03-28 op errx(1, "missing number of deletions");
521 763b7f49 2024-03-28 op while (*l == ' ')
524 763b7f49 2024-03-28 op t = strchr(l, ' ');
525 763b7f49 2024-03-28 op if (t == NULL)
526 763b7f49 2024-03-28 op errx(1, "malformed diffstat sum line");
529 763b7f49 2024-03-28 op n = strtonum(l, 0, INT_MAX, &errstr);
531 763b7f49 2024-03-28 op errx(1, "del counter is %s: %s", errstr, l);
532 763b7f49 2024-03-28 op fprintf(fp, "\"removed\":%d", n);
534 763b7f49 2024-03-28 op fputs("}}", fp);
539 ec405b99 2024-03-28 op /* unreachable */
540 ec405b99 2024-03-28 op errx(1, "unexpected line: %s", *line);
543 5565365c 2024-03-27 op if (ferror(stdin))
544 5565365c 2024-03-27 op err(1, "getline");
546 5565365c 2024-03-27 op errx(1, "unexpected EOF");
547 763b7f49 2024-03-28 op fputc('}', fp);
553 c1003102 2024-04-15 op jsonify_tag(FILE *fp, const char *repo, char **line, ssize_t *linesize)
555 553d8347 2024-03-28 op const char *errstr;
557 553d8347 2024-03-28 op ssize_t linelen;
558 553d8347 2024-03-28 op int msglen = 0, msgwrote = 0;
559 553d8347 2024-03-28 op int done = 0;
566 553d8347 2024-03-28 op } phase = P_FROM;
569 553d8347 2024-03-28 op if (strncmp(l, "tag ", 4) != 0)
570 553d8347 2024-03-28 op errx(1, "%s: unexpected line: %s", __func__, l);
573 553d8347 2024-03-28 op fputc('{', fp);
574 553d8347 2024-03-28 op json_field(fp, "type", "tag", 1);
575 c1003102 2024-04-15 op json_field(fp, "repo", repo, 1);
576 553d8347 2024-03-28 op json_field(fp, "tag", l, 1);
578 553d8347 2024-03-28 op while (!done) {
579 553d8347 2024-03-28 op if ((linelen = getline(line, linesize, stdin)) == -1)
582 553d8347 2024-03-28 op if ((*line)[linelen - 1] == '\n')
583 553d8347 2024-03-28 op (*line)[--linelen] = '\0';
586 553d8347 2024-03-28 op switch (phase) {
588 553d8347 2024-03-28 op if (strncmp(l, "from: ", 6) != 0)
589 553d8347 2024-03-28 op errx(1, "unexpected from line");
592 553d8347 2024-03-28 op json_author(fp, "tagger", l, 1);
594 553d8347 2024-03-28 op phase = P_DATE;
598 553d8347 2024-03-28 op /* optional */
599 553d8347 2024-03-28 op if (!strncmp(l, "date: ", 6)) {
601 553d8347 2024-03-28 op json_field(fp, "date", l, 1);
602 553d8347 2024-03-28 op phase = P_OBJECT;
605 553d8347 2024-03-28 op phase = P_OBJECT;
606 553d8347 2024-03-28 op /* fallthough */
608 553d8347 2024-03-28 op case P_OBJECT:
609 553d8347 2024-03-28 op /* optional */
610 553d8347 2024-03-28 op if (!strncmp(l, "object: ", 8)) {
611 553d8347 2024-03-28 op char *type, *id;
615 553d8347 2024-03-28 op id = strchr(l, ' ');
616 553d8347 2024-03-28 op if (id == NULL)
617 553d8347 2024-03-28 op errx(1, "malformed tag object line");
618 553d8347 2024-03-28 op *id++ = '\0';
620 553d8347 2024-03-28 op fputs("\"object\":{", fp);
621 553d8347 2024-03-28 op json_field(fp, "type", type, 1);
622 553d8347 2024-03-28 op json_field(fp, "id", id, 0);
623 553d8347 2024-03-28 op fputs("},", fp);
625 553d8347 2024-03-28 op phase = P_MSGLEN;
628 553d8347 2024-03-28 op phase = P_MSGLEN;
629 553d8347 2024-03-28 op /* fallthrough */
631 553d8347 2024-03-28 op case P_MSGLEN:
632 553d8347 2024-03-28 op if (strncmp(l, "messagelen: ", 12) != 0)
633 553d8347 2024-03-28 op errx(1, "unexpected messagelen line");
635 553d8347 2024-03-28 op msglen = strtonum(l, 1, INT_MAX, &errstr);
637 553d8347 2024-03-28 op errx(1, "message len is %s: %s", errstr, l);
641 553d8347 2024-03-28 op phase = P_MSG;
645 763b7f49 2024-03-28 op if (*l != ' ')
646 763b7f49 2024-03-28 op errx(1, "unexpected line in tag message");
648 763b7f49 2024-03-28 op l++; /* skip leading space */
651 763b7f49 2024-03-28 op if (msgwrote == 0 && linelen != 0) {
652 763b7f49 2024-03-28 op fprintf(fp, "\"message\":\"");
653 763b7f49 2024-03-28 op escape(fp, l);
654 763b7f49 2024-03-28 op escape(fp, "\n");
655 763b7f49 2024-03-28 op msgwrote += linelen;
656 763b7f49 2024-03-28 op } else if (msgwrote != 0) {
657 763b7f49 2024-03-28 op escape(fp, l);
658 763b7f49 2024-03-28 op escape(fp, "\n");
661 553d8347 2024-03-28 op msglen -= linelen + 1;
662 e789f02b 2024-03-28 op if (msglen <= 0) {
663 553d8347 2024-03-28 op fprintf(fp, "\"");
670 553d8347 2024-03-28 op /* unreachable */
671 553d8347 2024-03-28 op errx(1, "unexpected line: %s", *line);
674 553d8347 2024-03-28 op if (ferror(stdin))
675 553d8347 2024-03-28 op err(1, "getline");
677 553d8347 2024-03-28 op errx(1, "unexpected EOF");
678 553d8347 2024-03-28 op fputc('}', fp);
684 c1003102 2024-04-15 op jsonify(FILE *fp, const char *repo)
686 ec405b99 2024-03-28 op char *line = NULL;
687 ec405b99 2024-03-28 op size_t linesize = 0;
688 ec405b99 2024-03-28 op ssize_t linelen;
689 ec405b99 2024-03-28 op int needcomma = 0;
691 ec405b99 2024-03-28 op fprintf(fp, "{\"notifications\":[");
692 ec405b99 2024-03-28 op while ((linelen = getline(&line, &linesize, stdin)) != -1) {
693 ec405b99 2024-03-28 op if (line[linelen - 1] == '\n')
694 ec405b99 2024-03-28 op line[--linelen] = '\0';
696 ec405b99 2024-03-28 op if (*line == '\0')
699 ec405b99 2024-03-28 op if (needcomma)
700 ec405b99 2024-03-28 op fputc(',', fp);
701 ec405b99 2024-03-28 op needcomma = 1;
703 d6057084 2024-03-28 op if (strncmp(line, "Removed refs/heads/", 19) == 0) {
704 c1003102 2024-04-15 op if (jsonify_branch_rm(fp, line, repo) == -1)
705 d6057084 2024-03-28 op err(1, "jsonify_branch_rm");
709 ec405b99 2024-03-28 op if (strncmp(line, "commit ", 7) == 0) {
710 c1003102 2024-04-15 op if (jsonify_commit(fp, repo, &line, &linesize) == -1)
711 ec405b99 2024-03-28 op err(1, "jsonify_commit");
715 ec405b99 2024-03-28 op if (*line >= '0' && *line <= '9') {
716 c1003102 2024-04-15 op if (jsonify_commit_short(fp, line, repo) == -1)
717 ec405b99 2024-03-28 op err(1, "jsonify_commit_short");
721 553d8347 2024-03-28 op if (strncmp(line, "tag ", 4) == 0) {
722 c1003102 2024-04-15 op if (jsonify_tag(fp, repo, &line, &linesize) == -1)
723 553d8347 2024-03-28 op err(1, "jsonify_tag");
727 ec405b99 2024-03-28 op errx(1, "unexpected line: %s", line);
729 ec405b99 2024-03-28 op if (ferror(stdin))
730 ec405b99 2024-03-28 op err(1, "getline");
731 5565365c 2024-03-27 op fprintf(fp, "]}");
737 050c0b8c 2024-04-16 op sixet2ch(int c)
742 050c0b8c 2024-04-16 op return 'A' + c;
745 050c0b8c 2024-04-16 op return 'a' + c;
748 050c0b8c 2024-04-16 op return '0' + c;
755 050c0b8c 2024-04-16 op errx(1, "invalid sixet 0x%x", c);
758 5565365c 2024-03-27 op static char *
759 5565365c 2024-03-27 op basic_auth(const char *username, const char *password)
761 050c0b8c 2024-04-16 op char *str, *tmp, *end, *s, *p;
763 050c0b8c 2024-04-16 op int len, i, r;
765 050c0b8c 2024-04-16 op r = asprintf(&str, "%s:%s", username, password);
767 5565365c 2024-03-27 op err(1, "asprintf");
770 050c0b8c 2024-04-16 op * Will need 4 * r/3 bytes to encode the string, plus a
771 050c0b8c 2024-04-16 op * rounding to the next multiple of 4 for padding, plus NUL.
773 050c0b8c 2024-04-16 op len = 4 * r / 3;
774 050c0b8c 2024-04-16 op len = (len + 3) & ~3;
777 050c0b8c 2024-04-16 op tmp = calloc(1, len);
778 050c0b8c 2024-04-16 op if (tmp == NULL)
779 050c0b8c 2024-04-16 op err(1, "malloc");
783 050c0b8c 2024-04-16 op while (*s != '\0') {
784 050c0b8c 2024-04-16 op memset(buf, 0, sizeof(buf));
785 050c0b8c 2024-04-16 op for (i = 0; i < 3 && *s != '\0'; ++i, ++s)
788 050c0b8c 2024-04-16 op *p++ = sixet2ch(buf[0] >> 2);
789 050c0b8c 2024-04-16 op *p++ = sixet2ch((buf[1] >> 4) | (buf[0] << 4));
791 050c0b8c 2024-04-16 op *p++ = sixet2ch((buf[1] << 2) | (buf[2] >> 6));
793 050c0b8c 2024-04-16 op *p++ = sixet2ch(buf[2]);
796 050c0b8c 2024-04-16 op for (end = tmp + len - 1; p < end; ++p)
803 5565365c 2024-03-27 op static inline int
804 5565365c 2024-03-27 op bufio2poll(struct bufio *bio)
806 5565365c 2024-03-27 op int f, ret = 0;
808 5565365c 2024-03-27 op f = bufio_ev(bio);
809 5565365c 2024-03-27 op if (f & BUFIO_WANT_READ)
810 5565365c 2024-03-27 op ret |= POLLIN;
811 5565365c 2024-03-27 op if (f & BUFIO_WANT_WRITE)
812 5565365c 2024-03-27 op ret |= POLLOUT;
817 5565365c 2024-03-27 op main(int argc, char **argv)
820 5565365c 2024-03-27 op struct bufio bio;
821 5565365c 2024-03-27 op struct pollfd pfd;
822 5565365c 2024-03-27 op struct timespec timeout;
823 5565365c 2024-03-27 op const char *username;
824 5565365c 2024-03-27 op const char *password;
825 5565365c 2024-03-27 op const char *timeoutstr;
826 5565365c 2024-03-27 op const char *errstr;
827 c1003102 2024-04-15 op const char *repo = NULL;
828 5565365c 2024-03-27 op const char *host = NULL, *port = NULL, *path = NULL;
829 5565365c 2024-03-27 op char *auth, *line, *spc;
832 5565365c 2024-03-27 op off_t paylen;
834 5565365c 2024-03-27 op int response_code = 0, done = 0;
835 5565365c 2024-03-27 op int ch, flags, ret, nonstd = 0;
837 5565365c 2024-03-27 op #ifndef PROFILE
838 5565365c 2024-03-27 op if (pledge("stdio rpath tmppath dns inet", NULL) == -1)
839 5565365c 2024-03-27 op err(1, "pledge");
842 cb29e255 2024-04-18 stsp log_init(0, LOG_DAEMON);
844 c1003102 2024-04-15 op while ((ch = getopt(argc, argv, "ch:p:r:")) != -1) {
845 5565365c 2024-03-27 op switch (ch) {
850 5565365c 2024-03-27 op host = optarg;
853 5565365c 2024-03-27 op port = optarg;
856 c1003102 2024-04-15 op repo = optarg;
862 5565365c 2024-03-27 op argc -= optind;
863 5565365c 2024-03-27 op argv += optind;
865 c1003102 2024-04-15 op if (host == NULL || repo == NULL || argc != 1)
867 5565365c 2024-03-27 op if (tls && port == NULL)
868 5565365c 2024-03-27 op port = "443";
869 5565365c 2024-03-27 op path = argv[0];
871 5565365c 2024-03-27 op username = getenv("GOT_NOTIFY_HTTP_USER");
872 5565365c 2024-03-27 op password = getenv("GOT_NOTIFY_HTTP_PASS");
873 5565365c 2024-03-27 op if ((username != NULL && password == NULL) ||
874 5565365c 2024-03-27 op (username == NULL && password != NULL))
875 cb29e255 2024-04-18 stsp fatalx("username or password are not specified");
876 5565365c 2024-03-27 op if (username && *password == '\0')
877 cb29e255 2024-04-18 stsp fatalx("password can't be empty");
879 5565365c 2024-03-27 op /* used by the regression test suite */
880 5565365c 2024-03-27 op timeoutstr = getenv("GOT_NOTIFY_TIMEOUT");
881 5565365c 2024-03-27 op if (timeoutstr) {
882 5565365c 2024-03-27 op http_timeout = strtonum(timeoutstr, 0, 600, &errstr);
883 5565365c 2024-03-27 op if (errstr != NULL)
884 cb29e255 2024-04-18 stsp fatalx("timeout in seconds is %s: %s",
885 5565365c 2024-03-27 op errstr, timeoutstr);
888 5565365c 2024-03-27 op memset(&timeout, 0, sizeof(timeout));
889 5565365c 2024-03-27 op timeout.tv_sec = http_timeout;
891 5565365c 2024-03-27 op tmpfp = got_opentemp();
892 5565365c 2024-03-27 op if (tmpfp == NULL)
893 cb29e255 2024-04-18 stsp fatal("opentemp");
895 c1003102 2024-04-15 op jsonify(tmpfp, repo);
897 5565365c 2024-03-27 op paylen = ftello(tmpfp);
898 5565365c 2024-03-27 op if (paylen == -1)
899 cb29e255 2024-04-18 stsp fatal("ftello");
900 5565365c 2024-03-27 op if (fseeko(tmpfp, 0, SEEK_SET) == -1)
901 cb29e255 2024-04-18 stsp fatal("fseeko");
903 5565365c 2024-03-27 op #ifndef PROFILE
904 5565365c 2024-03-27 op /* drop tmppath */
905 5565365c 2024-03-27 op if (pledge("stdio rpath dns inet", NULL) == -1)
906 5565365c 2024-03-27 op err(1, "pledge");
909 5565365c 2024-03-27 op memset(&pfd, 0, sizeof(pfd));
910 5565365c 2024-03-27 op pfd.fd = dial(host, port);
912 5565365c 2024-03-27 op if ((flags = fcntl(pfd.fd, F_GETFL)) == -1)
913 cb29e255 2024-04-18 stsp fatal("fcntl(F_GETFL)");
914 5565365c 2024-03-27 op if (fcntl(pfd.fd, F_SETFL, flags | O_NONBLOCK) == -1)
915 cb29e255 2024-04-18 stsp fatal("fcntl(F_SETFL)");
917 5565365c 2024-03-27 op if (bufio_init(&bio) == -1)
918 cb29e255 2024-04-18 stsp fatal("bufio_init");
919 5565365c 2024-03-27 op bufio_set_fd(&bio, pfd.fd);
920 5565365c 2024-03-27 op if (tls && bufio_starttls(&bio, host, 0, NULL, 0, NULL, 0) == -1)
921 cb29e255 2024-04-18 stsp fatal("bufio_starttls");
923 5565365c 2024-03-27 op #ifndef PROFILE
924 5565365c 2024-03-27 op /* drop rpath dns inet */
925 5565365c 2024-03-27 op if (pledge("stdio", NULL) == -1)
926 5565365c 2024-03-27 op err(1, "pledge");
929 5565365c 2024-03-27 op if ((!tls && strcmp(port, "80") != 0) ||
930 5565365c 2024-03-27 op (tls && strcmp(port, "443")) != 0)
933 5565365c 2024-03-27 op ret = bufio_compose_fmt(&bio,
934 5565365c 2024-03-27 op "POST %s HTTP/1.1\r\n"
935 5565365c 2024-03-27 op "Host: %s%s%s\r\n"
936 5565365c 2024-03-27 op "Content-Type: application/json\r\n"
937 5565365c 2024-03-27 op "Content-Length: %lld\r\n"
938 5565365c 2024-03-27 op "User-Agent: %s\r\n"
939 5565365c 2024-03-27 op "Connection: close\r\n",
941 5565365c 2024-03-27 op nonstd ? ":" : "", nonstd ? port : "",
942 5565365c 2024-03-27 op (long long)paylen, USERAGENT);
943 5565365c 2024-03-27 op if (ret == -1)
944 cb29e255 2024-04-18 stsp fatal("bufio_compose_fmt");
946 5565365c 2024-03-27 op if (username) {
947 5565365c 2024-03-27 op auth = basic_auth(username, password);
948 5565365c 2024-03-27 op ret = bufio_compose_fmt(&bio, "Authorization: basic %s\r\n",
950 5565365c 2024-03-27 op if (ret == -1)
951 cb29e255 2024-04-18 stsp fatal("bufio_compose_fmt");
955 5565365c 2024-03-27 op if (bufio_compose(&bio, "\r\n", 2) == -1)
956 cb29e255 2024-04-18 stsp fatal("bufio_compose");
958 5565365c 2024-03-27 op while (!done) {
959 5565365c 2024-03-27 op struct timespec elapsed, start, stop;
960 5565365c 2024-03-27 op char buf[BUFSIZ];
962 5565365c 2024-03-27 op pfd.events = bufio2poll(&bio);
963 5565365c 2024-03-27 op clock_gettime(CLOCK_MONOTONIC, &start);
964 5565365c 2024-03-27 op ret = ppoll(&pfd, 1, &timeout, NULL);
965 5565365c 2024-03-27 op if (ret == -1)
966 cb29e255 2024-04-18 stsp fatal("poll");
967 5565365c 2024-03-27 op clock_gettime(CLOCK_MONOTONIC, &stop);
968 5565365c 2024-03-27 op timespecsub(&stop, &start, &elapsed);
969 5565365c 2024-03-27 op timespecsub(&timeout, &elapsed, &timeout);
970 5565365c 2024-03-27 op if (ret == 0 || timeout.tv_sec <= 0)
971 cb29e255 2024-04-18 stsp fatalx("timeout");
973 a9d9f6e4 2024-04-18 op if (bio.wbuf.len > 0) {
974 5565365c 2024-03-27 op if (bufio_write(&bio) == -1 && errno != EAGAIN)
975 cb29e255 2024-04-18 stsp fatalx("bufio_write: %s", bufio_io_err(&bio));
978 a9d9f6e4 2024-04-18 op r = bufio_read(&bio);
979 a9d9f6e4 2024-04-18 op if (r == -1 && errno != EAGAIN)
980 a9d9f6e4 2024-04-18 op fatalx("bufio_read: %s", bufio_io_err(&bio));
982 a9d9f6e4 2024-04-18 op fatalx("unexpected EOF");
985 a9d9f6e4 2024-04-18 op line = buf_getdelim(&bio.rbuf, "\r\n", &len);
986 a9d9f6e4 2024-04-18 op if (line == NULL)
988 a9d9f6e4 2024-04-18 op if (response_code && *line == '\0') {
990 a9d9f6e4 2024-04-18 op * end of headers, don't bother
991 a9d9f6e4 2024-04-18 op * reading the body, if there is.
996 a9d9f6e4 2024-04-18 op if (response_code) {
997 5565365c 2024-03-27 op buf_drain(&bio.rbuf, len);
1000 a9d9f6e4 2024-04-18 op spc = strchr(line, ' ');
1001 a9d9f6e4 2024-04-18 op if (spc == NULL)
1002 a9d9f6e4 2024-04-18 op fatalx("bad HTTP response from server");
1003 a9d9f6e4 2024-04-18 op *spc++ = '\0';
1004 a9d9f6e4 2024-04-18 op if (strcasecmp(line, "HTTP/1.1") != 0)
1005 a9d9f6e4 2024-04-18 op log_warnx("unexpected protocol: %s", line);
1008 a9d9f6e4 2024-04-18 op spc = strchr(line, ' ');
1009 a9d9f6e4 2024-04-18 op if (spc == NULL)
1010 a9d9f6e4 2024-04-18 op fatalx("bad HTTP response from server");
1011 a9d9f6e4 2024-04-18 op *spc++ = '\0';
1013 a9d9f6e4 2024-04-18 op response_code = strtonum(line, 100, 599,
1015 a9d9f6e4 2024-04-18 op if (errstr != NULL)
1016 a9d9f6e4 2024-04-18 op log_warnx("response code is %s: %s",
1017 a9d9f6e4 2024-04-18 op errstr, line);
1019 a9d9f6e4 2024-04-18 op buf_drain(&bio.rbuf, len);
1024 5565365c 2024-03-27 op if (!feof(tmpfp) && bio.wbuf.len < sizeof(buf)) {
1025 5565365c 2024-03-27 op len = fread(buf, 1, sizeof(buf), tmpfp);
1026 5565365c 2024-03-27 op if (len == 0) {
1027 5565365c 2024-03-27 op if (ferror(tmpfp))
1028 cb29e255 2024-04-18 stsp fatal("fread");
1032 5565365c 2024-03-27 op if (bufio_compose(&bio, buf, len) == -1)
1033 cb29e255 2024-04-18 stsp fatal("buf_compose");
1037 3b44bdbe 2024-03-27 op if (response_code >= 200 && response_code < 300)
1039 cb29e255 2024-04-18 stsp fatal("request failed with code %d", response_code);