pop3client.c
16.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/* pop3client.c -- An application which demonstrates how to use the
GNU Mailutils pop3 functions. This application interactively allows users
to contact a pop3 server.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <termios.h>
#include <signal.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <mailutils/pop3.h>
#include <mailutils/iterator.h>
/* A structure which contains information on the commands this program
can understand. */
typedef struct {
const char *name; /* User printable name of the function. */
int (*func) (char *); /* Function to call to do the job. */
const char *doc; /* Documentation for this function. */
} COMMAND;
/* The names of functions that actually do the manipulation. */
int com_apop (char *);
int com_capa (char *);
int com_disconnect (char *);
int com_dele (char *);
int com_exit (char *);
int com_help (char *);
int com_list (char *);
int com_noop (char *);
int com_connect (char *);
int com_pass (char *);
int com_quit (char *);
int com_retr (char *);
int com_rset (char *);
int com_stat (char *);
int com_top (char *);
int com_uidl (char *);
int com_user (char *);
int com_verbose (char *);
void initialize_readline (void);
char *stripwhite (char *);
COMMAND *find_command (char *);
void *xmalloc (size_t);
char *dupstr (const char *);
int execute_line (char *);
int valid_argument (const char *, char *);
void sig_int (int);
COMMAND commands[] = {
{ "apop", com_apop, "Authenticate with APOP: APOP user secret" },
{ "capa", com_capa, "List capabilities: capa" },
{ "disconnect", com_disconnect, "Close connection: disconnect" },
{ "dele", com_dele, "Mark message: DELE msgno" },
{ "exit", com_exit, "exit program" },
{ "help", com_help, "Display this text" },
{ "?", com_help, "Synonym for `help'" },
{ "list", com_list, "List messages: LIST [msgno]" },
{ "noop", com_noop, "Send no operation: NOOP" },
{ "pass", com_pass, "Send passwd: PASS [passwd]" },
{ "connect", com_connect, "Open connection: connect hostname [port]" },
{ "quit", com_quit, "Go to Update state : QUIT" },
{ "retr", com_retr, "Dowload message: RETR msgno" },
{ "rset", com_rset, "Unmark all messages: RSET" },
{ "stat", com_stat, "Get the size and count of mailbox : STAT [msgno]" },
{ "top", com_top, "Get the header of message: TOP msgno [lines]" },
{ "uidl", com_uidl, "Get the uniq id of message: UIDL [msgno]" },
{ "user", com_user, "send login: USER user" },
{ "verbose", com_verbose, "Enable Protocol tracing: verbose [on|off]" },
{ NULL, NULL, NULL }
};
/* The name of this program, as taken from argv[0]. */
char *progname;
/* Global handle for pop3. */
mu_pop3_t pop3;
/* Flag if verbosity is needed. */
int verbose;
/* When non-zero, this global means the user is done using this program. */
int done;
#if 0
void *
xmalloc (size_t size)
{
void *m = malloc (size);
if (!m)
{
fprintf (stderr, "Memory exhausted\n");
exit (1);
}
return m;
}
#endif
char *
dupstr (const char *s)
{
char *r;
r = xmalloc (strlen (s) + 1);
strcpy (r, s);
return r;
}
int
main (int argc, char **argv)
{
char *line, *s;
(void)argc;
progname = strrchr (argv[0], '/');
if (progname)
progname++;
else
progname = argv[0];
initialize_readline (); /* Bind our completer. */
/* Loop reading and executing lines until the user quits. */
for ( ; done == 0; )
{
line = readline ((char *)"pop3> ");
if (!line)
break;
/* Remove leading and trailing whitespace from the line.
Then, if there is anything left, add it to the history list
and execute it. */
s = stripwhite (line);
if (*s)
{
int status;
add_history (s);
status = execute_line (s);
if (status != 0)
fprintf (stderr, "Error: %s\n", strerror(status));
}
free (line);
}
exit (0);
}
/* Parse and execute a command line. */
int
execute_line (char *line)
{
register int i;
COMMAND *command;
char *word;
/* Isolate the command word. */
i = 0;
while (line[i] && whitespace (line[i]))
i++;
word = line + i;
while (line[i] && !whitespace (line[i]))
i++;
if (line[i])
line[i++] = '\0';
command = find_command (word);
if (!command)
{
fprintf (stderr, "%s: No such command for %s.\n", word, progname);
return (-1);
}
/* Get argument to command, if any. */
while (whitespace (line[i]))
i++;
word = line + i;
/* Call the function. */
return ((*(command->func)) (word));
}
/* Look up NAME as the name of a command, and return a pointer to that
command. Return a NULL pointer if NAME isn't a command name. */
COMMAND *
find_command (name)
char *name;
{
register int i;
for (i = 0; commands[i].name; i++)
if (strcmp (name, commands[i].name) == 0)
return (&commands[i]);
return ((COMMAND *)NULL);
}
/* Strip whitespace from the start and end of STRING. Return a pointer
into STRING. */
char *
stripwhite (char *string)
{
register char *s, *t;
for (s = string; whitespace (*s); s++)
;
if (*s == 0)
return (s);
t = s + strlen (s) - 1;
while (t > s && whitespace (*t))
t--;
*++t = '\0';
return s;
}
/* **************************************************************** */
/* */
/* Interface to Readline Completion */
/* */
/* **************************************************************** */
char *command_generator (const char *, int);
char **pop_completion (char *, int, int);
/* Tell the GNU Readline library how to complete. We want to try to complete
on command names if this is the first word in the line, or on filenames
if not. */
void
initialize_readline ()
{
/* Allow conditional parsing of the ~/.inputrc file. */
rl_readline_name = (char *)"pop3";
/* Tell the completer that we want a crack first. */
rl_attempted_completion_function = (CPPFunction *)pop_completion;
}
/* Attempt to complete on the contents of TEXT. START and END bound the
region of rl_line_buffer that contains the word to complete. TEXT is
the word to complete. We can use the entire contents of rl_line_buffer
in case we want to do some simple parsing. Return the array of matches,
or NULL if there aren't any. */
char **
pop_completion (char *text, int start, int end)
{
char **matches;
(void)end;
matches = (char **)NULL;
/* If this word is at the start of the line, then it is a command
to complete. Otherwise it is the name of a file in the current
directory. */
if (start == 0)
matches = completion_matches (text, command_generator);
return (matches);
}
/* Generator function for command completion. STATE lets us know whether
to start from scratch; without any state (i.e. STATE == 0), then we
start at the top of the list. */
char *
command_generator (const char *text, int state)
{
static int list_index, len;
const char *name;
/* If this is a new word to complete, initialize now. This includes
saving the length of TEXT for efficiency, and initializing the index
variable to 0. */
if (!state)
{
list_index = 0;
len = strlen (text);
}
/* Return the next name which partially matches from the command list. */
while ((name = commands[list_index].name))
{
list_index++;
if (strncmp (name, text, len) == 0)
return (dupstr(name));
}
/* If no names matched, then return NULL. */
return ((char *)NULL);
}
int
com_verbose (char *arg)
{
int status = 0;
if (!valid_argument ("verbose", arg))
return EINVAL;
verbose = (strcmp (arg, "on") == 0);
if (pop3 != NULL)
{
if (verbose == 1)
{
mu_debug_t debug;
mu_debug_create (&debug, NULL);
mu_debug_set_level (debug, MU_DEBUG_PROT);
status = mu_pop3_set_debug (pop3, debug);
}
else
{
status = mu_pop3_set_debug (pop3, NULL);
}
}
return status;
}
int
com_user (char *arg)
{
if (!valid_argument ("user", arg))
return EINVAL;
return mu_pop3_user (pop3, arg);
}
int
com_apop (char *arg)
{
char *user, *digest;
if (!valid_argument ("apop", arg))
return EINVAL;
user = strtok (arg, " ");
digest = strtok (NULL, " ");
if (!valid_argument ("apop", user) || !valid_argument ("apop", digest))
return EINVAL;
return mu_pop3_apop (pop3, user, digest);
}
int
com_capa (char *arg)
{
list_t list = NULL;
int status = mu_pop3_capa (pop3, &list);
(void)arg;
if (status == 0)
{
iterator_t iterator = NULL;
iterator_create (&iterator, list);
for (iterator_first (iterator);
!iterator_is_done (iterator);
iterator_next (iterator))
{
char *capa = NULL;
iterator_current (iterator, &capa);
printf ("Capa: %s\n", (capa) ? capa : "");
}
iterator_destroy (&iterator);
list_destroy (&list);
}
return status;
}
int
com_uidl (char *arg)
{
int status = 0;
if (arg == NULL || *arg == '\0')
{
list_t list = NULL;
status = mu_pop3_uidl_all (pop3, &list);
if (status == 0)
{
iterator_t uidl_iterator = NULL;
iterator_create (&uidl_iterator, list);
for (iterator_first (uidl_iterator);
!iterator_is_done (uidl_iterator);
iterator_next (uidl_iterator))
{
char *uidl = NULL;
iterator_current (uidl_iterator, &uidl);
printf ("UIDL: %s\n", (uidl) ? uidl : "") ;
}
iterator_destroy (&uidl_iterator);
list_destroy(&list);
}
}
else
{
char *uidl = NULL;
unsigned int msgno = strtoul (arg, NULL, 10);
status = mu_pop3_uidl (pop3, msgno, &uidl);
if (status == 0)
printf ("Msg: %d UIDL: %s\n", msgno, (uidl) ? uidl : "");
free (uidl);
}
return status;
}
int
com_list (char *arg)
{
int status = 0;
if (arg == NULL || *arg == '\0')
{
list_t list = NULL;
status = mu_pop3_list_all (pop3, &list);
if (status == 0)
{
iterator_t list_iterator;
iterator_create (&list_iterator, list);
for (iterator_first (list_iterator);
!iterator_is_done (list_iterator);
iterator_next (list_iterator))
{
char *list = NULL;
iterator_current (list_iterator, &list);
printf ("LIST: %s\n", (list) ? list : "");
}
iterator_destroy (&list_iterator);
list_destroy (&list);
}
}
else
{
size_t size = 0;
unsigned int msgno = strtoul (arg, NULL, 10);
status = mu_pop3_list (pop3, msgno, &size);
if (status == 0)
printf ("Msg: %d Size: %d\n", msgno, size);
}
return status;
}
int
com_noop (char *arg)
{
(void)arg;
return mu_pop3_noop (pop3);
}
static void
echo_off(struct termios *stored_settings)
{
struct termios new_settings;
tcgetattr (0, stored_settings);
new_settings = *stored_settings;
new_settings.c_lflag &= (~ECHO);
tcsetattr (0, TCSANOW, &new_settings);
}
static void
echo_on(struct termios *stored_settings)
{
tcsetattr (0, TCSANOW, stored_settings);
}
int
com_pass (char *arg)
{
char pass[256];
if (!arg || *arg == '\0')
{
struct termios stored_settings;
printf ("passwd:");
fflush (stdout);
echo_off (&stored_settings);
fgets (pass, sizeof pass, stdin);
echo_on (&stored_settings);
putchar ('\n');
fflush (stdout);
pass [strlen (pass) - 1] = '\0'; /* nuke the trailing line. */
arg = pass;
}
return mu_pop3_pass (pop3, arg);
}
int
com_stat (char *arg)
{
unsigned count, size;
int status = 0;
(void)arg;
count = size = 0;
status = mu_pop3_stat (pop3, &count, &size);
fprintf (stdout, "Mesgs: %d Size %d\n", count, size);
return status;
}
int
com_dele (arg)
char *arg;
{
unsigned msgno;
if (!valid_argument ("dele", arg))
return EINVAL;
msgno = strtoul (arg, NULL, 10);
return mu_pop3_dele (pop3, msgno);
}
/* Print out help for ARG, or for all of the commands if ARG is
not present. */
int
com_help (char *arg)
{
int i;
int printed = 0;
for (i = 0; commands[i].name; i++)
{
if (!*arg || (strcmp (arg, commands[i].name) == 0))
{
printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc);
printed++;
}
}
if (!printed)
{
printf ("No commands match `%s'. Possibilties are:\n", arg);
for (i = 0; commands[i].name; i++)
{
/* Print in six columns. */
if (printed == 6)
{
printed = 0;
printf ("\n");
}
printf ("%s\t", commands[i].name);
printed++;
}
if (printed)
printf ("\n");
}
return 0;
}
int
com_rset (char *arg)
{
(void)arg;
return mu_pop3_rset (pop3);
}
int
com_top (char *arg)
{
stream_t stream;
unsigned int msgno;
unsigned int lines;
char *space;
int status;
if (!valid_argument ("top", arg))
return EINVAL;
space = strchr (arg, ' ');
if (space)
{
*space++ = '\0';
lines = strtoul (space, NULL, 10);
}
else
lines = 0;
msgno = strtoul (arg, NULL, 10);
status = mu_pop3_top (pop3, msgno, lines, &stream);
if (status == 0)
{
size_t n = 0;
char buf[128];
while ((stream_readline (stream, buf, sizeof buf, 0, &n) == 0) && n)
printf ("%s", buf);
stream_destroy (&stream, NULL);
}
return status;
}
int
com_retr (char *arg)
{
stream_t stream;
unsigned int msgno;
int status;
if (!valid_argument ("retr", arg))
return EINVAL;
msgno = strtoul (arg, NULL, 10);
status = mu_pop3_retr (pop3, msgno, &stream);
if (status == 0)
{
size_t n = 0;
char buf[128];
while ((stream_readline (stream, buf, sizeof buf, 0, &n) == 0) && n)
printf ("%s", buf);
stream_destroy (&stream, NULL);
}
return status;
}
int
com_connect (char *arg)
{
char host[256];
int port = 0;
int status;
if (!valid_argument ("connect", arg))
return 1;
*host = '\0';
sscanf (arg, "%256s %d", host, &port);
if (!valid_argument ("connect", host))
return EINVAL;
if (pop3)
com_disconnect (NULL);
status = mu_pop3_create (&pop3);
if (status == 0)
{
stream_t tcp;
if (verbose)
com_verbose ("verbose on");
status = tcp_stream_create (&tcp, host, port, MU_STREAM_READ | MU_STREAM_NO_CHECK);
if (status == 0)
{
mu_pop3_set_carrier (pop3, tcp);
status = mu_pop3_connect (pop3);
}
else
{
mu_pop3_destroy (&pop3);
pop3 = NULL;
}
}
if (status != 0)
fprintf (stderr, "Failed to create pop3: %s\n", strerror (status));
return status;
}
int
com_disconnect (char *arg)
{
(void) arg;
if (pop3)
{
mu_pop3_disconnect (pop3);
mu_pop3_destroy (&pop3);
pop3 = NULL;
}
return 0;
}
int
com_quit (char *arg)
{
(void)arg;
if (pop3)
{
if (mu_pop3_quit (pop3) == 0)
{
mu_pop3_disconnect (pop3);
}
else
{
fprintf (stdout, "Try 'exit' to leave %s\n", progname);
}
}
else
fprintf (stdout, "Try 'exit' to leave %s\n", progname);
return 0;
}
int
com_exit (char *arg)
{
(void)arg;
if (pop3)
{
mu_pop3_disconnect (pop3);
mu_pop3_destroy (&pop3);
}
done = 1;
return 0;
}
/* Return non-zero if ARG is a valid argument for CALLER, else print
an error message and return zero. */
int
valid_argument (const char *caller, char *arg)
{
if (!arg || !*arg)
{
fprintf (stderr, "%s: Argument required.\n", caller);
return 0;
}
return 1;
}