Class: Cinch::Plugins::Ping

Inherits:
Object
  • Object
show all
Includes:
Cinch::Plugin
Defined in:
lib/cinch/plugins/ping.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Ping

Returns a new instance of Ping


11
12
13
14
# File 'lib/cinch/plugins/ping.rb', line 11

def initialize(*args)
  super
  @listen_for_ping = {}
end

Instance Attribute Details

#listen_for_pingObject

Returns the value of attribute listen_for_ping


9
10
11
# File 'lib/cinch/plugins/ping.rb', line 9

def listen_for_ping
  @listen_for_ping
end

Instance Method Details

#ctcp_ping(m) ⇒ Object


34
35
36
37
38
# File 'lib/cinch/plugins/ping.rb', line 34

def ctcp_ping(m)
  return unless  @listen_for_ping.has_key?(m.user.nick) && @listen_for_ping[m.user.nick][:ts].to_i == m.ctcp_args[0].to_i
  @listen_for_ping[m.user.nick][:target].msg "#{m.user.nick}#{m.user.nick[-1].casecmp("s") == 0 ? "'" : "'s"} ping to me on #{@bot.irc.isupport['NETWORK']} is #{((Time.now - @listen_for_ping[m.user.nick][:ts]) * 1000).round}ms."
  @listen_for_ping.delete(m.user.nick)
end

#execute(m, nick = nil) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cinch/plugins/ping.rb', line 17

def execute(m, nick=nil)
  nick ||= m.user.nick
  user = User(nick)
  return m.reply "You cannot make me ping myself!" if user == @bot
  user = m.user if user.unknown?
  t = Time.now
  user.ctcp("PING #{t.to_i}")
  @listen_for_ping[user.nick] = {target: (m.channel? ? m.channel : m.user), ts: t}
  Timer(5, shots: 1) {
    if @listen_for_ping.has_key?(user.nick)
      m.channel.msg "I could not determine #{user.nick}#{user.nick[-1].casecmp("s") == 0 ? "'" : "'s"} ping to me after 5 seconds."
      @listen_for_ping.delete(user.nick)
    end
  }
end