Write a functioning bare-bones IRCd A bit of an unusual one, but hey, why not? :)

The objective: Write a functioning IRC daemon in your language of choice that provides barebones functionality, in as few characters as possible. As long as it fulfills the criteria below, it does not have to fully comply with the IRC RFCs (this would make the challenge significantly less fun), it just needs to work.

Requirements:


Clients must be able to connect on port 6667 and use it. At least irssi and XChat must be able to connect to it successfully with a default configuration.
Users must be able to specify their own nickname, change it while already connected, join channels, leave channels, and exit cleanly (ie. QUIT).
Channels should be created like normal - joining a channel with no users in it 'creates' it. They do not have to be persistent.
Users must be able to send both channel messages and private messages (ie. to other users).
The WHOIS command must be implemented, as well as PING/PONG, LIST, and NAMES (mostly in order to keep clients happy).


Restrictions:


You may not use any third-party libraries (that includes non-core evented I/O libraries). Only standard libraries that come with the platform you use, are permitted.
If your implementation includes explicit support for IRC in the standard library, you may not use that either. Standard-library network functionality is, of course, fine.
Your submission must be able to run independently. No being clever with mIRC scripting :)


Modes, kicks, etc. do not have to be implemented (unless required to make it work with the above clients). SSL isn't necessary either. Just the above barebones functionality, to keep the challenge short and fun.

