summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2013-04-09 13:33:54 +0200
committerYorhel <git@yorhel.nl>2013-04-09 13:33:54 +0200
commit50b03a0407dc1b96ffb54b0310b821a0a12f7a44 (patch)
tree814a24e2178fe34e4bf313f585b2138b6456b686
parentaadd42744bb020d050968562358ff5285093c864 (diff)
hub: Save (almost) all hub config to the database
Yay, Globster now properly remembers hub config across restarts. Now it's finally safe to hardcode user IDs in scripts, too.
-rw-r--r--doc/api.pod18
-rw-r--r--src/hub/connection.c6
-rw-r--r--src/hub/hub.c17
-rw-r--r--src/hub/hub.h5
-rw-r--r--src/hub/manager.c2
-rw-r--r--src/hub/users.c4
6 files changed, 38 insertions, 14 deletions
diff --git a/doc/api.pod b/doc/api.pod
index 672de84..cfa7f27 100644
--- a/doc/api.pod
+++ b/doc/api.pod
@@ -171,6 +171,10 @@ UTF-8. Modifying the encoding while being connected to an NMDC hub may cause
odd issues with some chat messages and user nicks using the old encoding while
others use the new. A reconnect is thus recommended.
+All of the above settings, with the exception of MyPassword, are stored in the
+database and remembered across restarts. (I<TODO:> Figure something out for
+MyPassword, too).
+
property s HubName r
property s HubDescription r
property s HubVersion r
@@ -361,15 +365,11 @@ A user is internally identified with a signed 64-bit positive number. This
number uniquely identifies a (hub, user) pair within the entire application:
The same user on two different hubs will have a different ID. The number is
stable in the sense that it remains the same for a certain user even after
-reconnect. On ADC, this identifies a (hub, CID) tuple, on NMDC a (hub, nick)
-tuple. The special value 0 always identifies this client (and violates the
-"unique within the entire application" property, since it's 0 on every hub).
-Negative identifiers may have a special meaning depending on the context where
-the ID is used.
-
-I<Note:> The intention is that the same user will have the same ID even after a
-restart of the Globster daemon. This is currently not the case because Globster
-does not have a storage backend yet.
+reconnect or restart. On ADC, this identifies a (hub, CID) tuple, on NMDC a
+(hub, nick) tuple. The special value 0 always identifies this client (and
+violates the "unique within the entire application" property, since it's 0 on
+every hub). Negative identifiers may have a special meaning depending on the
+context where the ID is used.
User information is stored in the following I<fields>,
diff --git a/src/hub/connection.c b/src/hub/connection.c
index bcacc14..3d4ba92 100644
--- a/src/hub/connection.c
+++ b/src/hub/connection.c
@@ -227,7 +227,10 @@ static void hub_conn_connect(hub_t *h, const char *addr, DBusMessage *msg) {
yinfo("%d: Connecting to %s", h->id, addr);
h->c.cmsg = msg ? dbus_message_ref(msg) : NULL;
- dbo_hubc_ConnectAddress_set(h, &h->c.props, addr);
+ if(strcmp(addr, h->c.props.ConnectAddress) != 0) {
+ dbo_hubc_ConnectAddress_set(h, &h->c.props, addr);
+ hub_hub_setvar(h, "ConnectAddress", sqlasync_text(SQLASYNC_COPY, addr));
+ }
dbo_hubc_KeyprintSHA256_set(h, &h->c.props, "");
h->c.istls = strncmp(addr, "adcs://", 7) == 0 || strncmp(addr, "nmdcs://", 7) == 0;
@@ -361,6 +364,7 @@ static void ForceDisconnect(hub_t *h, DBusMessage *msg) {
static void set_ReconnectTimeout(hub_t *h, DBusMessage *msg, uint16_t timeout) {
dbo_hubc_ReconnectTimeout_setreply(h, &h->c.props, msg, timeout);
+ hub_hub_setvar(h, "ReconnectTimeout", sqlasync_int(timeout));
if(hub_conn_state(h) == HUBC_TIMER)
hub_conn_disconnect(h, HUBCD_RECONNECT, NULL, NULL);
}
diff --git a/src/hub/hub.c b/src/hub/hub.c
index 597d504..3a2dc3b 100644
--- a/src/hub/hub.c
+++ b/src/hub/hub.c
@@ -43,24 +43,28 @@ static void set_MyNick(hub_t *h, DBusMessage *msg, const char *val) {
return;
}
+ hub_hub_setvar(h, "MyNick", sqlasync_text(SQLASYNC_COPY, val));
dbo_hub_MyNick_setreply(h, &h->h.props, msg, val);
hub_hub_infchange(h, true);
}
static void set_MyDescription(hub_t *h, DBusMessage *msg, const char *val) {
+ hub_hub_setvar(h, "MyDescription", sqlasync_text(SQLASYNC_COPY, val));
dbo_hub_MyDescription_setreply(h, &h->h.props, msg, val);
hub_hub_infchange(h, true);
}
static void set_MyEmail(hub_t *h, DBusMessage *msg, const char *val) {
+ hub_hub_setvar(h, "MyEmail", sqlasync_text(SQLASYNC_COPY, val));
dbo_hub_MyEmail_setreply(h, &h->h.props, msg, val);
hub_hub_infchange(h, true);
}
static void set_MyConnection(hub_t *h, DBusMessage *msg, uint32_t val) {
+ hub_hub_setvar(h, "MyConnection", sqlasync_int(val));
dbo_hub_MyConnection_setreply(h, &h->h.props, msg, val);
hub_hub_infchange(h, true);
}
@@ -72,8 +76,10 @@ static DBO_HUB_MyPassword_SETTER(, hub_t, h.props);
static void set_HubEncoding(hub_t *h, DBusMessage *msg, const char *val) {
if(!nmdc_convert_check(val))
dbo_sendfree(dbus_message_new_error(msg, DBUS_ERROR_INVALID_ARGS, "Unsupported character encoding"));
- else
+ else {
+ hub_hub_setvar(h, "HubEncoding", sqlasync_text(SQLASYNC_COPY, val));
dbo_hub_HubEncoding_setreply(h, &h->h.props, msg, val);
+ }
}
@@ -151,6 +157,13 @@ void hub_hub_seterror(hub_t *h, hub_conn_flags_t disconnectflags, const char *er
}
+void hub_hub_setvar(hub_t *h, const char *name, sqlasync_value_t val) {
+ sqlasync_sql(db_sql, NULL, SQLASYNC_STATIC,
+ "INSERT OR REPLACE INTO hub_vars (hub, name, value) VALUES (?,?,?)",
+ 3, sqlasync_int(h->id), sqlasync_text(SQLASYNC_STATIC, name), val);
+}
+
+
void hub_hub_connected(hub_t *h) {
h->h.loggedin = h->h.infchanged = h->h.infimportant = false;
@@ -187,7 +200,7 @@ void hub_hub_init(hub_t *h, const char *nick, const char *desc, const char *mail
else {
h->h.props.MyNick = malloc(20);
snprintf(h->h.props.MyNick, 19, "Mr.%05d", rand() % 100000);
- /* TODO: Save nick */
+ hub_hub_setvar(h, "MyNick", sqlasync_text(SQLASYNC_COPY, h->h.props.MyNick));
}
h->h.props.MyDescription = strdup(desc);
diff --git a/src/hub/hub.h b/src/hub/hub.h
index 2a9680c..f203b85 100644
--- a/src/hub/hub.h
+++ b/src/hub/hub.h
@@ -69,6 +69,11 @@ void hub_hub_infchange(hub_t *h, bool important);
void hub_hub_seterror(hub_t *h, hub_conn_flags_t disconnectflags, const char *err, const char *msg, const char *redir);
+/* Convenience function to save a hub variable.
+ * The `name' pointer should be a static string. */
+void hub_hub_setvar(hub_t *h, const char *name, sqlasync_value_t val);
+
+
void hub_hub_connected(hub_t *h);
void hub_hub_disconnected(hub_t *h);
void hub_hub_init(hub_t *h, const char *nick, const char *desc, const char *mail, uint32_t conn, const char *encoding);
diff --git a/src/hub/manager.c b/src/hub/manager.c
index 2fa34c5..94e27be 100644
--- a/src/hub/manager.c
+++ b/src/hub/manager.c
@@ -112,7 +112,7 @@ static void hub_create(int id, sqlasync_result_t *r) {
INTVAL(ReconnectTimeout, 0, UINT16_MAX, 60)
);
hub_hub_init(h,
- MyNick.type == SQLITE_TEXT ? MyNick.val.ptr : "",
+ MyNick.type == SQLITE_TEXT ? MyNick.val.ptr : NULL,
MyDescription.type == SQLITE_TEXT ? MyDescription.val.ptr : "",
MyEmail.type == SQLITE_TEXT ? MyEmail.val.ptr : "",
INTVAL(MyConnection, 0, UINT32_MAX, 0),
diff --git a/src/hub/users.c b/src/hub/users.c
index 2887c6c..940d410 100644
--- a/src/hub/users.c
+++ b/src/hub/users.c
@@ -516,7 +516,9 @@ void hub_users_init(hub_t *h, const char *salt) {
base32_decode(salt, h->u.idsalt, sizeof(h->u.idsalt));
else {
crypt_rnd(h->u.idsalt, sizeof(h->u.idsalt));
- /* TODO: Save the salt in the database */
+ char *buf = malloc(53); /* assumes sizeof(h->u.idsalt) <= 32 */
+ base32_encode(h->u.idsalt, buf, sizeof(h->u.idsalt));
+ hub_hub_setvar(h, "UserIDSalt", sqlasync_text(SQLASYNC_FREE, buf));
}
ev_timer_init(&h->u.sweep, hub_users_sweep, SWEEP_TIMEOUT, SWEEP_TIMEOUT);