forked from DoctorWhooves/chatbot-rb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.rb
113 lines (102 loc) · 2.66 KB
/
util.rb
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
# encoding: ASCII-8BIT
require 'json'
module JSON
def self.is_json?(foo)
begin
return false unless foo.is_a?(String)
self.parse(foo).all?
rescue self::ParserError
false
end
end
end
class User
attr_reader :name
# @param [String] name
def initialize(name, mod = false, admin = false, staff = false)
@name = name
@mod = mod
@admin = admin
@staff = staff
@ignored = ignored?
end
# Check if a user has the given privilege. Privileges cascade, so:
# - +:mod+ returns true if user is +:dev+, +:staff+, +:admin+, or +:mod+
# - +:admin+ returns true if user is +:dev+, +:staff+, or +:admin+
# - +:staff+ returns true if user is +:dev+ or +:staff+
# - +:dev+ returns true if and only if <tt>@name == 'Sactage'</tt>
# @param [Symbol] right
# @return [TrueClass, FalseClass]
def is?(right)
case right
when :mod
@mod or @admin or @staff or is? :dev
when :admin
@admin or @staff or is? :dev
when :staff
@staff or is? :dev
when :dev
@name.eql? 'Sactage'
else
false
end
end
# @return [String] The user's name with underscores instead of spaces
def log_name
@name.gsub(' ', '_')
end
# Check if the user is ignored
# @return [TrueClass, FalseClass]
def ignored?
return @ignored unless @ignored.nil?
if File.exists? 'ignore.yml'
YAML::load_file('ignore.yml')['users'].include? @name
else
File.open('ignore.yml', 'w+') {|f| f.write({'users' => []}.to_yaml)}
false
end
end
# Ignore the user
def ignore
return if is? :dev
# @type [Hash] ignorefile
if File.exists? 'ignore.yml'
ignorefile = YAML::load_file('ignore.yml')
else
ignorefile = {'users' => []}
end
ignorefile['users'] << @name
File.open('ignore.yml', 'w+') {|f| f.write(ignorefile.to_yaml)}
@ignored = true
end
# Unignore the user
def unignore
if File.exists? 'ignore.yml'
ignorefile = YAML::load_file('ignore.yml')
else
ignorefile = {'users' => []}
end
ignorefile['users'].delete(@name)
File.open('ignore.yml', 'w+') {|f| f.write(ignorefile.to_yaml)}
@ignored = false
end
end
module Util
LOG_TS_FORMAT = '%H:%M:%S'
# @return [String] Log timestamp string of current time
def self.ts
Time.now.utc.strftime LOG_TS_FORMAT
end
# Format the given string into a socket.io-readable format
# @param [String] message
# @return [String]
def self.format_message(message)
message.size.to_s + ':' + message
end
end
class Time
# @return [Fixnum] The time since the UNIX epoch in micro-seconds
def to_ms
(self.to_f * 1000.0).to_i
end
end