diff --git a/lib/acp.js b/lib/acp.js index 96ff410e..c15d3c2d 100644 --- a/lib/acp.js +++ b/lib/acp.js @@ -16,20 +16,15 @@ var db = require("./database"); function handleAnnounce(user, data) { var sv = Server.getServer(); - sv.announcement = { + sv.announce({ title: data.title, text: data.content, from: user.name - }; - - sv.io.sockets.emit("announcement", sv.announcement); - if (sv.ioSecure) { - sv.ioSecure.sockets.emit("announcement", sv.announcement); - } + }); } function handleAnnounceClear(user) { - Server.getServer().announcement = null; + Server.getServer().announce(null); } function handleGlobalBan(user, data) { diff --git a/lib/database.js b/lib/database.js index c41818b4..cf5bbaba 100644 --- a/lib/database.js +++ b/lib/database.js @@ -3,6 +3,7 @@ var bcrypt = require("bcrypt"); var $util = require("./utilities"); var Logger = require("./logger"); var Config = require("./config"); +var Server = require("./server"); var pool = null; var global_ipbans = {}; @@ -148,7 +149,8 @@ module.exports.initGlobalTables = function () { return; } - require("./dbupdate").checkVersion(); + require("./database/update").checkVersion(); + module.exports.loadAnnouncement(); }); }; @@ -670,3 +672,45 @@ module.exports.listStats = function (callback) { }; /* END REGION */ + +/* Misc */ +module.exports.loadAnnouncement = function () { + var query = "SELECT * FROM `meta` WHERE `key`='announcement'"; + module.exports.query(query, function (err, rows) { + if (err) { + return; + } + + if (rows.length === 0) { + return; + } + + var announcement = rows[0].value; + try { + announcement = JSON.parse(announcement); + } catch (e) { + Logger.errlog.log("Invalid announcement data in database: " + + announcement.value); + module.exports.clearAnnouncement(); + return; + } + + var sv = Server.getServer(); + sv.announcement = announcement; + sv.io.sockets.emit("announcement", announcement); + if (sv.ioSecure) { + sv.ioSecure.sockets.emit("announcement", announcement); + } + }); +}; + +module.exports.setAnnouncement = function (data) { + var query = "INSERT INTO `meta` (`key`, `value`) VALUES ('announcement', ?) " + + "ON DUPLICATE KEY UPDATE `value`=?"; + var repl = JSON.stringify(data); + module.exports.query(query, [repl, repl]); +}; + +module.exports.clearAnnouncement = function () { + module.exports.query("DELETE FROM `meta` WHERE `key`='announcement'"); +}; diff --git a/lib/dbupdate.js b/lib/database/update.js similarity index 93% rename from lib/dbupdate.js rename to lib/database/update.js index 310c4a44..8cd4706c 100644 --- a/lib/dbupdate.js +++ b/lib/database/update.js @@ -1,5 +1,5 @@ -var db = require("./database"); -var Logger = require("./logger"); +var db = require("../database"); +var Logger = require("../logger"); const DB_VERSION = 1; diff --git a/lib/server.js b/lib/server.js index aed3a9a0..59de565d 100644 --- a/lib/server.js +++ b/lib/server.js @@ -43,6 +43,7 @@ var Logger = require("./logger"); var Channel = require("./channel"); var User = require("./user"); var $util = require("./utilities"); +var db = require("./database"); var Server = function () { var self = this; @@ -70,18 +71,6 @@ var Server = function () { "../httpaccess.log")); self.express = express(); require("./web/webserver").init(self.express); - self.express.get("/old/:channel(*)", function (req, res, next) { - var c = req.params.channel; - if (!$util.isValidChannelName(c)) { - res.redirect("/" + c); - return; - } - - self.logHTTP(req); - res.sendfile("channel.html", { - root: path.join(__dirname, "../www") - }); - }); // http/https/sio server init ----------------------------------------- if (Config.get("https.enabled")) { @@ -225,22 +214,18 @@ Server.prototype.packChannel = function (c) { return data; }; -Server.prototype.logHTTP = function (req, status) { - if (status === undefined) - status = 200; - - var ip = this.getHTTPIP(req); - var url = req.url; - // Remove query - if(url.indexOf("?") != -1) - url = url.substring(0, url.lastIndexOf("?")); - this.httplog.log([ - ip, - req.method, - url, - status, - req.header("user-agent") - ].join(" ")); +Server.prototype.announce = function (data) { + if (data == null) { + this.announcement = null; + db.clearAnnouncement(); + } else { + this.announcement = data; + db.setAnnouncement(data); + this.io.sockets.emit("announcement", data); + if (this.ioSecure) { + this.ioSecure.sockets.emit("announcement", data); + } + } }; Server.prototype.shutdown = function () {