Skip to content

Instantly share code, notes, and snippets.

@XadillaX
Last active December 19, 2015 18:18
Show Gist options
  • Save XadillaX/5997649 to your computer and use it in GitHub Desktop.
Save XadillaX/5997649 to your computer and use it in GitHub Desktop.
The last several lines. It said that s, msg and base header was undefined.
/**
* Created with JetBrains WebStorm.
* User: xadillax
* Date: 7/14/13
* Time: 9:44 PM
* NBUT Online Judge Impl.
*/
var base = require("../base");
var util = require("util");
/**
* Create an NOJ Module
* @param baseurl
* @returns {nbut}
*/
exports.create = function(baseurl) {
return new nbut(baseurl);
}
/**
* The NOJ Module
* @param baseurl
*/
function nbut(baseurl) {
base.core.call(this);
this.name = "NOJ";
this.logger = base.logger("NOJ");
if(undefined === baseurl) {
this.baseurl = "http://acm.nbut.edu.cn/";
}
}
util.inherits(nbut, base.core);
/**
* Overwrite and ignore the base.
* @param username
* @param password
*/
nbut.prototype.login = function(username, password, callback) {
var url = this.baseurl + "user/chklogin.xhtml";
var data = {
"username" : username,
"password" : password
};
var datalen = base.util.getDataLength(data);
var header = {
"content-type" : "application/x-www-form-urlencoded",
"content-length" : datalen,
"user-agent" : this.getUserAgent()
};
this.logger.info("Login NOJ: [ " + url + " ]");
this.logger.trace("Querystring: [ " + base.util.stringifyData(data) + " ]");
this.logger.trace("Request header: [ " + JSON.stringify(header) + " ]");
var par = this;
callback.bind(this);
this.spider.post(url, function(data, status, headers) {
par.logger.trace("Page status: " + status);
if(status !== 200) {
par.logger.warn("The server returns an error status");
}
if(data === "1") {
var s = true;
var msg = "";
par.cookies = headers["set-cookie"][0].substr(0, headers["set-cookie"][0].length - 7);
par.logger.info("Logged in");
var baseheader = { "cookie" : par.cookies, "content-length" : 0, "user-agent" : par.getUserAgent() };
/**
* Use fun.bind(par) to bind par as the "this" pointer for fun.
* @refer https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
* @refer NODE.JS IRC CHANNEL: 13:07 < shama> xadillax: foo(a, b callback.bind(foo))
* 13:10 < olalonde> foo (a, b fn) { fn = fn.bind(this); …. }
*/
if(callback !== undefined) callback.bind(par)(s, msg, baseheader);
} else {
par.logger.error("Failed while logging in: " + data);
var baseheader = { "content-length" : 0, "user-agent" : par.getUserAgent() };
if(callback !== undefined) callback.bind(par)(s, msg);
}
}, header, data, "utf8").on("error", function(e){
par.logger.error("Failed while logging in: " + e.message);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment