Browse Source

[wip] tmp backup

isundil 8 years ago
parent
commit
77f741cf40

+ 1 - 1
cli/msgFormatter

@@ -1 +1 @@
-Subproject commit ecf0aaad6b6e0f32bc3c1674e839b116691fd5d6
+Subproject commit 69d5c09843df62b28e24bd57fb7634dc1cfed14c

+ 19 - 2
srv/src/database.js

@@ -110,7 +110,15 @@ Database.prototype.query = function(table, fields, cb) {
         }
         first = false;
         if (fields[i]) {
-            query += '`' +i +'`=?';
+            query += '`' +i +'`';
+            if (typeof fields[i] === "object") {
+                for (var word in fields[i]) {
+                    query += ' ' +word +' "' +fields[i][word] +'"';
+                    break;
+                }
+            } else {
+                query += '=?';
+            }
             args.push(fields[i]);
         } else {
             query += '`' +i +'` IS NULL ';
@@ -137,12 +145,21 @@ Database.prototype.queryFirst = function(table, fields, cb) {
         }
         first = false;
         if (fields[i]) {
-            query += '`' +i +'`=?';
+            query += '`' +i +'`';
+            if (typeof fields[i] === "object") {
+                for (var word in fields[i]) {
+                    query += ' ' +word +' "' +fields[i][word] +'"';
+                    break;
+                }
+            } else {
+                query += '=?';
+            }
             args.push(fields[i]);
         } else {
             query += '`' +i +'` IS NULL ';
         }
     }
