"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.4.4/src/nrctbl.c" (20 Nov 2019, 7949 Bytes) of package /linux/misc/tin-2.4.4.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "nrctbl.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.4.3_vs_2.4.4.

    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   : 2019-07-08
    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-2020 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  */
   58 static void write_newsrctable_file(void);
   59 
   60 
   61 /*
   62  * write_newsrctable_file()
   63  * create newsrctable file in local rc directory
   64  */
   65 static void
   66 write_newsrctable_file(
   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     fchmod(fileno(fp), (mode_t) (S_IRUSR|S_IWUSR));
   76     fclose(fp);
   77 }
   78 
   79 
   80 #ifdef NNTP_ABLE
   81 /*
   82  * get_nntpserver()
   83  * returns the FQDN of NNTP server by looking up a given
   84  * nickname or alias in the newsrctable
   85  * ---> extend to allow nameserver-lookups, if search in table
   86  *      failed
   87  */
   88 void
   89 get_nntpserver(
   90     char *nntpserver_name,
   91     size_t nntpserver_name_len,
   92     char *nick_name)
   93 {
   94     FILE *fp;
   95     char *line_entry;
   96     char line[LEN];
   97     char name_found[PATH_LEN];
   98     int line_entry_counter;
   99     t_bool found = FALSE;
  100 
  101     if ((fp = fopen(local_newsrctable_file, "r")) != NULL) {
  102         while ((fgets(line, sizeof(line), fp) != NULL) && !found) {
  103             line_entry_counter = 0;
  104 
  105             if (!strchr("# ;", line[0])) {
  106                 while ((line_entry = strtok(line_entry_counter ? NULL : line, " \t\n")) != NULL) {
  107                     line_entry_counter++;
  108 
  109                     if (line_entry_counter == 1)
  110                         STRCPY(name_found, line_entry);
  111 
  112                     if ((line_entry_counter > 2) && (!strcasecmp(line_entry, nick_name)))
  113                         found = TRUE;
  114                 }
  115             }
  116         }
  117         fclose(fp);
  118         strncpy(nntpserver_name, (found ? name_found : nick_name), nntpserver_name_len);
  119     } else {
  120         write_newsrctable_file();
  121         strncpy(nntpserver_name, nick_name, nntpserver_name_len);
  122     }
  123     nntpserver_name[nntpserver_name_len - 1] = '\0';
  124 }
  125 #endif /* NNTP_ABLE */
  126 
  127 
  128 /*
  129  * get_newsrcname()
  130  * get name of newsrc file with given name of nntp server
  131  * returns TRUE if name was found, FALSE if the search failed
  132  */
  133 t_bool
  134 get_newsrcname(
  135     char *newsrc_name,
  136     size_t newsrc_name_len,
  137     const char *nntpserver_name) /* return value is always ignored */
  138 {
  139     FILE *fp;
  140     char *line_entry;
  141     char line[LEN];
  142     char name_found[PATH_LEN];
  143     int line_entry_counter;
  144     int found = 0;
  145     t_bool do_cpy = FALSE;
  146 
  147     if ((fp = fopen(local_newsrctable_file, "r")) != NULL) {
  148         while ((fgets(line, (int) sizeof(line), fp) != NULL) && (found != 1)) {
  149             line_entry_counter = 0;
  150 
  151             if (!strchr("# ;", line[0])) {
  152                 while ((line_entry = strtok(line_entry_counter ? NULL : line, " \t\n")) != NULL) {
  153                     line_entry_counter++;
  154 
  155                     if ((line_entry_counter == 1) && (!strcasecmp(line_entry, nntpserver_name))) {
  156                         found = 1;
  157                         do_cpy = TRUE;
  158                     }
  159 
  160                     if ((line_entry_counter == 1) && ((!strcasecmp(line_entry, "default")) || (!strcmp(line_entry, "*")))) {
  161                         found = 2;
  162                         do_cpy = TRUE;
  163                     }
  164                     if (do_cpy && (line_entry_counter == 2)) {
  165                         STRCPY(name_found, line_entry);
  166                         do_cpy = FALSE;
  167                     }
  168                 }
  169             }
  170         }
  171         fclose(fp);
  172         if (found) {
  173             char dir[PATH_LEN];
  174             char tmp_newsrc[PATH_LEN];
  175             int error = 0;
  176 
  177             if (!strfpath(name_found, tmp_newsrc, sizeof(tmp_newsrc), NULL, FALSE)) {
  178                 my_fprintf(stderr, _("couldn't expand %s\n"), name_found); /* TODO: -> lang.c */
  179                 error = 1;
  180             } else {
  181                 if (tmp_newsrc[0] == '/') {
  182                     (void) strncpy(newsrc_name, tmp_newsrc, newsrc_name_len);
  183                     newsrc_name[newsrc_name_len - 1] = '\0';
  184                 } else
  185                     joinpath(newsrc_name, newsrc_name_len, homedir, tmp_newsrc);
  186 
  187                 STRCPY(dir, newsrc_name);
  188                 if ((line_entry = strrchr(dir, '/'))) { /* dirname(newsrc_name) */
  189                     while (line_entry > &dir[0] && *line_entry == '/')
  190                         *line_entry-- = '\0';
  191                 }
  192 
  193                 /*
  194                  * TODO: shall we create a missing dir?
  195                  *       currently something like
  196                  *       ~/.tin/${NNTPSERVER-localhost}/.newsrc
  197                  *       in newsrctable usually ends with
  198                  *       "No permissions to go into /home/urs/.tin/${NNTPSERVER}"
  199                  */
  200                 /* FIXME - write a global permssion check routine */
  201                 if (access(dir, X_OK)) {
  202                     my_fprintf(stderr, _(txt_error_no_enter_permission), dir);
  203                     error = 1;
  204                 } else if (access(newsrc_name, F_OK)) {
  205                     my_fprintf(stderr, _(txt_error_no_such_file), newsrc_name);
  206                     error = 2;
  207                 } else if (access(dir, R_OK)) {
  208                     my_fprintf(stderr, _(txt_error_no_read_permission), dir);
  209                     error = 1;
  210                 } else if (access(newsrc_name, R_OK)) {
  211                     my_fprintf(stderr, _(txt_error_no_read_permission), newsrc_name);
  212                     error = 1;
  213                 } else if (access(dir, W_OK)) {
  214                     my_fprintf(stderr, _(txt_error_no_write_permission), dir);
  215                     error = 1;
  216                 } else if (access(newsrc_name, W_OK)) {
  217                     my_fprintf(stderr, _(txt_error_no_write_permission), newsrc_name);
  218                     error = 1;
  219                 }
  220             }
  221 
  222             if (error) {
  223                 char ch, default_ch = 'a';
  224 
  225                 do {
  226                     /* very ugly code, but curses is not initialized yet */
  227                     if (error >= 2) {
  228                         default_ch = 'c';
  229                         printf("%s%c\b", _(txt_nrctbl_create), default_ch);
  230                     } else
  231                         printf("%s%c\b", _(txt_nrctbl_default), default_ch);
  232 
  233                     if ((ch = (char) ReadCh()) == '\r' || ch == '\n')
  234                         ch = default_ch;
  235                 } while (ch != ESC && ch != 'a' && ch != 'c' && ch != 'd' && ch != 'q');
  236                 printf("%c\n", ch);
  237 
  238                 /* NOTE: these keys can not be remapped */
  239                 switch (ch) {
  240                     case 'c':
  241                         /* FIXME this doesn't check if we could create the file */
  242                         return TRUE;
  243 
  244                     case 'd':
  245                         joinpath(newsrc_name, newsrc_name_len, homedir, ".newsrc");
  246                         return TRUE;
  247 
  248                     case 'a':
  249                         /*
  250                          * FIXME this (e.g. the location of the alternative name)
  251                          * is not documented in the man page
  252                          */
  253                         snprintf(name_found, sizeof(name_found), ".newsrc-%s", nntpserver_name);
  254                         joinpath(newsrc_name, newsrc_name_len, homedir, name_found);
  255                         return TRUE;
  256 
  257                     case 'q':
  258                         exit(EXIT_SUCCESS);
  259                         /* keep lint quiet: */
  260                         /* FALLTHROUGH */
  261 
  262                     case ESC:
  263                     default:
  264                         return TRUE;
  265                 }
  266             }
  267             return TRUE;
  268         }
  269     } else
  270         write_newsrctable_file();
  271 
  272     return FALSE;
  273 }