15 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
16 #define MAILBUF (1 << 16)
17 #define MAXLINE (1 << 7)
18 #define BUFSIZE (1 << 12)
20 static struct mbox
*mbox
;
21 static struct sort
*sort
;
23 static int sel
[MAXMAILS
];
27 static int read_line(char *dst
, int size
)
29 static char buf
[BUFSIZE
];
34 int cur_len
= MIN(len
- cur
, size
- nw
- 1);
35 char *nl
= memchr(buf
+ cur
, '\n', cur_len
);
36 int nr
= nl
? nl
- buf
- cur
+ 1 : cur_len
;
38 memcpy(dst
+ nw
, buf
+ cur
, nr
);
43 if (nl
|| nw
== size
- 1)
46 if ((len
= read(STDIN_FILENO
, buf
, BUFSIZE
)) <= 0)
52 static char *till_eol(char *r
, int len
, char *s
)
55 while (r
< d
&& s
&& *s
&& *s
!= '\r' && *s
!= '\n')
60 static int msg_num(char *num
)
63 if (!*num
|| !strcmp(".", num
))
67 if (!strcmp("$", num
))
70 n
= cur
- (*(num
+ 1) ? atoi(num
+ 1) : 1);
72 n
= cur
+ (*(num
+ 1) ? atoi(num
+ 1) : 1);
73 if (!strcmp(",", num
)) {
75 while (++i
< sort
->n
) {
76 int stat
= sort
->mails
[i
]->stat
;
77 if (!(stat
& STAT_READ
) && (stat
& STAT_NEW
)) {
83 if (n
< 0 || n
>= mbox
->n
)
88 static int stat_char(struct mail
*mail
)
90 if (mail
->stat
& STAT_NEW
)
91 return mail
->stat
& STAT_READ
? 'R' : 'N';
93 return mail
->stat
& STAT_READ
? ' ' : 'U';
96 static int search(char *args
, int all
)
98 static char hdr_name
[MAXLINE
];
99 static char hdr_val
[MAXLINE
];
101 char *beg
= strchr(args
, '(');
102 char *spc
= beg
? strchr(beg
, ' ') : NULL
;
103 char *end
= spc
? strchr(spc
, ')') : NULL
;
105 if (beg
&& (!end
|| !spc
)) {
109 int hdr_len
= spc
- beg
- 1;
110 put_mem(hdr_name
, beg
+ 1, hdr_len
);
111 hdr_name
[hdr_len
] = '\0';
112 while (isspace(*spc
))
114 put_mem(hdr_val
, spc
, end
- spc
);
115 hdr_val
[end
- spc
] = '\0';
116 stat
= isalpha(*(args
+ 1)) ? toupper(*(args
+ 1)) : 0;
118 for (i
= all
? 0 : cur
+ 1; i
< sort
->n
; i
++) {
119 char *hdr
= mail_hdr(sort
->mails
[i
], hdr_name
);
120 char *s
= hdr
? hdr
+ strlen(hdr_name
) : NULL
;
121 char *d
= hdr
? hdr
+ hdr_len(hdr
) : NULL
;
123 if (!strncmp(hdr_val
, s
, strlen(hdr_val
)) &&
124 (!stat
|| stat_char(sort
->mails
[i
]) == stat
)) {
132 s
= memchr(s
+ 1, hdr_val
[0], d
- s
- 1);
138 static int sel_msgs(char *args
, int all
)
143 while (isspace(*args
))
146 return search(args
, all
);
147 args
= cut_word(num
, args
);
152 if (*num
&& (dash
= strchr(num
+ 1, '-'))) {
153 char beg_str
[MAXLINE
];
154 memcpy(beg_str
, num
, dash
- num
);
155 beg_str
[dash
- num
] = '\0';
156 beg
= msg_num(beg_str
);
157 end
= msg_num(dash
+ 1);
162 if (beg
!= -1 && end
!= -1) {
167 for (i
= beg
; i
<= end
; i
++)
170 args
= cut_word(num
, args
);
177 static void show_mail(char *args
, int filthdr
)
181 char *pg_args
[] = {PAGER
, NULL
};
183 if (!sel_msgs(args
, 0))
185 mail
= sort
->mails
[cur
];
186 mail
->stat
|= STAT_READ
;
188 s
+= mail_head(mail
, buf
, sizeof(buf
),
189 hdr_filt
, ARRAY_SIZE(hdr_filt
));
190 s
= put_mem(s
, mail
->body
, MIN(sizeof(buf
) - (s
- buf
),
193 s
= put_mem(s
, mail
->head
, mail
->len
);
195 exec_pipe(PAGER
, pg_args
, buf
, s
- buf
);
198 static void cmd_page(char *args
)
203 static void cmd_cat(char *args
)
208 static char *cut_cmd(char *dst
, char *s
)
210 while (isalpha(*s
)) {
217 static char *put_hdr(struct mail
*mail
, char *name
, int w
, char *s
)
219 char *hdr
= mail_hdr(mail
, name
);
220 return till_eol(s
, w
, hdr
? hdr
+ strlen(name
) + 1 : NULL
);
223 static void cmd_head(char *args
)
227 if (!sel_msgs(args
, 0))
229 beg
= cur
/ NHEAD
* NHEAD
;
230 end
= MIN(beg
+ NHEAD
, mbox
->n
);
231 for (i
= beg
; i
< end
; i
++) {
232 struct mail
*mail
= sort
->mails
[i
];
235 *s
++ = i
== cur
? '>' : ' ';
236 *s
++ = mail
->stat
& STAT_DEL
? 'D' : ' ';
237 *s
++ = stat_char(mail
);
238 s
= put_int(s
, i
, DIGITS
);
241 s
= put_hdr(mail
, "From:", 16, s
);
244 s
= sort_draw(sort
, i
, s
, WIDTH
- (s
- fmt
));
245 s
= put_hdr(mail
, "Subject:", WIDTH
- (s
- fmt
), s
);
247 write(STDOUT_FILENO
, fmt
, s
- fmt
);
251 static void cmd_z(char *args
)
257 page
= MIN(cur
+ NHEAD
, mbox
->n
) / NHEAD
;
259 page
= cur
/ NHEAD
- (*(num
+ 1) ? atoi(num
+ 1) : 1);
261 page
= cur
/ NHEAD
+ (*(num
+ 1) ? atoi(num
+ 1) : 1);
263 page
= mbox
->n
/ NHEAD
;
266 if (page
>= 0 && page
* NHEAD
< mbox
->n
) {
272 static void cmd_del(char *args
)
275 if (!sel_msgs(args
, 1))
277 for (i
= 0; i
< nsel
; i
++)
278 sort
->mails
[sel
[i
]]->stat
|= STAT_DEL
;
281 static void cmd_undel(char *args
)
284 if (!sel_msgs(args
, 1))
286 for (i
= 0; i
< nsel
; i
++)
287 sort
->mails
[sel
[i
]]->stat
&= ~STAT_DEL
;
290 static void print(char *s
)
292 write(STDOUT_FILENO
, s
, strlen(s
));
295 static int is_mbox(char *filename
)
299 return S_ISREG(st
.st_mode
);
302 static char *filename(char *path
)
304 char *slash
= strrchr(path
, '/');
305 return slash
? slash
+ 1 : path
;
308 static void sum_mbox(void)
316 s
= put_str(s
, filename(mbox
->path
));
317 s
= put_str(s
, ": ");
318 s
= put_int(s
, mbox
->n
, DIGITS
);
319 s
= put_str(s
, " messages ");
320 for (i
= 0; i
< mbox
->n
; i
++) {
321 if (!(mbox
->mails
[i
].stat
& STAT_READ
)) {
323 if (mbox
->mails
[i
].stat
& STAT_NEW
)
326 if (mbox
->mails
[i
].stat
& STAT_DEL
)
330 s
= put_int(s
, new, DIGITS
);
331 s
= put_str(s
, " new ");
334 s
= put_int(s
, unread
, DIGITS
);
335 s
= put_str(s
, " unread ");
338 s
= put_int(s
, del
, DIGITS
);
339 s
= put_str(s
, " deleted ");
341 s
= put_str(s
, "\n");
345 static void open_mbox(char *filename
)
347 mbox
= mbox_alloc(filename
);
348 sort
= sort_alloc(mbox
, THREADED
? SORT_THREAD
: 0);
353 static void close_mbox(void)
359 static int has_mail(char *path
)
362 if (stat(path
, &st
) == -1)
364 return st
.st_mtime
> st
.st_atime
;
367 static int mbox_path(char *path
, char *addr
)
372 s
= put_str(s
, FOLDER
);
373 s
= put_str(s
, addr
+ 1);
376 for (i
= 0; i
< ARRAY_SIZE(boxes
); i
++) {
377 if (has_mail(boxes
[i
])) {
378 s
= put_str(s
, boxes
[i
]);
383 if (!strcmp(".", addr
) && mbox
)
384 s
= put_str(s
, mbox
->path
);
385 if (s
== path
&& *addr
)
386 s
= put_str(s
, addr
);
390 static void warn_nomem(void)
392 print("no mem for new msgs\n");
395 static void cmd_fold(char *args
)
397 char filename
[MAXLINE
];
398 args
= cut_word(filename
, args
);
400 char path
[MAXPATHLEN
];
401 if (mbox_path(path
, filename
) && is_mbox(path
)) {
402 if (mbox_write(mbox
) == -1) {
414 static void cmd_news(char *args
)
419 for (i
= 0; i
< ARRAY_SIZE(boxes
); i
++) {
420 if (has_mail(boxes
[i
])) {
422 s
= put_str(s
, "\t");
423 s
= put_str(s
, filename(boxes
[i
]));
424 s
= put_str(s
, "\n");
430 static void cmd_inc(char *args
)
433 int new = mbox_inc(mbox
);
440 s
= put_int(s
, new, DIGITS
);
441 s
= put_str(s
, " new\n");
446 static void cmd_next(char *args
)
451 if (cur
+ 1 < mbox
->n
) {
461 static void copy_mail(char *args
, int del
)
466 char *spc
= strrchr(args
, ' ');
470 if (!sel_msgs(args
, 1) || !*last
)
472 mbox_path(path
, last
);
473 fd
= open(path
, O_WRONLY
| O_APPEND
| O_CREAT
, S_IRUSR
| S_IWUSR
);
475 print("failed to open mbox for writing\n");
478 for (i
= 0; i
< nsel
; i
++) {
479 struct mail
*mail
= sort
->mails
[sel
[i
]];
480 mail_write(mail
, fd
);
482 mail
->stat
|= STAT_DEL
;
487 static void cmd_copy(char *args
)
492 static void cmd_move(char *args
)
497 static void compose(struct draft
*draft
)
499 char record
[MAXPATHLEN
] = "";
502 char *pg_args
[] = {PAGER
, NULL
};
504 mbox_path(record
, RECORD
);
506 strcpy(record
, mbox
->path
);
507 while (read_line(line
, sizeof(line
)) > 0) {
509 if (!strcmp("~e", cmd
))
511 if (!strcmp("~.", cmd
)) {
513 draft_save(draft
, record
);
517 if (!strcmp("~p", cmd
))
518 exec_pipe(PAGER
, pg_args
, draft
->mail
, draft
->len
);
519 if (!strcmp("~q", cmd
) || !strcmp("~x", cmd
))
524 static void cmd_mail(char *args
)
527 while (isspace(*args
))
529 draft_init(&draft
, *args
? &args
: NULL
, *args
? 1 : 0, NULL
);
533 static void cmd_reply(char *args
)
536 if (!sel_msgs(args
, 0))
538 draft_reply(&draft
, sort
->mails
[cur
]);
542 static void prompt(void)
544 write(STDOUT_FILENO
, "? ", 2);
547 static void cmd_quit(char *args
)
549 if (mbox_write(mbox
) == -1)
555 static void cmd_exit(char *args
)
562 void (*cmd
)(char *args
);
566 {"header", cmd_head
},
569 {"folder", cmd_fold
},
577 {"reply", cmd_reply
},
581 {"undelete", cmd_undel
},
588 static void loop(void)
593 while (read_line(line
, sizeof(line
)) > 0) {
594 char *args
= cut_cmd(cmd
, line
);
595 int len
= strlen(cmd
);
600 for (i
= 0; i
< ARRAY_SIZE(cmds
); i
++)
601 if (!strncmp(cmds
[i
].name
, cmd
, len
)) {
612 int main(int argc
, char *argv
[])
615 char *filename
= NULL
;
618 if (argv
[i
][0] != '-')
620 if (!strcmp("-f", argv
[i
]))
621 filename
= argv
[++i
];
622 if (!strcmp("-s", argv
[i
]))
626 char path
[MAXPATHLEN
];
627 if (mbox_path(path
, filename
) && is_mbox(path
)) {
634 draft_init(&draft
, argv
+ i
, argc
- i
, subj
);