Skip to content

Commit

Permalink
Hacked together name->number hash persistance
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffrey Lin committed Oct 2, 2011
1 parent bbee029 commit a9bc38b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.DS_Store
*.swp
config.js
pingbot.db
numbers.db
34 changes: 29 additions & 5 deletions irc-pingbot.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Modules
var config = require('./config');
var fs = require('fs');
var IRC = require('irc-js');
var tropo_backend = require('./tropo-backend');
var util = require('util');
Expand All @@ -19,7 +20,9 @@ var ADD_TRIGGER_REGEX =
new RegExp('^'+NICK+'[^ A-Za-z0-9] add ([^ ]*) ([0-9]{10}$)');
var LIST_TRIGGER_REGEX =
new RegExp('^'+NICK+'[^ A-Za-z0-9] list', 'i');
var NAME_NUMBER_MAPPING = config.NAME_NUMBER_MAPPING;
var NAME_NUMBER_MAPPING_FILE = config.NAME_NUMBER_MAPPING_FILE;
var NAME_NUMBER_MAPPING = {};
loadNumbers(NAME_NUMBER_MAPPING_FILE);

var DEBUGGING = config.DEBUGGING;
var dbg = config.dbg;
Expand Down Expand Up @@ -88,7 +91,7 @@ function getOptions(msg){

function getTropoSMSOptions(options){
return {
to: (NAME_NUMBER_MAPPING[options.to] || null),
to: (getNumber(options.to) || null),
network: 'SMS',
say: (options.msg || null)
};
Expand All @@ -99,8 +102,8 @@ function handlePossibleAdd(msg){
if(match){
var key = match[1];
var value = '1'+match[2]; // make sure we add a 1 in front of the phone #
dbg('Mapping '+value+' to key '+key+' in NAME_NUMBER_MAPPING');
NAME_NUMBER_MAPPING[key] = value;
dbg('Mapping '+value+' to key '+key);
setNumber(key, value);
var replyTo = getReplyTo(msg);
irc.privmsg(replyTo, key+' now mapped to '+value);
return true;
Expand All @@ -112,7 +115,7 @@ function handlePossibleList(msg){
var match = msg.content.match( LIST_TRIGGER_REGEX );
if(match){
var replyTo = getReplyTo(msg);
var nicks = Object.keys(NAME_NUMBER_MAPPING).join(', ');
var nicks = getNumberKeys().join(', ');
irc.privmsg(replyTo, 'I have numbers for '+nicks+'.');
return true;
}
Expand All @@ -132,5 +135,26 @@ function isDuplicate(sessionID, msg){
return false;
}

function loadNumbers(){
NAME_NUMBER_MAPPING = JSON.parse(
fs.readFileSync(NAME_NUMBER_MAPPING_FILE, 'utf-8')
);
}

function setNumber(key, value){
NAME_NUMBER_MAPPING[key] = value;
fs.writeFileSync(
// this isn't async - boohoo... =P
NAME_NUMBER_MAPPING_FILE,
JSON.stringify(NAME_NUMBER_MAPPING),
'utf-8'
);
}

function getNumber(key){
return NAME_NUMBER_MAPPING[key];
}

function getNumberKeys(){
return Object.keys(NAME_NUMBER_MAPPING);
}

0 comments on commit a9bc38b

Please sign in to comment.