1 #ifndef lint
2 static char	sccsid[] = "@(#)list.c	1.11	(Berkeley) 5/11/89";
3 #endif
4 
5 #include "common.h"
6 
7 /*
8  * LIST
9  *
10  * List active newsgroups, newsgroup descriptions, distributions,
11  * active.times, and subscriptions.
12  *
13  */
14 
15 void
list(argc,argv)16 list(argc, argv)
17 	int		argc;
18 	const char	*argv[];
19 {
20 	char		line[NNTP_STRLEN];
21 	char		*grparray[2];
22 	char		*filename;
23 	const char	*items;
24 	const char	*format;
25 	register char	*cp;
26 	register FILE	*list_fp;
27 	const char	*wildarg = NULL;
28 	int		argc_ok = (argc == 2 || argc == 3);
29 
30 	if (argc == 1 || (argc_ok && !strcasecmp(argv[1],"active"))){
31 		if (argc == 3) {
32 			int first, last;
33 			int num = find_group(argv[2], &first, &last);
34 			if (num >= 0) {
35 				printf("%d list:\r\n%s\r\n.\r\n",
36 					OK_GROUPS,group_array[num]);
37 				(void) fflush(stdout);
38 				return;
39 			}
40 			wildarg = argv[2];
41 		}
42                 num_groups = read_groups();
43                 if (num_groups == 0){ /* can't get a group list */
44                   printf("%d Group update failed. Try later.\r\n",
45                          ERR_FAULT);
46                   (void) fflush(stdout);
47 #ifdef LOG
48                   syslog(LOG_INFO, "%s group update failed in LIST", hostname);
49 #endif
50                   exit(1);
51 		}
52 		filename = activefile;
53 		items = "active newsgroups";
54 		format = "Newsgroups in form \"group high low flags\".";
55 	} else if (argc == 2 && !strcasecmp(argv[1],"distributions")){
56 		filename = distributionsfile;
57 		items = "newsgroup distributions";
58 		format = "Distributions in form \"area description\".";
59 	} else if (argc_ok && !strcasecmp(argv[1],"newsgroups")){
60 		if (argc == 3)
61 			wildarg = argv[2];
62 		filename = newsgroupsfile;
63 		items = "newsgroup descriptions";
64 		format = "Descriptions in form \"group description\".";
65 	} else if (argc == 2 && !strcasecmp(argv[1],"subscriptions")){
66 		filename = subscriptionsfile;
67 		items = "group subscription list";
68 		format = "New-user subscription list in form \"group\".";
69 	} else if (argc == 2 && !strcasecmp(argv[1],"overview.fmt")){
70 		filename = overviewfmtfile;
71 		items = "overview format";
72 		format = "Order of fields in overview database.";
73 	} else if (argc == 2 && !strcasecmp(argv[1],"active.times")){
74 		filename = activetimesfile;
75 		items = "active times";
76 		format = "Active times in form \"group time email name\".";
77 	} else {
78 		printf("%d Usage: LIST [ACTIVE|NEWSGROUPS|DISTRIBUTIONS|SUBSCRIPTIONS|ACTIVE.TIMES]\r\n",
79 			ERR_CMDSYN);
80 		(void) fflush(stdout);
81 		return;
82 	}
83 
84 	grparray[0] = line;
85 	grparray[1] = NULL;
86 
87 	list_fp = fopen(filename, "r");
88 
89 	if (list_fp == NULL) {
90 		printf("%d No list of %s available.\r\n", ERR_FAULT,
91 			items);
92 		(void) fflush(stdout);
93 #ifdef SYSLOG
94 		syslog(LOG_ERR, "list: fopen %s: %m", filename);
95 #endif
96 		return;
97 	}
98 
99 	printf("%d %s\r\n",OK_GROUPS,format);
100 
101 	while (fgets(line, sizeof(line), list_fp) != NULL) {
102 		if ((cp = index(line, '\n')) != NULL)
103 			*cp = '\0';
104 		for (cp = line; *cp && !isspace(*cp); cp++) ;
105 		if (*cp)
106 			*cp = '\0';
107 		else
108 			cp = NULL;
109 		if (ngpermcount) {
110 			if (ngmatch(s1strneql, ALLBUT,
111 			    ngpermlist, ngpermcount,
112 			    grparray, 1) == 0)
113 				continue;
114 		}
115 		else if (ALLBUT==0) break; /* ngpermcnt==0 && ALLBUT == 0 means
116 					    * don't print the list, right? */
117 		if (!wildarg || wildmat(line, wildarg)) {
118 			if (cp)
119 				*cp = ' ';
120 			putline(line);
121 		}
122 	}
123 	(void) fclose(list_fp);
124 
125 	putline(".");
126 	(void) fflush(stdout);
127 }
128 
129 void
xgtitle(argc,argv)130 xgtitle(argc, argv)
131 	int		argc;
132 	char		*argv[];
133 {
134 	const char *v[3];
135 
136 	if (argc != 2) {
137 		printf("%d Usage: XGTITLE pattern\r\n", ERR_CMDSYN);
138 		(void) fflush(stdout);
139 		return;
140 	}
141 
142 	v[0] = "list";
143 	v[1] = "newsgroups";
144 	v[2] = argv[1];
145 	list(3, v);
146 }
147