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/post.c

291 lines
7.2 KiB
C
Raw Normal View History

2001-08-17 05:46:24 +00:00
/*****************************************************************************
*
2001-12-23 16:44:18 +00:00
* $Id$
2001-08-17 05:46:24 +00:00
* Purpose ...............: Post a message from a file.
*
*****************************************************************************
* Copyright (C) 1997-2005
2001-08-17 05:46:24 +00:00
*
* Michiel Broek FIDO: 2:280/2802
* Beekmansbos 10
* 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"
2002-01-07 19:16:03 +00:00
#include "../lib/users.h"
2004-02-21 17:22:00 +00:00
#include "../lib/mbsedb.h"
2001-08-17 05:46:24 +00:00
#include "../lib/msg.h"
#include "../lib/msgtext.h"
#include "post.h"
extern int do_quiet; /* Suppress screen output */
2001-08-17 05:46:24 +00:00
int Post(char *To, long Area, char *Subj, char *File, char *Flavor)
2001-08-17 05:46:24 +00:00
{
int i, rc = FALSE, has_tear = FALSE, has_origin = FALSE;
2004-06-06 14:55:00 +00:00
char *aka, *temp, *sAreas;
FILE *fp, *tp;
unsigned long crc = -1;
time_t tt;
struct tm *t;
2005-08-10 15:32:42 +00:00
mbse_CleanSubject(Subj);
2004-06-06 14:55:00 +00:00
if (!do_quiet) {
2005-08-19 20:48:54 +00:00
mbse_colour(CYAN, BLACK);
2004-06-06 14:55:00 +00:00
printf("Post \"%s\" to \"%s\" in area %ld\n", File, To, Area);
}
IsDoing("Posting");
Syslog('+', "Post \"%s\" area %ld to \"%s\" flavor %s", File, Area, To, Flavor);
Syslog('+', "Subject: \"%s\"", Subj);
if ((tp = fopen(File, "r")) == NULL) {
WriteError("$Can't open %s", File);
if (!do_quiet)
printf("Can't open \"%s\"\n", File);
return -1;
2004-06-06 14:55:00 +00:00
}
2001-08-17 05:46:24 +00:00
2004-06-06 14:55:00 +00:00
sAreas = calloc(PATH_MAX, sizeof(char));
sprintf(sAreas, "%s//etc/mareas.data", getenv("MBSE_ROOT"));
if ((fp = fopen(sAreas, "r")) == NULL) {
WriteError("$Can't open %s", sAreas);
2001-08-17 05:46:24 +00:00
free(sAreas);
2004-06-06 14:55:00 +00:00
fclose(tp);
return -1;
2004-06-06 14:55:00 +00:00
}
2001-08-17 05:46:24 +00:00
2004-06-06 14:55:00 +00:00
fread(&msgshdr, sizeof(msgshdr), 1, fp);
if (fseek(fp, (msgshdr.recsize + msgshdr.syssize) * (Area - 1), SEEK_CUR) == 0) {
if (fread(&msgs, msgshdr.recsize, 1, fp) == 1) {
rc = TRUE;
} else {
WriteError("$Can't read area %ld", Area);
2001-08-17 05:46:24 +00:00
}
2004-06-06 14:55:00 +00:00
} else {
WriteError("$Can't seek area %ld", Area);
}
2001-08-17 05:46:24 +00:00
2004-06-06 14:55:00 +00:00
free(sAreas);
if (rc == FALSE) {
2001-08-17 05:46:24 +00:00
fclose(fp);
2004-06-06 14:55:00 +00:00
fclose(tp);
return -1;
2004-06-06 14:55:00 +00:00
}
2001-08-17 05:46:24 +00:00
2004-06-06 14:55:00 +00:00
if (!msgs.Active) {
WriteError("Area %s not active", msgs.Name);
fclose(fp);
fclose(tp);
return -1;
2004-06-06 14:55:00 +00:00
}
/*
* Check the proper syntax in the To parameter, in netmail areas
* it must have a destination address, in all other areas just a
* full name.
*/
if (msgs.Type == NETMAIL) {
if ((strchr(To, '@') == NULL) || (strstr(To, (char *)".n") == NULL) || (strstr(To, (char *)".z") == NULL)) {
WriteError("No address in \"%s\" and area is netmail", To);
if (!do_quiet)
printf("No address in \"%s\" and area is netmail\n", To);
fclose(fp);
fclose(tp);
return -1;
2001-08-17 05:46:24 +00:00
}
2004-06-06 14:55:00 +00:00
} else {
if ((strchr(To, '@')) || (strstr(To, (char *)".n")) || (strstr(To, (char *)".z"))) {
WriteError("Address present in \"%s\" and area is not netmail", To);
if (!do_quiet)
printf("Address present in \"%s\" and area is not netmail\n", To);
fclose(fp);
fclose(tp);
return -1;
2001-08-17 05:46:24 +00:00
}
2004-06-06 14:55:00 +00:00
}
2001-08-17 05:46:24 +00:00
2004-06-06 14:55:00 +00:00
if (!Msg_Open(msgs.Base)) {
WriteError("Can't open %s", msgs.Base);
fclose(fp);
2001-08-17 05:46:24 +00:00
fclose(tp);
return -1;
2004-06-06 14:55:00 +00:00
}
2001-08-17 05:46:24 +00:00
2004-06-06 14:55:00 +00:00
if (!Msg_Lock(30L)) {
WriteError("Can't lock %s", msgs.Base);
Msg_Close();
fclose(fp);
fclose(tp);
return -1;
2004-06-06 14:55:00 +00:00
}
tt = time(NULL);
t = localtime(&tt);
Diw = t->tm_wday;
Miy = t->tm_mon;
memset(&Msg, 0, sizeof(Msg));
Msg_New();
/*
* Update statistic counter for message area
*/
fseek(fp, - msgshdr.recsize, SEEK_CUR);
msgs.Posted.total++;
msgs.Posted.tweek++;
msgs.Posted.tdow[Diw]++;
msgs.Posted.month[Miy]++;
fwrite(&msgs, msgshdr.recsize, 1, fp);
fclose(fp);
/*
* Start writing the message
*/
sprintf(Msg.From, CFG.sysop_name);
sprintf(Msg.To, To);
/*
* If netmail, clean the To field.
*/
if ((msgs.Type == NETMAIL) && strchr(To, '@')) {
for (i = 0; i < strlen(Msg.To); i++) {
if (Msg.To[i] == '_')
Msg.To[i] = ' ';
if (Msg.To[i] == '@') {
Msg.To[i] = '\0';
break;
}
}
}
sprintf(Msg.Subject, "%s", Subj);
sprintf(Msg.FromAddress, "%s", aka2str(msgs.Aka));
Msg.Written = time(NULL);
Msg.Arrived = time(NULL);
Msg.Local = TRUE;
if (strchr(Flavor, 'c'))
Msg.Crash = TRUE;
if (strchr(Flavor, 'p'))
Msg.Private = TRUE;
if (strchr(Flavor, 'h'))
Msg.Hold = TRUE;
switch (msgs.Type) {
case LOCALMAIL:
Msg.Localmail = TRUE;
break;
case NETMAIL:
Msg.Netmail = TRUE;
sprintf(Msg.ToAddress, "%s", ascfnode(parsefaddr(To), 0xff));
break;
case ECHOMAIL:
Msg.Echomail = TRUE;
break;
case NEWS:
Msg.News = TRUE;
break;
}
temp = calloc(PATH_MAX, sizeof(char));
sprintf(temp, "\001MSGID: %s %08lx", aka2str(msgs.Aka), sequencer());
MsgText_Add2(temp);
Msg.MsgIdCRC = upd_crc32(temp, crc, strlen(temp));
Msg.ReplyCRC = 0xffffffff;
sprintf(temp, "\001PID: MBSE-FIDO %s (%s-%s)", VERSION, OsName(), OsCPU());
MsgText_Add2(temp);
if (msgs.Charset != FTNC_NONE) {
sprintf(temp, "\001CHRS: %s", getftnchrs(msgs.Charset));
2004-06-06 14:55:00 +00:00
} else {
sprintf(temp, "\001CHRS: %s", getftnchrs(FTNC_LATIN_1));
2004-06-06 14:55:00 +00:00
}
MsgText_Add2(temp);
sprintf(temp, "\001TZUTC: %s", gmtoffset(tt));
MsgText_Add2(temp);
while ((Fgets(temp, PATH_MAX -1, tp)) != NULL) {
if (strncmp(temp, "--- ", 4) == 0)
has_tear = TRUE;
if (strncmp(temp, " * Origin: ", 11) == 0)
has_origin = TRUE;
}
rewind(tp);
Syslog('m', "has tearline=%s, has origin=%s", has_tear?"True":"False", has_origin?"True":"False");
2004-06-06 14:55:00 +00:00
/*
* Add the file as text
*/
Msg_Write(tp);
fclose(tp);
/*
* Finish the message
*/
2004-09-12 13:28:44 +00:00
if ((! has_tear) && (! has_origin)) {
MsgText_Add2((char *)"");
MsgText_Add2(TearLine());
}
2004-06-06 14:55:00 +00:00
if (! has_origin) {
aka = calloc(40, sizeof(char));
2004-06-06 14:55:00 +00:00
if (msgs.Aka.point)
sprintf(aka, "(%d:%d/%d.%d)", msgs.Aka.zone, msgs.Aka.net, msgs.Aka.node, msgs.Aka.point);
else
sprintf(aka, "(%d:%d/%d)", msgs.Aka.zone, msgs.Aka.net, msgs.Aka.node);
2004-06-06 14:55:00 +00:00
if (strlen(msgs.Origin))
sprintf(temp, " * Origin: %s %s", msgs.Origin, aka);
else
sprintf(temp, " * Origin: %s %s", CFG.origin, aka);
MsgText_Add2(temp);
free(aka);
}
2004-06-06 14:55:00 +00:00
Msg_AddMsg();
Msg_UnLock();
Syslog('+', "Posted message %ld", Msg.Id);
if (msgs.Type != LOCALMAIL) {
2001-08-17 05:46:24 +00:00
sprintf(temp, "%s/tmp/%smail.jam", getenv("MBSE_ROOT"), (msgs.Type == ECHOMAIL) ? "echo" : "net");
if ((fp = fopen(temp, "a")) != NULL) {
2004-06-06 14:55:00 +00:00
fprintf(fp, "%s %lu\n", msgs.Base, Msg.Id);
fclose(fp);
2001-08-17 05:46:24 +00:00
}
CreateSema((char *)"mailout");
2004-06-06 14:55:00 +00:00
}
2001-08-17 05:46:24 +00:00
2004-06-06 14:55:00 +00:00
free(temp);
Msg_Close();
return 0;
2001-08-17 05:46:24 +00:00
}