Integrate test_header in test_view_mail and various coding style modifications
[rmail.git] / src / network / test_pop3.c
blobcbc5d7a046be80cd5c3bb0f792dbeed0d7a3f9d4
1 #include <stdio.h>
2 #include <unistd.h>
4 #include "pop3.h"
6 /* Contains default for connections */
7 #include "pop3_config.h"
9 #define CMD_TOP 0
10 #define CMD_RETR 1
12 void usage(const char *appname)
14 fprintf(stderr, "Usage:\n%s [parameters]\n", appname);
15 fprintf(stderr, "Parameters:\n");
16 fprintf(stderr, " -h <host> Server Hostname or IP address\n"
17 " -p <port> Server Port\n"
18 " -u <user> Username\n"
19 " -P <password> Password\n"
20 " -s Use SSL connection\n"
21 " -r RETR command instead TOP\n");
24 int main(int argc, char **argv)
26 int opt;
27 int rc = -1;
29 char *hostname = NULL;
30 char *port = NULL;
31 char *user = NULL;
32 char *pass = NULL;
34 unsigned int authentication_type = POP3_AUTH_USERPASS;
36 /* POP3 Command used to test */
37 unsigned int pop3_command = CMD_TOP;
39 struct pop3_mailbox mb;
40 unsigned int n;
42 while ((opt = getopt(argc, argv, "u:h:p:P:sr")) != -1) {
43 switch (opt) {
44 case 'h':
45 hostname = optarg;
46 break;
47 case 'p':
48 port = optarg;
49 break;
50 case 'P':
51 pass = optarg;
52 break;
53 case 'r':
54 pop3_command = CMD_RETR;
55 break;
56 case 's':
57 authentication_type = POP3_AUTH_SSL;
58 break;
59 case 'u':
60 user = optarg;
61 break;
62 default:
63 fprintf(stderr, "Unknown parameter.\n");
64 usage(argv[0]);
65 return -1;
69 if (!hostname) {
70 if (authentication_type != POP3_AUTH_SSL)
71 hostname = POP3_DEFAULT_HOST;
72 else
73 hostname = POP3_SSL_DEFAULT_HOST;
76 if (!port) {
77 if (authentication_type != POP3_AUTH_SSL)
78 port = POP3_DEFAULT_PORT;
79 else
80 port = POP3_SSL_DEFAULT_PORT;
83 if (!user) {
84 if (authentication_type != POP3_AUTH_SSL)
85 user = POP3_DEFAULT_USER;
86 else
87 user = POP3_SSL_DEFAULT_USER;
90 if (!pass) {
91 if (authentication_type != POP3_AUTH_SSL)
92 pass = POP3_SSL_DEFAULT_PASSWORD;
93 else
94 pass = POP3_DEFAULT_PASSWORD;
97 mb.host.name = hostname;
98 mb.host.port = port;
100 mb.auth.type = authentication_type;
102 mb.auth.user = user;
103 mb.auth.pass = pass;
105 fprintf(stderr, "Connecting %s to %s:%s...\n", user, hostname, port);
107 if (pop3_connect(&mb) < 0) {
108 fprintf(stderr, "Connect error\n");
109 return -1;
112 if (pop3_authenticate(&mb) < 0) {
113 goto app_out;
116 /* Retrieve the server capabilities */
117 #if 10
118 if (pop3_capa(&mb) < 0) {
119 goto app_out;
121 #endif
123 /* Retrieve the number of messages present on the mailbox */
124 if (pop3_stat(&mb) < 0) {
125 goto app_out;
128 fprintf(stderr, "%u Message in maildrop\n", mb.messages);
130 /* Retrieve the message list */
131 if (mb.messages > 0) {
132 rc = pop3_list(&mb);
133 if (rc < 0)
134 goto app_out;
136 for (n = 1; n <= mb.messages; n++) {
137 if (CMD_RETR == pop3_command) {
138 rc = pop3_retr(&mb, n);
139 } else {
140 rc = pop3_top(&mb, n);
142 if (rc < 0) {
143 fprintf(stderr, "%s - ERROR n=%u\n", __func__, n);
144 break;
149 app_out:
150 pop3_disconnect(&mb);
151 return rc;