This repository was archived by the owner on Nov 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.coffee
103 lines (84 loc) · 3.08 KB
/
app.coffee
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
###
# Chatbot for Tim's Chat 3
# Copyright (C) 2011 - 2014 Tim Düsterhus
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###
winston = require 'winston'
winston.remove winston.transports.Console
winston.add winston.transports.Console,
colorize: yes
timestamp: yes
level: 'debug'
winston.info "Starting, PID #{process.pid}"
fs = require 'fs'
async = require 'async'
request = require 'request'
jar = request.jar()
jar._jar.rejectPublicSuffixes = false
request = request.defaults jar: jar
config = require './config'
process.title = "Chatbot (#{config.host})"
common = require './common'
common.request = request
config.host = config.host.replace /\/+$/, ''
config.handlers ?= [ ]
config.port ?= 9001
config.ip ?= '0.0.0.0'
config.enableFrontend ?= yes
config.database ?= __dirname + '/storage.sqlite3'
config.locale ?= 'en'
config.securityToken = ''
config.userID = null
config.upSince = new Date()
i18n = require './i18n'
db = require './db'
api = require './api'
handlers = require './handlers'
# gracefully leave chat and exit upon SIGTERM and SIGINT
process.on 'SIGTERM', -> api.leaveChat -> process.exit 0
process.on 'SIGINT', -> api.leaveChat -> process.exit 0
# gracefully exit chat upon encountering an uncaughtException
process.once 'uncaughtException', (err) ->
console.log 'Uncaught exception:'
console.log err.stack
api.leaveChat -> process.exit 0
api.fetchSecurityToken -> api.sendLoginRequest ->
# new session after login, refetch token
api.fetchSecurityToken -> api.getRoomList (roomList) ->
# probably no permissions, abort
common.fatal 'No available rooms' if roomList.length is 0
do (require './frontend').listen if config.enableFrontend
api.joinRoom roomList[0].roomID, ->
do handlers.loadHandlers
api.sendMessage i18n.__("I'm here!"), yes, ->
api.recursiveFetchMessages (data) ->
completedUsers = no
completedMessages = no
async.each data.messages, (item, callback) ->
handlers.handleMessage item, callback
, (err) ->
completedMessages = yes
#console.log "Handled each message"
async.each data.users, (item, callback) ->
handlers.handleUser item, callback
, (err) ->
completedUsers = yes
#console.log "Handled each message"
setTimeout ->
unless completedMessages
winston.warn "Handling messages takes very long, there might be a bug in a handler!"
unless completedUsers
winston.warn "Handling users takes very long, there might be a bug in a handler!"
, 10e3