home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <string.h>
- #include "uqwk.h"
- /*
- * Process offline commands
- */
-
- OffLine (bytes)
- int bytes;
- /*
- * Process offline commands. Message is open on rep_fd. We
- * must be careful to leave the file pointer ready for the
- * next message.
- */
- {
- FILE *pfd;
- char c, cmd[PATH_LEN];
-
- /* Open mail pipe to send results back to user */
- sprintf (buf, "%s -s 'Results of your request' %s",
- MAILER_PATH, user_name);
- if (NULL == (pfd = popen (buf, "w")))
- {
- fprintf (stderr, "%s: can't popen() mail\n", progname);
- while (bytes--) fread (&c, 1, 1, rep_fd);
- return (0);
- }
-
- fprintf (pfd, "Here are the results of your mail to UQWK:\n");
-
- /* Get lines, process them */
- while (GetLine(&bytes))
- {
- /* Echo command */
- fprintf (pfd, "\nCommand: %s\n", buf);
-
- /* Extract command */
- if (1 != sscanf (buf, "%s", cmd))
- {
- fprintf (pfd, "Malformed command.\n");
- }
- else
- {
- /* Look up command */
- if ( (!strcmp (cmd, "help")) ||
- (!strcmp (cmd, "HELP")) )
- {
- Help(pfd);
- }
- else if ( (!strcmp (cmd, "subscribe")) ||
- (!strcmp (cmd, "SUBSCRIBE")) )
- {
- Subscribe(pfd);
- }
- else if ( (!strcmp (cmd, "unsubscribe")) ||
- (!strcmp (cmd, "UNSUBSCRIBE")) )
- {
- Unsubscribe(pfd);
- }
- else if ( (!strcmp (cmd, "groups")) ||
- (!strcmp (cmd, "GROUPS")) )
- {
- Groups(pfd);
- }
- else if ( (!strcmp (cmd, "allgroups")) ||
- (!strcmp (cmd, "ALLGROUPS")) )
- {
- Allgroups(pfd);
- }
- else
- {
- fprintf (pfd, "No such command. ");
- fprintf (pfd, "Send HELP for help.\n");
- }
- }
- }
-
- fprintf (pfd, "\nEnd of commands.\n");
- pclose (pfd);
- }
-
- int GetLine (bytes)
- int *bytes;
- /*
- * Get a line from rep_fd, put it in buf, check for end of message
- */
- {
- int i;
- i = 0;
-
- /* Read bytes until EOL or end of message */
- while (*bytes)
- {
- fread (&buf[i], 1, 1, rep_fd);
- (*bytes)--;
-
- if ( (buf[i] == QWK_EOL) || (i == BUF_LEN-1) )
- {
- buf[i] = 0;
- return (1);
- }
- i++;
- }
-
- /* If we got here, we ran out of bytes */
- return (0);
- }
-
- Help (pfd)
- FILE *pfd;
- {
- fprintf (pfd, "\nAvailable commands:\n\n");
- fprintf (pfd, "HELP - This message.\n");
- fprintf (pfd, "SUBSCRIBE newsgroup - Subscribe to named newsgroup.\n");
- fprintf (pfd, "UNSUBSCRIBE newsgroup - Unsubscribe from newsgroup.\n");
- fprintf (pfd, "UNSUBSCRIBE ALL - Unsubscribe from all newsgroups.\n");
- fprintf (pfd, "GROUPS - List all subscribed newsgroups.\n");
- fprintf (pfd, "ALLGROUPS - List all available newsgroups.\n\n");
- }
-
- Subscribe (pfd)
- FILE *pfd;
- {
- struct act_ent *ap;
- struct nrc_ent *np;
- char group[PATH_LEN];
-
- /* Extract group name */
- if (1 != sscanf (buf, "%*s %s", group))
- {
- fprintf (pfd, "Usage: SUBSCRIBE newsgroup\n");
- return (0);
- }
-
- /* We will need active file and .newsrc */
- if (!ReadActive() || !ReadNewsrc())
- {
- fprintf (pfd, "Sorry, couldn't read system files.\n");
- return (0);
- }
-
- /* Already subscribed? */
- np = nrc_list;
- while (np != NULL)
- {
- if (!strcmp (group, np->name))
- {
- if (np->subscribed)
- {
- fprintf (pfd, "Already subscribed to %s.\n",
- group);
- return (0);
- }
- else
- {
- np->subscribed = 1;
- fprintf (pfd, "Okay, re-subscribed to %s.\n",
- group);
- WriteNewsrc();
- return (0);
- }
- }
- np = np->next;
- }
-
- /* Find group in active file */
- if (NULL == (ap = FindActive (group)))
- {
- fprintf (pfd, "No such newsgroup: %s\n", group);
- return (0);
- }
-
- /* Okay already, add to .newsrc */
- np = (struct nrc_ent *) malloc (sizeof (struct nrc_ent));
- if (np == NULL) OutOfMemory();
- np->name = (char *) malloc (1+strlen(group));
- if (np->name == NULL) OutOfMemory();
- strcpy (np->name, group);
- np->subscribed = 1;
- np->hi = ap->hi;
- np->next = nrc_list;
- nrc_list = np;
-
- WriteNewsrc();
- fprintf (pfd, "Okay, you are now subscribed to %s.\n", group);
- }
-
- Unsubscribe (pfd)
- FILE *pfd;
- {
- struct nrc_ent *np;
- char group[PATH_LEN];
-
- /* Parse group name */
- if (1 != sscanf (buf, "%*s %s", group))
- {
- fprintf (pfd, "Usage: UNSUBSCRIBE newsgroup\n");
- return (0);
- }
-
- /* Check for ALL */
- if ( (!strcmp (group, "ALL")) || (!strcmp (group, "all")) )
- {
- nrc_list = NULL;
- WriteNewsrc();
- fprintf (pfd,
- "Okay, you are now unsubscribed from all newsgroups.\n");
- return (0);
- }
-
- /* We need the .newsrc file */
- if (!ReadNewsrc())
- {
- fprintf (pfd, "Sorry, couldn't read .newsrc\n");
- return (0);
- }
-
- /* Look for group in newsrc */
- np = nrc_list;
- while (np != NULL)
- {
- if (!strcmp (group, np->name)) break;
- np = np->next;
- }
-
- if (np == NULL)
- {
- fprintf (pfd, "You are not currently subscribed to %s.\n",
- group);
- return (0);
- }
-
- np->subscribed = 0;
-
- WriteNewsrc();
- fprintf (pfd, "Okay, you are unsubscribed from %s.\n", group);
- }
-
- Groups (pfd)
- FILE *pfd;
- {
- struct nrc_ent *np;
-
- if (!ReadNewsrc())
- {
- fprintf (pfd, "Sorry, couldn't read .newsrc\n");
- return (0);
- }
-
- fprintf (pfd, "Newsgroups to which you are subscribed:\n\n");
-
- np = nrc_list;
- while (np != NULL)
- {
- fprintf (pfd, " %s\n", np->name);
- np = np->next;
- }
- }
-
- Allgroups (pfd)
- FILE *pfd;
- {
- struct act_ent *ap;
-
- if (!ReadActive())
- {
- fprintf (pfd, "Sorry, no newsgroups are available.\n");
- return (0);
- }
-
- fprintf (pfd, "List of available newsgroups:\n\n");
-
- ap = act_list;
- while (ap != NULL)
- {
- fprintf (pfd, " %s (%d articles)\n",
- ap->name, ap->hi - ap->lo);
- ap = ap->next;
- }
- }
-