wm: ircd

Download patch

ref: 19211dfc0b93392592a1a9720c37e2af67e2c1d6
parent: e5ebb78416d5860c502486f60896812696b718eb
author: mkf <mkf@cloud9p.org>
date: Tue Sep 26 04:24:18 EDT 2023

use actual numerical code for replies and errors

--- a/ircd.c
+++ b/ircd.c
@@ -13,27 +13,32 @@
 };
 
 enum {
-	Ping = 1,
-	Pong,
-	Welcome,
-	Join,
-	Part,
-	Quit,
-	Privmsg,
-	Nick,
-	Nosuchnick,
-	Erroneousnick,
-	Nickinuse,
-	Notonchannel,
-	Topic,
-	Notopic,
-	Rpltopic,
-	Rpltopicwho,
-	Notregistered,
-	Alreadyregistered,
-	Nosuchchannel,
-	Cantsendtochannel,
-	Unknowncmd,
+	/* commands */
+	PING = 1000,
+	PONG,
+	JOIN,
+	PART,
+	QUIT,
+	PRIVMSG,
+	NICK,
+	TOPIC,
+
+	/* numericals */
+	RPL_WELCOME = 001,
+
+	RPL_NOTOPIC = 331,
+	RPL_TOPIC = 332,
+	RPL_TOPICWHOTIME = 333,
+
+	ERR_NOSUCHNICK = 401,
+	ERR_NOSUCHCHANNEL = 403,
+	ERR_CANNOTSENDTOCHAN = 404,
+	ERR_UNKNOWNCOMMAND = 421,
+	ERR_ERRONEUSNICKNAME = 432,
+	ERR_NICKNAMEINUSE = 433,
+	ERR_NOTONCHANNEL = 442,
+	ERR_NOTREGISTERED = 451,
+	ERR_ALREADYREGISTERED = 461,
 };
 
 typedef struct Item Item;
