forked from robotarmy/bunuq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtocho.js
131 lines (107 loc) · 3.32 KB
/
tocho.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// load node-irc & other stuff
var irc = require('irc')
var fs = require('fs')
var json = JSON.stringify
var config = require('./lib/config')
var BangLast = require('./lib/bang_last')
var LastLog = require('./lib/last_log')
var SpeakTo = require('./lib/speak_to')
var error_f = function(err) {
if (err)
throw err
}
if(config.ircbot() == true) {
var bot = new irc.Client(config.server(), config.nick(), {
channels: config.channels(),
})
var speaker = SpeakTo.create(bot) // irc client
// logging
if (config.logmode() == 'nstore') {
var nStore = require('nstore')
var logfile = nStore.new(config.logfile(),error_f)
} else {
var logfile = config.logfile()
var log_fd
fs.open(logfile, 'a', mode=0666, function(err, fd) {
log_fd = fd
})
}
// load welcome message
console.log('tocho is getting ready to listen & log...')
// temp auth
var auth = config.auth()
//=== !last
var lastlog = LastLog.create(1000)
var bang = BangLast.create(lastlog)
var speak_if_bang = SpeakTo.bang_speaker(bot,bang)
// load messages to screen
bot.addListener('message', function (from, to, message) {
json_message = {date:Date(), from:from, message:message }
speak_if_bang(from,message) // run query first
lastlog.add(json_message)
if (config.logmode() == 'nstore') {
logfile.save(null, json_message ,error_f)
} else {
var log_message = (Date() + '__' + from + ':' + message + '\n')
fs.write(log_fd, log_message, encoding='utf8')
}
// if socket is on
if (!(config.webserver() == false)) {
socket.broadcast(json(json_message))
}
})
bot.addListener('pm', function (from, message) {
console.log(from + ' => TOCHO: ' + message)
if (config.logmode() == 'nstore') {
logfile.save(null, {date:Date(), from:from, message:message, private: true },error_f)
} else {
var log_message = (Date() + '__' + from + ':' + message + '\n')
fs.write(log_fd, log_message, encoding='utf8')
}
})
if(auth == true)
setTimeout(authbot, 20000)
function authbot() {
bot.say('nickserv', ("identify "+config.password()))
console.log("AUTH SENT.")
}
// web push via Socket.IO
// -- not ready yet.
if(config.webserver() == true) {
var http = require('http')
var io = require('socket.io')
var connect = require('connect')
var express= require('express')
// initalizing via express
var webpush = express.createServer(
connect.staticProvider(__dirname + '/static/')
)
webpush.set('view engine', 'jade')
webpush.get('/', function(req, res) {
// home route for socket.io connection to channel
res.render('index', {layout:false})
})
webpush.get('/search/:author', function(req, res) {
var author = req.params.author
console.log("request for conversations by " + author)
var mydata = []
var logSearch = require('./lib/logsearch')
var search_inst = new logSearch()
conversations = search_inst.results(config.logfile(),author)
conversations.on('data', function(chunk) {
for(var i in chunk)
mydata.push(chunk[i].message)
res.render('search', {layout:false, locals:{mydata:mydata,author:author}})
})
})
webpush.listen(8080)
console.log('tocho is listening to the web on 8080...')
var socket = io.listen(webpush)
socket.on('connection', function() {
console.log('incoming client...')
})
// socket methods
} else {
var socket = false
}
}