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
|
/*
* (c) Copyright 1994 HEWLETT-PACKARD COMPANY
*
* To anyone who acknowledges that this file is provided
* "AS IS" without any express or implied warranty:
* permission to use, copy, modify, and distribute this
* file for any purpose is hereby granted without fee,
* provided that the above copyright notice and this
* notice appears in all copies, and that the name of
* Hewlett-Packard Company not be used in advertising or
* publicity pertaining to distribution of the software
* without specific, written prior permission. Hewlett-
* Packard Company makes no representations about the
* suitability of this software for any purpose.
*/
/*
* Mailquery - contact the POP mail host an see if a user has
* mail. By default the result if reflected in the
* exit status.
*
* Usage: mailquery [-dv] [-e <cmd>]
* -d - debug
* -v - print result
* -e - exec this command if there is mail.
*/
#include <pwd.h>
#include <fcntl.h>
#include <sys/file.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HESIOD
#include <hesiod.h>
#endif
#include "pop.h"
extern int pop_debug;
int verbose = 0;
char *exec_cmd;
void usage()
{
fprintf(stderr, "usage: mailquery [-d] [-v] [-e cmd] [user[@host]]\n");
}
main(argc, argv)
int argc;
char *argv[];
{
extern char *getenv();
int nbytes;
char *mhost = NULL, *mhp;
char *user = 0;
struct passwd * pwd;
char response[128];
char c;
extern int optind;
extern char *optarg;
#ifdef HESIOD
struct hes_postoffice *p;
#endif /* HESIOD */
while ((c = getopt(argc, argv, "dve:")) != EOF) {
switch (c) {
case 'd':
pop_debug = 1;
break;
case 'e':
exec_cmd = optarg;
break;
case 'v':
verbose = 1;
break;
case '?':
usage();
exit(1);
}
}
argc -= optind;
argv += optind;
if (argc > 0) {
user = argv[0];
if ((mhost = strchr(argv[0], '@')) != NULL) {
*mhost = '\0';
mhost++;
}
#ifndef HESIOD
else {
mhost = DEFMAILHOST;
}
#endif
}
if (user == (char *) 0 || *user == '\0') {
if ((pwd = getpwuid(getuid())) == NULL) {
perror("getpwuid");
exit(1);
}
user = pwd->pw_name;
}
if ((mhost == NULL) &&
(mhp = getenv("MAILHOST")))
mhost = mhp;
#ifdef HESIOD
if (mhost == NULL) {
p = hes_getmailhost(user);
if (p != NULL && strcmp(p->po_type, "POP") == 0)
mhost = p->po_host;
else {
fprintf(stderr,"no POP server listed in Hesiod for %s\n", user);
exit(1);
}
}
#endif /* HESIOD */
if (mhost == NULL) {
mhost = DEFMAILHOST;
}
nbytes = mailquery(mhost, user);
if ((nbytes > 0) && (exec_cmd != 0)) {
if (pop_debug)
fprintf(stderr, "about to execute %s\n", exec_cmd);
system(exec_cmd);
}
exit(nbytes == 0);
}
mailquery(mhost, user)
char *mhost;
char *user;
{
int nbytes, nmsgs;
if (pop_init(mhost, 0) == NOTOK) {
error(Errmsg);
exit(1);
}
#ifdef KPOP
if (pop_command("USER %s", user) == NOTOK ||
pop_command("PASS %s", user) == NOTOK) {
#else /* !KPOP */
if (pop_command("USER %s", user) == NOTOK ||
pop_command("RPOP %s", user) == NOTOK) {
#endif /* KPOP */
error(Errmsg);
(void) pop_command("QUIT");
exit (1);
}
if (pop_stat(&nmsgs, &nbytes) == NOTOK) {
error(Errmsg);
(void) pop_command("QUIT");
exit (1);
}
(void) pop_command("QUIT");
if (verbose)
printf("%d messages (%d bytes) on host %s\n", nmsgs, nbytes, mhost);
return nbytes;
}
|