File: nntplist.c

package info (click to toggle)
trn4 4.0-test77-18
  • links: PTS, VCS
  • area: non-free
  • in suites: trixie
  • size: 4,016 kB
  • sloc: ansic: 48,332; sh: 6,795; tcl: 1,696; yacc: 662; perl: 108; makefile: 26
file content (199 lines) | stat: -rw-r--r-- 4,009 bytes parent folder | download | duplicates (12)
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
/* nntplist.c
 */
/* This software is copyrighted as detailed in the LICENSE file. */


#include "EXTERN.h"
#include "common.h"
#include "env.h"
#include "util2.h"
#include "util3.h"
#include "wildmat.h"
#include "nntpclient.h"
#include "nntpinit.h"

void Usage _((void));

char* server_name;
char* nntp_auth_file;

int debug = 0;			/* make nntpclient.c happy */
char* homedir;
char* dotdir;

#ifdef USE_GENAUTH
char* loginName;
#endif

int
main(argc, argv)
int argc;
char* argv[];
{
    char command[32];
    char* action = NULL;
    char* wildarg = NULL;
    char* cp;
    FILE* in_fp;
    register FILE* out_fp = NULL;

    while (--argc) {
	if (**++argv == '-') {
	    switch (*++*argv) {
	    case 'o':
		if (out_fp || !--argc)
		    Usage();
		out_fp = fopen(*++argv, "w");
		if (out_fp == NULL) {
		    perror(*argv);
		    exit(1);
		}
		break;
	    case 'x':
		if (wildarg || !--argc)
		    Usage();
		wildarg = *++argv;
		break;
	    default:
		Usage();
		/* NO RETURN */
	    }
	}
	else if (!action)
	    action = *argv;
	else
	    Usage();
    }
    if (action && strcaseEQ(action,"active"))
	action = NULL;
    if (!out_fp)
	out_fp = stdout;

#ifdef USE_GENAUTH
    /* get login name */
    loginName = getenv("USER");
    if (loginName == NULL) {
	loginName = getenv("LOGNAME");
#ifdef GETLOGIN
	if (loginName == NULL)
	    loginName = getlogin();
#endif
    }
#endif

    homedir = getenv("HOME");
    if (homedir == NULL)
	homedir = getenv("LOGDIR");
    dotdir = getenv("DOTDIR");
    if (!dotdir)
	dotdir = homedir;

    cp = getenv("NNTPSERVER");
    if (!cp) {
	cp = filexp(SERVER_NAME);
	if (*cp == '/')
	    cp = nntp_servername(cp);
    }
    if (strNE(cp,"local")) {
	server_name = savestr(cp);
	cp = index(server_name, ';');
#ifndef DECNET
	if (!cp)
	    cp = index(server_name, ':');
#endif
	if (cp) {
	    *cp = '\0';
	    nntplink.port_number = atoi(cp+1);
	}
	nntp_auth_file = filexp(NNTP_AUTH_FILE);
	if ((cp = getenv("NNTP_FORCE_AUTH")) != NULL
	 && (*cp == 'y' || *cp == 'Y'))
	    nntplink.flags |= NNTP_FORCE_AUTH_NEEDED;
    }

    if (server_name) {
	if (init_nntp() < 0
	 || nntp_connect(server_name,0) <= 0)
	    exit(1);
	if (action)
	    sprintf(command,"LIST %s",action);
	else
	    strcpy(command,"LIST");
	if (wildarg)
	    sprintf(command+strlen(command)," %s",wildarg);
	if (nntp_command(command) <= 0)
	    exit(1);
#ifdef HAS_SIGHOLD
	sighold(SIGINT);
#endif
	if (nntp_check() <= 0) {
	    fprintf(stderr,"nntplist: Can't get %s file from server.\n",action? action : "active");
	    fprintf(stderr, "Server said: %s\n", ser_line);
	    finalize(1);
	}
	while (nntp_gets(ser_line, sizeof ser_line) == 1) {
	    if (nntp_at_list_end(ser_line))
		break;
	    fputs(ser_line, out_fp);
	    putc('\n', out_fp);
	}

#ifdef HAS_SIGHOLD
	sigrelse(SIGINT);
#endif
	nntp_close(TRUE);
	cleanup_nntp();
    }
    else {
	cp = NULL;
	if (!action)
	    cp = ACTIVE;
	else if (strcaseEQ(action,"active.times"))
	    cp = ACTIVE_TIMES;
	else if (strcaseEQ(action,"newsgroups"))
	    cp = GROUPDESC;
	else if (strcaseEQ(action,"subscriptions"))
	    cp = SUBSCRIPTIONS;
	else if (strcaseEQ(action,"overview.fmt"))
	    cp = OVERVIEW_FMT;
	if (!cp || !*cp) {
	    fprintf(stderr, "Don't know how to list `%s' from your local system.\n",
		    action);
	    exit(1);
	}
	if ((in_fp = fopen(filexp(cp), "r")) == NULL) {
	    fprintf(stderr,"Unable to open `%s'.\n", cp);
	    exit(1);
	}
	while (fgets(ser_line, sizeof ser_line, in_fp)) {
	    if (wildarg) {
		for (cp = ser_line; *cp && !isspace(*cp); cp++) ;
		if (!cp)
		    continue;
		*cp = '\0';
		if (!wildmat(ser_line, wildarg))
		    continue;
		*cp = ' ';
	    }
	    fputs(ser_line, out_fp);
	}
    }
    return 0;
}

void
Usage()
{
    fprintf(stderr, "Usage: nntplist [-x WildSpec] [-o OutputFile] [type]\n\
\nWhere type is any of the LIST command arguments your server accepts.\n");
    exit(1);
}

#ifdef SUPPORT_NNTP
int
nntp_handle_timeout()
{
    fputs("\n503 Server timed out.\n",stderr);
    return -2;
}
#endif