-- Written by Moritz Hoffmann 2006, part of the orx-irc project
-- Released under the BSD license

::CLASS Channel PUBLIC
::METHOD Init
    expose name users id client mode
    use arg client, name
    -- register channel with client
    if client~channels~last = .nil then do
	client~channels[1] = self  -- the current user object is the first one to be created
	id = 1                   -- the user's id is 1
    end
    else do
	id = client~channels~last+1 -- create a new id
	client~channels[id] = self
    end
    mode = .directory~new
    -- users is an array with index=id of the user and the assigend string the user's mode
    users = .array~new()
    call debug "[Channel: Init: Created" name "("id")",3
  
::METHOD UnInit
    expose name users id client
    call debug "[Channel: UnInit:" name id,1
    client~channels~remove(id)
    drop self

::METHOD Event
    expose users client
    use arg message
    call debug "[Channel: Event:" message~class,1
    SELECT
	when message~class=.msgjoin then do
    	    -- a new user joined the channel
    	    users[message~sender~id] = .true
    	    call debug "[Channel: Event: join message received:" message~sender~MakeString "("message~sender~id")",8
    	    client~ClientEvent("IRC.EVENT.JOIN",message)
        end
	when message~class=.msgpart then do
    	    users~remove(message~sender~id)
    	    call debug "[Channel: Event: part message received:" message~sender~MakeString "("message~sender~id")",8
	    client~ClientEvent("IRC.EVENT.PART",message)
    	    if message~sender~id = 1 then self~UnInit
	end
	when message~class=.msgkick then do
	    users~remove(message~sender~id)
	    call debug "[Channel: Event: kick message received:" message~sender~MakeString "("message~sender~id") kicked" message~receiver~MakeString "("message~receiver~id")",8
    	    client~ClientEvent("IRC.EVENT.KICK",message)
	    if message~receiver~id = 1 then do
		-- remove this channel as we just got kicked
		self~UnInit
	    end
	end
	when message~class=.msgquit then do
    	    users~remove(message~sender~id)
    	    call debug "[Channel: Event: quit message received:" message~sender~MakeString "("message~sender~id")",8
    	    client~ClientEvent("IRC.EVENT.QUIT",message)
	end
	when message~class=.msgkill then do
    	    users~remove(message~sender~id)
    	    call debug "[Channel: Event: kill message received:" message~sender~MakeString "("message~sender~id")",8
    	    client~ClientEvent("IRC.EVENT.KILL",message)
	end
	when message~class=.msgmode then self~SetMode(message)
	when message~class=.smode then self~SetMode(message)
	when message~class=.msgtopic then do
    	    call debug "[Channel: Event: topic msg.   received:" message~sender~MakeString "("message~sender~id")",8
    	    client~ClientEvent("IRC.EVENT.TOPIC",message)
	end
	when message~class=.privmsg then
	    Client~ClientEvent("IRC.EVENT.CPRIVMSG",message)
	when message~class=.unotice then
	    Client~ClientEvent("IRC.EVENT.CPRIVMSG",message)
	otherwise do
	    call debug "[Channel: Event: unknown message object received!" message~class,16
	end
    END

::METHOD ID
    expose id; return id

::METHOD MakeString
    expose name; return name

::METHOD UserIsMember
    expose users
    use arg nick
    if datatype(nick,"NUM") then return users~hasindex(nick)
    else return users~hasindex(nick~id)
    
::METHOD Users
    expose users; return users

::METHOD SetMode PRIVATE
    expose mode bans excludes
    use arg message
    modestr = message~string~space
    parse var modestr modifiers arguments
    do i = 1 to modifiers~length
	char = modifiers~substr(i,1)
	SELECT
	    when char = "+" then status = .true
	    when char = "-" then status = .false
	    when char~verify("tn") = 0 then
		if status then mode = RemoveStr(mode,char)||char
		    else mode = RemoveStr(mode,char)
	    -- here the banlist needs to be updated
	    when char = "b" then nop
	    otherwise call debug "[Channel: SetMode: unknown modifier" char,8
	END
    end
    