-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIrcBot.rb
executable file
·174 lines (149 loc) · 4.15 KB
/
IrcBot.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
#!/usr/bin/ruby
require "IrcConn"
require "IrcParser"
require "ModLoader"
#require "./modules/CommandManager"
class IrcBot
@@sleepconst = 0.01
attr_reader :sock, :owner
attr_writer :expectpong
def initialize(addy, por, nic, usr, chans, ownr, pass=nil)
@sock = IrcConn.new(addy, por, nic, usr, pass)
@channels = chans
@owner = ownr
@expectpong = false
@thr = nil
@mutex = Mutex.new
@throttlemutex = Mutex.new
@throttlecondition = ConditionVariable.new
@messagequeue = Array.new
@secondcount = 0
@linecount = 0
@THROTTLELINES = 15 #20 is the real value, but i'm gonna give myself some leeway
@THROTTLESECONDS = 10
end
def joinT
@thr.join
if(@pingthread != nil)
@pingthread.join
end
if(@throttlethread != nil)
@throttlethread.join
end
end
def updateExpectpong(newval)
@mutex.synchronize {
@expectpong = newval
}
end
def run
@thr = Thread.new {
#puts "Entering IO Loop"
while(1)
#puts "while"
str = @sock.read
if(str) #its not still trying to read
print str
IrcParser.process(self, str)
end
sleep(@@sleepconst)
end
}
@pingthread = Thread.new {
while(1)
sleep(150)
#puts "preping"
@mutex.synchronize {
if(@expectpong)
#kill the old socket
@sock.close
#reconnect
#initialize(@sock.address, @sock.port, @sock.nick, @sock.user, @channels, @owner, @sock.password)
@sock = IrcConn.new(addy, por, nic, usr, pass)
@expectpong = false
else
@expectpong = true
@sock.write("PING :"+Time.now.to_i.to_s)
end
#puts "PING :"+Time.now.to_i.to_s
#puts "postping"
}
end
}
@throttlethread = Thread.new {
#@secondcount
#@linecount
#@THROTTLELINES
#@THROTTLESECONDS
#@throttlemutex
#@throttlecondition
senttimes = Array.new
while(1)
@throttlemutex.synchronize {
if(@messagequeue.length == 0)
@throttlecondition.wait(@throttlemutex)
end
#clean up senttimes
nowt = Time.now.to_i
while(senttimes.length > 0 && nowt-senttimes[0] > @THROTTLESECONDS)
senttimes.shift
end
#if there are lines and we can send, don't stop until we have to
#this seems to be misbehaving, i just made a change to the second clause, test it first tomorrow
while(senttimes.length < @THROTTLELINES && (senttimes.length == 0 || nowt-senttimes[0] <= @THROTTLESECONDS))
mess = @messagequeue.shift
if(mess != nil)
senttimes.push(Time.now.to_i)
@sock.write(mess)
else
#we ran out of lines
break
end
end
#we sent too many during the interval or ran out, we'll wait at the top of the loop
}
sleep(0.1)
end
}
end
def say(target, line)
@throttlemutex.synchronize {
@messagequeue.push("PRIVMSG #{target} :#{line}")
@throttlecondition.signal
}
#@sock.write("PRIVMSG #{target} :#{line}")
end
def sendraw(str)
@sock.write(str)
end
def gotmsg(str)
#do nothing
toks = str.split(" ", 4)
hook = ""
if(toks[3] =~ /^:(\S+)/)
hook = $1
end
CommandManager.execCmd(self, hook, str)
if(toks[0] =~ /^:#{@owner}!/)
if(toks[3] =~ /^:!modload/)
ModLoader.loadModules("./modules/")
elsif(toks[3] =~ /^:!coreload/)
ModLoader.loadCore
end
end
end
def doJoins
@channels.each { |channel|
@sock.write("JOIN #{channel}")
}
end
def nick
return @sock.nick
end
def appendToNick
@sock.nick = @sock.nick+"_"
end
def protocolHooks(num, str)
CommandManager.protocolHooks(self, num, str)
end
end