/* Create server connection record. `dest' is required, rest can be NULL. `dest' is either a server address or irc network */ IRC_SERVER_CONNECT_REC * irc_server_create_conn(const char *dest, int port, const char *password, const char *nick) { GSList *tmp; time_t now; int n; g_return_val_if_fail(dest != NULL, NULL); /* check if `dest' is IRC network */ if (ircnet_find(dest) == NULL) return create_addr_conn(dest, port, password, nick); /* first try to find a server that hasn't had any connection failures for the past half an hour. If that isn't found, try any server. */ now = time(NULL); for (n = 0; n < 2; n++) { for (tmp = setupservers; tmp != NULL; tmp = tmp->next) { SETUP_SERVER_REC *rec = tmp->data; if (rec->ircnet == NULL || g_strcasecmp(rec->ircnet, dest) != 0) continue; if (n == 1 || !rec->last_failed || rec->last_connect < now-FAILED_RECONNECT_WAIT) return create_addr_conn(rec->address, port, password, nick); } } return NULL; }
/* Connect to server where last connect succeeded (or we haven't tried to connect yet). If there's no such server, connect to server where we haven't connected for the longest time */ static SERVER_CONNECT_REC * create_chatnet_conn(const char *dest, int port, const char *password, const char *nick) { SERVER_SETUP_REC *bestrec; GSList *tmp; time_t now, besttime; now = time(NULL); bestrec = NULL; besttime = now; for (tmp = setupservers; tmp != NULL; tmp = tmp->next) { SERVER_SETUP_REC *rec = tmp->data; if (rec->chatnet == NULL || g_strcasecmp(rec->chatnet, dest) != 0) continue; if (!rec->last_failed) { bestrec = rec; break; } if (bestrec == NULL || besttime > rec->last_connect) { bestrec = rec; besttime = rec->last_connect; } } return bestrec == NULL ? NULL : create_addr_conn(bestrec->chat_type, bestrec->address, 0, dest, NULL, nick); }
/* Create server connection record. `dest' is required, rest can be NULL. `dest' is either a server address or chat network */ SERVER_CONNECT_REC * server_create_conn(int chat_type, const char *dest, int port, const char *chatnet, const char *password, const char *nick) { SERVER_CONNECT_REC *rec; CHATNET_REC *chatrec; g_return_val_if_fail(dest != NULL, NULL); chatrec = chatnet_find(dest); if (chatrec != NULL) { rec = create_chatnet_conn(chatrec->name, port, password, nick); if (rec != NULL) return rec; } chatrec = chatnet == NULL ? NULL : chatnet_find(chatnet); if (chatrec != NULL) chatnet = chatrec->name; return create_addr_conn(chat_type, dest, port, chatnet, password, nick); }