static void got_term(int z) { /* Now we die by default on sigterm, but scripts have the chance to * catch the event themselves and cancel shutdown by returning 1 */ if (check_tcl_signal("sigterm")) return; kill_bot("ACK, I've been terminated!", "TERMINATE SIGNAL -- SIGNING OFF"); }
static void send_quit() { struct irc_message *quit_msg; log_info("Received `!quit', quitting..."); kill_bot(); quit_msg = create_message(NULL, "QUIT", NULL); send_msg(quit_msg); free_message(quit_msg); }
void kill_bots (struct Bots *killme) { struct Bot *tempbot; struct Bot *tempbot2; tempbot = killme->list; while (tempbot) { tempbot2 = tempbot; /* Remember... */ tempbot = tempbot->next; /* Prepare to move on */ kill_bot (tempbot2); /* Call routine to kill that bot */ } }
static int fill_recv_buf() { int retval = 0; fd_set rfds; struct timeval tv; if (sockfd == -1) { debug("sockfd is -1"); if (time(NULL) - last_activity > LAG_INTERVAL) { debug("LAG_INTERVAL has been reached"); log_info("Trying to reconnect..."); sockfd = connect_to_server(); if (sockfd == -1) { log_info("Failed: %s", strerror(errno)); return -1; } else log_info("Succeeded!"); } else { debug("LAG_INTERVAL not reached; sleeping..."); sleep(5); return -1; } } FD_ZERO(&rfds); FD_SET(sockfd, &rfds); tv.tv_sec = 0; tv.tv_usec = 500000; retval = select(sockfd + 1, &rfds, NULL, NULL, &tv); if (retval == -1) { log_err("Error waiting for socket: %s", strerror(errno)); close(sockfd); sockfd = -1; return -1; } else if (!retval) { time_t curr; time(&curr); if (curr - last_activity > LAG_INTERVAL) { debug("appear to have lost connection"); if (!waiting_for_ping) { struct irc_message *ping_msg; debug("sending ping.."); ping_msg = create_message(NULL, "PING", ":ping"); send_msg(ping_msg); free_message(ping_msg); waiting_for_ping = 1; } else if (curr - last_activity > LAG_INTERVAL + PING_WAIT_TIME) { log_info("Lost connection..."); waiting_for_ping = 0; close(sockfd); sockfd = -1; reset(); } } return -1; } recv_buf_size = recv(sockfd, recv_buf, RECV_BUF_LENGTH - 1, 0); if (recv_buf_size == 0) { // connection has been terminated // likely due to ping timeout sockfd = -1; log_info("Connection terminated. Reconnecting.."); return -1; } else if (recv_buf_size == -1) { log_err("Error receiving packets: %s", strerror(errno)); kill_bot(); return -1; } recv_buf[recv_buf_size] = '\0'; recv_buf_pos = recv_buf; time(&last_activity); waiting_for_ping = 0; return 0; }