Repository navigation
Expand file tree
/
Copy pathmail.c
More file actions
494 lines (450 loc) · 14.3 KB
/
Copy pathmail.c
File metadata and controls
494 lines (450 loc) · 14.3 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/* mail.c - RFC 822 in and out, Maildir on disk. hai's conversations are
* mail: every turn is one message file (the headers it adds are the
* Hai-* ones, see MAIL.md), the way out is a sendmail-shaped command
* (`hml send -t` by default, whose local delivery puts @hai addresses
* into the same kind of Maildir), and the way in is files appearing in
* a Maildir's new/. Standard formats only; knows nothing about the loop
* or the daemon. */
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/inotify.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "hai.h"
#include "stb_ds.h"
const char *mail_sendcmd = "hml send -t";
const char *mail_from = "main@hai";
static int seq; /* per process, for ids and file names */
static const char *domain(void) {
const char *at = strrchr(mail_from, '@');
return at ? at + 1 : "hai";
}
static const char *shorthost(void) {
static char host[64];
if (!host[0]) {
char *dot;
if (gethostname(host, sizeof(host) - 1) < 0)
strcpy(host, "localhost");
host[sizeof(host) - 1] = '\0';
if ((dot = strchr(host, '.')))
*dot = '\0';
}
return host;
}
void mail_mid(char *dst, size_t n) {
snprintf(dst, n, "<%ld.%d.%d@%s>", (long)time(NULL), (int)getpid(), ++seq,
domain());
}
/* every address in a comma-separated list is @our domain */
int mail_islocal(const char *addrs) {
const char *p = addrs, *seg, *e, *at, *dom = domain();
size_t n = strlen(dom);
int any = 0;
while (*p) {
while (*p == ' ' || *p == ',')
p++;
if (!*p)
break;
seg = strchr(p, ',');
if (!seg)
seg = p + strlen(p);
for (at = seg; at > p && *at != '@'; at--)
;
if (*at != '@')
return 0;
for (e = seg; e > at && (e[-1] == ' ' || e[-1] == '>'); e--)
;
if ((size_t)(e - at - 1) != n || strncasecmp(at + 1, dom, n))
return 0;
any = 1;
p = seg;
}
return any;
}
/* --- composing --- */
/* one header line's worth: no line breaks, capped */
static void oneline(char *dst, size_t n, const char *s) {
size_t i;
for (i = 0; i + 1 < n && s && s[i]; i++)
dst[i] = (s[i] == '\n' || s[i] == '\r') ? ' ' : s[i];
dst[i] = '\0';
}
static void hdr(FILE *out, const char *name, const char *v) {
char line[1024];
if (!v || !*v)
return;
oneline(line, sizeof(line), v);
fprintf(out, "%s: %s\n", name, line);
}
static const char *intentof(const MailHdr *h, const Msg *m) {
if (h->intent)
return h->intent;
if (m->role && !strcmp(m->role, "system"))
return "system";
if (arrlen(m->calls))
return "tool-call";
if (m->callid)
return "tool-result";
if (m->summarizes)
return "summary";
return "message";
}
void mail_compose(FILE *out, const MailHdr *h, const Msg *m) {
char date[64], refs[512], boundary[64];
time_t now = time(NULL);
int multi = arrlen(m->calls) > 0;
strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %z", localtime(&now));
hdr(out, "From", h->from);
hdr(out, "To", h->to);
hdr(out, "Subject", h->subject);
hdr(out, "Date", h->date ? h->date : date);
hdr(out, "Message-ID", h->mid);
hdr(out, "In-Reply-To", h->inreplyto);
if (h->references)
hdr(out, "References", h->references);
else if (h->root || h->inreplyto) {
snprintf(refs, sizeof(refs), "%s%s%s", h->root ? h->root : "",
h->root && h->inreplyto && strcmp(h->root, h->inreplyto) ? " "
: "",
h->inreplyto && (!h->root || strcmp(h->root, h->inreplyto))
? h->inreplyto
: "");
hdr(out, "References", refs);
}
fputs("MIME-Version: 1.0\n", out);
if (multi) {
snprintf(boundary, sizeof(boundary), "hai-%ld-%d-%d", (long)now,
(int)getpid(), ++seq);
fprintf(out, "Content-Type: multipart/mixed; boundary=\"%s\"\n",
boundary);
} else
fputs("Content-Type: text/plain; charset=utf-8\n", out);
fputs("Content-Transfer-Encoding: 8bit\n", out);
hdr(out, "Hai-Conversation", h->root);
hdr(out, "Hai-Role", m->role);
hdr(out, "Hai-Intent", intentof(h, m));
hdr(out, "Hai-Call-Id", m->callid);
hdr(out, "Hai-Tool", m->name);
hdr(out, "Hai-Summarizes", m->summarizes);
hdr(out, "Hai-At", h->at);
hdr(out, "Hai-Every", h->every);
fputc('\n', out);
if (!multi) {
fputs(m->content ? m->content : "", out);
return;
}
/* part 1 for people: the text and one line per call; part 2 for
* hai: the message as the wire would carry it, verbatim */
{
cJSON *j = msg_json(m);
char *js = cJSON_PrintUnformatted(j);
ptrdiff_t i;
fprintf(out,
"--%s\nContent-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n\n",
boundary);
if (m->content && *m->content)
fprintf(out, "%s\n", m->content);
for (i = 0; i < arrlen(m->calls); i++)
fprintf(out, "-> %s %s\n", m->calls[i].name, m->calls[i].args);
fprintf(out, "--%s\nContent-Type: application/vnd.hai.message+json\n\n",
boundary);
fprintf(out, "%s\n--%s--\n", js, boundary);
free(js);
cJSON_Delete(j);
}
}
int mail_send(const MailHdr *h, const Msg *m, char *err, size_t errlen) {
MailHdr hh = *h;
char mid[128];
FILE *p;
int st;
if (!mail_sendcmd || !*mail_sendcmd) {
snprintf(err, errlen, "no sendcmd configured");
return -1;
}
if (!hh.mid) {
mail_mid(mid, sizeof(mid));
hh.mid = mid;
}
if (!(p = popen(mail_sendcmd, "w"))) {
snprintf(err, errlen, "%s: %s", mail_sendcmd, strerror(errno));
return -1;
}
mail_compose(p, &hh, m);
st = pclose(p);
if (st != 0) {
snprintf(err, errlen, "%s: exit %d", mail_sendcmd,
WIFEXITED(st) ? WEXITSTATUS(st) : -1);
return -1;
}
return 0;
}
/* into boxdir/<sub>/ by way of tmp/, the Maildir way; suffix is the
* flags a seen message carries in cur/ */
static int deposit(const char *boxdir, const char *sub, const char *suffix,
const MailHdr *h, const Msg *m, char *name, size_t n) {
char tmp[4300], dst[4300], fname[128];
FILE *f;
snprintf(fname, sizeof(fname), "%ld.%d_%06d.%s", (long)time(NULL),
(int)getpid(), ++seq, shorthost());
snprintf(tmp, sizeof(tmp), "%.4000s/tmp/%s", boxdir, fname);
snprintf(dst, sizeof(dst), "%.4000s/%s/%s%s", boxdir, sub, fname, suffix);
if (!(f = fopen(tmp, "w")))
return -1;
mail_compose(f, h, m);
if (fclose(f) != 0 || rename(tmp, dst) < 0) {
unlink(tmp);
return -1;
}
if (name)
snprintf(name, n, "%s%s", fname, suffix);
return 0;
}
int mail_write(const char *boxdir, const MailHdr *h, const Msg *m, char *name,
size_t n) {
return deposit(boxdir, "cur", ":2,S", h, m, name, n);
}
int mail_deliver(const char *boxdir, const MailHdr *h, const Msg *m) {
return deposit(boxdir, "new", "", h, m, NULL, 0);
}
/* --- parsing --- */
/* a header's value, unfolded; malloc'd or NULL. Stops at the blank line */
static char *header(const char *msg, const char *name) {
size_t n = strlen(name);
const char *p = msg, *e;
char *v, *w;
while (*p && *p != '\n' && !(p[0] == '\r' && p[1] == '\n')) {
e = strchr(p, '\n');
if (!e)
e = p + strlen(p);
if (!strncasecmp(p, name, n) && p[n] == ':') {
const char *s = p + n + 1;
while (*e == '\n' && (e[1] == ' ' || e[1] == '\t')) /* folded */
e = strchr(e + 1, '\n') ? strchr(e + 1, '\n') : e + strlen(e);
while (*s == ' ' || *s == '\t')
s++;
v = malloc((size_t)(e - s) + 1);
for (w = v; s < e; s++)
if (*s != '\r' && *s != '\n')
*w++ = *s;
*w = '\0';
while (w > v && isspace((unsigned char)w[-1]))
*--w = '\0';
return v;
}
p = *e ? e + 1 : e;
}
return NULL;
}
static const char *body(const char *msg) {
const char *p = strstr(msg, "\n\n"), *q = strstr(msg, "\n\r\n");
if (q && (!p || q < p))
return q + 3;
return p ? p + 2 : msg + strlen(msg);
}
static const char *rolename(const char *s) {
static const char *const roles[] = {"system", "user", "assistant", "tool"};
size_t i;
for (i = 0; s && i < 4; i++)
if (!strcmp(s, roles[i]))
return roles[i];
return NULL;
}
/* the JSON part of a hai multipart: from the second boundary's blank
* line to the closing boundary; malloc'd or NULL */
static char *jsonpart(const char *b, const char *boundary) {
char mark[80];
const char *p, *e;
size_t n;
snprintf(mark, sizeof(mark), "--%s", boundary);
n = strlen(mark);
if (!(p = strstr(b, mark)) || !(p = strstr(p + n, mark)))
return NULL;
if (!(p = strstr(p, "\n\n")))
return NULL;
p += 2;
if (!(e = strstr(p, mark)))
e = p + strlen(p);
while (e > p && (e[-1] == '\n' || e[-1] == '\r'))
e--;
{
char *r = malloc((size_t)(e - p) + 1);
memcpy(r, p, (size_t)(e - p));
r[e - p] = '\0';
return r;
}
}
int mail_parse(const char *path, Msg *m, MailHdr *h) {
char *s, *ct, *bnd = NULL;
const char *b;
memset(m, 0, sizeof(*m));
memset(h, 0, sizeof(*h));
if (!(s = readfile(path, 0)))
return -1;
h->from = header(s, "From");
h->to = header(s, "To");
h->subject = header(s, "Subject");
h->mid = header(s, "Message-ID");
h->inreplyto = header(s, "In-Reply-To");
h->references = header(s, "References");
h->root = header(s, "Hai-Conversation");
h->intent = header(s, "Hai-Intent");
h->date = header(s, "Date");
h->role = header(s, "Hai-Role");
h->at = header(s, "Hai-At");
h->every = header(s, "Hai-Every");
m->role = rolename(h->role);
m->callid = header(s, "Hai-Call-Id");
m->name = header(s, "Hai-Tool");
m->summarizes = header(s, "Hai-Summarizes");
m->mid = h->mid ? xstrdup(h->mid) : NULL;
b = body(s);
if ((ct = header(s, "Content-Type"))) {
char *q = strstr(ct, "boundary=");
if (!strncasecmp(ct, "multipart/", 10) && q) {
char *e;
q += 9;
if (*q == '"')
q++;
for (e = q; *e && *e != '"' && *e != ';' && *e != ' '; e++)
;
*e = '\0';
bnd = xstrdup(q);
}
free(ct);
}
if (bnd) {
char *js = jsonpart(b, bnd);
cJSON *j = js ? cJSON_Parse(js) : NULL, *c;
if (jstr(j, "content"))
m->content = xstrdup(jstr(j, "content"));
cJSON_ArrayForEach(c, jget(j, "tool_calls")) {
ToolCall tc;
if (!jget(c, "function"))
continue;
tc.id = xstrdup(jstr(c, "id"));
tc.name = xstrdup(jstr(c, "function.name"));
tc.args = xstrdup(jstr(c, "function.arguments"));
arrput(m->calls, tc);
}
if (!j) /* not ours: hand the body over as it is */
m->content = xstrdup(b);
cJSON_Delete(j);
free(js);
free(bnd);
} else
m->content = xstrdup(b);
free(s);
return 0;
}
void mailhdr_free(MailHdr *h) {
free((char *)h->from);
free((char *)h->to);
free((char *)h->subject);
free((char *)h->mid);
free((char *)h->inreplyto);
free((char *)h->references);
free((char *)h->root);
free((char *)h->intent);
free((char *)h->date);
free((char *)h->role);
free((char *)h->at);
free((char *)h->every);
memset(h, 0, sizeof(*h));
}
char *mail_header(const char *path, const char *name) {
char *s = readfile(path, 8192), *v;
if (!s)
return NULL;
v = header(s, name);
free(s);
return v;
}
int mail_peek(const char *path, const char *const *names, char **vals, int n) {
char *s = readfile(path, 8192);
int i;
for (i = 0; i < n; i++)
vals[i] = s ? header(s, names[i]) : NULL;
free(s);
return s ? 0 : -1;
}
int mail_answers(const char *path, const char *mid) {
char *s = readfile(path, 8192), *v;
int hit = 0;
if (!s)
return 0;
if ((v = header(s, "In-Reply-To")))
hit = strstr(v, mid) != NULL, free(v);
if (!hit && (v = header(s, "References")))
hit = strstr(v, mid) != NULL, free(v);
free(s);
return hit;
}
/* --- Maildirs --- */
int mail_mkdirs(const char *dir) {
char p[4200];
size_t i, n = strlen(dir);
if (!n || n > 4000)
return -1;
memcpy(p, dir, n + 1);
for (i = 1; i <= n; i++) { /* the parents too */
if (i < n && p[i] != '/')
continue;
p[i] = '\0';
if (mkdir(p, 0700) < 0 && errno != EEXIST)
return -1;
if (i < n)
p[i] = '/';
}
return 0;
}
int mail_mkbox(const char *boxdir) {
static const char *const sub[] = {"cur", "new", "tmp"};
char p[4200];
size_t i;
if (mail_mkdirs(boxdir) < 0)
return -1;
for (i = 0; i < 3; i++) {
snprintf(p, sizeof(p), "%s/%s", boxdir, sub[i]);
if (mkdir(p, 0700) < 0 && errno != EEXIST)
return -1;
}
return 0;
}
int mail_watch(const char *boxdir) {
char p[4200];
int fd;
snprintf(p, sizeof(p), "%.4000s/new", boxdir);
if ((fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC)) < 0)
return -1;
if (inotify_add_watch(fd, p, IN_MOVED_TO | IN_CREATE | IN_CLOSE_WRITE) <
0) {
close(fd);
return -1;
}
return fd;
}
static int bystr(const void *a, const void *b) {
return strcmp(*(char *const *)a, *(char *const *)b);
}
char **mail_names(const char *dir) {
char **names = NULL;
struct dirent *d;
DIR *dp;
if (!(dp = opendir(dir)))
return NULL;
while ((d = readdir(dp)))
if (d->d_name[0] != '.')
arrput(names, xstrdup(d->d_name));
closedir(dp);
if (arrlen(names) > 1)
qsort(names, (size_t)arrlen(names), sizeof(names[0]), bystr);
return names;
}