Class: Vetinari::Channel

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/vetinari/channel.rb

Overview

Data structure which is used for storing users: => {User => [modes]}

Example: => {:user => #<User nick=“Ponder”>, :modes => [‘v’, ‘o’]}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, bot) ⇒ Channel

Returns a new instance of Channel.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/vetinari/channel.rb', line 12

def initialize(name, bot)
  @name = name
  @bot = bot
  @users = Set.new
  @users_with_modes = {}
  @modes = {}
  @lists = Hash.new { |hash, key| hash[key] = [] }
  @mutex = Mutex.new

  link(@bot)
end

Instance Attribute Details

#listsObject (readonly)

Returns the value of attribute lists.



10
11
12
# File 'lib/vetinari/channel.rb', line 10

def lists
  @lists
end

#modesObject (readonly)

Returns the value of attribute modes.



10
11
12
# File 'lib/vetinari/channel.rb', line 10

def modes
  @modes
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/vetinari/channel.rb', line 10

def name
  @name
end

#usersObject (readonly)

Returns the value of attribute users.



10
11
12
# File 'lib/vetinari/channel.rb', line 10

def users
  @users
end

#users_with_modesObject (readonly)

Returns the value of attribute users_with_modes.



10
11
12
# File 'lib/vetinari/channel.rb', line 10

def users_with_modes
  @users_with_modes
end

Instance Method Details

#add_user(user, modes = []) ⇒ Object



160
161
162
163
164
165
# File 'lib/vetinari/channel.rb', line 160

def add_user(user, modes = [])
  @mutex.synchronize do
    @users << user
    @users_with_modes[user] = modes
  end
end

#ban(hostmask) ⇒ Object



54
55
56
# File 'lib/vetinari/channel.rb', line 54

def ban(hostmask)
  mode '+b', hostmask
end

#deop(user) ⇒ Object



87
88
89
# File 'lib/vetinari/channel.rb', line 87

def deop(user)
  mode '-o', user.nick
end

#devoice(user_or_nick) ⇒ Object



95
96
97
# File 'lib/vetinari/channel.rb', line 95

def devoice(user_or_nick)
  mode '-v', user.nick
end

#get_modeObject



253
254
255
# File 'lib/vetinari/channel.rb', line 253

def get_mode
  @bot.raw "MODE #{@name}"
end

#has_user?(user) ⇒ Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/vetinari/channel.rb', line 174

def has_user?(user)
  @users.include?(user)
end

#hop(message = nil) ⇒ Object



154
155
156
157
158
# File 'lib/vetinari/channel.rb', line 154

def hop(message = nil)
  key = @modes['k']
  part message
  join key
end

#inspectObject



261
262
263
# File 'lib/vetinari/channel.rb', line 261

def inspect
  "#<Channel name=#{@name.inspect}>"
end

#invite(user) ⇒ Object



79
80
81
# File 'lib/vetinari/channel.rb', line 79

def invite(user)
  @bot.raw "INVITE #{@name} #{user.nick}"
end

#join(key = nil) ⇒ Object



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
# File 'lib/vetinari/channel.rb', line 99

def join(key = nil)
  if @bot.channels.has_channel?(@name)
    return Future.new { :already_joined }
  end

  condition = Celluloid::Condition.new
  callbacks = Set.new

  callbacks << @bot.on(:join) do |env|
    if env[:channel].name == @name
      condition.signal :joined
      callbacks.each { |cb| cb.async.remove_and_terminate if cb.alive? }
    end
  end

  raw_messages = {
    475 => :locked,
    471 => :full,
    474 => :banned,
    473 => :invite_only
  }

  raw_messages.each do |raw, msg|
    callbacks << @bot.on(raw) do |env|
      channel_name = env[:params].split(' ')[1]

      if channel_name == @name
        condition.signal msg
        callbacks.each { |cb| cb.async.remove_and_terminate if cb.alive? }
      end
    end
  end

  after(5) do
    condition.signal :timeout
    callbacks.each { |cb| cb.async.remove_and_terminate if cb.alive? }
  end

  if key
    @bot.raw "JOIN #{@name} #{key}"
  else
    @bot.raw "JOIN #{@name}"
  end

  Future.new { condition.wait }
end

#kick(user, reason = nil) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/vetinari/channel.rb', line 71

def kick(user, reason = nil)
  if reason
    @bot.raw "KICK #{@name} #{user.nick} :#{reason}"
  else
    @bot.raw "KICK #{@name} #{user.nick}"
  end
end

#lock(key) ⇒ Object



62
63
64
# File 'lib/vetinari/channel.rb', line 62

def lock(key)
  @bot.raw "MODE #{@name} +k #{key}"
end

#message(message) ⇒ Object



257
258
259
# File 'lib/vetinari/channel.rb', line 257

def message(message)
  @bot.raw "PRIVMSG #{@name} :#{message}"
end

#mode(modes, params = nil) ⇒ Object



245
246
247
248
249
250
251
# File 'lib/vetinari/channel.rb', line 245

def mode(modes, params = nil)
  if params
    @bot.raw "MODE #{@name} #{modes} #{params}"
  else
    @bot.raw "MODE #{@name} #{modes}"
  end
end

#modes_of(user) ⇒ Object

=> user, :modes => @users_with_modes if user end



198
199
200
# File 'lib/vetinari/channel.rb', line 198

def modes_of(user)
  @users_with_modes[user] if has_user?(user)
end

#op(user) ⇒ Object



83
84
85
# File 'lib/vetinari/channel.rb', line 83

def op(user)
  mode '+o', user.nick
end

#part(message = nil) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/vetinari/channel.rb', line 146

def part(message = nil)
  if message
    @bot.raw "PART #{@name} :#{message}"
  else
    @bot.raw "PART #{@name}"
  end
end

#remove_user(user) ⇒ Object



167
168
169
170
171
172
# File 'lib/vetinari/channel.rb', line 167

def remove_user(user)
  @mutex.synchronize do
    @users_with_modes.delete(user)
    @users.delete?(user) ? user : nil
  end
end

#set_mode(mode, isupport) ⇒ Object

TODO



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
# File 'lib/vetinari/channel.rb', line 203

def set_mode(mode, isupport)
  if isupport['PREFIX'].keys.include?(mode[:mode])
    user = find_user(mode[:param])
    if user
      case mode[:direction]
      when :'+'
        @users_with_modes[user] << mode[:mode]
      when :'-'
        @users_with_modes[user].delete(mode[:mode])
      end
    end
  elsif isupport['CHANMODES']['A'].include?(mode[:mode])
    case mode[:direction]
    when :'+'
      add_to_list(mode[:mode], mode[:param])
    when :'-'
      remove_from_list(mode[:mode], mode[:param])
    end
  elsif isupport['CHANMODES']['B'].include?(mode[:mode])
    case mode[:direction]
    when :'+'
      set_channel_mode(mode[:mode], mode[:param])
    when :'-'
      unset_channel_mode(mode[:mode])
    end
  elsif isupport['CHANMODES']['C'].include?(mode[:mode])
    case mode[:direction] 
    when :'+'
      set_channel_mode(mode[:mode], mode[:param])
    when :'-'
      unset_channel_mode(mode[:mode])
    end
  elsif isupport['CHANMODES']['D'].include?(mode[:mode])
    case mode[:direction] 
    when :'+'
      set_channel_mode(mode[:mode], true)
    when :'-'
      unset_channel_mode(mode[:mode])
    end
  end
end

#to_sObject



265
266
267
# File 'lib/vetinari/channel.rb', line 265

def to_s
  @name
end

#topic=(topic) ⇒ Object

@topic

  end
end

end



50
51
52
# File 'lib/vetinari/channel.rb', line 50

def topic=(topic)
  @bot.raw "TOPIC #{@name} :#{topic}"
end

#unban(hostmask) ⇒ Object



58
59
60
# File 'lib/vetinari/channel.rb', line 58

def unban(hostmask)
  mode '-b', hostmask
end

#unlockObject



66
67
68
69
# File 'lib/vetinari/channel.rb', line 66

def unlock
  key = @modes['k']
  @bot.raw "MODE #{@name} -k #{key}" if key
end

#voice(user_or_nick) ⇒ Object



91
92
93
# File 'lib/vetinari/channel.rb', line 91

def voice(user_or_nick)
  mode '+v', user.nick
end