+    query += " LIMIT 1";
     this.db.get(query, args, (err, row) => {
         if (err) {
             console.warn(err);

+ 2 - 2
srv/src/httpServ.js

@@ -119,7 +119,7 @@ Server.prototype.execTemplate = function(template, req, res) {
 };
 
 Server.prototype.execRequest = function(req, res) {
-    if (req.urlObj.isTemplate() && req.urlObj.template.needLogin === false) {
+    if (req.urlObj.isPublicTemplate()) {
         return this.execTemplate(req.urlObj.template, req, res);
     } else if (req.urlObj.isPublic()) {
         if (!config.isDebug)
@@ -178,7 +178,7 @@ Server.prototype.onRequest = function(req, res) {
     req.session = sessionManager.forRequest(req);
     req.urlObj = new Url(req.url);
     req.account = null;
-    if (req.session) {
+    if (req.session && req.session.accountId) {
         var self = this;
         AccountManager.fromId(req.session.accountId, (acc) => {
             req.account = acc;

+ 14 - 0
srv/src/models/accounts.js

@@ -33,6 +33,13 @@ AccountManager.prototype.fromIrcPass = function(pass, cb) {
     });
 };
 
+AccountManager.prototype.fromPhoneAccess = function(acc, cb) {
+    var self = this;
+    db.database.queryFirst(TABLE_NAME, { permanentPhoneAccess: { like: "%," +acc +",%" }}, (err, result) => {
+        cb(self.fromDb(result));
+    });
+};
+
 AccountManager.prototype.fromId = function(id, cb) {
     var self = this;
     db.database.queryFirst(TABLE_NAME, { id: id }, (err, result) => {
@@ -113,6 +120,13 @@ Account.prototype.generateIrcPass = function() {
     return this;
 };
 
+Account.prototype.generatePermanentPhoneAccess = function() {
+    let token = "465465";
+    this.edit();
+    this.permanentPhoneAccess.push(token);
+    return token;
+};
+
 Account.prototype.addService = function(serviceType, serviceName, serviceId, oauthParam) {
     this.services.push([serviceType, serviceName, serviceId, oauthParam]);
 };

+ 1 - 0
srv/src/session.js

@@ -13,6 +13,7 @@ function Session(reqT, sessId) {
     this.sessId = sessId;
     this.accountId = null;
     this.modifiedAt = reqT;
+    this.isNative = false;
 }
 
 SessionManager.createSessionId = function(req) {

+ 5 - 1
srv/src/template/index.js

@@ -2,6 +2,10 @@
 const templates = require('./_templates.js'),
         config = require('../../config.js');
 
+module.exports.needLogin = function(urlObj) {
+    return !urlObj.queryTokens["local"];
+};
+
 module.exports.exec = function(req, res) {
     if (req.account && !req.account.cguReadAndAccepted) {
         if (config.allowNewAccounts)
@@ -100,7 +104,7 @@ module.exports.exec = function(req, res) {
               <a id="fileUploadCancel" class="button"/></a>
               <input type="submit" class="button"/>
           </form></div>
-          <div class="error" id="neterror"></div><script>var IS_LOCAL=` +!!(!req.account || req.urlObj.urlParts[0] === 'local') +`;</script>`
+          <div class="error" id="neterror"></div><script>var IS_LOCAL=` +!!req.urlObj.queryTokens['local'] +`;</script>`
         +templates.footer(["mimouchat.min.js"])
     };
 };

+ 0 - 7
srv/src/template/local.js

@@ -1,7 +0,0 @@
-
-module.exports.exec = require("./index.js").exec;
-module.exports.match = function(url) {
-    return url.urlParts[0] === 'local' && url.urlParts.length === 1;
-};
-module.exports.needLogin = false;
-

+ 41 - 5
srv/src/template/login.js

@@ -132,7 +132,34 @@ module.exports.match = function(url) {
 };
 
 module.exports.exec = function(req, res, srv) {
-    if (!req.urlObj.urlParts[1]) {
+    if (req.urlObj.queryTokens["phoneAccess"]) {
+        var self = this;
+        accountManager.fromPhoneAccess(req.urlObj.queryTokens["phoneAccess"], (acc) => {
+            if (acc) {
+                req.account = acc;
+                req.session = sessionManager.lazyForRequest(req);
+                req.session.setAccountId(req.reqT, acc.id);
+                res.writeHeader("302", {
+                    Location: config.rootUrl,
+                    "Set-Cookie": "sessID="+req.session.sessId +'; Path=/'
+                });
+                sessionManager.saveSession(req.session);
+            } else {
+                res.writeHeader("302", {
+                    Location: "login"
+                });
+            }
+            res.end();
+        });
+    } else if (!req.urlObj.urlParts[1]) {
+        if (req.urlObj.queryTokens["native"]) {
+            req.session = sessionManager.lazyForRequest(req);
+            req.session.isNative = true;
+            sessionManager.saveSession(req.session);
+            res.writeHeader("200", {
+                "Set-Cookie": "sessID="+req.session.sessId +'; Path=/'
+            });
+        }
         res.end(makeLoginPage());
     } else {
         checkTokens(req.urlObj.urlParts[1], req, (account) => {
@@ -140,10 +167,19 @@ module.exports.exec = function(req, res, srv) {
                 req.account = account;
                 req.session = sessionManager.lazyForRequest(req);
                 req.session.setAccountId(req.reqT, account.id);
-                res.writeHeader("302", {
-                    Location: config.rootUrl,
-                    "Set-Cookie": "sessID="+req.session.sessId +'; Path=/'
-                });
+                if (req.session.isNative) {
+                    req.session.isNative = false;
+                    res.writeHeader("302", {
+                        Location: "intent:#Intent;scheme=mimou://open?token=" +account.generatePermanentPhoneAccess() +"&;package=com.knacki.mimou;end",
+                        "Set-Cookie": "sessID="+req.session.sessId +'; Path=/'
+                    });
+                    accountManager.save(account);
+                } else {
+                    res.writeHeader("302", {
+                        Location: config.rootUrl,
+                        "Set-Cookie": "sessID="+req.session.sessId +'; Path=/'
+                    });
+                }
                 sessionManager.saveSession(req.session);
                 res.end();
             } else if (account === null) {

+ 8 - 0
srv/src/url.js

@@ -89,6 +89,14 @@ Url.prototype.isTemplate = function() {
     return !!this.template;
 };
 
+Url.prototype.isPublicTemplate = function() {
+    if (!this.isTemplate() || this.template.needLogin === true)
+        return false;
+    if (typeof this.template.needLogin === "function")
+        return !this.template.needLogin(this);
+    return true;
+};
+
 Url.prototype.getReadStream = function() {
     return fs.createReadStream(this.serve.filename);
 };