-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.rb
More file actions
122 lines (121 loc) · 3.91 KB
/
command.rb
File metadata and controls
122 lines (121 loc) · 3.91 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
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
def can_send?(message, users, name)
args = message.split(' ')
if args[0].start_with? '!'
case args[0]
when '!help'
show_help(users, name)
when '!show'
show_license(users, name)
when '!pass'
change_password(args[1..-1], users, name)
when '!seen'
seen(args[1], users, name)
when '!tell'
tell(args[1], args[2..-1].join(' '), users, name)
when '!yell'
yell(args[1..-1].join(' '), users, name)
when '!pick'
pick(users, name)
when '!dice'
dice(users, name)
when '!flip'
flip(users, name)
when '!ball'
ball(users, name)
when '!motd'
motd(args[1..-1].join(' '), users, name)
end
false
else
true
end
end
def show_help(users, name)
users[name].send(erb(:message_help_command, layout: nil))
end
def show_license(users, name)
users[name].send(erb(:message_show_command, layout: nil))
end
def change_password(arg, users, name)
users[name].send(erb(:message_command, layout: nil, locals: { text: 'Your password has been changed successfully.' }))
with_user(name) do |user|
user['password'] = digest(arg)
end
end
def seen(arg, users, name)
with_user(arg) do |user|
if user['lastSeen'].nil?
users[name].send(erb(:message_command, layout: nil, locals: { text: '#{arg} has never been on this chat before.' }))
else
users[name].send(erb(:message_seen_command, layout: nil, locals: { name: arg, time: user['lastSeen'] }))
end
end
end
def tell(arg, message, users, name)
with_user(arg) do |user|
if user['notes'].nil?
users[name].send(erb(:message_command, layout: nil, locals: { text: '#{arg} has never been on this chat before.' }))
else
users[name].send(erb(:message_command, layout: nil, locals: { text: 'Your message has been sent to #{arg}!' }))
user['notes'].push({ name => message })
end
end
end
def yell(message, users, name)
users.each do |user, sockets|
sockets.send(erb(:message_yell_command, layout: nil, locals: { name: name, text: message }))
end
end
def pick(users, name)
name_picked = users.keys.sample
users.each do |user, sockets|
sockets.send(erb(:message_pick_command, layout: nil, locals: { name: name, pick: name_picked }))
end
end
def dice(users, name)
users.each do |user, sockets|
sockets.send(erb(:message_dice_command, layout: nil, locals: { name: name, roll: rand(1..6) }))
end
end
def flip(users, name)
users.each do |user, sockets|
sockets.send(erb(:message_flip_command, layout: nil, locals: { name: name, flip: rand(0..1) }))
end
end
def ball(users, name)
message = ['Yes', 'No.', 'Maybe.', 'Possibly.', 'I doubt it.', 'Definitely.', 'Totally.', 'Hell no.', 'Of course.'].sample
users.each do |user, sockets|
sockets.send(erb(:message_ball_command, layout: nil, locals: { name: name, message: message }))
end
end
def motd(arg, users, name)
if File.exist? './private/motd.json'
message = Oj.load_file('./private/motd.json')
end
message['motd'] = arg
Oj.to_file('./private/motd.json', message)
users.each do |user, sockets|
sockets.send(erb(:message_command, layout: nil, locals: { text: 'MOTD has been changed to “#{arg}”' }))
end
end
# Escapes HTML tags, prevents XSS attacks.
def unxss(message)
message.gsub('<', '<').gsub('>', '>')
end
def digest(input)
return Digest::SHA256.hexdigest(input + '_salt')
end
def with_user(name)
users = {}
if File.exist? './private/users.json'
users = Oj.load_file('./private/users.json')
end
if not users.key? name
users[name] = {}
end
yield(users[name])
Oj.to_file('./private/users.json', users)
end
def timestamp
Time.now.to_i
end