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
# 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
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



158
159
160
161
162
163
# File 'lib/vetinari/channel.rb', line 158

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

#ban(hostmask) ⇒ Object



52
53
54
# File 'lib/vetinari/channel.rb', line 52

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

#deop(user) ⇒ Object



85
86
87
# File 'lib/vetinari/channel.rb', line 85

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

#devoice(user_or_nick) ⇒ Object



93
94
95
# File 'lib/vetinari/channel.rb', line 93

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

#get_modeObject



251
252
253
# File 'lib/vetinari/channel.rb', line 251

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

#has_user?(user) ⇒ Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/vetinari/channel.rb', line 172

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

#hop(message = nil) ⇒ Object



152
153
154
155
156
# File 'lib/vetinari/channel.rb', line 152

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

#inspectObject



259
260
261
# File 'lib/vetinari/channel.rb', line 259

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

#invite(user) ⇒ Object



77
78
79
# File 'lib/vetinari/channel.rb', line 77

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

#join(key = nil) ⇒ Object



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

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.remove_and_terminate }
    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.remove_and_terminate }
      end
    end
  end

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

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

  Future.new { condition.wait }
end

#kick(user, reason = nil) ⇒ Object



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

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



60
61
62
# File 'lib/vetinari/channel.rb', line 60

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

#message(message) ⇒ Object



255
256
257
# File 'lib/vetinari/channel.rb', line 255

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

#mode(modes, params = nil) ⇒ Object



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

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



196
197
198
# File 'lib/vetinari/channel.rb', line 196

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

#op(user) ⇒ Object



81
82
83
# File 'lib/vetinari/channel.rb', line 81

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

#part(message = nil) ⇒ Object



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

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

#remove_user(user) ⇒ Object



165
166
167
168
169
170
# File 'lib/vetinari/channel.rb', line 165

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



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

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



263
264
265
# File 'lib/vetinari/channel.rb', line 263

def to_s
  @name
end

#topic=(topic) ⇒ Object

@topic

  end
end

end



48
49
50
# File 'lib/vetinari/channel.rb', line 48

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

#unban(hostmask) ⇒ Object



56
57
58
# File 'lib/vetinari/channel.rb', line 56

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

#unlockObject



64
65
66
67
# File 'lib/vetinari/channel.rb', line 64

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

#voice(user_or_nick) ⇒ Object



89
90
91
# File 'lib/vetinari/channel.rb', line 89

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