tin  2.6.1
About: TIN is a threaded NNTP and spool based UseNet newsreader.
  Fossies Dox: tin-2.6.1.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

nrctbl.c
Go to the documentation of this file.
1/*
2 * Project : tin - a Usenet reader
3 * Module : nrctbl.c
4 * Author : Sven Paulus <sven@tin.org>
5 * Created : 1996-10-06
6 * Updated : 2020-04-23
7 * Notes : This module does the NNTP server name lookup in
8 * ~/.tin/newsrctable and returns the real hostname
9 * and the name of the newsrc file for a given
10 * alias of the server.
11 *
12 * Copyright (c) 1996-2022 Sven Paulus <sven@tin.org>
13 * All rights reserved.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 *
19 * 1. Redistributions of source code must retain the above copyright notice,
20 * this list of conditions and the following disclaimer.
21 *
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * 3. Neither the name of the copyright holder nor the names of its
27 * contributors may be used to endorse or promote products derived from
28 * this software without specific prior written permission.
29 *
30 * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
34 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
41 */
42
43
44#ifndef TIN_H
45# include "tin.h"
46#endif /* !TIN_H */
47#ifndef TCURSES_H
48# include "tcurses.h"
49#endif /* !TCURSES_H */
50#ifndef VERSION_H
51# include "version.h"
52#endif /* !VERSION_H */
53
54
55/*
56 * local prototypes
57 */
58static void write_newsrctable_file(void);
59
60
61/*
62 * write_newsrctable_file()
63 * create newsrctable file in local rc directory
64 */
65static void
67 void)
68{
69 FILE *fp;
70
71 if ((fp = fopen(local_newsrctable_file, "w")) == NULL)
72 return;
73
74 fprintf(fp, _(txt_nrctbl_info), PRODUCT, VERSION);
75#ifdef HAVE_FCHMOD
76 fchmod(fileno(fp), (mode_t) (S_IRUSR|S_IWUSR));
77#else
78# ifdef HAVE_CHMOD
79 chmod(local_newsrctable_file, (mode_t) (S_IRUSR|S_IWUSR));
80# endif /* HAVE_CHMOD */
81#endif /* HAVE_FCHMOD */
82 fclose(fp);
83}
84
85
86#ifdef NNTP_ABLE
87/*
88 * get_nntpserver()
89 * returns the FQDN of NNTP server by looking up a given
90 * nickname or alias in the newsrctable
91 * ---> extend to allow nameserver-lookups, if search in table
92 * failed
93 */
94void
95get_nntpserver(
96 char *nntpserver_name,
97 size_t nntpserver_name_len,
98 char *nick_name)
99{
100 FILE *fp;
101 char *line_entry;
102 char line[LEN];
103 char name_found[PATH_LEN];
104 int line_entry_counter;
105 t_bool found = FALSE;
106
107 if ((fp = fopen(local_newsrctable_file, "r")) != NULL) {
108 while ((fgets(line, sizeof(line), fp) != NULL) && !found) {
109 line_entry_counter = 0;
110
111 if (!strchr("# ;", line[0])) {
112 while ((line_entry = strtok(line_entry_counter ? NULL : line, " \t\n")) != NULL) {
113 line_entry_counter++;
114
115 if (line_entry_counter == 1)
116 STRCPY(name_found, line_entry);
117
118 if ((line_entry_counter > 2) && (!strcasecmp(line_entry, nick_name)))
119 found = TRUE;
120 }
121 }
122 }
123 fclose(fp);
124 strncpy(nntpserver_name, (found ? name_found : nick_name), nntpserver_name_len);
125 } else {
127 strncpy(nntpserver_name, nick_name, nntpserver_name_len);
128 }
129 nntpserver_name[nntpserver_name_len - 1] = '\0';
130}
131#endif /* NNTP_ABLE */
132
133
134/*
135 * get_newsrcname()
136 * get name of newsrc file with given name of nntp server
137 * returns TRUE if name was found, FALSE if the search failed
138 */
139t_bool
141 char *newsrc_name,
142 size_t newsrc_name_len,
143 const char *nntpserver_name) /* return value is always ignored */
144{
145 FILE *fp;
146 char *line_entry;
147 char line[LEN];
148 char name_found[PATH_LEN];
149 int line_entry_counter;
150 int found = 0;
151 t_bool do_cpy = FALSE;
152
153 if ((fp = fopen(local_newsrctable_file, "r")) != NULL) {
154 while ((fgets(line, (int) sizeof(line), fp) != NULL) && (found != 1)) {
155 line_entry_counter = 0;
156
157 if (!strchr("# ;", line[0])) {
158 while ((line_entry = strtok(line_entry_counter ? NULL : line, " \t\n")) != NULL) {
159 line_entry_counter++;
160
161 if ((line_entry_counter == 1) && (!strcasecmp(line_entry, nntpserver_name))) {
162 found = 1;
163 do_cpy = TRUE;
164 }
165
166 if ((line_entry_counter == 1) && ((!strcasecmp(line_entry, "default")) || (!strcmp(line_entry, "*")))) {
167 found = 2;
168 do_cpy = TRUE;
169 }
170 if (do_cpy && (line_entry_counter == 2)) {
171 STRCPY(name_found, line_entry);
172 do_cpy = FALSE;
173 }
174 }
175 }
176 }
177 fclose(fp);
178 if (found) {
179 char dir[PATH_LEN];
180 char tmp_newsrc[PATH_LEN];
181 int error = 0;
182
183 if (!strfpath(name_found, tmp_newsrc, sizeof(tmp_newsrc), NULL, FALSE)) {
184 my_fprintf(stderr, _("couldn't expand %s\n"), name_found); /* TODO: -> lang.c */
185 error = 1;
186 } else {
187 if (tmp_newsrc[0] == '/') {
188 (void) strncpy(newsrc_name, tmp_newsrc, newsrc_name_len);
189 newsrc_name[newsrc_name_len - 1] = '\0';
190 } else
191 joinpath(newsrc_name, newsrc_name_len, homedir, tmp_newsrc);
192
193 STRCPY(dir, newsrc_name);
194 if ((line_entry = strrchr(dir, '/'))) { /* dirname(newsrc_name) */
195 while (line_entry > &dir[0] && *line_entry == '/')
196 *line_entry-- = '\0';
197 }
198
199 /*
200 * TODO: shall we create a missing dir?
201 * currently something like
202 * ~/.tin/${NNTPSERVER-localhost}/.newsrc
203 * in newsrctable usually ends with
204 * "No permissions to go into /home/urs/.tin/${NNTPSERVER}"
205 */
206 /* FIXME - write a global permssion check routine */
207 if (access(dir, X_OK)) {
209 error = 1;
210 } else if (access(newsrc_name, F_OK)) {
211 my_fprintf(stderr, _(txt_error_no_such_file), newsrc_name);
212 error = 2;
213 } else if (access(dir, R_OK)) {
215 error = 1;
216 } else if (access(newsrc_name, R_OK)) {
217 my_fprintf(stderr, _(txt_error_no_read_permission), newsrc_name);
218 error = 1;
219 } else if (access(dir, W_OK)) {
221 error = 1;
222 } else if (access(newsrc_name, W_OK)) {
223 my_fprintf(stderr, _(txt_error_no_write_permission), newsrc_name);
224 error = 1;
225 }
226 }
227
228 if (error) {
229 char ch, default_ch = 'a';
230
231 do {
232 /* very ugly code, but curses is not initialized yet */
233 if (error >= 2) {
234 default_ch = 'c';
235 printf("%s%c\b", _(txt_nrctbl_create), default_ch);
236 } else
237 printf("%s%c\b", _(txt_nrctbl_default), default_ch);
238
239 if ((ch = (char) ReadCh()) == '\r' || ch == '\n')
240 ch = default_ch;
241 } while (ch != ESC && ch != 'a' && ch != 'c' && ch != 'd' && ch != 'q');
242 printf("%c\n", ch);
243
244 /* NOTE: these keys can not be remapped */
245 switch (ch) {
246 case 'c':
247 /* FIXME this doesn't check if we could create the file */
248 return TRUE;
249
250 case 'd':
251 joinpath(newsrc_name, newsrc_name_len, homedir, ".newsrc");
252 return TRUE;
253
254 case 'a':
255 /*
256 * FIXME this (e.g. the location of the alternative name)
257 * is not documented in the man page
258 */
259 snprintf(name_found, sizeof(name_found), ".newsrc-%s", nntpserver_name);
260 joinpath(newsrc_name, newsrc_name_len, homedir, name_found);
261 return TRUE;
262
263 case 'q':
264 exit(EXIT_SUCCESS);
265 /* keep lint quiet: */
266 /* FALLTHROUGH */
267
268 case ESC:
269 default:
270 return TRUE;
271 }
272 }
273 return TRUE;
274 }
275 } else
277
278 return FALSE;
279}
unsigned t_bool
Definition: bool.h:77
#define TRUE
Definition: bool.h:74
#define FALSE
Definition: bool.h:70
constext txt_nrctbl_info[]
Definition: lang.c:713
char homedir[PATH_LEN]
Definition: init.c:78
constext txt_error_no_enter_permission[]
Definition: lang.c:255
constext txt_nrctbl_default[]
Definition: lang.c:712
constext txt_error_no_read_permission[]
Definition: lang.c:259
char local_newsrctable_file[PATH_LEN]
Definition: init.c:87
constext txt_nrctbl_create[]
Definition: lang.c:711
constext txt_error_no_write_permission[]
Definition: lang.c:261
constext txt_error_no_such_file[]
Definition: lang.c:260
#define ESC
Definition: keymap.h:140
t_bool get_newsrcname(char *newsrc_name, size_t newsrc_name_len, const char *nntpserver_name)
Definition: nrctbl.c:140
static void write_newsrctable_file(void)
Definition: nrctbl.c:66
int ReadCh(void)
Definition: curses.c:1110
void joinpath(char *result, size_t result_size, const char *dir, const char *file)
Definition: joinpath.c:50
int strfpath(const char *format, char *str, size_t maxsize, struct t_group *group, t_bool expand_all)
Definition: misc.c:1699
int strcasecmp(const char *p, const char *q)
Definition: string.c:475
#define my_fprintf
Definition: tcurses.h:176
#define LEN
Definition: tin.h:860
#define R_OK
Definition: tin.h:2227
#define STRCPY(dst, src)
Definition: tin.h:820
#define X_OK
Definition: tin.h:2233
#define EXIT_SUCCESS
Definition: tin.h:1298
#define S_IRUSR
Definition: tin.h:2185
#define _(Text)
Definition: tin.h:94
#define PATH_LEN
Definition: tin.h:843
#define snprintf
Definition: tin.h:2464
#define S_IWUSR
Definition: tin.h:2186
#define W_OK
Definition: tin.h:2230
#define F_OK
Definition: tin.h:2236
#define VERSION
Definition: version.h:46
#define PRODUCT
Definition: version.h:44