@@ -315,14 +320,39 @@
 
 	n = sizeof(buf);
 	switch(r->code){
-	case Ping:
+	case PING:
 		n = snprint(buf, n, "PING :%s\r\n", servername);
 		break;
-	case Pong:
+	case PONG:
 		/* r->argv is: pong */
 		n = snprint(buf, n, ":%s PONG %s\r\n", servername, r->argv[0]);
 		break;
-	case Welcome:
+	case JOIN:
+		/* r->argv is: joiner, channel */
+		n = snprint(buf, n, ":%s JOIN %s\r\n", r->argv[0], r->argv[1]);
+		break;
+	case PART:
+		/* r->argv is: parter, channel, reason */
+		n = snprint(buf, n, ":%s PART %s :%s\r\n", r->argv[0], r->argv[1], r->argv[2]);
+		break;
+	case QUIT:
+		/* r->argv is: quitter, reason */
+		n = snprint(buf, n, ":%s QUIT :%s\r\n", r->argv[0], r->argv[1]);
+		break;
+	case PRIVMSG:
+		/* r->argv is: sender, target, message */
+		n = snprint(buf, n, ":%s PRIVMSG %s :%s\r\n", r->argv[0], r->argv[1], r->argv[2]);
+		break;
+	case NICK:
+		/* r->argv is: old prefix, new nick */
+		n = snprint(buf, n, ":%s NICK %s\r\n", r->argv[0], r->argv[1]);
+		break;
+	case TOPIC:
+		/* r->argv is: setter, channel, topic */
+		n = snprint(buf, n, ":%s TOPIC %s :%s\r\n", r->argv[0], r->argv[1], r->argv[2]);
+		break;
+
+	case RPL_WELCOME:
 		n = snprint(buf, n,
 		":%s 001 %s :Welcome %s\r\n"
 		":%s 002 %s :Your host is %s, running version %s\r\n" 
@@ -336,82 +366,60 @@
 		servername, c->nick, /* 004, we don't support anything yet */
 		servername, c->nick, Nicklen, Chanlen); /* 005 */
  		break;
+
+	case RPL_NOTOPIC:
+		/* r->argv is: channel */
+		n = snprint(buf, n, ":%s 331 %s %s :No topic is set\r\n", servername, c->nick, r->argv[0]);
 		break;
-	case Join:
-		/* r->argv is: joiner, channel */
-		n = snprint(buf, n, ":%s JOIN %s\r\n", r->argv[0], r->argv[1]);
+	case RPL_TOPIC:
+		/* r->argv is: channel, topic */
+		n = snprint(buf, n, ":%s 332 %s %s :%s\r\n", servername, c->nick, r->argv[0], r->argv[1]);
 		break;
-	case Part:
-		/* r->argv is: parter, channel, reason */
-		n = snprint(buf, n, ":%s PART %s :%s\r\n", r->argv[0], r->argv[1], r->argv[2]);
+	case RPL_TOPICWHOTIME:
+		/* r->argv is: channel, topicwhotime */
+		n = snprint(buf, n, ":%s 333 %s %s :%s\r\n", servername, c->nick, r->argv[0], r->argv[1]);
 		break;
-	case Quit:
-		/* r->argv is: quitter, reason */
-		n = snprint(buf, n, ":%s QUIT :%s\r\n", r->argv[0], r->argv[1]);
-		break;
-	case Privmsg:
-		/* r->argv is: sender, target, message */
-		n = snprint(buf, n, ":%s PRIVMSG %s :%s\r\n", r->argv[0], r->argv[1], r->argv[2]);
-		break;
-	case Nick:
-		/* r->argv is: old prefix, new nick */
-		n = snprint(buf, n, ":%s NICK %s\r\n", r->argv[0], r->argv[1]);
-		break;
-	case Nosuchnick:
+
+	case ERR_NOSUCHNICK:
 		/* r->argv[0] is nick/channel */
 		n = snprint(buf, n, ":%s 401 %s %s :No such nick/channel\r\n", servername, c->nick, r->argv[0]);
 		break;
-	case Nosuchchannel:
+	case ERR_NOSUCHCHANNEL:
 		/* r->argv is: channel */
 		n = snprint(buf, n, ":%s 403 %s %s :No such channel\r\n", servername, c->nick, r->argv[0]);
 		break;
-	case Cantsendtochannel:
+	case ERR_CANNOTSENDTOCHAN:
 		/* r->argv is: channel */
 		n = snprint(buf, n, ":%s 404 %s %s :Cannot send to channel\r\n", servername, c->nick, r->argv[0]);
 		break;
-	case Erroneousnick:
+	case ERR_UNKNOWNCOMMAND:
+		/* r->argv is: command */
+		n = snprint(buf, n, ":%s 421 %s %s :Unknown command\r\n", servername, c->nick, r->argv[0]);
+		break;
+	case ERR_ERRONEUSNICKNAME:
 		/* r->argv[0] is nick */
-		n = snprint(buf, n, ":%s 432 %s %s :Erroneous nickname\r\n", servername, c->nick, r->argv[0]);
+		n = snprint(buf, n, ":%s 432 %s %s :Erroneus nickname\r\n", servername, c->nick, r->argv[0]);
 		break;
-	case Nickinuse:
+	case ERR_NICKNAMEINUSE:
 		/* r->argv[0] is nick */
 		n = snprint(buf, n, ":%s 433 %s %s :Nickname is already in use\r\n", servername, c->nick, r->argv[0]);
 		break;
-	case Notonchannel:
+	case ERR_NOTONCHANNEL:
 		/* r->argv[0] is channel */
 		n = snprint(buf, n, ":%s 442 %s %s :You're not on that channel\r\n", servername, c->nick, r->argv[0]);
 		break;
-	case Topic:
-		/* r->argv is: setter, channel, topic */
-		n = snprint(buf, n, ":%s TOPIC %s :%s\r\n", r->argv[0], r->argv[1], r->argv[2]);
-		break;
-	case Rpltopic:
-		/* r->argv is: channel, topic */
-		n = snprint(buf, n, ":%s 332 %s %s :%s\r\n", servername, c->nick, r->argv[0], r->argv[1]);
-		break;
-	case Rpltopicwho:
-		/* r->argv is: channel, topicwhotime */
-		n = snprint(buf, n, ":%s 333 %s %s :%s\r\n", servername, c->nick, r->argv[0], r->argv[1]);
-		break;
-	case Notopic:
-		/* r->argv is: channel */
-		n = snprint(buf, n, ":%s 331 %s %s :No topic is set\r\n", servername, c->nick, r->argv[0]);
-		break;
-	case Notregistered:
+	case ERR_NOTREGISTERED:
 		n = snprint(buf, n, ":%s 451 %s :Connection not registered\r\n", servername, c->nick);
 		break;
-	case Alreadyregistered:
+	case ERR_ALREADYREGISTERED:
 		n = snprint(buf, n, ":%s 461 %s :Connection already registered", servername, c->nick);
 		break;
-	case Unknowncmd:
-		/* r->argv is: command */
-		n = snprint(buf, n, ":%s 421 %s %s :Unknown command\r\n", servername, c->nick, r->argv[0]);
-		break;	
+
 	default:
 		fprint(2, "%d: reply: unknown code %d\n", c->fd, r->code);
 		assert(0);
 	}
