aboutsummaryrefslogtreecommitdiffstats
path: root/src/irc/channel.lua
blob: f1fbf3c3aa749bf07c0fb28cbf0c90c93337248e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
---
-- Implementation of the Channel class

-- initialization {{{
local base =   _G
local irc =    require 'irc'
local misc =   require 'irc.misc'
local socket = require 'socket'
local table =  require 'table'
-- }}}

---
-- This module implements a channel object representing a single channel we
-- have joined
-- @release 0.02
module 'irc.channel'

-- object metatable {{{
-- TODO: this <br /> shouldn't be necessary - bug in luadoc
---
-- An object of the Channel class represents a single joined channel. It has
-- several table fields, and can be used in string contexts (returning the
-- channel name).<br />
-- @class table
-- @name Channel
-- @field name     Name of the channel (read only)
-- @field topic    Channel topic, if set (read/write, writing to this sends a
--                 topic change request to the server for this channel)
-- @field chanmode Channel mode (public/private/secret) (read only)
-- @field members  Array of all members of this channel
local mt = {
    -- __index() {{{
    __index =    function(self, key)
                     if key == "name" then
                         return self._name
                     elseif key == "topic" then
                         return self._topic
                     elseif key == "chanmode" then
                         return self._chanmode
                     else
                         return _M[key]
                     end
                 end,
    -- }}}
    -- __newindex() {{{
    __newindex = function(self, key, value)
                     if key == "name" then
                         return
                     elseif key == "topic" then
                         irc.send("TOPIC", self._name, value)
                     elseif key == "chanmode" then
                         return
                     else
                         base.rawset(self, key, value)
                     end
                 end,
    -- }}}
    -- __concat() {{{
    __concat =   function(first, second)
                     local first_str, second_str

                     if base.type(first) == "table" then
                         first_str = first._name
                     else
                         first_str = first
                     end
                     if base.type(second) == "table" then
                         second_str = second._name
                     else
                         second_str = second
                     end

                     return first_str .. second_str
                 end,
    -- }}}
    -- __tostring() {{{
    __tostring = function(self)
                     return self._name
                 end
    -- }}}
}
-- }}}

-- private methods {{{
-- set_basic_mode {{{
--
-- Sets a no-arg mode on a channel.
-- @name chan:set_basic_mode
-- @param self   Channel object
-- @param set    True to set the mode, false to unset it
-- @param letter Letter of the mode
local function set_basic_mode(self, set, letter)
    if set then
        irc.send("MODE", self.name, "+" .. letter)
    else
        irc.send("MODE", self.name, "-" .. letter)
    end
end
-- }}}
-- }}}

-- constructor {{{
---
-- Creates a new Channel object.
-- @param chan Name of the new channel
-- @return The new channel instance
function new(chan)
    return base.setmetatable({_name = chan, _topic = {}, _chanmode = "",
                              _members = {}}, mt)
end
-- }}}

