File: popi.c

package info (click to toggle)
nmh 1.3-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,056 kB
  • ctags: 4,531
  • sloc: ansic: 50,788; sh: 3,141; makefile: 965; awk: 74
file content (636 lines) | stat: -rw-r--r-- 12,925 bytes parent folder | download
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

/*
 * popi.c -- POP initiator for MPOP
 *
 * $Id: popi.c,v 1.11 2006/04/27 12:00:28 jjr Exp $
 *
 * This code is Copyright (c) 2002, by the authors of nmh.  See the
 * COPYRIGHT file in the root directory of the nmh distribution for
 * complete copyright information.
 */

#include <h/mh.h>
#include <h/fmt_scan.h>
#include <h/scansbr.h>
#include <h/mts.h>
#include <h/utils.h>
#include <errno.h>

#ifndef	RPOP
# define RPOPminc(a) (a)
#else
# define RPOPminc(a)  0
#endif

#ifndef	APOP
# define APOPminc(a) (a)
#else
# define APOPminc(a)  0
#endif

#ifndef	BPOP
# define BPOPminc(a) (a)
#else
# define BPOPminc(a)  0
#endif

#ifndef	SMTPMTS
# define BULKminc(a) (a)
#else
# define BULKminc(a)  0
#endif

static struct swit  switches[] = {
#define	APOPSW                  0
    { "apop", APOPminc (-4) },
#define	NAPOPSW                 1
    { "noapop", APOPminc (-6) },
#define	AUTOSW                  2
    { "auto", BPOPminc(-4) },
#define	NAUTOSW                 3
    { "noauto", BPOPminc(-6) },
#define	BULKSW                  4
    { "bulk directory", BULKminc(-4) },
#define	FORMSW                  5
    { "form formatfile", 0 },
#define	FMTSW                   6
    { "format string", 5 },
#define	HOSTSW                  7
    { "host host", 0 },
#define	PROGSW                  8
    { "mshproc program", 0 },
#define	RPOPSW                  9
    { "rpop", RPOPminc (-4) },
#define	NRPOPSW                10
    { "norpop", RPOPminc (-6) },
#define	USERSW                 11
    { "user user", 0 },
#define	WIDTHSW                12
    { "width columns", 0 },
#define VERSIONSW              13
    { "version", 0 },
#define	HELPSW                 14
    { "help", 0 },
    { NULL, 0 }
};

static char *bulksw = NULL;
static int snoop = 0;
static int width = 0;
static char mailname[BUFSIZ];
static char *nfs = NULL;
static struct msgs *mp;

extern char response[];

/*
 * prototypes
 */
int sc_width (void);  /* from termsbr.c */