-	if(r->code != Ping && r->code != Pong)
+	if(r->code != PING && r->code != PONG)
 		dprint("%d: reply: %s", c->fd, buf);
 
 	ewrite(c->fd, buf, n);
@@ -516,7 +524,7 @@
 
 	if(c->user != nil)
 	{
-		r.code = Alreadyregistered;
+		r.code = ERR_ALREADYREGISTERED;
 		reply(c, &r);
 		return 1;
 	}
@@ -533,7 +541,7 @@
 	if(c->nick == nil)
 		return 1;
 
-	r.code = Welcome;
+	r.code = RPL_WELCOME;
 	reply(c, &r);
 	return 1;
 }
@@ -552,7 +560,7 @@
 
 	if(!validnick(nick)){
 		dprint("%d: invalid nick %s\n", c->fd, nick);
-		r.code = Erroneousnick;
+		r.code = ERR_ERRONEUSNICKNAME;
 		r.argv[0] = nick;
 		reply(c, &r);
 		return 1;
@@ -565,13 +573,13 @@
 	qlock(&clock);
 	if(nickinuse(nick)){
 		dprint("%d: nick %s is in use\n", c->fd, nick);
-		r.code = Nickinuse;
+		r.code = ERR_NICKNAMEINUSE;
 		r.argv[0] = nick;
 		reply(c, &r);
 	}else{
 		/* nick change */
 		if(c->nick != nil){
-			r.code = Nick;
+			r.code = NICK;
 			r.argv[0] = c->prefix; /* old prefix */
 			r.argv[1] = nick;      /* new nick */
 			replychans(c, &r, c);
@@ -588,7 +596,7 @@
 	qunlock(&clock);
 
 	if(first && c->user != nil){
-		r.code = Welcome;
+		r.code = RPL_WELCOME;
 		reply(c, &r);
 	}
 	return 1;
@@ -661,7 +669,7 @@
 	if(ch->topic == nil)
 		return;
 
-	r.code = Rpltopic;
+	r.code = RPL_TOPIC;
 	r.argv[0] = ch->name;
 	r.argv[1] = ch->topic;
 	reply(c, &r);
@@ -669,7 +677,7 @@
 	if(ch->topicwho == nil)
 		return;
 
-	r.code = Rpltopicwho;
+	r.code = RPL_TOPICWHOTIME;
 	r.argv[1] = ch->topicwho;
 	reply(c, &r);
 }
@@ -694,7 +702,7 @@
 	if(ch == nil){
 		if(channels.size == Maxchannels){
 			fprint(2, "%d: Maxchannels reached\n", c->fd);
-			r.code = Nosuchchannel;
+			r.code = ERR_NOSUCHCHANNEL;
 			r.argv[0] = chan;
 			reply(c, &r);
 			goto end;
@@ -701,7 +709,7 @@
 		}
 		if(!validchan(chan)){
 			dprint("%d: invalid chan %s\n", c->fd, chan);
-			r.code = Nosuchchannel;
+			r.code = ERR_NOSUCHCHANNEL;
 			r.argv[0] = chan;
 			reply(c, &r);
 			goto end;
@@ -711,7 +719,7 @@
 	add(&ch->members, c);
 	add(&c->channels, ch);	
 	dprint("%d: join chan=%s\n", c->fd, chan);
-	r.code = Join;
+	r.code = JOIN;
 	r.argv[0] = c->prefix;
 	r.argv[1] = chan;
 	replychan(ch, &r, nil);
@@ -731,12 +739,12 @@
 	qlock(&clock);
 	ch = getchan(&c->channels, chan);
 	if(ch == nil){
-		r.code = Notonchannel;
+		r.code = ERR_NOTONCHANNEL;
 		r.argv[0] = chan;
 		reply(c, &r);
 	}else{
 		dprint("%d: part chan=%s\n", c->fd, chan);
-		r.code = Part;
+		r.code = PART;
 		r.argv[0] = c->prefix;
 		r.argv[1] = chan;
 		r.argv[2] = reason;
@@ -757,7 +765,7 @@
 	Reply r;
 
 	if(!joined(c, chan)){
-		r.code = Notonchannel;
+		r.code = ERR_NOTONCHANNEL;
 		r.argv[0] = chan;
 		reply(c, &r);
 		return 1;
@@ -770,7 +778,7 @@
 	}
 	if(topic == nil){ /* get */
 		if(ch->topic == nil){
-			r.code = Notopic;
+			r.code = RPL_NOTOPIC;
 			r.argv[0] = chan;
 			reply(c, &r);
 		}else{
@@ -785,7 +793,7 @@
 			free(ch->topicwho);
 		ch->topicwho = smprint("%s %ld", c->prefix, time(0));
 
-		r.code = Topic;
+		r.code = TOPIC;
 		r.argv[0] = c->prefix;
 		r.argv[1] = chan;
 		r.argv[2] = topic;
@@ -802,7 +810,7 @@
 	Reply r;
 
 	dprint("%d: quit: %s\n", c->fd, reason);
-	r.code = Quit;
+	r.code = QUIT;
 	r.argv[0] = c->prefix;
 	r.argv[1] = reason;
 	qlock(&clock);
@@ -817,7 +825,7 @@
 	Chan *ch;
 	Reply r;
 
-	r.code = Privmsg;
+	r.code = PRIVMSG;
 	r.argv[0] = c->prefix; /* from */
 	r.argv[1] = target;
 	r.argv[2] = msg;
@@ -829,12 +837,12 @@
 			if(joined(c, target))
 				replychan(ch, &r, c);
 			else{
-				r.code = Cantsendtochannel;
+				r.code = ERR_CANNOTSENDTOCHAN;
 				r.argv[0] = target;
 				reply(c, &r);
 			}
 		}else{
-			r.code = Nosuchnick;
+			r.code = ERR_NOSUCHNICK;
 			r.argv[0] = target;
 			reply(c, &r);
 		}
@@ -843,7 +851,7 @@
 		if(tc != nil)
 			reply(tc, &r);
 		else{
-			r.code = Nosuchnick;
+			r.code = ERR_NOSUCHNICK;
 			r.argv[0] = target;
 			reply(c, &r);
 		}
@@ -968,7 +976,7 @@
 
 	if(strcmp(cmd, "PING") == 0){
 		r.argv[0] = strtok(0, " \r"); /* pong */
-		r.code = Pong;
+		r.code = PONG;
 		reply(c, &r);
 		return 1;
 	}
@@ -994,7 +1002,7 @@
 	}
 
 	if(c->prefix == nil){ /* not registered */
-		r.code = Notregistered;
+		r.code = ERR_NOTREGISTERED;
 		reply(c, &r);
 		return 1;
 	}
@@ -1055,7 +1063,7 @@
 		tmp = strtok(0, " \r"); /* nicks */
 		return whois(c, tmp);
 	}
-	r.code = Unknowncmd;
+	r.code = ERR_UNKNOWNCOMMAND;
 	r.argv[0] = cmd;
 	reply(c, &r);
 	return 1;
@@ -1100,7 +1108,7 @@
 		quit(c, "timed out");
 		return 1;
 	}
-	r.code = Ping;
+	r.code = PING;
 	reply(c, &r);
 	c->pings++;
 	return 0;