public
Description: Direct Connect bot written in Ruby
Clone URL: git://github.com/kballard/dcbot.git
kballard (author)
Sat Feb 16 08:28:32 -0800 2008
commit  7648b1a8b5acafa65a23256aae09bb5148fe68a1
tree    76da8d17d1827753543506f2cf7521881451d1b9
parent  295af7bf0817496987310a390cf35a06c74b64ba
dcbot / dcprotocol.rb
100644 353 lines (300 sloc) 8.313 kb
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
require 'stringio'
require './he3'
 
class DCProtocol < EventMachine::Connection
  include EventMachine::Protocols::LineText2
  
  def registerCallback(callback, &block)
    @callbacks[callback] = block
  end
  
  def lockToKey(lock)
    key = String.new(lock)
    1.upto(key.size - 1) do |i|
      key[i] = lock[i] ^ lock[i-1]
    end
    key[0] = lock[0] ^ lock[-1] ^ lock[-2] ^ 5
    
    # nibble-swap
    0.upto(key.size - 1) do |i|
      key[i] = ((key[i]<<4) & 240) | ((key[i]>>4) & 15)
    end
    
    0.upto(key.size - 1) do |i|
      if [0,5,36,96,124,126].include?(key[i]) then
        key[i,1] = ("/%%DCN%03d%%/" % key[i])
      end
    end
    
    key
  end
  
  def send_command(cmd, *args)
    data = "$#{cmd}#{["", *args].join(" ")}|"
    send_data(data)
  end
  
  def call_callback(callback, *args)
    @callbacks[callback].call(self, *args) if @callbacks.has_key? callback
  end
  
  def connection_completed
    call_callback :connected
  end
  
  def receive_line(line)
    line.chomp!("|")
    cmd = line.slice!(/^\S+/)
    line.slice!(/^ /)
    
    if cmd =~ /^<.*>$/ then
      # this must be a public message
      nick = cmd[1...-1]
      call_callback :message, nick, line, false, false
    elsif cmd =~ /^\$\S+$/ then
      # this is a proper command
      cmd.slice!(0)
      # hardcode the $To: command since the colon is ugly
      # this protocol is pretty messy
      cmd = "To" if cmd == "To:"
      if self.respond_to? "cmd_#{cmd}" then
        self.send "cmd_#{cmd}", line
      else
        STDERR.puts "Unknown command: $#{cmd} #{line}"
      end
    else
      STDERR.puts "Garbage data: #{line}"
    end
  end
  
  def post_init
    @callbacks = {}
    set_delimiter "|"
  end
  
  def unbind
    call_callback :unbind
  end
end
 