int
main (int argc, char **argv)
{
    int	autosw = 1, noisy = 1, rpop;
    char *cp, *maildir, *folder = NULL, *form = NULL;
    char *format = NULL, *host = NULL, *user = NULL;
    char *pass = NULL, buf[BUFSIZ], **argp;
    char **arguments;
    struct stat st;

    invo_name = r1bindex (argv[0], '/');

    /* read user profile/context */
    context_read();

    mts_init (invo_name);
    arguments = getarguments (invo_name, argc, argv, 1);
    argp = arguments;

    if (pophost && *pophost)
	host = pophost;
    if ((cp = getenv ("MHPOPDEBUG")) && *cp)
	snoop++;

    rpop = getuid() && !geteuid();

    while (cp = *argp++) {
	if (*cp == '-')
	    switch (smatch (++cp, switches)) {
		case AMBIGSW: 
		    ambigsw (cp, switches);
		    done (1);
		case UNKWNSW: 
		    adios (NULL, "-%s unknown", cp);

		case HELPSW: 
		    snprintf (buf, sizeof(buf), "%s [+folder] [switches]",
			invo_name);
		    print_help (buf, switches, 1);
		    done (1);
		case VERSIONSW:
		    print_version(invo_name);
		    done (1);

		case AUTOSW:
		    autosw = 1;
		    continue;
		case NAUTOSW:
		    autosw = 0;
		    continue;

		case BULKSW: 
		    if (!(bulksw = *argp++) || *bulksw == '-')
			adios (NULL, "missing argument to %s", argp[-2]);
		    continue;

		case FORMSW: 
		    if (!(form = *argp++) || *form == '-')
			adios (NULL, "missing argument to %s", argp[-2]);
		    format = NULL;
		    continue;
		case FMTSW: 
		    if (!(format = *argp++) || *format == '-')
			adios (NULL, "missing argument to %s", argp[-2]);
		    form = NULL;
		    continue;

		case WIDTHSW: 
		    if (!(cp = *argp++) || *cp == '-')
			adios (NULL, "missing argument to %s", argp[-2]);
		    width = atoi (cp);
		    continue;

		case HOSTSW:
		    if (!(host = *argp++) || *host == '-')
			adios (NULL, "missing argument to %s", argp[-2]);
		    continue;
		case USERSW:
		    if (!(user = *argp++) || *user == '-')
			adios (NULL, "missing argument to %s", argp[-2]);
		    continue;

		case APOPSW:
		    rpop = -1;
		    continue;
		case RPOPSW:
		    rpop = 1;
		    continue;
		case NAPOPSW:
		case NRPOPSW:
		    rpop = 0;
		    continue;

		case PROGSW:
		    if (!(mshproc = *argp++) || *mshproc == '-')
			adios (NULL, "missing argument to %s", argp[-2]);
		    continue;
	    }
	if (*cp == '+' || *cp == '@') {
	    if (folder)
		adios (NULL, "only one folder at a time!");
	    else
		folder = pluspath (cp);
	}
	else
	    adios (NULL, "usage: %s [+folder] [switches]", invo_name);
    }

    if (!host)
	adios (NULL, "usage: %s -host \"host\"", invo_name);

#ifdef SMTPMTS
    if (bulksw)
	do_bulk (host);
#endif

    if (user == NULL)
	user = getusername ();
    if (rpop > 0)
	pass = getusername ();
    else {
	setuid (getuid ());
	ruserpass (host, &user, &pass);
    }
    snprintf (mailname, sizeof(mailname), "PO box for %s@%s", user, host);

    if (pop_init (host, user, pass, NULL, snoop, rpop) == NOTOK)
	adios (NULL, "%s", response);
    if (rpop > 0)
	setuid (getuid ());

    /* get new format string */
    nfs = new_fs (form, format, FORMAT);

    if (!context_find ("path"))
	free (path ("./", TFOLDER));
    if (!folder)
	folder = getfolder (0);
    maildir = m_maildir (folder);

    create_folder(maildir, 0, done);

    if (chdir (maildir) == NOTOK)
	adios (maildir, "unable to change directory to");

    if (!(mp = folder_read (folder)))
	adios (NULL, "unable to read folder %s", folder);

#ifdef BPOP
    if (autosw)
	msh ();
    else
#endif

    popi();
    pop_quit();

    context_replace (pfolder, folder);	/* update current folder   */
    seq_setunseen (mp, 0);		/* set the Unseen-Sequence */
    seq_save (mp);
    context_save ();			/* save the context file   */
    return done (0);
}


static struct swit popicmds[] = {
#define	DELECMD	 0
    "dele", 0,
#define	LASTCMD	 1
    "last", 0,
#define	LISTCMD	 2
    "list", 0,
#define	NOOPCMD	 3
    "noop", 0,
#define	QUITCMD	 4
    "quit", 0,
#define	RETRCMD	 5
    "retr", 0,
#define	RSETCMD	 6
    "rset", 0,
#define	SCANCMD	 7
    "scan", 0,
#define	STATCMD	 8
    "stat", 0,
#define	TOPCMD	 9
    "top", 0,
#ifdef	BPOP
#define	MSHCMD	10
    "msh", 0,
#endif

    NULL, 0
};


