1 /* $Id: feedone.c 6135 2003-01-19 01:15:40Z rra $
3 ** Connect to the NNTP server and feed one article.
10 #include "inn/messages.h"
15 static FILE *FromServer;
16 static FILE *ToServer;
21 ** Read a line from the server or die trying.
24 GetFromServer(buff, size, text)
29 if (fgets(buff, size, FromServer) == NULL)
32 printf("S: %s", buff);
37 ** Flush a stdio FILE; exit if there are any errors.
43 if (fflush(F) == EOF || ferror(F))
44 sysdie("cannot send text to server");
55 fprintf(ToServer, "quit\r\n");
58 GetFromServer(buff, sizeof buff, "cannot get reply to quit");
66 fprintf(stderr, "Usage: feedone [-r|-m msgid] [-p] [-t] articlefile\n");
76 static char MESGIDHDR[] = "Message-ID:";
89 message_program_name = "feedone";
92 while ((i = getopt(ac, av, "m:prt")) != EOF)
97 case 'm': /* Specified Message-ID */
101 mesgid = concat("<", optarg, ">", (char *) 0);
103 case 'p': /* Use Post, not ihave */
106 case 'r': /* Random Message-ID */
107 length = snprintf(NULL, 0, "<%ld@%ld>", (long) getpid(),
109 mesgid = xmalloc(length + 1);
110 snprintf(mesgid, length, "<%ld@%ld>", (long) getpid(),
120 /* One argument; the input filename. */
123 if ((F = fopen(av[0], "r")) == NULL)
124 sysdie("cannot open input");
126 /* Scan for the message-id. */
127 if (mesgid == NULL) {
128 while (fgets(buff, sizeof buff, F) != NULL)
129 if (strncasecmp(buff, MESGIDHDR, strlen(MESGIDHDR)) == 0) {
130 if ((p = strchr(buff, '<')) == NULL
131 || (q = strchr(p, '>')) == NULL)
132 die("bad message ID line");
138 die("no message ID");
141 /* Connect to the server. */
142 if (NNTPremoteopen(NNTP_PORT, &FromServer, &ToServer, buff) < 0
143 || FromServer == NULL
144 || ToServer == NULL) {
146 warn("server says: %s", buff);
147 sysdie("cannot connect to server");
150 /* Does the server want this article? */
152 fprintf(ToServer, "post\r\n");
153 i = NNTP_START_POST_VAL;
156 fprintf(ToServer, "ihave %s\r\n", mesgid);
160 GetFromServer(buff, sizeof buff, "cannot offer article to server");
161 if (atoi(buff) != i) {
162 warn("server doesn't want the article: %s", buff);
166 /* Send the file over. */
167 fseeko(F, 0, SEEK_SET);
168 while (fgets(buff, sizeof buff, F) != NULL) {
169 if (strncasecmp(buff, MESGIDHDR, strlen(MESGIDHDR)) == 0) {
170 fprintf(ToServer, "%s %s\r\n", MESGIDHDR, mesgid);
173 if ((p = strchr(buff, '\n')) != NULL)
175 fprintf(ToServer, buff[0] == '.' ? ".%s\r\n" : "%s\r\n",
179 fprintf(ToServer, ".\r\n");
183 /* How did the server respond? */
184 GetFromServer(buff, sizeof buff,
185 "no reply from server after sending the article");
186 i = PostMode ? NNTP_POSTEDOK_VAL : NNTP_TOOKIT_VAL;
188 sysdie("cannot send article to the server: %s", buff);