+void resethubnum(void) {
+ hubnum=0;
+}
+
+void nexthub(void) {
+ /*
+ * this is set if the connection before this (failed one)
+ * was successful, so we attempted reconnecting to the
+ * same server, and now we've failed, so reset to the first
+ * which should be the preferred one.
+ */
+
+ if (previouslyconnected) {
+ previouslyconnected=0;
+ if((hubcount<2) || (hubnum!=0)) {
+ hubnum=0;
+ } else {
+ hubnum=1;
+ }
+ return;
+ }
+
+ /* 1 % 0 is fun */
+ if (hubcount>0)
+ hubnum=(hubnum+1)%hubcount;
+}
+
+int gethub(int servernum, char **conto, long *portnum, char **conpass, long *pingfreq) {
+ sstring *conto_s, *portnum_s, *conpass_s, *pingfreq_s;
+ static char conto_b[512], conpass_b[512];
+ int ret, s;
+ char *section;
+
+ if (hubcount) {
+ static char realsection[512];
+ snprintf(realsection,sizeof(realsection),"hub-%s",hublist[servernum]->content);
+ section=realsection;
+ s=1;
+ } else {
+ section="irc";
+ s=0;
+ }
+
+ conto_s=getconfigitem(section,s?"host":"hubhost");
+ if (!conto_s || !conto_s->content || !conto_s->content[0]) {
+ ret=0;
+ } else {
+ ret=1;
+ }
+
+ if (conto) {
+ conto_s=getcopyconfigitem(section,s?"host":"hubhost","127.0.0.1",HOSTLEN);
+ strlcpy(conto_b, conto_s->content, sizeof(conto_b));
+ *conto=conto_b;
+
+ freesstring(conto_s);
+ }
+
+ if (portnum) {
+ portnum_s=getcopyconfigitem(section,s?"port":"hubport","4400",7);
+ *portnum=strtol(portnum_s->content,NULL,10);
+
+ freesstring(portnum_s);
+ }
+
+ if (conpass) {
+ conpass_s=getcopyconfigitem(section,s?"pass":"hubpass","erik",20);
+ strlcpy(conpass_b, conpass_s->content, sizeof(conpass_b));
+ *conpass = conpass_b;
+
+ freesstring(conpass_s);
+ }
+
+ if (pingfreq) {
+ pingfreq_s=getcopyconfigitem(section,"pingfreq","90",10);
+ *pingfreq=strtol(pingfreq_s->content,NULL,10);
+
+ freesstring(pingfreq_s);
+ }
+
+ return ret;
+}
+
+/* we check at startup that (most things) resolve */
+void checkhubconfig(void) {
+ array *servers_ar;
+ int i;
+
+ resethubnum();
+ hubcount=0;
+
+ servers_ar=getconfigitems("irc", "hub");
+
+ /*
+ * we don't bother doing the gethostbyname check for
+ * the old style of configuration, as it'll
+ * be checked by irc_connect anyway.
+ */
+ if (!servers_ar || !servers_ar->cursi) {
+ Error("irc",ERR_WARNING,"Using legacy hub configuration format.");
+ return;
+ }
+
+ hublist=(sstring **)servers_ar->content;
+ hubcount=servers_ar->cursi;
+
+ for (i=0;i<hubcount;i++) {
+ char *conto;
+
+ if (!gethub(i, &conto, NULL, NULL, NULL)) {
+ Error("irc",ERR_FATAL,"No server configuration specified for '%s'.",hublist[i]->content);
+ exit(1);
+ }
+
+ if (!gethostbyname(conto)) {
+ Error("irc",ERR_FATAL,"Couldn't resolve host %s.",conto);
+ exit(1);
+ }
+ }
+}
+
+void ircrehash(int hookhum, void *arg) {
+ checkhubconfig();
+}
+