This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
deb-mbse/mbfido/newspost.c

234 lines
5.8 KiB
C
Raw Normal View History

2001-08-17 05:46:24 +00:00
/*****************************************************************************
*
2002-01-07 19:16:03 +00:00
* $Id$
2001-08-17 05:46:24 +00:00
* Purpose ...............: Post newsarticles in temp newsfile.
*
*****************************************************************************
2005-08-13 21:54:29 +00:00
* Copyright (C) 1997-2005
2001-08-17 05:46:24 +00:00
*
2002-01-07 19:16:03 +00:00
* Michiel Broek FIDO: 2:280/2802
2001-08-17 05:46:24 +00:00
* Beekmansbos 10 Internet: mbroek@users.sourceforge.net
* 1971 BV IJmuiden
* the Netherlands
*
* This file is part of MBSE BBS.
*
* This BBS is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* MBSE BBS 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.
*
* You should have received a copy of the GNU General Public License
* along with MBSE BBS; see the file COPYING. If not, write to the Free
2003-08-15 20:05:34 +00:00
* Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
2001-08-17 05:46:24 +00:00
*****************************************************************************/
2002-06-30 12:48:44 +00:00
#include "../config.h"
2004-02-21 17:22:00 +00:00
#include "../lib/mbselib.h"
2001-08-17 05:46:24 +00:00
#include "../lib/mbinet.h"
#include "newspost.h"
extern FILE *nfp;
extern int newsopen;
extern int news_out;
extern int news_bad;
int newspost(void)
{
int start = TRUE, fatal = FALSE;
2005-08-13 21:54:29 +00:00
char *buf, *p;
2005-10-11 20:49:41 +00:00
int curpos, count, seqnr;
2005-08-13 21:54:29 +00:00
FILE *ofp = NULL, *nb;
struct utsname utsbuf;
2001-08-17 05:46:24 +00:00
2005-08-13 21:54:29 +00:00
if (newsopen)
fclose(nfp);
buf = calloc(10240, sizeof(char));
/*
* Now reopen the file for reading. If it fails and
* the file was original closed we leave quiet.
* If the file wasn't open previously but there is
* a file, try to post the articles. They may be
* still here if the newsserver wasn't available.
*/
2005-08-28 14:10:06 +00:00
snprintf(buf, 10240, "%s/tmp/newsout", getenv("MBSE_ROOT"));
2005-08-13 21:54:29 +00:00
if ((nfp = fopen(buf, "r")) == NULL) {
2001-08-17 05:46:24 +00:00
if (newsopen)
2005-08-13 21:54:29 +00:00
WriteError("$Can't reopen %s", buf);
free(buf);
return newsopen;
}
IsDoing("Post news");
if (CFG.newsfeed == FEEDINN) {
Syslog('+', "Posting news articles to the NNTP server");
if (nntp_connect() == -1) {
free(buf);
return TRUE;
2001-08-17 05:46:24 +00:00
}
2005-08-13 21:54:29 +00:00
while (fgets(buf, 10240, nfp)) {
if (start) {
if (nntp_cmd((char *)"POST\r\n", 340) != 0) {
WriteError("NNTP: POST refused");
2005-08-13 21:54:29 +00:00
free(buf);
return TRUE;
2001-08-17 05:46:24 +00:00
}
2005-08-13 21:54:29 +00:00
}
start = FALSE;
if (!strcmp(buf, ".\n")) {
if (nntp_cmd((char *)".\r\n", 240) == 0) {
news_out++;
Syslog('+', "NTTP: article %d accepted", news_out);
2005-08-13 21:54:29 +00:00
} else {
WriteError("NNTP: refused article %d", news_out+1);
news_bad++;
2001-08-17 05:46:24 +00:00
}
2005-08-13 21:54:29 +00:00
start = TRUE;
} else {
/*
* Most NNTP servers like cr/lf after each line.
*/
Striplf(buf);
p = buf+strlen(buf);
*p++ = '\r';
*p++ = '\n';
*p = '\0';
nntp_send(buf);
}
Nopper();
}
nntp_close();
}
/*
* Create newsbatch file.
*/
if ((CFG.newsfeed == FEEDUUCP) || (CFG.newsfeed == FEEDRNEWS)) {
2005-08-13 22:00:47 +00:00
Syslog('+', "Posting news articles to the news batchfile");
2005-08-28 14:10:06 +00:00
snprintf(buf, 10240, "%s/tmp/newsbatch", getenv("MBSE_ROOT"));
2005-08-13 21:54:29 +00:00
if ((ofp = fopen(buf, "w+")) == NULL) {
WriteError("$Can't create %s", buf);
free(buf);
fclose(nfp);
return TRUE;
2001-08-17 05:46:24 +00:00
}
2005-08-13 21:54:29 +00:00
count = curpos = 0;
while (feof(ofp) == 0) {
/*
* Count the total length of the message
*/
while (fgets(buf, 10240, nfp)) {
if (strcmp(buf, ".\n")) {
count += strlen(buf);
} else {
break;
2001-08-17 05:46:24 +00:00
}
2005-08-13 21:54:29 +00:00
}
if (!count)
break;
fseek(nfp, curpos, SEEK_SET);
2005-10-11 20:49:41 +00:00
fprintf(ofp, "#! rnews %d\n", count);
2005-08-13 21:54:29 +00:00
while (fgets(buf, 10240, nfp)) {
if (strcmp(buf, ".\n")) {
fprintf(ofp, buf);
} else {
break;
2001-08-17 05:46:24 +00:00
}
2005-08-13 21:54:29 +00:00
}
news_out++;
curpos = ftell(nfp);
count = 0;
2001-08-17 05:46:24 +00:00
}
2005-08-13 21:54:29 +00:00
/*
* Rewind the newsbatch and leave it open.
*/
rewind(ofp);
}
2001-08-17 05:46:24 +00:00
2005-08-13 21:54:29 +00:00
fclose(nfp);
newsopen = FALSE;
2001-08-17 05:46:24 +00:00
2005-08-13 22:00:47 +00:00
/*
* Mode rnews, pipe just created newsbatch to rnews.
*/
if (CFG.newsfeed == FEEDRNEWS) {
if ((nb = (expipe(CFG.rnewspath, NULL, NULL))) == NULL) {
WriteError("Could not open (pipe) output for %s", CFG.rnewspath);
newsopen = FALSE;
return TRUE;
2001-08-17 05:46:24 +00:00
}
2005-08-13 22:00:47 +00:00
while (fgets(buf, 10240, ofp)) {
fputs(buf, nb);
}
if (exclose(nb)) {
WriteError("Error closing pipe");
newsopen = FALSE;
return TRUE;
} else
Syslog('+', "Articles send through %s", CFG.rnewspath);
fclose(ofp);
2005-08-28 14:10:06 +00:00
snprintf(buf, 10240, "%s/tmp/newsbatch", getenv("MBSE_ROOT"));
2005-08-13 22:00:47 +00:00
unlink(buf);
}
2001-08-17 05:46:24 +00:00
2005-08-13 22:00:47 +00:00
/*
* Mode UUCP, create UUCP files.
*/
if (CFG.newsfeed == FEEDUUCP) {
seqnr = sequencer();
memset(&utsbuf, 0, sizeof(utsbuf));
if (uname(&utsbuf)) {
WriteError("Can't get system nodename");
newsopen = FALSE;
return TRUE;
}
2001-08-17 05:46:24 +00:00
2005-10-11 20:49:41 +00:00
snprintf(buf, 10240, "%s/C.%s%x", CFG.rnewspath, CFG.nntpnode, seqnr);
2005-08-13 22:00:47 +00:00
if ((nb = fopen(buf, "a")) == NULL) {
WriteError("Can't create %s", buf);
newsopen = FALSE;
return TRUE;
}
seqnr = sequencer();
2005-10-11 20:49:41 +00:00
fprintf(nb, "E D.%s%x D.%s%x news -C D.%s%x 0666 \"\" 0 rnews\n",
2001-08-17 05:46:24 +00:00
utsbuf.nodename, seqnr, utsbuf.nodename, seqnr, utsbuf.nodename, seqnr);
2005-08-13 22:00:47 +00:00
fclose(nb);
2005-10-11 20:49:41 +00:00
snprintf(buf, 10240, "%s/D.%s%x", CFG.rnewspath, utsbuf.nodename, seqnr);
2005-08-13 22:00:47 +00:00
if ((nb = fopen(buf, "a")) == NULL) {
WriteError("Can't create %s", buf);
newsopen = FALSE;
return TRUE;
2001-08-17 05:46:24 +00:00
}
2005-08-13 22:00:47 +00:00
while (fgets(buf, 10240, ofp)) {
fputs(buf, nb);
}
Syslog('+', "Articles placed in %s", CFG.rnewspath);
fclose(ofp);
2005-08-28 14:10:06 +00:00
snprintf(buf, 10240, "%s/tmp/newsbatch", getenv("MBSE_ROOT"));
2005-08-13 22:00:47 +00:00
unlink(buf);
}
2001-08-17 05:46:24 +00:00
snprintf(buf, 10240, "%s/tmp/newsout", getenv("MBSE_ROOT"));
if (! fatal) {
2005-08-13 22:00:47 +00:00
unlink(buf);
} else {
Syslog('+', "Fatal errors posting news, %s not removed", buf);
2005-08-13 22:00:47 +00:00
}
2005-08-13 21:54:29 +00:00
2005-08-13 22:00:47 +00:00
free(buf);
return FALSE;
2001-08-17 05:46:24 +00:00
}