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

275 lines
5.6 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 ...............: Change BBS status
*
*****************************************************************************
2004-01-25 10:57:35 +00:00
* Copyright (C) 1997-2004
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 "mbstat.h"
extern int do_quiet;
time_t t_start, t_end;
void Help(void)
{
do_quiet = FALSE;
ProgName();
colour(11, 0);
printf("\nUsage: mbstat [command] <options>\n\n");
colour(9, 0);
printf(" Commands are:\n\n");
colour(3, 0);
printf(" c close Close the BBS for users\n");
printf(" o open Open the BBS for users\n");
printf(" s set semafore Set named semafore\n");
printf(" w wait Wait until the BBS is free\n\n");
colour(9,0);
printf(" Options are:\n\n");
colour(3, 0);
printf(" -q -quiet Quiet, no screen output\n");
colour(7, 0);
2002-10-20 20:58:55 +00:00
die(MBERR_COMMANDLINE);
2001-08-17 05:46:24 +00:00
}
void ProgName(void)
{
if (do_quiet)
return;
colour(15, 0);
printf("\nMBSTAT: MBSE BBS %s Status Changer\n", VERSION);
colour(14, 0);
printf(" %s\n", COPYRIGHT);
2001-08-17 05:46:24 +00:00
colour(3, 0);
}
void die(int onsig)
{
signal(onsig, SIG_IGN);
if (onsig)
2002-05-10 18:14:27 +00:00
WriteError("Terminated on signal %d", onsig);
2001-08-17 05:46:24 +00:00
if (!do_quiet) {
colour(7, 0);
printf("\n");
}
2001-12-23 16:44:18 +00:00
t_end = time(NULL);
2001-08-17 05:46:24 +00:00
Syslog(' ', "MBSTAT finished in %s", t_elapsed(t_start, t_end));
ExitClient(onsig);
}
int main(int argc, char **argv)
{
int i;
char *cmd, *semafore = NULL;
int do_open = FALSE;
int do_close = FALSE;
int do_wait = FALSE;
int do_sema = FALSE;
struct passwd *pw;
InitConfig();
2004-02-21 17:22:00 +00:00
TermInit(1, 80, 24);
2001-12-23 16:44:18 +00:00
t_start = time(NULL);
2001-08-17 05:46:24 +00:00
/*
* Catch or ignore signals
*/
for (i = 0; i < NSIG; i++) {
2002-10-20 20:58:55 +00:00
if ((i == SIGHUP) || (i == SIGINT) || (i == SIGBUS) || (i == SIGILL) || (i == SIGSEGV) || (i == SIGTERM))
2001-08-17 05:46:24 +00:00
signal(i, (void (*))die);
2003-12-04 22:07:33 +00:00
else if ((i != SIGKILL) && (i != SIGSTOP))
2001-08-17 05:46:24 +00:00
signal(i, SIG_IGN);
}
cmd = xstrcpy((char *)"Command line: mbstat");
for (i = 1; i < argc; i++) {
cmd = xstrcat(cmd, (char *)" ");
cmd = xstrcat(cmd, tl(argv[i]));
if (!strncmp(tl(argv[i]), "w", 1))
do_wait = TRUE;
if (!strncmp(tl(argv[i]), "o", 1))
do_open = TRUE;
if (!strncmp(tl(argv[i]), "c", 1))
do_close = TRUE;
if (!strncmp(tl(argv[i]), "s", 1)) {
do_sema = TRUE;
i++;
semafore = xstrcpy(tl(argv[i]));
cmd = xstrcat(cmd, (char *)" ");
cmd = xstrcat(cmd, tl(argv[i]));
}
if (!strncmp(tl(argv[i]), "-q", 2))
do_quiet = TRUE;
}
ProgName();
pw = getpwuid(getuid());
2003-09-09 19:39:51 +00:00
InitClient(pw->pw_name, (char *)"mbstat", CFG.location, CFG.logfile,
CFG.util_loglevel, CFG.error_log, CFG.mgrlog, CFG.debuglog);
2001-08-17 05:46:24 +00:00
Syslog(' ', " ");
Syslog(' ', "MBSTAT v%s", VERSION);
Syslog(' ', cmd);
free(cmd);
if (!do_quiet) {
colour(3, 0);
printf("\n");
}
if (do_close)
do_open = FALSE;
if (do_open) {
Open();
do_close = FALSE;
do_wait = FALSE;
}
if (do_close)
Close();
if (do_wait)
Wait();
if (do_sema)
Semafore(semafore);
if (!(do_open || do_close || do_wait || do_sema))
Help();
2002-10-20 20:58:55 +00:00
die(MBERR_OK);
2001-08-17 05:46:24 +00:00
return 0;
}
int Semafore(char *semafore)
{
char buf[81];
strcpy(buf, SockR("SECR:1,%s;", semafore));
if (strncmp(buf, "100:0;", 6) == 0) {
Syslog('+', "Semafore \"%s\" is set", semafore);
if (!do_quiet)
printf("Semafore \"%s\" is set\n", semafore);
return TRUE;
} else {
Syslog('+', "Failed to set \"%s\" semafore", semafore);
printf("Failed to set \"%s\" semafore\n", semafore);
return FALSE;
}
}
int Close(void)
{
char buf[81];
strcpy(buf, SockR("SCLO:0;"));
if (strncmp(buf, "100:0;", 6) == 0) {
Syslog('+', "The BBS is closed");
if (!do_quiet)
printf("The BBS is closed\n");
return TRUE;
} else {
printf("Failed to close the BBS\n");
return FALSE;
}
}
int Open(void)
{
char buf[81];
strcpy(buf, SockR("SOPE:0;"));
if (strncmp(buf, "100:0;", 6) == 0) {
Syslog('+', "The BBS is open");
if (!do_quiet)
printf("The BBS is open\n");
return TRUE;
} else {
printf("Failed to open the BBS\n");
return FALSE;
}
}
int Wait(void)
{
int Waiting = 3600;
char buf[PATH_MAX];
2004-09-14 20:37:57 +00:00
if (IsSema((char *)"upsdown"))
2001-08-17 05:46:24 +00:00
Waiting = 30;
Syslog('+', "Waiting for the BBS to become free, timeout %d seconds", Waiting);
2001-08-17 05:46:24 +00:00
while (Waiting) {
strcpy(buf, SockR("SFRE:0;"));
if (strncmp(buf, "100:0;", 6) == 0) {
Syslog('+', "The BBS is free");
if (!do_quiet)
printf("The BBS is free. \n");
return TRUE;
}
if (!do_quiet) {
buf[strlen(buf) -1] = '\0';
printf("\r%s\r", buf+6);
fflush(stdout);
}
sleep(1);
Waiting--;
}
WriteError("Wait for BBS free timeout, aborting");
return FALSE;
}