-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.js
174 lines (168 loc) · 4.68 KB
/
commands.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
var util = require('./util')
function Commander (view, cabal) {
if (!(this instanceof Commander)) return new Commander(view, cabal)
this.cabal = cabal
this.channel = '!status'
this.view = view
this.pattern = (/^\/(\w*)\s*(.*)/)
this.history = []
var self = this
this.commands = {
nick: {
help: () => 'change your display name',
call: (arg) => {
if (arg === '') return
self.cabal.publishNick(arg)
self.view.writeLine("* you're now known as " + arg)
}
},
emote: {
help: () => 'write an old-school text emote',
call: (arg) => {
self.cabal.publish({
type: 'chat/emote',
content: {
channel: self.channel,
text: arg
}
})
}
},
names: {
help: () => 'display the names of the currently online peers',
call: (arg) => {
self.cabal.users.getAll(function (err, users) {
if (err) { return }
var userkeys = Object.keys(users).map((key) => users[key]).sort(util.cmpUser)
self.view.writeLine('* history of peers in this cabal:')
userkeys.map((u) => {
var username = u.name || 'conspirator'
var spaces = ' '.repeat(15)
var paddedName = (username + spaces).slice(0, spaces.length)
self.view.writeLine.bind(self.view)(` ${paddedName} ${u.key}`)
})
})
}
},
channels: {
help: () => "display the cabal's channels",
call: (arg) => {
self.cabal.channels.get((err, channels) => {
if (err) return
self.view.writeLine('* channels:')
channels.map((m) => {
self.view.writeLine.bind(self.view)(` ${m}`)
})
})
}
},
join: {
help: () => 'join a new channel',
call: (arg) => {
if (arg === '') arg = 'default'
self.channel = arg
self.view.loadChannel(arg)
}
},
clear: {
help: () => 'clear the current backscroll',
call: (arg) => {
self.view.clear()
}
},
help: {
help: () => 'display this help message',
call: (arg) => {
for (var key in self.commands) {
self.view.writeLine.bind(self.view)(`/${key}`)
self.view.writeLine.bind(self.view)(` ${self.commands[key].help()}`)
}
self.view.writeLine.bind(self.view)(`alt-n`)
self.view.writeLine.bind(self.view)(` move between channels/cabals panes`)
self.view.writeLine.bind(self.view)(`ctrl+{n,p}`)
self.view.writeLine.bind(self.view)(` move up/down channels/cabals`)
}
},
quit: {
help: () => 'exit the cabal process',
call: (arg) => {
process.exit(0)
}
},
exit: {
help: () => 'exit the cabal process',
call: (arg) => {
process.exit(0)
}
},
topic: {
help: () => 'set the topic/description/`message of the day` for a channel',
call: (arg) => {
self.cabal.publishChannelTopic(self.channel, arg)
}
},
whoami: {
help: () => 'display your local user key',
call: (arg) => {
self.view.writeLine.bind(self.view)('Local user key: ' + self.cabal.client.user.key)
}
}
}
// add aliases to commands
this.alias('emote', 'me')
this.alias('join', 'j')
this.alias('nick', 'n')
this.alias('topic', 'motd')
this.alias('whoami', 'key')
// add in experimental commands
if (self.view.isExperimental) {
self.commands['add'] = {
help: () => 'add a cabal',
call: (arg) => {
if (arg === '') {
self.view.writeLine('* Usage example: /add cabalkey')
return
}
self.channel = arg
self.view.addCabal(arg)
}
}
self.alias('add', 'cabal')
}
}
Commander.prototype.alias = function (command, alias) {
var self = this
self.commands[alias] = {
help: self.commands[command].help,
call: self.commands[command].call
}
}
Commander.prototype.process = function (line) {
var self = this
line = line.trim()
self.history.push(line)
if (self.history.length > 1000) self.history.shift()
var match = self.pattern.exec(line)
var cmd = match ? match[1] : ''
var arg = match ? match[2] : ''
arg = arg.trim()
if (cmd in self.commands) {
self.commands[cmd].call(arg)
} else if (cmd) {
self.view.writeLine(`${cmd} is not a command, type /help for commands`)
} else if (self.channel === '!status') {
self.view.writeLine(line)
} else {
line = line.trim()
if (line !== '') {
self.cabal.publish({
type: 'chat/text',
content: {
channel: self.channel,
text: line
}
})
}
}
}
module.exports = Commander