Article View: news.software.nntp
Article #53905Re: posting news to NNTP server from Windows
From: remove 'nospam' to reply
Date: Tue, 21 Aug 2001 17:16
Date: Tue, 21 Aug 2001 17:16
407 lines
9603 bytes
9603 bytes
syoder wrote:
> Is there a simple way to post to a NNTP server from a
> MS-DOS command shell (Windows 98/NT/2000) or batch
> file.
>
> How do I post a 'Hello World' message?
>
> Thanks,
>
> Stuart Yoder
Here is some c code which does it for you.
/*
*/
#ifndef POST_UNIX
#ifndef POST_NT
#error One of (POST_UNIX, POST_NT) must be defined
#endif
#endif
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <ctype.h>
/* various flavors of Unix */
#ifdef POST_UNIX
/* (g)cc -DPOST_UNIX post.c -o post (-lsocket -lm -lnsl) */
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
/* perror to be called after failed socket calls */
#define perror_sock perror
/* how to close a socket */
#define close_sock close
#endif
/* Windows NT, /subsystem:console. The executable is supposed to work
under NT and Windows 95, but not under Win32s. */
#ifdef POST_NT
/* important note: when compiling on NT, say something like
cl /DPOST_NT /Ogaityb1 /G5Fs /ML post.c wsock32.lib */
#include <winsock.h>
#include <time.h>
/* regular perror doesn't work with WinSock under NT */
#define perror_sock(s) fprintf(stderr,"%s : WinSock error
%d\n",s,WSAGetLastError())
/* regular close doesn't work with WinSock under NT */
#define close_sock closesocket
/* NT doesn't understand unix-style sleep in seconds */
#define sleep(n) Sleep(n*1000)
#endif
#ifndef FD_ZERO
/* macros for select() not defined on VAX or HPUX
However they are defined to be something completely different
under NT WinSock, so we must use macros */
#define fd_set int
#define FD_ZERO(p) {*(p)=0;}
#define FD_SET(s,p) {*(p)|=(1<<(s));}
#define FD_ISSET(s,p) ((*(p)&(1<<(s)))!=0)
#endif
#define ASCII_CR 13
#define ASCII_LF 10
/* seconds */
#define TIMEOUT 12000
#define BUFFERSIZE 4096
char buffer[BUFFERSIZE];
int cfd; /* socket handle */
int parse_state; /* for parsing server responses */
int nretry; /* number of retries in various places */
int nbytes;
short int port=119;
int post_recv_resp(char);
int post_test_sock(int);
int main(int argc,char**argv)
{
unsigned long host_ip;
struct hostent *host_struct;
struct in_addr *host_node;
struct sockaddr_in serverUaddr;
#ifdef POST_NT
WSADATA wsaData; /* needed for WSAStartup */
#endif
if (argc!=2 && argc!=4)
{
fprintf(stderr,"Usage: post nntp-server [user [pass]] <article-file\n\
Usenet article with headers is read from standard input.\n");
return(1);
}
#ifdef SIGPIPE
signal(SIGPIPE,SIG_IGN); /* ignore broken pipes if this platform knows
them */
#endif
#ifdef POST_NT
if (WSAStartup(MAKEWORD(1,1),&wsaData))
{
perror_sock("WSAStartup()");
fprintf(stderr,"couldn't start up WinSock\n");
return(0);
}
fprintf(stderr,"Found WinSock: %s\n",wsaData.szDescription);
#endif
/* translate the hostname (argv[1]) into an ip address. If it starts
with a digit,
try to interpret it as a A.B.C.D address */
if (!isdigit(*argv[1])||(0xFFFFFFFF==(host_ip=inet_addr(argv[1]))))
{
fprintf(stderr,"Resolving host name to IP with DNS\n");
if (NULL==(host_struct=gethostbyname(argv[1])))
{
perror("gethostbyname");
fprintf(stderr,"DNS can't resolve host name %s to IP\n",argv[1]);
return(1);
}
host_node=(struct in_addr*)host_struct->h_addr;
fprintf(stderr,"Trying to connect to NNTP server at %s, ^C to
stop\n",inet_ntoa(*host_node));
host_ip=host_node->s_addr;
}
/* fill in the address to connect to */
memset(&serverUaddr,0,sizeof(serverUaddr));
serverUaddr.sin_family=PF_INET;
serverUaddr.sin_addr.s_addr=/*htonl*/(host_ip); /* already in net order
*/
serverUaddr.sin_port=htons(port);
/* try to create a socket */
if ((cfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
perror_sock("socket()");
return(1);
}
conn1:
if (0>=connect(cfd,(struct sockaddr*)&serverUaddr,sizeof(serverUaddr)))
goto conn2;
if (nretry>10000)
{
fprintf(stderr,"give up trying to connect to %s port %hd\n",
argv[1],port);
close_sock(cfd);
return(1);
}
perror_sock("connect()");
nretry++;
sleep(1);
goto conn1;
conn2:
if (!post_recv_resp('2'))
{
fprintf(stderr,"NNTP problem after connecting to %s port %hd\n",
argv[1],port);
close_sock(cfd);
return(1);
}
if (argc==4) /* user and password */
{
sprintf(buffer,"authinfo user %s\r\n",argv[2]);
nbytes=strlen(buffer);
/* send the user name to the server */
if (nbytes!=send(cfd,buffer,nbytes,0))
{
perror_sock("send authinfo user()");
return(1);
}
if (!post_recv_resp('3'))
{
fprintf(stderr,"NNTP problem while trying to send authinfo user\n");
return(1);
}
sprintf(buffer,"authinfo pass %s\r\n",argv[3]);
nbytes=strlen(buffer);
/* send the password to the server */
if (nbytes!=send(cfd,buffer,nbytes,0))
{
perror_sock("send authinfo pass()");
return(1);
}
if (!post_recv_resp('2'))
{
fprintf(stderr,"NNTP problem while trying to send authinfo pass\n");
return(1);
}
} /* end user and password */
/* send the post command to the server */
if (6!=send(cfd,"POST\r\n",6,0))
{
perror_sock("POST send()");
return(1);
}
if (!post_recv_resp('3'))
{
fprintf(stderr,"NNTP problem while trying to post\n");
return(1);
}
fprintf(stderr,"Sending the article from stdin\n");
while(NULL!=fgets(buffer,sizeof(buffer),stdin))
{
nbytes=strlen(buffer);
if (nbytes && buffer[nbytes-1]=='\n') /* trailing lf from fgets */
nbytes--;
while (nbytes && buffer[nbytes-1]==' ') /* trailing spaces */
nbytes--;
if (nbytes==1 && buffer[0]=='.')
{
/* single dot to dot-space */
nbytes++;
buffer[1]=' ';
}
buffer[nbytes]='\r';
nbytes++;
buffer[nbytes]='\n';
nbytes++;
buffer[nbytes]=0;
if (nbytes!=send(cfd,buffer,nbytes,0))
{
perror_sock("send stdin line()");
return(1);
}
}
fprintf(stderr,"Waiting for the OK\n");
if (3!=send(cfd,".\r\n",3,0))
{
perror_sock("POST send()");
return(1);
}
if (!post_recv_resp('2'))
{
fprintf(stderr,"NNTP problem while trying to post\n");
return(1);
}
fprintf(stderr,"Success!\n");
return(0);
}
int post_recv_resp(char c)
{
int i;
parse_state=0;
nretry=0;
recv_resp:
if (!post_test_sock(cfd)) /* nothing to read */
{
if (nretry>TIMEOUT)
{
fprintf(stderr,"timeout waiting to recv response\n");
return(0);
}
fprintf(stderr,".");
nretry++;
sleep(1);
goto recv_resp;
}
nbytes=recv(cfd,buffer,sizeof(buffer),0);
if (nbytes<0) /* an error shouldn't happen here */
{
perror_sock("response recv()");
return(0);
}
/* #ifdef DEBUG */
fwrite(buffer,1,nbytes,stderr); /* for debugging only!! */
/* #endif */
/* now see if what we received makes sense */
for (i=0; i<nbytes; i++)
{
switch(parse_state)
{
case 0:
if (buffer[i]==c)
parse_state=1;
else
goto recv_bad_resp;
break;
case 1:
if (buffer[i]==ASCII_CR)
parse_state=2;
break;
case 2:
if (buffer[i]==ASCII_LF)
parse_state=3;
else
goto recv_bad_resp;
break;
case 3: /* more data after final \n */
goto recv_bad_resp;
}
}
if (parse_state!=3)
goto recv_resp;
/* normal competion */
return(1);
recv_bad_resp:
fprintf(stderr,"Unexpected response (expected %cxx message) ",c);
if (i)
{
fprintf(stderr,"after \"");
fwrite(buffer,1,i,stderr);
fprintf(stderr,"\" ");
}
if (i<nbytes)
{
fprintf(stderr,"before \"");
fwrite(buffer+i,1,nbytes-i,stderr);
fprintf(stderr,"\"");
}
fprintf(stderr,"\n");
return(0);
}
/* use select to see if there's data here.
There don't seem to be any unixes left which understand poll and not
select.*/
int post_test_sock(int sock)
{
fd_set setm;
static struct timeval zerotime={0,0};
FD_ZERO(&setm);
FD_SET(sock,&setm);
if (select(sock+1,&setm,NULL,NULL,&zerotime)<0)
{
perror_sock("select()");
}
if (FD_ISSET(sock,&setm))
return(1);
else
return(0);
}
Usage:
post Usenet-server <file
or
post Usenet-server user pass <file
The text file should already contain
All the Usenet headers that you want included, in any random order:
Required
Newsgroups:
Subject:
From:
Date: (e.g. Sat, 1 Apr 94 14:15:16 GMT)
Recommended
Path:
Message-id:
Organization:
Optional
Approved:
Keywords:
Supersedes: (another article's message-id)
Distribution:
Followup-To:
Expires: (same format as Date:)
X-Newsreader:
X-Pedophile:
X-Face:
etc.
Followed by a single blank line, followed by the body of your article.
Technical Details
Message-ID:
<3B829754.3E62CA6B@nospam.nospam>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!sn-xit-01!supernews.com!news.alt.net!usenetReferences:
<ad07e1ae.0108201444.6660764e@posting.google.com>Show Full Headers
Complete Article Headers
Message-ID: <3B829754.3E62CA6B@nospam.nospam> Subject: Re: posting news to NNTP server from Windows Date: Tue, 21 Aug 2001 13:16:05 -0400 From: remove 'nospam' to reply <nospam@nospam.nospam> References: <ad07e1ae.0108201444.6660764e@posting.google.com> Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!sn-xit-01!supernews.com!news.alt.net!usenet Newsgroups: news.software.nntp Organization: nospam Lines: 406 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 4.7 [en] (WinNT; I) X-Accept-Language: en