added experimental chat server

This commit is contained in:
Andrew Pamment 2017-03-20 11:37:56 +10:00
parent ac729c1f69
commit 7bb688b32f
14 changed files with 459 additions and 127 deletions

View File

@ -5,8 +5,9 @@ JAMLIB = deps/jamlib/jamlib.a
ZMODEM = deps/Xmodem/libzmodem.a
LUA = deps/lua/liblua.a
ODOORS = deps/odoors/libs-`uname -s`/libODoors.a
JSMN = deps/jsmn/libjsmn.a
all: magicka magimail magiedit ticproc mgpost
all: magicka magimail magiedit ticproc mgpost magichat
${LUA}:
cd deps/lua && $(MAKE) -f Makefile freebsd MAKEFLAGS=
@ -20,12 +21,15 @@ ${ZMODEM}:
${ODOORS}:
cd deps/odoors/ && $(MAKE)
$(JSMN):
cd deps/jsmn/ && $(MAKE)
OBJ = inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o doors.o bbs_list.o chat_system.o email.o files.o settings.o lua_glue.o strings.o bluewave.o hashmap/hashmap.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
magicka: $(OBJ) ${LUA} ${JAMLIB} ${ZMODEM}
$(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 $(JAMLIB) $(ZMODEM) $(LUA) -lutil -lm -lssl -lcrypto -lssh
magicka: $(OBJ) ${LUA} ${JAMLIB} ${ZMODEM} ${JSMN}
$(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 -lutil -lm -lssl -lcrypto -lssh
magimail: $(JAMLIB)
cd utils/magimail && $(MAKE) freebsd
@ -36,6 +40,9 @@ magiedit: $(ODOORS)
ticproc:
cd utils/ticproc && $(MAKE)
magichat: $(JSMN)
cd utils/magichat && $(MAKE)
mgpost: $(JAMLIB)
cd utils/mgpost && $(MAKE)
@ -51,3 +58,4 @@ clean:
cd deps/odoors/ && rm -rf libs-`uname -s` objs-`uname -s` exe-`uname -s`
cd utils/ticproc && $(MAKE) clean
cd utils/mgpost && $(MAKE) clean
cd utils/magichat && $(MAKE) clean

View File

@ -7,8 +7,9 @@ LUA = deps/lua/liblua.a
B64 = deps/libb64-1.2/src/libb64.a
MICROHTTPD=-lmicrohttpd
ODOORS = deps/odoors/libs-`uname -s`/libODoors.a
JSMN = deps/jsmn/libjsmn.a
all: magicka magimail magiedit ticproc mgpost
all: magicka magimail magiedit ticproc mgpost magichat
${LUA}:
cd deps/lua && $(MAKE) -f Makefile freebsd MAKEFLAGS=
@ -25,12 +26,15 @@ ${B64}:
${ODOORS}:
cd deps/odoors/ && $(MAKE)
$(JSMN):
cd deps/jsmn/ && $(MAKE)
OBJ = inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o doors.o bbs_list.o chat_system.o email.o files.o settings.o lua_glue.o strings.o bluewave.o www.o www_email.o www_msgs.o www_last10.o hashmap/hashmap.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
magicka: $(OBJ) ${LUA} ${ZMODEM} ${JAMLIB} ${B64}
$(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 $(JAMLIB) $(ZMODEM) $(LUA) $(B64) -lutil -lm -lssl -lcrypto -lssh $(MICROHTTPD)
magicka: $(OBJ) ${LUA} ${ZMODEM} ${JAMLIB} ${B64} ${JSMN}
$(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 -lutil -lm -lssl -lcrypto -lssh $(MICROHTTPD)
magimail: $(JAMLIB)
cd utils/magimail && $(MAKE) freebsd
@ -44,6 +48,9 @@ ticproc:
mgpost: $(JAMLIB)
cd utils/mgpost && $(MAKE)
magichat: $(JSMN)
cd utils/magichat && $(MAKE)
.PHONY: clean
clean:
@ -57,3 +64,4 @@ clean:
cd deps/odoors/ && rm -rf libs-`uname -s` objs-`uname -s` exe-`uname -s`
cd utils/ticproc && $(MAKE) clean
cd utils/mgpost && $(MAKE) clean
cd utils/magichat && $(MAKE) clean

View File

@ -5,8 +5,9 @@ JAMLIB = deps/jamlib/jamlib.a
ZMODEM = deps/Xmodem/libzmodem.a
LUA = deps/lua/liblua.a
ODOORS = deps/odoors/libs-`uname -s`/libODoors.a
JSMN = deps/jsmn/libjsmn.a
all: magicka magimail magiedit ticproc mgpost
all: magicka magimail magiedit ticproc mgpost magichat
${LUA}:
cd deps/lua && $(MAKE) -f Makefile linux MAKEFLAGS=
@ -20,12 +21,15 @@ ${ZMODEM}:
${ODOORS}:
cd deps/odoors/ && $(MAKE)
$(JSMN):
cd deps/jsmn/ && $(MAKE)
OBJ = inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o doors.o bbs_list.o chat_system.o email.o files.o settings.o lua_glue.o strings.o bluewave.o hashmap/hashmap.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
magicka: $(OBJ) ${LUA} ${ZMODEM} ${JAMLIB}
$(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 $(JAMLIB) $(ZMODEM) $(LUA) -lutil -lm -ldl -lssl -lcrypto -lssh
magicka: $(OBJ) ${LUA} ${ZMODEM} ${JAMLIB} ${JSMN}
$(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 -lutil -lm -ldl -lssl -lcrypto -lssh
magimail: $(JAMLIB)
cd utils/magimail && $(MAKE) linux
@ -39,6 +43,9 @@ ticproc:
mgpost: $(JAMLIB)
cd utils/mgpost && $(MAKE)
magichat: $(JSMN)
cd utils/magichat && $(MAKE)
.PHONY: clean
clean:
@ -51,3 +58,4 @@ clean:
cd deps/odoors/ && rm -rf libs-`uname -s` objs-`uname -s` exe-`uname -s`
cd utils/ticproc && $(MAKE) clean
cd utils/mgpost && $(MAKE) clean
cd utils/magichat && $(MAKE) clean

View File

@ -7,8 +7,9 @@ LUA = deps/lua/liblua.a
B64 = deps/libb64-1.2/src/libb64.a
MICROHTTPD=-lmicrohttpd
ODOORS = deps/odoors/libs-`uname -s`/libODoors.a
JSMN = deps/jsmn/libjsmn.a
all: magicka magimail magiedit ticproc mgpost
all: magicka magimail magiedit ticproc mgpost magichat
${LUA}:
cd deps/lua && $(MAKE) -f Makefile linux MAKEFLAGS=
@ -25,12 +26,15 @@ ${B64}:
${ODOORS}:
cd deps/odoors/ && $(MAKE)
$(JSMN):
cd deps/jsmn/ && $(MAKE)
OBJ = inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o doors.o bbs_list.o chat_system.o email.o files.o settings.o lua_glue.o strings.o www.o www_email.o www_msgs.o www_last10.o bluewave.o hashmap/hashmap.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
magicka: $(OBJ) ${LUA} ${JAMLIB} ${ZMODEM} ${B64}
$(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 $(JAMLIB) $(ZMODEM) $(LUA) $(B64) -lutil -lm -ldl -lssl -lcrypto -lssh $(MICROHTTPD)
magicka: $(OBJ) ${LUA} ${JAMLIB} ${ZMODEM} ${B64} ${JSMN}
$(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 -lutil -lm -ldl -lssl -lcrypto -lssh $(MICROHTTPD)
magimail: $(JAMLIB)
cd utils/magimail && $(MAKE) linux
@ -44,6 +48,9 @@ ticproc:
mgpost: $(JAMLIB)
cd utils/mgpost && $(MAKE)
magichat: $(JSMN)
cd utils/magichat && $(MAKE)
.PHONY: clean
clean:
@ -57,3 +64,4 @@ clean:
cd deps/odoors/ && rm -rf libs-`uname -s` objs-`uname -s` exe-`uname -s`
cd utils/ticproc && $(MAKE) clean
cd utils/mgpost && $(MAKE) clean
cd utils/magichat && $(MAKE) clean

View File

@ -5,8 +5,9 @@ JAMLIB = deps/jamlib/jamlib.a
ZMODEM = deps/Xmodem/libzmodem.a
LUA = deps/lua/liblua.a
ODOORS = deps/odoors/libs-`uname -s`/libODoors.a
JSMN = deps/jsmn/libjsmn.a
all: magicka magimail magiedit ticproc mgpost
all: magicka magimail magiedit ticproc mgpost magichat
${LUA}:
cd deps/lua && $(MAKE) -f Makefile bsd MAKEFLAGS=
@ -20,12 +21,15 @@ ${ZMODEM}:
${ODOORS}:
cd deps/odoors/ && $(MAKE)
$(JSMN):
cd deps/jsmn/ && $(MAKE)
OBJ = inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o doors.o bbs_list.o chat_system.o email.o files.o settings.o lua_glue.o strings.o bluewave.o hashmap/hashmap.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
magicka: $(OBJ) ${LUA} ${ZMODEM} ${JAMLIB}
$(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/pkg/lib -lsqlite3 $(JAMLIB) $(ZMODEM) $(LUA) -lutil -lm -lssl -lcrypto -lssh
magicka: $(OBJ) ${LUA} ${ZMODEM} ${JAMLIB} ${JSMN}
$(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/pkg/lib -lsqlite3 -lutil -lm -lssl -lcrypto -lssh
magiedit: $(ODOORS)
cd utils/magiedit && $(MAKE)
@ -39,6 +43,9 @@ ticproc:
mgpost: $(JAMLIB)
cd utils/mgpost && $(MAKE)
magichat: $(JSMN)
cd utils/magichat && $(MAKE)
.PHONY: clean
clean:
@ -51,3 +58,4 @@ clean:
cd deps/odoors/ && rm -rf libs-`uname -s` objs-`uname -s` exe-`uname -s`
cd utils/ticproc && $(MAKE) clean
cd utils/mgpost && $(MAKE) clean
cd utils/magichat && $(MAKE) clean

View File

@ -7,8 +7,9 @@ LUA = deps/lua/liblua.a
B64 = deps/libb64-1.2/src/libb64.a
MICROHTTPD=-lmicrohttpd
ODOORS = deps/odoors/libs-`uname -s`/libODoors.a
JSMN = deps/jsmn/libjsmn.a
all: magicka magimail magiedit ticproc mgpost
all: magicka magimail magiedit ticproc mgpost magichat
${LUA}:
cd deps/lua && $(MAKE) -f Makefile bsd MAKEFLAGS=
@ -25,12 +26,15 @@ ${B64}:
${ODOORS}:
cd deps/odoors/ && $(MAKE)
$(JSMN):
cd deps/jsmn/ && $(MAKE)
OBJ = inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o doors.o bbs_list.o chat_system.o email.o files.o settings.o lua_glue.o strings.o bluewave.o www.o www_email.o www_msgs.o www_last10.o hashmap/hashmap.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
magicka: $(OBJ) ${LUA} ${ZMODEM} ${JAMLIB} ${B64}
$(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/pkg/lib -lsqlite3 $(JAMLIB) $(ZMODEM) $(LUA) $(B64) -lutil -lm -lssl -lcrypto -lssh $(MICROHTTPD)
magicka: $(OBJ) ${LUA} ${ZMODEM} ${JAMLIB} ${B64} ${JSMN}
$(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/pkg/lib -lsqlite3 -lutil -lm -lssl -lcrypto -lssh $(MICROHTTPD)
magiedit: $(ODOORS)
cd utils/magiedit && $(MAKE)
@ -44,6 +48,9 @@ ticproc:
mgpost: $(JAMLIB)
cd utils/mgpost && $(MAKE)
magichat: $(JSMN)
cd utils/magichat && $(MAKE)
.PHONY: clean
clean:
@ -57,3 +64,4 @@ clean:
cd deps/odoors/ && rm -rf libs-`uname -s` objs-`uname -s` exe-`uname -s`
cd utils/ticproc && $(MAKE) clean
cd utils/mgpost && $(MAKE) clean
cd utils/magichat && $(MAKE) clean

View File

@ -5,10 +5,11 @@ JAMLIB = deps/jamlib/jamlib.a
ZMODEM = deps/Xmodem/libzmodem.a
LUA = deps/lua/liblua.a
ODOORS = deps/odoors/libs-`uname -s`/libODoors.a
JSMN = deps/jsmn/libjsmn.a
OBJ = inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o doors.o bbs_list.o chat_system.o email.o files.o settings.o lua_glue.o strings.o bluewave.o hashmap/hashmap.o
all: magicka magimail magiedit ticproc mgpost
all: magicka magimail magiedit ticproc mgpost magichat
${LUA}:
cd deps/lua && $(MAKE) -f Makefile macosx MAKEFLAGS=
@ -22,11 +23,14 @@ ${ZMODEM}:
${ODOORS}:
cd deps/odoors/ && $(MAKE)
$(JSMN):
cd deps/jsmn/ && $(MAKE)
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
magicka: $(OBJ) ${LUA} ${ZMODEM} ${JAMLIB}
$(CC) -o magicka -o $@ $^ $(CFLAGS) -L/opt/local/lib -lsqlite3 $(JAMLIB) $(ZMODEM) $(LUA) -lutil -lm -ldl -lssl -lcrypto -lssh
magicka: $(OBJ) ${LUA} ${ZMODEM} ${JAMLIB} ${JSMN}
$(CC) -o magicka -o $@ $^ $(CFLAGS) -L/opt/local/lib -lsqlite3 -lutil -lm -ldl -lssl -lcrypto -lssh
magimail: $(JAMLIB)
cd utils/magimail && $(MAKE) linux
@ -40,6 +44,9 @@ ticproc:
mgpost: $(JAMLIB)
cd utils/mgpost && $(MAKE)
magichat: $(JSMN)
cd utils/magichat && $(MAKE)
.PHONY: clean
clean:
@ -52,3 +59,4 @@ clean:
cd deps/odoors/ && rm -rf libs-`uname -s` objs-`uname -s` exe-`uname -s`
cd utils/ticproc && $(MAKE) clean
cd utils/mgpost && $(MAKE) clean
cd utils/magichat && $(MAKE) clean

View File

@ -1,5 +1,5 @@
CC=cc
CFLAGS=-I/opt/local/include -I./deps/ -I./deps/libb64-1.2/include -DENABLE_WWW=1
CFLAGS=-I/opt/local/include -I./deps/ -I./deps/libb64-1.2/include -DENABLE_WWW=1 -Wall
DEPS = bbs.h
JAMLIB = deps/jamlib/jamlib.a
ZMODEM = deps/Xmodem/libzmodem.a
@ -7,8 +7,9 @@ LUA = deps/lua/liblua.a
B64 = deps/libb64-1.2/src/libb64.a
MICROHTTPD=-lmicrohttpd
ODOORS = deps/odoors/libs-`uname -s`/libODoors.a
JSMN = deps/jsmn/libjsmn.a
all: magicka magimail magiedit ticproc mgpost
all: magicka magimail magiedit ticproc mgpost magichat
${LUA}:
cd deps/lua && $(MAKE) -f Makefile macosx MAKEFLAGS=
@ -25,12 +26,15 @@ ${B64}:
${ODOORS}:
cd deps/odoors/ && $(MAKE)
$(JSMN):
cd deps/jsmn/ && $(MAKE)
OBJ = inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o doors.o bbs_list.o chat_system.o email.o files.o settings.o lua_glue.o strings.o www.o www_email.o www_msgs.o www_last10.o bluewave.o hashmap/hashmap.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
magicka: $(OBJ) ${LUA} ${ZMODEM} ${JAMLIB} ${B64}
$(CC) -o magicka -o $@ $^ $(CFLAGS) -L/opt/local/lib -lsqlite3 $(JAMLIB) $(ZMODEM) $(LUA) $(B64) -lutil -lm -ldl -lssl -lcrypto -lssh $(MICROHTTPD)
magicka: $(OBJ) ${LUA} ${ZMODEM} ${JAMLIB} ${B64} ${JSMN}
$(CC) -o magicka -o $@ $^ $(CFLAGS) -L/opt/local/lib -lsqlite3 -lutil -lm -ldl -lssl -lcrypto -lssh $(MICROHTTPD)
magimail: $(JAMLIB)
cd utils/magimail && $(MAKE) linux
@ -44,6 +48,9 @@ ticproc:
mgpost: $(JAMLIB)
cd utils/mgpost && $(MAKE)
magichat: $(JSMN)
cd utils/magichat && $(MAKE)
.PHONY: clean
clean:
@ -56,3 +63,4 @@ clean:
cd deps/odoors/ && rm -rf libs-`uname -s` objs-`uname -s` exe-`uname -s`
cd utils/ticproc && $(MAKE) clean
cd utils/mgpost && $(MAKE) clean
cd utils/magichat && $(MAKE) clean

6
bbs.h
View File

@ -134,9 +134,9 @@ struct bbs_config {
char *ssh_dsa_key;
char *ssh_rsa_key;
char *string_file;
char *irc_server;
int irc_port;
char *irc_channel;
char *mgchat_server;
int mgchat_port;
char *mgchat_bbstag;
int bwave_max_msgs;
struct fido_addr *main_aka;

View File

@ -7,6 +7,7 @@
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "deps/jsmn/jsmn.h"
#include "bbs.h"
extern struct bbs_config conf;
@ -21,6 +22,34 @@ static int row_at;
static char sbuf[512];
extern struct user_record gUser;
struct chat_msg {
char nick[16];
char bbstag[16];
char msg[256];
};
static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
return 0;
}
return -1;
}
static char *encapsulate_quote(char *in) {
char out[160];
int i;
int j = 0;
for (i=0;i<strlen(in);i++) {
if (in[j] == '\"') {
out[i++] = '\\';
}
out[i] = in[j];
j++;
}
return strdup(out);
}
void scroll_up() {
int y;
@ -101,17 +130,17 @@ void chat_system(struct user_record *user) {
char buffer2[256];
char buffer[513];
char outputbuffer[513];
char readbuffer[1024];
int buffer_at = 0;
int do_update = 1;
int i;
int j;
char *usr;
char *cmd;
char *where;
char *message;
char *sep;
char *target;
int chat_in;
jsmn_parser parser;
jsmntok_t tokens[6];
int r;
struct chat_msg msg;
char *input_b;
if (sshBBS) {
chat_in = STDIN_FILENO;
@ -120,24 +149,27 @@ void chat_system(struct user_record *user) {
}
memset(inputbuffer, 0, 80);
if (conf.irc_server == NULL) {
if (conf.mgchat_server == NULL) {
s_putstring(get_string(49));
return;
}
jsmn_init(&parser);
row_at = 0;
line_at = 0;
s_putstring("\e[2J");
s_putstring("\e[2J\e[1;1H");
memset(&servaddr, 0, sizeof(struct sockaddr_in));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(conf.irc_port);
servaddr.sin_port = htons(conf.mgchat_port);
if ( (chat_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
return;
}
if (inet_pton(AF_INET, conf.irc_server, &servaddr.sin_addr) != 0) {
hostname_to_ip(conf.irc_server, buffer);
if (inet_pton(AF_INET, conf.mgchat_server, &servaddr.sin_addr) != 0) {
hostname_to_ip(conf.mgchat_server, buffer);
if (!inet_pton(AF_INET, buffer, &servaddr.sin_addr)) {
return;
}
@ -146,10 +178,6 @@ void chat_system(struct user_record *user) {
return;
}
raw("USER %s 0 0 :%s\r\n", user->loginname, user->loginname);
raw("NICK %s\r\n", user->loginname);
raw("JOIN %s\r\n", conf.irc_channel);
memset(buffer, 0, 513);
screenbuffer = (char **)malloc(sizeof(char *) * 23);
@ -175,14 +203,14 @@ void chat_system(struct user_record *user) {
if (FD_ISSET(chat_in, &fds)) {
len = read(chat_in, &c, 1);
if (len == 0) {
raw("QUIT\r\n");
close(chat_socket);
disconnect("Socket closed");
}
if (c == '\r') {
if (inputbuffer[0] == '/') {
if (strcasecmp(&inputbuffer[1], "quit") == 0) {
raw("QUIT\r\n");
close(chat_socket);
for (i=0;i<22;i++) {
free(screenbuffer[i]);
}
@ -190,7 +218,9 @@ void chat_system(struct user_record *user) {
return;
}
} else {
raw("PRIVMSG %s :%s\r\n", conf.irc_channel, inputbuffer);
input_b = encapsulate_quote(inputbuffer);
raw("{ \"bbstag\": \"%s\", \"nick\": \"%s\", \"msg\": \"%s\" }", conf.mgchat_bbstag, user->loginname, input_b);
free(input_b);
sprintf(buffer2, "%s: %s", user->loginname, inputbuffer);
append_screenbuffer(buffer2);
do_update = 1;
@ -211,7 +241,7 @@ void chat_system(struct user_record *user) {
}
}
if (FD_ISSET(chat_socket, &fds)) {
len = read(chat_socket, &c, 1);
len = read(chat_socket, readbuffer, 1024);
if (len == 0) {
s_putstring("\r\n\r\n\r\nLost connection to chat server!\r\n");
for (i=0;i<22;i++) {
@ -221,57 +251,41 @@ void chat_system(struct user_record *user) {
return;
}
if (c == '\r' || buffer_at == 512) {
if (!strncmp(buffer, "PING", 4)) {
buffer[1] = 'O';
raw(buffer);
} else if (buffer[0] == ':') {
usr = cmd = where = message = NULL;
for (j=1;j<buffer_at;j++) {
if (buffer[j] == ' ') {
usr = &buffer[1];
buffer[j] = '\0';
cmd = &buffer[j+1];
break;
}
}
// json parse
// we got some data from a client
r = jsmn_parse(&parser, readbuffer, len, tokens, sizeof(tokens)/sizeof(tokens[0]));
for (;j<buffer_at;j++) {
if (buffer[j] == ' ') {
message = &buffer[j+1];
buffer[j] = '\0';
break;
if ((r < 0) || (r < 1 || tokens[0].type != JSMN_OBJECT)) {
// invalid json
} else {
for (j = 1; j < r; j++) {
if (jsoneq(readbuffer, &tokens[j], "bbs") == 0) {
sprintf(msg.bbstag, "%.*s", tokens[j+1].end-tokens[j+1].start, readbuffer + tokens[j+1].start);
j++;
}
if (jsoneq(readbuffer, &tokens[j], "nick") == 0) {
sprintf(msg.nick, "%.*s", tokens[j+1].end-tokens[j+1].start, readbuffer + tokens[j+1].start);
j++;
}
if (jsoneq(readbuffer, &tokens[j], "msg") == 0) {
sprintf(msg.msg, "%.*s", tokens[j+1].end-tokens[j+1].start, readbuffer + tokens[j+1].start);
j++;
}
}
if (!strncmp(cmd, "PRIVMSG", 7) || !strncmp(cmd, "NOTICE", 6)) {
for (j=0;j<strlen(message);j++) {
if (message[j] == ' ') {
where = message;
message[j] = '\0';
message = &message[j+2];
break;
}
// set outputbuffer
if (strcmp(msg.bbstag, "SYSTEM") == 0 && strcmp(msg.nick, "SYSTEM") == 0) {
snprintf(outputbuffer, 512, ">> %s", msg.msg);
} else {
snprintf(outputbuffer, 512, "(%s)[%s]: %s", msg.bbstag, msg.nick, msg.msg);
}
if ((sep = strchr(usr, '!')) != NULL) usr[sep - usr] = '\0';
if (where[0] == '#' || where[0] == '&' || where[0] == '+' || where[0] == '!') target = where; else target = usr;
if (!strncmp(cmd, "PRIVMSG", 7)) {
if (strcmp(target, conf.irc_channel) == 0) {
sprintf(outputbuffer, "%s: %s", usr, message);
}
// screen_append output buffer
append_screenbuffer(outputbuffer);
do_update = 1;
}
}
}
memset(buffer, 0, 513);
buffer_at = 0;
} else if (c != '\n') {
buffer[buffer_at] = c;
buffer_at++;
}
}
}
if (do_update == 1) {

View File

@ -4,9 +4,9 @@ BBS Name = Magicka BBS
Sysop Name = sysop
nodes = 4
New User Level = 10
IRC Server = localhost
IRC Port = 6667
IRC Channel = #bbs
MagiChat Server = localhost
MagiChat Port = 6667
MagiChat BBSTag = Magicka
Default Tagline = Brought to you by Another Magicka BBS!
External Editor cmd = /home/andrew/MagickaBBS/doors/oedit.sh
External Editor stdio = false

16
main.c
View File

@ -432,12 +432,12 @@ static int handler(void* user, const char* section, const char* name,
conf->nodes = atoi(value);
} else if (strcasecmp(name, "new user level") == 0) {
conf->newuserlvl = atoi(value);
} else if (strcasecmp(name, "irc server") == 0) {
conf->irc_server = strdup(value);
} else if (strcasecmp(name, "irc port") == 0) {
conf->irc_port = atoi(value);
} else if (strcasecmp(name, "irc channel") == 0) {
conf->irc_channel = strdup(value);
} else if (strcasecmp(name, "magichat server") == 0) {
conf->mgchat_server = strdup(value);
} else if (strcasecmp(name, "magichat port") == 0) {
conf->mgchat_port = atoi(value);
} else if (strcasecmp(name, "magichat bbstag") == 0) {
conf->mgchat_bbstag = strdup(value);
} else if (strcasecmp(name, "default tagline") == 0) {
conf->default_tagline = strdup(value);
} else if (strcasecmp(name, "external editor cmd") == 0) {
@ -1081,8 +1081,8 @@ int main(int argc, char **argv) {
conf.mail_conference_count = 0;
conf.door_count = 0;
conf.file_directory_count = 0;
conf.irc_server = NULL;
conf.irc_port = 6667;
conf.mgchat_server = NULL;
conf.mgchat_port = 2025;
conf.text_file_count = 0;
conf.external_editor_cmd = NULL;
conf.log_path = NULL;

22
utils/magichat/Makefile Normal file
View File

@ -0,0 +1,22 @@
CC=cc
DEPS = main.c
OBJ = main.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
JSMN = ../../deps/jsmn/libjsmn.a
all: magichat
$(JSMN):
cd ../../deps/jsmn/ && $(MAKE)
magichat: $(OBJ) $(JSMN)
$(CC) -o magichat -o $@ $^ $(CFLAGS) $(LDFLAGS)
.PHONY: clean
clean:
rm -f $(OBJ) magichat

232
utils/magichat/main.c Normal file
View File

@ -0,0 +1,232 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "../../deps/jsmn/jsmn.h"
struct chat_msg {
char nick[16];
char bbstag[16];
char msg[256];
};
struct client {
char bbstag[16];
char nick[16];
int fd;
};
struct client **clients;
int client_count = 0;
typedef enum { START, KEY, PRINT, SKIP, STOP } parse_state;
static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
return 0;
}
return -1;
}
int main(int argc, char **argv) {
int port;
int server_socket;
struct sockaddr_in server, client;
fd_set master, read_fds;
int fdmax;
int c;
int new_fd;
struct chat_msg msg;
int i, j, k;
char buffer[1024];
char buf[1024];
jsmn_parser parser;
jsmntok_t tokens[6];
int r;
int nbytes;
if (argc < 2) {
printf("Usage: magichat [port]\n");
return 0;
}
port = atoi(argv[1]);
if (port <= 1024 && port > 65535) {
printf("Invalid port number, must be between 1024 - 65535\n");
return 0;
}
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
fprintf(stderr, "Couldn't create socket..\n");
exit(1);
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(port);
if (bind(server_socket, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("Bind Failed, Error\n");
exit(1);
}
listen(server_socket, 3);
FD_ZERO(&master);
FD_SET(server_socket, &master);
fdmax = server_socket;
c = sizeof(struct sockaddr_in);
jsmn_init(&parser);
while (1) {
read_fds = master;
if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
perror("select");
exit(-1);
}
for(i = 0; i <= fdmax; i++) {
if (FD_ISSET(i, &read_fds)) {
if (i == server_socket) {
new_fd = accept(server_socket, (struct sockaddr *)&client, (socklen_t *)&c);
if (new_fd == -1) {
perror("accept");
} else {
if (client_count == 0) {
clients = (struct client **)malloc(sizeof(struct client *));
} else {
clients = (struct client **)realloc(clients, sizeof(struct client *) * (client_count + 1));
}
if (!clients) {
fprintf(stderr, "Out of memory!\n");
return -1;
}
clients[client_count] = (struct client *)malloc(sizeof(struct client));
if (!clients[client_count]) {
fprintf(stderr, "Out of memory!\n");
return -1;
}
sprintf(clients[client_count]->bbstag, "UNKNOWN");
sprintf(clients[client_count]->nick, "UNKNOWN");
clients[client_count]->fd = new_fd;
client_count++;
FD_SET(new_fd, &master);
if (new_fd > fdmax) {
fdmax = new_fd;
}
}
} else {
if ((nbytes = recv(i, buf, sizeof buf, 0)) <= 0) {
for (k=0;k<client_count;k++) {
if (clients[k]->fd == i) {
if (strcmp(clients[k]->nick, "UNKNOWN") != 0) {
snprintf(buffer, 1024, "{\"bbs\": \"SYSTEM\", \"nick\": \"SYSTEM\", \"msg\": \"%s (%s) has left the chat\" }", clients[k]->nick, clients[k]->bbstag);
for (j=0;j<=fdmax;j++) {
if (FD_ISSET(j, &master)) {
if (j != server_socket && j != clients[k]->fd) {
if (send(j, buffer, strlen(buffer) + 1, 0) == -1) {
perror("send");
}
}
}
}
}
free(clients[k]);
for (j=k;j<client_count-1;j++) {
clients[j] = clients[j+1];
}
client_count--;
if (client_count == 0) {
free(clients);
} else {
clients = realloc(clients, sizeof(struct client) * (client_count));
}
}
}
close(i); // bye!
FD_CLR(i, &master); // remove from master set
} else {
// we got some data from a client
r = jsmn_parse(&parser, buf, nbytes, tokens, sizeof(tokens)/sizeof(tokens[0]));
if (r < 0) {
continue;
}
if (r < 1 || tokens[0].type != JSMN_OBJECT) {
continue;
}
for (j = 1; j < r; j++) {
if (jsoneq(buf, &tokens[j], "bbs") == 0) {
sprintf(msg.bbstag, "%.*s", tokens[j+1].end-tokens[j+1].start, buf + tokens[j+1].start);
j++;
}
if (jsoneq(buf, &tokens[j], "nick") == 0) {
sprintf(msg.nick, "%.*s", tokens[j+1].end-tokens[j+1].start, buf + tokens[j+1].start);
j++;
}
if (jsoneq(buf, &tokens[j], "msg") == 0) {
sprintf(msg.msg, "%.*s", tokens[j+1].end-tokens[j+1].start, buf + tokens[j+1].start);
j++;
}
}
if (strcmp(msg.msg, "LOGIN") == 0) {
for (j=0;j<client_count;j++) {
if (clients[j]->fd == i) {
strncpy(clients[j]->bbstag, msg.bbstag, 16);
strncpy(clients[j]->nick, msg.nick, 16);
for(k = 0; k <= client_count; k++) {
if (i != clients[k]->fd && strcmp(clients[k]->nick, "UNKNOWN") != 0) {
snprintf(buffer, 1024, "{\"bbs\": \"SYSTEM\", \"nick\": \"SYSTEM\", \"msg\": \"%s (%s) has joined the chat\" }", clients[j]->nick, clients[j]->bbstag);
if (send(k, buffer, strlen(buffer) + 1, 0) == -1) {
perror("send");
}
}
}
break;
}
}
} else {
for (j=0;j<client_count;j++) {
if (clients[j]->fd == i) {
if (strcmp(clients[j]->nick, "UNKNOWN") != 0) {
for(k = 0; k <= client_count; k++) {
if (i != clients[k]->fd && strcmp(clients[k]->nick, "UNKNOWN") != 0) {
if (send(k, buf, nbytes, 0) == -1) {
perror("send");
}
}
}
}
break;
}
}
}
}
}
}
}
}
}