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

1044 lines
29 KiB
C
Raw Normal View History

/*****************************************************************************
*
* $Id$
* Purpose ...............: mbtask - chat server
*
*****************************************************************************
2004-02-21 17:22:00 +00:00
* Copyright (C) 1997-2004
*
* 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
* Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*****************************************************************************/
#include "../config.h"
2004-02-21 17:22:00 +00:00
#include "../lib/mbselib.h"
2003-03-24 19:44:38 +00:00
#include "taskutil.h"
#include "taskregs.h"
#include "taskchat.h"
2005-04-16 14:49:58 +00:00
#include "taskibc.h"
2005-04-15 21:36:44 +00:00
2005-04-23 14:27:52 +00:00
#ifndef USE_EXPERIMENT
2003-04-01 21:41:36 +00:00
#define MAXCHANNELS 10 /* Maximum chat channels */
2005-04-23 14:27:52 +00:00
#endif
2003-03-24 19:44:38 +00:00
#define MAXMESSAGES 100 /* Maximum ringbuffer for messages */
typedef enum {CH_FREE, CH_PRIVATE, CH_PUBLIC} CHANNELTYPE;
/*
* Users connected to the chatserver.
*/
typedef struct _ch_user_rec {
pid_t pid; /* User's pid */
2005-04-20 16:59:32 +00:00
char realname[36]; /* Real name */
2005-04-22 20:01:21 +00:00
char name[10]; /* Unix name */
2005-04-20 16:59:32 +00:00
char nick[10]; /* Nickname */
2003-03-24 19:44:38 +00:00
time_t connected; /* Time connected */
2005-04-23 14:27:52 +00:00
#ifdef USE_EXPERIMENT
char channel[21]; /* Connected channel */
#else
2003-03-24 19:44:38 +00:00
int channel; /* Connected channel or -1 */
2005-04-23 14:27:52 +00:00
#endif
2003-03-24 19:44:38 +00:00
int pointer; /* Message pointer */
unsigned chatting : 1; /* Is chatting in a channel */
unsigned chanop : 1; /* Is a chanop */
2003-04-02 21:36:47 +00:00
unsigned sysop : 1; /* User is sysop in channel #sysop */
2003-03-24 19:44:38 +00:00
} _chat_users;
/*
* Buffer for messages, this is the structure of a ringbuffer which will
* hold all messages, private public etc. There is one global input pointer
* which points to the current head of the ringbuffer. When a user connects
* to a channel, he will get the latest messages in the channel if they
* are present.
*/
typedef struct _chatmsg {
pid_t topid; /* Destination pid of message */
char fromname[36]; /* Message from user */
char message[81]; /* The message to display */
time_t posted; /* Timestamp for posted message */
} _chat_messages;
2003-04-01 21:41:36 +00:00
/*
* List of channels
*/
2005-04-23 14:27:52 +00:00
#ifndef USE_EXPERIMENT
2003-04-01 21:41:36 +00:00
typedef struct _channel_rec {
char name[21]; /* Channel name */
2003-04-03 20:51:19 +00:00
char topic[55]; /* Channel topic */
2003-04-01 21:41:36 +00:00
pid_t owner; /* Channel owner */
int users; /* Users in channel */
time_t created; /* Creation time */
unsigned active : 1; /* Channel active */
} _channel;
2005-04-23 14:27:52 +00:00
#endif
2003-04-01 21:41:36 +00:00
2003-03-24 19:44:38 +00:00
/*
* List of banned users from a channel. This is a dynamic list.
*/
typedef struct _banned {
int channel; /* Channel the user is banned from */
char user[36]; /* The user who is banned */
} banned_users;
2003-04-01 21:41:36 +00:00
2003-03-24 19:44:38 +00:00
/*
* The buffers
*/
2003-04-01 21:41:36 +00:00
_chat_messages chat_messages[MAXMESSAGES];
_chat_users chat_users[MAXCLIENT];
2005-04-23 14:27:52 +00:00
#ifndef USE_EXPERIMENT
2003-04-01 21:41:36 +00:00
_channel chat_channels[MAXCHANNELS];
2005-04-23 14:27:52 +00:00
#endif
2003-03-24 19:44:38 +00:00
int buffer_head = 0; /* Messages buffer head */
extern struct sysconfig CFG; /* System configuration */
2003-04-03 20:51:19 +00:00
extern int s_bbsopen; /* The BBS open status */
2005-04-20 18:45:32 +00:00
#ifdef USE_EXPERIMENT
2005-04-20 16:59:32 +00:00
extern srv_list *servers; /* Connected servers */
2005-04-20 20:27:32 +00:00
extern usr_list *users; /* Connected users */
2005-04-23 14:27:52 +00:00
extern chn_list *channels; /* Connected channels */
2005-04-22 20:10:03 +00:00
extern int usrchg;
2005-04-23 14:27:52 +00:00
extern int chnchg;
2005-04-20 18:45:32 +00:00
#endif
2003-03-24 19:44:38 +00:00
2003-04-01 21:41:36 +00:00
/*
* Prototypes
*/
2005-04-23 14:27:52 +00:00
#ifdef USE_EXPERIMENT
void chat_msg(char *, char *, char *);
#else
2003-04-01 21:41:36 +00:00
void chat_msg(int, char *, char *);
2005-04-23 14:27:52 +00:00
#endif
2003-04-03 20:51:19 +00:00
void chat_dump(void);
void system_msg(pid_t, char *);
void chat_help(pid_t);
int join(pid_t, char *, int);
int part(pid_t, char*);
2003-04-01 21:41:36 +00:00
void chat_dump(void)
{
2003-04-02 21:36:47 +00:00
int i, first;
2003-04-01 21:41:36 +00:00
2003-04-02 21:36:47 +00:00
first = TRUE;
2003-04-01 21:41:36 +00:00
for (i = 0; i < MAXCLIENT; i++)
2003-04-02 21:36:47 +00:00
if (chat_users[i].pid) {
if (first) {
2005-04-23 15:35:39 +00:00
#ifdef USE_EXPERIMENT
Syslog('u', " pid username nick channel chats sysop");
Syslog('u', "----- ------------------------------------ --------- -------------------- ----- -----");
#else
2005-04-20 16:59:32 +00:00
Syslog('u', " pid username nick ch chats sysop");
Syslog('u', "----- ------------------------------------ --------- -- ----- -----");
2005-04-23 15:35:39 +00:00
#endif
2003-04-02 21:36:47 +00:00
first = FALSE;
}
2005-04-23 15:35:39 +00:00
#ifdef USE_EXPERIMENT
Syslog('u', "%5d %-36s %-9s %-20s %s %s", chat_users[i].pid, chat_users[i].realname, chat_users[i].nick,
chat_users[i].channel, chat_users[i].chatting?"True ":"False", chat_users[i].sysop?"True ":"False");
#else
Syslog('u', "%5d %-36s %-9s %2d %s %s", chat_users[i].pid, chat_users[i].realname, chat_users[i].nick,
chat_users[i].channel, chat_users[i].chatting?"True ":"False", chat_users[i].sysop?"True ":"False");
#endif
2003-04-02 21:36:47 +00:00
}
first = TRUE;
2005-04-23 14:27:52 +00:00
#ifndef USE_EXPERIMENT
2003-04-01 21:41:36 +00:00
for (i = 0; i < MAXCHANNELS; i++)
2003-04-02 21:36:47 +00:00
if (chat_channels[i].owner) {
if (first) {
Syslog('c', "channel name owner cnt activ");
Syslog('c', "-------------------- ----- --- -----");
first = FALSE;
}
2003-04-01 21:41:36 +00:00
Syslog('c', "%-20s %5d %3d %s", chat_channels[i].name, chat_channels[i].owner, chat_channels[i].users,
chat_channels[i].active?"True":"False");
2003-04-02 21:36:47 +00:00
}
2005-04-23 14:27:52 +00:00
#endif
2003-04-01 21:41:36 +00:00
}
2003-03-24 19:44:38 +00:00
/*
* Put a system message into the chatbuffer
*/
void system_msg(pid_t pid, char *msg)
{
if (buffer_head < MAXMESSAGES)
buffer_head++;
else
buffer_head = 0;
Syslog('-', "system_msg(%d, %s) ptr=%d", pid, msg, buffer_head);
memset(&chat_messages[buffer_head], 0, sizeof(_chat_messages));
chat_messages[buffer_head].topid = pid;
sprintf(chat_messages[buffer_head].fromname, "Server");
strncpy(chat_messages[buffer_head].message, msg, 80);
chat_messages[buffer_head].posted = time(NULL);
}
2005-04-22 21:18:45 +00:00
/*
* Shout a message to all users
*/
void system_shout(const char *format, ...)
{
int i;
char buf[512];
va_list va_ptr;
va_start(va_ptr, format);
vsprintf(buf, format, va_ptr);
va_end(va_ptr);
for (i = 0; i < MAXCLIENT; i++)
if (chat_users[i].pid) {
system_msg(chat_users[i].pid, buf);
}
}
2003-03-24 19:44:38 +00:00
/*
* Show help
*/
void chat_help(pid_t pid)
{
2003-04-03 20:51:19 +00:00
system_msg(pid, (char *)" Help topics available:");
system_msg(pid, (char *)"");
system_msg(pid, (char *)" /BYE - Exit from chatserver");
system_msg(pid, (char *)" /ECHO <message> - Echo message to yourself");
2003-04-03 20:51:19 +00:00
system_msg(pid, (char *)" /EXIT - Exit from chatserver");
system_msg(pid, (char *)" /JOIN #channel - Join or create a channel");
system_msg(pid, (char *)" /J #channel - Join or create a channel");
// system_msg(pid, (char *)" /KICK <nick> - Kick nick out of the channel");
system_msg(pid, (char *)" /LIST - List active channels");
system_msg(pid, (char *)" /NAMES - List nicks in current channel");
2003-04-03 20:51:19 +00:00
system_msg(pid, (char *)" /NICK <name> - Set new nickname");
system_msg(pid, (char *)" /PART - Leave current channel");
system_msg(pid, (char *)" /QUIT - Exit from chatserver");
system_msg(pid, (char *)" /TOPIC <topic> - Set topic for current channel");
2003-03-24 19:44:38 +00:00
system_msg(pid, (char *)"");
2003-04-03 20:51:19 +00:00
system_msg(pid, (char *)" All other input (without a starting /) is sent to the channel.");
2003-03-24 19:44:38 +00:00
}
2003-04-01 21:41:36 +00:00
/*
* Join a channel
*/
2003-04-03 20:51:19 +00:00
int join(pid_t pid, char *channel, int sysop)
2003-04-01 21:41:36 +00:00
{
2005-04-23 14:27:52 +00:00
int j;
char buf[81];
#ifdef USE_EXPERIMENT
chn_list *tmp;
#else
int i;
#endif
2003-04-01 21:41:36 +00:00
Syslog('-', "Join pid %d to channel %s", pid, channel);
2005-04-23 14:27:52 +00:00
#ifdef USE_EXPERIMENT
if (channels) {
for (tmp = channels; tmp; tmp = tmp->next) {
if (strcmp(tmp->name, channel) == 0) {
tmp->users++;
for (j = 0; j < MAXCLIENT; j++) {
if (chat_users[j].pid == pid) {
strncpy(chat_users[j].channel, channel, 20);
chat_users[j].chatting = TRUE;
Syslog('-', "Added user %d to channel %s", j, channel);
chat_dump();
sprintf(buf, "%s has joined channel #%s, now %d users", chat_users[j].nick, channel, tmp->users);
chat_msg(channel, NULL, buf);
/*
* The sysop channel is private to the system, no broadcast
*/
if (strcasecmp(channel, "sysop"))
send_all("JOIN %s@%s %s\r\n", chat_users[j].nick, CFG.myfqdn, channel);
return TRUE;
}
}
}
}
}
#else
2003-04-01 21:41:36 +00:00
for (i = 0; i < MAXCHANNELS; i++) {
if (strcasecmp(chat_channels[i].name, channel) == 0) {
/*
2003-04-03 20:51:19 +00:00
* Existing channel, add user to channel.
2003-04-01 21:41:36 +00:00
*/
chat_channels[i].users++;
for (j = 0; j < MAXCLIENT; j++) {
if (chat_users[j].pid == pid) {
chat_users[j].channel = i;
chat_users[j].chatting = TRUE;
Syslog('-', "Added user %d to channel %d", j, i);
chat_dump();
2005-04-20 16:59:32 +00:00
sprintf(buf, "%s has joined channel #%s, now %d users", chat_users[j].nick, channel, chat_channels[i].users);
2003-04-01 21:41:36 +00:00
chat_msg(i, NULL, buf);
return TRUE;
}
}
}
}
2005-04-23 14:27:52 +00:00
#endif
2003-04-01 21:41:36 +00:00
2003-04-03 20:51:19 +00:00
/*
* A new channel must be created, but only the sysop may create the "sysop" channel
*/
if (!sysop && (strcasecmp(channel, "sysop") == 0)) {
sprintf(buf, "*** Only the sysop may create channel \"%s\"", channel);
system_msg(pid, buf);
return FALSE;
}
2003-04-01 21:41:36 +00:00
/*
* No matching channel found, add a new channel.
*/
2005-04-23 14:27:52 +00:00
#ifdef USE_EXPERIMENT
for (j = 0; j < MAXCLIENT; j++) {
if (chat_users[j].pid == pid) {
if (add_channel(&channels, channel, chat_users[j].nick, CFG.myfqdn) == 0) {
strncpy(chat_users[j].channel, channel, 20);
chat_users[j].chatting = TRUE;
Syslog('-', "Added user %d to channel %s", j, channel);
sprintf(buf, "Created channel #%s", channel);
chat_msg(channel, NULL, buf);
chat_dump();
send_all("JOIN %s@%s %s\r\n", chat_users[j].nick, CFG.myfqdn, channel);
return TRUE;
}
}
}
#else
2003-04-01 21:41:36 +00:00
for (i = 0; i < MAXCHANNELS; i++) {
if (chat_channels[i].active == FALSE) {
/*
* Got one, register channel.
*/
strncpy(chat_channels[i].name, channel, 20);
chat_channels[i].owner = pid;
chat_channels[i].users = 1;
chat_channels[i].created = time(NULL);
chat_channels[i].active = TRUE;
Syslog('-', "Created channel %d", i);
/*
* Register user to channel
*/
for (j = 0; j < MAXCLIENT; j++) {
if (chat_users[j].pid == pid) {
chat_users[j].channel = i;
chat_users[j].chatting = TRUE;
Syslog('-', "Added user %d to channel %d", j, i);
sprintf(buf, "Created channel #%s", channel);
chat_msg(i, NULL, buf);
}
}
chat_dump();
return TRUE;
}
}
2005-04-23 14:27:52 +00:00
#endif
2003-04-01 21:41:36 +00:00
/*
* No matching or free channels
*/
2003-04-03 20:51:19 +00:00
sprintf(buf, "*** Cannot create chat channel %s, no free channels", channel);
system_msg(pid, buf);
Syslog('+', "%s", buf);
2003-04-01 21:41:36 +00:00
return FALSE;
}
/*
* Part from a channel
*/
int part(pid_t pid, char *reason)
{
2005-04-23 14:27:52 +00:00
int i;
char buf[81];
#ifdef USE_EXPERIMENT
chn_list *tmp;
#endif
2003-04-01 21:41:36 +00:00
2005-04-24 12:14:39 +00:00
if (strlen(reason) > 54)
reason[54] = '\0';
2003-04-01 21:41:36 +00:00
Syslog('-', "Part pid %d from channel, reason %s", pid, reason);
2005-04-23 14:27:52 +00:00
#ifdef USE_EXPERIMENT
for (i = 0; i < MAXCLIENT; i++) {
if ((chat_users[i].pid == pid) && chat_users[i].chatting) {
for (tmp = channels; tmp; tmp = tmp->next) {
if (strcmp(tmp->name, chat_users[i].channel) == 0) {
tmp->users--;
chnchg = TRUE;
/*
* Inform other users
*/
if (reason != NULL)
chat_msg(chat_users[i].channel, chat_users[i].nick, reason);
sprintf(buf, "%s has left channel #%s, %d users left", chat_users[i].nick, tmp->name, tmp->users);
chat_msg(chat_users[i].channel, NULL, buf);
2005-04-24 12:14:39 +00:00
if (strcmp(tmp->name, (char *)"sysop")) {
if (reason && strlen(reason))
send_all("PART %s@%s %s %s\r\n", chat_users[i].nick, CFG.myfqdn, tmp->name, reason);
else
send_all("PART %s@%s %s\r\n", chat_users[i].nick, CFG.myfqdn, tmp->name);
}
2005-04-23 14:27:52 +00:00
/*
* Clean channel
*/
Syslog('-', "Nick %s leaves channel %s", chat_users[i].nick, tmp->name);
if (tmp->users == 0) {
/*
* Last user in channel, remove channel
*/
Syslog('-', "Remove channel %s, no more users left", tmp->name);
del_channel(&channels, tmp->name);
}
chat_users[i].channel[0] = '\0';
chat_users[i].chatting = FALSE;
chat_dump();
return TRUE;
}
}
}
}
#else
2003-04-01 21:41:36 +00:00
for (i = 0; i < MAXCLIENT; i++) {
if ((chat_users[i].pid == pid) && chat_users[i].chatting) {
chat_channels[chat_users[i].channel].users--;
/*
* Inform other users
*/
if (reason != NULL)
2005-04-20 16:59:32 +00:00
chat_msg(chat_users[i].channel, chat_users[i].nick, reason);
sprintf(buf, "%s has left channel #%s, %d users left", chat_users[i].nick, chat_channels[chat_users[i].channel].name,
2003-04-01 21:41:36 +00:00
chat_channels[chat_users[i].channel].users);
chat_msg(chat_users[i].channel, NULL, buf);
/*
* First clean channel
*/
Syslog('-', "User leaves channel %s", chat_channels[chat_users[i].channel].name);
if (chat_channels[chat_users[i].channel].users == 0) {
/*
* Last user from channel, clear channel
*/
Syslog('-', "Remove channel %s, no more users left", chat_channels[chat_users[i].channel].name);
memset(&chat_channels[chat_users[i].channel], 0, sizeof(_channel));
}
chat_users[i].channel = -1;
chat_users[i].chatting = FALSE;
chat_dump();
return TRUE;
}
}
2005-04-23 14:27:52 +00:00
#endif
2003-04-01 21:41:36 +00:00
Syslog('-', "No channel found");
return FALSE;
}
2003-03-24 19:44:38 +00:00
void chat_init(void)
{
2005-04-23 14:27:52 +00:00
#ifndef USE_EXPERIMENT
int i;
2005-04-23 14:27:52 +00:00
for (i = 0; i < MAXCLIENT; i++)
chat_users[i].channel = -1;
2003-04-01 21:41:36 +00:00
memset(&chat_channels, 0, sizeof(chat_channels));
2005-04-23 14:27:52 +00:00
#endif
memset(&chat_users, 0, sizeof(chat_users));
memset(&chat_messages, 0, sizeof(chat_messages));
2003-04-01 21:41:36 +00:00
}
void chat_cleanuser(pid_t pid)
{
part(pid, (char *)"I'm hanging up!");
}
/*
* Send message into channel
*/
2005-04-23 14:27:52 +00:00
#ifdef USE_EXPERIMENT
void chat_msg(char *channel, char *nick, char *msg)
#else
2003-04-01 21:41:36 +00:00
void chat_msg(int channel, char *nick, char *msg)
2005-04-23 14:27:52 +00:00
#endif
2003-04-01 21:41:36 +00:00
{
int i;
char buf[128], *logm;
2003-04-01 21:41:36 +00:00
if (nick == NULL)
sprintf(buf, "%s", msg);
else
sprintf(buf, "<%s> %s", nick, msg);
2003-04-07 20:18:16 +00:00
if (CFG.iAutoLog && strlen(CFG.chat_log)) {
logm = calloc(PATH_MAX, sizeof(char));
sprintf(logm, "%s/log/%s", getenv("MBSE_ROOT"), CFG.chat_log);
2005-04-23 14:27:52 +00:00
#ifdef USE_EXPERIMENT
ulog(logm, (char *)"+", channel, (char *)"-1", buf);
#else
ulog(logm, (char *)"+", chat_channels[channel].name, (char *)"-1", buf);
2005-04-23 14:27:52 +00:00
#endif
free(logm);
2003-04-07 20:18:16 +00:00
}
2003-04-01 21:41:36 +00:00
buf[79] = '\0';
for (i = 0; i < MAXCLIENT; i++) {
2005-04-23 14:27:52 +00:00
#ifdef USE_EXPERIMENT
if ((strcmp(chat_users[i].channel, channel) == 0) && chat_users[i].chatting) {
#else
2003-04-01 21:41:36 +00:00
if ((chat_users[i].channel == channel) && chat_users[i].chatting) {
2005-04-23 14:27:52 +00:00
#endif
2003-04-01 21:41:36 +00:00
system_msg(chat_users[i].pid, buf);
}
}
2005-04-15 21:36:44 +00:00
#ifdef USE_EXPERIMENT
2005-04-23 14:27:52 +00:00
send_all("MSG %s\r\n", buf);
2005-04-15 21:36:44 +00:00
#endif
2003-03-24 19:44:38 +00:00
}
/*
* Connect a session to the chatserver.
*/
char *chat_connect(char *data)
{
2005-04-20 16:59:32 +00:00
char *pid, *realname, *nick;
2003-03-24 19:44:38 +00:00
static char buf[200];
2005-04-20 16:59:32 +00:00
int i, count = 0, sys = FALSE;
2005-04-20 18:45:32 +00:00
#ifdef USE_EXPERIMENT
2005-04-20 16:59:32 +00:00
srv_list *sl;
2005-04-20 18:45:32 +00:00
#endif
2003-03-24 19:44:38 +00:00
Syslog('-', "CCON:%s", data);
memset(&buf, 0, sizeof(buf));
2003-04-02 21:36:47 +00:00
if (IsSema((char *)"upsalarm")) {
2003-04-03 20:51:19 +00:00
sprintf(buf, "100:1,*** Power failure, running on UPS;");
return buf;
}
if (s_bbsopen == FALSE) {
sprintf(buf, "100:1,*** The BBS is closed now;");
2003-04-02 21:36:47 +00:00
return buf;
}
2003-03-24 19:44:38 +00:00
/*
* Search free userslot
*/
for (i = 0; i < MAXCLIENT; i++) {
if (chat_users[i].pid == 0) {
/*
* Oke, found
*/
2003-04-02 21:36:47 +00:00
pid = strtok(data, ","); /* Should be 3 */
pid = strtok(NULL, ","); /* The pid */
2005-04-20 16:59:32 +00:00
realname = strtok(NULL, ","); /* Username */
nick = strtok(NULL, ","); /* Mickname */
2003-04-02 21:36:47 +00:00
sys = atoi(strtok(NULL, ";")); /* Sysop flag */
2003-03-24 19:44:38 +00:00
chat_users[i].pid = atoi(pid);
2005-04-20 16:59:32 +00:00
strncpy(chat_users[i].realname, realname, 36);
strncpy(chat_users[i].nick, nick, 9);
2005-04-22 20:01:21 +00:00
strncpy(chat_users[i].name, nick, 9);
2003-03-24 19:44:38 +00:00
chat_users[i].connected = time(NULL);
chat_users[i].pointer = buffer_head;
2005-04-23 14:27:52 +00:00
#ifdef USE_EXPERIMENT
chat_users[i].channel[0] = '\0';
#else
2003-03-24 19:44:38 +00:00
chat_users[i].channel = -1;
2005-04-23 14:27:52 +00:00
#endif
2003-04-02 21:36:47 +00:00
chat_users[i].sysop = sys;
2003-03-24 19:44:38 +00:00
2005-04-20 16:59:32 +00:00
Syslog('-', "Connected user %s (%s) with chatserver, slot %d, sysop %s", realname, pid, i, sys ? "True":"False");
2005-04-20 18:45:32 +00:00
#ifdef USE_EXPERIMENT
2005-04-20 16:59:32 +00:00
/*
* Register with IBC
*/
2005-04-20 20:27:32 +00:00
add_user(&users, CFG.myfqdn, nick, realname);
2005-04-21 21:12:50 +00:00
send_all("USER %s@%s %s\r\n", nick, CFG.myfqdn, realname);
2005-04-20 18:45:32 +00:00
#endif
2003-03-24 19:44:38 +00:00
/*
* Now put welcome message into the ringbuffer and report success.
*/
sprintf(buf, "MBSE BBS v%s chat server; type /help for help", VERSION);
system_msg(chat_users[i].pid, buf);
2005-04-20 16:59:32 +00:00
sprintf(buf, "Welcome to the Internet BBS Chat Network");
system_msg(chat_users[i].pid, buf);
2005-04-20 18:45:32 +00:00
#ifdef USE_EXPERIMENT
2005-04-20 16:59:32 +00:00
sprintf(buf, "Current connected servers:");
2003-03-24 19:44:38 +00:00
system_msg(chat_users[i].pid, buf);
2005-04-20 16:59:32 +00:00
for (sl = servers; sl; sl = sl->next) {
sprintf(buf, " %s (%d user%s)", sl->fullname, sl->users, (sl->users == 1) ? "":"s");
system_msg(chat_users[i].pid, buf);
count += sl->users;
}
2005-04-20 18:45:32 +00:00
#endif
2003-03-24 19:44:38 +00:00
sprintf(buf, "There %s %d user%s connected", (count != 1)?"are":"is", count, (count != 1)?"s":"");
system_msg(chat_users[i].pid, buf);
sprintf(buf, "100:0;");
return buf;
}
}
sprintf(buf, "100:1,Too many users connected;");
return buf;
}
char *chat_close(char *data)
{
static char buf[200];
char *pid;
int i;
Syslog('-', "CCLO:%s", data);
memset(&buf, 0, sizeof(buf));
pid = strtok(data, ",");
pid = strtok(NULL, ";");
2005-04-20 16:59:32 +00:00
2003-03-24 19:44:38 +00:00
for (i = 0; i < MAXCLIENT; i++) {
if (chat_users[i].pid == atoi(pid)) {
2005-04-20 16:59:32 +00:00
/*
* Remove from IBC network
*/
2005-04-20 18:45:32 +00:00
#ifdef USE_EXPERIMENT
2005-04-22 20:01:21 +00:00
del_user(&users, CFG.myfqdn, chat_users[i].name);
send_all("QUIT %s@%s Leaving chat\r\n", chat_users[i].name, CFG.myfqdn);
2005-04-20 18:45:32 +00:00
#endif
2003-03-24 19:44:38 +00:00
Syslog('-', "Closing chat for pid %s, slot %d", pid, i);
memset(&chat_users[i], 0, sizeof(_chat_users));
2005-04-23 14:27:52 +00:00
#ifdef USE_EXPERIMENT
chat_users[i].channel[0] = '\0';
#else
chat_users[i].channel = -1;
2005-04-23 14:27:52 +00:00
#endif
2003-03-24 19:44:38 +00:00
sprintf(buf, "100:0;");
return buf;
}
}
Syslog('-', "Pid %s was not connected to chatserver");
2003-04-03 20:51:19 +00:00
sprintf(buf, "100:1,*** ERROR - Not connected to server;");
2003-03-24 19:44:38 +00:00
return buf;
}
char *chat_put(char *data)
{
static char buf[200];
char *pid, *msg, *cmd;
int i, j, first, count;
2005-04-22 19:40:05 +00:00
#ifdef USE_EXPERIMENT
int found;
usr_list *tmp;
2005-04-23 14:27:52 +00:00
chn_list *tmpc;
2005-04-22 19:40:05 +00:00
#endif
2003-03-24 19:44:38 +00:00
Syslog('-', "CPUT:%s", data);
memset(&buf, 0, sizeof(buf));
2003-04-02 21:36:47 +00:00
if (IsSema((char *)"upsalarm")) {
2003-04-03 20:51:19 +00:00
sprintf(buf, "100:2,1,*** Power alarm, running on UPS;");
2003-04-02 21:36:47 +00:00
return buf;
}
2003-04-03 20:51:19 +00:00
if (s_bbsopen == FALSE) {
sprintf(buf, "100:2,1,*** The BBS is closed now;");
return buf;
}
2003-03-24 19:44:38 +00:00
pid = strtok(data, ",");
pid = strtok(NULL, ",");
msg = strtok(NULL, "\0");
msg[strlen(msg)-1] = '\0';
for (i = 0; i < MAXCLIENT; i++) {
if (chat_users[i].pid == atoi(pid)) {
if (msg[0] == '/') {
/*
* A command, process this
*/
if (strncasecmp(msg, "/help", 5) == 0) {
chat_help(atoi(pid));
2003-04-01 21:41:36 +00:00
goto ack;
} else if (strncasecmp(msg, "/echo", 5) == 0) {
sprintf(buf, "%s", msg);
system_msg(chat_users[i].pid, buf);
goto ack;
2003-04-03 20:51:19 +00:00
} else if ((strncasecmp(msg, "/exit", 5) == 0) ||
2003-04-01 21:41:36 +00:00
(strncasecmp(msg, "/quit", 5) == 0) ||
(strncasecmp(msg, "/bye", 4) == 0)) {
part(chat_users[i].pid, (char *)"Quitting");
2003-03-24 19:44:38 +00:00
sprintf(buf, "Goodbye");
system_msg(chat_users[i].pid, buf);
2003-04-02 21:36:47 +00:00
goto hangup;
2003-04-03 20:51:19 +00:00
} else if ((strncasecmp(msg, "/join", 5) == 0) ||
(strncasecmp(msg, "/j ", 3) == 0)) {
2003-03-24 19:44:38 +00:00
cmd = strtok(msg, " \0");
Syslog('-', "\"%s\"", cmd);
cmd = strtok(NULL, "\0");
Syslog('-', "\"%s\"", cmd);
if ((cmd == NULL) || (cmd[0] != '#') || (strcmp(cmd, "#") == 0)) {
sprintf(buf, "** Try /join #channel");
2003-03-24 19:44:38 +00:00
system_msg(chat_users[i].pid, buf);
2005-04-23 14:27:52 +00:00
#ifdef USE_EXPERIMENT
} else if (strlen(chat_users[i].channel)) {
#else
2003-04-03 20:51:19 +00:00
} else if (chat_users[i].channel != -1) {
2005-04-23 14:27:52 +00:00
#endif
sprintf(buf, "** Cannot join while in a channel");
2003-04-03 20:51:19 +00:00
system_msg(chat_users[i].pid, buf);
2003-03-24 19:44:38 +00:00
} else {
Syslog('-', "Trying to join channel %s", cmd);
2003-04-03 20:51:19 +00:00
join(chat_users[i].pid, cmd+1, chat_users[i].sysop);
2003-03-24 19:44:38 +00:00
}
2003-04-03 20:51:19 +00:00
chat_dump();
2003-04-01 21:41:36 +00:00
goto ack;
2003-04-03 20:51:19 +00:00
} else if (strncasecmp(msg, "/list", 5) == 0) {
first = TRUE;
2005-04-23 14:27:52 +00:00
#ifdef USE_EXPERIMENT
for (tmpc = channels; tmpc; tmpc = tmpc->next) {
if (first) {
sprintf(buf, "Cnt Channel name Channel topic");
system_msg(chat_users[i].pid, buf);
sprintf(buf, "--- -------------------- ------------------------------------------------------");
system_msg(chat_users[i].pid, buf);
}
first = FALSE;
sprintf(buf, "%3d %-20s %-54s", tmpc->users, tmpc->name, tmpc->topic);
system_msg(chat_users[i].pid, buf);
#else
2003-04-03 20:51:19 +00:00
for (j = 0; j < MAXCHANNELS; j++) {
if (chat_channels[j].owner && chat_channels[j].active) {
if (first) {
sprintf(buf, "Cnt Channel name Channel topic");
system_msg(chat_users[i].pid, buf);
sprintf(buf, "--- -------------------- ------------------------------------------------------");
system_msg(chat_users[i].pid, buf);
}
first = FALSE;
sprintf(buf, "%3d %-20s %-54s", chat_channels[j].users, chat_channels[j].name, chat_channels[j].topic);
system_msg(chat_users[i].pid, buf);
}
2005-04-23 14:27:52 +00:00
#endif
2003-04-03 20:51:19 +00:00
}
if (first) {
sprintf(buf, "No active channels to list");
system_msg(chat_users[i].pid, buf);
}
goto ack;
} else if (strncasecmp(msg, "/names", 6) == 0) {
2005-04-23 14:27:52 +00:00
#ifdef USE_EXPERIMENT
if (strlen(chat_users[i].channel)) {
#else
if (chat_users[i].channel != -1) {
2005-04-23 14:27:52 +00:00
#endif
sprintf(buf, "Present in this channel:");
system_msg(chat_users[i].pid, buf);
count = 0;
for (j = 0; j < MAXCLIENT; j++) {
2005-04-23 14:27:52 +00:00
#ifdef USE_EXPERIMENT
if ((strcmp(chat_users[j].channel, chat_users[i].channel) == 0) && chat_users[j].pid) {
#else
if ((chat_users[j].channel == chat_users[i].channel) && chat_users[j].pid) {
2005-04-23 14:27:52 +00:00
#endif
2005-04-20 16:59:32 +00:00
sprintf(buf, "%s %s", chat_users[j].nick,
chat_users[j].chanop ?"(chanop)": chat_users[j].sysop ?"(sysop)":"");
system_msg(chat_users[i].pid, buf);
count++;
}
}
sprintf(buf, "%d user%s in this channel", count, (count == 1) ?"":"s");
system_msg(chat_users[i].pid, buf);
} else {
sprintf(buf, "** Not in a channel");
system_msg(chat_users[i].pid, buf);
}
goto ack;
2003-04-03 20:51:19 +00:00
} else if (strncasecmp(msg, "/nick", 5) == 0) {
cmd = strtok(msg, " \0");
cmd = strtok(NULL, "\0");
2005-04-20 16:59:32 +00:00
if ((cmd == NULL) || (strlen(cmd) == 0) || (strlen(cmd) > 9)) {
sprintf(buf, "** Nickname must be between 1 and 9 characters");
2003-04-03 20:51:19 +00:00
} else {
2005-04-22 19:40:05 +00:00
#ifdef USE_EXPERIMENT
found = FALSE;
for (tmp = users; tmp; tmp = tmp->next) {
if ((strcmp(tmp->name, cmd) == 0) || (strcmp(tmp->nick, cmd) == 0)) {
found = TRUE;
}
}
if (!found ) {
for (tmp = users; tmp; tmp = tmp->next) {
if ((strcmp(tmp->server, CFG.myfqdn) == 0) && (strcmp(tmp->realname, chat_users[i].realname) == 0)) {
found = TRUE;
strncpy(tmp->nick, cmd, 9);
strncpy(chat_users[i].nick, cmd, 9);
sprintf(buf, "Nick set to \"%s\"", cmd);
system_msg(chat_users[i].pid, buf);
2005-04-22 20:01:21 +00:00
send_all("NICK %s %s %s %s\r\n", chat_users[i].nick, chat_users[i].name,
CFG.myfqdn, chat_users[i].realname);
2005-04-22 20:10:03 +00:00
usrchg = TRUE;
2005-04-22 19:40:05 +00:00
chat_dump();
goto ack;
}
}
}
2005-04-22 20:10:03 +00:00
sprintf(buf, "Can't set nick");
2005-04-22 19:40:05 +00:00
}
#else
2005-04-20 16:59:32 +00:00
strncpy(chat_users[i].nick, cmd, 9);
2003-04-03 20:51:19 +00:00
sprintf(buf, "Nick set to \"%s\"", cmd);
}
2005-04-22 19:40:05 +00:00
#endif
2003-04-03 20:51:19 +00:00
system_msg(chat_users[i].pid, buf);
chat_dump();
goto ack;
} else if (strncasecmp(msg, "/part", 5) == 0) {
2003-04-01 21:41:36 +00:00
cmd = strtok(msg, " \0");
Syslog('-', "\"%s\"", cmd);
cmd = strtok(NULL, "\0");
Syslog('-', "\"%s\"", cmd);
if (part(chat_users[i].pid, cmd) == FALSE) {
sprintf(buf, "** Not in a channel");
2003-04-01 21:41:36 +00:00
system_msg(chat_users[i].pid, buf);
}
2003-04-03 20:51:19 +00:00
chat_dump();
goto ack;
} else if (strncasecmp(msg, "/topic", 6) == 0) {
2005-04-23 14:27:52 +00:00
#ifdef USE_EXPERIMENT
if (strlen(chat_users[i].channel)) {
2005-04-23 15:35:39 +00:00
sprintf(buf, "** Internal system error");
2005-04-23 14:27:52 +00:00
for (tmpc = channels; tmpc; tmpc = tmpc->next) {
2005-04-23 15:56:22 +00:00
if (strcmp(chat_users[i].channel, tmpc->name) == 0) {
2005-04-23 14:27:52 +00:00
if ((strcmp(chat_users[i].name, tmpc->owner) == 0) || (strcmp(chat_users[i].nick, tmpc->owner) == 0)) {
cmd = strtok(msg, " \0");
cmd = strtok(NULL, "\0");
if ((cmd == NULL) || (strlen(cmd) == 0) || (strlen(cmd) > 54)) {
sprintf(buf, "** Topic must be between 1 and 54 characters");
} else {
strncpy(tmpc->topic, cmd, 54);
sprintf(buf, "Topic set to \"%s\"", cmd);
send_all("TOPIC %s %s\r\n", tmpc->name, tmpc->topic);
}
} else {
sprintf(buf, "** You are not the channel owner");
}
break;
2005-04-23 15:35:39 +00:00
} else {
Syslog('r', "channel %s is not what we want", tmpc->name);
2005-04-23 14:27:52 +00:00
}
}
#else
if (chat_users[i].channel != -1) {
if (chat_channels[chat_users[i].channel].owner == chat_users[i].pid) {
cmd = strtok(msg, " \0");
cmd = strtok(NULL, "\0");
2005-04-23 14:27:52 +00:00
if ((cmd == NULL) || (strlen(cmd) == 0) || (strlen(cmd) > 54)) {
sprintf(buf, "** Topic must be between 1 and 54 characters");
} else {
strncpy(chat_channels[chat_users[i].channel].topic, cmd, 54);
sprintf(buf, "Topic set to \"%s\"", cmd);
}
} else {
sprintf(buf, "** You are not the channel owner");
}
2005-04-23 14:27:52 +00:00
#endif
} else {
sprintf(buf, "** Not in a channel");
}
system_msg(chat_users[i].pid, buf);
chat_dump();
goto ack;
2003-04-03 20:51:19 +00:00
} else {
/*
* If still here, the command was not recognized.
*/
cmd = strtok(msg, " \t\r\n\0");
sprintf(buf, "*** \"%s\" :Unknown command", cmd+1);
system_msg(chat_users[i].pid, buf);
2003-04-01 21:41:36 +00:00
goto ack;
2003-03-24 19:44:38 +00:00
}
}
2005-04-23 14:27:52 +00:00
#ifdef USE_EXPERIMENT
if (strlen(chat_users[i].channel) == 0) {
#else
2003-03-24 19:44:38 +00:00
if (chat_users[i].channel == -1) {
2005-04-23 14:27:52 +00:00
#endif
2003-03-24 19:44:38 +00:00
/*
* Trying messages while not in a channel
*/
sprintf(buf, "** No channel joined. Try /join #channel");
2003-03-24 19:44:38 +00:00
system_msg(chat_users[i].pid, buf);
2003-04-01 21:41:36 +00:00
chat_dump();
goto ack;
} else {
2005-04-20 16:59:32 +00:00
chat_msg(chat_users[i].channel, chat_users[i].nick, msg);
2003-04-01 21:41:36 +00:00
chat_dump();
2003-03-24 19:44:38 +00:00
}
2003-04-01 21:41:36 +00:00
goto ack;
2003-03-24 19:44:38 +00:00
}
}
Syslog('-', "Pid %s was not connected to chatserver");
2003-04-03 20:51:19 +00:00
sprintf(buf, "100:2,1,*** ERROR - Not connected to server;");
2003-03-24 19:44:38 +00:00
return buf;
2003-04-01 21:41:36 +00:00
ack:
sprintf(buf, "100:0;");
return buf;
2003-04-02 21:36:47 +00:00
hangup:
sprintf(buf, "100:2,1,Disconnecting;");
return buf;
2003-03-24 19:44:38 +00:00
}
2003-04-03 20:51:19 +00:00
/*
* Check for a message for the user. Return the message or signal that
* nothing is there to display.
*/
2003-03-24 19:44:38 +00:00
char *chat_get(char *data)
{
static char buf[200];
char *pid;
int i;
2003-04-02 21:36:47 +00:00
if (IsSema((char *)"upsalarm")) {
2003-04-03 20:51:19 +00:00
sprintf(buf, "100:2,1,*** Power failure, running on UPS;");
return buf;
}
if (s_bbsopen == FALSE) {
sprintf(buf, "100:2,1,*** The BBS is closed now;");
2003-04-02 21:36:47 +00:00
return buf;
}
2003-03-24 19:44:38 +00:00
memset(&buf, 0, sizeof(buf));
pid = strtok(data, ",");
pid = strtok(NULL, ";");
for (i = 0; i < MAXCLIENT; i++) {
if (atoi(pid) == chat_users[i].pid) {
2003-04-03 20:51:19 +00:00
/*
* First check if we are a normal user in the sysop channel
*/
// if ((! chat_users[i].sysop) && (strcasecmp(channels[chat_users[i].channel].name, "sysop") == 0)) {
// }
2003-03-24 19:44:38 +00:00
while (chat_users[i].pointer != buffer_head) {
if (chat_users[i].pointer < MAXMESSAGES)
chat_users[i].pointer++;
else
chat_users[i].pointer = 0;
if (chat_users[i].pid == chat_messages[chat_users[i].pointer].topid) {
/*
* Message is for us.
*/
2003-04-02 21:36:47 +00:00
sprintf(buf, "100:2,0,%s;", chat_messages[chat_users[i].pointer].message);
2003-03-24 19:44:38 +00:00
Syslog('-', "%s", buf);
return buf;
}
}
sprintf(buf, "100:0;");
return buf;
}
}
2003-04-03 20:51:19 +00:00
sprintf(buf, "100:2,1,*** ERROR - Not connected to server;");
2003-04-02 21:36:47 +00:00
return buf;
}
/*
* Check for sysop present for forced chat
*/
char *chat_checksysop(char *data)
{
static char buf[20];
char *pid;
int i;
memset(&buf, 0, sizeof(buf));
pid = strtok(data, ",");
pid = strtok(NULL, ";");
if (reg_ispaging(pid)) {
Syslog('-', "Check sysopchat for pid %s, user has paged", pid);
/*
* Now check if sysop is present in the sysop channel
*/
for (i = 0; i < MAXCLIENT; i++) {
if (atoi(pid) != chat_users[i].pid) {
if (chat_users[i].chatting && chat_users[i].sysop) {
Syslog('-', "Sending ACK on check");
sprintf(buf, "100:1,1;");
reg_sysoptalk(pid);
return buf;
}
}
}
}
sprintf(buf, "100:1,0;");
2003-03-24 19:44:38 +00:00
return buf;
}