-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
146 lines (129 loc) · 4.49 KB
/
index.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var express = require('express')
, serveStatic = require('serve-static')
, errorHandler = require('errorhandler')
, Emitter = require('primus-emitter')
, Primus = require('primus')
, xmpp = require('xmpp-ftw')
, Buddycloud = require('xmpp-ftw-buddycloud')
, helmet = require('helmet')
, index = require('./src/routes/index-get')
, account = require('./src/routes/account')
, fault = require('./src/fault')
, debug = require('debug')('wifi-chat:index')
, bodyParser = require('body-parser')
, report = require('./src/report')
require('colors')
var environment = process.env.NODE_ENV || 'production'
try {
var config = require('./config.' + environment + '.js')
config.environment = environment
} catch (e) {
console.log(('Config file config.' + environment + '.js not found').red)
process.exit(1)
}
var setConfigs = function() {
account.setConfig(config)
index.setConfig(config)
report.setConfig(config)
fault.setConfig(config)
}
setConfigs()
var app = express()
var server = app.listen(3000, function() {
debug('Listening on port %d', server.address().port)
})
app.disable('x-powered-by')
app.use(serveStatic(__dirname + '/public'))
app.use(errorHandler({
dumpExceptions: ('development' === environment),
showStack: ('development' === environment)
}))
app.use(helmet())
app.use(bodyParser.json())
app.set('strict routing', false)
var router = express.Router()
router.post('/account', account.createAccount)
router.post('/account/reset', account.generateResetPasswordToken)
router.post('/account/reset/:token', account.resetPassword)
router.get('/*', index.route)
app.use('/', router)
var options = {
transformer: 'engine.io',
parser: 'JSON',
transports: [
/* 'websocket', */
'xhr-polling',
'htmlfile',
'jsonp-polling'
]
}
var primus = new Primus(server, options)
primus.use('emitter', Emitter)
primus.save(__dirname + '/public/scripts/primus.js')
/**
* Prevent users from posting new threads
*/
Buddycloud.prototype.publishItem = function(data, callback) {
if (!this._checkCall(data, callback)) return
if (!data.content ||
!data.content['in-reply-to'] ||
(data.content['in-reply-to'].length < 5)) {
this._clientError(
'You are not able to create new topics currently', data, callback
)
}
this.publish(data, callback)
}
Buddycloud.prototype._events = {
'xmpp.buddycloud.discover': 'discover',
'xmpp.buddycloud.register': 'setRegister',
'xmpp.buddycloud.presence': 'setPresence',
'xmpp.buddycloud.publish': 'publishItem',
'xmpp.buddycloud.retrieve': 'retrieve',
'xmpp.buddycloud.items.recent': 'recentItems',
'xmpp.buddycloud.items.feed': 'userFeedItems',
'xmpp.buddycloud.items.replies': 'getReplies',
'xmpp.buddycloud.items.thread': 'getThread',
'xmpp.buddycloud.config.set': 'setConfiguration',
'xmpp.buddycloud.config.get': 'getConfiguration',
'xmpp.buddycloud.subscriptions': 'getNodeSubscriptions',
'xmpp.buddycloud.affiliations': 'getNodeAffiliations',
'xmpp.buddycloud.discover.items': 'discoverItems',
'xmpp.buddycloud.discover.info': 'discoverFeatures',
'xmpp.buddycloud.discover.media-server': 'discoverMediaServer',
'xmpp.buddycloud.search.get': 'searchGet',
'xmpp.buddycloud.search.do': 'performSearch',
'xmpp.buddycloud.register.get': 'getRegister',
'xmpp.buddycloud.register.set': 'setRegister',
'xmpp.buddycloud.register.password': 'changePassword',
'xmpp.buddycloud.http.confirm': 'approveRequest',
'xmpp.buddycloud.http.deny': 'denyRequest'
}
var buddycloudCache = {}
primus.on('connection', function(socket) {
debug('Websocket connection made')
var xmppFtw = new xmpp.Xmpp(socket)
var buddycloud = new Buddycloud()
buddycloud.setDiscoveryTimeout(10000)
buddycloud.setMediaServerDiscoveryTimeout(5000)
buddycloud.setCache(buddycloudCache)
xmppFtw.addListener(buddycloud)
socket.xmppFtw = xmppFtw
socket.on('message.report', function(data, callback) {
try {
data.reportedBy = xmppFtw.getJidType('bare')
report.sendReport(data, callback)
} catch(e) {
debug('Error with report email, likely client is not connected')
debug(e)
}
})
socket.on('ticket.create', function(data, callback) {
data.username = xmppFtw.getJidType('bare')
fault.sendReport(data, callback)
})
})
primus.on('disconnection', function(socket) {
debug('Client disconnected, logging them out')
socket.xmppFtw.logout()
})