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

587 lines
14 KiB
C
Raw Normal View History

2001-08-17 05:46:24 +00:00
/*****************************************************************************
*
2001-11-14 21:37:58 +00:00
* $Id$
2001-08-17 05:46:24 +00:00
* Purpose ...............: Setup Oneliners.
*
*****************************************************************************
2002-01-07 19:16:03 +00:00
* Copyright (C) 1997-2002
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.
*
* MB 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 MB 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"
2001-08-17 05:46:24 +00:00
#include "../lib/libs.h"
#include "../lib/structs.h"
2002-01-07 19:16:03 +00:00
#include "../lib/users.h"
2001-08-17 05:46:24 +00:00
#include "../lib/records.h"
#include "../lib/common.h"
#include "../lib/clcomm.h"
#include "screen.h"
#include "mutil.h"
#include "ledit.h"
#include "m_global.h"
#include "m_ol.h"
int OnelUpdated = 0;
/*
* Count nr of oneline records in the database.
* Creates the database if it doesn't exist.
*/
int CountOneline(void)
{
FILE *fil;
char ffile[PATH_MAX];
2001-08-17 05:46:24 +00:00
int count;
struct tm *l_date;
char buf[12];
time_t Time;
Time = time(NULL);
l_date = localtime(&Time);
sprintf(buf, "%02d-%02d-%04d", l_date->tm_mday, l_date->tm_mon+1, l_date->tm_year+1900);
2001-08-17 05:46:24 +00:00
sprintf(ffile, "%s/etc/oneline.data", getenv("MBSE_ROOT"));
if ((fil = fopen(ffile, "r")) == NULL) {
if ((fil = fopen(ffile, "a+")) != NULL) {
Syslog('+', "created new %s", ffile);
2001-08-17 05:46:24 +00:00
olhdr.hdrsize = sizeof(olhdr);
olhdr.recsize = sizeof(ol);
fwrite(&olhdr, sizeof(olhdr), 1, fil);
memset(&ol, 0, sizeof(ol));
sprintf(ol.UserName, "Sysop");
sprintf(ol.DateOfEntry, "%s", buf);
ol.Available = TRUE;
sprintf(ol.Oneline, "\"640K ought to be enough for anybody.\" Bill Gates '81");
fwrite(&ol, sizeof(ol), 1, fil);
sprintf(ol.Oneline, "\"Build a watch in 179 easy steps\" by C. Forsberg.");
fwrite(&ol, sizeof(ol), 1, fil);
sprintf(ol.Oneline, "\"Keyboard? How quaint!\" - Scotty");
fwrite(&ol, sizeof(ol), 1, fil);
sprintf(ol.Oneline, "\"Luke... Luke... Use the MOUSE, Luke\" - Obi Wan Gates");
fwrite(&ol, sizeof(ol), 1, fil);
sprintf(ol.Oneline, "\"Suicide Hotline...please hold.\"");
fwrite(&ol, sizeof(ol), 1, fil);
sprintf(ol.Oneline, "(A)bort, (R)etry, (P)retend this never happened...");
fwrite(&ol, sizeof(ol), 1, fil);
sprintf(ol.Oneline, "A Smith & Wesson *ALWAYS* beats 4 Aces.");
fwrite(&ol, sizeof(ol), 1, fil);
sprintf(ol.Oneline, "A dirty book is rarely dusty.");
fwrite(&ol, sizeof(ol), 1, fil);
sprintf(ol.Oneline, "An Elephant; A Mouse built to government specifications.");
fwrite(&ol, sizeof(ol), 1, fil);
sprintf(ol.Oneline, "At a store: In God we trust; all others pay cash.");
fwrite(&ol, sizeof(ol), 1, fil);
2001-08-17 05:46:24 +00:00
fclose(fil);
2001-11-14 21:37:58 +00:00
chmod(ffile, 0660);
return 10;
2001-08-17 05:46:24 +00:00
} else
return -1;
}
count = 0;
fread(&olhdr, sizeof(olhdr), 1, fil);
while (fread(&ol, olhdr.recsize, 1, fil) == 1) {
count++;
}
fclose(fil);
return count;
}
/*
* Open database for editing. The datafile is copied, if the format
* is changed it will be converted on the fly. All editing must be
* done on the copied file.
*/
int OpenOneline(void);
2001-08-17 05:46:24 +00:00
int OpenOneline(void)
{
FILE *fin, *fout;
char fnin[PATH_MAX], fnout[PATH_MAX];
2001-08-17 05:46:24 +00:00
long oldsize;
sprintf(fnin, "%s/etc/oneline.data", getenv("MBSE_ROOT"));
sprintf(fnout, "%s/etc/oneline.temp", getenv("MBSE_ROOT"));
if ((fin = fopen(fnin, "r")) != NULL) {
if ((fout = fopen(fnout, "w")) != NULL) {
fread(&olhdr, sizeof(olhdr), 1, fin);
/*
* In case we are automaic upgrading the data format
* we save the old format. If it is changed, the
* database must always be updated.
*/
oldsize = olhdr.recsize;
if (oldsize != sizeof(ol)) {
2001-08-17 05:46:24 +00:00
OnelUpdated = 1;
Syslog('+', "Upgraded %s, format changed", fnin);
} else
2001-08-17 05:46:24 +00:00
OnelUpdated = 0;
olhdr.hdrsize = sizeof(olhdr);
olhdr.recsize = sizeof(ol);
fwrite(&olhdr, sizeof(olhdr), 1, fout);
/*
* The datarecord is filled with zero's before each
* read, so if the format changed, the new fields
* will be empty.
*/
memset(&ol, 0, sizeof(ol));
while (fread(&ol, oldsize, 1, fin) == 1) {
fwrite(&ol, sizeof(ol), 1, fout);
memset(&ol, 0, sizeof(ol));
}
fclose(fin);
fclose(fout);
return 0;
} else
return -1;
}
return -1;
}
void CloseOneline(int);
void CloseOneline(int force)
2001-08-17 05:46:24 +00:00
{
char fin[PATH_MAX], fout[PATH_MAX];
2001-08-17 05:46:24 +00:00
sprintf(fin, "%s/etc/oneline.data", getenv("MBSE_ROOT"));
sprintf(fout,"%s/etc/oneline.temp", getenv("MBSE_ROOT"));
if (OnelUpdated == 1) {
if (force || (yes_no((char *)"Database is changed, save changes") == 1)) {
2001-08-17 05:46:24 +00:00
working(1, 0, 0);
if ((rename(fout, fin)) == 0)
unlink(fout);
2001-11-14 21:37:58 +00:00
chmod(fin, 0660);
2001-08-17 05:46:24 +00:00
Syslog('+', "Updated \"oneline.data\"");
return;
}
}
2002-01-25 20:21:50 +00:00
chmod(fin, 0660);
2001-08-17 05:46:24 +00:00
working(1, 0, 0);
unlink(fout);
}
int AppendOneline(void)
{
FILE *fil;
char ffile[PATH_MAX];
2001-08-17 05:46:24 +00:00
sprintf(ffile, "%s/etc/oneline.temp", getenv("MBSE_ROOT"));
if ((fil = fopen(ffile, "a")) != NULL) {
memset(&ol, 0, sizeof(ol));
fwrite(&ol, sizeof(ol), 1, fil);
fclose(fil);
OnelUpdated = 1;
return 0;
} else
return -1;
}
/*
* Edit one record, return -1 if there are errors, 0 if ok.
*/
int EditOnelRec(int Area)
{
FILE *fil;
char mfile[PATH_MAX];
2001-08-17 05:46:24 +00:00
long offset;
int j;
unsigned long crc, crc1;
clr_index();
working(1, 0, 0);
IsDoing("Edit Oneline");
sprintf(mfile, "%s/etc/oneline.temp", getenv("MBSE_ROOT"));
if ((fil = fopen(mfile, "r")) == NULL) {
working(2, 0, 0);
return -1;
}
offset = sizeof(olhdr) + ((Area -1) * sizeof(ol));
if (fseek(fil, offset, 0) != 0) {
working(2, 0, 0);
return -1;
}
fread(&ol, sizeof(ol), 1, fil);
fclose(fil);
crc = 0xffffffff;
crc = upd_crc32((char *)&ol, crc, sizeof(ol));
working(0, 0, 0);
set_color(WHITE, BLACK);
mvprintw( 5, 2, "8.7.1 EDIT ONELINER");
2001-08-17 05:46:24 +00:00
set_color(CYAN, BLACK);
mvprintw( 7, 2, "1. Text");
mvprintw( 8, 2, "2. User");
mvprintw( 9, 2, "3. Date");
mvprintw(10, 2, "4. Avail");
for (;;) {
set_color(WHITE, BLACK);
show_str( 7,12,68, ol.Oneline);
show_str( 8,12,35, ol.UserName);
show_str( 9,12,10, ol.DateOfEntry);
show_bool(10,12, ol.Available);
j = select_menu(4);
switch(j) {
case 0: crc1 = 0xffffffff;
crc1 = upd_crc32((char *)&ol, crc1, sizeof(ol));
if (crc != crc1) {
if (yes_no((char *)"Record is changed, save") == 1) {
working(1, 0, 0);
if ((fil = fopen(mfile, "r+")) == NULL) {
working(2, 0, 0);
return -1;
}
fseek(fil, offset, 0);
fwrite(&ol, sizeof(ol), 1, fil);
fclose(fil);
OnelUpdated = 1;
working(1, 0, 0);
working(0, 0, 0);
}
}
IsDoing("Browsing Menu");
return 0;
case 1: E_STR( 7,12,68,ol.Oneline, "The ^Oneline^ text to show")
case 2: E_STR( 8,12,30,ol.UserName, "The ^Username^ of the owner of this oneline")
case 3: E_STR( 9,12,10,ol.DateOfEntry,"The ^Date^ this oneliner is added, format: ^DD-MM-YYYY^")
case 4: E_BOOL(10,12, ol.Available, "Is this oneline ^available^")
}
}
return 0;
}
void EditOneline(void)
{
int records, i, x, y, o;
char pick[12];
FILE *fil;
char temp[PATH_MAX];
2001-08-17 05:46:24 +00:00
long offset;
clr_index();
working(1, 0, 0);
IsDoing("Browsing Menu");
if (config_read() == -1) {
working(2, 0, 0);
return;
}
records = CountOneline();
if (records == -1) {
working(2, 0, 0);
return;
}
if (OpenOneline() == -1) {
working(2, 0, 0);
return;
}
working(0, 0, 0);
o = 0;
for (;;) {
clr_index();
set_color(WHITE, BLACK);
mvprintw( 5, 2, "8.7.1 ONELINERS SETUP");
2001-08-17 05:46:24 +00:00
set_color(CYAN, BLACK);
if (records != 0) {
sprintf(temp, "%s/etc/oneline.temp", getenv("MBSE_ROOT"));
working(1, 0, 0);
if ((fil = fopen(temp, "r")) != NULL) {
fread(&olhdr, sizeof(olhdr), 1, fil);
x = 2;
y = 7;
set_color(CYAN, BLACK);
for (i = 1; i <= 20; i++) {
if (i == 11) {
x = 42;
y = 7;
}
if ((o + i) <= records) {
offset = sizeof(olhdr) + (((o + i) - 1) * olhdr.recsize);
fseek(fil, offset, 0);
fread(&ol, olhdr.recsize, 1, fil);
if (ol.Available)
set_color(CYAN, BLACK);
else
set_color(LIGHTBLUE, BLACK);
sprintf(temp, "%3d. %-32s", o + i, ol.Oneline);
temp[38] = '\0';
mvprintw(y, x, temp);
y++;
}
}
fclose(fil);
}
}
working(0, 0, 0);
strcpy(pick, select_record(records,20));
if (strncmp(pick, "-", 1) == 0) {
CloseOneline(FALSE);
2001-08-17 05:46:24 +00:00
return;
}
if (strncmp(pick, "A", 1) == 0) {
working(1, 0, 0);
if (AppendOneline() == 0) {
records++;
working(1, 0, 0);
} else
working(2, 0, 0);
working(0, 0, 0);
}
if (strncmp(pick, "N", 1) == 0)
if ((o + 20) < records)
o = o + 20;
if (strncmp(pick, "P", 1) == 0)
if ((o - 20) >= 0)
o = o - 20;
2002-01-19 19:27:29 +00:00
if ((atoi(pick) >= 1) && (atoi(pick) <= records)) {
2001-08-17 05:46:24 +00:00
EditOnelRec(atoi(pick));
2002-01-19 19:27:29 +00:00
o = ((atoi(pick) - 1) / 20) * 20;
}
2001-08-17 05:46:24 +00:00
}
}
void InitOneline(void)
{
CountOneline();
OpenOneline();
CloseOneline(TRUE);
}
2001-08-17 05:46:24 +00:00
void PurgeOneline(void)
{
FILE *pOneline, *fp;
int recno = 0, iCount = 0;
char *sFileName, temp[81];
clr_index();
set_color(WHITE, BLACK);
mvprintw( 5, 6, "8.7.2 ONELINERS PURGE");
set_color(CYAN, BLACK);
working(1, 0, 0);
if (config_read() == -1) {
working(2, 0, 0);
return;
}
IsDoing("Purge Oneliners");
sFileName = calloc(PATH_MAX, sizeof(char));
sprintf(sFileName,"%s/etc/oneline.data", getenv("MBSE_ROOT"));
if ((pOneline = fopen(sFileName, "r")) == NULL) {
free(sFileName);
return;
}
fread(&olhdr, sizeof(olhdr), 1, pOneline);
while (fread(&ol, olhdr.recsize, 1, pOneline) == 1) {
recno++;
if (!ol.Available)
iCount++;
}
working(0, 0, 0);
sprintf(temp, "%d records, %d records to purge", recno, iCount);
mvprintw(7, 6, temp);
if (iCount == 0) {
mvprintw(9, 6, "Press any key");
readkey(9, 20, LIGHTGRAY, BLACK);
free(sFileName);
return;
}
if (yes_no((char *)"Purge deleted records") == TRUE) {
2001-08-17 05:46:24 +00:00
working(1, 0, 0);
fseek(pOneline, olhdr.hdrsize, 0);
fp = fopen("tmp.1", "a");
fwrite(&olhdr, sizeof(olhdr), 1, fp);
2001-08-17 05:46:24 +00:00
while (fread(&ol, olhdr.recsize, 1, pOneline) == 1) {
if (ol.Available)
fwrite(&ol, olhdr.recsize, 1, fp);
2001-08-17 05:46:24 +00:00
}
fclose(fp);
fclose(pOneline);
if ((rename("tmp.1", sFileName)) != 0)
working(2, 0, 0);
unlink("tmp.1");
2001-08-17 05:46:24 +00:00
working(0, 0, 0);
free(sFileName);
}
2001-08-17 05:46:24 +00:00
}
void ImportOneline(void)
{
FILE *Imp, *pOneline;
int recno = 0, skipped = 0;
struct tm *l_date;
char *temp, buf[12];
time_t Time;
clr_index();
set_color(WHITE, BLACK);
mvprintw(5, 6, "8.7.3 IMPORT ONELINERS");
set_color(CYAN, BLACK);
temp = calloc(PATH_MAX, sizeof(char));
memset(temp, 0, sizeof(temp));
strcpy(temp, edit_str(21, 6,64, temp, (char *)"The ^full path and filename^ of the file to import"));
if (strlen(temp) == 0) {
free(temp);
return;
}
working(1, 0, 0);
if (config_read() == -1) {
working(2, 0, 0);
free(temp);
return;
}
if ((Imp = fopen(temp, "r")) == NULL) {
working(2, 0, 0);
working(0, 0, 0);
mvprintw(21, 6, temp);
readkey(22, 6, LIGHTGRAY, BLACK);
free(temp);
return;
}
sprintf(temp, "%s/etc/oneline.data", getenv("MBSE_ROOT"));
/*
* Check if database exists, if not create a new one
*/
if ((pOneline = fopen(temp, "r" )) == NULL) {
if ((pOneline = fopen(temp, "w")) != NULL) {
olhdr.hdrsize = sizeof(olhdr);
olhdr.recsize = sizeof(ol);
fwrite(&olhdr, sizeof(olhdr), 1, pOneline);
fclose(pOneline);
2001-08-17 05:46:24 +00:00
}
} else
fclose(pOneline);
2001-08-17 05:46:24 +00:00
/*
* Open database for appending
*/
if ((pOneline = fopen(temp, "a+")) == NULL) {
working(2, 0, 0);
working(0, 0, 0);
fclose(Imp);
mvprintw(21, 6, temp);
readkey(22, 6, LIGHTGRAY, BLACK);
free(temp);
return;
}
Time = time(NULL);
l_date = localtime(&Time);
sprintf(buf, "%02d-%02d-%04d", l_date->tm_mday, l_date->tm_mon+1, l_date->tm_year+1900);
while ((fgets(temp, 80, Imp)) != NULL) {
Striplf(temp);
if ((strlen(temp) > 0) && (strlen(temp) < 69)) {
memset(&ol, 0, sizeof(ol));
strcpy(ol.Oneline, temp);
strcpy(ol.UserName, CFG.sysop_name);
strcpy(ol.DateOfEntry, buf);
ol.Available = TRUE;
fwrite(&ol, sizeof(ol), 1, pOneline);
recno++;
} else {
skipped++;
2001-08-17 05:46:24 +00:00
}
}
2001-08-17 05:46:24 +00:00
fclose(Imp);
fclose(pOneline);
working(0, 0, 0);
2001-08-17 05:46:24 +00:00
sprintf(temp, "Imported %d records, skipped %d long/empty lines", recno, skipped);
mvprintw(21, 6, temp);
readkey(21, 7 + strlen(temp), LIGHTGRAY, BLACK);
free(temp);
2001-08-17 05:46:24 +00:00
}
void ol_menu(void)
{
for (;;) {
clr_index();
set_color(WHITE, BLACK);
mvprintw( 5, 6, "8.7 ONELINER SETUP");
set_color(CYAN, BLACK);
mvprintw( 7, 6, "1. Edit Oneliners");
mvprintw( 8, 6, "2. Purge Oneliners");
mvprintw( 9, 6, "3. Import Oneliners");
switch(select_menu(3)) {
case 0: return;
case 1: EditOneline();
break;
case 2: PurgeOneline();
break;
case 3: ImportOneline();
break;
2001-08-17 05:46:24 +00:00
}
}
2001-08-17 05:46:24 +00:00
}