forked from theappleman/dmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpop3.c
110 lines (102 loc) · 2.53 KB
/
pop3.c
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
/* dmc - dynamic mail client
* See LICENSE file for copyright and license details.
*/
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <poll.h>
#include "sock.c"
static char *cmd = NULL;
static char word[1024];
static char *getword() {
fscanf (stdin, "%255s", word);
if (feof (stdin))
*word = '\0';
return word;
}
static int waitreply(int res) {
char result[1024];
char *ch, *str;
int reply = -1;
ftruncate (2, 0);
lseek (2, 0, SEEK_SET);
result[0] = '\0';
while (sock_ready () && sock_read (word, 512) > 1) {
str = word;
if (reply == -1 && (reply = (word[0] == '+'))) {
if ((ch = strchr (str, '\r')) || (ch = strchr (str, '\n'))) {
*ch = '\0';
snprintf (result, 1023, "### %s %d \"%s\"\n", cmd, reply, str);
str = ch + (ch[1] == '\n' ? 2 : 1);
}
}
// TODO: Fix possible \r\n issues
if ((ch = strstr (str, "\r\n.")))
*ch = '\0';
write (2, str, strlen (str));
}
write (2, "\n", 1);
if (res) {
if (result[0] == '\0')
snprintf (result, 1023, "### %s %d \"\"\n", cmd, reply);
write (1, result, strlen (result));
}
return reply;
}
static int doword(char *word) {
int ret = 1;
free (cmd);
cmd = strdup (word);
if (*word == '\0') {
/* Do nothing */
} else
if (!strcmp (word, "exit")) {
sock_printf ("QUIT\n");
waitreply (1);
ret = 0;
} else
if (!strcmp (word, "help") || !strcmp (word, "?")) {
printf ("Use: ls cat head rm login exit\n");
} else
if (!strcmp (word, "ls")) {
sock_printf ("LIST\n");
waitreply (1);
} else
if (!strcmp (word, "cat")) {
sock_printf ("RETR %d\n", atoi (getword ()));
waitreply (1);
} else
if (!strcmp (word, "head")) {
sock_printf ("TOP %d 50\n", atoi (getword ()));
waitreply (1);
} else
if (!strcmp (word, "rm")) {
sock_printf ("DELE %d\n", atoi (getword ()));
waitreply (1);
} else
if (!strcmp (word, "login")) {
sock_printf ("USER %s\n", getword ());
waitreply (0); // TODO: if user fail, do not send pass
sock_printf ("PASS %s\n", getword ());
waitreply (1);
} else sock_printf ("NOOP\n");
return ret;
}
int main(int argc, char **argv) {
int ssl = 0, ret = 1;
if (argc > 2) {
if (argc > 3)
ssl = (*argv[3] == '1');
if (sock_connect (argv[1], atoi (argv[2]), ssl) >= 0) {
ret = atexit (sock_close);
waitreply (1);
while (doword (getword ()));
} else printf ("Cannot connect to %s %d\n", argv[1], atoi (argv[2]));
} else printf ("Usage: dmc-pop3 host port [ssl] 2> body > fifo < input\n");
return 0;
}