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

450 lines
11 KiB
C
Raw Normal View History

2001-08-17 05:46:24 +00:00
/*****************************************************************************
*
* $Id$
2001-08-17 05:46:24 +00:00
* Purpose ...............: Oneliner functions.
*
*****************************************************************************
2005-08-29 10:29:22 +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"
2001-08-17 05:46:24 +00:00
#include "../lib/mbse.h"
2002-01-07 19:16:03 +00:00
#include "../lib/users.h"
2001-08-17 05:46:24 +00:00
#include "oneline.h"
#include "funcs.h"
#include "input.h"
2001-08-17 05:46:24 +00:00
#include "language.h"
#include "term.h"
2004-11-03 20:48:45 +00:00
#include "ttyio.h"
2001-08-17 05:46:24 +00:00
char sOneliner[81];
int iColour; /* Current color */
void Oneliner_Check()
{
FILE *pOneline;
char *sFileName;
2001-08-17 05:46:24 +00:00
sFileName = calloc(PATH_MAX, sizeof(char));
2005-08-29 10:29:22 +00:00
snprintf(sFileName, PATH_MAX, "%s/etc/oneline.data", getenv("MBSE_ROOT"));
if ((pOneline = fopen(sFileName, "r")) == NULL) {
if ((pOneline = fopen(sFileName, "w")) != NULL) {
olhdr.hdrsize = sizeof(olhdr);
olhdr.recsize = sizeof(ol);
fwrite(&olhdr, sizeof(olhdr), 1, pOneline);
fclose(pOneline);
Syslog('-', "Created oneliner database");
2001-08-17 05:46:24 +00:00
}
} else {
fclose(pOneline);
}
chmod(sFileName, 0660);
free(sFileName);
2001-08-17 05:46:24 +00:00
}
void Oneliner_Add()
{
2004-11-03 20:48:45 +00:00
FILE *pOneline;
char *sFileName;
int x;
char temp[81];
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
Oneliner_Check();
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
sFileName = calloc(PATH_MAX, sizeof(char));
2005-08-29 10:29:22 +00:00
snprintf(sFileName, PATH_MAX, "%s/etc/oneline.data", getenv("MBSE_ROOT"));
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
if ((pOneline = fopen(sFileName, "a+")) == NULL) {
WriteError("Can't open file: %s", sFileName);
return;
}
free(sFileName);
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
memset(&ol, 0, sizeof(ol));
clear();
/* MBSE BBS Oneliners will randomly appear on the main menu. */
poutCR(WHITE, BLACK, Language(341));
Enter(1);
/* Obscene or libellous oneliners will be deleted!! */
poutCR(WHITE, BLUE, Language(342));
Enter(1);
/* Please enter your oneliner below. You have 75 characters.*/
poutCR(LIGHTRED, BLACK, Language(343));
pout(WHITE, BLACK, (char *)"> ");
colour(CFG.InputColourF, CFG.InputColourB);
GetstrC(temp, 75);
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
if ((strcmp(temp, "")) == 0) {
fclose(pOneline);
return;
} else {
x = strlen(temp);
if (x >= 78)
temp[78] = '\0';
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
strcpy(ol.Oneline, temp);
}
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
Enter(1);
/* Oneliner added */
pout(CYAN, BLACK, Language(344));
Enter(2);
Pause();
Syslog('!', "User added oneliner:");
Syslog('!', ol.Oneline);
2001-08-17 05:46:24 +00:00
2005-08-29 10:29:22 +00:00
snprintf(ol.UserName,36,"%s", exitinfo.sUserName);
snprintf(ol.DateOfEntry,12,"%02d-%02d-%04d",l_date->tm_mday,l_date->tm_mon+1,l_date->tm_year+1900);
2004-11-03 20:48:45 +00:00
ol.Available = TRUE;
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
fwrite(&ol, sizeof(ol), 1, pOneline);
fclose(pOneline);
2001-08-17 05:46:24 +00:00
}
/*
* Print global string sOneliner centered on the screen
*/
void Oneliner_Print()
{
2004-11-03 20:48:45 +00:00
int i, x, z, Strlen, Maxlen = 80;
char sNewOneliner[81] = "";
/*
* Select a new colour
*/
if (iColour < 8)
iColour = 8;
else
if (iColour == 15)
iColour = 8;
2001-08-17 05:46:24 +00:00
else
2004-11-03 20:48:45 +00:00
iColour++;
/*
* Get a random oneliner
*/
strcpy(sOneliner, Oneliner_Get());
/*
* Now display it on screen
*/
Strlen = strlen(sOneliner);
if (Strlen == Maxlen) {
PUTSTR(sOneliner);
Enter(1);
} else {
x = Maxlen - Strlen;
z = x / 2;
for(i = 0; i < z; i++)
strcat(sNewOneliner," ");
strcat(sNewOneliner, sOneliner);
colour(iColour, 0);
PUTSTR(sNewOneliner);
Enter(1);
}
2001-08-17 05:46:24 +00:00
}
/*
* Get a random oneliner
*/
char *Oneliner_Get()
{
2004-11-03 20:48:45 +00:00
FILE *pOneline;
int i, j, in, id, recno = 0;
long offset;
int nrecno;
char *sFileName;
static char temp[81];
/*
* Get a random oneliner
*/
sFileName = calloc(128, sizeof(char));
2005-08-29 10:29:22 +00:00
snprintf(sFileName, PATH_MAX, "%s/etc/oneline.data", getenv("MBSE_ROOT"));
2004-11-03 20:48:45 +00:00
if ((pOneline = fopen(sFileName, "r+")) == NULL) {
WriteError("Can't open file: %s", sFileName);
return '\0';
}
fread(&olhdr, sizeof(olhdr), 1, pOneline);
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
while (fread(&ol, olhdr.recsize, 1, pOneline) == 1) {
recno++;
}
nrecno = recno;
fseek(pOneline, olhdr.hdrsize, 0);
/*
* Generate random record number
*/
while (TRUE) {
in = nrecno;
id = getpid();
i = rand();
j = i % id;
if ((j <= in))
break;
}
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
offset = olhdr.hdrsize + (j * olhdr.recsize);
if (fseek(pOneline, offset, 0) != 0) {
WriteError("Can't move pointer in %s", sFileName);
return '\0';
}
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
fread(&ol, olhdr.recsize, 1, pOneline);
memset(&temp, 0, sizeof(temp));
strcpy(temp, ol.Oneline);
fclose(pOneline);
free(sFileName);
return temp;
2001-08-17 05:46:24 +00:00
}
/*
* List Oneliners
*/
void Oneliner_List()
{
2004-11-03 20:48:45 +00:00
FILE *pOneline;
int recno = 0, Colour = 1;
char *sFileName, msg[81];
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
clear();
sFileName = calloc(PATH_MAX, sizeof(char));
2005-08-29 10:29:22 +00:00
snprintf(sFileName, PATH_MAX, "%s/etc/oneline.data", getenv("MBSE_ROOT"));
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
if ((pOneline = fopen(sFileName, "r+")) == NULL) {
WriteError("Can't open file: %s", sFileName);
return;
}
fread(&olhdr, sizeof(olhdr), 1, pOneline);
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
if ((SYSOP == TRUE) || (exitinfo.Security.level >= CFG.sysop_access)) {
/* # A Date User Description */
pout(LIGHTGREEN, BLACK, Language(345));
} else {
/* # Description */
pout(LIGHTGREEN, BLACK, Language(346));
}
Enter(1);
colour(GREEN, BLACK);
sLine();
while (fread(&ol, olhdr.recsize, 1, pOneline) == 1) {
if ((SYSOP == TRUE) || (exitinfo.Security.level >= CFG.sysop_access)) {
2005-08-29 10:29:22 +00:00
snprintf(msg, 81, "%2d", recno);
2004-11-03 20:48:45 +00:00
pout(WHITE, BLACK, msg);
2005-08-29 10:29:22 +00:00
snprintf(msg, 81, "%2d ", ol.Available);
2004-11-03 20:48:45 +00:00
pout(LIGHTBLUE, BLACK, msg);
pout(LIGHTCYAN, BLACK, ol.DateOfEntry);
2005-08-29 10:29:22 +00:00
snprintf(msg, 81, "%-15s ", ol.UserName);
2004-11-03 20:48:45 +00:00
pout(CYAN, BLACK, msg);
2005-08-29 10:29:22 +00:00
snprintf(msg, 81, "%-.48s", ol.Oneline);
2004-11-03 20:48:45 +00:00
poutCR(Colour, BLACK, msg);
2001-08-17 05:46:24 +00:00
} else {
2005-08-29 10:29:22 +00:00
snprintf(msg, 81, "%2d ", recno);
2004-11-03 20:48:45 +00:00
pout(WHITE, BLACK, msg);
2005-08-29 10:29:22 +00:00
snprintf(msg, 81, "%-.76s", ol.Oneline);
2004-11-03 20:48:45 +00:00
poutCR(Colour, BLACK, msg);
2001-08-17 05:46:24 +00:00
}
2004-11-03 20:48:45 +00:00
recno++;
Colour++;
if (Colour >= 16)
Colour = 1;
}
fclose(pOneline);
Enter(1);
Pause();
free(sFileName);
2001-08-17 05:46:24 +00:00
}
void Oneliner_Show()
{
2004-11-03 20:48:45 +00:00
FILE *pOneline;
int recno = 0;
long offset;
2005-08-29 13:42:56 +00:00
char *sFileName, msg[81];
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
sFileName = calloc(PATH_MAX, sizeof(char));
2005-08-29 10:29:22 +00:00
snprintf(sFileName, PATH_MAX, "%s/etc/oneline.data", getenv("MBSE_ROOT"));
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
if ((pOneline = fopen(sFileName, "r+")) == NULL) {
WriteError("Can't open file: %s", sFileName);
return;
}
fread(&olhdr, sizeof(olhdr), 1, pOneline);
2005-08-29 13:42:56 +00:00
fseek(pOneline, 0, SEEK_END);
recno = (ftell(pOneline) - olhdr.hdrsize) / olhdr.recsize;
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
Enter(1);
/* Please enter number to list: */
2005-08-29 13:42:56 +00:00
snprintf(msg, 81, "%s (1..%d) ", Language(347), recno -1);
pout(WHITE, BLACK, msg);
2004-11-03 20:48:45 +00:00
colour(CFG.InputColourF, CFG.InputColourB);
2005-08-29 13:42:56 +00:00
msg[0] = '\0';
Getnum(msg, 10);
recno = atoi(msg);
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
offset = olhdr.hdrsize + (recno * olhdr.recsize);
2005-08-29 13:42:56 +00:00
if (fseek(pOneline, offset, SEEK_SET) != 0)
2004-11-03 20:48:45 +00:00
WriteError("Can't move pointer in %s",sFileName);
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
fread(&ol, olhdr.recsize, 1, pOneline);
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
Enter(1);
2005-08-29 10:29:22 +00:00
snprintf(msg, 11, "%d ", recno);
2004-11-03 20:48:45 +00:00
pout(WHITE, BLACK, msg);
pout(LIGHTRED, BLACK, ol.Oneline);
Enter(2);
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
Pause();
fclose(pOneline);
free(sFileName);
2001-08-17 05:46:24 +00:00
}
void Oneliner_Delete()
{
2004-11-03 20:48:45 +00:00
FILE *pOneline;
int recno = 0, nrecno = 0;
long offset;
char srecno[7], *sFileName, stemp[50], sUser[36], msg[81];
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
sFileName = calloc(PATH_MAX, sizeof(char));
2005-08-29 10:29:22 +00:00
snprintf(sFileName, PATH_MAX, "%s/etc/oneline.data", getenv("MBSE_ROOT"));
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
if ((pOneline = fopen(sFileName, "r+")) == NULL) {
WriteError("Can't open file: %s", sFileName);
return;
}
fread(&olhdr, sizeof(olhdr), 1, pOneline);
Enter(1);
/* Please enter number to delete: */
pout(WHITE, BLACK, Language(331));
colour(CFG.InputColourF, CFG.InputColourB);
GetstrC(srecno, 6);
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
if ((strcmp(srecno,"")) == 0) {
fclose(pOneline);
return;
}
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
recno = atoi(srecno);
nrecno = recno;
recno = 0;
2001-08-17 05:46:24 +00:00
2004-11-03 20:48:45 +00:00
while (fread(&ol, olhdr.recsize, 1, pOneline) == 1) {
recno++;
}
if (nrecno >= recno) {
Enter(1);
/* Record does not exist */
pout(LIGHTRED, BLACK, Language(319));
Enter(2);
fclose(pOneline);
Pause();
} else {
offset = olhdr.hdrsize + (nrecno * olhdr.recsize);
if (fseek(pOneline, offset, 0) != 0) {
WriteError("Can't move pointer in %s",sFileName);
2001-08-17 05:46:24 +00:00
}
2004-11-03 20:48:45 +00:00
fread(&ol, olhdr.recsize, 1, pOneline);
/* Convert Record Int to string, so we can print to logfiles */
2005-08-29 10:29:22 +00:00
snprintf(stemp,50,"%d", nrecno);
2004-11-03 20:48:45 +00:00
/* Print UserName to String, so we can compare for deletion */
2005-08-29 10:29:22 +00:00
snprintf(sUser,36,"%s", exitinfo.sUserName);
2004-11-03 20:48:45 +00:00
if ((strcmp(sUser, ol.UserName)) != 0) {
if ((!SYSOP) && (exitinfo.Security.level < CFG.sysop_access)) {
2001-08-17 05:46:24 +00:00
Enter(1);
2004-11-03 20:48:45 +00:00
/* Record *//* does not belong to you.*/
2005-08-29 10:29:22 +00:00
snprintf(msg, 81, "%s%s %s", (char *) Language(332), stemp, (char *) Language(333));
2004-11-03 20:48:45 +00:00
pout(LIGHTRED, BLACK, msg);
2001-08-17 05:46:24 +00:00
Enter(2);
2004-11-03 20:48:45 +00:00
Syslog('!', "User tried to delete somebody else's record: %s", stemp);
2001-08-17 05:46:24 +00:00
Pause();
2004-11-03 20:48:45 +00:00
fclose(pOneline);
return;
}
}
Enter(1);
if ((ol.Available ) == FALSE) {
/* Record: %d already marked for deletion */
2005-08-29 10:29:22 +00:00
snprintf(msg, 81, "%s%d %s", (char *) Language(332), nrecno, (char *) Language(334));
2004-11-03 20:48:45 +00:00
pout(LIGHTRED, BLACK, msg);
Syslog('!', "User tried to mark an already marked record: %s", stemp);
2001-08-17 05:46:24 +00:00
} else {
2004-11-03 20:48:45 +00:00
ol.Available = FALSE;
/* Record *//* marked for deletion */
2005-08-29 10:29:22 +00:00
snprintf(msg, 81, "%s%d %s", (char *) Language(332), nrecno, (char *) Language(334));
2004-11-03 20:48:45 +00:00
pout(LIGHTGREEN, BLACK, msg);
Syslog('+', "User marked oneliner record for deletion: %s", stemp);
2001-08-17 05:46:24 +00:00
}
2004-11-03 20:48:45 +00:00
Enter(2);
Pause();
if (fseek(pOneline, offset, 0) != 0)
WriteError("Can't move pointer in %s",sFileName);
fwrite(&ol, olhdr.recsize, 1, pOneline);
}
fclose(pOneline);
free(sFileName);
2001-08-17 05:46:24 +00:00
}