8 Commits
0.9 ... 1.0

5 changed files with 61 additions and 58 deletions

View File

@ -6,3 +6,4 @@ a3549fb4c72ff0edb816c8c29be7ff289db5b003 0.4
d7923d9e717c1c6f1ed3b17ec90bfdd7e7bfcca0 0.6
643a6e8b8634b70d2459637fcfff6eca776fc919 0.7
07fb3efaa2e9ed18c6c16f0ddd8576cb66fec9c6 0.8
96eb1bfede5b72fcee3f515d3113d814f7e87108 0.9

View File

@ -1,7 +1,7 @@
MIT/X Consortium License
(C)opyright MMV-MMVI Anselm R. Garbe <garbeam@gmail.com>
(C)opyright MMV Nico Golde <nico at ngolde dot de>
© 2005-2008 Anselm R Garbe <garbeam at gmail dot com>
© 2005 Nico Golde <nico at ngolde dot de>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),

View File

@ -1,5 +1,4 @@
# sic - simple irc client
# (C)opyright MMVI Anselm R. Garbe
include config.mk
@ -13,7 +12,6 @@ options:
@echo "CFLAGS = ${CFLAGS}"
@echo "LDFLAGS = ${LDFLAGS}"
@echo "CC = ${CC}"
@echo "LD = ${LD}"
.c.o:
@echo CC $<
@ -22,9 +20,8 @@ options:
${OBJ}: config.mk
sic: ${OBJ}
@echo LD $@
@${LD} -o $@ ${OBJ} ${LDFLAGS}
@strip $@
@echo CC -o $@
@${CC} -o $@ ${OBJ} ${LDFLAGS}
clean:
@echo cleaning
@ -45,7 +42,7 @@ install: all
@chmod 755 ${DESTDIR}${PREFIX}/bin/sic
@echo installing manual page to ${DESTDIR}${MANPREFIX}/man1
@mkdir -p ${DESTDIR}${MANPREFIX}/man1
@sed 's/VERSION/${VERSION}/g' < sic.1 > ${DESTDIR}${MANPREFIX}/man1/sic.1
@sed "s/VERSION/${VERSION}/g" < sic.1 > ${DESTDIR}${MANPREFIX}/man1/sic.1
@chmod 644 ${DESTDIR}${MANPREFIX}/man1/sic.1
uninstall:

View File

@ -1,5 +1,5 @@
# sic version
VERSION = 0.9
VERSION = 1.0
# Customize below to fit your system
@ -12,11 +12,9 @@ INCS = -I. -I/usr/include
LIBS = -L/usr/lib -lc
# flags
CFLAGS = -Os ${INCS} -DVERSION=\"${VERSION}\"
LDFLAGS = ${LIBS}
#CFLAGS = -g -Wall -O2 ${INCS} -DVERSION=\"${VERSION}\"
#LDFLAGS = -g ${LIBS}
CPPFLAGS = -DVERSION=\"${VERSION}\" -D_GNU_SOURCE
CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
LDFLAGS = -s ${LIBS}
# compiler and linker
CC = cc
LD = ${CC}

95
sic.c
View File

