Class: Iarm::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/iarm/server.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.start(uri = nil) ⇒ Object



103
104
105
106
# File 'lib/iarm/server.rb', line 103

def self.start(uri=nil)
  DRb.start_service(uri, self.new)
  DRb.thread
end

Instance Method Details

#depart(nickname, channelname = nil) ⇒ Object

nil=depart ALL channels and log out client



46
47
48
49
50
51
# File 'lib/iarm/server.rb', line 46

def depart(nickname, channelname=nil)  # nil=depart ALL channels and log out client
  handle = touch_nickname(nickname)
  channel = (find_channel(channelname) if channelname)
  handle.depart(channel).each {|ch| check_channel_empty(ch) }
  @handles.delete(nickname) if channelname.nil?
end

#get_topic(channelname) ⇒ Object



97
98
99
100
101
# File 'lib/iarm/server.rb', line 97

def get_topic(channelname)
  if(channel = find_channel(channelname))
    channel.topic
  end
end

#getmsg(who, timeout = 0) ⇒ Object

returns msg or nil if no messages and timed out. also serves as a keep-alive to avoid getting killed by ttl if who=nil then it listens on all channels, but only one client can do this at once if another client is already listening with the same who-id, it has the effect of making them return immediately (before their timeout is up)



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/iarm/server.rb', line 58

def getmsg(who, timeout=0)
  handle = touch_nickname(who)
  if(timeout != 0 && handle.no_msgs?)
    handle.poke

    handle.timer.wait(timeout) do |mode|
      mode && handle.no_msgs?
    end
  end
  handle.next_msg
end

#getmsgs(who, timeout = 0) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/iarm/server.rb', line 70

def getmsgs(who, timeout=0)
  res = [ getmsg(who, timeout) ]
  while(!res.empty? && (msg = getmsg(who, 0)))
    res << msg
  end
  res
end

#join(who, channelname, key = nil) ⇒ Object

returns true if joined, false if denied, and nil if new channel formed



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/iarm/server.rb', line 27

def join(who, channelname, key=nil)      # returns true if joined, false if denied, and nil if new channel formed
  retval = nil
  handle = touch_nickname(who)
  @channel_mutex.synchronize do
    if(channel = find_channel(channelname))
      retval = channel.key == key		# verify password
    else
      @channels[channelname].key = key 	# create the channel
    end

    if(retval != false)  # if retval is true (joined existing) or nil (new channel formed)
      if(!(channel = @channels[channelname]).members.has_key?(handle)) # don't re-join them if they've already joined before
        handle.join(channel)
      end
    end
  end
  retval
end

#list(pattern = nil) ⇒ Object



19
20
21
# File 'lib/iarm/server.rb', line 19

def list(pattern=nil)
  pattern ? @channels.keys.grep(pattern) : @channels.keys
end

#ping(nickname = nil) ⇒ Object



9
10
11
12
# File 'lib/iarm/server.rb', line 9

def ping(nickname=nil)
  touch_nickname(nickname) if nickname
  'pong'
end

#say(nickname, channelname, data) ⇒ Object



78
79
80
81
82
83
# File 'lib/iarm/server.rb', line 78

def say(nickname, channelname, data)
  handle = touch_nickname(nickname)
  if(channel = find_channel(channelname))
    channel.post(handle, Iarm::Msg.new(channelname, nickname, data))
  end
end

#set_topic(nickname, channelname, topic_data) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/iarm/server.rb', line 85

def set_topic(nickname, channelname, topic_data)
  handle = touch_nickname(nickname)
  if(channel = find_channel(channelname))
    topic_data = Msg::Topic.new(channelname, nickname, topic_data) unless topic_data.kind_of?(Msg::Topic)
    if(channel.topic != topic_data)
      channel.topic = topic_data
      channel.post(handle, topic_data)
      topic_data
    end
  end
end

#ttl(nickname, ttl_secs) ⇒ Object



14
15
16
17
# File 'lib/iarm/server.rb', line 14

def ttl(nickname, ttl_secs)
  handle = touch_nickname(nickname)
  handle.ttl = ttl_secs
end

#who(channelname) ⇒ Object



23
24
25
# File 'lib/iarm/server.rb', line 23

def who(channelname)
  (ch = find_channel(channelname)) && ch.members_by_name || {}
end