-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.rb
executable file
·187 lines (143 loc) · 3.41 KB
/
test.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
#!/usr/bin/env ruby
require 'em-irc'
require 'logger'
require 'pp'
require 'data_mapper'
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, (ENV["DATABASE_URL"]|| 'sqlite://'+File.expand_path('../git.db',__FILE__)))
DataMapper::Model.raise_on_save_failure=true
class Sentence
include DataMapper::Resource
property :id, Serial
property :author, Text
property :text, Text
property :count, Integer
property :context, String
end
class Friend
include DataMapper::Resource
property :id, Serial
property :name, Text
end
DataMapper.finalize
DataMapper.auto_upgrade!
$channel="#zweitbot"
$mynick="godrintest"
$known_sentences=[]
def sentenceValid?(s)
if s.length<10
false
elsif $known_sentences.member?(s)
false
elsif s.match(/2[0-9]{3}-[0-9].*/)
false
elsif s.match(/#{$mynick}/)
false
else
true
end
end
Sentence.all.each{|s|
if not sentenceValid?(s.text)
s.destroy
else
$known_sentences<<s.text
end
}
class BotState
attr_accessor :justLoggedIn, :friends
def initialize
@justLoggedIn=true
@friends=[]
EM.add_timer(20,proc {
@justLoggedIn=false
})
end
end
client = EventMachine::IRC::Client.new do
host 'irc.chaostal.de'
port '6667'
@bot=nil
def say(what)
message($channel,what)
end
on(:connect) do
nick($mynick)
puts "connected - nick #{$mynick}"
@bot=BotState.new
end
on(:nick) do |a,b,c|
puts "nick"
pp a,b,c
join($channel)
end
on(:join) do |who,channel,names| # called after joining a channel
puts "on join"
if who==$mynick
pp who,channel,names
message(channel, "howdy all")
# send_data("hi again test")
#
#EM.add_timer(20,proc {
# say Time.now.to_s
#})
EM.add_timer(3,proc {
message( "@godrin","fluester")
})
cleanupCount=proc {
Sentence.all.each{|s|s.count=(s.count||1)-1
s.save
}
EM.add_timer(90,cleanupCount)
}
cleanupCount.call
saySth=proc {
s=Sentence.all({:count.lt=>3}).sample
if s
someNick=Friend.all.sample
text=s.text.gsub("<<SOMENICK>>",someNick.name)
say(text) if s
s.count||=0
s.count+=1
s.save
end
EM.add_timer(Random.rand(30)+1,saySth)
}
EM.add_timer(7,saySth)
else
if Friend.first :name=>who
say "Hey "+who+" - wir kennen uns schon :-)"
end
end
end
on(:message) do |source, target, message| # called when being messaged
puts "message: <#{source}> -> <#{target}>: #{message}"
return if source==$mynick
case message
when /#{$mynick}/i
say("Was geht ?")
when /wetter/i
say("Das Wetter nervt echt !")
when /heute/i
say("Man was ist hier heute los ???")
when /(hi|hallo|na)/i
if @bot.justLoggedIn
Friend.create :name=>source unless Friend.first :name=>source
#@bot.friends<<source
#say("Ja, Du bist mein Freund #{source}")
end
else
words=message.split(" ").select{|w|w[0..0]=~/[A-Z]/}
if words.length>0
say("Was soll das mit #{words.shuffle[0]} ?")
end
end
message.gsub!($mynick,"<<SOMENICK>>")
Sentence.create :author=>source, :text=>message unless Sentence.first :text=>message if sentenceValid?(message)
end
# callback for all messages sent from IRC server
on(:parsed) do |hash|
puts "parsed: #{hash[:prefix]} #{hash[:command]} #{hash[:params].join(' ')}"
end
end
client.run! # start EventMachine loop