Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

remaining changes…

  • Loading branch information...
commit d207c46a13e76df23656d9820419541de3cd7e4b 1 parent 3262b84
@dominikh authored
View
27 README.md
@@ -162,7 +162,6 @@ unloaded.
require 'weechat'
include Weechat
- include Script::Skeleton
@script = {
:name => "testscript", # must not be empty
@@ -248,6 +247,32 @@ Updating the gem
The easiest way of updating the gem is to run `gem update weechat` and
then, in WeeChat, `/upgrade`.
+Asynchronous calls
+==================
+
+Due to the nature of IRC, some information cannot be requested
+synchronously, e.g. those that /whois returns. In order to use them,
+one has to use callbacks (not to be confused with WeeChat callbacks),
+which will get called as soon as the requested information are
+available.
+
+ user.real_name do |rn|
+ # this will get called as soon as the real_name is available.
+ # rn will contain the requested information
+ end
+
+Information like a user's real name are internally requested and
+handled using the {Weechat::IRC::Whois} class, which can be accessed
+using {Weechat::IRC::User#whois}, in the same fashion a single property can be requested:
+
+ user.whois do |whois|
+ # one can either rely on Whois#method_missing ...
+ whois.real_name
+
+ # or access the data hash directly
+ whois.data # => {:real_name => "...", ...}
+ end
+
Contributing to the project
===========================
View
2  Rakefile
@@ -1,7 +1,7 @@
require 'rubygems'
require 'rake/gempackagetask'
-VERSION = "0.0.5"
+VERSION = "0.0.6"
spec = Gem::Specification.new do |s|
s.name = "weechat"
View
1  lib/weechat.rb
@@ -277,6 +277,7 @@ def directories
require 'weechat/irc/host.rb'
require 'weechat/irc/identifier.rb'
require 'weechat/irc/user.rb'
+require 'weechat/irc/whois.rb'
require 'weechat/infolist.rb'
require 'weechat/color.rb'
require 'weechat/plugin.rb'
View
26 lib/weechat/irc/user.rb
@@ -6,10 +6,16 @@ class User
attr_reader :flags
attr_reader :color
attr_reader :channel
+ attr_reader :server
+
+
+ # FIXME make users server agnostic
def initialize(args = {})
@name = args[:name]
@identifier = IRC::Identifier.new(args[:host])
@flags, @color, @channel = args.values_at(:flags, :color, :channel)
+ # @flags, @color = args.values_at(:flags, :color)
+ @server = @channel.server
end
%w(chanowner? chanadmin? chanadmin2? op?
@@ -20,11 +26,23 @@ def initialize(args = {})
alias_method :voiced?, :voice?
def ==(other)
- @name == other.name && @host == other.host && @channel == other.channel
+ @name == other.name && @host == other.host #&& @channel == other.channel
end
alias_method :eql?, "=="
alias_method :equal?, :eql?
+ def whois
+ Weechat::IRC::Whois.new(self) { |w| yield(w) if block_given? }
+ end
+
+ def real_name
+ whois { |w| yield(w.real_name) if block_given? }
+ end
+
+ def channels
+ whois { |w| yield(w.channels) if block_given? }
+ end
+
def op
@channel.exec("/op #@name")
end
@@ -65,6 +83,6 @@ def kickban(reason="")
kick(reason)
ban
end
- end
- end
-end
+ end # User
+ end # IRC
+end # Weechat
View
98 lib/weechat/irc/whois.rb
@@ -0,0 +1,98 @@
+module Weechat
+ module IRC
+ class Whois
+ attr_reader :data
+
+ def populated?
+ @populated
+ end
+
+ def process_reply(line)
+ ret = line
+ m = Weechat::IRC::Message.new(line.message)
+ m.params.shift
+
+ nick = m.params.first
+ if nick == @user.name
+ ret = nil
+
+ case m.command
+ when "401", "318" # ERR_NOSUCHNICK, RPL_ENDOFWHOIS
+ # NOTE: 401 (no such nick) may not result in a 318
+ @hooks.each do |hook|
+ hook.unhook
+ end
+ @hooks = []
+ @populated = true
+
+ if @block
+ @block.call(self)
+ end
+
+ when "311" # RPL_WHOISUSER
+ @data[:nick] = m.params[0]
+ @data[:user] = m.params[1]
+ @data[:host] = m.params[2]
+ @data[:real_name] = m.params[4]
+
+ when "312" # RPL_WHOISSERVER
+
+ when "313" # RPL_WHOISOPERATOR
+ @data[:operator] = true
+
+ when "317" # RPL_WHOISIDLE
+ @data[:idle] = m.params.first.to_i
+
+ when "301" # RPL_AWAY
+ @data[:away_reason] = m.params.last
+
+ when "319" # RPL_WHOISCHANNELS
+ m.params[1].split(" ").each do |channel|
+ if channel !~ /^([&#!.~]|\+{2}).+$/
+ channel[0..0] = ""
+ end
+
+ @data[:channels] << Weechat::IRC::Channel.new(@user.server, channel)
+ end
+ end
+ end
+
+ ret
+ end
+
+ def initialize(user, &block)
+ @data = {
+ :nick => "",
+ :user => "",
+ :host => "",
+ :real_name => "",
+ :operator => false,
+ :idle => 0,
+ :away_reason => "",
+ :channels => [],
+ }
+ @hooks = []
+ @populated = false
+ @block = block #async callback
+ @user = user
+
+ # DONE: 301, 311, 313, 317, 318, 319, 401
+ # CONSIDERED: 312
+ # TODO: 307, 310, 320, 338, 330, 378, 379, 671
+ [301,307,310,311,312,313,317,318,319,320,338,330,378,379,401,671].each do |numeric|
+ @hooks << Weechat::Modifier.new("irc_in_#{numeric}") {|modifier, line| process_reply(line) }
+ end
+
+ user.server.exec("/whois #{user.name}")
+ end
+
+ def method_missing(m, *args)
+ if @data.has_key?(m.to_sym)
+ @data[m.to_sym]
+ else
+ super
+ end
+ end
+ end # Whois
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.