Experimental work on magichat

This commit is contained in:
Andrew Pamment 2017-09-30 00:10:44 +10:00
parent 59bc08b24c
commit 9dfd8dd453
2 changed files with 69 additions and 44 deletions

View File

@ -132,6 +132,8 @@ void chat_system(struct user_record *user) {
char buffer[513]; char buffer[513];
char outputbuffer[513]; char outputbuffer[513];
char readbuffer[1024]; char readbuffer[1024];
char message[1024];
char partmessage[1024];
int buffer_at = 0; int buffer_at = 0;
int do_update = 1; int do_update = 1;
int i; int i;
@ -142,6 +144,8 @@ void chat_system(struct user_record *user) {
int r; int r;
struct chat_msg msg; struct chat_msg msg;
char *input_b; char *input_b;
char *ptr;
int z;
if (sshBBS) { if (sshBBS) {
chat_in = STDIN_FILENO; chat_in = STDIN_FILENO;
@ -188,8 +192,9 @@ void chat_system(struct user_record *user) {
screenbuffer[i] = (char *)malloc(81); screenbuffer[i] = (char *)malloc(81);
memset(screenbuffer[i], 0, 81); memset(screenbuffer[i], 0, 81);
} }
memset(partmessage, 0, 1024);
raw("{ \"bbs\": \"%s\", \"nick\": \"%s\", \"msg\": \"LOGIN\" }", conf.mgchat_bbstag, user->loginname); raw("{ \"bbs\": \"%s\", \"nick\": \"%s\", \"msg\": \"LOGIN\" }\n", conf.mgchat_bbstag, user->loginname);
while (1) { while (1) {
FD_ZERO(&fds); FD_ZERO(&fds);
@ -224,7 +229,7 @@ void chat_system(struct user_record *user) {
} }
} else { } else {
input_b = encapsulate_quote(inputbuffer); input_b = encapsulate_quote(inputbuffer);
raw("{ \"bbs\": \"%s\", \"nick\": \"%s\", \"msg\": \"%s\" }", conf.mgchat_bbstag, user->loginname, input_b); raw("{ \"bbs\": \"%s\", \"nick\": \"%s\", \"msg\": \"%s\" }\n", conf.mgchat_bbstag, user->loginname, input_b);
free(input_b); free(input_b);
sprintf(buffer2, "(%s)[%s]: %s", conf.mgchat_bbstag, user->loginname, inputbuffer); sprintf(buffer2, "(%s)[%s]: %s", conf.mgchat_bbstag, user->loginname, inputbuffer);
append_screenbuffer(buffer2); append_screenbuffer(buffer2);
@ -246,7 +251,7 @@ void chat_system(struct user_record *user) {
} }
} }
if (FD_ISSET(chat_socket, &fds)) { if (FD_ISSET(chat_socket, &fds)) {
len = read(chat_socket, readbuffer, 1024); len = read(chat_socket, readbuffer, 512);
if (len == 0) { if (len == 0) {
s_putstring("\r\n\r\n\r\nLost connection to chat server!\r\n"); s_putstring("\r\n\r\n\r\nLost connection to chat server!\r\n");
for (i=0;i<22;i++) { for (i=0;i<22;i++) {
@ -256,25 +261,35 @@ void chat_system(struct user_record *user) {
return; return;
} }
strncat(partmessage, readbuffer, 1024);
strcpy(readbuffer, partmessage);
len = strlen(readbuffer);
for (z = 0;z < len; z++) {
if (readbuffer[z] != '\n') {
message[z] = readbuffer[z];
message[z+1] = '\0';
} else {
// json parse // json parse
jsmn_init(&parser); jsmn_init(&parser);
// we got some data from a client // we got some data from a client
r = jsmn_parse(&parser, readbuffer, len, tokens, sizeof(tokens)/sizeof(tokens[0])); r = jsmn_parse(&parser, message, strlen(message), tokens, sizeof(tokens)/sizeof(tokens[0]));
if ((r < 0) || (r < 1 || tokens[0].type != JSMN_OBJECT)) { if ((r < 0) || (r < 1 || tokens[0].type != JSMN_OBJECT)) {
// invalid json // invalid json
} else { } else {
for (j = 1; j < r; j++) { for (j = 1; j < r; j++) {
if (jsoneq(readbuffer, &tokens[j], "bbs") == 0) { if (jsoneq(message, &tokens[j], "bbs") == 0) {
sprintf(msg.bbstag, "%.*s", tokens[j+1].end-tokens[j+1].start, readbuffer + tokens[j+1].start); sprintf(msg.bbstag, "%.*s", tokens[j+1].end-tokens[j+1].start, message + tokens[j+1].start);
j++; j++;
} }
if (jsoneq(readbuffer, &tokens[j], "nick") == 0) { if (jsoneq(message, &tokens[j], "nick") == 0) {
sprintf(msg.nick, "%.*s", tokens[j+1].end-tokens[j+1].start, readbuffer + tokens[j+1].start); sprintf(msg.nick, "%.*s", tokens[j+1].end-tokens[j+1].start, message + tokens[j+1].start);
j++; j++;
} }
if (jsoneq(readbuffer, &tokens[j], "msg") == 0) { if (jsoneq(message, &tokens[j], "msg") == 0) {
sprintf(msg.msg, "%.*s", tokens[j+1].end-tokens[j+1].start, readbuffer + tokens[j+1].start); sprintf(msg.msg, "%.*s", tokens[j+1].end-tokens[j+1].start, message + tokens[j+1].start);
j++; j++;
} }
} }
@ -293,6 +308,15 @@ void chat_system(struct user_record *user) {
memset(buffer, 0, 513); memset(buffer, 0, 513);
buffer_at = 0; buffer_at = 0;
} }
}
if (z < len) {
memset(partmessage, 0, 1024);
memcpy(partmessage, &readbuffer[z], len - z);
} else {
memset(partmessage, 0, 1024);
}
}
} }
if (do_update == 1) { if (do_update == 1) {
s_putstring("\e[2J\e[1;1H"); s_putstring("\e[2J\e[1;1H");

View File

@ -137,11 +137,11 @@ int main(int argc, char **argv) {
for (k=0;k<client_count;k++) { for (k=0;k<client_count;k++) {
if (clients[k]->fd == i) { if (clients[k]->fd == i) {
if (strcmp(clients[k]->nick, "UNKNOWN") != 0) { 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); snprintf(buffer, 1024, "{\"bbs\": \"SYSTEM\", \"nick\": \"SYSTEM\", \"msg\": \"%s (%s) has left the chat\" }\n", clients[k]->nick, clients[k]->bbstag);
for (j=0;j<=fdmax;j++) { for (j=0;j<=fdmax;j++) {
if (FD_ISSET(j, &master)) { if (FD_ISSET(j, &master)) {
if (j != server_socket && j != clients[k]->fd) { if (j != server_socket && j != clients[k]->fd) {
if (send(j, buffer, strlen(buffer) + 1, 0) == -1) { if (send(j, buffer, strlen(buffer), 0) == -1) {
perror("send"); perror("send");
} }
} }
@ -204,8 +204,8 @@ int main(int argc, char **argv) {
for(k = 0; k < client_count; k++) { for(k = 0; k < client_count; k++) {
if (i != clients[k]->fd && strcmp(clients[k]->nick, "UNKNOWN") != 0) { 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); snprintf(buffer, 1024, "{\"bbs\": \"SYSTEM\", \"nick\": \"SYSTEM\", \"msg\": \"%s (%s) has joined the chat\" }\n", clients[j]->nick, clients[j]->bbstag);
if (send(clients[k]->fd, buffer, strlen(buffer) + 1, 0) == -1) { if (send(clients[k]->fd, buffer, strlen(buffer), 0) == -1) {
perror("send"); perror("send");
} }
} }
@ -224,8 +224,9 @@ int main(int argc, char **argv) {
} }
snprintf(buffer, 1024, "{\"bbs\": \"SYSTEM\", \"nick\": \"SYSTEM\", \"msg\": \"%s\" }", motd); snprintf(buffer, 1024, "{\"bbs\": \"SYSTEM\", \"nick\": \"SYSTEM\", \"msg\": \"%s\" }\n", motd);
if (send(clients[j]->fd, buffer, strlen(buffer) + 1, 0) == -1) {
if (send(i, buffer, strlen(buffer), 0) == -1) {
perror("send"); perror("send");
} }
fgets(motd, 256, fptr); fgets(motd, 256, fptr);