This repository has been 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
/
handlers.coffee
124 lines (107 loc) · 3.62 KB
/
handlers.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
###
# 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/>.
###
common = require './common'
config = require './config'
winston = require 'winston'
debug = (require 'debug')('Chatbot:handlers')
async = require 'async'
loadedHandlers = {}
loadHandlers = ->
winston.info "Loading message handlers"
for name in [ 'opserv', 'core' ]
loadHandler name, (err) ->
return unless err?
common.fatal "panic() - Going nowhere without my #{name}"
async.each config.handlers, loadHandler, (err) ->
winston.info "Finished loading handlers"
loadHandler = (name, callback) ->
debug "Loading handler: #{name}"
unless /^[a-z]+$/.test name
winston.warn "Trying to load invalid named handler", name
callback? "invalid"
return
if loadedHandlers[name]?
winston.warn "Trying to load loaded handler", name
callback? "loaded"
else
try
loadedHandlers[name] = require './handlers/' + name
if loadedHandlers[name].handleMessage? and loadedHandlers[name].handleUser?
if loadedHandlers[name].onLoad?
loadedHandlers[name].onLoad callback
else
callback?()
else
winston.error "Invalid handler, unloading:", name
unloadHandler name
callback? "invalid"
catch e
winston.error "Failed to compile handler “#{name}”: #{e.message}"
unloadHandler name
callback? "compile"
unloadHandler = (name, callback) ->
if name in [ 'core', 'opserv' ]
winston.warn "Trying to unload", name
callback? "permissionDenied"
return
debug "Unloading handler: #{name}"
if loadedHandlers[name]?
if loadedHandlers[name].unload?
loadedHandlers[name].unload -> callback?()
else
callback?()
delete loadedHandlers[name]
else
winston.warn "Trying to unload unloaded handler", name
callback? "notLoaded"
purgeHandler = (name, callback) ->
debug "Purging handler: #{name}"
if loadedHandlers[name]?
if loadedHandlers[name].purge?
handler = loadedHandlers[name]
unloadHandler name, (err) ->
unless err?
handler.purge (err) ->
if err?
debug "Purging of handler “#{name}” failed: #{err}"
callback? err
else
callback?()
else
callback? err
else
winston.warn "Handler “#{name}” doesn't have a purge function!", name
callback? "noFunc"
else
winston.warn "Trying to purge unloaded handler", name
callback? "unloaded"
getLoadedHandlers = -> k for k of loadedHandlers
# calls handleMessage of each handler and calls the callback after every handler handled it
handleMessage = (message, callback) ->
async.applyEach (v.handleMessage for k, v of loadedHandlers), message, callback
# calls handleUser of each handler and calls the callback after every handler handled it
handleUser = (user, callback) ->
async.applyEach (v.handleUser for k, v of loadedHandlers), user, callback
module.exports =
loadHandlers: loadHandlers
loadHandler: loadHandler
unloadHandler: unloadHandler
purgeHandler: purgeHandler
getLoadedHandlers: getLoadedHandlers
handleMessage: handleMessage
handleUser: handleUser