1
0
mirror of https://github.com/osmarks/ngircd.git synced 2024-10-27 20:36:18 +00:00

Clean up and document IRC_STATS() function

This commit is contained in:
Alexander Barton 2010-01-16 23:24:19 +01:00
parent ecad9f32c8
commit ef157715a0

View File

@ -465,6 +465,10 @@ uptime_mins(time_t *now)
} }
/**
* Handler for the IRC command "STATS".
* See RFC 2812 section 3.4.4.
*/
GLOBAL bool GLOBAL bool
IRC_STATS( CLIENT *Client, REQUEST *Req ) IRC_STATS( CLIENT *Client, REQUEST *Req )
{ {
@ -475,11 +479,12 @@ IRC_STATS( CLIENT *Client, REQUEST *Req )
time_t time_now; time_t time_now;
unsigned int days, hrs, mins; unsigned int days, hrs, mins;
assert( Client != NULL ); assert(Client != NULL);
assert( Req != NULL ); assert(Req != NULL);
if (Req->argc > 2) if (Req->argc > 2)
return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command); return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
Client_ID(Client), Req->command);
/* use prefix to determine "From" */ /* use prefix to determine "From" */
if (Client_Type(Client) == CLIENT_SERVER) if (Client_Type(Client) == CLIENT_SERVER)
@ -487,18 +492,21 @@ IRC_STATS( CLIENT *Client, REQUEST *Req )
else else
from = Client; from = Client;
if (! from) if (!from)
return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix); return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
Client_ID(Client), Req->prefix);
if (Req->argc == 2) { if (Req->argc == 2) {
/* forward to another server? */ /* forward to another server? */
target = Client_Search( Req->argv[1] ); target = Client_Search(Req->argv[1]);
if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) if ((!target) || (Client_Type(target) != CLIENT_SERVER))
return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[1] ); return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
Client_ID(from), Req->argv[1]);
if( target != Client_ThisServer()) { if (target != Client_ThisServer()) {
/* forward to another server */ /* forward to another server */
return IRC_WriteStrClientPrefix( target, from, "STATS %s %s", Req->argv[0], Req->argv[1] ); return IRC_WriteStrClientPrefix(target, from,
"STATS %s %s", Req->argv[0], Req->argv[1]);
} }
} }
@ -508,57 +516,70 @@ IRC_STATS( CLIENT *Client, REQUEST *Req )
query = '*'; query = '*';
switch (query) { switch (query) {
case 'l': /* Links */ case 'l': /* Link status (servers and own link) */
case 'L': case 'L':
time_now = time(NULL); time_now = time(NULL);
for (con = Conn_First(); con != NONE ;con = Conn_Next(con)) { for (con = Conn_First(); con != NONE; con = Conn_Next(con)) {
cl = Conn_GetClient(con); cl = Conn_GetClient(con);
if (!cl) if (!cl)
continue; continue;
if ((Client_Type(cl) == CLIENT_SERVER) || (cl == Client)) { if ((Client_Type(cl) == CLIENT_SERVER)
/* Server link or our own connection */ || (cl == Client)) {
/* Server link or our own connection */
#ifdef ZLIB #ifdef ZLIB
if (Conn_Options(con) & CONN_ZIP) { if (Conn_Options(con) & CONN_ZIP) {
if (!IRC_WriteStrClient(from, RPL_STATSLINKINFOZIP_MSG, if (!IRC_WriteStrClient
Client_ID(from), Client_Mask(cl), Conn_SendQ(con), (from, RPL_STATSLINKINFOZIP_MSG,
Conn_SendMsg(con), Zip_SendBytes(con), Conn_SendBytes(con), Client_ID(from), Client_Mask(cl),
Conn_RecvMsg(con), Zip_RecvBytes(con), Conn_RecvBytes(con), (long)(time_now - Conn_StartTime(con)))) Conn_SendQ(con), Conn_SendMsg(con),
return DISCONNECTED; Zip_SendBytes(con),
continue; Conn_SendBytes(con),
} Conn_RecvMsg(con),
#endif Zip_RecvBytes(con),
if (!IRC_WriteStrClient(from, RPL_STATSLINKINFO_MSG, Client_ID(from), Conn_RecvBytes(con),
Client_Mask(cl), Conn_SendQ(con), Conn_SendMsg(con), Conn_SendBytes(con), (long)(time_now - Conn_StartTime(con))))
Conn_RecvMsg(con), Conn_RecvBytes(con), (long)(time_now - Conn_StartTime(con))))
return DISCONNECTED;
}
}
break;
case 'm': /* IRC-Commands */
case 'M':
cmd = Parse_GetCommandStruct( );
for (; cmd->name ; cmd++) {
if (cmd->lcount == 0 && cmd->rcount == 0)
continue;
if (!IRC_WriteStrClient(from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
return DISCONNECTED;
}
break;
case 'u': /* server uptime */
case 'U':
time_now = time(NULL) - NGIRCd_Start;
days = uptime_days(&time_now);
hrs = uptime_hrs(&time_now);
mins = uptime_mins(&time_now);
if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
days, hrs, mins, (unsigned int) time_now))
return DISCONNECTED; return DISCONNECTED;
break; continue;
}
#endif
if (!IRC_WriteStrClient
(from, RPL_STATSLINKINFO_MSG,
Client_ID(from), Client_Mask(cl),
Conn_SendQ(con), Conn_SendMsg(con),
Conn_SendBytes(con), Conn_RecvMsg(con),
Conn_RecvBytes(con),
(long)(time_now - Conn_StartTime(con))))
return DISCONNECTED;
}
}
break;
case 'm': /* IRC command status (usage count) */
case 'M':
cmd = Parse_GetCommandStruct();
for (; cmd->name; cmd++) {
if (cmd->lcount == 0 && cmd->rcount == 0)
continue;
if (!IRC_WriteStrClient
(from, RPL_STATSCOMMANDS_MSG, Client_ID(from),
cmd->name, cmd->lcount, cmd->bytes, cmd->rcount))
return DISCONNECTED;
}
break;
case 'u': /* Server uptime */
case 'U':
time_now = time(NULL) - NGIRCd_Start;
days = uptime_days(&time_now);
hrs = uptime_hrs(&time_now);
mins = uptime_mins(&time_now);
if (!IRC_WriteStrClient(from, RPL_STATSUPTIME, Client_ID(from),
days, hrs, mins, (unsigned int)time_now))
return DISCONNECTED;
break;
} }
IRC_SetPenalty(from, 2); IRC_SetPenalty(from, 2);
return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG, Client_ID(from), query); return IRC_WriteStrClient(from, RPL_ENDOFSTATS_MSG,
Client_ID(from), query);
} /* IRC_STATS */ } /* IRC_STATS */