fdongles
Version:
An IRC framework that supports plugin modules and middleware
93 lines (64 loc) • 2.21 kB
text/coffeescript
colors = require 'colors'
irc = require 'irc'
_ = require 'underscore'
util = require 'util'
class module.exports extends irc.Client
constructor: ->
= []
= {}
super
'error', (event) -> JSON.stringify event.command.toUpperCase()
module for module in .modules if .modules?
connect: ->
"Connecting to #{@opt.server}"
super
'registered', -> "Connected to #{@opt.server}"
log: -> util.log arg, colors: true for arg in arguments
info: ->
for arg in arguments
util.log 'Info: '.green + util.inspect arg, colors: true
warn: ->
for arg in arguments
util.log 'Warn: '.yellow + util.inspect arg, colors: true
error: ->
for arg in arguments
util.log 'Error: '.red + util.inspect arg, colors: true
load: (mod, cb) =>
try
module = require(mod)
catch err
msg = if err.code is 'MODULE_NOT_FOUND'
"Module #{mod} not found"
else
"Module #{mod} cannot be loaded"
msg
return cb?(msg)
if .hasOwnProperty mod
msg = "Module #{mod} already loaded"
msg
return cb?(msg)
"Loaded module #{mod}"
module = new Module(@) if typeof Module is 'function'
[mod] = module
module.init?(@)
cb? null
stop: (mod, cb) =>
unless .hasOwnProperty mod
msg = "Module #{mod} not loaded"
msg
return cb?(msg)
[mod].destruct?()
delete require.cache[require.resolve(mod)]
delete [mod]
"Stopped module #{mod}"
cb? null
addListener: (event, middlewares..., fn) ->
super event, fn, middlewares
on: -> arguments...
once: (event, middlewares..., fn) -> super event, fn, middlewares
wrap: (fn, middlewares) -> (args...) =>
_.reduceRight(.concat(middlewares), (memo, item) =>
next = => memo.apply @, args
return -> item.apply @, [args..., next]
, fn).apply @, arguments
use: -> .push arguments...