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

290 lines
7.3 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-2006
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
2005-10-11 20:49:41 +00:00
int Post(char *To, int 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;
2005-10-11 20:49:41 +00:00
unsigned int crc = -1;
2004-06-06 14:55:00 +00:00
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);
2005-10-11 20:49:41 +00:00
printf("Post \"%s\" to \"%s\" in area %d\n", File, To, Area);
2004-06-06 14:55:00 +00:00
}
IsDoing("Posting");
2005-10-11 20:49:41 +00:00
Syslog('+', "Post \"%s\" area %d to \"%s\" flavor %s", File, Area, To, Flavor);
2004-06-06 14:55:00 +00:00
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));
2005-08-28 14:10:06 +00:00
snprintf(sAreas, PATH_MAX, "%s/etc/mareas.data", getenv("MBSE_ROOT"));
2004-06-06 14:55:00 +00:00
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
*/
2005-08-28 14:10:06 +00:00
snprintf(Msg.From, 101, CFG.sysop_name);
snprintf(Msg.To, 101, To);
2004-06-06 14:55:00 +00:00
/*
* 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;
}
}
}
2005-08-28 14:10:06 +00:00
snprintf(Msg.Subject, 101, "%s", Subj);
snprintf(Msg.FromAddress, 101, "%s", aka2str(msgs.Aka));
Msg.Written = Msg.Arrived = time(NULL) - (gmt_offset((time_t)0) * 60);
2004-06-06 14:55:00 +00:00
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;
2005-08-28 14:10:06 +00:00
snprintf(Msg.ToAddress, 101, "%s", ascfnode(parsefaddr(To), 0xff));
2004-06-06 14:55:00 +00:00
break;
case ECHOMAIL:
Msg.Echomail = TRUE;
break;
case NEWS:
Msg.News = TRUE;
break;
}
temp = calloc(PATH_MAX, sizeof(char));
2005-10-11 20:49:41 +00:00
snprintf(temp, PATH_MAX, "\001MSGID: %s %08x", aka2str(msgs.Aka), sequencer());
2004-06-06 14:55:00 +00:00
MsgText_Add2(temp);
Msg.MsgIdCRC = upd_crc32(temp, crc, strlen(temp));
Msg.ReplyCRC = 0xffffffff;
2005-08-28 14:10:06 +00:00
snprintf(temp, PATH_MAX, "\001PID: MBSE-FIDO %s (%s-%s)", VERSION, OsName(), OsCPU());
2004-06-06 14:55:00 +00:00
MsgText_Add2(temp);
if (msgs.Charset != FTNC_NONE) {
2005-08-28 14:10:06 +00:00
snprintf(temp, PATH_MAX, "\001CHRS: %s", getftnchrs(msgs.Charset));
2004-06-06 14:55:00 +00:00
} else {
2005-08-28 14:10:06 +00:00
snprintf(temp, PATH_MAX, "\001CHRS: %s", getftnchrs(FTNC_LATIN_1));
2004-06-06 14:55:00 +00:00
}
MsgText_Add2(temp);
2005-08-28 14:10:06 +00:00
snprintf(temp, PATH_MAX, "\001TZUTC: %s", gmtoffset(tt));
2004-06-06 14:55:00 +00:00
MsgText_Add2(temp);
2005-08-28 14:10:06 +00:00
while ((Fgets(temp, PATH_MAX, 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)
2005-08-28 14:10:06 +00:00
snprintf(aka, 40, "(%d:%d/%d.%d)", msgs.Aka.zone, msgs.Aka.net, msgs.Aka.node, msgs.Aka.point);
else
2005-08-28 14:10:06 +00:00
snprintf(aka, 40, "(%d:%d/%d)", msgs.Aka.zone, msgs.Aka.net, msgs.Aka.node);
2004-06-06 14:55:00 +00:00
if (strlen(msgs.Origin))
2005-08-28 14:10:06 +00:00
snprintf(temp, 81, " * Origin: %s %s", msgs.Origin, aka);
else
2005-08-28 14:10:06 +00:00
snprintf(temp, 81, " * 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) {
2005-08-28 14:10:06 +00:00
snprintf(temp, PATH_MAX, "%s/tmp/%smail.jam", getenv("MBSE_ROOT"), (msgs.Type == ECHOMAIL) ? "echo" : "net");
2001-08-17 05:46:24 +00:00
if ((fp = fopen(temp, "a")) != NULL) {
2005-10-11 20:49:41 +00:00
fprintf(fp, "%s %u\n", msgs.Base, Msg.Id);
2004-06-06 14:55:00 +00:00
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
}