1 /* nntplist.c
2  */
3 /* This software is copyrighted as detailed in the LICENSE file. */
4 
5 
6 #include "EXTERN.h"
7 #include "common.h"
8 #include "env.h"
9 #include "util2.h"
10 #include "util3.h"
11 #include "wildmat.h"
12 #include "nntpclient.h"
13 #include "nntpinit.h"
14 
15 void Usage _((void));
16 
17 char* server_name;
18 char* nntp_auth_file;
19 
20 int debug = 0;			/* make nntpclient.c happy */
21 char* homedir;
22 char* dotdir;
23 
24 #ifdef USE_GENAUTH
25 char* loginName;
26 #endif
27 
28 int
main(argc,argv)29 main(argc, argv)
30 int argc;
31 char* argv[];
32 {
33     char command[32];
34     char* action = NULL;
35     char* wildarg = NULL;
36     char* cp;
37     FILE* in_fp;
38     register FILE* out_fp = NULL;
39 
40     while (--argc) {
41 	if (**++argv == '-') {
42 	    switch (*++*argv) {
43 	    case 'o':
44 		if (out_fp || !--argc)
45 		    Usage();
46 		out_fp = fopen(*++argv, "w");
47 		if (out_fp == NULL) {
48 		    perror(*argv);
49 		    exit(1);
50 		}
51 		break;
52 	    case 'x':
53 		if (wildarg || !--argc)
54 		    Usage();
55 		wildarg = *++argv;
56 		break;
57 	    default:
58 		Usage();
59 		/* NO RETURN */
60 	    }
61 	}
62 	else if (!action)
63 	    action = *argv;
64 	else
65 	    Usage();
66     }
67     if (action && strcaseEQ(action,"active"))
68 	action = NULL;
69     if (!out_fp)
70 	out_fp = stdout;
71 
72 #ifdef USE_GENAUTH
73     /* get login name */
74     loginName = getenv("USER");
75     if (loginName == NULL) {
76 	loginName = getenv("LOGNAME");
77 #ifdef GETLOGIN
78 	if (loginName == NULL)
79 	    loginName = getlogin();
80 #endif
81     }
82 #endif
83 
84     homedir = getenv("HOME");
85     if (homedir == NULL)
86 	homedir = getenv("LOGDIR");
87     dotdir = getenv("DOTDIR");
88     if (!dotdir)
89 	dotdir = homedir;
90 
91     cp = getenv("NNTPSERVER");
92     if (!cp) {
93 	cp = filexp(SERVER_NAME);
94 	if (*cp == '/')
95 	    cp = nntp_servername(cp);
96     }
97     if (strNE(cp,"local")) {
98 	server_name = savestr(cp);
99 	cp = index(server_name, ';');
100 #ifndef DECNET
101 	if (!cp)
102 	    cp = index(server_name, ':');
103 #endif
104 	if (cp) {
105 	    *cp = '\0';
106 	    nntplink.port_number = atoi(cp+1);
107 	}
108 	nntp_auth_file = filexp(NNTP_AUTH_FILE);
109 	if ((cp = getenv("NNTP_FORCE_AUTH")) != NULL
110 	 && (*cp == 'y' || *cp == 'Y'))
111 	    nntplink.flags |= NNTP_FORCE_AUTH_NEEDED;
112     }
113 
114     if (server_name) {
115 	if (init_nntp() < 0
116 	 || nntp_connect(server_name,0) <= 0)
117 	    exit(1);
118 	if (action)
119 	    sprintf(command,"LIST %s",action);
120 	else
121 	    strcpy(command,"LIST");
122 	if (wildarg)
123 	    sprintf(command+strlen(command)," %s",wildarg);
124 	if (nntp_command(command) <= 0)
125 	    exit(1);
126 #ifdef HAS_SIGHOLD
127 	sighold(SIGINT);
128 #endif
129 	if (nntp_check() <= 0) {
130 	    fprintf(stderr,"nntplist: Can't get %s file from server.\n",action? action : "active");
131 	    fprintf(stderr, "Server said: %s\n", ser_line);
132 	    finalize(1);
133 	}
134 	while (nntp_gets(ser_line, sizeof ser_line) == 1) {
135 	    if (nntp_at_list_end(ser_line))
136 		break;
137 	    fputs(ser_line, out_fp);
138 	    putc('\n', out_fp);
139 	}
140 
141 #ifdef HAS_SIGHOLD
142 	sigrelse(SIGINT);
143 #endif
144 	nntp_close(TRUE);
145 	cleanup_nntp();
146     }
147     else {
148 	cp = NULL;
149 	if (!action)
150 	    cp = ACTIVE;
151 	else if (strcaseEQ(action,"active.times"))
152 	    cp = ACTIVE_TIMES;
153 	else if (strcaseEQ(action,"newsgroups"))
154 	    cp = GROUPDESC;
155 	else if (strcaseEQ(action,"subscriptions"))
156 	    cp = SUBSCRIPTIONS;
157 	else if (strcaseEQ(action,"overview.fmt"))
158 	    cp = OVERVIEW_FMT;
159 	if (!cp || !*cp) {
160 	    fprintf(stderr, "Don't know how to list `%s' from your local system.\n",
161 		    action);
162 	    exit(1);
163 	}
164 	if ((in_fp = fopen(filexp(cp), "r")) == NULL) {
165 	    fprintf(stderr,"Unable to open `%s'.\n", cp);
166 	    exit(1);
167 	}
168 	while (fgets(ser_line, sizeof ser_line, in_fp)) {
169 	    if (wildarg) {
170 		for (cp = ser_line; *cp && !isspace(*cp); cp++) ;
171 		if (!cp)
172 		    continue;
173 		*cp = '\0';
174 		if (!wildmat(ser_line, wildarg))
175 		    continue;
176 		*cp = ' ';
177 	    }
178 	    fputs(ser_line, out_fp);
179 	}
180     }
181     return 0;
182 }
183 
184 void
Usage()185 Usage()
186 {
187     fprintf(stderr, "Usage: nntplist [-x WildSpec] [-o OutputFile] [type]\n\
188 \nWhere type is any of the LIST command arguments your server accepts.\n");
189     exit(1);
190 }
191 
192 #ifdef SUPPORT_NNTP
193 int
nntp_handle_timeout()194 nntp_handle_timeout()
195 {
196     fputs("\n503 Server timed out.\n",stderr);
197     return -2;
198 }
199 #endif
200