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

375 lines
8.5 KiB
C
Raw Normal View History

2001-08-17 05:46:24 +00:00
/*****************************************************************************
*
* $Id$
2001-08-17 05:46:24 +00:00
* Purpose ...............: .TIC files magic processing.
*
*****************************************************************************
2005-08-28 14:52:14 +00:00
* 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"
2005-11-12 12:52:30 +00:00
#include "orphans.h"
2001-08-17 05:46:24 +00:00
#include "tic.h"
#include "utic.h"
#include "magic.h"
2005-10-11 20:49:41 +00:00
int MagicNr; /* Current magic record number */
2001-08-17 05:46:24 +00:00
int Magics = 0; /* Processed magics */
char *Magic_Macro(int);
char *Magic_Macro(int C)
{
2002-11-26 20:35:50 +00:00
static char buf[PATH_MAX];
buf[0] = '\0';
switch(toupper(C)) {
2005-08-28 14:52:14 +00:00
case 'F': snprintf(buf, PATH_MAX, "%s/%s", TIC.BBSpath, TIC.NewFile);
2002-11-26 20:35:50 +00:00
break;
2005-08-28 14:52:14 +00:00
case 'P': snprintf(buf, PATH_MAX, "%s", TIC.BBSpath);
2002-11-26 20:35:50 +00:00
break;
2005-08-28 14:52:14 +00:00
case 'N': snprintf(buf, PATH_MAX, "%s", strtok(strdup(TIC.NewFile), "."));
2002-11-26 20:35:50 +00:00
break;
2005-08-28 14:52:14 +00:00
case 'E': snprintf(buf, PATH_MAX, "%s", strrchr(TIC.NewFile, '.'));
2002-11-26 20:35:50 +00:00
break;
2005-08-28 14:52:14 +00:00
case 'L': snprintf(buf, PATH_MAX, "%s", strrchr(TIC.NewFile, '.'));
2002-11-26 20:35:50 +00:00
buf[0] = buf[1];
buf[1] = buf[2];
buf[2] = '\0';
break;
2005-08-28 14:52:14 +00:00
case 'D': snprintf(buf, 3, "%03d", Day_Of_Year());
2002-11-26 20:35:50 +00:00
break;
2005-08-28 14:52:14 +00:00
case 'C': snprintf(buf, 3, "%03d", Day_Of_Year());
2002-11-26 20:35:50 +00:00
buf[0] = buf[1];
buf[1] = buf[2];
buf[2] = '\0';
break;
2005-08-28 14:52:14 +00:00
case 'A': snprintf(buf, PATH_MAX, "%s", TIC.TicIn.Area);
2002-11-26 20:35:50 +00:00
break;
}
2001-08-17 05:46:24 +00:00
2002-11-26 20:35:50 +00:00
Syslog('f', "Mgc Macro(%c): \"%s\"", C, buf);
return buf;
2001-08-17 05:46:24 +00:00
}
int GetMagicRec(int Typ, int First)
{
int Eof = FALSE;
2004-03-08 20:51:04 +00:00
char *temp, mask[256];
2002-04-18 20:32:57 +00:00
FILE *FeM;
if (First)
MagicNr = 0;
temp = calloc(PATH_MAX, sizeof(char));
2005-08-28 14:52:14 +00:00
snprintf(temp, PATH_MAX, "%s/etc/magic.data", getenv("MBSE_ROOT"));
2002-04-18 20:32:57 +00:00
if ((FeM = fopen(temp, "r")) == NULL) {
Syslog('+', "Huh? No magic file? (%s)", temp);
free(temp);
return FALSE;
}
2001-08-17 05:46:24 +00:00
2002-04-18 20:32:57 +00:00
fread(&magichdr, sizeof(magichdr), 1, FeM);
2001-08-17 05:46:24 +00:00
2002-04-18 20:32:57 +00:00
do {
if (fseek(FeM, magichdr.hdrsize + (MagicNr * magichdr.recsize), SEEK_SET) != 0) {
WriteError("$Can't seek record %ld in %s", MagicNr, temp);
free(temp);
fclose(FeM);
return FALSE;
}
2001-08-17 05:46:24 +00:00
2002-04-18 20:32:57 +00:00
MagicNr++;
if (fread(&magic, magichdr.recsize, 1, FeM) == 1) {
if ((magic.Active) && (magic.Attrib == Typ) && (strcasecmp(magic.From, TIC.TicIn.Area) == 0)) {
2002-05-10 18:33:23 +00:00
memset(&mask, 0, sizeof(mask));
2004-03-08 20:51:04 +00:00
strcpy(mask, re_mask(magic.Mask, FALSE));
if ((re_comp(mask)) == NULL) {
2002-11-26 20:35:50 +00:00
if (re_exec(TIC.NewFile)) {
fclose(FeM);
free(temp);
return TRUE;
2002-04-18 20:32:57 +00:00
}
2001-08-17 05:46:24 +00:00
} else {
WriteError("Magic: re_comp(%s) failed", mask);
2002-04-18 20:32:57 +00:00
}
}
2001-08-17 05:46:24 +00:00
2002-04-18 20:32:57 +00:00
} else {
Eof = TRUE;
}
} while (!Eof);
free(temp);
fclose(FeM);
return FALSE;
2001-08-17 05:46:24 +00:00
}
void MagicResult(char *format, ...)
{
2002-11-26 20:35:50 +00:00
char *outputstr;
va_list va_ptr;
2001-08-17 05:46:24 +00:00
2002-11-26 20:35:50 +00:00
outputstr = calloc(1024, sizeof(char));
2001-08-17 05:46:24 +00:00
2002-11-26 20:35:50 +00:00
va_start(va_ptr, format);
2005-08-28 14:52:14 +00:00
vsnprintf(outputstr, 1024, format, va_ptr);
2002-11-26 20:35:50 +00:00
va_end(va_ptr);
2001-08-17 05:46:24 +00:00
2002-11-26 20:35:50 +00:00
Syslog('+', "Magic: %s", outputstr);
free(outputstr);
Magics++;
2001-08-17 05:46:24 +00:00
}
2002-01-15 21:32:07 +00:00
2001-08-17 05:46:24 +00:00
void Magic_CheckCompile(void);
void Magic_CheckCompile(void)
{
2002-11-26 20:35:50 +00:00
if (magic.Compile) {
CompileNL = TRUE;
Syslog('+', "Magic: Trigger Compile Nodelists");
}
2001-08-17 05:46:24 +00:00
}
int Magic_MoveFile(void)
{
2002-11-26 20:35:50 +00:00
if (GetMagicRec(MG_MOVE, TRUE)) {
strcpy(TIC.TicIn.Area, magic.ToArea);
MagicResult((char *)"Move %s to Area %s", TIC.NewFile, TIC.TicIn.Area);
return TRUE;
} else
return FALSE;
2001-08-17 05:46:24 +00:00
}
void Magic_ExecCommand(void)
{
2002-11-26 20:35:50 +00:00
int i, j, k, Err, First = TRUE;
char Line[256], Temp[PATH_MAX], *cmd, *opts;
while (GetMagicRec(MG_EXEC, First)) {
First = FALSE;
j = 0;
memset(&Line, 0, sizeof(Line));
for (i = 0; i < strlen(magic.Cmd); i++) {
if (magic.Cmd[i] != '%') {
Line[j] = magic.Cmd[i];
j++;
} else {
i++;
if (magic.Cmd[i] == '%') {
Line[j] = '%';
j++;
} else {
Temp[0] = '\0';
2005-08-28 14:52:14 +00:00
snprintf(Temp, PATH_MAX, "%s", Magic_Macro(magic.Cmd[i]));
2002-11-26 20:35:50 +00:00
for (k = 0; k < strlen(Temp); k++) {
Line[j] = Temp[k];
j++;
}
2001-08-17 05:46:24 +00:00
}
2002-11-26 20:35:50 +00:00
}
}
2001-08-17 05:46:24 +00:00
2002-11-26 20:35:50 +00:00
cmd = xstrcpy(getenv("MBSE_ROOT"));
cmd = xstrcat(cmd, (char *)"/bin/");
cmd = xstrcat(cmd, strtok(Line, " "));
opts = strtok(NULL, "\0");
MagicResult((char *)"Exec: \"%s %s\"", cmd, opts);
2001-08-17 05:46:24 +00:00
2004-03-02 20:47:23 +00:00
if ((Err = execute_str(cmd, opts, NULL, (char *)"/dev/null", (char *)"/dev/null", (char *)"/dev/null")) == 0) {
2002-11-26 20:35:50 +00:00
Magics++;
} else
Syslog('!', "Mgc Exec: (%s %s) returns %d", cmd, opts, Err);
2001-08-17 05:46:24 +00:00
2002-11-26 20:35:50 +00:00
Magic_CheckCompile();
}
2001-08-17 05:46:24 +00:00
}
void Magic_CopyFile(void)
{
2002-11-26 20:35:50 +00:00
int First = TRUE, rc;
char *From, *To;
From = calloc(PATH_MAX, sizeof(char));
To = calloc(PATH_MAX, sizeof(char));
2001-08-17 05:46:24 +00:00
2002-11-26 20:35:50 +00:00
while (GetMagicRec(MG_COPY, First)) {
First = FALSE;
2005-08-28 14:52:14 +00:00
snprintf(From, PATH_MAX, "%s/%s", TIC.BBSpath, TIC.NewFile);
snprintf(To, PATH_MAX, "%s/%s", magic.Path, TIC.NewFile);
2002-11-26 20:35:50 +00:00
if ((rc = file_cp(From, To) == 0)) {
MagicResult((char *)"%s copied to %s", From, To);
Magic_CheckCompile();
} else
WriteError("Magic: copy: %s to %s failed, %s", strerror(rc));
}
free(From);
free(To);
2001-08-17 05:46:24 +00:00
}
void Magic_UnpackFile(void)
{
2002-05-18 12:57:00 +00:00
int rc, First = TRUE;
char *Fn, *buf = NULL, *unarc = NULL, *cmd = NULL;
2002-01-15 21:32:07 +00:00
2002-05-18 12:57:00 +00:00
Fn = calloc(PATH_MAX, sizeof(char));
2002-01-15 21:32:07 +00:00
while (GetMagicRec(MG_UNPACK, First)) {
First = FALSE;
buf = calloc(PATH_MAX, sizeof(char));
getcwd(buf, 128);
if (chdir(magic.Path) == 0) {
2005-08-28 14:52:14 +00:00
snprintf(Fn, PATH_MAX, "%s/%s", TIC.BBSpath, TIC.NewFile);
2002-01-15 21:32:07 +00:00
if ((unarc = unpacker(Fn)) != NULL) {
if (getarchiver(unarc)) {
2003-01-12 14:51:21 +00:00
cmd = xstrcpy(archiver.munarc);
2002-01-15 21:32:07 +00:00
if (strlen(cmd)) {
2004-03-02 20:47:23 +00:00
rc = execute_str(cmd, Fn, (char *)NULL, (char *)"/dev/null", (char *)"/dev/null", (char *)"/dev/null");
2002-01-15 21:32:07 +00:00
if (rc)
WriteError("$Magic: unpack in %s, error %d", magic.Path, rc);
else
MagicResult((char *)"unpacked in %s", magic.Path);
}
free(cmd);
}
}
2001-08-17 05:46:24 +00:00
2002-01-15 21:32:07 +00:00
chdir(buf);
Magic_CheckCompile();
} else
WriteError("$Magic: unpack: can't chdir \"%s\"", magic.Path);
2001-08-17 05:46:24 +00:00
2002-01-15 21:32:07 +00:00
free(buf);
}
2002-05-18 12:57:00 +00:00
free(Fn);
2001-08-17 05:46:24 +00:00
}
void Magic_Keepnum(void)
{
2002-11-26 20:35:50 +00:00
if (GetMagicRec(MG_KEEPNUM, TRUE)) {
TIC.KeepNum = magic.KeepNum;
MagicResult((char *)"Keep %d files", TIC.KeepNum);
}
2001-08-17 05:46:24 +00:00
}
void Magic_UpDateAlias(void)
{
2002-11-26 20:35:50 +00:00
if (GetMagicRec(MG_UPDALIAS, TRUE)) {
2004-03-09 21:42:29 +00:00
magic_update(TIC.TicIn.Area, TIC.NewFile);
2002-11-26 20:35:50 +00:00
MagicResult((char *)"Update Alias");
}
2001-08-17 05:46:24 +00:00
}
void Magic_AdoptFile(void)
{
2002-11-26 20:35:50 +00:00
int First = TRUE;
char *temp;
FILE *Tf;
2001-08-17 05:46:24 +00:00
2002-11-26 20:35:50 +00:00
temp = calloc(PATH_MAX, sizeof(char));
while (GetMagicRec(MG_ADOPT, First)) {
First = FALSE;
if (SearchTic(magic.ToArea)) {
MagicResult((char *)"Adoptfile in %s", magic.ToArea);
2005-08-28 14:52:14 +00:00
snprintf(temp, PATH_MAX, "%s/%s", TIC.Inbound, MakeTicName());
2002-11-26 20:35:50 +00:00
if ((Tf = fopen(temp, "a+")) == NULL)
WriteError("$Can't create %s", temp);
else {
fprintf(Tf, "Hatch\r\n");
fprintf(Tf, "NoMove\r\n");
fprintf(Tf, "Created MBSE BBS v%s, %s\r\n", VERSION, SHORTRIGHT);
fprintf(Tf, "Area %s\r\n", magic.ToArea);
fprintf(Tf, "Origin %s\r\n", aka2str(tic.Aka));
fprintf(Tf, "From %s\r\n", aka2str(tic.Aka));
if (strlen(TIC.TicIn.Replace))
fprintf(Tf, "Replaces %s\r\n", TIC.TicIn.Replace);
if (strlen(TIC.TicIn.Magic))
fprintf(Tf, "Magic %s\r\n", TIC.TicIn.Magic);
fprintf(Tf, "File %s\r\n", TIC.NewFile);
if (strlen(TIC.NewFullName))
fprintf(Tf, "Fullname %s\r\n", TIC.NewFullName);
fprintf(Tf, "Pth %s\r\n", TIC.BBSpath);
fprintf(Tf, "Desc %s\r\n", TIC.TicIn.Desc);
fprintf(Tf, "Crc %s\r\n", TIC.TicIn.Crc);
fprintf(Tf, "Pw %s\r\n", CFG.hatchpasswd);
fclose(Tf);
}
SearchTic(TIC.TicIn.Area);
} else
WriteError("Mgc Adopt: Area \"%s\" not found");
}
free(temp);
2001-08-17 05:46:24 +00:00
}
int Magic_DeleteFile(void)
{
2002-11-26 20:35:50 +00:00
int Result;
2001-08-17 05:46:24 +00:00
2002-11-26 20:35:50 +00:00
if ((Result = GetMagicRec(MG_DELETE, TRUE)))
MagicResult((char *)"Delete file");
2001-08-17 05:46:24 +00:00
2002-11-26 20:35:50 +00:00
return Result;
2001-08-17 05:46:24 +00:00
}