home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / news / nntp / nntplink3.1.0 / history.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-25  |  3.8 KB  |  193 lines

  1. #include "conf.h"
  2. #include "strfuns.h"
  3. #ifdef LOOKUP_ARTICLE
  4. #ifdef HAVE_STRING_H
  5. #include <string.h>
  6. #else
  7. #include <strings.h>
  8. #endif
  9. #include <fcntl.h>
  10. #include <ctype.h>
  11. #ifdef FAKESYSLOG
  12. #include "fsyslog.h"
  13. #else
  14. #include <syslog.h>
  15. #endif
  16. #include <sys/param.h>
  17. #include "readline.h"
  18. #include "nntplink.h"
  19. /*
  20.  - get_history_entry -- return the path name of an article from history file
  21.  *
  22.  *    Parameters:    "msg_id" is the message ID of the
  23.  *            article, enclosed in <>'s.
  24.  *            "lookup", only check if article exists
  25.  *
  26.  *    Returns:    A char pointer to a static data area
  27.  *            containing the full pathname of the
  28.  *            article, or NULL if the message-id is not
  29.  *            in the history file.
  30.  *
  31.  *    Side effects:    opens dbm database
  32.  *            (only once, keeps it open after that).
  33.  *            If running Bnews, converts "msg_id" to lower case.
  34.  *            If running Cnews, converts "msg_id" per rfc822.
  35.  *
  36.  */
  37.  
  38. extern char *History_file;
  39.  
  40. extern char *E_open;
  41. extern char *E_fseek;
  42.  
  43. extern void log();
  44. extern void fail();
  45.  
  46. #ifdef DBM
  47. static Boolean    dbopen = FALSE;
  48. #endif
  49.  
  50. #ifdef NDBM
  51. static DBM    *db = NULL;    /* History file, dbm version */
  52. #endif
  53.  
  54. static int    hfd = FAIL;    /* history file, text version */
  55. static FileBuf *hfbp = NULL;
  56.  
  57. char *
  58.   get_history_entry(msg_id, lookup)
  59. char        *msg_id;
  60. int        lookup;
  61. {
  62.     char        *fname = "get_history_entry: ";
  63.     char        *tmp;
  64.     register char    *cp;
  65.     long        ltmp;
  66. #ifdef DBM
  67.     datum        fetch();
  68. #endif /* DBM */
  69.     datum         key, content;
  70.  
  71. #ifndef BNEWS
  72.     cp = strrchr(msg_id,'@');    /* look for @ in message id */
  73.     if( cp != NULL) {
  74.     for(;*cp != '\0';++cp)
  75. #else
  76.     {
  77.     for (cp = msg_id; *cp != '\0'; ++cp)
  78. #endif
  79.       if (isupper(*cp))
  80.         *cp = tolower(*cp);
  81. /* Make ctags happy */
  82. #ifndef BNEWS
  83.     }
  84. #else
  85.     }
  86. #endif
  87. #ifdef DBM
  88.     if (!dbopen) {
  89.     if (dbminit(History_file) < 0) {
  90.         log(LOG_ERR, fname, "%s%s: dbminit %s: %s\n", Host.name,
  91.         History_file, errmsg(errno));
  92.         return NULL;
  93.     } else
  94.       dbopen = TRUE;
  95.     }
  96. #else /* ndbm */
  97.     if (db == NULL) {
  98.     db = dbm_open(History_file, O_RDONLY, 0);
  99.     if (db == NULL) {
  100.         log(LOG_ERR, fname, "%s%s: dbm_open %s: %s\n", Host.name,
  101.         History_file, errmsg(errno));
  102.         return NULL;
  103.     }
  104.     }
  105. #endif /* DBM */
  106.  
  107.     key.dptr = msg_id;
  108.     key.dsize = strlen(msg_id) + 1;
  109.  
  110. #ifdef DBM
  111.     content = fetch(key);
  112. #else /* ndbm */
  113.     content = dbm_fetch(db, key);
  114. #endif /* DBM */
  115.     if (content.dptr == NULL)
  116.       return NULL;
  117.  
  118.     /*
  119.      * If we are just checking to see if it exists return a non-NULL
  120.      * result
  121.      */
  122.     if (lookup)
  123.       return (char *)1;
  124.  
  125.     if (hfd == FAIL) {
  126.     if ((hfd = open(History_file, O_RDONLY)) == FAIL) {
  127.         log(LOG_ERR, fname, E_open, Host.name, History_file, "r",
  128.         errmsg(errno));
  129.         return NULL;
  130.     }
  131.     hfbp = fb_fdopen(hfd);
  132.     }
  133.  
  134.     memcpy((char *)<mp, content.dptr, sizeof (long));
  135.     if (fb_seek(hfbp, ltmp, 0) < 0) {
  136.     log(LOG_ERR, fname, E_fseek, Host.name, History_file, errmsg(errno));
  137.     return NULL;
  138.     }
  139.  
  140.     tmp = fb_readline(hfbp, NULL);
  141.  
  142.     if (fb_error(hfbp))
  143.       fail(fname, "%s%s: error while reading history file: %s\n", Host.name,
  144.        errmsg(errno));
  145.  
  146.     if ((cp = strchr(tmp, '\n')) != NULL)
  147.       *cp = '\0';
  148.  
  149.     cp = strchr(tmp, '\t');
  150.  
  151.     if (cp != NULL)
  152.       cp = strchr(cp+1, '\t');
  153.     else
  154.       log(LOG_ERR, fname,
  155.       "%s%s: malformed line in history file at %ld bytes, id %s\n",
  156.       Host.name, ltmp, msg_id);
  157.  
  158.     if (cp == NULL)
  159.       return NULL; /* this article has expired */
  160.  
  161.     tmp = cp+1;
  162.  
  163.     if ((cp = strchr(tmp, ' ')) != NULL)
  164.       *cp = '\0';
  165.  
  166.     while ((cp = strchr(tmp, '.')) != NULL)
  167.       *cp = '/';
  168.  
  169.     return tmp;
  170. }
  171.  
  172. void
  173.   close_history()
  174. {
  175. #ifdef DBM
  176.     if (dbopen) {
  177.     dbmclose();
  178.     dbopen = FALSE;
  179.     }
  180. #else /* ndbm */
  181.     if (db != NULL)
  182.       dbm_close();
  183. #endif
  184.  
  185.     if (hfd != FAIL) {
  186.     CLOSE(hfd);
  187.     fb_close(hfbp);
  188.     }
  189.  
  190.     return;
  191. }
  192. #endif /* LOOKUP_ARTICLE */
  193.