More information on what IRC is here, RFCs are 1459 and 2812 (I can't link to them directly due to my lack of reputation).
Shortest functional and requirements-compliant submission wins!
C++ (partially golfed) 5655 (with CRLF counting for 1)
This compiles in VS 2013 (uses auto, lambdas and winsock)  It seemed to be working before I golfed it so unless I mucked it up it should still be ok.  One of the reasons it is so big is that the numeric replies I am returning include the text specified in the RFC - I don't know if that is necessary or not.  I tested it with KVirc because it runs portably (not allowed to install software on my PC!)  KVirc seems to work with my server but I don't know about other clients - I did what I thought the RFC said but a lot of it is underspecified so hopefully I understood it right.
The server handles DIE, KILL, NICK, USER, MODE, WHOIS, WHO, JOIN, PART, TOPIC, LIST, NAMES, PRIVMSG, USERS, PING, PONG and QUIT to varying degrees.  For most of them I return the required responses including doing most of the checks needed to return the specified error replies.  For some of them I cheat:

USERS always returns 446 &quot;USERS has been disabled&quot;
the channel MODE message always returns 477 &quot;Channel doesn't support modes&quot;
the user MODE message works properly but the flags are not used by the other commands

I figure it is only partially golfed because I am not very good at golfing so if you see something big please edit the answer and fix it.
Here is the golfed version
#include&lt;time.h&gt;
#include&lt;map&gt;
#include&lt;list&gt;
#include&lt;vector&gt;
#include&lt;string&gt;
#include&lt;sstream&gt;
#include&lt;iostream&gt;
#include&lt;algorithm&gt;
#include&lt;winSock2.h&gt;
#pragma comment(lib,&quot;ws2_32.lib&quot;)
#define P m.p[0]
#define Q m.p[1]
#define E SOCKET_ERROR
#define I INVALID_SOCKET
#define T c_str()
#define H second
#define Y first
#define S string
#define W stringstream
#define G else
#define J G if
#define A auto
#define Z bool
#define B empty()
#define K return
#define N 513
#define X(n,x)if(x){r=n;goto f;};
#define U(x,i)for(A i=x.begin();i !=x.end();++i)
#define L(x)U(x,i)
#define V(x)for(A i=x.begin();i!=--x.end();++i)
#define M(x)FD_ZERO(&amp;x);FD_SET(t.s,&amp;x);L(l){FD_SET(i-&gt;s,&amp;x);}
#define R(a,b,...){M v={a,b,{__VA_ARGS__}};w(d,v);}
#define F(x)}J(!_stricmp(m.c.T,x)){
using namespace std;struct C{S t;list&lt;S&gt;n;};struct M{S f;S c;vector&lt;S&gt;p;};struct D{SOCKET s;SOCKADDR_IN a;int m,l,i;char b[N];S n,u,h,r;time_t p,q;};map&lt;S,C&gt;c;list&lt;D&gt;l;void w(D d,M m);void x(D&amp;t,S r,Z n){L(c)i-&gt;H.n.remove(t.n);L(l){A d=*i;if(d.n!=t.n)R(d.n,&quot;QUIT&quot;,t.n,r)J(n)R(&quot;&quot;,&quot;ERROR&quot;,&quot;QUIT&quot;,r)}closesocket(t.s);t.s=I;}void w(D d,M m){S s=(!m.p.B?&quot;:&quot;+m.f+&quot; &quot;:&quot;&quot;)+m.c;V(m.p)s+=&quot; &quot;+*i;s+=&quot; :&quot;+*m.p.rbegin()+&quot;\r\n&quot;;int c=0;do{int b=send(d.s,s.T+c,s.size()-c,0);if(b&gt;0)c+=b;G x(d,&quot;send error&quot;,0);}while(s.size()-c&gt;0);}Z e(D&amp;d,M m){A z=m.p.size();if(!_stricmp(m.c.T,&quot;DIE&quot;)){K 1;F(&quot;KILL&quot;)if(z&lt;1)R(&quot;&quot;,&quot;461&quot;,d.n,&quot;USER&quot;,&quot;Not enough parameters&quot;)G{Z f=0;L(l)if(i-&gt;n==P){f=1;x((*i),P,1);}if(f==0)R(&quot;&quot;,&quot;401&quot;,d.n,P,&quot;No such nick/channel&quot;)}F(&quot;NICK&quot;)if(z&lt;1)R(&quot;&quot;,&quot;431&quot;,d.n,&quot;No nickname given&quot;)G{Z f=0;L(l)if(i-&gt;n==P)f=1;if(f==1)R(&quot;&quot;,&quot;433&quot;,d.n,&quot;Nickname is already in use&quot;)G d.n=P;}F(&quot;USER&quot;)if(z&lt;4)R(&quot;&quot;,&quot;461&quot;,d.n,&quot;USER&quot;,&quot;Not enough parameters&quot;)G{Z f=0;L(l)if(i-&gt;u==P)f=1;if(f==1)R(&quot;&quot;,&quot;462&quot;,d.n,&quot;Unauthorized channel (already registered)&quot;)G{d.u=P;d.m=atoi(Q.T);d.h=m.p[2];d.r=m.p[3];R(&quot;&quot;,&quot;001&quot;,d.n,&quot;Welcome to the Internet Relay Network &quot;+d.n+&quot;!&quot;+d.u+&quot;@&quot;+d.h)}}F(&quot;MODE&quot;)if(z&lt;1)R(&quot;&quot;,&quot;461&quot;,d.n,&quot;MODE&quot;,&quot;Not enough parameters&quot;)J(P==d.n){if(z&lt;2)R(&quot;&quot;,&quot;221&quot;,d.n,S(&quot;&quot;)+(d.m&amp;2?&quot;+w&quot;:&quot;-w&quot;)+(d.m&amp;3?&quot;+i&quot;:&quot;-i&quot;))G{A x=(147-Q[1])/14;if(Q[0]=='+'){d.m|=1&lt;&lt;x;}G{d.m&amp;=~(1&lt;&lt;x);}}}G R(&quot;&quot;,&quot;477&quot;,d.n,P,&quot;Channel doesn't support modes&quot;)F(&quot;WHOIS&quot;)if(z&lt;1)R(&quot;&quot;,&quot;431&quot;,d.n,&quot;No nickname given&quot;)G{Z f=0;L(l)if(i-&gt;n==P){f=1;R(&quot;&quot;,&quot;311&quot;,d.n,(i-&gt;n,i-&gt;u,i-&gt;h,&quot;*&quot;,i-&gt;r))}if(f==1)R(&quot;&quot;,&quot;318&quot;,d.n,P,&quot;End of WHOIS&quot;)G R(&quot;&quot;,&quot;401&quot;,d.n,P,&quot;No such nick/channel&quot;)}F(&quot;WHO&quot;)L(c[P].n)U(l,j)if(*i==j-&gt;n)R(&quot;&quot;,&quot;352&quot;,d.n,P,j-&gt;u,j-&gt;h,&quot;*&quot;,j-&gt;n,&quot;&quot;,j-&gt;r)R(&quot;&quot;,&quot;315&quot;,d.n,P,&quot;End of WHO&quot;)F(&quot;JOIN&quot;)if(z&lt;1)R(&quot;&quot;,&quot;461&quot;,d.n,&quot;JOIN&quot;,&quot;Not enough parameters&quot;)J(P==&quot;0&quot;)L(c){U(i-&gt;H.n,j)if(*j==d.n)R(&quot;&quot;,&quot;PART&quot;,i-&gt;Y,d.n)i-&gt;H.n.remove(d.n);}G{A&amp;C=c[P];Z f=0;L(C.n)if(*i==d.n){f=1;}if(f==0){C.n.push_back(d.n);R(d.n,&quot;JOIN&quot;,P)if(C.t.B)R(&quot;&quot;,&quot;331&quot;,d.n,P,&quot;No topic is set&quot;)G R(&quot;&quot;,&quot;332&quot;,d.n,P,C.t)S q;L(C.n)q+=(q.B?&quot;&quot;:&quot; &quot;)+*i;R(&quot;&quot;,&quot;353&quot;,d.n,&quot;=&quot;,P,q)R(&quot;&quot;,&quot;366&quot;,d.n,P,&quot;End of NAMES&quot;)}}F(&quot;PART&quot;)if(z&lt;1)R(&quot;&quot;,&quot;461&quot;,d.n,&quot;PART&quot;,&quot;Not enough parameters&quot;)G{Z f=0;A&amp;C=c[P];L(C.n)if(*i==d.n)f=1;C.n.remove(d.n);if(f){if(z&lt;2)m.p.push_back(d.n);R(d.n,&quot;PART&quot;,P,Q)}G R(&quot;&quot;,&quot;442&quot;,d.n,P,&quot;You're not on that channel&quot;)}F(&quot;TOPIC&quot;)if(z&lt;1)R(&quot;&quot;,&quot;461&quot;,d.n,&quot;TOPIC&quot;,&quot;Not enough parameters&quot;)G{A&amp;C=c[P];if(z&lt;2){C.t=&quot;&quot;;R(&quot;&quot;,&quot;331&quot;,d.n,P,&quot;No topic is set&quot;)}G{C.t=Q;R(&quot;&quot;,&quot;332&quot;,d.n,P,C.t)}}F(&quot;LIST&quot;)if(z&lt;1){L(c){W ss;ss&lt;&lt;i-&gt;H.n.size();R(&quot;&quot;,&quot;322&quot;,d.n,i-&gt;Y,ss.str(),i-&gt;H.t.B?&quot;No topic is set&quot;:i-&gt;H.t)}R(&quot;&quot;,&quot;323&quot;,d.n,&quot;End of LIST&quot;)}G{W ss;ss&lt;&lt;c[P].n.size();R(&quot;&quot;,&quot;322&quot;,d.n,P,ss.str(),c[P].t.B?&quot;No topic is set&quot;:c[P].t)R(&quot;&quot;,&quot;323&quot;,d.n,&quot;End of LIST&quot;)}F(&quot;NAMES&quot;)if(z&lt;1){L(c){S q;U(i-&gt;H.n,j)q+=(q.B?&quot;&quot;:&quot; &quot;)+*j;R(&quot;&quot;,&quot;353&quot;,d.n,&quot;=&quot;,i-&gt;Y,q)}R(&quot;&quot;,&quot;366&quot;,d.n,&quot;End of NAMES&quot;)}G{S q;L(c[P].n)q+=(q.B?&quot;&quot;:&quot; &quot;)+*i;R(&quot;&quot;,&quot;353&quot;,d.n,&quot;=&quot;,P,q)R(&quot;&quot;,&quot;366&quot;,d.n,P,&quot;End of NAMES&quot;)}F(&quot;PRIVMSG&quot;)if(z&lt;1)R(&quot;&quot;,&quot;411&quot;,d.n,&quot;No recipient given(PRIVMSG)&quot;)J(z&lt;2)R(&quot;&quot;,&quot;412&quot;,d.n,&quot;No text to send&quot;)G{Z f=0;A from=d.n;L(c)if(i-&gt;Y==P){f=1;U(i-&gt;H.n,k)U(l,j)if(*k==j-&gt;n){A d=*j;R(from,&quot;PRIVMSG&quot;,d.n,Q)}}if(f==0)L(l)if(i-&gt;n==P){f=1;A d=*i;R(from,&quot;PRIVMSG&quot;,d.n,Q)}if(f==0)R(&quot;&quot;,&quot;401&quot;,d.n,P,&quot;No such nick/channel&quot;)}F(&quot;USERS&quot;)R(&quot;&quot;,&quot;446&quot;,d.n,&quot;USERS has been disabled&quot;)F(&quot;PING&quot;)R(&quot;&quot;,&quot;PONG&quot;,P,Q)F(&quot;PONG&quot;)d.p=time(NULL)+60;d.q=0;F(&quot;QUIT&quot;)if(!z)m.p.push_back(d.n);x(d,P,1);}G{R(&quot;&quot;,&quot;421&quot;,d.n,m.c,&quot;Unknown command&quot;)}K 0;}M g(char*d){M m;char*n=d;while(*d!='\0'){if(m.c.B){if(*d==':'){for(;*d!='\0'&amp;&amp;*d!=' ';++d);*d='\0';m.f=n+1;n=++d;}for(;*d!='\0'&amp;&amp;*d!=' ';++d);*d='\0';m.c=n;n=++d;}J(*d==':'){for(;*d!='\0';++d);m.p.push_back(n+1);n=++d;}G{for(;*d!='\0'&amp;&amp;*d!=' ';++d);*d='\0';m.p.push_back(n);n=++d;}}K m;}int main(){int r;WSADATA u;SOCKADDR_IN la;la.sin_family=AF_INET;la.sin_port=htons(6667);la.sin_addr.s_addr=htonl(INADDR_ANY);timeval h;h.tv_sec=0;h.tv_usec=10000;fd_set rs,ws,es;D t;t.n=&quot;IRCd&quot;;X(1,(0!=WSAStartup(MAKEWORD(2,2),&amp;u)))X(2,(I==(t.s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))))X(3,(E==bind(t.s,(SOCKADDR*)&amp;la,sizeof(la))))X(4,(E==listen(t.s,SOMAXCONN)))while(1){M(rs)M(ws)M(es)X(5,(E==select(0,&amp;rs,&amp;ws,&amp;es,&amp;h)))X(6,(FD_ISSET(t.s,&amp;es)))if(FD_ISSET(t.s,&amp;rs)){D d={};d.l=sizeof(d.a);d.s=accept(t.s,(SOCKADDR*)&amp;d.a,&amp;d.l);X(7,(I==d.s))W s;s&lt;&lt;inet_ntoa(d.a.sin_addr)&lt;&lt;&quot;:&quot;&lt;&lt;ntohs(d.a.sin_port);d.n=s.str();d.p=time(NULL)+60;d.q=0;l.push_back(d);}L(l){D&amp;d=*i;if(d.p&gt;0&amp;&amp;time(NULL)&gt;d.p){R(&quot;&quot;,&quot;PING&quot;,d.n)d.p=0;d.q=time(NULL)+60;}if(d.q&gt;0&amp;&amp;time(NULL)&gt;d.q)x(d,&quot;PONG&quot;,1);if(FD_ISSET(d.s,&amp;es))x(d,&quot;select except&quot;,0);if(FD_ISSET(d.s,&amp;rs)){int b=recv(d.s,d.b+d.i,sizeof(d.b)-d.i-1&gt;0,0);if(b&gt;0)d.i+=b;G x(d,&quot;recv error&quot;,0);char*y=d.b+d.i-2;if(!strcmp(y,&quot;\r\n&quot;)){*y++='\0';*y='\0';M m=g(d.b);memset(d.b,0,N);d.i=0;if(d.p&gt;0&amp;&amp;time(NULL)&lt;d.p){d.p=time(NULL)+60;d.q=0;}if(e(d,m))X(0,1)}}}l.remove_if([](const D&amp;d){K d.s==I;});}r=0;f:L(l)x(*i,&quot;exit&quot;,0);x(t,&quot;exit&quot;,0);WSACleanup();K r;}

