More additions for the chatserver

This commit is contained in:
Michiel Broek 2003-04-03 20:51:19 +00:00
parent de14e49f5c
commit f01d13146e
3 changed files with 175 additions and 51 deletions

View File

@ -424,6 +424,40 @@ void soft_info(void)
/*
* Colorize the chat window
*/
void Showline(int y, int x, char *msg)
{
int i;
if (strlen(msg)) {
if (msg[0] == '<') {
locate(y, x);
colour(LIGHTCYAN, BLACK);
putchar('<');
colour(LIGHTBLUE, BLACK);
for (i = 1; i < strlen(msg); i++) {
if (msg[i] == '>') {
colour(LIGHTCYAN, BLACK);
putchar(msg[i]);
colour(CYAN, BLACK);
} else {
putchar(msg[i]);
}
}
} else if (msg[0] == '*') {
colour(LIGHTRED, BLACK);
mvprintw(y, x, msg);
} else {
colour(GREEN, BLACK);
mvprintw(y, x, msg);
}
}
}
/*
* Display received chat message
*/
@ -433,7 +467,7 @@ void DispMsg(char *msg)
int i;
strncpy(rbuf[rpointer], msg, 80);
mvprintw(4+rpointer, 1, rbuf[rpointer]);
Showline(4+rpointer, 1, rbuf[rpointer]);
if (rpointer == rsize) {
/*
* Scroll buffer
@ -442,7 +476,7 @@ void DispMsg(char *msg)
locate(i+4,1);
clrtoeol();
sprintf(rbuf[i], "%s", rbuf[i+1]);
mvprintw(i+4, 1, rbuf[i]);
Showline(i+4, 1, rbuf[i]);
}
} else {
rpointer++;
@ -490,7 +524,7 @@ void Chat(int sysop)
clrtoeol();
mvprintw(lines - 2, 2, "Chat, type \"/EXIT\" to exit");
set_color(LIGHTGRAY, BLACK);
set_color(WHITE, BLACK);
mvprintw(lines - 1, 1, ">");
memset(&sbuf, 0, sizeof(sbuf));
memset(&rbuf, 0, sizeof(rbuf));
@ -516,7 +550,8 @@ void Chat(int sysop)
while (data) {
sprintf(buf, "CGET:1,%d;", mypid);
if (socket_send(buf) == 0) {
strcpy(buf, socket_receive());
memset(&buf, 0, sizeof(buf));
strncpy(buf, socket_receive(), sizeof(buf)-1);
if (strncmp(buf, "100:2,", 6) == 0) {
Syslog('-', "> CGET:1,%d;", mypid);
Syslog('-', "< %s", buf);
@ -524,6 +559,7 @@ void Chat(int sysop)
strncpy(resp, strtok(NULL, ","), 5); /* Should be 2 */
strncpy(resp, strtok(NULL, ","), 5); /* 1= fatal error */
rc = atoi(resp);
memset(&resp, 0, sizeof(resp));
strncpy(resp, strtok(NULL, "\0"), 80); /* The message */
resp[strlen(resp)-1] = '\0';
DispMsg(resp);
@ -550,9 +586,8 @@ void Chat(int sysop)
* Check for a pressed key, if so then process it
*/
ch = testkey(lines -1, curpos + 2);
if (ch == '@') {
break;
} else if (isprint(ch)) {
if (isprint(ch)) {
set_color(CYAN, BLACK);
if (curpos < 77) {
putchar(ch);
fflush(stdout);
@ -562,6 +597,7 @@ void Chat(int sysop)
putchar(7);
}
} else if ((ch == KEY_BACKSPACE) || (ch == KEY_RUBOUT) || (ch == KEY_DEL)) {
set_color(CYAN, BLACK);
if (curpos) {
curpos--;
sbuf[curpos] = '\0';
@ -593,6 +629,7 @@ void Chat(int sysop)
memset(&sbuf, 0, sizeof(sbuf));
locate(lines - 1, 2);
clrtoeol();
set_color(WHITE, BLACK);
mvprintw(lines - 1, 1, ">");
}
}
@ -604,7 +641,7 @@ void Chat(int sysop)
while (data) {
sprintf(buf, "CGET:1,%d;", mypid);
if (socket_send(buf) == 0) {
strcpy(buf, socket_receive());
strncpy(buf, socket_receive(), sizeof(buf)-1);
if (strncmp(buf, "100:2,", 6) == 0) {
Syslog('-', "> CGET:1,%d;", mypid);
Syslog('-', "< %s", buf);
@ -612,6 +649,7 @@ void Chat(int sysop)
strncpy(resp, strtok(NULL, ","), 5); /* Should be 2 */
strncpy(resp, strtok(NULL, ","), 5); /* 1= fatal error */
rc = atoi(resp);
memset(&resp, 0, sizeof(resp));
strncpy(resp, strtok(NULL, "\0"), 80); /* The message */
resp[strlen(resp)-1] = '\0';
DispMsg(resp);

View File

@ -104,7 +104,7 @@ void Showline(int y, int x, char *msg)
locate(y, x);
colour(LIGHTCYAN, BLACK);
putchar('<');
colour(BLUE, BLACK);
colour(LIGHTBLUE, BLACK);
for (i = 1; i < strlen(msg); i++) {
if (msg[i] == '>') {
colour(LIGHTCYAN, BLACK);
@ -114,8 +114,11 @@ void Showline(int y, int x, char *msg)
putchar(msg[i]);
}
}
} else if (msg[0] == '*') {
colour(LIGHTRED, BLACK);
mvprintw(y, x, msg);
} else {
colour(RED, BLACK);
colour(GREEN, BLACK);
mvprintw(y, x, msg);
}
}
@ -208,7 +211,7 @@ void Chat(char *username, char *channel)
sprintf(buf, "CCON,3,%d,%s,0;", mypid, exitinfo.Name);
Syslog('-', "> %s", buf);
if (socket_send(buf) == 0) {
strcpy(buf, socket_receive());
strncpy(buf, socket_receive(), sizeof(buf)-1);
Syslog('-', "< %s", buf);
if (strncmp(buf, "100:1,", 6) == 0) {
cnt = strtok(buf, ",");
@ -258,7 +261,7 @@ void Chat(char *username, char *channel)
while (data) {
sprintf(buf, "CGET:1,%d;", mypid);
if (socket_send(buf) == 0) {
strcpy(buf, socket_receive());
strncpy(buf, socket_receive(), sizeof(buf)-1);
if (strncmp(buf, "100:2,", 6) == 0) {
Syslog('-', "> CGET:1,%d;", mypid);
Syslog('-', "< %s", buf);
@ -266,6 +269,7 @@ void Chat(char *username, char *channel)
strncpy(resp, strtok(NULL, ","), 5); /* Should be 2 */
strncpy(resp, strtok(NULL, ","), 5); /* 1= fatal, chat ended */
rc = atoi(resp);
memset(&resp, 0, sizeof(resp));
strncpy(resp, strtok(NULL, "\0"), 80); /* The message */
resp[strlen(resp)-1] = '\0';
DispMsg(resp);
@ -334,7 +338,6 @@ void Chat(char *username, char *channel)
}
chatting = FALSE;
/*
* Before sending the close command, purge all outstanding messages.
*/
@ -342,7 +345,7 @@ void Chat(char *username, char *channel)
while (data) {
sprintf(buf, "CGET:1,%d;", mypid);
if (socket_send(buf) == 0) {
strcpy(buf, socket_receive());
strncpy(buf, socket_receive(), sizeof(buf)-1);
if (strncmp(buf, "100:2,", 6) == 0) {
Syslog('-', "> CGET:1,%d;", mypid);
Syslog('-', "< %s", buf);
@ -350,6 +353,7 @@ void Chat(char *username, char *channel)
strncpy(resp, strtok(NULL, ","), 5); /* Should be 2 */
strncpy(resp, strtok(NULL, ","), 5); /* 1= fatal error */
rc = atoi(resp);
memset(&resp, 0, sizeof(resp));
strncpy(resp, strtok(NULL, "\0"), 80); /* The message */
resp[strlen(resp)-1] = '\0';
DispMsg(resp);

View File

@ -79,6 +79,7 @@ typedef struct _chatmsg {
*/
typedef struct _channel_rec {
char name[21]; /* Channel name */
char topic[55]; /* Channel topic */
pid_t owner; /* Channel owner */
int users; /* Users in channel */
time_t created; /* Creation time */
@ -107,12 +108,19 @@ _channel chat_channels[MAXCHANNELS];
int buffer_head = 0; /* Messages buffer head */
extern struct sysconfig CFG; /* System configuration */
extern int s_bbsopen; /* The BBS open status */
/*
* Prototypes
*/
void chat_msg(int, char *, char *);
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*);
@ -150,7 +158,6 @@ void chat_dump(void)
/*
* Put a system message into the chatbuffer
*/
void system_msg(pid_t, char *);
void system_msg(pid_t pid, char *msg)
{
if (buffer_head < MAXMESSAGES)
@ -171,12 +178,24 @@ void system_msg(pid_t pid, char *msg)
/*
* Show help
*/
void chat_help(pid_t);
void chat_help(pid_t pid)
{
system_msg(pid, (char *)"Topics available:");
system_msg(pid, (char *)" Help topics available:");
system_msg(pid, (char *)"");
system_msg(pid, (char *)" EXIT HELP");
system_msg(pid, (char *)" /BYE - Exit from chatserver");
// system_msg(pid, (char *)" /ECHO <message> - Echo message to yourself");
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");
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");
system_msg(pid, (char *)"");
system_msg(pid, (char *)" All other input (without a starting /) is sent to the channel.");
}
@ -184,8 +203,7 @@ void chat_help(pid_t pid)
/*
* Join a channel
*/
int join(pid_t, char *);
int join(pid_t pid, char *channel)
int join(pid_t pid, char *channel, int sysop)
{
int i, j;
char buf[81];
@ -194,7 +212,7 @@ int join(pid_t pid, char *channel)
for (i = 0; i < MAXCHANNELS; i++) {
if (strcasecmp(chat_channels[i].name, channel) == 0) {
/*
* Excisting channel, add user to channel.
* Existing channel, add user to channel.
*/
chat_channels[i].users++;
for (j = 0; j < MAXCLIENT; j++) {
@ -211,6 +229,15 @@ int join(pid_t pid, char *channel)
}
}
/*
* 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;
}
/*
* No matching channel found, add a new channel.
*/
@ -245,7 +272,9 @@ int join(pid_t pid, char *channel)
/*
* No matching or free channels
*/
Syslog('+', "Cannot create chat channel %s, no free channels", channel);
sprintf(buf, "*** Cannot create chat channel %s, no free channels", channel);
system_msg(pid, buf);
Syslog('+', "%s", buf);
return FALSE;
}
@ -254,7 +283,6 @@ int join(pid_t pid, char *channel)
/*
* Part from a channel
*/
int part(pid_t, char*);
int part(pid_t pid, char *reason)
{
int i;
@ -350,7 +378,12 @@ char *chat_connect(char *data)
memset(&buf, 0, sizeof(buf));
if (IsSema((char *)"upsalarm")) {
sprintf(buf, "100:1,Power failure, running on UPS;");
sprintf(buf, "100:1,*** Power failure, running on UPS;");
return buf;
}
if (s_bbsopen == FALSE) {
sprintf(buf, "100:1,*** The BBS is closed now;");
return buf;
}
@ -418,7 +451,7 @@ char *chat_close(char *data)
}
}
Syslog('-', "Pid %s was not connected to chatserver");
sprintf(buf, "100:1,ERROR - Not connected to server;");
sprintf(buf, "100:1,*** ERROR - Not connected to server;");
return buf;
}
@ -428,13 +461,18 @@ char *chat_put(char *data)
{
static char buf[200];
char *pid, *msg, *cmd;
int i;
int i, j, first;
Syslog('-', "CPUT:%s", data);
memset(&buf, 0, sizeof(buf));
if (IsSema((char *)"upsalarm")) {
sprintf(buf, "100:2,1,Power alarm, running on UPS;");
sprintf(buf, "100:2,1,*** Power alarm, running on UPS;");
return buf;
}
if (s_bbsopen == FALSE) {
sprintf(buf, "100:2,1,*** The BBS is closed now;");
return buf;
}
@ -445,11 +483,6 @@ char *chat_put(char *data)
for (i = 0; i < MAXCLIENT; i++) {
if (chat_users[i].pid == atoi(pid)) {
/*
* We are connected and known, first send the input back to ourself.
*/
// system_msg(chat_users[i].pid, msg);
if (msg[0] == '/') {
/*
* A command, process this
@ -457,16 +490,15 @@ char *chat_put(char *data)
if (strncasecmp(msg, "/help", 5) == 0) {
chat_help(atoi(pid));
goto ack;
}
if ((strncasecmp(msg, "/exit", 5) == 0) ||
} else if ((strncasecmp(msg, "/exit", 5) == 0) ||
(strncasecmp(msg, "/quit", 5) == 0) ||
(strncasecmp(msg, "/bye", 4) == 0)) {
part(chat_users[i].pid, (char *)"Quitting");
sprintf(buf, "Goodbye");
system_msg(chat_users[i].pid, buf);
goto hangup;
}
if (strncasecmp(msg, "/join", 5) == 0) {
} else if ((strncasecmp(msg, "/join", 5) == 0) ||
(strncasecmp(msg, "/j ", 3) == 0)) {
cmd = strtok(msg, " \0");
Syslog('-', "\"%s\"", cmd);
cmd = strtok(NULL, "\0");
@ -474,13 +506,48 @@ char *chat_put(char *data)
if ((cmd == NULL) || (cmd[0] != '#') || (strcmp(cmd, "#") == 0)) {
sprintf(buf, "Try /join #channel");
system_msg(chat_users[i].pid, buf);
} else if (chat_users[i].channel != -1) {
sprintf(buf, "Cannot join while in a channel");
system_msg(chat_users[i].pid, buf);
} else {
Syslog('-', "Trying to join channel %s", cmd);
join(chat_users[i].pid, cmd+1);
join(chat_users[i].pid, cmd+1, chat_users[i].sysop);
}
chat_dump();
goto ack;
} else if (strncasecmp(msg, "/list", 5) == 0) {
first = TRUE;
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);
}
}
if (first) {
sprintf(buf, "No active channels to list");
system_msg(chat_users[i].pid, buf);
}
goto ack;
} else if (strncasecmp(msg, "/nick", 5) == 0) {
cmd = strtok(msg, " \0");
cmd = strtok(NULL, "\0");
if ((cmd == NULL) || (strlen(cmd) == 0) || (strlen(cmd) > 36)) {
sprintf(buf, "Nickname must be between 1 and 36 characters");
} else {
strncpy(chat_users[i].name, cmd, 36);
sprintf(buf, "Nick set to \"%s\"", cmd);
}
if (strncasecmp(msg, "/part", 5) == 0) {
system_msg(chat_users[i].pid, buf);
chat_dump();
goto ack;
} else if (strncasecmp(msg, "/part", 5) == 0) {
cmd = strtok(msg, " \0");
Syslog('-', "\"%s\"", cmd);
cmd = strtok(NULL, "\0");
@ -489,16 +556,18 @@ char *chat_put(char *data)
sprintf(buf, "Not in a channel");
system_msg(chat_users[i].pid, buf);
}
chat_dump();
goto ack;
}
} else {
/*
* If still here, the command was not recognized.
*/
cmd = strtok(msg, " \t\r\n\0");
sprintf(buf, "%s :Unknown command", cmd+1);
sprintf(buf, "*** \"%s\" :Unknown command", cmd+1);
system_msg(chat_users[i].pid, buf);
goto ack;
}
}
if (chat_users[i].channel == -1) {
/*
* Trying messages while not in a channel
@ -515,7 +584,7 @@ char *chat_put(char *data)
}
}
Syslog('-', "Pid %s was not connected to chatserver");
sprintf(buf, "100:2,1,ERROR - Not connected to server;");
sprintf(buf, "100:2,1,*** ERROR - Not connected to server;");
return buf;
ack:
@ -529,16 +598,23 @@ hangup:
/*
* Check for a message for the user. Return the message or signal that
* nothing is there to display.
*/
char *chat_get(char *data)
{
static char buf[200];
char *pid;
int i;
// Syslog('-', "CGET:%s", data);
if (IsSema((char *)"upsalarm")) {
sprintf(buf, "100:2,1,Power failure, running on UPS;");
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;");
return buf;
}
@ -548,6 +624,12 @@ char *chat_get(char *data)
for (i = 0; i < MAXCLIENT; i++) {
if (atoi(pid) == chat_users[i].pid) {
/*
* 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)) {
// }
while (chat_users[i].pointer != buffer_head) {
if (chat_users[i].pointer < MAXMESSAGES)
chat_users[i].pointer++;
@ -566,7 +648,7 @@ char *chat_get(char *data)
return buf;
}
}
sprintf(buf, "100:2,1,ERROR - Not connected to server;");
sprintf(buf, "100:2,1,*** ERROR - Not connected to server;");
return buf;
}