Class: Mpex::Irc

Inherits:
Object
  • Object
show all
Defined in:
lib/mpex/irc.rb

Overview

TODO to be improved and to be made configurable!

Constant Summary collapse

ASSBOT =
"assbot"
MPEXBOT =
"mpexbot"
TIMEOUT =

seconds

30

Instance Method Summary collapse

Constructor Details

#initializeIrc

Returns a new instance of Irc.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mpex/irc.rb', line 13

def initialize
  @irc = Net::YAIL.new(
    :address    => 'irc.freenode.net',
    :username   => 'mp_rb_client',
    :realname   => 'MPEx.rb irc client',
    :nicknames  => ["mp_rb_client#{Random.rand(42..4096)}", "mp_rb_client#{Random.rand(42..4096)}"]
  )
  log = Logger.new(STDOUT)
  log.level = Logger::WARN
  @irc.log = log
end

Instance Method Details

#connectObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mpex/irc.rb', line 25

def connect
  @irc.on_welcome do |event|
    puts "\nConnected to IRC."
    @connected = true
  end

  @irc.hearing_msg do |event|
    @last_message = {:nick => event.nick, :message => event.message}
  end

  puts "Connecting. Please wait."

  @irc.start_listening
end

#connected?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/mpex/irc.rb', line 44

def connected?
  @connected
end

#depth(&block) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/mpex/irc.rb', line 88

def depth(&block)
  puts "[IRC] Messaging #{MPEXBOT}: $depth"
  @irc.msg MPEXBOT, '$depth'

  status = Timeout::timeout(TIMEOUT) {
    yield wait_for_mpexbot_message
  }
end

#disconnectObject



40
41
42
# File 'lib/mpex/irc.rb', line 40

def disconnect
  @irc.stop_listening
end

#handle_assbot_incoming(msg) ⇒ Object



72
73
74
75
76
77
# File 'lib/mpex/irc.rb', line 72

def handle_assbot_incoming(msg)
  if msg.start_with? "Response: http:"
    resp_url = msg.split[1]
    return Net::HTTP.get(URI.parse(resp_url))
  end
end

#handle_mpexbot_incoming(msg) ⇒ Object



132
133
134
135
136
137
138
139
140
# File 'lib/mpex/irc.rb', line 132

def handle_mpexbot_incoming(msg)
  if msg.match(/http:\/\/pastebin\.com\/\d+/)
    id = URI.parse(msg).path[1..-1]
    uri = URI.parse("http://pastebin.com/raw.php?i=" + id)
    return Net::HTTP.get(uri)
  elsif msg.start_with?("http://pastebin.com/raw.php")
    return Net::HTTP.get(URI.parse(msg))
  end
end

#list_proxies(&block) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/mpex/irc.rb', line 97

def list_proxies(&block)
  puts "[IRC] Messaging #{MPEXBOT}: $proxies"
  @irc.msg MPEXBOT, '$proxies'

  status = Timeout::timeout(TIMEOUT) {
    yield wait_for_mpexbot_plain_message
  }
end

#send_encrypted(message, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/mpex/irc.rb', line 48

def send_encrypted(message, &block)
  res = Net::HTTP.post_form(URI.parse("http://dpaste.com/api/v1/"), { 'content' => "#{message}" })
  dpaste_url = res['Location']
  puts "[IRC] Messaging #{ASSBOT}: !mp  #{dpaste_url}"
  @irc.msg(ASSBOT, "!mp " + dpaste_url)

  status = Timeout::timeout(TIMEOUT) {
    yield wait_for_assbot_message
  }
end

#vwap(&block) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/mpex/irc.rb', line 79

def vwap(&block)
  puts "[IRC] Messaging #{MPEXBOT}: $vwap"
  @irc.msg MPEXBOT, '$vwap'

  status = Timeout::timeout(TIMEOUT) {
    yield wait_for_mpexbot_message
  }
end

#wait_for_assbot_messageObject



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mpex/irc.rb', line 59

def wait_for_assbot_message
  while true
    if @last_message
      answer = handle_assbot_incoming(@last_message[:message])
      if answer
        puts "[IRC] #{ASSBOT} answered: #{@last_message[:message]}"
        @last_message = nil
        return answer
      end
    end
  end
end

#wait_for_mpexbot_messageObject



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/mpex/irc.rb', line 119

def wait_for_mpexbot_message
  while true
    if @last_message
      answer = handle_mpexbot_incoming(@last_message[:message])
      if answer
        puts "[IRC] #{MPEXBOT} answered: #{@last_message[:message]}"
        @last_message = nil
        return answer
      end
    end
  end
end

#wait_for_mpexbot_plain_messageObject



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/mpex/irc.rb', line 106

def wait_for_mpexbot_plain_message
  while true
    if @last_message
      answer = @last_message[:message]
      if answer
        puts "[IRC] #{MPEXBOT} answered: #{@last_message[:message]}"
        @last_message = nil
        return answer
      end
    end
  end
end