static void
popi (void)
{
    int	eof = 0;

    for (;;) {
	int i;
	register char *cp;
	char buffer[BUFSIZ];

	if (eof)
	    return;

	printf ("(%s) ", invo_name);
	for (cp = buffer; (i = getchar ()) != '\n'; ) {
	    if (i == EOF) {
		putchar ('\n');
		if (cp == buffer)
		    return;
		eof = 1;
		break;
	    }

	    if (cp < buffer + sizeof buffer - 2)
		*cp++ = i;
	}
	*cp = '\0';
	if (buffer[0] == '\0')
	    continue;
	if (buffer[0] == '?') {
	    printf ("commands:\n");
	    print_sw (ALL, popicmds, "");
	    printf ("type CTRL-D or use \"quit\" to leave %s\n", invo_name);
	    continue;
	}

	if (cp = strchr (buffer, ' '))
	    *cp = '\0';
	switch (i = smatch (buffer, popicmds)) {
	    case AMBIGSW:
	        ambigsw (buffer, popicmds);
		continue;
	    case UNKWNSW:
		printf ("%s unknown -- type \"?\" for help\n", buffer);
		continue;
		
	    case QUITCMD:
		return;

	    case STATCMD:
	    case DELECMD:
	    case NOOPCMD:
	    case LASTCMD:
	    case RSETCMD:
	    case TOPCMD:
		if (cp)
		    *cp = ' ';
		pop_command ("%s%s", popicmds[i].sw, cp ? cp : "");
		printf ("%s\n", response);
		break;		

	    case LISTCMD:
		if (cp)
		    *cp = ' ';
		if (pop_command ("%s%s", popicmds[i].sw, cp ? cp : "")
		        == OK) {
		    printf ("%s\n", response);
		    if (!cp)
			for (;;) {
			    switch (pop_multiline ()) {
				case DONE:
				    strcpy (response, ".");
				    /* and fall... */
			        case NOTOK:
				    printf ("%s\n", response);
			            break;

				case OK:
				    printf ("%s\n", response);
				    continue;
			     }
			    break;
			}
		}
		break;

	    case RETRCMD:
		if (!cp) {
		    advise (NULL, "missing argument to %s", buffer);
		    break;
		}
		retr_action (NULL, OK);
		pop_retr (atoi (++cp), retr_action);
		retr_action (NULL, DONE);
		printf ("%s\n", response);
		break;

	    case SCANCMD:
		{
		    char   *dp,
			   *ep,
			   *fp;

		    if (width == 0)
			width = sc_width ();

		    for (dp = nfs, i = 0; *dp; dp++, i++)
			if (*dp == '\\' || *dp == '"' || *dp == '\n')
			    i++;
		    i++;
		    ep = mh_xmalloc ((unsigned) i);
		    for (dp = nfs, fp = ep; *dp; dp++) {
			if (*dp == '\n') {
			    *fp++ = '\\', *fp++ = 'n';
			    continue;
			}
			if (*dp == '"' || *dp == '\\')
			    *fp++ = '\\';
			*fp++ = *dp;
		    }
		    *fp = '\0';

		    pop_command ("xtnd scan %d \"%s\"", width, ep);
		    printf ("%s\n", response);

		    free (ep);
		}
		break;

#ifdef	BPOP
	    case MSHCMD:
		msh ();
		break;
#endif
	}
    }
}


static int
retr_action (char *rsp, int flag)
{
    static FILE *fp;

    if (rsp == NULL) {
	static int msgnum;
	static char *cp;

	if (flag == OK) {
	    if (!(mp = folder_realloc (mp, mp->lowoff, msgnum = mp->hghmsg + 1)))
		adios (NULL, "unable to allocate folder storage");

	    cp = getcpy (m_name (mp->hghmsg + 1));
	    if ((fp = fopen (cp, "w+")) == NULL)
		adios (cp, "unable to write");
	    chmod (cp, m_gmprot ());
	}
	else {
	    struct stat st;

	    fflush (fp);
	    if (fstat (fileno (fp), &st) != NOTOK && st.st_size > 0) {
		clear_msg_flags (mp, msgnum);
		set_exists (mp, msgnum);
		set_unseen (mp, msgnum);
		mp->msgflags |= SEQMOD;

		if (ferror (fp))
		    advise (cp, "write error on");
		mp->hghmsg = msgnum;
	    }
	    else
		unlink (cp);

	    fclose (fp), fp = NULL;
	    free (cp), cp = NULL;
	}

	return;
    }

    fprintf (fp, "%s\n", rsp);
}