@ -1,7 +1,6 @@
/* (C)opyright MMV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
* (C)opyright MMV-MMVI Nico Golde <nico at ngolde dot de>
* See LICENSE file for license details.
*/
/* © 2005-2008 Anselm R Garbe <garbeam at gmail dot com>
* © 2005 Nico Golde <nico at ngolde dot de>
* See LICENSE file for license details. */
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
@ -15,7 +14,14 @@
#include <sys/time.h>
#define PINGTIMEOUT 300
#define MAXMSG 4096
#define MAXMSG 4096
static void die(const char *errstr, ...);
static void printl(char *channel, char *msg);
static void privmsg(char *channel, char *msg);
static void parsein(char *msg);
static void parsesrv(char *msg);
static int readl(int fd, unsigned int len, char *buf);
static char *host = "irc.oftc.net";
static unsigned short port = 6667;
@ -27,8 +33,8 @@ static char channel[256];
static int srv;
static time_t trespond;
static void
eprint(const char *errstr, ...) {
void
die(const char *errstr, ...) {
va_list ap;
va_start(ap, errstr);
@ -37,23 +43,8 @@ eprint(const char *errstr, ...) {
exit(EXIT_FAILURE);
}
static int
getline(int fd, unsigned int len, char *buf) {
unsigned int i = 0;
char c;
do {
if(read(fd, &c, sizeof(char)) != sizeof(char))
return -1;
buf[i++] = c;
}
while(c != '\n' && i < len);
buf[i - 1] = 0;
return 0;
}
static void
pout(char *channel, char *msg) {
void
printl(char *channel, char *msg) {
static char timestr[18];
time_t t = time(0);
@ -61,17 +52,17 @@ pout(char *channel, char *msg) {
fprintf(stdout, "%-12.12s: %s %s\n", channel, timestr, msg);
}
static void
void
privmsg(char *channel, char *msg) {
if(channel[0] == 0)
return;
snprintf(bufout, sizeof bufout, "<%s> %s", nick, msg);
pout(channel, bufout);
printl(channel, bufout);
snprintf(bufout, sizeof bufout, "PRIVMSG %s :%s\r\n", channel, msg);
write(srv, bufout, strlen(bufout));
}
static void
void
parsein(char *msg) {
char *p;
@ -100,7 +91,7 @@ parsein(char *msg) {
write(srv, bufout, strlen(bufout));
}
static void
void
parsesrv(char *msg) {
char *chan, *cmd, *p, *txt, *usr;
@ -136,7 +127,7 @@ parsesrv(char *msg) {
for(; *p && *p != ' '; p++);
*p = 0;
snprintf(bufout, sizeof bufout, "<%s> %s", usr, txt);
pout(chan, bufout);
printl(chan, bufout);
}
else if(!strncmp("PING", cmd, 4) && txt) {
snprintf(bufout, sizeof bufout, "PONG %s\r\n", txt);
@ -144,12 +135,28 @@ parsesrv(char *msg) {
}
else {
snprintf(bufout, sizeof bufout, ">< %s: %s", cmd, txt ? txt : "");
pout(usr, bufout);
printl(usr, bufout);
if(!strncmp("NICK", cmd, 4) && !strncmp(usr, nick, sizeof nick) && txt)
strncpy(nick, txt, sizeof nick);
}
}
int
readl(int fd, unsigned int len, char *buf) {
unsigned int i = 0;
char c;
do {
if(read(fd, &c, sizeof(char)) != sizeof(char))
return -1;
buf[i++] = c;
}
while(c != '\n' && i < len);
buf[i - 1] = 0;
return 0;
}
int
main(int argc, char *argv[]) {
int i;
@ -174,30 +181,30 @@ main(int argc, char *argv[]) {
if(++i < argc) password = argv[i];
}
else if(!strncmp(argv[i], "-v", 3))
eprint("sic-"VERSION", (C)opyright MMVI Anselm R. Garbe\n");
die("sic-"VERSION", © 2005-2008 Anselm R Garbe, Nico Golde\n");
else
eprint("usage: sic [-h host] [-p port] [-n nick] [-k keyword] [-v]\n");
die("usage: sic [-h host] [-p port] [-n nick] [-k keyword] [-v]\n");
/* init */
if((srv = socket(AF_INET, SOCK_STREAM, 0)) < 0)
eprint("sic: cannot connect host '%s'\n", host);
die("error: cannot connect host '%s'\n", host);
if(NULL == (hp = gethostbyname(host)))
eprint("sic: cannot resolve hostname '%s'\n", host);
die("error: cannot resolve hostname '%s'\n", host);
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
if(connect(srv, (struct sockaddr *) &addr, sizeof(struct sockaddr_in))) {
close(srv);
eprint("sic: cannot connect host '%s'\n", host);
die("error: cannot connect host '%s'\n", host);
}
/* login */
if(password)
snprintf(bufout, sizeof bufout,
"PASS %s\r\nNICK %s\r\nUSER %s localhost %s :%s\r\n",
password, nick, nick, host, nick);
"PASS %s\r\nNICK %s\r\nUSER %s localhost %s :%s\r\n",
password, nick, nick, host, nick);
else
snprintf(bufout, sizeof bufout, "NICK %s\r\nUSER %s localhost %s :%s\r\n",
nick, nick, host, nick);
nick, nick, host, nick);
write(srv, bufout, strlen(bufout));
snprintf(ping, sizeof ping, "PING %s\r\n", host);
channel[0] = 0;
@ -213,23 +220,23 @@ main(int argc, char *argv[]) {
if(i < 0) {
if(errno == EINTR)
continue;
eprint("sic: error on select()");
die("error: error on select()");
}
else if(i == 0) {
if(time(NULL) - trespond >= PINGTIMEOUT)
eprint("sic shutting down: parse timeout");
die("error: sic shutting down: parse timeout");
write(srv, ping, strlen(ping));
continue;
}
if(FD_ISSET(srv, &rd)) {
if(getline(srv, sizeof bufin, bufin) == -1)
eprint("sic: remote host closed connection");
if(readl(srv, sizeof bufin, bufin) == -1)
die("error: remote host closed connection");
parsesrv(bufin);
trespond = time(NULL);
}
if(FD_ISSET(0, &rd)) {
if(getline(0, sizeof bufin, bufin) == -1)
eprint("sic: broken pipe");
if(readl(0, sizeof bufin, bufin) == -1)
die("error: broken pipe");
parsein(bufin);
}
}