-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfurbot.rb
executable file
·221 lines (178 loc) · 6.56 KB
/
furbot.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require "cinch"
require "optparse"
require "syslog"
require "time"
require "pathname"
require_relative "cinch-plugins/plugins/git_commits"
require_relative "cinch-plugins/plugins/history"
require_relative "cinch-plugins/plugins/echo"
require_relative "cinch-plugins/plugins/link_info"
require_relative "cinch-plugins/plugins/vote"
require_relative "cinch-plugins/plugins/tickets"
require_relative "cinch-plugins/plugins/quit"
require_relative "cinch-plugins/plugins/seen"
require_relative "cinch-plugins/plugins/channel_record"
require_relative "other_plugins/shakespeare"
require_relative "other_plugins/mailinglist"
require_relative "cinch_syslog"
DIR = File.dirname(File.expand_path(__FILE__))
#################### Argument handling ####################
$options = {
:debug => false
}
op = OptionParser.new do |opts|
opts.banner = "Usage: furbot.rb [OPTIONS]"
opts.separator ""
opts.on("-d", "--[no-]debug", "Log verbosely to the standard output."){|bool| $options[:debug] = bool}
opts.on_tail("-h", "--help"){puts(opts); exit(0)}
end
op.parse!
#################### Cinch specification ####################
cinch = Cinch::Bot.new do
configure do
config.server = "irc.libera.chat"
config.port = 6697
config.ssl.use = true
config.ssl.verify = false
config.channels = ["#secretchronicles"]
config.nick = "furbot"
config.user = "furbot"
config.realname = "Furball Bot Left"
end
# Use this one for testing on a local server instead
#configure do
# config.server = "localhost"
# config.port = 6667
#
# config.channels = ["#test"]
# config.nick = "furbot"
# config.user = "furbot"
# config.realname = "Furball Bot Left"
#end
config.plugins.prefix = "!"
config.plugins.options[Cinch::Seen] = {
:file => "/var/lib/furbot/seenlog.dat",
:max_age => 60 * 60 * 24 * 365 # 1 year
}
config.plugins.options[Cinch::History] = {
:mode => :max_age,
:only_talk => true,
:max_age => 15 # minutes
}
config.plugins.options[Cinch::Vote] = {
:auth_required => true,
:voters => %w[Bugsbane DarkAceZ datahead8888 Luiji quintus sydneyjd xet7]
}
config.plugins.options[Cinch::Tickets] = {
:url => "https://github.com/Secretchronicles/TSC/issues/%d"
}
config.plugins.options[Cinch::GitCommits] = {
:directory => "/srv/git"
}
config.plugins.options[Cinch::Quit] = {
:op => true
}
config.plugins.options[Cinch::ChannelRecord] = {
:file => "/var/lib/furbot/channelrecord.dat"
}
config.plugins.options[Cinch::MailmanObserver] = {
:logfile => "/var/log/mailman3/mailman.log"
}
config.plugins.plugins = [Cinch::Echo,
Cinch::GitCommits,
Cinch::History,
Cinch::LinkInfo,
Cinch::Tickets,
Cinch::Quit,
Cinch::Shakespeare,
Cinch::MailmanObserver,
Cinch::Vote,
Cinch::Seen,
Cinch::ChannelRecord]
$quitnow = false
Timer(5) do
if $quitnow
bot.loggers.warn $quitnow
bot.quit($quitnow)
end
end
Timer(60 * 10) do
bot.nick = "furbot" unless bot.nick == "furbot"
end
trap "SIGINT" do
$quitnow = "Received SIGINT."
end
trap "SIGTERM" do
$quitnow = "Received SIGTERM."
end
on :message, /!search (.*)/ do |msg, term|
msg.channel.send("https://duckduckgo.com/?q=#{CGI.escape(term)}")
end
on :message, /!tzconv (.*?) (\w+) to (\w+)$/ do |msg, sourcetimestr, source, target|
# Time.zone_offset only has a few selected zones.
# Let’s add some more.
more_zones = {"CET" => 60 * 60, # Central European Time
"CEST" => 60 * 60 * 2, # Central European Summer Time
"EET" => 60 * 60 * 2, # Eastern European Time
"EEST" => 60 * 60 * 3, # Eastern European Summer Time
"WET" => 0, # Western European Time
"WEST" => 60 * 60, # Western European Summer Time
"WEDT" => 60 * 60, # Western European Daylight Time
"CNST" => 60 * 60 * 8, # Chinese Standard Time
"JST" => 60 * 60 * 9, # Japan Standard Time
"ACST" => 60 * 60 * 9 + 30, # Australian Central Standard Time
"ACDT" => 60 * 60 * 10 + 30, # Australian Central Daylight Time
"AEST" => 60 * 60 * 10, # Australian Eastern Standard Time
"AEDT" => 60 * 60 * 11, # Australian Eastern Daylight Time
"NZST" => 60 * 60 * 12, # New Zealand Standard Time
"NZDT" => 60 * 60 * 13} # New Zealand Daylight Time
sourcetime = Time.parse(sourcetimestr)
# The above yields a wrong result (local time zone). Force
# into the correct zone.
if offset = Time.zone_offset(source) || offset = more_zones[source] # Single = intended
sourcetime = Time.new(sourcetime.year, sourcetime.month, sourcetime.day,
sourcetime.hour, sourcetime.min, sourcetime.sec,
offset)
else
msg.reply "I don’t know the source timezone #{source}."
next
end
target_offset = Time.zone_offset(target) || more_zones[target]
unless target_offset
msg.reply "I don’t know the target timezone #{target}."
next
end
target_time = sourcetime.dup.utc
target_time += target_offset
target_time = Time.new(target_time.year, target_time.month, target_time.day,
target_time.hour, target_time.min, target_time.sec,
target_offset)
msg.reply "#{sourcetime} in #{target} is #{target_time}"
end
end
#################### Start action code ####################
# Nice process name
$0 = "furbot"
# Open the syslog
Syslog.open("furbot")
Syslog.log(Syslog::LOG_INFO, "Starting up.")
at_exit do
Syslog.log(Syslog::LOG_INFO, "Finished, closing syslog.")
Syslog.close
end
syslogger = CinchSyslogLogger.new
syslogger.level = :info
cinch.loggers.clear unless $options[:debug]
cinch.loggers.push(syslogger)
# Set our file permissions
File.umask 0133 # rw-r--r--
# Fail if target directories do not exist or is
# otherwise not accessible
p1 = Pathname.new(cinch.config.plugins.options[Cinch::Seen][:file]).dirname
fail "Not a directory: #{p1}" unless p1.directory?
fail "Directory not writable: #{p1}" unless p1.writable?
Thread.abort_on_exception = true
Dir.chdir("/")
cinch.start