/* * packet.C: IRC packet handling. * * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team * Rights to this code are as documented in COPYING. * * * Portions of this file were derived from sources bearing the following license: * Rights to this code are documented in doc/pod/license.pod. * Copyright © 2005-2007 Atheme Project (http://www.atheme.org) */ static char const rcsid[] = "$Id: packet.C,v 1.8 2007/09/22 14:27:30 pippijn Exp $"; #include "atheme.h" #include #include #include "uplink.h" #include "datastream.h" #include "connection.h" static void on_connected (connection_t *cptr); /* bursting timer */ #if HAVE_GETTIMEOFDAY struct timeval burstime; #endif static void irc_recvq_handler (connection_t *cptr) { bool wasnonl; char parsebuf[BUFSIZE + 1]; int count; wasnonl = cptr->flags & CF_NONEWLINE ? true : false; count = recvq_getline (cptr, parsebuf, sizeof parsebuf - 1); if (count <= 0) return; cnt.bin += count; /* ignore the excessive part of a too long line */ if (wasnonl) return; me.uplinkpong = NOW; if (parsebuf[count - 1] == '\n') count--; if (count > 0 && parsebuf[count - 1] == '\r') count--; parsebuf[count] = '\0'; parse (parsebuf); } static void ping_uplink (void *arg) { unsigned int diff; if (me.connected) { phandler->ping_sts (); diff = NOW - me.uplinkpong; if (diff >= 600) { slog (LG_INFO, "ping_uplink(): uplink appears to be dead, disconnecting"); sts ("ERROR :Closing Link: 127.0.0.1 (Ping timeout: %d seconds)", diff); sendq_flush (curr_uplink->conn); if (me.connected) { errno = 0; curr_uplink->conn->close (); } } } if (!me.connected) event_delete (ping_uplink, NULL); } static void on_connected (connection_t *cptr) { /* add our server */ if (cptr == curr_uplink->conn) { cptr->flags = CF_UPLINK; cptr->recvq_handler = irc_recvq_handler; me.connected = true; /* no SERVER message received */ me.recvsvr = false; slog (LG_INFO, "irc_handle_connect(): connection to uplink established"); phandler->server_login (); #ifdef HAVE_GETTIMEOFDAY /* start our burst timer */ s_time (&burstime); #endif /* done bursting by this time... */ phandler->ping_sts (); /* ping our uplink every 5 minutes */ event_delete (ping_uplink, NULL); event_add ("ping_uplink", ping_uplink, NULL, 300); me.uplinkpong = NOW; } } void init_ircpacket (void) { connection_t::callback.connected.attach (on_connected); }