-- public methods {{{
-- iterators {{{
-- each_op {{{
---
-- Iterator over the ops in the channel
-- @param self Channel object
function each_op(self)
    return function(state, arg)
               return misc.value_iter(state, arg,
                                      function(v)
                                          return v:sub(1, 1) == "@"
                                      end)
           end,
           self._members,
           nil
end
-- }}}

-- each_voice {{{
---
-- Iterator over the voiced users in the channel
-- @param self Channel object
function each_voice(self)
    return function(state, arg)
               return misc.value_iter(state, arg,
                                      function(v)
                                          return v:sub(1, 1) == "+"
                                      end)
           end,
           self._members,
           nil
end
-- }}}

-- each_user {{{
---
-- Iterator over the normal users in the channel
-- @param self Channel object
function each_user(self)
    return function(state, arg)
               return misc.value_iter(state, arg,
                                      function(v)
                                          return v:sub(1, 1) ~= "@" and
                                                 v:sub(1, 1) ~= "+"
                                      end)
           end,
           self._members,
           nil
end
-- }}}

-- each_member {{{
---
-- Iterator over all users in the channel
-- @param self Channel object
function each_member(self)
    return misc.value_iter, self._members, nil
end
-- }}}
-- }}}

-- return tables of users {{{
-- ops {{{
---
-- Gets an array of all the ops in the channel.
-- @param self Channel object
-- @return Array of channel ops
function ops(self)
    local ret = {}
    for nick in self:each_op() do
        table.insert(ret, nick)
    end
    return ret
end
-- }}}

-- voices {{{
---
-- Gets an array of all the voiced users in the channel.
-- @param self Channel object
-- @return Array of channel voiced users
function voices(self)
    local ret = {}
    for nick in self:each_voice() do
        table.insert(ret, nick)
    end
    return ret
end
-- }}}

-- users {{{
---
-- Gets an array of all the normal users in the channel.
-- @param self Channel object
-- @return Array of channel normal users
function users(self)
    local ret = {}
    for nick in self:each_user() do
        table.insert(ret, nick)
    end
    return ret
end
-- }}}

-- members {{{
---
-- Gets an array of all the users in the channel.
-- @param self Channel object
-- @return Array of channel users
function members(self)
    local ret = {}
    -- not just returning self._members, since the return value shouldn't be
    -- modifiable
    for nick in self:each_member() do
        table.insert(ret, nick)
    end
    return ret
end
-- }}}
-- }}}

-- setting modes {{{
-- ban {{{
-- TODO: hmmm, this probably needs an appropriate mask, rather than a nick
---
-- Ban a user from a channel.
-- @param self Channel object
-- @param name User to ban
function ban(self, name)
    irc.send("MODE", self.name, "+b", name)
end
-- }}}

-- unban {{{
-- TODO: same here
---
-- Remove a ban on a user.
-- @param self Channel object
-- @param name User to unban
function unban(self, name)
    irc.send("MODE", self.name, "-b", name)
end
-- }}}

-- voice {{{
---
-- Give a user voice on a channel.
-- @param self Channel object
-- @param name User to give voice to
function voice(self, name)
    irc.send("MODE", self.name, "+v", name)
end
-- }}}

-- devoice {{{
---
-- Remove voice from a user.
-- @param self Channel object
-- @param name User to remove voice from
function devoice(self, name)
    irc.send("MODE", self.name, "-v", name)
end
-- }}}

-- op {{{
---
-- Give a user ops on a channel.
-- @param self Channel object
-- @param name User to op
function op(self, name)
    irc.send("MODE", self.name, "+o", name)
end
-- }}}

-- deop {{{
---
-- Remove ops from a user.
-- @param self Channel object
-- @param name User to remove ops from
function deop(self, name)
    irc.send("MODE", self.name, "-o", name)
end
-- }}}

-- set_limit {{{
---
-- Set a channel limit.
-- @param self      Channel object
-- @param new_limit New value for the channel limit (optional; limit is unset
--                  if this argument isn't passed)
function set_limit(self, new_limit)
    if new_limit then
        irc.send("MODE", self.name, "+l", new_limit)
    else
        irc.send("MODE", self.name, "-l")
    end
end
-- }}}

-- set_key {{{
---
-- Set a channel password.
-- @param self Channel object
-- @param key  New channel password (optional; password is unset if this
--             argument isn't passed)
function set_key(self, key)
    if key then
        irc.send("MODE", self.name, "+k", key)
    else
        irc.send("MODE", self.name, "-k")
    end
end
-- }}}

-- set_private() {{{
---
-- Set the private state of a channel.
-- @param self Channel object
-- @param set  True to set the channel as private, false to unset it
function set_private(self, set)
    set_basic_mode(self, set, "p")
end
-- }}}

-- set_secret {{{
---
-- Set the secret state of a channel.
-- @param self Channel object
-- @param set  True to set the channel as secret, false to unset it
function set_secret(self, set)
    set_basic_mode(self, set, "s")
end
-- }}}

-- set_invite_only {{{
---
-- Set whether joining the channel requires an invite.
-- @param self Channel object
-- @param set  True to set the channel invite only, false to unset it
function set_invite_only(self, set)
    set_basic_mode(self, set, "i")
end
-- }}}

-- set_topic_lock {{{
---
-- If true, the topic can only be changed by an op.
-- @param self Channel object
-- @param set  True to lock the topic, false to unlock it
function set_topic_lock(self, set)
    set_basic_mode(self, set, "t")
end
-- }}}

-- set_no_outside_messages {{{
---
-- If true, users must be in the channel to send messages to it.
-- @param self Channel object
-- @param set  True to require users to be in the channel to send messages to
--             it, false to remove this restriction
function set_no_outside_messages(self, set)
    set_basic_mode(self, set, "n")
end
-- }}}

-- set moderated {{{
---
-- Set whether voice is required to speak.
-- @param self Channel object
-- @param set  True to set the channel as moderated, false to unset it
function set_moderated(self, set)
    set_basic_mode(self, set, "m")
end
-- }}}
-- }}}

-- accessors {{{
-- contains {{{
---
-- Test if a user is in the channel.
-- @param self Channel object
-- @param nick Nick to search for
-- @return True if the nick is in the channel, false otherwise
function contains(self, nick)
    for member in self:each_member() do
        local member_nick = member:gsub('@+', '')
        if member_nick == nick then
            return true
        end
    end
    return false
end
-- }}}

-- add_user() {{{
function add_user(self, user, mode)
    mode = mode or ''
    self._members[user] = mode .. user
end
-- }}}

-- remove_user() {{{
function remove_user(self, user)
    self._members[user] = nil
end
-- }}}

-- change_status() {{{
function change_status(self, user, on, mode)
    if on then
        if mode == 'o' then
            self._members[user] = '@' .. user
        elseif mode == 'v' then
            self._members[user] = '+' .. user
        end
    else
        if (mode == 'o' and self._members[user]:sub(1, 1) == '@') or
           (mode == 'v' and self._members[user]:sub(1, 1) == '+') then
            self._members[user] = user
        end
    end
end
-- }}}

-- change_nick {{{
function change_nick(self, old_nick, new_nick)
    for member in self:each_member() do
        local member_nick = member:gsub('@+', '')
        if member_nick == old_nick then
            local mode = self._members[old_nick]:sub(1, 1)
            if mode ~= '@' and mode ~= '+' then mode = "" end
            self._members[old_nick] = nil
            self._members[new_nick] = mode .. new_nick
            break
        end
    end
end
-- }}}
-- }}}
-- }}}