File: newsgroups.c

package info (click to toggle)
gup 0.5.17
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 200 kB
  • sloc: ansic: 1,609; sh: 211; makefile: 34
file content (192 lines) | stat: -rw-r--r-- 4,687 bytes parent folder | download | duplicates (5)
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
#include "gup.h"

static int newsgroups_loaded = FALSE;

static void load_newsgroups(void);

/* List out the active file entries that match the pattern */
extern void print_newsgroups(FILE *fp, const char *gname)
{
    GROUP *gp;
    int glen;
    int subflag;
    int gcount = 0;
    int subcount = 0;

    if (!newsgroups_loaded)
	load_newsgroups();

    /* Print all matching active entries */
    fprintf(fp, "\n%-*s Sub Description\n",
	    GROUP_OVERFLOW_LP, "Name");
    fprintf(fp, "%-*s --- ----------------------------------------\n",
	    GROUP_OVERFLOW_LP, "---------------");

    TRAVERSE(active_list, gp) {
	if (wildmat(gp->name, gname)) {
	    gcount++;
	    subflag = subscribed(group_list, gp->name);
	    if (subflag)
		subcount++;

	    fputs(gp->name, fp);

    /*
     * Try and fit the description nicely on the current line.
     * Either:      groupname       DESC
     * or:          very-long-group-name
     *                              DESC
     */
	    glen = strlen(gp->name);
	    if (glen > GROUP_OVERFLOW_LP) {
		glen = 0;
		putc('\n', fp);
	    }
	    glen = (GROUP_OVERFLOW_LP - glen) + 1;
	    while (glen-- > 0)
		putc(' ', fp);

	    fprintf(fp, "%s %s\n", subflag ? "Yes" : "No ", gp->desc);
	}
    }
    fprintf(fp, "%-*s --- ----------------------------------------\n",
	    GROUP_OVERFLOW_LP, "---------------");
    fprintf(fp, "\nGroups listed: %d, of which you subscribe to %d.\n\n",
	    gcount, subcount);
}

/* Try and load the newsgroups file and add descriptions to the active */
static void load_newsgroups(void)
{
#ifdef STDIO_LOADFILE
    FILE *fp;
    char lbuf[MAX_LINE_SIZE];
    char *cp;
    char *name;
#else
    int fd;
    struct stat sb;
    char *desc;
    int length;
#endif
    GROUP *gp = NULL;

    newsgroups_loaded = TRUE;
    if (!newsgroups_path || !*newsgroups_path) {
	logit(L_BOTH,
	    "WARNING: No newsgroups file defined - needed for descriptions");
	return;
    }
#ifdef STDIO_LOADFILE
    if (!(fp = fopen(newsgroups_path, "r"))) {
	logit(L_BOTH, "WARNING: Could not open newsgroups file '%s'",
		newsgroups_path);
	return;
    }
    while (fgets(lbuf, sizeof(lbuf) - 1, fp)) {		/* Load them all up */
	lbuf[strlen(lbuf) - 1] = '\0';	/* Zip trailing \n */

/* Crack it into a name and a description */

	for (name = lbuf; *name; name++)	/* Ignore leading spaces */
	    if (!isspace(*name))
		break;

	for (cp = name; *cp; cp++)
	    if (isspace(*cp)) {
		*cp++ = '\0';
		break;
	    }

	while (*cp && isspace(*cp))
	    cp++;

/*
 * name points to name and cp points to the description. Find the matching
 * active entry. Gen a warning if not found - useful for annoying the 
 * news admins.
 */
	TRAVERSE(active_list, gp) {
	    if ((strcasecmp(gp->name, name) == 0) && !*gp->desc) {
		gp->desc = xstrdup(cp);
		break;
	    }
	}
    }

    fclose(fp);
#else
    if ((fd = open(newsgroups_path, O_RDONLY)) < 0) {
	logit(L_BOTH, "WARNING: Could not open newsgroups file '%s'",
		newsgroups_path);
	return;
    }
    /* see how big it is */
    if (fstat(fd, &sb) < 0) {
	logit(L_BOTH, "WARNING: Could not stat newsgroups file '%s'",
		newsgroups_path);
	return;
    }
    /* grab ourselves a buffer */
    desc = malloc(sb.st_size + 1);
    if (!desc) {
	logit(L_BOTH, "WARNING: Could not malloc space for newsgroups");
	return;
    }
    /* slurp it in */
    length = read(fd, desc, (int) sb.st_size);
    if (length != sb.st_size) {
	logit(L_BOTH, "WARNING: Error reading newsgroups");
	return;
    }
    /* terminate the end */
    desc[length] = 0;

    /*
     * hurl through the newsgroups, assigning descriptions to active_list
     * groups as we go
     */
    while (*desc) {
	char *eoln, *p;

	/*
	 * terminate the end of the description, to prevent anything
	 * screaming off through the entire file
	 */
	if ((eoln = strchr(desc, '\n')))
	    *eoln = 0;
	else
	    logit(L_LOG, "WARNING: premature end of newsgroups file");

	/* locate end of group name */
	if ((p = strchr(desc, '\t'))) {
	    *p = 0;			/* found it */

	    while (isspace(*++p));	/* gobble whitespace */

	    /* check whether there's still a description to add */
	    if (*p) {
		/*
		 * check if we know about it; first try the current group we're
		 * up to, to make it fast for sorted active & newsgroups
		 */
		if (gp && !strcmp(gp->name, desc))
		    gp->desc = p;	/* add description */
		else {
		    TRAVERSE(active_list, gp) {
			if (!strcmp(gp->name, desc) && !*gp->desc) {
			    gp->desc = p; /* OK, we've found it */

			    /* jump to the next group for next time around */
			    NEXT(active_list, gp);
			    break;
			}
		    }
		}
	    }
	}
	/* advance to next line */
	desc = eoln + 1;
    }
#endif
}