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.
magicka/chat_system.c

295 lines
6.1 KiB
C
Raw Normal View History

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "bbs.h"
extern struct bbs_config conf;
extern int mynode;
2016-08-06 04:36:36 +00:00
extern int gSocket;
2016-08-07 03:47:59 +00:00
extern int sshBBS;
static char **screenbuffer;
static int chat_socket;
static int line_at;
static int row_at;
static char sbuf[512];
extern struct user_record gUser;
void scroll_up() {
int y;
for (y=1;y<23;y++) {
strcpy(screenbuffer[y-1], screenbuffer[y]);
}
memset(screenbuffer[22], 0, 81);
row_at = 0;
}
void raw(char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vsnprintf(sbuf, 512, fmt, ap);
va_end(ap);
write(chat_socket, sbuf, strlen(sbuf));
}
int hostname_to_ip(char * hostname , char* ip) {
struct hostent *he;
struct in_addr **addr_list;
int i;
2016-08-06 04:36:36 +00:00
if ( (he = gethostbyname( hostname ) ) == NULL)
{
// get the host info
return 1;
}
2016-08-06 04:36:36 +00:00
addr_list = (struct in_addr **) he->h_addr_list;
2016-08-06 04:36:36 +00:00
for(i = 0; addr_list[i] != NULL; i++) {
strcpy(ip , inet_ntoa(*addr_list[i]) );
return 0;
}
2016-08-06 04:36:36 +00:00
return 1;
}
void append_screenbuffer(char *buffer) {
int z;
2016-08-06 04:36:36 +00:00
for (z=0;z<strlen(buffer);z++) {
if (row_at == 80) {
if (line_at == 22) {
scroll_up();
row_at = 0;
} else {
row_at = 0;
line_at++;
}
}
2016-08-06 04:36:36 +00:00
screenbuffer[line_at][row_at] = buffer[z];
row_at++;
screenbuffer[line_at][row_at] = '\0';
}
if (line_at == 22) {
scroll_up();
}
if (line_at < 22) {
line_at++;
2016-08-06 04:36:36 +00:00
}
row_at = 0;
}
2016-08-06 04:36:36 +00:00
void chat_system(struct user_record *user) {
struct sockaddr_in servaddr;
2016-08-06 04:36:36 +00:00
fd_set fds;
int t;
int ret;
char inputbuffer[80];
int inputbuffer_at = 0;
int len;
char c;
char buffer2[256];
char buffer[513];
char outputbuffer[513];
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;
if (sshBBS) {
chat_in = STDIN_FILENO;
} else {
chat_in = gSocket;
}
memset(inputbuffer, 0, 80);
if (conf.irc_server == NULL) {
s_putstring(get_string(49));
return;
}
row_at = 0;
line_at = 0;
2016-08-06 04:36:36 +00:00
s_putstring("\e[2J");
memset(&servaddr, 0, sizeof(struct sockaddr_in));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(conf.irc_port);
2016-08-06 04:36:36 +00:00
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);
2016-03-28 04:46:48 +00:00
if (!inet_pton(AF_INET, buffer, &servaddr.sin_addr)) {
return;
}
}
if (connect(chat_socket, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0 ) {
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);
2016-08-06 04:36:36 +00:00
memset(buffer, 0, 513);
2016-08-06 04:36:36 +00:00
screenbuffer = (char **)malloc(sizeof(char *) * 23);
for (i=0;i<23;i++) {
screenbuffer[i] = (char *)malloc(81);
memset(screenbuffer[i], 0, 81);
}
2016-08-06 04:36:36 +00:00
while (1) {
FD_ZERO(&fds);
FD_SET(chat_in, &fds);
FD_SET(chat_socket, &fds);
2016-08-06 04:36:36 +00:00
if (chat_socket > chat_in) {
t = chat_socket + 1;
} else {
t = chat_in + 1;
}
2016-08-06 04:36:36 +00:00
ret = select(t, &fds, NULL, NULL, NULL);
2016-08-06 04:36:36 +00:00
if (ret > 0) {
if (FD_ISSET(chat_in, &fds)) {
len = read(chat_in, &c, 1);
if (len == 0) {
raw("QUIT\r\n");
2016-08-06 04:36:36 +00:00
disconnect("Socket closed");
}
2016-08-06 04:36:36 +00:00
if (c == '\r') {
if (inputbuffer[0] == '/') {
if (strcasecmp(&inputbuffer[1], "quit") == 0) {
raw("QUIT\r\n");
for (i=0;i<22;i++) {
free(screenbuffer[i]);
}
free(screenbuffer);
return;
}
} else {
raw("PRIVMSG %s :%s\r\n", conf.irc_channel, inputbuffer);
sprintf(buffer2, "%s: %s", user->loginname, inputbuffer);
append_screenbuffer(buffer2);
do_update = 1;
}
memset(inputbuffer, 0, 80);
inputbuffer_at = 0;
} else if (c != '\n') {
if (c == '\b' || c == 127) {
if (inputbuffer_at > 0) {
inputbuffer_at--;
inputbuffer[inputbuffer_at] = '\0';
do_update = 2;
}
} else if (inputbuffer_at < 79) {
inputbuffer[inputbuffer_at++] = c;
do_update = 2;
}
}
2016-08-06 04:36:36 +00:00
}
if (FD_ISSET(chat_socket, &fds)) {
len = read(chat_socket, &c, 1);
if (len == 0) {
2016-08-06 04:36:36 +00:00
s_putstring("\r\n\r\n\r\nLost connection to chat server!\r\n");
for (i=0;i<22;i++) {
free(screenbuffer[i]);
}
2016-08-06 04:36:36 +00:00
free(screenbuffer);
return;
}
2016-08-06 04:36:36 +00:00
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;
}
}
for (;j<buffer_at;j++) {
if (buffer[j] == ' ') {
message = &buffer[j+1];
buffer[j] = '\0';
break;
}
}
2016-08-06 04:36:36 +00:00
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;
}
}
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);
2016-08-06 04:36:36 +00:00
}
append_screenbuffer(outputbuffer);
do_update = 1;
2016-08-06 04:36:36 +00:00
}
}
}
2016-08-06 04:36:36 +00:00
memset(buffer, 0, 513);
buffer_at = 0;
} else if (c != '\n') {
buffer[buffer_at] = c;
buffer_at++;
}
}
}
if (do_update == 1) {
2016-08-06 04:36:36 +00:00
s_putstring("\e[2J");
for (i=0;i<=line_at;i++) {
2016-08-06 04:36:36 +00:00
s_printf("%s\r\n", screenbuffer[i]);
}
for (i=line_at+1;i<22;i++) {
2016-08-06 04:36:36 +00:00
s_putstring("\r\n");
}
s_putstring(get_string(50));
if (inputbuffer_at > 0) {
2016-08-06 04:36:36 +00:00
s_putstring(inputbuffer);
}
do_update = 0;
} else if (do_update == 2) {
2016-08-06 04:36:36 +00:00
s_printf("\e[24;1f%s\e[K", inputbuffer);
}
}
}