ii/ii.c

488 lines
12 KiB
C
Raw Normal View History

2005-12-19 16:39:54 +03:00
/*
2006-01-21 19:53:33 +03:00
* (C)opyright MMV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
* (C)opyright MMV-MMVI Nico Golde <nico at ngolde dot de>
2005-12-19 16:39:54 +03:00
* See LICENSE file for license details.
*/
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <fcntl.h>
#include <string.h>
#include <pwd.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
#ifndef nil
#define nil NULL /* for those who don't understand, nil is used in Plan9 */
#endif
enum { TOK_NICKSRV = 0, TOK_USER, TOK_CMD, TOK_CHAN, TOK_ARG, TOK_TEXT, TOK_LAST };
2005-12-29 19:59:54 +03:00
typedef struct Channel Channel;
struct Channel {
int fd;
char *name;
Channel *next;
};
2005-12-19 16:39:54 +03:00
static int irc;
2005-12-29 19:59:54 +03:00
static Channel *channels = nil;
static char *host = "irc.freenode.net";
2005-12-19 16:39:54 +03:00
static char nick[32]; /* might change while running */
static char path[_POSIX_PATH_MAX];
2005-12-29 19:59:54 +03:00
static char message[PIPE_BUF]; /* message buf used for communication */
2005-12-19 16:39:54 +03:00
static void usage()
{
fprintf(stderr, "%s",
"ii - irc it - " VERSION "\n"
"(C)opyright MMVI Anselm R. Garbe, Nico Golde\n"
2005-12-29 19:59:54 +03:00
"usage: ii [-i <irc dir>] [-s <host>] [-p <port>]\n"
2005-12-19 16:39:54 +03:00
" [-n <nick>] [-k <password>] [-f <fullname>]\n");
exit(EXIT_SUCCESS);
}
2005-12-29 19:59:54 +03:00
/* creates directories top-down, if necessary */
static void create_dirtree(const char *dir)
{
char tmp[256];
char *p;
size_t len;
snprintf(tmp, sizeof(tmp),"%s",dir);
len = strlen(tmp);
if(tmp[len - 1] == '/')
tmp[len - 1] = 0;
for(p = tmp + 1; *p; p++)
if(*p == '/') {
*p = 0;
mkdir(tmp, S_IRWXU);
*p = '/';
}
mkdir(tmp, S_IRWXU);
}
2005-12-29 20:24:42 +03:00
static int get_filepath(char *filepath, size_t len, char *channel, char *file)
2005-12-29 19:59:54 +03:00
{
if(channel) {
if(!snprintf(filepath, len, "%s/%s", path, channel))
return 0;
create_dirtree(filepath);
return snprintf(filepath, len, "%s/%s/%s", path, channel, file);
}
return snprintf(filepath, len, "%s/%s", path, file);
}
2005-12-29 20:24:42 +03:00
static void create_filepath(char *filepath, size_t len, char *channel, char *suffix)
2005-12-29 19:59:54 +03:00
{
if(!get_filepath(filepath, len, channel, suffix)) {
fprintf(stderr, "%s", "ii: path to irc directory too long\n");
exit(EXIT_FAILURE);
}
}
static int open_channel(char *name)
{
static char infile[256];
create_filepath(infile, sizeof(infile), name, "in");
2005-12-29 20:24:42 +03:00
if(access(infile, F_OK) == -1)
mkfifo(infile, S_IRWXU);
2005-12-29 19:59:54 +03:00
return open(infile, O_RDONLY | O_NONBLOCK, 0);
}
static void add_channel(char *name)
{
Channel *c;
2006-01-23 15:11:36 +03:00
int fd;
for(c = channels; c; c = c->next)
if(!strcmp(name, c->name))
return; /* already handled */
2005-12-29 19:59:54 +03:00
2006-01-23 15:11:36 +03:00
fd = open_channel(name);
2005-12-29 20:24:42 +03:00
if(fd == -1) {
2006-01-23 15:11:36 +03:00
perror("ii: cannot create in channel");
2005-12-29 19:59:54 +03:00
return;
}
2006-01-23 15:29:51 +03:00
c = calloc(1, sizeof(Channel));
2005-12-29 19:59:54 +03:00
if(!c) {
perror("ii: cannot allocate memory");
exit(EXIT_FAILURE);
}
2006-01-23 15:35:55 +03:00
if(!channels)
2006-01-23 15:29:51 +03:00
channels = c;
2006-01-25 18:08:19 +03:00
else {
2006-01-23 15:29:51 +03:00
c->next = channels;
2006-01-25 18:08:19 +03:00
channels = c;
}
2005-12-29 19:59:54 +03:00
c->fd = fd;
c->name = strdup(name);
}
static void rm_channel(Channel *c)
{
Channel *p;
if(channels == c)
channels = channels->next;
else {
for(p = channels; p && p->next != c; p = p->next);
if(p->next == c)
p->next = c->next;
}
free(c->name);
free(c);
}
2005-12-19 16:39:54 +03:00
static void login(char *key, char *fullname)
{
if(key)
2005-12-29 19:59:54 +03:00
snprintf(message, PIPE_BUF,
2005-12-19 16:39:54 +03:00
"PASS %s\r\nNICK %s\r\nUSER %s localhost %s :%s\r\n", key,
2005-12-29 19:59:54 +03:00
nick, nick, host, fullname ? fullname : nick);
2005-12-19 16:39:54 +03:00
else
2005-12-29 19:59:54 +03:00
snprintf(message, PIPE_BUF, "NICK %s\r\nUSER %s localhost %s :%s\r\n",
nick, nick, host, fullname ? fullname : nick);
write(irc, message, strlen(message)); /* login */
2005-12-19 16:39:54 +03:00
}
static int tcpopen(unsigned short port)
{
int fd;
struct sockaddr_in sin;
2005-12-29 19:59:54 +03:00
struct hostent *hp = gethostbyname(host);
2005-12-19 16:39:54 +03:00
memset(&sin, 0, sizeof(struct sockaddr_in));
if(hp == nil) {
perror("ii: cannot retrieve host information");
exit(EXIT_FAILURE);
}
sin.sin_family = AF_INET;
2006-01-28 23:34:27 +03:00
memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
2005-12-19 16:39:54 +03:00
sin.sin_port = htons(port);
if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("ii: cannot create socket");
exit(EXIT_FAILURE);
}
if(connect(fd, (const struct sockaddr *) &sin, sizeof(sin)) < 0) {
2005-12-29 19:59:54 +03:00
perror("ii: cannot connect to host");
2005-12-19 16:39:54 +03:00
exit(EXIT_FAILURE);
}
return fd;
}
static size_t tokenize(char **result, size_t reslen, char *str, char delim)
{
char *p, *n;
size_t i;
if(!str)
return 0;
for(n = str; *n == ' '; n++);
p = n;
for(i = 0; *n != 0;) {
if(i == reslen)
return 0;
if(*n == delim) {
*n = 0;
result[i++] = p;
p = ++n;
} else
n++;
}
result[i++] = p;
return i + 2; /* number of tokens */
}
2005-12-29 19:59:54 +03:00
static void print_out(char *channel, char *buf)
2005-12-19 16:39:54 +03:00
{
static char outfile[256];
FILE *out;
static char buft[8];
time_t t = time(0);
create_filepath(outfile, sizeof(outfile), channel, "out");
out = fopen(outfile, "a");
strftime(buft, sizeof(buft), "%R", localtime(&t));
2005-12-29 19:59:54 +03:00
fprintf(out, "%s %s\n", buft, buf);
2005-12-19 16:39:54 +03:00
fclose(out);
}
2005-12-29 19:59:54 +03:00
static void proc_channels_privmsg(char *channel, char *buf)
2005-12-19 16:39:54 +03:00
{
2005-12-29 19:59:54 +03:00
snprintf(message, PIPE_BUF, "<%s> %s", nick, buf);
print_out(channel, message);
snprintf(message, PIPE_BUF, "PRIVMSG %s :%s\r\n", channel, buf);
write(irc, message, strlen(message));
2005-12-19 16:39:54 +03:00
}
2006-01-27 15:51:11 +03:00
static void proc_channels_input(Channel *c, char *buf)
2005-12-19 16:39:54 +03:00
{
static char infile[256];
char *p;
2005-12-29 19:59:54 +03:00
if(buf[0] != '/' && buf[0] != 0) {
2005-12-29 19:59:54 +03:00
proc_channels_privmsg(c->name, buf);
2005-12-19 16:39:54 +03:00
return;
}
2005-12-29 19:59:54 +03:00
switch (buf[1]) {
2005-12-19 16:39:54 +03:00
case 'j':
2005-12-29 19:59:54 +03:00
p = strchr(&buf[3], ' ');
2005-12-19 16:39:54 +03:00
if(p)
*p = 0;
2005-12-29 19:59:54 +03:00
snprintf(message, PIPE_BUF, "JOIN %s\r\n", &buf[3]);
add_channel(&buf[3]);
2005-12-19 16:39:54 +03:00
break;
case 't':
2005-12-29 19:59:54 +03:00
snprintf(message, PIPE_BUF, "TOPIC %s :%s\r\n", c->name, &buf[3]);
2005-12-19 16:39:54 +03:00
break;
case 'a':
2005-12-29 19:59:54 +03:00
snprintf(message, PIPE_BUF, "-!- %s is away \"%s\"", nick, &buf[3]);
print_out(c->name, message);
if(buf[2] == 0)
snprintf(message, PIPE_BUF, "AWAY\r\n");
2005-12-27 02:08:44 +03:00
else
2005-12-29 19:59:54 +03:00
snprintf(message, PIPE_BUF, "AWAY :%s\r\n", &buf[3]);
2005-12-19 16:39:54 +03:00
break;
case 'm':
2005-12-29 19:59:54 +03:00
p = strchr(&buf[3], ' ');
2005-12-19 16:39:54 +03:00
if(p) {
*p = 0;
2005-12-29 19:59:54 +03:00
add_channel(&buf[3]);
proc_channels_privmsg(&buf[3], p + 1);
2005-12-19 16:39:54 +03:00
}
return;
break;
case 'n':
2005-12-29 19:59:54 +03:00
snprintf(nick, sizeof(nick),"%s", buf);
snprintf(message, PIPE_BUF, "NICK %s\r\n", &buf[3]);
2005-12-19 16:39:54 +03:00
break;
case 'l':
2005-12-29 19:59:54 +03:00
if(c->name[0] == 0)
2005-12-19 16:39:54 +03:00
return;
2005-12-29 19:59:54 +03:00
if(buf[2] == ' ')
snprintf(message, PIPE_BUF, "PART %s :%s\r\n", c->name, &buf[3]);
2005-12-19 16:39:54 +03:00
else
2005-12-29 19:59:54 +03:00
snprintf(message, PIPE_BUF,
"PART %s :ii - 500SLOC are too much\r\n", c->name);
write(irc, message, strlen(message));
2006-01-27 15:51:11 +03:00
close(c->fd);
2005-12-29 19:59:54 +03:00
create_filepath(infile, sizeof(infile), c->name, "in");
2005-12-19 16:39:54 +03:00
unlink(infile);
2005-12-29 19:59:54 +03:00
rm_channel(c);
2005-12-19 16:39:54 +03:00
return;
break;
default:
2005-12-29 19:59:54 +03:00
snprintf(message, PIPE_BUF, "%s\r\n", &buf[1]);
2005-12-19 16:39:54 +03:00
break;
}
2005-12-29 19:59:54 +03:00
write(irc, message, strlen(message));
2005-12-19 16:39:54 +03:00
}
2005-12-29 19:59:54 +03:00
static void proc_server_cmd(char *buf)
2005-12-19 16:39:54 +03:00
{
char *argv[TOK_LAST], *cmd, *p;
int i;
2005-12-29 19:59:54 +03:00
if(!buf)
2005-12-19 16:39:54 +03:00
return;
for(i = 0; i < TOK_LAST; i++)
argv[i] = nil;
/*
<message> ::= [':' <prefix> <SPACE> ] <command> <params> <crlf>
<prefix> ::= <servername> | <nick> [ '!' <user> ] [ '@' <host> ]
<command> ::= <letter> { <letter> } | <number> <number> <number>
<SPACE> ::= ' ' { ' ' }
<params> ::= <SPACE> [ ':' <trailing> | <middle> <params> ]
<middle> ::= <Any *non-empty* sequence of octets not including SPACE
or NUL or CR or LF, the first of which may not be ':'>
<trailing> ::= <Any, possibly *empty*, sequence of octets not including NUL or CR or LF>
<crlf> ::= CR LF
*/
2005-12-29 19:59:54 +03:00
if(buf[0] == ':') { /* check prefix */
p = strchr(buf, ' ');
2005-12-19 16:39:54 +03:00
*p = 0;
for(++p; *p == ' '; p++);
cmd = p;
2005-12-29 19:59:54 +03:00
argv[TOK_NICKSRV] = &buf[1];
if((p = strchr(buf, '!'))) {
2005-12-19 16:39:54 +03:00
*p = 0;
argv[TOK_USER] = ++p;
}
} else
2005-12-29 19:59:54 +03:00
cmd = buf;
2005-12-19 16:39:54 +03:00
/* remove CRLFs */
for(p = cmd; p && *p != 0; p++)
if(*p == '\r' || *p == '\n')
*p = 0;
if((p = strchr(cmd, ':'))) {
*p = 0;
argv[TOK_TEXT] = ++p;
}
tokenize(&argv[TOK_CMD], TOK_LAST - TOK_CMD + 1, cmd, ' ');
if(!strncmp("PING", argv[TOK_CMD], 5)) {
2005-12-29 19:59:54 +03:00
snprintf(message, PIPE_BUF, "PONG %s\r\n", argv[TOK_TEXT]);
write(irc, message, strlen(message));
2005-12-19 16:39:54 +03:00
return;
} else if(!argv[TOK_NICKSRV] || !argv[TOK_USER]) { /* server command */
2005-12-29 19:59:54 +03:00
snprintf(message, PIPE_BUF, "%s", argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
print_out(0, message);
2005-12-19 16:39:54 +03:00
return;
} else if(!strncmp("ERROR", argv[TOK_CMD], 6))
2005-12-29 19:59:54 +03:00
snprintf(message, PIPE_BUF, "-!- error %s", argv[TOK_TEXT] ? argv[TOK_TEXT] : "unknown");
2005-12-19 16:39:54 +03:00
else if(!strncmp("JOIN", argv[TOK_CMD], 5)) {
if(argv[TOK_TEXT]!=nil){
p = strchr(argv[TOK_TEXT], ' ');
if(p)
*p = 0;
}
argv[TOK_CHAN] = argv[TOK_TEXT];
2005-12-29 19:59:54 +03:00
snprintf(message, PIPE_BUF, "-!- %s(%s) has joined %s", argv[TOK_NICKSRV], argv[TOK_USER], argv[TOK_TEXT]);
2005-12-19 16:39:54 +03:00
} else if(!strncmp("PART", argv[TOK_CMD], 5)) {
2005-12-29 19:59:54 +03:00
snprintf(message, PIPE_BUF, "-!- %s(%s) has left %s", argv[TOK_NICKSRV], argv[TOK_USER], argv[TOK_CHAN]);
2005-12-19 16:39:54 +03:00
} else if(!strncmp("MODE", argv[TOK_CMD], 5))
2005-12-29 19:59:54 +03:00
snprintf(message, PIPE_BUF, "-!- %s changed mode/%s -> %s %s", argv[TOK_NICKSRV], argv[TOK_CMD + 1], argv[TOK_CMD + 2], argv[TOK_CMD + 3]);
2005-12-19 16:39:54 +03:00
else if(!strncmp("QUIT", argv[TOK_CMD], 5))
snprintf(message, PIPE_BUF, "-!- %s(%s) has quit \"%s\"", argv[TOK_NICKSRV], argv[TOK_USER], argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
2005-12-19 16:39:54 +03:00
else if(!strncmp("NICK", argv[TOK_CMD], 5))
2005-12-29 19:59:54 +03:00
snprintf(message, PIPE_BUF, "-!- %s changed nick to %s", argv[TOK_NICKSRV], argv[TOK_TEXT]);
2005-12-19 16:39:54 +03:00
else if(!strncmp("TOPIC", argv[TOK_CMD], 6))
2005-12-29 19:59:54 +03:00
snprintf(message, PIPE_BUF, "-!- %s changed topic to \"%s\"", argv[TOK_NICKSRV], argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
2005-12-19 16:39:54 +03:00
else if(!strncmp("KICK", argv[TOK_CMD], 5))
2005-12-29 19:59:54 +03:00
snprintf(message, PIPE_BUF, "-!- %s kicked %s (\"%s\")", argv[TOK_NICKSRV], argv[TOK_ARG], argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
2005-12-19 16:39:54 +03:00
else if(!strncmp("NOTICE", argv[TOK_CMD], 7))
2005-12-29 19:59:54 +03:00
snprintf(message, PIPE_BUF, "-!- \"%s\")", argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
2005-12-19 16:39:54 +03:00
else if(!strncmp("PRIVMSG", argv[TOK_CMD], 8))
2005-12-29 19:59:54 +03:00
snprintf(message, PIPE_BUF, "<%s> %s", argv[TOK_NICKSRV], argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
2005-12-19 16:39:54 +03:00
if(!argv[TOK_CHAN] || !strncmp(argv[TOK_CHAN], nick, strlen(nick)))
2005-12-29 19:59:54 +03:00
print_out(argv[TOK_NICKSRV], message);
2005-12-19 16:39:54 +03:00
else
2005-12-29 19:59:54 +03:00
print_out(argv[TOK_CHAN], message);
2005-12-19 16:39:54 +03:00
}
2005-12-29 19:59:54 +03:00
static int read_line(int fd, size_t res_len, char *buf)
2005-12-19 16:39:54 +03:00
{
2005-12-29 19:59:54 +03:00
size_t i = 0;
2005-12-19 16:39:54 +03:00
char c;
do {
if(read(fd, &c, sizeof(char)) != sizeof(char))
2005-12-29 19:59:54 +03:00
return -1;
buf[i++] = c;
2005-12-19 16:39:54 +03:00
}
2005-12-29 19:59:54 +03:00
while(c != '\n' && i < res_len);
buf[i - 1] = 0; /* eliminates '\n' */
return 0;
2005-12-19 16:39:54 +03:00
}
2005-12-29 19:59:54 +03:00
static void handle_channels_input(Channel *c)
2005-12-19 16:39:54 +03:00
{
2005-12-29 19:59:54 +03:00
static char buf[PIPE_BUF];
if(read_line(c->fd, PIPE_BUF, buf) == -1) {
int fd = open_channel(c->name);
if(fd != -1)
c->fd = fd;
else
rm_channel(c);
2005-12-19 16:39:54 +03:00
return;
}
2006-01-27 15:51:11 +03:00
proc_channels_input(c, buf);
2005-12-19 16:39:54 +03:00
}
static void handle_server_output()
{
2005-12-29 19:59:54 +03:00
static char buf[PIPE_BUF];
if(read_line(irc, PIPE_BUF, buf) == -1) {
2005-12-19 16:39:54 +03:00
perror("ii: remote host closed connection");
exit(EXIT_FAILURE);
}
2005-12-29 19:59:54 +03:00
proc_server_cmd(buf);
2005-12-19 16:39:54 +03:00
}
static void run()
{
2005-12-29 19:59:54 +03:00
Channel *c;
int r, maxfd;
2005-12-19 16:39:54 +03:00
fd_set rd;
for(;;) {
FD_ZERO(&rd);
maxfd = irc;
FD_SET(irc, &rd);
2005-12-29 19:59:54 +03:00
for(c = channels; c; c = c->next) {
if(maxfd < c->fd) {
maxfd = c->fd;
FD_SET(c->fd, &rd);
2005-12-19 16:39:54 +03:00
}
}
r = select(maxfd + 1, &rd, 0, 0, 0);
if(r == -1 && errno == EINTR)
continue;
if(r < 0) {
perror("ii: error on select()");
exit(EXIT_FAILURE);
} else if(r > 0) {
2006-01-23 15:11:36 +03:00
if(FD_ISSET(irc, &rd))
handle_server_output();
for(c = channels; c; c = c->next)
if(FD_ISSET(c->fd, &rd))
handle_channels_input(c);
2005-12-19 16:39:54 +03:00
}
}
}
int main(int argc, char *argv[])
{
int i;
unsigned short port = 6667;
struct passwd *spw = getpwuid(getuid());
char *key = nil;
char prefix[_POSIX_PATH_MAX];
char *fullname = nil;
if(!spw) {
fprintf(stderr,"ii: getpwuid() failed\n");
exit(EXIT_FAILURE);
}
snprintf(nick, sizeof(nick), "%s", spw->pw_name);
2006-01-29 01:14:01 +03:00
snprintf(prefix, sizeof(prefix),"%s/irc", spw->pw_dir);
2005-12-19 16:39:54 +03:00
if(argc == 2 && argv[1][0] == '-' && argv[1][1] == 'h')
usage();
for(i = 1; (i + 1 < argc) && (argv[i][0] == '-'); i++) {
switch (argv[i][1]) {
2006-01-21 19:53:33 +03:00
case 'i': snprintf(prefix,sizeof(prefix),"%s", argv[++i]); break;
case 's': host = argv[++i]; break;
case 'p': port = atoi(argv[++i]); break;
case 'n': snprintf(nick,sizeof(nick),"%s", argv[++i]); break;
case 'k': key = argv[++i]; break;
case 'f': fullname = argv[++i]; break;
default: usage(); break;
2005-12-19 16:39:54 +03:00
}
}
irc = tcpopen(port);
2006-01-29 01:14:01 +03:00
if(!snprintf(path, sizeof(path), "%s/%s", prefix, host)) {
2005-12-19 16:39:54 +03:00
fprintf(stderr, "%s", "ii: path to irc directory too long\n");
exit(EXIT_FAILURE);
}
2005-12-29 19:59:54 +03:00
create_dirtree(path);
2005-12-19 16:39:54 +03:00
2005-12-29 19:59:54 +03:00
add_channel(""); /* master channel */
2005-12-19 16:39:54 +03:00
login(key, fullname);
run();
return 0;
}