home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / OFFLINE / UQWK18.ZIP / UQWK18.TAR / offline.c < prev    next >
C/C++ Source or Header  |  1994-01-20  |  9KB  |  464 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "uqwk.h"
  4. /*
  5.  *  Process offline commands
  6.  */
  7.  
  8. QWKOffLine (bytes, fd)
  9. int bytes;
  10. FILE *fd;
  11. /*
  12.  *  Process offline commands.  Message is open on fd.  We
  13.  *  must be careful to leave the file pointer ready for the
  14.  *  next message.
  15.  */
  16. {
  17.     FILE *pfd;
  18.     char c, cmd[PATH_LEN];
  19.  
  20.     /* Open mail pipe to send results back to user */
  21.     sprintf (buf, "%s -s 'Results of your request' %s",
  22.             MAILER_PATH, user_name);
  23.     if (NULL == (pfd = popen (buf, "w")))
  24.     {
  25.         fprintf (stderr, "%s: can't popen() mail\n", progname);
  26.         while (bytes--) fread (&c, 1, 1, fd);
  27.         return (0);
  28.     }
  29.  
  30.     fprintf (pfd, "Here are the results of your mail to UQWK:\n");
  31.  
  32.     /* Get lines, process them */
  33.     while (GetLine (&bytes, fd))
  34.     {
  35.         /* Echo command */
  36.         fprintf (pfd, "\nCommand: %s\n", buf);
  37.  
  38.         /* Extract command */
  39.         if (1 != sscanf (buf, "%s", cmd))
  40.         {
  41.             fprintf (pfd, "Malformed command.\n");
  42.         }
  43.         else
  44.         {
  45.             /* Look up command */
  46.             if ( (!strcmp (cmd, "help")) ||
  47.                  (!strcmp (cmd, "HELP")) )
  48.             {
  49.                 Help(pfd);
  50.             }
  51.             else if ( (!strcmp (cmd, "subscribe")) ||
  52.                   (!strcmp (cmd, "SUBSCRIBE")) )
  53.             {
  54.                 Subscribe(pfd);
  55.             }
  56.             else if ( (!strcmp (cmd, "unsubscribe")) ||
  57.                   (!strcmp (cmd, "UNSUBSCRIBE")) )
  58.             {
  59.                 Unsubscribe(pfd);
  60.             }
  61.             else if ( (!strcmp (cmd, "groups")) ||
  62.                       (!strcmp (cmd, "GROUPS")) )
  63.             {
  64.                 Groups(pfd);
  65.             }
  66.             else if ( (!strcmp (cmd, "allgroups")) ||
  67.                       (!strcmp (cmd, "ALLGROUPS")) )
  68.             {
  69.                 Allgroups(pfd);
  70.             }
  71.             else if ( (!strcmp (cmd, "shell")) ||
  72.                   (!strcmp (cmd, "SHELL")) )
  73.             {
  74.                 Shell(pfd);
  75.             }
  76.             else if ( (!strcmp (cmd, "catchup")) ||
  77.                   (!strcmp (cmd, "CATCHUP")) )
  78.             {
  79.                 Catchup(pfd);
  80.             }
  81.             else
  82.             {
  83.                 fprintf (pfd, "No such command.  ");
  84.                 fprintf (pfd, "Send HELP for help.\n");
  85.             }
  86.         }
  87.     }
  88.  
  89.     fprintf (pfd, "\nEnd of commands.\n");
  90.     pclose (pfd);
  91.     return (1);
  92. }
  93.  
  94. int GetLine (bytes, fd)
  95. int *bytes;
  96. FILE *fd;
  97. /*
  98.  *  Get a line from fd, put it in buf, check for end of message
  99.  */
  100. {
  101.     int i;
  102.     unsigned char eol;
  103.  
  104.     i = 0;
  105.  
  106.     eol = QWK_EOL;
  107.     if (slnp_mode || zip_mode) eol = '\n';
  108.  
  109.     /* Read bytes until EOL or end of message */
  110.     while (*bytes)
  111.     {
  112.         fread (&buf[i], 1, 1, fd);
  113.         (*bytes)--;
  114.  
  115.         /* Lose CR's from ZipNews */
  116.         if (buf[i] == '\r') buf[i] = 0;
  117.  
  118.         if ( (buf[i] == eol) || (i == BUF_LEN-1) )
  119.         {
  120.             buf[i] = 0;
  121.             return (1);
  122.         }
  123.         i++;
  124.     }
  125.  
  126.     /* If we got here, we ran out of bytes */
  127.     return (0);
  128. }
  129.  
  130. Help (pfd)
  131. FILE *pfd;
  132. {
  133.     fprintf (pfd, "\nAvailable commands:\n\n");
  134.     fprintf (pfd, "HELP - This message.\n");
  135.     fprintf (pfd, "SUBSCRIBE newsgroup - Subscribe to named newsgroup.\n");
  136.     fprintf (pfd, "UNSUBSCRIBE newsgroup - Unsubscribe from newsgroup.\n");
  137.     fprintf (pfd, "UNSUBSCRIBE ALL - Unsubscribe from all newsgroups.\n");
  138.     fprintf (pfd, "GROUPS - List all subscribed newsgroups.\n");
  139.     fprintf (pfd, "ALLGROUPS - List all available newsgroups.\n");
  140.     fprintf (pfd, "CATCHUP newsgroup - Mark all articles as read.\n");
  141.     fprintf (pfd, "SHELL command - Execute shell command\n\n");
  142. }
  143.  
  144. Subscribe (pfd)
  145. FILE *pfd;
  146. {
  147.     struct act_ent *ap;
  148.     struct nrc_ent *np;
  149.     char group[PATH_LEN];
  150.  
  151.     /* Extract group name */
  152.     if (1 != sscanf (buf, "%*s %s", group))
  153.     {
  154.         fprintf (pfd, "Usage: SUBSCRIBE newsgroup\n");
  155.         return (0);
  156.     }
  157.  
  158.     /* We will need active file and .newsrc */
  159.     if (!ReadActive() || !ReadNewsrc())
  160.     {
  161.         fprintf (pfd, "Sorry, couldn't read system files.\n");
  162.         return (0);
  163.     }
  164.  
  165.     /* Already subscribed? */
  166.     np = nrc_list;
  167.     while (np != NULL)
  168.     {
  169.         if (!strcmp (group, np->name))
  170.         {
  171.             if (np->subscribed)
  172.             {
  173.                 fprintf (pfd, "Already subscribed to %s.\n",
  174.                     group);
  175.                 return (0);
  176.             }
  177.             else
  178.             {
  179.                 np->subscribed = 1;
  180.                 fprintf (pfd, "Okay, re-subscribed to %s.\n",
  181.                     group);
  182.                 WriteNewsrc();
  183.                 return (0);
  184.             }
  185.         }
  186.         np = np->next;
  187.     }
  188.  
  189.     /* Find group in active file */
  190.     if (NULL == (ap = FindActive (group)))
  191.     {
  192.         fprintf (pfd, "No such newsgroup: %s\n", group);
  193.         return (0);
  194.     }
  195.  
  196.     /* Okay already, add to .newsrc */
  197.     np = (struct nrc_ent *) malloc (sizeof (struct nrc_ent));
  198.     if (np == NULL) OutOfMemory();
  199.     np->name = (char *) malloc (1+strlen(group));
  200.     if (np->name == NULL) OutOfMemory();
  201.     strcpy (np->name, group);
  202.     np->subscribed = 1;
  203.  
  204.     /* Make subscription list - everything is read */
  205.     if (NULL == (np->sub = (struct sub_ent *) malloc
  206.             (sizeof (struct sub_ent)))) OutOfMemory();
  207.     np->sub->lo = 1;
  208.     np->sub->hi = ap->hi;
  209.     np->sub->next = NULL;
  210.  
  211.     np->next = nrc_list;
  212.     nrc_list = np;
  213.  
  214.     WriteNewsrc();
  215.     fprintf (pfd, "Okay, you are now subscribed to %s.\n", group);
  216.  
  217.     return (1);
  218. }
  219.  
  220. Unsubscribe (pfd)
  221. FILE *pfd;
  222. {
  223.     struct nrc_ent *np;
  224.     char group[PATH_LEN];
  225.  
  226.     /* Parse group name */
  227.     if (1 != sscanf (buf, "%*s %s", group))
  228.     {
  229.         fprintf (pfd, "Usage: UNSUBSCRIBE newsgroup\n");
  230.         return (0);
  231.     }
  232.  
  233.     /* Check for ALL */
  234.     if ( (!strcmp (group, "ALL")) || (!strcmp (group, "all")) )
  235.     {
  236.         nrc_list = NULL;
  237.         WriteNewsrc();
  238.         fprintf (pfd,
  239.           "Okay, you are now unsubscribed from all newsgroups.\n");
  240.         return (0);
  241.     }
  242.  
  243.     /* We need the .newsrc file */
  244.     if (!ReadNewsrc())
  245.     {
  246.         fprintf (pfd, "Sorry, couldn't read .newsrc\n");
  247.         return (0);
  248.     }
  249.  
  250.     /* Look for group in newsrc */
  251.     np = nrc_list;
  252.     while (np != NULL)
  253.     {
  254.         if (!strcmp (group, np->name)) break;
  255.         np = np->next;
  256.     }
  257.  
  258.     if (np == NULL)
  259.     {
  260.         fprintf (pfd, "You are not currently subscribed to %s.\n",
  261.                  group);
  262.         return (0);
  263.     }
  264.  
  265.     np->subscribed = 0;
  266.  
  267.     WriteNewsrc();
  268.     fprintf (pfd, "Okay, you are unsubscribed from %s.\n", group);
  269.  
  270.     return (1);
  271. }
  272.  
  273. Groups (pfd)
  274. FILE *pfd;
  275. {
  276.     struct nrc_ent *np;
  277.  
  278.     if (!ReadNewsrc())
  279.     {
  280.         fprintf (pfd, "Sorry, couldn't read .newsrc\n");
  281.         return (0);
  282.     }
  283.  
  284.     fprintf (pfd, "Newsgroups to which you are subscribed:\n\n");
  285.  
  286.     np = nrc_list;
  287.     while (np != NULL)
  288.     {
  289.         if (np->subscribed) fprintf (pfd, "    %s\n", np->name);
  290.         np = np->next;
  291.     }
  292.     return (1);
  293. }
  294.  
  295. Allgroups (pfd)
  296. FILE *pfd;
  297. {
  298.     struct act_ent *ap;
  299.  
  300.     if (!ReadActive())
  301.     {
  302.         fprintf (pfd, "Sorry, no newsgroups are available.\n");
  303.         return (0);
  304.     }
  305.  
  306.     fprintf (pfd, "List of available newsgroups:\n\n");
  307.  
  308.     ap = act_list;
  309.     while (ap != NULL)
  310.     {
  311.         fprintf (pfd, "    %s (%d articles)\n",
  312.             ap->name, ap->hi - ap->lo);
  313.         ap = ap->next;
  314.     }
  315.     return (1);
  316. }
  317.  
  318. Catchup (pfd)
  319. FILE *pfd;
  320. {
  321.     struct act_ent *ap;
  322.     struct nrc_ent *np;
  323.     struct sub_ent *sp, *tsp;
  324.     char group[PATH_LEN];
  325.  
  326.     /* Extract group name */
  327.     if (1 != sscanf (buf, "%*s %s", group))
  328.     {
  329.         fprintf (pfd, "Usage: CATCHUP newsgroup\n");
  330.         return (0);
  331.     }
  332.  
  333.     /* We will need active file and .newsrc */
  334.     if (!ReadActive() || !ReadNewsrc())
  335.     {
  336.         fprintf (pfd, "Sorry, couldn't read system files.\n");
  337.         return (0);
  338.     }
  339.  
  340.     /* Not subscribed? */
  341.     np = nrc_list;
  342.     while (np != NULL)
  343.     {
  344.         if (!strcmp (group, np->name))
  345.         {
  346.             if (np->subscribed)
  347.             {
  348.                 break;
  349.             }
  350.             else
  351.             {
  352.                 fprintf (pfd,
  353.                     "You are not subscribed to %s.\n",
  354.                     group);
  355.                 return (0);
  356.             }
  357.         }
  358.         np = np->next;
  359.     }
  360.  
  361.     if (np == NULL)
  362.     {
  363.         fprintf (pfd, "You are not subscribed to %s.\n", group);
  364.         return (0);
  365.     }
  366.  
  367.     /* Find group in active file */
  368.     if (NULL == (ap = FindActive (group)))
  369.     {
  370.         fprintf (pfd, "No such newsgroup: %s\n", group);
  371.         return (0);
  372.     }
  373.  
  374.     /* Free subscription list */
  375.     sp = np->sub;
  376.     while (sp != NULL)
  377.     {
  378.         tsp = sp->next;
  379.         free (sp);
  380.         sp = tsp;
  381.     }
  382.  
  383.     /* Okay already, add to .newsrc */
  384.     np = (struct nrc_ent *) malloc (sizeof (struct nrc_ent));
  385.     if (np == NULL) OutOfMemory();
  386.     np->name = (char *) malloc (1+strlen(group));
  387.     if (np->name == NULL) OutOfMemory();
  388.     strcpy (np->name, group);
  389.     np->subscribed = 1;
  390.  
  391.     /* Make subscription list - everything is read */
  392.     if (NULL == (np->sub = (struct sub_ent *) malloc
  393.             (sizeof (struct sub_ent)))) OutOfMemory();
  394.     np->sub->lo = 1;
  395.     np->sub->hi = ap->hi;
  396.     np->sub->next = NULL;
  397.  
  398.     np->next = nrc_list;
  399.     nrc_list = np;
  400.  
  401.     WriteNewsrc();
  402.     fprintf (pfd, "Okay, you are now caught up in %s.\n", group);
  403.  
  404.     return (1);
  405. }
  406.  
  407. Shell (pfd)
  408. FILE *pfd;
  409. {
  410.     int c;
  411.     FILE *cfd;
  412.  
  413.     if (strlen(buf) < 7)
  414.     {
  415.         fprintf (pfd, "Usage: SHELL command\n");
  416.         return (0);
  417.     }
  418.  
  419.     if (NULL == (cfd = popen (&buf[6], "r")))
  420.     {
  421.         fprintf (pfd, "Can't open pipe for command\n");
  422.         return (0);
  423.     }
  424.  
  425.     while (EOF != (c = fgetc (cfd)))
  426.     {
  427.         fputc ( (0xff & c), pfd);
  428.     }
  429.     fputc ('\n', pfd);
  430.     pclose (cfd);
  431.     return (1);
  432. }
  433.  
  434. OffLine (fd, bytes)
  435. FILE *fd;
  436. int bytes;
  437. /*
  438.  *  Offline command
  439.  */
  440. {
  441.     int n, read;
  442.     char *rc;
  443.     long offset;
  444.  
  445.     /* Skip header */
  446.     offset = ftell (fd);        /* Remember where we are */
  447.     rc = Fgets (buf, BUF_LEN, fd);
  448.     n = strlen (buf);
  449.     read = ftell(fd) - offset;    /* Compute bytes read */
  450.     while ( (rc != NULL) && (bytes > 0) && (n > 0) )
  451.     {
  452.         /* Get next line */
  453.         bytes -= read;
  454.         offset = ftell (fd);
  455.         rc = Fgets (buf, BUF_LEN, fd);
  456.         read = ftell(fd) - offset;
  457.         if (rc != NULL) n = strlen (buf);
  458.     }
  459.  
  460.     /* Hand off to the QWK Offline function */
  461.     QWKOffLine (bytes, fd);
  462. }
  463.  
  464.