home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Professional
/
OS2PRO194.ISO
/
os2
/
com
/
utils
/
smplnews
/
post.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-08-02
|
11KB
|
368 lines
/*
SNEWS 2.0
snews - posting stuff
Copyright (C) 1991 John McCombs, Christchurch, NEW ZEALAND
john@ahuriri.gen.nz
PO Box 2708, Christchurch, NEW ZEALAND
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 1, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
See the file COPYING, which contains a copy of the GNU General
Public License.
*/
#include "defs.h"
#include "snews.h"
#include <io.h>
#include <time.h>
/*------------------------------- post an article --------------------------*/
void post(TEXT *tx, char *gx)
{
/*
* Ask for the data we are not given in the arguments,
* let him edit the article, then post it
*/
FILE *article, *sig;
LINE *ln;
char buf[MAXLINE];
char fn[256], newsgroups[256], subject[256];
char sig_fn[80];
char author[80], msg_id[256];
int ch;
strcpy(fn, my_stuff.news_dir);
strcat(fn, "article");
unlink(fn);
strcpy(newsgroups, gx);
strcpy(author, "");
strcpy(msg_id, "");
if ((article = fopen(fn, "w+")) != NULL) {
if (strlen(newsgroups) == 0) {
/* if no groups ask for them */
lmessage("Newsgroups? ");
gets(newsgroups);
} else {
/* there is a group ask if he wants to change */
sprintf(buf, "Group: %s, post to *this* group (y/n)?", newsgroups);
message(buf);
while (((ch = getch()) != 'y') && (ch != 'n'));
if (ch == 'n') {
lmessage("Newsgroups? ");
gets(newsgroups);
}
}
/* check for a valid newsgroups line */
if (newsgroups_valid(newsgroups)) {
/* add the quoted message */
/* are we quoting */
if (tx != NULL) {
if (strlen(tx->subject) == 0) {
lmessage("Subject? ");
gets(subject);
}
else
if ( strnicmp(tx->subject, "Re: ", 4) != 0 )
sprintf(subject, "Re: %s", tx->subject);
else
strcpy(subject, tx->subject);
message("Quote article (y/n)? ");
while (((ch = getch()) != 'y') && (ch != 'n'));
if (ch == 'y') {
get_his_stuff(tx, author, msg_id);
fprintf(article, "%s writes in article %s:\n", author, msg_id);
ln = tx->start;
while (ln != NULL) {
fprintf(article, "> %s", ln->data);
ln = ln->next;
}
}
}
else {
lmessage("Subject? ");
gets(subject);
}
/* append the signature if there is one */
strcpy(sig_fn, my_stuff.home);
strcat(sig_fn, my_stuff.signature);
if ((sig = fopen(sig_fn, "rt")) != NULL) {
while (fgets(buf, 79, sig) != NULL)
fputs(buf, article);
fclose(sig);
}
fclose(article);
sprintf(buf, my_stuff.editor, fn);
system(buf);
article = fopen(fn, "rt");
sprintf(buf, "Post this article to %s (y/n)? ", newsgroups);
message(buf);
while (((ch = getch()) != 'y') && (ch != 'n'));
gotoxy(1,PAGE_SIZE);
if (ch == 'y')
post_it(article, newsgroups, subject, "world", msg_id);
}
fclose(article);
/* unlink(fn); /* keep it around like rn */
} else {
message("*** couldn't open temp article file - press any key ***");
getch();
}
}
/*--------------------------- post an article ---------------------------*/
void post_it(FILE *article, char *newsgroups, char *subject, char *dist,
char *msg_id)
{
/*
* Post an article. The article is passed as an open file,
* with the other header data. The header is added to the file
* and the article sent.
*/
FILE *tmp, *local, *log;
char buf[256], *p;
char ng[256], timestr[64];
char d_name[20], x_name[20];
char short_d_name[20], short_x_name[20];
char remotegroups[256];
int ct, seq;
time_t t;
struct tm *gmt, *tmnow;
long where;
ACTIVE *gp;
static char *dow[] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static char *mth[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec"
};
ct = 0;
seq = post_sequence();
sprintf(d_name, "D.%s%04x", my_stuff.my_site, seq);
sprintf(x_name, "X.%s%04x", my_stuff.my_site, seq);
sprintf(short_d_name, "D.%.3s", my_stuff.my_site);
sprintf(short_x_name, "X.%.3s", my_stuff.my_site);
/* count the lines */
rewind(article);
while (fgets(buf, 255, article) != NULL)
ct++;
/*
* Make the final article file. This posting stuff is a horrible
* kludge (blush). If you get a around to fixing this, please
* post me a copy :)
*/
if ((tmp = fopen(short_d_name, "w+b")) != NULL) {
remotegroups[0] = 0;
strcpy(ng, newsgroups);
p = strtok(ng, " ,");
while (p != NULL) {
if (!is_local_group(p)) {
if (remotegroups[0])
strcat(remotegroups, ",");
strcat(remotegroups, p);
}
p = strtok(NULL, " ,");
}
fprintf(tmp, "Path: %s!%s\n", my_stuff.my_site, my_stuff.user);
fprintf(tmp, "From: %s@%s (%s)\n", my_stuff.user, my_stuff.my_domain,
my_stuff.my_name);
fprintf(tmp, "Newsgroups: %s\n", newsgroups);
fprintf(tmp, "Subject: %s\n", subject);
fprintf(tmp, "Distribution: %s\n", remotegroups[0] ? dist : "local");
fprintf(tmp, "Message-ID: <%ldsnx@%s>\n", time(&t), my_stuff.my_domain);
if (strlen(msg_id) > 0)
fprintf(tmp, "References: %s\n", msg_id);
time(&t);
gmt = gmtime(&t);
fprintf(tmp, "Date: %s, %02d %s %02d %02d:%02d:%02d GMT\n",
dow[gmt->tm_wday],
gmt->tm_mday, mth[gmt->tm_mon], (gmt->tm_year % 100),
gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
fprintf(tmp, "Organization: %s\n", my_stuff.my_organisation);
fprintf(tmp, "Lines: %d\n", ct);
strcpy(buf, my_stuff.news_dir);
strcat(buf, "headers");
if ((local = fopen(buf, "r")) != NULL) {
while (fgets(buf, 255, local) != NULL)
fputs(buf, tmp);
fclose(local);
}
fputs("\n", tmp);
/* copy the rest */
rewind(article);
while (fgets(buf, 255, article) != NULL) {
fputs(buf, tmp);
}
/* ok, now post it locally */
strcpy(ng, newsgroups);
p = strtok(ng, " ,");
while (p != NULL) {
if (fseek(tmp, 0L, SEEK_SET) != 0) {
fprintf(stderr, "cannot rewind temp file\n");
exit(1);
}
local = open_out_file(p);
where = ftell(local);
while (fgets(buf, 255, tmp) != NULL) {
fputs(buf, local);
}
fprintf(local, "\n@@@@END\n");
fclose(local);
/* save the data in the index file */
local = open_index_file(p);
gp = find_news_group(p);
fprintf(local,"%08ld %08ld %09ld %s\n", where, gp->hi_num, t,
subject);
fclose(local);
p = strtok(NULL, " ,");
}
/* finally log it */
strcpy(buf, my_stuff.news_dir);
strcat(buf, "post.log");
if ((log = flockopen(buf, "at")) != NULL) {
fprintf(log, "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
"\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\n");
tmnow = localtime(&t);
strftime(timestr, sizeof(timestr), "%a %b %d %H:%M:%S %z %Y", gmt);
fprintf(log, "From %s!%s %s\n", my_stuff.my_site, my_stuff.user,
timestr);
fseek(tmp, 0L, SEEK_SET);
while (fgets(buf, 255, tmp) != NULL) {
fputs(buf, log);
}
fclose(log);
}
fclose(tmp);
/* now the the X... file */
if (remotegroups[0])
if ((tmp = fopen(short_x_name, "wb")) != NULL) {
fprintf(tmp, "U news %s\n", my_stuff.my_site);
fprintf(tmp, "Z\n");
fprintf(tmp, "F %s\n", d_name);
fprintf(tmp, "I %s\n", d_name);
fprintf(tmp, "C rnews\n");
fclose(tmp);
sprintf(buf, "uucp -C %s %s!%s 2>nul", short_d_name,
my_stuff.mail_server, d_name);
gotoxy(1,PAGE_SIZE); clreol();
system(buf);
sprintf(buf, "uucp -C %s %s!%s 2>nul", short_x_name,
my_stuff.mail_server, x_name);
gotoxy(1,PAGE_SIZE); clreol();
system(buf);
} else {
message("*** couldn't open temp post file - press any key ***");
getch();
}
unlink(short_d_name);
unlink(short_x_name);
} else {
message("*** couldn't open temp post file - press any key ***");
getch();
}
}
/*------------------------ parse and check newsgroups ---------------------*/
int newsgroups_valid(char *ng)
{
/*
* This routine parses the newsgroup list, and checks them for
* validity
*/
char buf[256], msg[128], *p;
strcpy(buf, ng);
if ((p = strtok(buf, ", ")) == NULL)
return(FALSE);
while (p != NULL) {
if (!check_valid_post_group(p)) {
sprintf(msg, "*** can't post to %s - press any key ***", p);
message(msg);
getch();
return(FALSE);
}
p = strtok(NULL, ", ");
}
return(TRUE);
}