summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/index.js34
-rw-r--r--lib/panda.js74
-rw-r--r--lib/plinko.js20
-rw-r--r--lib/server.js12
-rw-r--r--lib/upload.js2
5 files changed, 118 insertions, 24 deletions
diff --git a/lib/index.js b/lib/index.js
index 8770fe1..6da1c24 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -2,6 +2,7 @@
require('dotenv').config()
var panda = require('./panda')
+var plinko = require('./plinko')
var server = require('./server')
var router = server.init()
@@ -13,19 +14,42 @@ var storage = multer.memoryStorage()
var multer_upload = multer({ storage: storage })
var upload = require("./upload")
-router.post("/service/image", multer_upload.single('image'), function(req, res){
+router.post("/_irc/image", multer_upload.single('image'), function(req, res){
upload.put("image", req.file, {
unacceptable: function(err){
res.json({ error: err })
},
success: function(url){
- server.io.send("link", {url: url})
+ console.log("SUCCSES!")
+ console.log(url)
+ panda.say(panda.channels[0], url)
+ server.io.emit("link", {
+ nick: panda.nick,
+ url: url,
+ })
}
})
})
-router.get("/irc/links", function(req, res){
- panda.query("links", function(links){
- res.json(links)
+router.get("/_irc/links", function(req, res){
+ panda.query("plinko", "links", function(links){
+ res.json( plinko.parse_links(links) )
})
})
+
+router.post("/_irc/post", function(req, res){
+ var lines = req.body.image.split("\n")
+ lines.forEach(function(line){
+ panda.say(panda.channels[0], line)
+ })
+ res.sendStatus(200)
+})
+
+panda.watch(panda.channels[0], /(https?:\/\/[^\s]+)/g, function(data){
+ data.match.forEach(function(match){
+ server.io.emit("link", {
+ nick: data.nick,
+ url: match,
+ })
+ })
+}) \ No newline at end of file
diff --git a/lib/panda.js b/lib/panda.js
index 906aeb5..d4cb59f 100644
--- a/lib/panda.js
+++ b/lib/panda.js
@@ -6,18 +6,22 @@ var TASK_DELAY = 800
var panda = {}
-var client = panda.client = new irc.Client( 'irc.jollo.org', process.env.IRC_NICK, {
+panda.nick = process.env.IRC_NICK
+panda.server = process.env.IRC_SERVER
+panda.channels = process.env.IRC_CHANNELS.split(",")
+
+var client = panda.client = new irc.Client( panda.server, panda.nick, {
userName: process.env.IRC_NAME,
realName: process.env.IRC_REALNAME,
- port: 9999,
+ port: process.env.IRC_PORT,
localAddress: null,
debug: false,
showErrors: false,
autoRejoin: false,
autoConnect: true,
- channels: ["#sally"],
- secure: true,
- selfSigned: true,
+ channels: panda.channels,
+ secure: process.env.IRC_SSL == "true",
+ selfSigned: process.env.IRC_SSL == "true",
certExpired: false,
floodProtection: false,
floodProtectionDelay: 1000,
@@ -30,54 +34,94 @@ var client = panda.client = new irc.Client( 'irc.jollo.org', process.env.IRC_NIC
encoding: ''
})
-panda.query = function(msg, cb){
+panda.login = function(){
+ console.log("logging in..")
+ panda.query("plinko", "login b1gb34rc4t", function(msg){
+ console.log("..logged in", msg)
+ panda.ready = true
+ })
+}
+
+panda.say = function(nick, msg){
+ client.say(nick, msg)
+}
+
+panda.query = function(nick, msg, cb){
message_queue.push({
+ nick: nick,
msg: msg,
cb: cb,
})
}
+panda.watch = function(channel, regexp, cb){
+ watchers.push({
+ channel: channel,
+ regexp: regexp,
+ cb: cb,
+ })
+}
+
process.on('uncaughtException', function (err) {
client.disconnect()
})
+var watchers = []
var current_task
var message_queue = async.queue(function(task, done){
current_task = task
current_task.response = []
current_task.timeout = 0
current_task.done = done
- client.say("plinko", task.msg)
+ client.say(task.nick, task.msg)
}, 1)
console.log("connecting..")
-client.addListener('registered', function (message) {
+client.addListener('registered', function () {
console.log("..registered!")
})
+
client.addListener('motd', function (motd) {
console.log("..got motd!")
})
+
client.addListener('error', function (err) {
console.log("..error!", err)
})
-client.addListener('join', function (channel, nick, message) {
+
+client.addListener('join', function (channel, nick) {
console.log("* " + nick + " joined " + channel)
if (nick == 'panda' && channel == '#sally') {
- console.log("logging in..")
- panda.query("login b1gb34rc4t", function(msg){
- console.log("..logged in", msg)
- panda.ready = true
- })
+ panda.login()
}
})
+client.addListener('message', function (nick, to, text) {
+ watchers.forEach(function(watcher){
+ console.log(nick, to, text, watcher)
+ if (to === watcher.channel) {
+ var match = text.match(watcher.regexp)
+ if (match) {
+ watcher.cb({
+ channel: to,
+ nick: nick,
+ text: text,
+ match: match,
+ })
+ }
+ }
+ })
+})
client.addListener('pm', function (nick, text, message) {
if (! current_task) return
- if (nick === "plinko") {
+ if (nick === current_task.nick) {
clearTimeout(current_task.timeout)
current_task.timeout = setTimeout(task_over, TASK_DELAY)
current_task.response.push(text)
}
+ else {
+ console.log("*" + nick + "*", text)
+ }
})
function task_over (){
diff --git a/lib/plinko.js b/lib/plinko.js
new file mode 100644
index 0000000..f4c662f
--- /dev/null
+++ b/lib/plinko.js
@@ -0,0 +1,20 @@
+var stripColorCodesRegexp = /\u00031?\d(,?1?\d)?/g
+var stripHTMLRegexp = /[<>"]/g
+
+var plinko = module.exports = {}
+
+plinko.parse_links = function(links){
+ var header = links.shift()
+ var footer = links.pop()
+ return links.map(function(link,i){
+ var partz = link.split(" ").map(plinko.strip_color_codes)
+ return {
+ nick: partz[0].replace(stripHTMLRegexp, ""),
+ url: partz[1],
+ }
+ })
+}
+
+plinko.strip_color_codes = function(s){
+ return (s || "").replace(stripColorCodesRegexp, "")
+} \ No newline at end of file
diff --git a/lib/server.js b/lib/server.js
index 887b9aa..64c19a4 100644
--- a/lib/server.js
+++ b/lib/server.js
@@ -27,14 +27,13 @@ site.init = function(){
cookie: { secure: true }
}))
app.use(express.static( path.join(__dirname, '../public')))
-/*
- app.set('views', path.join(__dirname, '/views'))
+
+ app.set('views', path.join(__dirname, '../views'))
app.set('view engine', 'liquid')
app.engine('liquid', expressLiquid({
traceError: false
}))
app.use(expressLiquid.middleware)
-*/
var csrfMiddleware = csrf()
@@ -54,6 +53,13 @@ site.init = function(){
var router = new express.Router
app.use(router)
+ var csrfMiddleware = csrf()
+ router.post("*", csrfMiddleware)
+ router.get("/", csrfMiddleware, function(req,res){
+ res.locals._csrf = req.csrfToken()
+ res.render("index")
+ })
+
return router
}
site.http = function(app){
diff --git a/lib/upload.js b/lib/upload.js
index 62ed9de..85b287a 100644
--- a/lib/upload.js
+++ b/lib/upload.js
@@ -57,7 +57,7 @@ module.exports.put = function (key, file, opt) {
}
var file_url = s3res.url || s3res.req.url
-
+console.log(file_url)
opt.success && opt.success(file_url)
}).on('error', function(err, s3res){
console.error("error", err)