applied anonymous cleanup patch, thanks anonymous!

This commit is contained in:
Anselm R Garbe 2010-04-17 11:12:54 +01:00
parent dad75aa6ba
commit 75d42255f2
2 changed files with 66 additions and 72 deletions

87
sic.c
View File

@ -55,76 +55,77 @@ privmsg(char *channel, char *msg) {
} }
static void static void
parsein(char *msg) { parsein(char *s) {
char *p; char c, *p;
char c;
if(msg[0] == '\0') if(s[0] == '\0')
return; return;
msg = ctok(&msg, '\n'); skip(s, '\n');
if(msg[0] != ':') { if(s[0] != ':') {
privmsg(channel, msg); privmsg(channel, s);
return; return;
} }
c = *++msg; c = *++s;
if(!c || !isspace(msg[1])) if(c != '\0' && isspace(s[1])) {
sout("%s", msg); p = s + 2;
else {
if(msg[1])
msg += 2;
switch(c) { switch(c) {
case 'j': case 'j':
sout("JOIN %s", msg); sout("JOIN %s", p);
if(channel[0] == '\0') if(channel[0] == '\0')
strlcpy(channel, msg, sizeof channel); strlcpy(channel, p, sizeof channel);
break; return;
case 'l': case 'l':
p = tok(&msg); s = eat(p, isspace, 1);
p = eat(s, isspace, 0);
if(!*s)
s = channel;
if(*p)
*p++ = '\0';
if(!*p) if(!*p)
p = channel; p = "sic - 250 LOC are too much!";
if(!*msg) sout("PART %s :%s", s, p);
msg = "sic - 250 LOC are too much!"; return;
sout("PART %s :%s", p, msg);
break;
case 'm': case 'm':
p = tok(&msg); s = eat(p, isspace, 1);
privmsg(p, msg); p = eat(s, isspace, 0);
break; if(*p)
*p++ = '\0';
privmsg(s, p);
return;
case 's': case 's':
strlcpy(channel, msg, sizeof channel); strlcpy(channel, p, sizeof channel);
break; return;
default:
sout("%c %s", c, msg);
break;
} }
} }
sout("%s", s);
} }
static void static void
parsesrv(char *msg) { parsesrv(char *cmd) {
char *cmd, *p, *usr, *txt; char *usr, *par, *txt;
usr = host; usr = host;
if(!msg || !*msg) if(!cmd || !*cmd)
return; return;
if(msg[0] == ':') { if(cmd[0] == ':') {
msg++; usr = cmd + 1;
p = tok(&msg); cmd = skip(usr, ' ');
if(!*msg) if(cmd[0] == '\0')
return; return;
usr = ctok(&p, '!'); skip(usr, '!');
} }
txt = ctok(&msg, '\r'); skip(cmd, '\r');
msg = ctok(&txt, ':'); par = skip(cmd, ' ');
cmd = tok(&msg); txt = skip(par, ':');
trim(par);
if(!strcmp("PONG", cmd)) if(!strcmp("PONG", cmd))
return; return;
if(!strcmp("PRIVMSG", cmd)) if(!strcmp("PRIVMSG", cmd))
pout(msg, "<%s> %s", usr, txt); pout(par, "<%s> %s", usr, txt);
else if(!strcmp("PING", cmd)) else if(!strcmp("PING", cmd))
sout("PONG %s", txt); sout("PONG %s", txt);
else { else {
pout(usr, ">< %s: %s", cmd, txt); pout(usr, ">< %s (%s): %s", cmd, par, txt);
if(!strcmp("NICK", cmd) && !strcmp(usr, nick)) if(!strcmp("NICK", cmd) && !strcmp(usr, nick))
strlcpy(nick, txt, sizeof nick); strlcpy(nick, txt, sizeof nick);
} }

51
util.c
View File

@ -47,35 +47,28 @@ strlcpy(char *to, const char *from, int l) {
to[l-1] = '\0'; to[l-1] = '\0';
} }
static char *
eat(char *s, int (*p)(int), int r) {
while(s != '\0' && p(*s) == r)
s++;
return s;
}
static char*
skip(char *s, char c) {
while(*s != c && *s != '\0')
s++;
if (*s != '\0')
*s++ = '\0';
return s;
}
static void static void
eat(char **s, int (*p)(int), int r) { trim(char *s) {
char *q; char *e;
for(q = *s; *q && p(*q) == r; q++) e = s + strlen(s) - 1;
; while (isspace(*e) && e > s)
*s = q; e--;
} *(e + 1) = '\0';
static char*
tok(char **s) {
char *p;
eat(s, isspace, 1);
p = *s;
eat(s, isspace, 0);
if(**s)
*(*s)++ = '\0';
return p;
}
static char*
ctok(char **s, int c) {
char *p, *q;
q = *s;
for(p = q; *p && *p != c; p++)
;
if(*p) *p++ = '\0';
*s = p;
return q;
} }