-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAcroBot.rb
More file actions
160 lines (137 loc) · 5.18 KB
/
AcroBot.rb
File metadata and controls
160 lines (137 loc) · 5.18 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
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
require 'rubygems'
require 'cinch'
require 'yaml'
#comment goes here
DEFAULT_DICTIONARY = {
'new' => { 'tbd' => 'To be done.' }
}
module AbbrevBot
@@home_dir = File.expand_path '~/.config'
@@install_dir = File.expand_path File.dirname(__FILE__)
@@data_dir = File.expand_path File.dirname(__FILE__), 'data'
@@abbrevs_file = "#{@@data_dir}/abbrev.yaml"
@@cfg_file = 'acrobot.yaml'
def initialize_dictionary
File.open(@@abbrevs_file, 'w') { |f|
YAML.dump(DEFAULT_DICTIONARY, f)
}
dictionary
end
# Try to load the YAML file. If it does not exist initialize the
# file and populate it with DEFAULT_DICTIONARY.
# If the file exists but is scrumbled (false), do the same thing.
#
def dictionary
begin
YAML.load_file(@@abbrevs_file) || initialize_dictionary
rescue Errno::ENOENT
initialize_dictionary
end
end
# Open the YAML file with dictionary and look for the abbrev
def lookup_dictionary(abbrev)
results = []
dictionary.each do |key, values|
next if values.nil?
matched_keys = values.keys.select { |k| k.to_s.casecmp(abbrev) == 0 }
# => ['tbd', 'rhel'] ...
matched_keys.each do |k|
results << [k, values[k]]
end
end
results.empty? ? false : results
end
#Find abbrevs with a certain tag
def find_values(tag)
results =[]
dictionary.each do |key, values|
next if values.nil?
matched_keys = values.select { |k,v| v.to_s =~ /@#{tag}( |$)/i }.keys.each do |k|
results << k
end
end
results
end
def save_abbrev(name, description)
dict = dictionary
dict['new'] ||= {}
dict['new'][name.strip] = description.strip
File.open(@@abbrevs_file, 'w') { |f| YAML.dump(dict, f) }
end
def load_settings
require 'yaml'
return YAML::load_file("#{@@home_dir}/#{@@cfg_file}") if File.exists? "#{@@home_dir}/#{@@cfg_file}"
return YAML::load_file("#{@@install_dir}/#{@@cfg_file}")
end
end
include AbbrevBot
bot = Cinch::Bot.new do
settings = load_settings
# evaluate the string regex into a regex object (use of eval), then extract the regex value
prefix_str = eval(settings['prefix']).source # default: ^!
# same as prefix_str, but removing the caret character for when showing examples on which command to use
prefix_str_command = prefix_str.gsub('^', '')
configure do |c|
c.nick = settings['nick'] # "acrobot"
c.realname = settings['realname'] # "IRC Acronym and Abbreviation Expander Bot. '!help' for help"
c.user = settings['user'] # "acrobot" (user name when connecting)
c.server = settings['server'] # "irc.freenode.net"
c.channels = settings['channels'] # ["#openshift","#satellite6","#zanata","#theforeman","#ansible"]
# c.channels = ["#acrobot"]
c.prefix = settings['prefix'] # /^!/
c.sasl.username = settings['sasl_username'] unless settings['sasl_username'].nil?
c.sasl.password = ENV['SASL_PASSWORD'] unless ENV['SASL_PASSWORD'].nil?
end
on :message, /#{prefix_str}([\w\-\_\+\&\/]+)\=(.+)/ do |m, abbrev, desc|
nick = m.channel? ? m.user.nick+": " : ""
save_abbrev(abbrev, desc)
nick = m.channel? ? m.user.nick+"":""
m.reply("#{nick} Thanks! [#{abbrev}=#{desc}]")
end
on :message, /#{prefix_str}help/i do |m|
nick = m.channel? ? m.user.nick+": " : ""
m.reply("To expand an acronym, type (e.g.), #{prefix_str_command}ftp")
m.reply("To add a new acronym, type (e.g.), #{prefix_str_command}FTP=File Transfer Protocol")
m.reply("To associate a tag with an acronym, type (e.g.), #{prefix_str_command}IP=Internet Protocol @networking")
m.reply("To list abbreviations associated with a tag, type eg. #{prefix_str_command}@kernel")
m.reply("To list all tags, type #{prefix_str_command}@tags")
m.reply("AcroBot uses initcaps for expansions by default. Your own style guides may vary.")
m.reply("Contribute to AcroBot at https://github.com/theacrobot/AcroBot")
m.reply("Follow Acrobot on Twitter: @_acrobot")
end
on :message, /#{prefix_str}@([\w\-\_\+\&\/]+)$/ do |m, tag|
tag = tag.strip
match_abbrevs = find_values(tag)
nick = m.channel? ? m.user.nick+": " : ""
if match_abbrevs.empty?
m.reply("#{nick} Sorry, no such tag. To list all tags, type #{prefix_str_command}@tags")
else
m.reply("#{nick}#{match_abbrevs.join(', ')}")
end
end
on :message, /#{prefix_str}([\w\-\_\+\&\/]+)$/ do |m, abbrev|
abbrev = abbrev.strip
unless abbrev =~ /^help$/i
nick_str = m.channel? ? "#{m.user.nick}:" : ""
if replies = lookup_dictionary(abbrev)
replies.each do |original_abbrev, value|
value, *tags = value.split('@')
reply_str = "%s %s: %s %s" % [
nick_str,
Cinch::Formatting.format(:bold, original_abbrev.to_s),
Cinch::Formatting.format(:bold, value.strip),
tags.map { |t| "@#{t.strip}" }.join(', ')
]
m.reply(reply_str.strip)
end
else
m.reply("#{nick_str} Sorry, no definition for #{abbrev}")
end
end
end
on :message, /#{settings['nick']}.*(thanks|thank you)+.*$/ do |m|
nick_str = m.channel? ? "#{m.user.nick}:" : ""
m.reply("#{nick_str} You're welcome!")
end
end
bot.start