-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.rb
executable file
·124 lines (99 loc) · 3.08 KB
/
bot.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
#!/usr/bin/env ruby1.9.1
require 'cinch'
require 'chronic'
###############################################################################
class AutoHello
include Cinch::Plugin
listen_to :join
def listen(m)
unless m.user.nick.end_with?("-bot")
m.reply "hi #{m.user.nick}"
end
end
end
###############################################################################
class History
MAX_MSGS = 10000
include Cinch::Plugin
class HistoryMsg < Struct.new(:user, :time, :channel, :msg)
def to_s
"[#{time.strftime "%H:%M:%S"}] #{user}: #{msg}"
end
end
def initialize(*args)
super
@history = []
end
match(/(.*)/, :use_prefix => false)
def execute(m)
# only record messages on the channel
if m.channel? then
@history << HistoryMsg.new(m.user, Time.now, m.channel, m.message)
if @history.size > MAX_MSGS then
@history.delete_at(0)
end
end
# react to messages starting with !history
if m.message =~ /^!history(.*)/ then
# send history with private messages to the user depending on the channel
begin
time = Chronic.parse($1, :context => :past)
if time == nil then
raise "wrong date format"
else
if m.channel? then
m.user.msg "history on the channel " + m.channel + " after " + time.to_s + ":"
@history.each { |msg| m.user.msg(msg) if msg.channel == m.channel and msg.time >= time }
else
m.user.msg "history after " + time.to_s + ":"
@history.each { |msg| m.user.msg(msg) if msg.time >= time }
end
end
rescue
m.user.msg "please give a date: !history yesterday, !history 2 hours ago, !history 6 in the morning, etc."
m.user.msg "more documentation is available at http://chronic.rubyforge.org/files/README.html"
end
end
end
end
###############################################################################
class Leave
include Cinch::Plugin
match /leave (.+)/
def execute(m)
if not m.channel? then
if m.message =~ /!leave NOW!/ then
m.bot.channels.each { |c| c.msg "#{m.user.nick} asked me to leave... bye all!" }
m.bot.quit
else
m.reply "incorrect password. I'm not leaving!"
end
end
end
end
###############################################################################
class Help
include Cinch::Plugin
match /help/
def execute(m)
m.reply "available commands:\n!help\n!history date (date being written in natural language, e.g. two hours ago)"
end
end
###############################################################################
# quit unless our script gets one command line arguments
unless ARGV.length == 1
puts "Usage: irc_bot.rb [start|stop|restart] <channel (without the # sign)>"
exit
end
channel = ARGV[0]
bot = Cinch::Bot.new do
configure do |c|
c.server = "irc.freenode.org"
c.channels = ["#" + channel]
c.messages_per_second = 0.5
c.nick = channel + "-bot"
c.plugins.plugins = [AutoHello, Help, History, Leave]
c.verbose = true
end
end
bot.start