+/*
+ * Create a config key for a room's netconfig entry
+ */
+void netcfg_keyname(char *keybuf, long roomnum)
+{
+ if (!keybuf) return;
+ sprintf(keybuf, "c_netconfig_%010ld", roomnum);
+}
+
+
+
+/*
+ * Given a room number and a textual netconfig, convert to base64 and write to the configdb
+ */
+void write_netconfig_to_configdb(long roomnum, const char *raw_netconfig)
+{
+ char keyname[25];
+ char *enc;
+ int enc_len;
+ int len;
+
+ len = strlen(raw_netconfig);
+ netcfg_keyname(keyname, roomnum);
+ enc = malloc(len * 2);
+
+ if (enc) {
+ enc_len = CtdlEncodeBase64(enc, raw_netconfig, len, 0);
+ if ((enc_len > 1) && (enc[enc_len-2] == 13)) enc[enc_len-2] = 0;
+ if ((enc_len > 0) && (enc[enc_len-1] == 10)) enc[enc_len-1] = 0;
+ enc[enc_len] = 0;
+ syslog(LOG_DEBUG, "Writing key '%s' (length=%d)", keyname, enc_len);
+ CtdlSetConfigStr(keyname, enc);
+ free(enc);
+ }
+}
+
+
+
+/*
+ * Given a room number, attempt to load the netconfig configdb entry for that room.
+ * If it returns NULL, there is no netconfig.
+ * Otherwise the caller owns the returned memory and is responsible for freeing it.
+ */
+char *LoadRoomNetConfigFile(long roomnum)
+{
+ char keyname[25];
+ char *encoded_netconfig = NULL;
+ char *decoded_netconfig = NULL;
+
+ netcfg_keyname(keyname, roomnum);
+ encoded_netconfig = CtdlGetConfigStr(keyname);
+ if (!encoded_netconfig) return NULL;
+
+ decoded_netconfig = malloc(strlen(encoded_netconfig)); // yeah, way bigger than it needs to be, but safe
+ CtdlDecodeBase64(decoded_netconfig, encoded_netconfig, strlen(encoded_netconfig));
+ return decoded_netconfig;
+}
+
+
+/*
+ * Deserialize a netconfig , allocate and return structured data
+ */
+OneRoomNetCfg *ParseRoomNetConfigFile(char *serialized_data)
+{
+ const char *Pos = NULL;
+ const CfgLineType *pCfg = NULL;
+ StrBuf *Line = NULL;
+ StrBuf *InStr = NULL;
+ StrBuf *Cfg = NULL;
+ OneRoomNetCfg *OneRNCfg = NULL;
+ int num_lines = 0;
+ int i = 0;
+
+ OneRNCfg = malloc(sizeof(OneRoomNetCfg));
+ memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
+
+ Line = NewStrBuf();
+ InStr = NewStrBuf();
+ Cfg = NewStrBufPlain(serialized_data, -1);
+ num_lines = num_tokens(ChrPtr(Cfg), '\n');
+
+ for (i=0; i<num_lines; ++i) {
+ StrBufExtract_token(Line, Cfg, i, '\n');
+ if (StrLength(Line) > 0) {
+ Pos = NULL;
+ StrBufExtract_NextToken(InStr, Line, &Pos, '|');
+
+ pCfg = GetCfgTypeByStr(SKEY(InStr));
+ if (pCfg != NULL)