class DCClientProtocol < DCProtocol
  RUBYBOT_VERSION = 0.1
  
  # known keys for args are:
  # password
  # description
  # speed
  # speed_class
  # email
  def self.connect(host, port, nickname, args = {})
    EventMachine::connect(host, port, self) do |c|
      c.instance_eval do
        @nickname = nickname
        @config = args
        @config[:description] ||= ""
        @config[:speed] ||= "Bot"
        @config[:speed_class] ||= 1
        @config[:email] = ""
      end
      yield c if block_given?
    end
  end
  
  def sendPublicMessage(message, isaction = false)
    if isaction then
      nick = "*"
      message = "#{@nickname} #{message}"
    else
      nick = @nickname
    end
    send_data "<#{nick}> #{message}|"
  end
  
  def sendPrivateMessage(recipient, message, isaction = false)
    if isaction then
      nick = "*"
      message = "#{@nickname} #{message}"
    else
      nick = @nickname
    end
    send_command "To:", recipient, "From:", @nickname, "$<#{nick}>", message
  end
  
  def close
    @quit = true
    close_connection
  end
  
  attr_reader :nickname, :hubname, :quit
  
  # protocol implementation
  
  def cmd_Lock(line)
    lock = line.split(" ")[0]
    key = lockToKey(lock)
    
    send_command("Key", "#{key}")
    send_command("ValidateNick", "#{@nickname}")
  end
  
  def cmd_ValidateDenide(line)
    STDERR.puts "Nickname in use or invalid"
    self.close
  end
  
  def cmd_GetPass(line)
    if @config.has_key? :password
      send_command "MyPass", @config[:password]
    else
      STDERR.puts "Password required but not given"
      self.close
    end
  end
  
  def cmd_BadPass(line)
    STDERR.puts "Bad password given"
    self.close
  end
  
  def cmd_LogedIn(line)
    call_callback :logged_in
  end
  
  def cmd_HubName(line)
    @hubname = line
    call_callback :hubname, @hubname
  end
  
  def cmd_Hello(line)
    nick = line
    if nick == @nickname then
      # this is us, we should respond
      send_command "Version", "1,0091"
      send_command "GetNickList"
      send_command "MyINFO", "$ALL #{@nickname} #{@config[:description]}<RubyBot V:#{RUBYBOT_VERSION}>$", \
                             "$#{@config[:speed]}#{@config[:speed_class].chr}$#{@config[:email]}$0$"
    else
      call_callback :user_connected, nick
    end
  end
  
  def cmd_NickList(line)
    call_callback :nicklist, line.split("$$")
  end
  
  def cmd_OpList(line)
    call_callback :oplist, line.split("$$")
  end
  
  def cmd_MyINFO(line)
    if line =~ /^\$ALL (\S+) ([^$]*)\$ +\$([^$]*)\$([^$]*)\$([^$]*)\$$/ then
      nick = $1
      interest = $2
      speed = $3
      email = $4
      sharesize = $5
      if speed.length > 0 and speed[-1] < 0x20 then
        # assume last byte a control character means it's the speed class
        speed_class = speed.slice!(-1)
      else
        speed_class = 0
      end
      call_callback :info, nick, interest, speed, speed_class, email, sharesize unless nick == @nickname
    end
  end
  
  def cmd_ConnectToMe(line)
    # another peer is trying to connect to me
    if line =~ /^(\S+) (\S+):(\d+)$/ then
      nick = $1
      ip = $2
      port = $3.to_i
      connect_to_peer(nick, ip, port)
    end
  end
  
  def cmd_RevConnectToMe(line)
    if line =~ /^(\S+) (\S+)$/ then
      # for the moment we're just going to be a passive client
      STDERR.puts "$RevConnectToMe: #{line}"
      send_command "RevConnectToMe", $2, $1
    end
  end
  
  def cmd_To(line)
    if line =~ /^(\S+) From: (\S+) \$<(\S+)> (.*)$/ then
      mynick = $1
      nick = $2
      displaynick = $3 # ignored for now
      message = $4
      call_callback :message, nick, message, true, (displaynick == "*")
    else
      STDERR.puts "Garbage To: #{line}"
    end
  end
  
  def cmd_Quit(line)
    nick = line
    call_callback :user_quit, nick
  end
  
  def cmd_Search(line)
    # for the moment, completely ignore this
  end
  
  # utility methods
  
  def connect_to_peer(nick, ip, port)
    STDERR.puts "Connecting to peer: #{nick} (#{ip}:#{port})"
    @peers << EventMachine::connect(ip, port, DCPeerProtocol) do |c|
      c.parent = self
      c.registerCallback :unbind do |socket|
        @peers.delete socket
      end
    end
  end
  
  # event handling methods
  
  def post_init
    super
    @quit = false
    @peers = []
  end
end
 
# major assumption in this implementation is that we are simply uploading
# if we want to be able to initiate downloads, this needs some tweaking
# we're also a passive client, so we're always connecting to the other client
class DCPeerProtocol < DCProtocol
  XML_FILE_LISTING = <<EOF
<?xml version="1.0" encoding="utf-8"?>
<FileListing Version="1" Generator="RubyBot">
<Directory Name="Send a /pm with !help for help">
</Directory>
</FileListing>
EOF
  DCLST_FILE_LISTING = <<EOF
Send a /pm with !help for help
EOF
  DCLST_FILE_LISTING_HE3 = he3_encode(DCLST_FILE_LISTING)
  
  attr_writer :parent
  
  def post_init
    super
  end
  
  def connection_completed
    super
    send_command "MyNick", @parent.nickname
    send_command "Lock", "FOO", "Pk=BAR"
  end
  
  def cmd_MyNick(line)
    @remote_nick = line
  end
  
  def cmd_Lock(line)
    lock = line.split(" ")[0]
    key = lockToKey(lock)
    send_command "Direction", "Upload", rand(0x7FFF)
    send_command "Key", key
  end
  
  def cmd_Key(line)
    # who cares if they got the key right? just ignore it
  end
  
  def cmd_Direction(line)
    direction, rnd = line.split(" ")
    if direction != "Download" then
      # why did they send me a ConnectToMe if they don't want to download?
      close_connection
    end
  end
  
  def cmd_GetListLen(line)
    send_command "ListLen", DCLST_FILE_LISTING_HE3.length
  end
  
  def cmd_Get(line)
    if line =~ /^([^$]+)\$(\d+)$/ then
      @filename = $1
      @offset = $2.to_i - 1 # it's 1-based
      @fileio = StringIO.new(DCLST_FILE_LISTING_HE3)
      @fileio.pos = @offset
      if @filename == "MyList.DcLst" then
        send_command "FileLength", @fileio.size - @fileio.pos
      else
        send_command "Error", "File Not Available"
      end
    else
      send_command "Error", "Unknown $Get format"
      close_connection_after_writing
    end
  end
  
  def cmd_Send(line)
    if @filename.nil? then
      # we haven't been asked for the file yet
      send_command "Error", "Unexpected $Send"
      close_connection_after_writing
    else
      data = @fileio.read(40906)
      send_data data
    end
  end
  
  def cmd_Canceled(line)
    close_connection
  end
end