Skip to content

Instantly share code, notes, and snippets.

@donpdonp
Last active March 4, 2020 17:42
Show Gist options
  • Save donpdonp/2fa830610b086b955a49ad755af80108 to your computer and use it in GitHub Desktop.
Save donpdonp/2fa830610b086b955a49ad755af80108 to your computer and use it in GitHub Desktop.
gluon wifi
(function(){
return {name: "wifi"}
})
var prefix = "wifi:"
function go(msg) {
if (msg.method == "irc.privmsg") {
var regex = /^!wifi\s+(.*)/
var cmd_match = regex.exec(msg.params.message)
if (cmd_match) {
var name, pass
var tree = quotes.parse(cmd_match[1])
if(typeof tree == 'object') {
var qwords = tree.elements.map(function(l){return l.elements[0].text})
var words = qwords.map(function(q){return q.replace(/^"(.+)"$/,'$1')})
name = words[0]
pass = words[1]
bot.say(bot.admin_channel, "wifi peg "+JSON.stringify(words))
} else {
bot.say(bot.admin_channel, "wifi pegerr "+JSON.stringify(tree))
}
bot.say(bot.admin_channel, "wifi parsed name "+JSON.stringify(name)+" pass "+JSON.stringify(pass))
if(name) {
report(msg, name, pass)
} else {
help(msg)
}
}
}
}
function report(msg, nickname, password) {
if (password) {
var setmsg = msg.params.nick+": "+set(nickname, null, password)+" Service guarantees citizenship."
bot.say(msg.params.channel, setmsg)
} else {
// async so no return value :(
search(msg, nickname)
}
}
function set(nickname, apname, password) {
var np = {essid: apname, password: password}
var json = JSON.stringify(np)
var lowername = nickname.toLowerCase()
var key = 'wifi:'+lowername
db.set(key, json)
bot.say(bot.admin_channel, "wifi saved: "+key+" = "+json)
return "The wifi password for "+nickname+" has been recorded."
}
function search(msg, nickname) {
var lowername = nickname.toLowerCase()
if(nickname.indexOf('*') >= 0) {
scan(lowername, msg)
} else {
db.get('wifi:'+lowername, function(value) {
if(value) {
var data = JSON.parse(value)
var message = winner(lowername, data)
bot.say(msg.params.channel, msg.params.nick+": "+message)
} else {
//help(msg, nickname)
var scannick = '*'+nickname+'*'
//bot.say(msg.params.channel, 'trying again with '+scannick)
scan(scannick.toLowerCase(), msg)
}
})
}
}
function help(msg, nickname) {
nickname = nickname || "nickname"
bot.say(msg.params.channel, "no wifi data. search with !wifi *"+nickname+"*. set with !wifi "+nickname+" <password>")
}
function winner(name, data) {
var message = name+" has a password of "+JSON.stringify(data.password)
if(data.essid) { message += " on network "+JSON.stringify(data.essid)+"" }
return message
}
function scan(match, msg, cursor, answers, loop) {
if(cursor == null) { cursor = 0; answers = []; loop = 1 }
loop = loop + 1
db.scan(cursor, prefix+match, 100, function(result){
cursor = result[0]
answers = answers.concat(result[1])
var count = answers.length/2
if(cursor == 0) {
var names = []//wut answers.reduce(function(m,e){return m.push(e)}, [])
answers.forEach(function(ab, idx){ if(idx % 2 == 0) { names.push(ab.substr(prefix.length)) } })
if (names.length > 1) {
bot.say(msg.params.channel, msg.params.nick+": found "+names.length+" matches: "+names.join(', '))
}
if (names.length == 1) {
db.get('wifi:'+names[0], function(value) {
var data = JSON.parse(value)
var message = winner(names[0], data)
bot.say(msg.params.channel, msg.params.nick+": "+message)
})
}
} else {
if(loop < 1500) {
scan(match, msg, cursor, answers, loop)
} else {
bot.say(msg.params.channel, msg.params.nick+": wifi scan overload. stopping.")
}
}
})
}
// http://canopy.jcoglan.com/
//GRAMMAR quotes
// root <- (word [ ]*)*
// word <- quotedword / unquotedword
// unquotedword <- noquotenospace+
// quotedword <- ["] noquote+ ["]
// noquote <- [A-Za-z0-9 !@#$%^&*()'_\-=+]
// noquotenospace <- [A-Za-z0-9!@#$%^&*()'_\-=+]
var quotes =
(function() {
'use strict';
var extend = function (destination, source) {
if (!destination || !source) return destination;
for (var key in source) {
if (destination[key] !== source[key])
destination[key] = source[key];
}
return destination;
};
var formatError = function (input, offset, expected) {
var lines = input.split(/\n/g),
lineNo = 0,
position = 0;
while (position <= offset) {
position += lines[lineNo].length + 1;
lineNo += 1;
}
var message = 'Line ' + lineNo + ': expected ' + expected.join(', ') + '\n',
line = lines[lineNo - 1];
message += line + '\n';
position -= line.length + 1;
while (position < offset) {
message += ' ';
position += 1;
}
return message + '^';
};
var inherit = function (subclass, parent) {
var chain = function() {};
chain.prototype = parent.prototype;
subclass.prototype = new chain();
subclass.prototype.constructor = subclass;
};
var TreeNode = function(text, offset, elements) {
this.text = text;
this.offset = offset;
this.elements = elements || [];
};
TreeNode.prototype.forEach = function(block, context) {
for (var el = this.elements, i = 0, n = el.length; i < n; i++) {
block.call(context, el[i], i, el);
}
};
var TreeNode1 = function(text, offset, elements) {
TreeNode.apply(this, arguments);
this['word'] = elements[0];
};
inherit(TreeNode1, TreeNode);
var FAILURE = {};
var Grammar = {
_read_root: function() {
var address0 = FAILURE, index0 = this._offset;
this._cache._root = this._cache._root || {};
var cached = this._cache._root[index0];
if (cached) {
this._offset = cached[1];
return cached[0];
}
var remaining0 = 0, index1 = this._offset, elements0 = [], address1 = true;
while (address1 !== FAILURE) {
var index2 = this._offset, elements1 = new Array(2);
var address2 = FAILURE;
address2 = this._read_word();
if (address2 !== FAILURE) {
elements1[0] = address2;
var address3 = FAILURE;
var remaining1 = 0, index3 = this._offset, elements2 = [], address4 = true;
while (address4 !== FAILURE) {
var chunk0 = null;
if (this._offset < this._inputSize) {
chunk0 = this._input.substring(this._offset, this._offset + 1);
}
if (chunk0 !== null && /^[ ]/.test(chunk0)) {
address4 = new TreeNode(this._input.substring(this._offset, this._offset + 1), this._offset);
this._offset = this._offset + 1;
} else {
address4 = FAILURE;
if (this._offset > this._failure) {
this._failure = this._offset;
this._expected = [];
}
if (this._offset === this._failure) {
this._expected.push('[ ]');
}
}
if (address4 !== FAILURE) {
elements2.push(address4);
--remaining1;
}
}
if (remaining1 <= 0) {
address3 = new TreeNode(this._input.substring(index3, this._offset), index3, elements2);
this._offset = this._offset;
} else {
address3 = FAILURE;
}
if (address3 !== FAILURE) {
elements1[1] = address3;
} else {
elements1 = null;
this._offset = index2;
}
} else {
elements1 = null;
this._offset = index2;
}
if (elements1 === null) {
address1 = FAILURE;
} else {
address1 = new TreeNode1(this._input.substring(index2, this._offset), index2, elements1);
this._offset = this._offset;
}
if (address1 !== FAILURE) {
elements0.push(address1);
--remaining0;
}
}
if (remaining0 <= 0) {
address0 = new TreeNode(this._input.substring(index1, this._offset), index1, elements0);
this._offset = this._offset;
} else {
address0 = FAILURE;
}
this._cache._root[index0] = [address0, this._offset];
return address0;
},
_read_word: function() {
var address0 = FAILURE, index0 = this._offset;
this._cache._word = this._cache._word || {};
var cached = this._cache._word[index0];
if (cached) {
this._offset = cached[1];
return cached[0];
}
var index1 = this._offset;
address0 = this._read_quotedword();
if (address0 === FAILURE) {
this._offset = index1;
address0 = this._read_unquotedword();
if (address0 === FAILURE) {
this._offset = index1;
}
}
this._cache._word[index0] = [address0, this._offset];
return address0;
},
_read_unquotedword: function() {
var address0 = FAILURE, index0 = this._offset;
this._cache._unquotedword = this._cache._unquotedword || {};
var cached = this._cache._unquotedword[index0];
if (cached) {
this._offset = cached[1];
return cached[0];
}
var remaining0 = 1, index1 = this._offset, elements0 = [], address1 = true;
while (address1 !== FAILURE) {
address1 = this._read_noquotenospace();
if (address1 !== FAILURE) {
elements0.push(address1);
--remaining0;
}
}
if (remaining0 <= 0) {
address0 = new TreeNode(this._input.substring(index1, this._offset), index1, elements0);
this._offset = this._offset;
} else {
address0 = FAILURE;
}
this._cache._unquotedword[index0] = [address0, this._offset];
return address0;
},
_read_quotedword: function() {
var address0 = FAILURE, index0 = this._offset;
this._cache._quotedword = this._cache._quotedword || {};
var cached = this._cache._quotedword[index0];
if (cached) {
this._offset = cached[1];
return cached[0];
}
var index1 = this._offset, elements0 = new Array(3);
var address1 = FAILURE;
var chunk0 = null;
if (this._offset < this._inputSize) {
chunk0 = this._input.substring(this._offset, this._offset + 1);
}
if (chunk0 !== null && /^["]/.test(chunk0)) {
address1 = new TreeNode(this._input.substring(this._offset, this._offset + 1), this._offset);
this._offset = this._offset + 1;
} else {
address1 = FAILURE;
if (this._offset > this._failure) {
this._failure = this._offset;
this._expected = [];
}
if (this._offset === this._failure) {
this._expected.push('["]');
}
}
if (address1 !== FAILURE) {
elements0[0] = address1;
var address2 = FAILURE;
var remaining0 = 1, index2 = this._offset, elements1 = [], address3 = true;
while (address3 !== FAILURE) {
address3 = this._read_noquote();
if (address3 !== FAILURE) {
elements1.push(address3);
--remaining0;
}
}
if (remaining0 <= 0) {
address2 = new TreeNode(this._input.substring(index2, this._offset), index2, elements1);
this._offset = this._offset;
} else {
address2 = FAILURE;
}
if (address2 !== FAILURE) {
elements0[1] = address2;
var address4 = FAILURE;
var chunk1 = null;
if (this._offset < this._inputSize) {
chunk1 = this._input.substring(this._offset, this._offset + 1);
}
if (chunk1 !== null && /^["]/.test(chunk1)) {
address4 = new TreeNode(this._input.substring(this._offset, this._offset + 1), this._offset);
this._offset = this._offset + 1;
} else {
address4 = FAILURE;
if (this._offset > this._failure) {
this._failure = this._offset;
this._expected = [];
}
if (this._offset === this._failure) {
this._expected.push('["]');
}
}
if (address4 !== FAILURE) {
elements0[2] = address4;
} else {
elements0 = null;
this._offset = index1;
}
} else {
elements0 = null;
this._offset = index1;
}
} else {
elements0 = null;
this._offset = index1;
}
if (elements0 === null) {
address0 = FAILURE;
} else {
address0 = new TreeNode(this._input.substring(index1, this._offset), index1, elements0);
this._offset = this._offset;
}
this._cache._quotedword[index0] = [address0, this._offset];
return address0;
},
_read_noquote: function() {
var address0 = FAILURE, index0 = this._offset;
this._cache._noquote = this._cache._noquote || {};
var cached = this._cache._noquote[index0];
if (cached) {
this._offset = cached[1];
return cached[0];
}
var chunk0 = null;
if (this._offset < this._inputSize) {
chunk0 = this._input.substring(this._offset, this._offset + 1);
}
if (chunk0 !== null && /^[A-Za-z0-9 !@#$%^&*()'_\-=+]/.test(chunk0)) {
address0 = new TreeNode(this._input.substring(this._offset, this._offset + 1), this._offset);
this._offset = this._offset + 1;
} else {
address0 = FAILURE;
if (this._offset > this._failure) {
this._failure = this._offset;
this._expected = [];
}
if (this._offset === this._failure) {
this._expected.push('[A-Za-z0-9 !@#$%^&*()\'_\\-=+]');
}
}
this._cache._noquote[index0] = [address0, this._offset];
return address0;
},
_read_noquotenospace: function() {
var address0 = FAILURE, index0 = this._offset;
this._cache._noquotenospace = this._cache._noquotenospace || {};
var cached = this._cache._noquotenospace[index0];
if (cached) {
this._offset = cached[1];
return cached[0];
}
var chunk0 = null;
if (this._offset < this._inputSize) {
chunk0 = this._input.substring(this._offset, this._offset + 1);
}
if (chunk0 !== null && /^[A-Za-z0-9!@#$%^&*()'_\-=+]/.test(chunk0)) {
address0 = new TreeNode(this._input.substring(this._offset, this._offset + 1), this._offset);
this._offset = this._offset + 1;
} else {
address0 = FAILURE;
if (this._offset > this._failure) {
this._failure = this._offset;
this._expected = [];
}
if (this._offset === this._failure) {
this._expected.push('[A-Za-z0-9!@#$%^&*()\'_\\-=+]');
}
}
this._cache._noquotenospace[index0] = [address0, this._offset];
return address0;
}
};
var Parser = function(input, actions, types) {
this._input = input;
this._inputSize = input.length;
this._actions = actions;
this._types = types;
this._offset = 0;
this._cache = {};
this._failure = 0;
this._expected = [];
};
Parser.prototype.parse = function() {
var tree = this._read_root();
if (tree !== FAILURE && this._offset === this._inputSize) {
return tree;
}
if (this._expected.length === 0) {
this._failure = this._offset;
this._expected.push('<EOF>');
}
this.constructor.lastError = {offset: this._offset, expected: this._expected};
return formatError(this._input, this._failure, this._expected);
};
var parse = function(input, options) {
options = options || {};
var parser = new Parser(input, options.actions, options.types);
return parser.parse();
};
extend(Parser.prototype, Grammar);
return {Grammar: Grammar, Parser: Parser, parse: parse};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment