added argv stuff

This commit is contained in:
arg@10ksloc.org 2006-07-20 09:33:14 +02:00
parent d170268748
commit bb52356d15

54
sic.c
View File

@ -19,12 +19,11 @@
enum { Tnick, Tuser, Tcmd, Tchan, Targ, Ttext, Tlast }; enum { Tnick, Tuser, Tcmd, Tchan, Targ, Ttext, Tlast };
/* CUSTOMIZE */ /* CUSTOMIZE */
static const char *ping = "PING irc.oftc.net\r\n"; static char *server = "irc.oftc.net";
static const char *host = "irc.oftc.net"; static int port = 6667;
static const int port = 6667; static char *nick = "arg";
static const char *nick = "arg"; static char *fullname = "Anselm R. Garbe";
static const char *fullname = "Anselm R. Garbe"; static char *password = NULL;
static const char *password = NULL;
static char bufin[MAXMSG], bufout[MAXMSG]; static char bufin[MAXMSG], bufout[MAXMSG];
static char channel[256]; static char channel[256];
@ -146,7 +145,7 @@ parsesrv(char *msg)
/* /*
<bufout> ::= [':' <prefix> <SPACE> ] <command> <params> <crlf> <bufout> ::= [':' <prefix> <SPACE> ] <command> <params> <crlf>
<prefix> ::= <servername> | <nick> [ '!' <user> ] [ '@' <host> ] <prefix> ::= <servername> | <nick> [ '!' <user> ] [ '@' <server> ]
<command> ::= <letter> { <letter> } | <number> <number> <number> <command> ::= <letter> { <letter> } | <number> <number> <number>
<SPACE> ::= ' ' { ' ' } <SPACE> ::= ' ' { ' ' }
<params> ::= <SPACE> [ ':' <trailing> | <middle> <params> ] <params> ::= <SPACE> [ ':' <trailing> | <middle> <params> ]
@ -187,7 +186,7 @@ parsesrv(char *msg)
return; return;
} else if(!argv[Tnick] || !argv[Tuser]) { /* server command */ } else if(!argv[Tnick] || !argv[Tuser]) { /* server command */
snprintf(bufout, sizeof(bufout), "%s", argv[Ttext] ? argv[Ttext] : ""); snprintf(bufout, sizeof(bufout), "%s", argv[Ttext] ? argv[Ttext] : "");
pout((char *)host, bufout); pout(server, bufout);
return; return;
} else if(!strncmp("ERROR", argv[Tcmd], 6)) } else if(!strncmp("ERROR", argv[Tcmd], 6))
snprintf(bufout, sizeof(bufout), "-!- error %s", snprintf(bufout, sizeof(bufout), "-!- error %s",
@ -240,14 +239,31 @@ main(int argc, char *argv[])
struct timeval tv; struct timeval tv;
struct hostent *hp; struct hostent *hp;
struct sockaddr_in addr = { 0 }; struct sockaddr_in addr = { 0 };
char ping[256];
fd_set rd; fd_set rd;
for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) { for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
switch (argv[i][1]) { switch (argv[i][1]) {
default: default:
fputs("usage: sic [-v]\n", stderr); fputs("usage: sic [-s server] [-p port] [-n nick]"
" [-k keyword] [-f fullname] [-v]\n", stderr);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
break; break;
case 's':
server = argv[i++];
break;
case 'p':
port = atoi(argv[i++]);
break;
case 'n':
nick = argv[i++];
break;
case 'k':
password = argv[i++];
break;
case 'f':
fullname = argv[i++];
break;
case 'v': case 'v':
fputs("sic-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout); fputs("sic-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
@ -257,29 +273,30 @@ main(int argc, char *argv[])
/* init */ /* init */
if((srv = socket(AF_INET, SOCK_STREAM, 0)) < 0) { if((srv = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
fprintf(stderr, "sic: cannot connect server '%s'\n", host); fprintf(stderr, "sic: cannot connect server '%s'\n", server);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
hp = gethostbyname(host); hp = gethostbyname(server);
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_port = htons(port); addr.sin_port = htons(port);
bcopy(hp->h_addr, &addr.sin_addr, hp->h_length); bcopy(hp->h_addr, &addr.sin_addr, hp->h_length);
if(connect(srv, (struct sockaddr *) &addr, sizeof(struct sockaddr_in))) { if(connect(srv, (struct sockaddr *) &addr, sizeof(struct sockaddr_in))) {
close(srv); close(srv);
fprintf(stderr, "sic: cannot connect server '%s'\n", host); fprintf(stderr, "sic: cannot connect server '%s'\n", server);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/* login */ /* login */
if(password) if(password)
snprintf(bufout, sizeof(bufout), snprintf(bufout, sizeof(bufout),
"PASS %s\r\nNICK %s\r\nUSER %s localhost %s :%s\r\n", "PASS %s\r\nNICK %s\r\nUSER %s localserver %s :%s\r\n",
password, nick, nick, host, fullname ? fullname : nick); password, nick, nick, server, fullname ? fullname : nick);
else else
snprintf(bufout, sizeof(bufout), "NICK %s\r\nUSER %s localhost %s :%s\r\n", snprintf(bufout, sizeof(bufout), "NICK %s\r\nUSER %s localserver %s :%s\r\n",
nick, nick, host, fullname ? fullname : nick); nick, nick, server, fullname ? fullname : nick);
write(srv, bufout, strlen(bufout)); write(srv, bufout, strlen(bufout));
snprintf(ping, sizeof(ping), "PING %s\r\n", server);
channel[0] = 0; channel[0] = 0;
setbuf(stdout, NULL); /* unbuffered stdout */ setbuf(stdout, NULL); /* unbuffered stdout */
for(;;) { for(;;) {
@ -296,7 +313,7 @@ main(int argc, char *argv[])
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} else if(i == 0) { } else if(i == 0) {
if(time(NULL) - trespond >= PINGTIMEOUT) { if(time(NULL) - trespond >= PINGTIMEOUT) {
pout((char *)host, "-!- sic shutting down: parseing timeout"); pout(server, "-!- sic shutting down: parseing timeout");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
write(srv, ping, strlen(ping)); write(srv, ping, strlen(ping));
@ -304,13 +321,14 @@ main(int argc, char *argv[])
} }
if(FD_ISSET(srv, &rd)) { if(FD_ISSET(srv, &rd)) {
if(getline(srv, sizeof(bufin), bufin) == -1) { if(getline(srv, sizeof(bufin), bufin) == -1) {
perror("sic: remote host closed connection"); perror("sic: remote server closed connection");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
parsesrv(bufin); parsesrv(bufin);
trespond = time(NULL); trespond = time(NULL);
} }
if(FD_ISSET(0, &rd)) { if(FD_ISSET(0, &rd)) {
fprintf(stdout, "%s> ", channel);
if(getline(0, sizeof(bufin), bufin) == -1) { if(getline(0, sizeof(bufin), bufin) == -1) {
perror("sic: broken pipe"); perror("sic: broken pipe");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);