summaryrefslogtreecommitdiff
path: root/lib/rbot/ircsocket.rb
blob: b3c3dc8acc08c48975bce76058c149440d5383e6 (plain)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
module Irc

  require 'socket'
  require 'thread'
  require 'rbot/timer'

  # wrapped TCPSocket for communication with the server.
  # emulates a subset of TCPSocket functionality
  class IrcSocket
    # total number of lines sent to the irc server
    attr_reader :lines_sent

    # total number of lines received from the irc server
    attr_reader :lines_received

    # total number of bytes sent to the irc server
    attr_reader :bytes_sent

    # total number of bytes received from the irc server
    attr_reader :bytes_received

    # accumulator for the throttle
    attr_reader :throttle_bytes

    # byterate components
    attr_reader :bytes_per
    attr_reader :seconds_per

    # delay between lines sent
    attr_reader :sendq_delay

    # max lines to burst
    attr_reader :sendq_burst

    # server:: server to connect to
    # port::   IRCd port
    # host::   optional local host to bind to (ruby 1.7+ required)
    # create a new IrcSocket
    def initialize(server, port, host, sendq_delay=2, sendq_burst=4, brt="400/2")
      @timer = Timer::Timer.new
      @timer.add(0.2) do
        spool
      end
      @server = server.dup
      @port = port.to_i
      @host = host
      @sock = nil
      @spooler = false
      @lines_sent = 0
      @lines_received = 0
      if sendq_delay
        @sendq_delay = sendq_delay.to_f
      else
        @sendq_delay = 2
      end
      @last_send = Time.new - @sendq_delay
      @last_throttle = Time.new
      @burst = 0
      if sendq_burst
        @sendq_burst = sendq_burst.to_i
      else
        @sendq_burst = 4
      end
      @bytes_per = 400
      @seconds_per = 2
      @throttle_bytes = 0
      @throttle_div = 1
      setbyterate(brt)
    end

    def setbyterate(brt)
      if brt.match(/(\d+)\/(\d)/)
        @bytes_per = $1.to_i
        @seconds_per = $2.to_i
        debug "Byterate now #{byterate}"
        return true
      else
        debug "Couldn't set byterate #{brt}"
        return false
      end
    end

    def connected?
      !@sock.nil?
    end

    # open a TCP connection to the server
    def connect
      if connected?
        debug "reconnecting socket while connected"
        shutdown
      end
      if(@host)
        begin
          @sock=TCPSocket.new(@server, @port, @host)
        rescue ArgumentError => e
          error "Your version of ruby does not support binding to a "
          error "specific local address, please upgrade if you wish "
          error "to use HOST = foo"
          error "(this option has been disabled in order to continue)"
          @sock=TCPSocket.new(@server, @port)
        end
      else
        @sock=TCPSocket.new(@server, @port)
      end
      @qthread = false
      @qmutex = Mutex.new
      @sendq = Array.new
    end

    def sendq_delay=(newfreq)
      debug "changing sendq frequency to #{newfreq}"
      @qmutex.synchronize do
        @sendq_delay = newfreq
        if newfreq == 0
          clearq
          @timer.stop
        else
          @timer.start
        end
      end
    end

    def sendq_burst=(newburst)
      @qmutex.synchronize do
        @sendq_burst = newburst
      end
    end

    def byterate
      return "#{@bytes_per}/#{@seconds_per}"
    end

    def byterate=(newrate)
      @qmutex.synchronize do
        setbyterate(newrate)
      end
    end

    def run_throttle(more=0)
      now = Time.new
      if @throttle_bytes > 0
        # If we ever reach the limit, we halve the actual allowed byterate
        # until we manage to reset the throttle.
        # I don't know if this is the best way, though, because the real
        # problem is probably non-queued messages like PINGs and PONGs.
        # A better solution would probably be to have two queues,
        # one for priority messages and another one for normal messages.
        # Even better, we should have:
        # * one queue for server stuff
        # * one for each channel
        # * one for each private communication
        # The server queue would have priority, everything else would be served
        # round-robin, so that someone making the bot flood one channel wouldn't
        # prevent the bot from working on other channels (or private communications)
        if @throttle_bytes >= @bytes_per
          @throttle_div = 0.5
        end
        delta = ((now - @last_throttle)*@throttle_div*@bytes_per/@seconds_per).floor
        if delta > 0
          @throttle_bytes -= delta
          @throttle_bytes = 0 if @throttle_bytes < 0
          @last_throttle = now
        end
      end
      if @throttle_bytes == 0
        @throttle_div = 1
      end
      @throttle_bytes += more
    end

    # used to send lines to the remote IRCd
    # message: IRC message to send
    def puts(message)
      @qmutex.synchronize do
        # debug "In puts - got mutex"
        puts_critical(message)
      end
    end

    # get the next line from the server (blocks)
    def gets
      if @sock.nil?
        debug "socket get attempted while closed"
        return nil
      end
      begin
        reply = @sock.gets
        @lines_received += 1
        reply.strip! if reply
        debug "RECV: #{reply.inspect}"
        return reply
      rescue => e
        debug "socket get failed: #{e.inspect}"
        return nil
      end
    end

    def queue(msg)
      if @sendq_delay > 0
        @qmutex.synchronize do
          @sendq.push msg
        end
        @timer.start
      else
        # just send it if queueing is disabled
        self.puts(msg)
      end
    end

    # pop a message off the queue, send it
    def spool
      if @sendq.empty?
        @timer.stop
        return
      end
      now = Time.new
      if (now >= (@last_send + @sendq_delay))
        # reset burst counter after @sendq_delay has passed
        @burst = 0
        debug "in spool, resetting @burst"
      elsif (@burst >= @sendq_burst)
        # nope. can't send anything, come back to us next tick...
        @timer.start
        return
      end
      @qmutex.synchronize do
        debug "(can send #{@sendq_burst - @burst} lines, there are #{@sendq.length} to send)"
        (@sendq_burst - @burst).times do
          break if @sendq.empty?
          mess = @sendq[0]
          if @throttle_bytes == 0 or mess.length+@throttle_bytes < @bytes_per
            puts_critical(@sendq.shift)
          else
            debug "(flood protection: throttling message of length #{mess.length})"
	    debug "(byterate: #{byterate}, throttle bytes: #{@throttle_bytes})"
            run_throttle
            break
          end
        end
      end
      if @sendq.empty?
        @timer.stop
      end
    end

    def clearq
      if @sock
        unless @sendq.empty?
          @qmutex.synchronize do
            @sendq.clear
          end
        end
      else
        debug "Clearing socket while disconnected"
      end
    end

    # flush the TCPSocket
    def flush
      @sock.flush
    end

    # Wraps Kernel.select on the socket
    def select(timeout=nil)
      Kernel.select([@sock], nil, nil, timeout)
    end

    # shutdown the connection to the server
    def shutdown(how=2)
      @sock.shutdown(how) unless @sock.nil?
      @sock = nil
    end

    private

    # same as puts, but expects to be called with a mutex held on @qmutex
    def puts_critical(message)
      # debug "in puts_critical"
      debug "SEND: #{message.inspect}"
      @sock.send(message + "\n",0)
      @last_send = Time.new
      @lines_sent += 1
      @burst += 1
      run_throttle(message.length + 1)
    end

  end

end