#ifdef BPOP
static void
msh (void)
{
    int	child_id, vecp;
    char buf1[BUFSIZ], buf2[BUFSIZ], *vec[9];

    if (pop_fd (buf1, sizeof(buf1), buf2, sizeof(buf2)) == NOTOK)
	adios (NULL, "%s", response);

    vecp = 0;
    vec[vecp++] = r1bindex (mshproc, '/');
		    
    switch (child_id = fork ()) {
	case NOTOK:
	    adios ("fork", "unable to");

	case OK:
	    vec[vecp++] = "-popread";
	    vec[vecp++] = buf1;
	    vec[vecp++] = "-popwrite";
	    vec[vecp++] = buf2;
	    vec[vecp++] = "-idname";
	    vec[vecp++] = mailname;
	    vec[vecp++] = mailname;
	    vec[vecp] = NULL;
	    execvp (mshproc, vec);
	    fprintf (stderr, "unable to exec ");
	    perror (mshproc);
	    _exit (-1);

       default:
	    pidXwait (child_id, mshproc);
	    break;
   }
}
#endif


#ifdef SMTPMTS
#include <h/mts.h>
#include <mts/smtp/smtp.h>

static int
dselect (struct direct *d)
{
    int	i;

    if ((i = strlen (d->d_name)) < sizeof "smtp"
	    || strncmp (d->d_name, "smtp", sizeof "smtp" - 1))
	return 0;
    return ((i -= (sizeof ".bulk" - 1)) > 0
	        && !strcmp (d->d_name + i, ".bulk"));
}


static int
dcompar (struct direct *d1, struct direct *d2)
{
    struct stat s1, s2;

    if (stat ((*d1)->d_name, &s1) == NOTOK)
	return 1;
    if (stat ((*d2)->d_name, &s2) == NOTOK)
	return -1;
    return ((int) (s1.st_mtime - s2.st_mtime));
}


static void
do_bulk (char *host)
{
    register int i;
    int	n, retval, sm;
    struct direct **namelist;

    if (chdir (bulksw) == NOTOK)
	adios (bulksw, "unable to change directory to");

    if ((n = scandir (".", &namelist, dselect, dcompar)) == NOTOK)
	adios (bulksw, "unable to scan directory");

    sm = NOTOK;
    for (i = 0; i < n; i++) {
	register struct direct *d = namelist[i];

	if (sm == NOTOK) {
	    if (rp_isbad (retval = sm_init (NULL, host, 1, 1, snoop)))
		adios (NULL, "problem initializing server: %s",
		       rp_string (retval));
	    else
		sm = OK;
	}

	switch (retval = sm_bulk (d->d_name)) {
	    default:
	        if (rp_isbad (retval))
		    adios (NULL, "problem delivering msg %s: %s",
			   d->d_name, rp_string (retval));
		/* else fall... */
	    case RP_OK:
	    case RP_NO:
	    case RP_NDEL:
		advise (NULL, "msg %s: %s", d->d_name, rp_string (retval));
		break;
	}
    }

    if (sm == OK) {
	register int j;
	int	l,
		m;
	struct direct **newlist;

	while ((l = scandir (".", &newlist, dselect, dcompar)) > OK) {
	    m = 0;

	    for (j = 0; j < l; j++) {
		register struct direct *d = newlist[j];

		for (i = 0; i < n; i++)
		    if (strcmp (d->d_name, namelist[i]->d_name) == 0)
			break;
		if (i >= n) {
		    switch (retval = sm_bulk (d->d_name)) {
		        default:
			    if (rp_isbad (retval))
				adios (NULL, "problem delivering msg %s: %s",
				       d->d_name, rp_string (retval));
			    /* else fall... */
			case RP_OK:
			case RP_NO:
			case RP_NDEL:
			    advise (NULL, "msg %s: %s", d->d_name,
				    rp_string (retval));
			    break;
		    }

		    m = 1;
		}
	    }

	    for (i = 0; i < n; i++)
		free ((char *) namelist[i]);
	    free ((char *) namelist);
	    namelist = newlist, n = l;

	    if (!m)
		break;
	    newlist = NULL;
	}
    }

    if (sm == OK && rp_isbad (retval = sm_end (OK)))
	adios (NULL, "problem finalizing server: %s", rp_string (retval));

    for (i = 0; i < n; i++)
	free ((char *) namelist[i]);
    free ((char *) namelist);

    free ((char *) namelist);

    done (0);
}
#endif