Here is the mostly ungolfed version (still uses some macros):
#include &lt;time.h&gt;
#include &lt;map&gt;
#include &lt;list&gt;
#include &lt;vector&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;
#include &lt;iostream&gt;
#include &lt;algorithm&gt;
#include &lt;winSock2.h&gt;
#pragma comment(lib, &quot;ws2_32.lib&quot;)
#define READ_BUFFER_SIZE 513
#define EXIT_IF(n,x) if (x) { retval=n; goto finished; };
#define LOOPX(x,it) for (auto it = x.begin(); it != x.end(); ++it)
#define LOOP(x) LOOPX(x,it)
#define LOOP2(x) for (auto it = x.begin(); it != --x.end(); ++it)
#define MAKE_SET(x) FD_ZERO(&amp;x); FD_SET(listener.socket, &amp;x); LOOP(socket_list) { FD_SET(it-&gt;socket, &amp;x); }
#define RESPOND(a, b, ...) { message response = {a, b, {__VA_ARGS__}}; tell(data, response); }
#define CASE(x) } else if (!_stricmp(msg.command.c_str(),x)) { std::cout &lt;&lt; &quot;Received &quot; &lt;&lt; x &lt;&lt; &quot; from &quot; &lt;&lt; data.nickname &lt;&lt; std::endl;
struct channel { std::string topic;  std::list&lt;std::string&gt; nicknames; };
struct message { std::string prefix; std::string command; std::vector&lt;std::string&gt; params; };
struct socket_data { SOCKET socket; SOCKADDR_IN address; int mode,address_length,read_buffer_index; char read_buffer[READ_BUFFER_SIZE]; std::string nickname,username,servername,realname; time_t ping_timer,pong_timer; };
std::map&lt;std::string,channel&gt; channels;
std::list&lt;socket_data&gt; socket_list;
void tell(socket_data data, message msg);
void disconnect(socket_data&amp; target, std::string reason, bool notify)
{
    LOOP(channels) it-&gt;second.nicknames.remove(target.nickname);
    LOOP(socket_list)
    {
        auto data = *it;
        if (data.nickname != target.nickname) RESPOND(data.nickname, &quot;QUIT&quot;, target.nickname, reason)
        else if (notify) RESPOND(&quot;&quot;, &quot;ERROR&quot;, &quot;QUIT&quot;, reason)
    }
    closesocket(target.socket);
    target.socket = INVALID_SOCKET;
    std::cout &lt;&lt; &quot;Disconnected &quot; &lt;&lt; target.nickname &lt;&lt; &quot; reason=&quot; &lt;&lt; reason &lt;&lt; std::endl;
}
void print(socket_data data, message msg, char *heading)
{
    std::cout &lt;&lt; heading &lt;&lt; &quot;:\n  &quot; &lt;&lt; inet_ntoa(data.address.sin_addr) &lt;&lt; &quot;:&quot; &lt;&lt; ntohs(data.address.sin_port) &lt;&lt; &quot;\n&quot;;
    if (!msg.prefix.empty()) std::cout &lt;&lt; &quot;  Prefix=&quot; &lt;&lt; msg.prefix &lt;&lt; &quot;\n&quot;;
    std::cout &lt;&lt; &quot;  Command=&quot; &lt;&lt; msg.command;
    int count = 0; LOOP(msg.params) std::cout &lt;&lt; &quot;\n  Param[&quot; &lt;&lt; count++ &lt;&lt; &quot;]=&quot; &lt;&lt; *it;
    std::cout &lt;&lt; std::endl;
}
void tell(socket_data data, message msg)
{
    print(data, msg, &quot;Response&quot;);

    std::string str = (!msg.prefix.empty() ? &quot;:&quot; + msg.prefix + &quot; &quot; : &quot;&quot;) + msg.command;
    LOOP2(msg.params) str += &quot; &quot; + *it;
    str += &quot; :&quot; + *msg.params.rbegin() + &quot;\r\n&quot;;

    int start = 0;
    do
    {
        int bytes = send(data.socket, str.c_str() + start, str.size() - start, 0);
        if (bytes &gt; 0) start += bytes; else disconnect(data, &quot;send error&quot;, 0);
    }
    while (str.size() - start &gt; 0);
}
bool process(socket_data &amp;data, message msg)
{
    print(data, msg, &quot;Request&quot;);

    auto size = msg.params.size();
    auto first = size&lt;1 ? &quot;&quot; : msg.params[0], second = size&lt;2 ? &quot;&quot; : msg.params[1];
    if (!_stricmp(msg.command.c_str(), &quot;DIE&quot;)) { return true;
    // and now all the cases
    CASE(&quot;KILL&quot;)    if (size&lt;1)
                        RESPOND(&quot;&quot;, &quot;461&quot;, data.nickname, &quot;USER&quot;, &quot;Not enough parameters&quot;)
                    else
                    {
                        bool found = false;
                        LOOP(socket_list) if (it-&gt;nickname == first) { found = true; disconnect((*it), first, 1); }
                        if (found == false) RESPOND(&quot;&quot;, &quot;401&quot;, data.nickname, first, &quot;No such nick/channel&quot;)
                    }
    CASE(&quot;NICK&quot;)    if (size&lt;1)
                        RESPOND(&quot;&quot;, &quot;431&quot;, data.nickname, &quot;No nickname given&quot;)
                    else
                    {
                        bool found = false;
                        LOOP(socket_list) if (it-&gt;nickname == first) found = true;
                        if (found == true) RESPOND(&quot;&quot;, &quot;433&quot;, data.nickname, &quot;Nickname is already in use&quot;)
                        else data.nickname = first;
                    }
    CASE(&quot;USER&quot;)    if (size&lt;4)
                        RESPOND(&quot;&quot;, &quot;461&quot;, data.nickname, &quot;USER&quot;, &quot;Not enough parameters&quot;)
                    else
                    {
                        bool found = false;
                        LOOP(socket_list) if (it-&gt;username == first) found = true;
                        if (found == true) RESPOND(&quot;&quot;, &quot;462&quot;, data.nickname, &quot;Unauthorized command (already registered)&quot;)
                        else
                        {
                            data.username = first; data.mode = atoi(second.c_str()); data.servername = msg.params[2];  data.realname = msg.params[3];
                            RESPOND(&quot;&quot;, &quot;001&quot;, data.nickname, &quot;Welcome to the Internet Relay Network &quot; + data.nickname + &quot;!&quot; + data.username + &quot;@&quot; + data.servername)
                        }
                    }
    CASE(&quot;MODE&quot;)    if (size&lt;1)
                        RESPOND(&quot;&quot;, &quot;461&quot;, data.nickname, &quot;MODE&quot;, &quot;Not enough parameters&quot;)
                    else if (first == data.nickname)
                    {
                        if (size &lt; 2)
                            RESPOND(&quot;&quot;, &quot;221&quot;, data.nickname, std::string(&quot;&quot;) + (data.mode &amp; 2 ? &quot;+w&quot; : &quot;-w&quot;) + (data.mode &amp; 3 ? &quot;+i&quot; : &quot;-i&quot;))
                        else
                        {
                            auto x = (147 - second[1]) / 14;
                            if (second[0] == '+')
                            {
                                data.mode |= 1 &lt;&lt; x;
                                std::cout &lt;&lt; &quot;set &quot; &lt;&lt; first &lt;&lt; &quot; mode bit &quot; &lt;&lt; x &lt;&lt; &quot;w=2, i=3&quot; &lt;&lt; std::endl;
                            }
                            else
                            {
                                data.mode &amp;= ~(1 &lt;&lt; x);
                                std::cout &lt;&lt; &quot;clear &quot; &lt;&lt; first &lt;&lt; &quot; mode bit &quot; &lt;&lt; x &lt;&lt; &quot;w=2, i=3&quot; &lt;&lt; std::endl;
                            }
                        }
                    }
                    else
                        RESPOND(&quot;&quot;, &quot;477&quot;, data.nickname, first, &quot;Channel doesn't support modes&quot;)
    CASE(&quot;WHOIS&quot;)   if (size &lt; 1)
                        RESPOND(&quot;&quot;, &quot;431&quot;, data.nickname, &quot;No nickname given&quot;)
                    else
                    {
                        bool found = false;
                        LOOP(socket_list) if (it-&gt;nickname == first) { found = true; RESPOND(&quot;&quot;, &quot;311&quot;, data.nickname, (it-&gt;nickname, it-&gt;username, it-&gt;servername, &quot;*&quot;, it-&gt;realname)) }
                        if (found == true) RESPOND(&quot;&quot;, &quot;318&quot;, data.nickname, first, &quot;End of WHOIS&quot;)
                        else RESPOND(&quot;&quot;, &quot;401&quot;, data.nickname, first, &quot;No such nick/channel&quot;)
                    }
    CASE(&quot;WHO&quot;)     LOOP(channels[first].nicknames) LOOPX(socket_list, dit) if (*it == dit-&gt;nickname)
                        RESPOND(&quot;&quot;, &quot;352&quot;, data.nickname, first, dit-&gt;username, dit-&gt;servername, &quot;*&quot;, dit-&gt;nickname, &quot;&quot;, dit-&gt;realname)
                    RESPOND(&quot;&quot;, &quot;315&quot;, data.nickname, first, &quot;End of WHO&quot;)
    CASE(&quot;JOIN&quot;)    if (size &lt; 1)
                        RESPOND(&quot;&quot;, &quot;461&quot;, data.nickname, &quot;JOIN&quot;, &quot;Not enough parameters&quot;)
                    else if (first == &quot;0&quot;)
                        LOOP(channels) { LOOPX(it-&gt;second.nicknames, dit) if (*dit == data.nickname) RESPOND(&quot;&quot;,&quot;PART&quot;, it-&gt;first, data.nickname) it-&gt;second.nicknames.remove(data.nickname); }
                    else
                    {
                        auto&amp; channel = channels[first];
                        bool found = false;
                        LOOP(channel.nicknames) if (*it == data.nickname) { found = true; }
                        if (found == false)
                        {
                            channel.nicknames.push_back(data.nickname);
                            RESPOND(data.nickname, &quot;JOIN&quot;, first)
                            if (channel.topic.empty()) RESPOND(&quot;&quot;, &quot;331&quot;, data.nickname, first, &quot;No topic is set&quot;)
                            else RESPOND(&quot;&quot;, &quot;332&quot;, data.nickname, first, channel.topic)
                            std::string list; LOOP(channel.nicknames) list += (list.empty() ? &quot;&quot; : &quot; &quot;) + *it;
                            RESPOND(&quot;&quot;, &quot;353&quot;, data.nickname, &quot;=&quot;, first, list)
                            RESPOND(&quot;&quot;, &quot;366&quot;, data.nickname, first, &quot;End of NAMES&quot;)
                        }
                    }
    CASE(&quot;PART&quot;)    if (size &lt; 1)
                        RESPOND(&quot;&quot;, &quot;461&quot;, data.nickname, &quot;PART&quot;, &quot;Not enough parameters&quot;)
                    else
                    {
                        bool found = false;
                        auto &amp;channel = channels[first];
                        LOOP(channel.nicknames) if (*it == data.nickname) found = true;
                        channel.nicknames.remove(data.nickname);
                        if (found)
                        {
                            if (size &lt; 2) msg.params.push_back(data.nickname);
                            RESPOND(data.nickname, &quot;PART&quot;, first, second)
                        }
                        else RESPOND(&quot;&quot;, &quot;442&quot;, data.nickname, first, &quot;You're not on that channel&quot;)
                    }
    CASE(&quot;TOPIC&quot;)   if (size &lt; 1)
                        RESPOND(&quot;&quot;, &quot;461&quot;, data.nickname, &quot;TOPIC&quot;, &quot;Not enough parameters&quot;)
                    else
                    {
                        auto&amp; channel = channels[first];
                        if (size &lt; 2) { channel.topic = &quot;&quot;; RESPOND(&quot;&quot;, &quot;331&quot;, data.nickname, first, &quot;No topic is set&quot;) }
                        else { channel.topic = second; RESPOND(&quot;&quot;, &quot;332&quot;, data.nickname, first, channel.topic) }
                    }
    CASE(&quot;LIST&quot;)    if (size &lt; 1)
                    {
                        LOOP(channels)
                        {
                            std::stringstream ss; ss &lt;&lt; it-&gt;second.nicknames.size();
                            RESPOND(&quot;&quot;, &quot;322&quot;, data.nickname, it-&gt;first, ss.str(), it-&gt;second.topic.empty() ? &quot;No topic is set&quot; : it-&gt;second.topic)
                        }
                        RESPOND(&quot;&quot;, &quot;323&quot;, data.nickname, &quot;End of LIST&quot;)
                    }
                    else
                    {
                        std::stringstream ss; ss &lt;&lt; channels[first].nicknames.size();
                        RESPOND(&quot;&quot;, &quot;322&quot;, data.nickname, first, ss.str(), channels[first].topic.empty() ? &quot;No topic is set&quot; : channels[first].topic)
                        RESPOND(&quot;&quot;, &quot;323&quot;, data.nickname, &quot;End of LIST&quot;)
                    }
    CASE(&quot;NAMES&quot;)   if (size &lt; 1)
                    {
                        LOOP(channels)
                        {
                            std::string list; LOOPX(it-&gt;second.nicknames, dit) list += (list.empty() ? &quot;&quot; : &quot; &quot;) + *dit;
                            RESPOND(&quot;&quot;, &quot;353&quot;, data.nickname, &quot;=&quot;, it-&gt;first, list)
                        }
                        RESPOND(&quot;&quot;, &quot;366&quot;, data.nickname, &quot;End of NAMES&quot;)
                    }
                    else
                    {
                        std::string list; LOOP(channels[first].nicknames) list += (list.empty() ? &quot;&quot; : &quot; &quot;) + *it;
                        RESPOND(&quot;&quot;, &quot;353&quot;, data.nickname, &quot;=&quot;, first, list)
                        RESPOND(&quot;&quot;, &quot;366&quot;, data.nickname, first, &quot;End of NAMES&quot;)
                    }
    CASE(&quot;PRIVMSG&quot;) if (size &lt; 1)
                        RESPOND(&quot;&quot;, &quot;411&quot;, data.nickname, &quot;No recipient given (PRIVMSG)&quot;)
                    else if (size &lt; 2)
                        RESPOND(&quot;&quot;, &quot;412&quot;, data.nickname, &quot;No text to send&quot;)
                    else
                    {
                        bool found = false;
                        auto from = data.nickname;
                        LOOP(channels) if (it-&gt;first == first)
                        {
                            found = true;
                            LOOPX(it-&gt;second.nicknames, nit) LOOPX(socket_list, dit) if (*nit == dit-&gt;nickname) { auto data = *dit; RESPOND(from, &quot;PRIVMSG&quot;, data.nickname, second) }
                        }
                        if (found == false)
                            LOOP(socket_list) if (it-&gt;nickname == first)
                            {
                                found = true;
                                auto data = *it; RESPOND(from, &quot;PRIVMSG&quot;, data.nickname, second)
                            }
                        if (found == false)
                            RESPOND(&quot;&quot;, &quot;401&quot;, data.nickname, first, &quot;No such nick/channel&quot;)
                    }
    CASE(&quot;USERS&quot;)   RESPOND(&quot;&quot;, &quot;446&quot;, data.nickname, &quot;USERS has been disabled&quot;)
    CASE(&quot;PING&quot;)    RESPOND(&quot;&quot;, &quot;PONG&quot;, first, second)
    CASE(&quot;PONG&quot;)    data.ping_timer = time(NULL) + 60; data.pong_timer = 0;
    CASE(&quot;QUIT&quot;)    if (!size) msg.params.push_back(data.nickname);
                    disconnect(data, first, 1);
    // end of the cases
    } else {
        std::cout &lt;&lt; &quot;Received invalid message from &quot; &lt;&lt; data.nickname &lt;&lt; &quot; msg=&quot; &lt;&lt; msg.command &lt;&lt; std::endl;
        RESPOND(&quot;&quot;, &quot;421&quot;, data.nickname, msg.command, &quot;Unknown command&quot;)
    }
    return false;
}
message parse(char *data)
{
    message msg;
    char *pointer = data;
    while (*data != '\0')
    {
        if (msg.command.empty())
        {
            if (*data == ':')
            {
                for (; *data != '\0' &amp;&amp; *data != ' '; ++data); *data = '\0';
                msg.prefix = pointer + 1;
                pointer = ++data;
            }
            for (; *data != '\0' &amp;&amp; *data != ' '; ++data); *data = '\0';
            msg.command = pointer;
            pointer = ++data;
        }
        else if (*data == ':')
        {
            for (; *data != '\0'; ++data);
            msg.params.push_back(pointer+1);
            pointer = ++data;
        }
        else
        {
            for (; *data != '\0' &amp;&amp; *data != ' '; ++data); *data = '\0';
            msg.params.push_back(pointer);
            pointer = ++data;
        }
    }
    return msg;
}
int main()
{
    int retval;
    WSADATA wsaData;
    SOCKADDR_IN listen_address; listen_address.sin_family = AF_INET; listen_address.sin_port = htons(6667); listen_address.sin_addr.s_addr = htonl(INADDR_ANY);
    timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = 10000;
    fd_set socket_read_set, socket_write_set, socket_except_set;
    socket_data listener; listener.nickname = &quot;IRCd&quot;;
    EXIT_IF(1, (0 != WSAStartup(MAKEWORD(2, 2), &amp;wsaData)))
    EXIT_IF(2, (INVALID_SOCKET == (listener.socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))))
    EXIT_IF(3, (SOCKET_ERROR == bind(listener.socket, (SOCKADDR *)&amp;listen_address, sizeof(listen_address))))
    EXIT_IF(4, (SOCKET_ERROR == listen(listener.socket, SOMAXCONN)))
    while (1)
    {
        MAKE_SET(socket_read_set) MAKE_SET(socket_write_set) MAKE_SET(socket_except_set)
        EXIT_IF(5, (SOCKET_ERROR == select(0, &amp;socket_read_set, &amp;socket_write_set, &amp;socket_except_set, &amp;timeout)))
        EXIT_IF(6, (FD_ISSET(listener.socket, &amp;socket_except_set)))
        if (FD_ISSET(listener.socket, &amp;socket_read_set))
        {
            socket_data data = {}; // zero everything
            data.address_length = sizeof(data.address);
            data.socket = accept(listener.socket, (SOCKADDR *)&amp;data.address, &amp;data.address_length);
            EXIT_IF(7, (INVALID_SOCKET == data.socket))
            std::stringstream ss; ss &lt;&lt; inet_ntoa(data.address.sin_addr) &lt;&lt; &quot;:&quot; &lt;&lt; ntohs(data.address.sin_port); data.nickname = ss.str();
            data.ping_timer = time(NULL)+60; data.pong_timer = 0;
            socket_list.push_back(data);
            std::cout  &lt;&lt; &quot;Connected &quot; &lt;&lt; data.nickname &lt;&lt; &quot; ping=&quot; &lt;&lt; data.ping_timer &lt;&lt; std::endl;
        }
        LOOP(socket_list)
        {
            socket_data &amp;data = *it;
            if (data.ping_timer &gt; 0 &amp;&amp; time(NULL) &gt; data.ping_timer)
            {
                RESPOND(&quot;&quot;, &quot;PING&quot;, data.nickname)
                data.ping_timer = 0; data.pong_timer = time(NULL) + 60;
                std::cout &lt;&lt; &quot;Sent PING to &quot; &lt;&lt; data.nickname &lt;&lt; &quot; pong=&quot; &lt;&lt; data.pong_timer &lt;&lt; std::endl;
            }
            if (data.pong_timer &gt; 0 &amp;&amp; time(NULL) &gt; data.pong_timer) disconnect(data, &quot;PONG&quot;, 1);
            if (FD_ISSET(data.socket, &amp;socket_except_set)) disconnect(data, &quot;select except&quot;, 0);
            if (FD_ISSET(data.socket, &amp;socket_read_set))
            {
                int bytes = recv(data.socket, data.read_buffer + data.read_buffer_index, sizeof(data.read_buffer) - data.read_buffer_index - 1 &gt; 0, 0);
                if (bytes &gt; 0) data.read_buffer_index += bytes; else disconnect(data, &quot;recv error&quot;, 0);
                char *pointer = data.read_buffer + data.read_buffer_index - 2;
                if (!strcmp(pointer, &quot;\r\n&quot;))
                {
                    *pointer++ = '\0'; *pointer = '\0'; // remove the \r\n
                    message msg = parse(data.read_buffer);
                    memset(data.read_buffer, 0, READ_BUFFER_SIZE); data.read_buffer_index = 0;
                    if (data.ping_timer &gt; 0 &amp;&amp; time(NULL) &lt; data.ping_timer)
                    {
                        data.ping_timer = time(NULL) + 60; data.pong_timer = 0;
                        std::cout &lt;&lt; &quot;Reset ping for &quot; &lt;&lt; data.nickname &lt;&lt; &quot; ping=&quot; &lt;&lt; data.ping_timer &lt;&lt; std::endl;
                    }
                    if (process(data, msg)) EXIT_IF(0, true)
                }
            }
        }
        socket_list.remove_if([](const socket_data&amp; data){ return data.socket == INVALID_SOCKET; });
    }
    retval = 0;
finished:
    LOOP(socket_list) disconnect(*it, &quot;exit&quot;, 0);
    disconnect(listener, &quot;exit&quot;, 0);
    WSACleanup();
    return retval;
}