"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.4.1/src/nrctbl.c" (12 Oct 2016, 7758 Bytes) of package /linux/misc/tin-2.4.1.tar.gz:


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.0_vs_2.4.1.

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