home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <string.h>
- #include "uqwk.h"
- /*
- * All sorts of stuff to do news processing
- */
-
- DoNews ()
- /*
- * Collect unread news into QWK packet
- */
- {
- struct act_ent *ap;
- struct nrc_ent *np;
-
- /* Read .newsrc file */
- if (!ReadNewsrc()) return (0);
-
- /* And active file */
- if (!ReadActive()) return (0);
-
- /* Look through the newsrc file */
- np = nrc_list;
- while (np != NULL)
- {
- /* Check if too many blocks already */
- if ( (blk_cnt >= max_blks) && (max_blks > 0) )
- {
- return (0);
- }
-
- if ( (np->subscribed) &&
- (NULL != (ap = FindActive (np->name))) )
- {
- /* Do this group */
- DoGroup (np, ap);
- }
- np = np->next;
- }
- }
-
- int ReadNewsrc()
- /*
- * Read the .newsrc file
- */
- {
- char group_name[PATH_LEN];
- struct nrc_ent *np;
- int n, c;
-
- /* Don't bother if we've alread read it */
- if (nrc_list != NULL) return (1);
-
- /* Open it */
- if (NULL == (nrc_fd = fopen (nrc_file, "r")))
- {
- fprintf (stderr, "%s: can't open %s\n", progname, nrc_file);
- return (0);
- }
-
- /* Read through */
- while (NULL != Fgets (buf, BUF_LEN, nrc_fd))
- {
- /* Allocate a new nrc entry */
- np = (struct nrc_ent *) malloc (sizeof (struct nrc_ent));
- if (np == NULL) OutOfMemory();
-
- /* Parse group name */
- sscanf (buf, "%s", group_name);
- n = strlen (group_name);
-
- if (group_name[n-1] == ':')
- {
- np->subscribed = 1;
- }
- else
- {
- np->subscribed = 0;
- }
-
- group_name[n-1] = 0;
- np->name = (char *) malloc (n);
- if (np->name == NULL) OutOfMemory();
- strcpy (np->name, group_name);
-
- /* Hi article number */
- np->hi = LastInt (buf);
-
- /* Add to nrc list */
- np->next = nrc_list;
- nrc_list = np;
- }
-
- /* Walk through the nrc list, assign conference numbers */
- np = nrc_list;
- c = 0;
- while (np != NULL)
- {
- np->conf = c;
- c++;
- np = np->next;
- }
-
- fclose (nrc_fd);
- return (1);
- }
-
- int ReadActive()
- /*
- * Read active file
- */
- {
- char group_name[PATH_LEN];
- struct act_ent *ap;
- int n;
-
- /* Don't bother if it's already here */
- if (act_list != NULL) return (1);
-
- /* Open the active file */
- if (NULL == (act_fd = fopen (act_file, "r")))
- {
- fprintf (stderr, "%s: can't open %s\n", progname, act_file);
- return (0);
- }
-
- /* Read through it */
- while (NULL != Fgets (buf, BUF_LEN, act_fd))
- {
- /* Get new act entry */
- ap = (struct act_ent *) malloc (sizeof (struct act_ent));
- if (ap == NULL) OutOfMemory();
-
- /* Parse name, message numbers */
- sscanf (buf, "%s %d %d", group_name, &ap->hi, &ap->lo);
- ap->name = (char *) malloc (1+strlen(group_name));
- if (ap->name == NULL) OutOfMemory();
- strcpy (ap->name, group_name);
-
- /* Add to list */
- ap->next = act_list;
- act_list = ap;
- }
-
- fclose (act_fd);
- return (1);
- }
-
- struct act_ent *FindActive (c)
- char *c;
- /*
- * Look for group's active entry given group name
- */
- {
- struct act_ent *ap;
-
- ap = act_list;
- while (NULL != ap)
- {
- if (!strcmp (c, ap->name)) return (ap);
- ap = ap->next;
- }
- return (NULL);
- }
-
- DoGroup (np, ap)
- struct nrc_ent *np;
- struct act_ent *ap;
- /*
- * Process given group
- */
- {
- char news_path[PATH_LEN];
- char art_file[PATH_LEN];
- int i, n;
-
- printf ("%s: %s\n", progname, np->name);
-
- /* Make a new conference with this name */
- NewConference (np->name);
-
- /* Construct path name for articles in this group */
- strcpy (news_path, news_dir);
- strcat (news_path, "/");
- strcat (news_path, np->name);
- strcat (news_path, "/");
- n = strlen (news_path);
- for (i=0; i<n; i++) if (news_path[i] == '.') news_path[i] = '/';
-
- /* If highest read article is greater than highest available
- article, assume group has been reset */
- if (np->hi > ap->hi) np->hi = ap->lo;
-
- /* Look through unread articles */
- for (i=np->hi+1; i<=ap->hi; i++)
- {
- /* Check max block count */
- if ( (blk_cnt >= max_blks) && (max_blks > 0) )
- {
- fclose (ndx_fd);
- np->hi = i;
- return (0);
- }
-
- /* Construct article's file name */
- sprintf (art_file, "%s%d", news_path, i);
-
- /* Process this article */
- DoArticle (art_file);
- }
- fclose (ndx_fd);
-
- /* Reset hi article number */
- np->hi = ap->hi;
- }
-
- DoArticle (art_file)
- char *art_file;
- {
- struct qwk_hdr hdr;
- struct stat stat_buf;
- long txt_offset, end_offset;
- int n, out_bytes;
- char ndx[5], *eof, from[PATH_LEN];
- FILE *art_fd;
-
- /* Forget it if we can't open the article */
- if (NULL == (art_fd = fopen (art_file, "r"))) return (0);
-
- /* stat() the article to get file size */
- if (0 != stat (art_file, &stat_buf))
- {
- fclose (art_fd);
- return (0);
- }
- end_offset = stat_buf.st_size;
-
- /* Skip empty articles */
- if (end_offset == 0)
- {
- fclose (art_fd);
- return (0);
- }
-
- /* Write the index file entry */
- inttoms (blk_cnt, ndx);
- ndx[4] = conf_cnt - 1;
- fwrite (ndx, 5, 1, ndx_fd);
-
- Spaces (&hdr, 128);
-
- /* Fill in some header fields */
- hdr.status = QWK_PUBLIC;
- PadNum (msg_cnt, hdr.number, 7);
- Spaces (hdr.password, 12);
- Spaces (hdr.refer, 8);
- hdr.flag = QWK_ACT_FLAG;
- IntNum (conf_cnt-1, hdr.conference);
- IntNum (msg_cnt+1, hdr.msg_num);
- hdr.tag = ' ';
- PadString ("ALL", hdr.to, 25);
-
- msg_cnt++;
-
- /* Process header lines */
- eof = Fgets (buf, BUF_LEN, art_fd);
- while ( (0 != strlen(buf)) && (eof != NULL) )
- {
- if (!strncmp (buf, "Date: ", 6))
- {
- ParseDate (&buf[6], &hdr);
- }
- else if (!strncmp (buf, "Subject: ", 9))
- {
- PadString (&buf[9], hdr.subject, 25);
- }
- else if (!strncmp (buf, "From: ", 6))
- {
- sscanf (&buf[6], "%s", from);
- PadString (from, hdr.from, 25);
- }
-
- eof = Fgets (buf, BUF_LEN, art_fd);
- }
-
- txt_offset = ftell (art_fd);
-
- /* Compute block count */
- if (inc_hdrs)
- {
- PadNum (2+end_offset/128, hdr.blocks, 6);
- blk_cnt += 1+end_offset/128;
- }
- else
- {
- PadNum (2+(end_offset-txt_offset)/128, hdr.blocks, 6);
- blk_cnt += 1+(end_offset-txt_offset)/128;
- }
-
- /* Write the message header */
- fwrite (&hdr, 128, 1, msg_fd);
- blk_cnt++;
-
- /* Now write the article's text */
- if (inc_hdrs) fseek (art_fd, 0, 0);
- out_bytes = 0;
-
- while (NULL != Fgets (buf, BUF_LEN, art_fd))
- {
- n = strlen (buf);
- fwrite (buf, n, 1, msg_fd);
- out_bytes += n;
-
- if (n < BUF_LEN-1)
- {
- fputc (QWK_EOL, msg_fd);
- out_bytes++;
- }
- }
-
- /* Pad block as necessary */
- n = out_bytes % 128;
- for (;n<128;n++) fputc (' ', msg_fd);
-
- fclose (art_fd);
- }
-
- OutOfMemory()
- {
- fprintf (stderr, "%s: out of memory\n", progname);
- exit (0);
- }
-