pop.c
17 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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010 Free Software Foundation, Inc.
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 3, 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, see <http://www.gnu.org/licenses/>. */
#if defined(HAVE_CONFIG_H)
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <netdb.h>
#include <mailutils/mailutils.h>
#include "mu.h"
#include "argp.h"
#include "xalloc.h"
static char pop_doc[] = N_("mu pop - POP3 client shell.");
char pop_docstring[] = N_("POP3 client shell");
static char pop_args_doc[] = "";
static struct argp_option pop_options[] = {
{ NULL }
};
static error_t
pop_parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp pop_argp = {
pop_options,
pop_parse_opt,
pop_args_doc,
pop_doc,
NULL,
NULL,
NULL
};
int
get_bool (const char *str, int *pb)
{
if (mu_c_strcasecmp (str, "yes") == 0
|| mu_c_strcasecmp (str, "on") == 0
|| mu_c_strcasecmp (str, "true") == 0)
*pb = 1;
else if (mu_c_strcasecmp (str, "no") == 0
|| mu_c_strcasecmp (str, "off") == 0
|| mu_c_strcasecmp (str, "false") == 0)
*pb = 0;
else
return 1;
return 0;
}
int
get_port (const char *port_str, int *pn)
{
short port_num;
long num;
char *p;
num = port_num = strtol (port_str, &p, 0);
if (*p == 0)
{
if (num != port_num)
{
mu_error ("bad port number: %s", port_str);
return 1;
}
}
else
{
struct servent *sp = getservbyname (port_str, "tcp");
if (!sp)
{
mu_error ("unknown port name");
return 1;
}
port_num = ntohs (sp->s_port);
}
*pn = port_num;
return 0;
}
/* Global handle for pop3. */
mu_pop3_t pop3;
/* Flag if verbosity is needed. */
int verbose;
#define VERBOSE_MASK(n) (1<<((n)+1))
#define SET_VERBOSE_MASK(n) (verbose |= VERBOSE_MASK (n))
#define CLR_VERBOSE_MASK(n) (verbose &= VERBOSE_MASK (n))
#define QRY_VERBOSE_MASK(n) (verbose & VERBOSE_MASK (n))
#define HAS_VERBOSE_MASK(n) (verbose & ~1)
#define SET_VERBOSE() (verbose |= 1)
#define CLR_VERBOSE() (verbose &= ~1)
#define QRY_VERBOSE() (verbose & 1)
enum pop_session_status
{
pop_session_disconnected,
pop_session_connected,
pop_session_logged_in
};
enum pop_session_status pop_session_status;
int connect_argc;
char **connect_argv;
/* Host we are connected to. */
#define host connect_argv[0]
int port = 110;
char *username;
const char *
pop_session_str (enum pop_session_status stat)
{
switch (stat)
{
case pop_session_disconnected:
return "disconnected";
case pop_session_connected:
return "connected";
case pop_session_logged_in:
return "logged in";
}
return "unknown";
}
static void
pop_prompt_env ()
{
if (!mutool_prompt_env)
mutool_prompt_env = xcalloc (2*7 + 1, sizeof(mutool_prompt_env[0]));
mutool_prompt_env[0] = "user";
mutool_prompt_env[1] = (pop_session_status == pop_session_logged_in) ?
username : "[nouser]";
mutool_prompt_env[2] = "host";
mutool_prompt_env[3] = (pop_session_status != pop_session_disconnected) ?
host : "[nohost]";
mutool_prompt_env[4] = "program-name";
mutool_prompt_env[5] = (char*) mu_program_name;
mutool_prompt_env[6] = "canonical-program-name";
mutool_prompt_env[7] = "mu";
mutool_prompt_env[8] = "package";
mutool_prompt_env[9] = PACKAGE;
mutool_prompt_env[10] = "version";
mutool_prompt_env[11] = PACKAGE_VERSION;
mutool_prompt_env[12] = "status";
mutool_prompt_env[13] = (char*) pop_session_str (pop_session_status);
mutool_prompt_env[14] = NULL;
}
static int
string_to_xlev (const char *name, int *pv)
{
if (strcmp (name, "secure") == 0)
*pv = MU_XSCRIPT_SECURE;
else if (strcmp (name, "payload") == 0)
*pv = MU_XSCRIPT_PAYLOAD;
else
return 1;
return 0;
}
static int
change_verbose_mask (int set, int argc, char **argv)
{
int i;
for (i = 0; i < argc; i++)
{
int lev;
if (string_to_xlev (argv[i], &lev))
{
mu_error ("unknown level: %s", argv[i]);
return 1;
}
if (set)
SET_VERBOSE_MASK (lev);
else
CLR_VERBOSE_MASK (lev);
}
return 0;
}
void
set_verbose (mu_pop3_t p)
{
if (p)
{
if (QRY_VERBOSE ())
{
mu_pop3_trace (p, MU_POP3_TRACE_SET);
}
else
mu_pop3_trace (p, MU_POP3_TRACE_CLR);
}
}
void
set_verbose_mask (mu_pop3_t p)
{
if (p)
{
mu_pop3_trace_mask (p, QRY_VERBOSE_MASK (MU_XSCRIPT_SECURE)
? MU_POP3_TRACE_SET : MU_POP3_TRACE_CLR,
MU_XSCRIPT_SECURE);
mu_pop3_trace_mask (p, QRY_VERBOSE_MASK (MU_XSCRIPT_PAYLOAD)
? MU_POP3_TRACE_SET : MU_POP3_TRACE_CLR,
MU_XSCRIPT_PAYLOAD);
}
}
static int
com_verbose (int argc, char **argv)
{
if (argc == 1)
{
if (QRY_VERBOSE ())
{
mu_stream_printf (mustrout, "verbose is on");
if (HAS_VERBOSE_MASK ())
{
char *delim = " (";
if (QRY_VERBOSE_MASK (MU_XSCRIPT_SECURE))
{
mu_stream_printf (mustrout, "%ssecure", delim);
delim = ", ";
}
if (QRY_VERBOSE_MASK (MU_XSCRIPT_PAYLOAD))
mu_stream_printf (mustrout, "%spayload", delim);
mu_stream_printf (mustrout, ")");
}
mu_stream_printf (mustrout, "\n");
}
else
mu_stream_printf (mustrout, "verbose is off\n");
}
else
{
int bv;
if (get_bool (argv[1], &bv) == 0)
{
verbose |= bv;
if (argc > 2)
change_verbose_mask (verbose, argc - 2, argv + 2);
set_verbose (pop3);
}
else if (strcmp (argv[1], "mask") == 0)
change_verbose_mask (1, argc - 2, argv + 2);
else if (strcmp (argv[1], "unmask") == 0)
change_verbose_mask (0, argc - 2, argv + 2);
else
mu_error ("unknown subcommand");
set_verbose_mask (pop3);
}
return 0;
}
static int
com_user (int argc, char **argv)
{
int status;
status = mu_pop3_user (pop3, argv[1]);
if (status == 0)
{
username = strdup (argv[1]);
pop_prompt_env ();
}
return status;
}
static int
com_apop (int argc, char **argv)
{
int status;
char *pwd, *passbuf = NULL;
if (argc == 3)
pwd = argv[2];
else if (!mutool_shell_interactive)
{
mu_error (_("apop: password required"));
return 1;
}
else
{
status = mu_getpass (mustrin, mustrout, "Password:", &passbuf);
if (status)
return status;
pwd = passbuf;
}
status = mu_pop3_apop (pop3, argv[1], pwd);
if (status == 0)
{
username = strdup (argv[1]);
pop_session_status = pop_session_logged_in;
}
free (passbuf);
return status;
}
static int
com_capa (int argc, char **argv)
{
mu_iterator_t iterator = NULL;
int status = 0;
int reread = 0;
int i = 1;
for (i = 1; i < argc; i++)
{
if (strcmp (argv[i], "-reread") == 0)
reread = 1;
else
break;
}
if (i < argc)
{
if (reread)
{
status = mu_pop3_capa (pop3, 1, NULL);
if (status)
return status;
}
for (; i < argc; i++)
{
const char *elt;
int rc = mu_pop3_capa_test (pop3, argv[i], &elt);
switch (rc)
{
case 0:
if (*elt)
mu_stream_printf (mustrout, "%s: %s\n", argv[i], elt);
else
mu_stream_printf (mustrout, "%s is set\n", argv[i]);
break;
case MU_ERR_NOENT:
mu_stream_printf (mustrout, "%s is not set\n", argv[i]);
break;
default:
return rc;
}
}
}
else
{
status = mu_pop3_capa (pop3, reread, &iterator);
if (status == 0)
{
for (mu_iterator_first (iterator);
!mu_iterator_is_done (iterator); mu_iterator_next (iterator))
{
char *capa = NULL;
mu_iterator_current (iterator, (void **) &capa);
mu_stream_printf (mustrout, "CAPA: %s\n", capa ? capa : "");
}
mu_iterator_destroy (&iterator);
}
}
return status;
}
static int
com_uidl (int argc, char **argv)
{
int status = 0;
if (argc == 1)
{
mu_stream_t out = mutool_open_pager ();
mu_iterator_t uidl_iterator = NULL;
status = mu_pop3_uidl_all (pop3, &uidl_iterator);
if (status == 0)
{
for (mu_iterator_first (uidl_iterator);
!mu_iterator_is_done (uidl_iterator);
mu_iterator_next (uidl_iterator))
{
char *uidl = NULL;
mu_iterator_current (uidl_iterator, (void **) &uidl);
mu_stream_printf (out, "UIDL: %s\n", uidl ? uidl : "");
}
mu_iterator_destroy (&uidl_iterator);
}
mu_stream_destroy (&out);
}
else
{
char *uidl = NULL;
unsigned int msgno = strtoul (argv[1], NULL, 10);
status = mu_pop3_uidl (pop3, msgno, &uidl);
if (status == 0)
mu_stream_printf (mustrout, "Msg: %d UIDL: %s\n", msgno, uidl ? uidl : "");
free (uidl);
}
return status;
}
static int
com_list (int argc, char **argv)
{
int status = 0;
if (argc == 1)
{
mu_stream_t out = mutool_open_pager ();
mu_iterator_t list_iterator;
status = mu_pop3_list_all (pop3, &list_iterator);
if (status == 0)
{
for (mu_iterator_first (list_iterator);
!mu_iterator_is_done (list_iterator);
mu_iterator_next (list_iterator))
{
char *list = NULL;
mu_iterator_current (list_iterator, (void **) &list);
mu_stream_printf (out, "LIST: %s\n", (list) ? list : "");
}
mu_iterator_destroy (&list_iterator);
mu_stream_destroy (&out);
}
}
else
{
size_t size = 0;
unsigned int msgno = strtoul (argv[1], NULL, 10);
status = mu_pop3_list (pop3, msgno, &size);
if (status == 0)
mu_stream_printf (mustrout, "Msg: %u Size: %lu\n", msgno, (unsigned long) size);
}
return status;
}
static int
com_noop (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
return mu_pop3_noop (pop3);
}
static int
com_pass (int argc, char **argv)
{
int status;
char *pwd, *passbuf = NULL;
if (argc == 1)
{
if (!mutool_shell_interactive)
{
mu_error (_("pass: password required"));
return 1;
}
status = mu_getpass (mustrin, mustrout, "Password:", &passbuf);
if (status)
return status;
pwd = passbuf;
}
else
pwd = argv[1];
status = mu_pop3_pass (pop3, pwd);
if (status == 0)
{
pop_session_status = pop_session_logged_in;
pop_prompt_env ();
}
free (passbuf);
return status;
}
static int
com_stat (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
size_t count = 0;
mu_off_t size = 0;
int status = 0;
status = mu_pop3_stat (pop3, &count, &size);
mu_stream_printf (mustrout, "Mesgs: %lu Size %lu\n",
(unsigned long) count, (unsigned long) size);
return status;
}
static int
com_stls (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
return mu_pop3_stls (pop3);
}
static int
com_dele (int argc, char **argv)
{
unsigned msgno;
msgno = strtoul (argv[1], NULL, 10);
return mu_pop3_dele (pop3, msgno);
}
static int
com_rset (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
return mu_pop3_rset (pop3);
}
static int
com_top (int argc, char **argv)
{
mu_stream_t stream;
unsigned int msgno;
unsigned int lines;
int status;
msgno = strtoul (argv[1], NULL, 10);
if (argc == 3)
lines = strtoul (argv[2], NULL, 10);
else
lines = 5;
status = mu_pop3_top (pop3, msgno, lines, &stream);
if (status == 0)
{
mu_stream_t out = mutool_open_pager ();
mu_stream_copy (out, stream, 0, NULL);
mu_stream_destroy (&out);
mu_stream_destroy (&stream);
}
return status;
}
static int
com_retr (int argc, char **argv)
{
mu_stream_t stream;
unsigned int msgno;
int status;
msgno = strtoul (argv[1], NULL, 10);
status = mu_pop3_retr (pop3, msgno, &stream);
if (status == 0)
{
mu_stream_t out = mutool_open_pager ();
mu_stream_copy (out, stream, 0, NULL);
mu_stream_destroy (&out);
mu_stream_destroy (&stream);
}
return status;
}
static int
com_disconnect (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
if (pop3)
{
mu_pop3_disconnect (pop3);
mu_pop3_destroy (&pop3);
pop3 = NULL;
mu_argcv_free (connect_argc, connect_argv);
connect_argc = 0;
connect_argv = NULL;
pop_session_status = pop_session_disconnected;
pop_prompt_env ();
}
return 0;
}
static int
com_connect (int argc, char **argv)
{
int status;
int n = 0;
int tls = 0;
int i = 1;
for (i = 1; i < argc; i++)
{
if (strcmp (argv[i], "-tls") == 0)
{
if (WITH_TLS)
tls = 1;
else
{
mu_error ("TLS not supported");
return 0;
}
}
else
break;
}
argc -= i;
argv += i;
if (argc >= 2)
{
if (get_port (argv[1], &n))
return 0;
}
else if (tls)
n = MU_POP3_DEFAULT_SSL_PORT;
else
n = MU_POP3_DEFAULT_PORT;
if (pop_session_status != pop_session_disconnected)
com_disconnect (0, NULL);
status = mu_pop3_create (&pop3);
if (status == 0)
{
mu_stream_t tcp;
if (QRY_VERBOSE ())
{
set_verbose (pop3);
set_verbose_mask (pop3);
}
status = mu_tcp_stream_create (&tcp, argv[0], n, MU_STREAM_READ);
if (status == 0)
{
#ifdef WITH_TLS
if (tls)
{
mu_stream_t tlsstream;
status = mu_tls_client_stream_create (&tlsstream, tcp, tcp, 0);
mu_stream_unref (tcp);
if (status)
{
mu_error ("cannot create TLS stream: %s",
mu_strerror (status));
return 0;
}
tcp = tlsstream;
}
#endif
mu_pop3_set_carrier (pop3, tcp);
status = mu_pop3_connect (pop3);
}
else
{
mu_pop3_destroy (&pop3);
pop3 = NULL;
}
}
if (status)
mu_error ("Failed to create pop3: %s", mu_strerror (status));
else
{
connect_argc = argc;
connect_argv = xcalloc (argc, sizeof (*connect_argv));
for (i = 0; i < argc; i++)
connect_argv[i] = xstrdup (argv[i]);
connect_argv[i] = NULL;
port = n;
pop_session_status = pop_session_connected;
pop_prompt_env ();
}
return status;
}
static int
com_quit (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
{
int status = 0;
if (pop3)
{
if (mu_pop3_quit (pop3) == 0)
{
status = com_disconnect (0, NULL);
}
else
{
mu_stream_printf (mustrout, "Try 'exit' to leave %s\n", mu_program_name);
}
}
else
mu_stream_printf (mustrout, "Try 'exit' to leave %s\n", mu_program_name);
return status;
}
struct mutool_command pop_comtab[] = {
{ "apop", 2, 3, com_apop,
N_("USER [PASS]"),
N_("authenticate with APOP") },
{ "capa", 1, -1, com_capa,
/* TRANSLATORS: -reread is a keyword; do not translate. */
N_("[-reread] [NAME...]"),
N_("list server capabilities") },
{ "disconnect", 1, 1,
com_disconnect,
NULL,
N_("close connection") },
{ "dele", 2, 2, com_dele,
N_("NUMBER"),
N_("mark message for deletion") },
{ "list", 1, 2, com_list,
N_("[NUMBER]"),
N_("list messages") },
{ "noop", 1, 1, com_noop,
NULL,
N_("send a \"no operation\"") },
{ "pass", 1, 2, com_pass,
N_("[PASSWORD]"),
N_("send password") },
{ "connect", 1, 4, com_connect,
/* TRANSLATORS: --tls is a keyword. */
N_("[-tls] HOSTNAME [PORT]"),
N_("open connection") },
{ "quit", 1, 1, com_quit,
NULL,
N_("quit pop3 session") },
{ "retr", 2, 2, com_retr,
"NUMBER",
N_("retrieve a message") },
{ "rset", 1, 1, com_rset,
NULL,
N_("remove deletion marks") },
{ "stat", 1, 1, com_stat,
NULL,
N_("get the mailbox size and number of messages in it") },
{ "stls", 1, 1, com_stls,
NULL,
N_("start TLS negotiation") },
{ "top", 2, 3, com_top,
"MSGNO [NUMBER]",
N_("display message headers and first NUMBER (default 5) lines of"
" its body") },
{ "uidl", 1, 2, com_uidl,
N_("[NUMBER]"),
N_("show unique message identifiers") },
{ "user", 2, 2, com_user,
N_("NAME"),
N_("send login") },
{ "verbose", 1, 4, com_verbose,
"[on|off|mask|unmask] [secret [payload]]",
N_("control the protocol tracing") },
{ NULL }
};
int
mutool_pop (int argc, char **argv)
{
int index;
if (argp_parse (&pop_argp, argc, argv, ARGP_IN_ORDER, &index, NULL))
return 1;
argc -= index;
argv += index;
if (argc)
{
mu_error (_("too many arguments"));
return 1;
}
/* Command line prompt */
mutool_shell_prompt = xstrdup ("pop> ");
pop_prompt_env ();
mutool_shell ("pop", pop_comtab);
return 0;
}
/*
MU Setup: pop
mu-handler: mutool_pop
mu-docstring: pop_docstring
End MU Setup:
*/