+extern FILE *replay_file;
+
+time_t boot_time, burst_begin, now;
+unsigned long burst_length;
+struct log_type *MAIN_LOG;
+
+int quit_services, max_cycles;
+
+char *services_config = "x3.conf";
+
+char **services_argv;
+int services_argc;
+
+struct cManagerNode cManager;
+
+struct policer_params *oper_policer_params, *luser_policer_params, *god_policer_params;
+
+static const struct message_entry msgtab[] = {
+ { "MSG_NONE", "None" },
+ { "MSG_ON", "On" },
+ { "MSG_OFF", "Off" },
+ { "MSG_NEVER", "Never" },
+ { "MSG_BAR", "----------------------------------------" },
+ { "MSG_SERVICE_IMMUNE", "$b%s$b may not be kicked, killed, banned, or deopped." },
+ { "MSG_SERVICE_PRIVILEGED", "$b%s$b is a privileged service." },
+ { "MSG_NOT_A_SERVICE", "$b%s$b is not a service bot." },
+ { "MSG_COMMAND_UNKNOWN", "$b%s$b is an unknown command." },
+ { "MSG_COMMAND_PRIVILEGED", "$b%s$b is a privileged command." },
+ { "MSG_COMMAND_DISABLED", "$b%s$b is a disabled command." },
+ { "MSG_SETTING_PRIVILEGED", "$b%s$b is a privileged setting." },
+ { "MSG_AUTHENTICATE", "You must first authenticate with $b$N$b." },
+ { "MSG_USER_AUTHENTICATE", "%s must first authenticate with $b$N$b." },
+ { "MSG_SET_EMAIL_ADDR", "You must first set your account's email address. (Contact network staff if you cannot auth to your account.)" },
+ { "MSG_HANDLE_UNKNOWN", "Account $b%s$b has not been registered." },
+ { "MSG_NICK_UNKNOWN", "User with nick $b%s$b does not exist." },
+ { "MSG_CHANNEL_UNKNOWN", "Channel with name $b%s$b does not exist." },
+ { "MSG_SERVER_UNKNOWN", "Server with name $b%s$b does not exist or is not linked." },
+ { "MSG_MODULE_UNKNOWN", "No module has been registered with name $b%s$b." },
+ { "MSG_INVALID_MODES", "$b%s$b is an invalid set of channel modes." },
+ { "MSG_INVALID_GLINE", "Invalid G-line '%s'." },
+ { "MSG_INVALID_SHUN", "Invalid Shun '%s'." },
+ { "MSG_INVALID_DURATION", "Invalid time span '%s'." },
+ { "MSG_NOT_TARGET_NAME", "You must provide the name of a channel or user." },
+ { "MSG_NOT_CHANNEL_NAME", "The channel name you specified is not a valid channel name." },
+ { "MSG_INVALID_CHANNEL", "The channel name you specified does not exist." },
+ { "MSG_CHANNEL_ABSENT", "You aren't currently in $b%s$b." },
+ { "MSG_CHANNEL_USER_ABSENT", "$b%s$b isn't currently in $b%s$b." },
+ { "MSG_MISSING_PARAMS", "$b%s$b requires more parameters." },
+ { "MSG_DEPRECATED_COMMAND", "The $b%s$b command has been deprecated, and will be removed in the future; please use $b%s$b instead." },
+ { "MSG_OPER_SUSPENDED", "Your $b$O$b access has been suspended." },
+ { "MSG_USER_OUTRANKED", "$b%s$b outranks you (command has no effect)." },
+ { "MSG_STUPID_ACCESS_CHANGE", "Please ask someone $belse$b to demote you." },
+ { "MSG_NO_SEARCH_ACCESS", "You do not have enough access to search based on $b%s$b." },
+ { "MSG_INVALID_CRITERIA", "$b%s$b is an invalid search criteria." },
+ { "MSG_MATCH_COUNT", "-----------Found $b%3u$b Matches------------" },
+ { "MSG_NO_MATCHES", "Nothing matched the criteria of your search." },
+ { "MSG_TOPIC_UNKNOWN", "No help on that topic." },
+ { "MSG_INVALID_BINARY", "$b%s$b is an invalid binary value." },
+ { "MSG_INTERNAL_FAILURE", "Your command could not be processed due to an internal failure." },
+ { "MSG_DB_UNKNOWN", "I do not know of a database named %s." },
+ { "MSG_DB_IS_MONDO", "Database %s is in the \"mondo\" database and cannot be written separately." },
+ { "MSG_DB_WRITE_ERROR", "Error while writing database %s." },
+ { "MSG_DB_WROTE_DB", "Wrote database %s (in "FMT_TIME_T".%06lu seconds)." },
+ { "MSG_DB_WROTE_ALL", "Wrote all databases (in "FMT_TIME_T".%06lu seconds)." },
+ { "MSG_AND", "," },
+ { "MSG_0_SECONDS", "0 seconds" },
+ { "MSG_YEAR", "y" },
+ { "MSG_YEARS", "y" },
+ { "MSG_WEEK", "w" },
+ { "MSG_WEEKS", "w" },
+ { "MSG_DAY", "d" },
+ { "MSG_DAYS", "d" },
+ { "MSG_HOUR", "h" },
+ { "MSG_HOURS", "h" },
+ { "MSG_MINUTE", "m" },
+ { "MSG_MINUTES", "m" },
+ { "MSG_SECOND", "s" },
+ { "MSG_SECONDS", "s" },
+ { NULL, NULL }
+};
+
+void uplink_select(char *name);
+
+static int
+uplink_insert(const char *key, void *data, UNUSED_ARG(void *extra))
+{
+ struct uplinkNode *uplink = malloc(sizeof(struct uplinkNode));
+ struct record_data *rd = data;
+ struct addrinfo hints, *ai;
+ int enabled = 1;
+ char *str;
+
+ if(!uplink)
+ {
+ return 0;
+ }
+
+ uplink->name = (char *)key;
+ uplink->host = database_get_data(rd->d.object, "address", RECDB_QSTRING);
+
+ str = database_get_data(rd->d.object, "port", RECDB_QSTRING);
+ uplink->port = str ? atoi(str) : 6667;
+ uplink->password = database_get_data(rd->d.object, "password", RECDB_QSTRING);
+ uplink->their_password = database_get_data(rd->d.object, "uplink_password", RECDB_QSTRING);
+
+ str = database_get_data(rd->d.object, "enabled", RECDB_QSTRING);
+ if(str)
+ {
+ enabled = atoi(str) ? 1 : 0;
+ }
+
+ cManager.enabled += enabled;
+
+ str = database_get_data(rd->d.object, "max_tries", RECDB_QSTRING);
+ uplink->max_tries = str ? atoi(str) : 3;
+ uplink->flags = enabled ? 0 : UPLINK_UNAVAILABLE;
+ uplink->state = DISCONNECTED;
+ uplink->tries = 0;
+
+ str = database_get_data(rd->d.object, "bind_address", RECDB_QSTRING);
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_flags = AI_PASSIVE;
+ hints.ai_socktype = SOCK_STREAM;
+ if (!getaddrinfo(str, NULL, &hints, &ai))
+ {
+ uplink->bind_addr_len = ai->ai_addrlen;
+ uplink->bind_addr = calloc(1, ai->ai_addrlen);
+ memcpy(uplink->bind_addr, ai->ai_addr, ai->ai_addrlen);
+ freeaddrinfo(ai);
+ }
+ else
+ {
+ uplink->bind_addr = NULL;
+ uplink->bind_addr_len = 0;
+ }
+
+ uplink->next = cManager.uplinks;
+ uplink->prev = NULL;
+
+ if(cManager.uplinks)
+ {
+ cManager.uplinks->prev = uplink;
+ }
+
+ cManager.uplinks = uplink;
+
+ /* If the configuration is being reloaded, set the current uplink
+ to the reloaded equivalent, if possible. */
+ if(cManager.uplink
+ && enabled
+ && !irccasecmp(uplink->host, cManager.uplink->host)
+ && uplink->port == cManager.uplink->port)
+ {
+ uplink->state = cManager.uplink->state;
+ uplink->tries = cManager.uplink->tries;
+ cManager.uplink = uplink;
+ }
+
+ return 0;
+}
+
+void
+uplink_compile(void)
+{
+ const char *cycles;
+ dict_t conf_node;
+ struct uplinkNode *oldUplinks = NULL, *oldUplink = NULL;
+
+ /* Save the old uplinks, we'll remove them later. */
+ oldUplink = cManager.uplink;
+ oldUplinks = cManager.uplinks;
+
+ cycles = conf_get_data("server/max_cycles", RECDB_QSTRING);
+ max_cycles = cycles ? atoi(cycles) : 30;
+ if(!(conf_node = conf_get_data("uplinks", RECDB_OBJECT)))
+ {
+ log_module(MAIN_LOG, LOG_FATAL, "No uplinks configured; giving up.");
+ exit(1);
+ }
+
+ cManager.enabled = 0;
+ dict_foreach(conf_node, uplink_insert, NULL);
+
+ /* Remove the old uplinks, if any. It doesn't matter if oldUplink (below)
+ is a reference to one of these, because it won't get dereferenced. */
+ if(oldUplinks)
+ {
+ struct uplinkNode *uplink, *next;
+
+ oldUplinks->prev->next = NULL;
+
+ for(uplink = oldUplinks; uplink; uplink = next)
+ {
+ next = uplink->next;
+ free(uplink->bind_addr);
+ free(uplink);
+ }
+ }
+
+ /* If the uplink hasn't changed, it's either NULL or pointing at
+ an uplink that was just deleted, select a new one. */
+ if(cManager.uplink == oldUplink)
+ {
+ if(oldUplink)
+ {
+ irc_squit(self, "Uplinks updated; selecting new uplink.", NULL);
+ }
+
+ cManager.uplink = NULL;
+ uplink_select(NULL);
+ }
+}
+
+struct uplinkNode *
+uplink_find(char *name)
+{
+ struct uplinkNode *uplink;
+
+ if(!cManager.enabled || !cManager.uplinks)
+ {
+ return NULL;
+ }
+
+ for(uplink = cManager.uplinks; uplink; uplink = uplink->next)
+ {
+ if(!strcasecmp(uplink->name, name))
+ {
+ return uplink;
+ }
+ }
+
+ return NULL;
+}
+
+void
+uplink_select(char *name)
+{
+ struct uplinkNode *start, *uplink, *next;
+ int stop;
+
+ if(!cManager.enabled || !cManager.uplinks)
+ {
+ log_module(MAIN_LOG, LOG_FATAL, "No uplinks enabled; giving up.");
+ exit(1);
+ }
+
+ if(!cManager.uplink)
+ {
+ start = cManager.uplinks;
+ }
+ else
+ {
+ start = cManager.uplink->next;
+ if(!start)
+ {
+ start = cManager.uplinks;
+ }
+ }
+
+ stop = 0;
+ for(uplink = start; uplink; uplink = next)
+ {
+ next = uplink->next ? uplink->next : cManager.uplinks;
+
+ if(stop)
+ {
+ uplink = NULL;
+ break;
+ }
+
+ /* We've wrapped around the list. */
+ if(next == start)
+ {
+ sleep((cManager.cycles >> 1) * 5);
+ cManager.cycles++;
+
+ if(max_cycles && (cManager.cycles >= max_cycles))
+ {
+ log_module(MAIN_LOG, LOG_FATAL, "Maximum uplink list cycles exceeded; giving up.");
+ exit(1);
+ }
+
+ /* Give the uplink currently in 'uplink' consideration,
+ and if not selected, break on the next iteration. */
+ stop = 1;
+ }
+
+ /* Skip bad uplinks. */
+ if(uplink->flags & UPLINK_UNAVAILABLE)
+ {
+ continue;
+ }
+
+ if(name && irccasecmp(uplink->name, name))
+ {
+ /* If we were told to connect to a specific uplink, don't stop
+ until we find it.
+ */
+ continue;
+ }
+
+ /* It would be possible to track uplink health through a variety
+ of statistics and only break on the best uplink. For now, break
+ on the first available one.
+ */
+
+ break;
+ }
+
+ if(!uplink)
+ {
+ /* We are shit outta luck if every single uplink has been passed
+ over. Use the current uplink if possible. */
+ if(!cManager.uplink || cManager.uplink->flags & UPLINK_UNAVAILABLE)
+ {
+ log_module(MAIN_LOG, LOG_FATAL, "All available uplinks exhausted; giving up.");
+ exit(1);
+ }
+
+ return;
+ }
+
+ cManager.uplink = uplink;
+}
+
+int
+uplink_connect(void)
+{
+ struct uplinkNode *uplink = cManager.uplink;
+
+ if(uplink->state != DISCONNECTED)
+ {
+ return 0;
+ }
+
+ if(uplink->flags & UPLINK_UNAVAILABLE)
+ {
+ uplink_select(NULL);
+ uplink = cManager.uplink;
+ }
+
+ if(uplink->tries)
+ {
+ /* This delay could scale with the number of tries. */
+ sleep(2);
+ }
+
+ if(!create_socket_client(uplink))
+ {
+ if(uplink->max_tries && (uplink->tries >= uplink->max_tries))
+ {
+ /* This is a bad uplink, move on. */
+ uplink->flags |= UPLINK_UNAVAILABLE;
+ uplink_select(NULL);
+ }
+
+ return 0;
+ }
+ else
+ {
+ uplink->state = AUTHENTICATING;
+ irc_introduce(uplink->password);
+ }
+
+ return 1;
+}
+
+void
+received_ping(void)
+{
+ /* This function is called when a ping is received. Take it as
+ a sign of link health and reset the connection manager
+ information. */
+
+ cManager.cycles = 0;
+}