-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
179 lines (159 loc) · 5.13 KB
/
server.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
'use strict'
let Command = require('./commands/Command').Command
let netflux = require('netflux')
let MessageModel = require('./model/message')
// Configuration of the default WebSocketServer
let host = '127.0.0.1'
let port = 9000
// Saving status of the bot server
let save = false
// Protocol of the messages
let netfluxChat = true
// Streams objects for twitter and slack connections
let twitterStream = null
let slackStream = null
// Map of users (Peer id => name) for the slack connection
let users = new Map()
// Constants for the database storing
const MESSAGE = 'MESSAGE'
const COMMAND = 'COMMAND'
const JOIN = 'JOIN'
const LEAVE = 'LEAVE'
// Check arguments to configure the bot server
process.argv.forEach((value, index, array) => {
if (value.match('-(h|-host)')) {
host = array[index + 1]
} else if (value.match('-(p|-port)')) {
port = array[index + 1]
} else if (value.match('-(s|-save)')) {
save = (array[index + 1] === 'true')
} else if (value.match('-(nc|-netfluxChat)')) {
netfluxChat = !(array[index + 1] === 'false')
}
})
// Function to launch the bot server
let init = () => {
let bot = new netflux.Bot()
bot.listen({host, port, log: true})
.then(() => {
console.log('Bot listen on ws://' + bot.settings.host + ':' + bot.settings.port)
})
// Once the bot is added to a WebChannel this function is called
bot.onWebChannel = (wc) => {
let id = wc.myId
/**
* Send the message to the WebChannel
* @param {string} msg - Message to be send
* @param {number} toIdUser - Id of the recipient peer (default = 0)
*/
let send = (msg, toIdUser = 0) => {
if (netfluxChat) {
// Protocol of Netflux-chat messages
var message = {
'type': 'message',
'data': {
'fromIdUser': id,
'toIdUser': '' + toIdUser,
'content': msg,
'date': Date.now()
}
}
wc.send(JSON.stringify(message))
} else wc.send(msg)
if (save) MessageModel.saveMsg(new MessageModel.Message({label: MESSAGE, content: msg, fromId: id}))
}
/**
* Send a hello world message and user informations for Netflux-chat
*/
let hello = () => {
if (netfluxChat) {
var userInfos = {
'type': 'userInfos',
'data': {
'backgroundColor': '',
'textColor': '58ACFA',
'whispColor': 'bbbbbb',
'id': '' + id,
'peerId': id,
'nickname':
'Server :desktop:',
'online': true
}
}
wc.send(JSON.stringify(userInfos))
}
send('Hello, I\'m a server')
}
/**
* Analyse a message to find commands to execute
* @param {string} str - Message to analyse
* @param {number} fromId - Id of the peer who sent the message
*/
let analyse = (str, fromId) => {
let label = MESSAGE
let regexCommands = /\/(\w+)\s*((?:(?:([\w\d]|\#)+|\"([^"]|\')+\"|-(\w+|-\w+))\s*)*)/g
let regexArguments = /(?:([\w\d]|\#)+|\"([^"]|\')+\"|-\w+)/g
let commands
if (str !== undefined && typeof str === 'string') {
commands = str.match(regexCommands)
}
if (commands) {
commands.forEach((cmd, index, array) => {
label = COMMAND
let args = cmd.match(regexArguments)
let name = args.splice(0, 1)[0]
if (save) {
let content = name
content += (args.length !== 0) ? ' ' + args.join(' ') : ''
MessageModel.saveMsg(new MessageModel.Message({label, content, fromId}))
}
let c = new Command({name, args, fromId, save, send, wc, bot,
twitterStream, slackStream, users, netfluxChat})
c.exec()
.then(() => {
save = c.getSave()
twitterStream = c.getTwitterStream()
slackStream = c.getSlackStream()
users = c.getUsers()
})
})
} else if (slackStream !== null) slackStream.sendToSlack({message: str, id: fromId})
if (save && label === MESSAGE) {
MessageModel.saveMsg(new MessageModel.Message({label, content: str, fromId}))
}
}
// If someone join the WebChannel this function is called
wc.onJoining = (id) => {
if (save) {
MessageModel.saveMsg(new MessageModel.Message({label: JOIN, content: id}))
.onResolve(() => send('Welcome'))
}
else send('Welcome')
}
// If someone leave the WebChannel this function is called
wc.onLeaving = (id) => {
if (save) MessageModel.saveMsg(new MessageModel.Message({label: LEAVE, content: id}))
}
// If someone send a message to the WebChannel this function is called
wc.onMessage = (id, msg) => {
let content
if (netfluxChat) content = JSON.parse(msg).data.content
else content = msg
analyse(content, id)
}
hello()
}
}
try {
let mongoose = require('mongoose')
// mongoose.connect('mongodb://localhost/netfluxBotLog', function (err) {
// if (err) { throw err }
// })
//
// mongoose.connection.on('connected', () => {
// init()
// })
} catch (err) {
init()
}
init()