Class: IRCd::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/rupircd/channel.rb

Direct Known Subclasses

NoModeChannel, SafeChannel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, who, chname) ⇒ Channel



221
222
223
224
225
226
227
228
# File 'lib/rupircd/channel.rb', line 221

def initialize(server, who, chname)
  @server = server
  @mode = ChannelMode.new(server, chname)
  @name = chname
  @topic = ""
  @members = []
  @invited = []
end

Instance Attribute Details

#membersObject (readonly)

Returns the value of attribute members.



219
220
221
# File 'lib/rupircd/channel.rb', line 219

def members
  @members
end

#modeObject (readonly)

Returns the value of attribute mode.



219
220
221
# File 'lib/rupircd/channel.rb', line 219

def mode
  @mode
end

#nameObject (readonly)

Returns the value of attribute name.



219
220
221
# File 'lib/rupircd/channel.rb', line 219

def name
  @name
end

Instance Method Details

#get_topic(user, text = false) ⇒ Object



344
345
346
347
348
349
350
351
352
# File 'lib/rupircd/channel.rb', line 344

def get_topic(user, text=false)
  return nil if @mode.secret && !joined?(user)
  return @topic if text
  if @topic.empty?
    ["331", @name, "No topic is set"]
  else
    ["332", @name, @topic]
  end
end

#handle_mode(who, params) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/rupircd/channel.rb', line 304

def handle_mode(who, params)
  if params.empty?
    ["324", @name, *@mode.get_flags]
  else
    flags, *targets = params
    flags = "+" + flags if flags[0,1] != "+" && flags[0,1] != "-"
    if @mode.op?(who)
      rpl = @mode.set_flags(who, flags, targets)
      send_to_members(who, Command::MODE, @name, *rpl) if rpl
      nil
    elsif !joined?(who)
      return ["482", @name, "You're not channel operator"]
    else
      return ["442", @name, "You're not on that channel"]
    end
  end
end

#invite(from, who) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/rupircd/channel.rb', line 284

def invite(from, who)
  if !joined?(from)
    return ["442", @name, "You're not on that channel"]
  elsif joined?(who)
    return ["443", who.nick, "is already on channel"]
  elsif !@mode.can_invite?(from)
    return ["482", @name, "You're not channel operator"]
  else 
    @invited.push who
    @server.send_client_message(who, from, Command::INVITE, who.nick, @name)
    return ["341", who.nick, @name]
  end
end

#join(who, key = "") ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/rupircd/channel.rb', line 230

def join(who, key="")
  if @mode.banned?(who)
    ["474", @name, "Cannot join channel (+b)"]
  elsif @mode.invite && !@invited.include?(who)
    ["473", @name, "Cannot join channel (+i)"]
  elsif !@mode.key.empty? && @mode.key != key
    ["475", @name, "Cannot join channel (+k)"]
  elsif @mode.user_max >= @members.size
    ["471", @name, "Cannot join channel (+l)"]
  elsif !joined?(who)
    who.joined_channels.push self
    @members.unshift who
    
    @mode.add_op who if @members.size == 1
    sends = []
    sends << get_topic(who)
    sends += names(who)
    @server.send_client_message(who, who, Command::JOIN, @name)
    @server.handle_reply(who, sends)
    send_to_other_members(who, Command::JOIN, @name)
    return nil
  end
end

#joined?(who) ⇒ Boolean



280
281
282
# File 'lib/rupircd/channel.rb', line 280

def joined?(who)
  @members.include?(who)
end

#kick(who, by, msg = "no reason") ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/rupircd/channel.rb', line 254

def kick(who, by, msg="no reason")
  if !@members.include?(by)
    ["442", @name, "You're not on that channel"]
  elsif !@members.include?(who)
    ["441", who.nick, @name, "They aren't on that channel"]
  elsif @mode.op?(by)
    send_to_members(by, Command::KICK, @name, who.nick, msg)
    unregister(who)
    nil
  else
    ["482", @name, "You're not channel operator"]
  end
end

#names(user) ⇒ Object



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/rupircd/channel.rb', line 376

def names(user)
  return nil if @mode.secret && !joined?(user)
  mems = @members.dup
  unless @members.include?(user)
    mems = mems.find_all{|usr|
      !usr.invisible
    }
  end
  memlist = mems.map{|us|
    if @mode.op?(us)
      pr = "@"
    else
      pr = ""
    end
    pr+us.nick
  }.join(" ")
  sends = []
  unless memlist.empty?
    prefix = if @mode.secret
      "@"
    elsif @mode.private
      "*"
    else
      "="
    end
    sends << ["353", prefix, @name, memlist]
  end
  sends << ["366", @name, "End of NAMES list"]
  sends
end

#notice(who, msg) ⇒ Object



365
366
367
368
369
370
# File 'lib/rupircd/channel.rb', line 365

def notice(who, msg)
  if @mode.can_talk?(who)
    send_to_other_members(who, Command::NOTICE, @name, msg)
  end
  return nil
end

#part(who, message = "") ⇒ Object



322
323
324
325
326
327
328
329
330
# File 'lib/rupircd/channel.rb', line 322

def part(who, message="")
  if joined?(who)
    send_to_members(who, Command::PART, @name, message)
    unregister(who)
    return nil
  else
    return ["442", @name, "You're not on that channel"]
  end
end

#privmsg(who, msg) ⇒ Object



354
355
356
357
358
359
360
361
362
363
# File 'lib/rupircd/channel.rb', line 354

def privmsg(who, msg)
  if @mode.n && !@members.include?(who)
    return ["404", @name, "Cannot send to channel"]
  elsif @mode.can_talk?(who)
    send_to_other_members(who, Command::PRIVMSG, @name, msg)
    nil
  else
    return ["404", @name, "Cannot send to channel"]
  end
end

#send_to_members(user, command, *args) ⇒ Object



268
269
270
271
272
# File 'lib/rupircd/channel.rb', line 268

def send_to_members(user, command, *args)
  @members.each{|mem|
    @server.send_client_message(mem, user, command, *args)
  }
end

#send_to_other_members(user, command, *args) ⇒ Object



274
275
276
277
278
# File 'lib/rupircd/channel.rb', line 274

def send_to_other_members(user, command, *args)
  (@members-[user]).each{|mem|
    @server.send_client_message(mem, user, command, *args)
  }
end

#set_topic(who, str) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
# File 'lib/rupircd/channel.rb', line 332

def set_topic(who, str)
  if @mode.can_change_topic?(who)
    @topic = str
    send_to_members(who, Command::TOPIC, @name, @topic)
    return nil
  elsif !@members.include?(who)
    return ["442", @name, "You're not on that channel"]
  else
    return ["482", @name, "You're not channel operator"]
  end
end

#unregister(usr) ⇒ Object



298
299
300
301
302
# File 'lib/rupircd/channel.rb', line 298

def unregister(usr)
  @members.delete(usr)
  @mode.unregister(usr)
  usr.joined_channels.delete(self)
end

#visible?(user) ⇒ Boolean



372
373
374
# File 'lib/rupircd/channel.rb', line 372

def visible?(user)
  !@mode.secret || joined?(user)
end