home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume36 / uqwk / part01 / offline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-11  |  5.3 KB  |  281 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "uqwk.h"
  4. /*
  5.  *  Process offline commands
  6.  */
  7.  
  8. OffLine (bytes)
  9. int bytes;
  10. /*
  11.  *  Process offline commands.  Message is open on rep_fd.  We
  12.  *  must be careful to leave the file pointer ready for the
  13.  *  next message.
  14.  */
  15. {
  16.     FILE *pfd;
  17.     char c, cmd[PATH_LEN];
  18.  
  19.     /* Open mail pipe to send results back to user */
  20.     sprintf (buf, "%s -s 'Results of your request' %s",
  21.             MAILER_PATH, user_name);
  22.     if (NULL == (pfd = popen (buf, "w")))
  23.     {
  24.         fprintf (stderr, "%s: can't popen() mail\n", progname);
  25.         while (bytes--) fread (&c, 1, 1, rep_fd);
  26.         return (0);
  27.     }
  28.  
  29.     fprintf (pfd, "Here are the results of your mail to UQWK:\n");
  30.  
  31.     /* Get lines, process them */
  32.     while (GetLine(&bytes))
  33.     {
  34.         /* Echo command */
  35.         fprintf (pfd, "\nCommand: %s\n", buf);
  36.  
  37.         /* Extract command */
  38.         if (1 != sscanf (buf, "%s", cmd))
  39.         {
  40.             fprintf (pfd, "Malformed command.\n");
  41.         }
  42.         else
  43.         {
  44.             /* Look up command */
  45.             if ( (!strcmp (cmd, "help")) ||
  46.                  (!strcmp (cmd, "HELP")) )
  47.             {
  48.                 Help(pfd);
  49.             }
  50.             else if ( (!strcmp (cmd, "subscribe")) ||
  51.                   (!strcmp (cmd, "SUBSCRIBE")) )
  52.             {
  53.                 Subscribe(pfd);
  54.             }
  55.             else if ( (!strcmp (cmd, "unsubscribe")) ||
  56.                   (!strcmp (cmd, "UNSUBSCRIBE")) )
  57.             {
  58.                 Unsubscribe(pfd);
  59.             }
  60.             else if ( (!strcmp (cmd, "groups")) ||
  61.                       (!strcmp (cmd, "GROUPS")) )
  62.             {
  63.                 Groups(pfd);
  64.             }
  65.             else if ( (!strcmp (cmd, "allgroups")) ||
  66.                       (!strcmp (cmd, "ALLGROUPS")) )
  67.             {
  68.                 Allgroups(pfd);
  69.             }
  70.             else
  71.             {
  72.                 fprintf (pfd, "No such command.  ");
  73.                 fprintf (pfd, "Send HELP for help.\n");
  74.             }
  75.         }
  76.     }
  77.  
  78.     fprintf (pfd, "\nEnd of commands.\n");
  79.     pclose (pfd);
  80. }
  81.  
  82. int GetLine (bytes)
  83. int *bytes;
  84. /*
  85.  *  Get a line from rep_fd, put it in buf, check for end of message
  86.  */
  87. {
  88.     int i;
  89.     i = 0;
  90.  
  91.     /* Read bytes until EOL or end of message */
  92.     while (*bytes)
  93.     {
  94.         fread (&buf[i], 1, 1, rep_fd);
  95.         (*bytes)--;
  96.  
  97.         if ( (buf[i] == QWK_EOL) || (i == BUF_LEN-1) )
  98.         {
  99.             buf[i] = 0;
  100.             return (1);
  101.         }
  102.         i++;
  103.     }
  104.  
  105.     /* If we got here, we ran out of bytes */
  106.     return (0);
  107. }
  108.  
  109. Help (pfd)
  110. FILE *pfd;
  111. {
  112.     fprintf (pfd, "\nAvailable commands:\n\n");
  113.     fprintf (pfd, "HELP - This message.\n");
  114.     fprintf (pfd, "SUBSCRIBE newsgroup - Subscribe to named newsgroup.\n");
  115.     fprintf (pfd, "UNSUBSCRIBE newsgroup - Unsubscribe from newsgroup.\n");
  116.     fprintf (pfd, "UNSUBSCRIBE ALL - Unsubscribe from all newsgroups.\n");
  117.     fprintf (pfd, "GROUPS - List all subscribed newsgroups.\n");
  118.     fprintf (pfd, "ALLGROUPS - List all available newsgroups.\n\n");
  119. }
  120.  
  121. Subscribe (pfd)
  122. FILE *pfd;
  123. {
  124.     struct act_ent *ap;
  125.     struct nrc_ent *np;
  126.     char group[PATH_LEN];
  127.  
  128.     /* Extract group name */
  129.     if (1 != sscanf (buf, "%*s %s", group))
  130.     {
  131.         fprintf (pfd, "Usage: SUBSCRIBE newsgroup\n");
  132.         return (0);
  133.     }
  134.  
  135.     /* We will need active file and .newsrc */
  136.     if (!ReadActive() || !ReadNewsrc())
  137.     {
  138.         fprintf (pfd, "Sorry, couldn't read system files.\n");
  139.         return (0);
  140.     }
  141.  
  142.     /* Already subscribed? */
  143.     np = nrc_list;
  144.     while (np != NULL)
  145.     {
  146.         if (!strcmp (group, np->name))
  147.         {
  148.             if (np->subscribed)
  149.             {
  150.                 fprintf (pfd, "Already subscribed to %s.\n",
  151.                     group);
  152.                 return (0);
  153.             }
  154.             else
  155.             {
  156.                 np->subscribed = 1;
  157.                 fprintf (pfd, "Okay, re-subscribed to %s.\n",
  158.                     group);
  159.                 WriteNewsrc();
  160.                 return (0);
  161.             }
  162.         }
  163.         np = np->next;
  164.     }
  165.  
  166.     /* Find group in active file */
  167.     if (NULL == (ap = FindActive (group)))
  168.     {
  169.         fprintf (pfd, "No such newsgroup: %s\n", group);
  170.         return (0);
  171.     }
  172.  
  173.     /* Okay already, add to .newsrc */
  174.     np = (struct nrc_ent *) malloc (sizeof (struct nrc_ent));
  175.     if (np == NULL) OutOfMemory();
  176.     np->name = (char *) malloc (1+strlen(group));
  177.     if (np->name == NULL) OutOfMemory();
  178.     strcpy (np->name, group);
  179.     np->subscribed = 1;
  180.     np->hi = ap->hi;
  181.     np->next = nrc_list;
  182.     nrc_list = np;
  183.  
  184.     WriteNewsrc();
  185.     fprintf (pfd, "Okay, you are now subscribed to %s.\n", group);
  186. }
  187.  
  188. Unsubscribe (pfd)
  189. FILE *pfd;
  190. {
  191.     struct nrc_ent *np;
  192.     char group[PATH_LEN];
  193.  
  194.     /* Parse group name */
  195.     if (1 != sscanf (buf, "%*s %s", group))
  196.     {
  197.         fprintf (pfd, "Usage: UNSUBSCRIBE newsgroup\n");
  198.         return (0);
  199.     }
  200.  
  201.     /* Check for ALL */
  202.     if ( (!strcmp (group, "ALL")) || (!strcmp (group, "all")) )
  203.     {
  204.         nrc_list = NULL;
  205.         WriteNewsrc();
  206.         fprintf (pfd,
  207.           "Okay, you are now unsubscribed from all newsgroups.\n");
  208.         return (0);
  209.     }
  210.  
  211.     /* We need the .newsrc file */
  212.     if (!ReadNewsrc())
  213.     {
  214.         fprintf (pfd, "Sorry, couldn't read .newsrc\n");
  215.         return (0);
  216.     }
  217.  
  218.     /* Look for group in newsrc */
  219.     np = nrc_list;
  220.     while (np != NULL)
  221.     {
  222.         if (!strcmp (group, np->name)) break;
  223.         np = np->next;
  224.     }
  225.  
  226.     if (np == NULL)
  227.     {
  228.         fprintf (pfd, "You are not currently subscribed to %s.\n",
  229.                  group);
  230.         return (0);
  231.     }
  232.  
  233.     np->subscribed = 0;
  234.  
  235.     WriteNewsrc();
  236.     fprintf (pfd, "Okay, you are unsubscribed from %s.\n", group);
  237. }
  238.  
  239. Groups (pfd)
  240. FILE *pfd;
  241. {
  242.     struct nrc_ent *np;
  243.  
  244.     if (!ReadNewsrc())
  245.     {
  246.         fprintf (pfd, "Sorry, couldn't read .newsrc\n");
  247.         return (0);
  248.     }
  249.  
  250.     fprintf (pfd, "Newsgroups to which you are subscribed:\n\n");
  251.  
  252.     np = nrc_list;
  253.     while (np != NULL)
  254.     {
  255.         fprintf (pfd, "    %s\n", np->name);
  256.         np = np->next;
  257.     }
  258. }
  259.  
  260. Allgroups (pfd)
  261. FILE *pfd;
  262. {
  263.     struct act_ent *ap;
  264.  
  265.     if (!ReadActive())
  266.     {
  267.         fprintf (pfd, "Sorry, no newsgroups are available.\n");
  268.         return (0);
  269.     }
  270.  
  271.     fprintf (pfd, "List of available newsgroups:\n\n");
  272.  
  273.     ap = act_list;
  274.     while (ap != NULL)
  275.     {
  276.         fprintf (pfd, "    %s (%d articles)\n",
  277.             ap->name, ap->hi - ap->lo);
  278.         ap = ap->next;
  279.     }
  280. }
  281.