-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.coffee
More file actions
51 lines (39 loc) · 1.24 KB
/
index.coffee
File metadata and controls
51 lines (39 loc) · 1.24 KB
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
util = require 'util'
junction = require 'junction'
Room = require './room'
module.exports = class MucHandler
constructor: (@connection) ->
@rooms = {}
setConnection: (connection) ->
@connection = connection
handle: (stanza, res, next) ->
roomId = stanza.from.split("/")[0]
room = @rooms[roomId]
if not room then return next()
if stanza.attrs.type is 'error'
room.errorHandler(stanza)
return next()
switch stanza.name
when 'presence' then @handlePresence room, stanza
when 'message' then room.messageHandler(stanza)
next()
handlePresence: (room, stanza) ->
@connection ?= stanza.connection
switch stanza.attrs.type
when 'unavailable' then room.unavailableHandler(stanza)
when 'error' then room.errorHandler(stanza)
else room.availableHandler(stanza)
joinRoom: (roomId, nick) ->
if not @connection
return console.error "No connection found. Call mucHandler.setConnection() to provide a connection object."
@connection.send new junction.elements.Presence(roomId + "/" + nick)
@addRoom(roomId)
addRoom: (roomId) ->
@rooms[roomId] = new Room(roomId)
partRoom: (roomId) ->
@rooms[roomId].part()
@removeRoom(room.roomId)
removeRoom: (roomId) ->
room = @rooms[roomId]
delete @rooms[roomId]
room