-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommands.lua
472 lines (446 loc) · 13.9 KB
/
commands.lua
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
--List of files to load
dofile("tableSave.lua")
math.randomseed(os.time())
commands = {}
allCommands = {}
local stepcount=0
local cmdcount = 0
local function infhook()
stepcount = stepcount+1
if stepcount>100000 then
stepcount=0
debug.sethook()
error("Break INF LOOP")
else
return
end
end
function add_cmd(f, name, lvl, help, shown, aliases)
if type(f)~="function" then return end
lvl = commandPermissions[name] or lvl
allCommands[name]={["name"]=name,["f"]=f,["level"]=lvl,["helptext"]=help,["show"]=shown}
commands[name]=allCommands[name]
if aliases then
for k,v in pairs(aliases) do
allCommands[v] = {["name"]=name,["f"]=f,["level"]=lvl,["helptext"]=help,false}
commands[v]=allCommands[v]
end
end
end
--Helper to return user object from a name
function getUserFromNick(nick, normalize)
if not nick then return end
nick = nick:lower()
for k,v in pairs(irc.channels) do
if v and v.users then
for k2,v2 in pairs(v.users) do
if v2 and v2.nick:lower() == nick then
if normalize then
return normalizeHost(v2)
end
return v2
end
end
end
end
end
-- Hack to change irccloud user hosts to something static
function normalizeHost(usr)
if usr and usr.host and usr.host:find("gateway/web/irccloud.com/x%-%a+") then
copy = {}
for k,v in pairs(usr) do
copy[k] = v
end
copy.host = "gateway/web/irccloud.com/"..copy.username
return copy
end
return usr
end
--Load all plugins in plugins/ here
local listcmd = WINDOWS and "dir /b" or "ls"
local pluginList = io.popen(listcmd.." \"plugins\"")
for file in pluginList:lines() do
if file:sub(#file-3,#file) == ".lua" then
local s,e = pcall(dofile, "plugins/"..file)
if not s then
if config.logchannel then
ircSendChatQ(config.logchannel, e)
end
print("Error loading plugins/"..file..": "..e)
else
print("Loaded plugins/"..file)
end
end
end
--CORE FUNCTIONS HERE
local function userstatus(usr,chan,msg,args)
if chan:sub(1,1)~="#" then return "Be in chan idiot" end
if irc.channels[chan].users[msg] then
local info = msg.." on "..chan
if irc.channels[chan].users[msg].access then
info = info.." has "..irc.channels[chan].users[msg].access
end
ircSendChatQ(chan,info)
end
end
add_cmd(userstatus,"userinfo",101,"Test info about someone",false)
--DISABLE a command for the bot
local function disable(usr,chan,msg,args)
if not msg then return "Usage: '/disable <cmd> [<cmd2> ...]'" end
if args[1]=="all" then
for k,v in pairs(commands) do
if k~="enable" then commands[k]=nil end
end
return "Disabled all"
else
local t={}
for i=1,#args do
local dcmd = args[i]:lower()
if dcmd~="enable" and commands[dcmd] then
commands[dcmd]=nil
table.insert(t,dcmd)
end
end
return "Disabled: "..table.concat(t," ")
end
end
add_cmd(disable,"disable",100,"Disable a command for the bot, '/disable <cmd> [<cmd2> ...]'",true)
--ENABLE a command previously disabled
local function enable(usr,chan,msg,args)
if not msg then return "Usage: '/enable <cmd> [<cmd2> ...]'" end
if args[1]=="all" then
for k,v in pairs(allCommands) do
if not commands[k] then commands[k]=v end
end
return "Enabled all"
else
local t={}
for i=1,#args do
local ecmd = args[i]:lower()
if not commands[ecmd] and allCommands[ecmd] then
commands[ecmd]=allCommands[ecmd]
table.insert(t,ecmd)
end
end
return "Enabled: "..table.concat(t," ")
end
end
add_cmd(enable,"enable",100,"Enables a command previously disabled, '/enable <cmd> [<cmd2> ...]'",true)
--QUIT
local function suicide(usr,chan,msg)
ircSendRawQ("QUIT :moo")
shutdown = true;
end
add_cmd(suicide,"suicide",101,"Quits the bot",true,{"quit","die"})
--PING
local function ping(usr,chan,msg)
return "pong"
end
add_cmd(ping,"ping",0,"pong",true)
--DO
local function dothis(usr,chan,msg) --fix DO and ME with filters
if msg then return "\001ACTION does "..msg.."\001",true end
end
add_cmd(dothis,"do",0,"Performs an action, '/do <text>'",true)
--ME
local function methis(usr,chan,msg)
if msg then return "\001ACTION "..msg.."\001",true end
end
add_cmd(methis,"me",0,"Performs an action, '/me <text>'",true)
--SNEAAK
local function sneaky(usr,chan,msg)
return "You found me!"
end
add_cmd(sneaky,"./",0,nil,false)
local function sneaky2(usr,chan,msg)
ircSendChatQ(usr.nick,"1 point gained")
return nil
end
add_cmd(sneaky2,"./moo",0,nil,false)
local function sneaky3(usr,chan,msg)
return "MooOoOoooOooo"
end
add_cmd(sneaky3,"moo",0,nil,false)
--RELOAD files
local function reload(usr,chan,msg,args)
if not args[1] then args[1]="hooks" args[2]="commands"
else
if getPerms(usr.host)<101 then return "You can't use args" end
end
local rmsg=""
for k,v in pairs(args) do
local s,r = pcall(dofile,v..".lua")
if s then
rmsg = rmsg .. "Loaded: "..v.." "
elseif r:find("No such file or directory") then
s,r = pcall(dofile,"plugins/"..v..".lua")
if s then
rmsg = rmsg .. "Loaded: "..v.." "
else
rmsg = rmsg .. r .. " "
end
else
rmsg = rmsg .. r .. " "
end
end
return rmsg
end
add_cmd(reload,"load",100,"Loads file(s), '/load [<file1>] [<files...>]', Only admin can specify file names.",true,{"reload"})
--ECHO
local function echo(usr,chan,msg)
return msg,true
end
add_cmd(echo,"echo",0,"Replies same text, '/echo <text>'",true,{"say"})
--LIST
local function list(usr,chan,msg,args)
local perm,chanPerm = tonumber(args[1]) or getPerms(usr.host), not tonumber(args[1]) and getPerms(usr.host,chan)
local t = {}
local cmdcount=0
for k,v in pairs(commands) do
if (chanPerm or perm)>=getCommandPerms(k, chan) and commands[k].show then
cmdcount=cmdcount+1
t[cmdcount]=k
end
end
table.sort(t,function(x,y)return x<y end)
return "Commands("..perm..(chanPerm and perm~=chanPerm and ":"..chanPerm or "").."): " .. table.concat(t,", ")
end
add_cmd(list,"list",0,"Lists commands for the specified level, or your own, '/list [<level>]'",true,{"ls","commands"})
local function changeLevel(usr,chan,msg,args,isignore)
local channel, noescape = nil, false
if #args > 1 and args[1]:sub(1,1) == "#" then
channel = args[1]
table.remove(args, 1)
end
if args[1] == "-noescape" then
noescape = true
table.remove(args, 1)
end
if not args[1] or (not isignore and not args[2]) then
if isignore then
return "Usage: '/ignore [<channel>] [-noescape] <user/host> [<seconds>]'"
else
return "Usage: '/chmod [<channel>] [-noescape] <user/host> <level>'"
end
end
local user, seconds, host = args[1], tonumber(args[2]), nil
if irc.channels[chan].users[user] then
host = irc.channels[chan].users[user].host
else
local found = false
for k,v in pairs(irc.channels) do
if irc.channels[k].users[user] then
host = irc.channels[k].users[user].host
found = true
break
end
end
if not found then
host = user
end
end
if not noescape then
host = host:gsub("([%.%-%+%*%%%?%(%)%[%]%^%$])","%%%1")
end
local perm, chanPerm, otherPerm = getPerms(usr.host), getPerms(usr.host, channel), getPerms(host, channel)
if otherPerm >= perm and not channel then
if isignore then
return "You cannot ignore "..args[1]
else
return "You cannot modify the permissions for "..args[1]
end
elseif otherPerm >= chanPerm and channel then
if isignore then
return "You cannot ignore "..args[1].." in "..channel
else
return "You cannot modify the permissions for "..args[1].." in "..channel
end
elseif perm < getCommandPerms(isignore and "ignore" or "chmod") and not channel then
if isignore then
return "You cannot ignore people globally"
else
return "You cannot set permission levels globally"
end
elseif chanPerm < getCommandPerms(isignore and "ignore" or "chmod", channel) then
if isignore then
return "You cannot ignore people in "..channel
else
return "You cannot set permission levels in "..channel
end
elseif isignore then
if otherPerm == -1 then
return args[1].." is already ignored"
end
else
if channel and seconds > chanPerm then
return "You can't set permissions that high in "..channel
elseif (not channel and seconds > perm) or seconds > 99 then
return "You can't set permissions that high"
end
end
if channel then
if not channelPermissions[channel] then
channelPermissions[channel] = {}
end
if isignore and seconds then
local oldlevel = channelPermissions[channel][host]
addTimer(function() channelPermissions[channel][host] = oldlevel end, seconds, chan, usr.nick)
end
channelPermissions[channel][host] = isignore and -1 or seconds
if isignore then
return "ignored "..host.." in "..channel..(seconds and " for "..seconds.." second"..(seconds==1 and "" or "s") or "")
else
return "permissions for "..host.." in "..channel.." changed to "..seconds
end
else
if isignore and seconds then
local oldlevel = permissions[host]
addTimer(function() permissions[host] = oldlevel end, seconds, chan, usr.nick)
end
permissions[host] = isignore and -1 or seconds
if isignore then
return "ignored "..host..(seconds and " for "..seconds.." second"..(seconds==1 and "" or "s") or "")
else
return "permissions for "..host.." changed to "..seconds
end
end
end
local function chmod(usr,chan,msg,args)
return changeLevel(usr, chan, msg, args, false)
end
add_cmd(chmod,"chmod",40,"Sets permission levels on a user, '/chmod [<channel>] [-noescape] <user/host> <level>'",true)
local function ignore(usr,chan,msg,args)
return changeLevel(usr, chan, msg, args, true)
end
add_cmd(ignore,"ignore",40,"Sets a global or channel ignore on a user, '/ignore [<channel>] [-noescape] <user/host> [<seconds>]'",true)
--hostmask
local function getHost(usr,chan,msg,args)
if not args[1] then return usr.host end
local user = getUserFromNick(args[1])
if not user then
return "Invalid User"
end
return user.host
end
add_cmd(getHost,"host",0,"The host for a user, '/host <name>' Use /hostmask for full hostmask",false)
local function getHostmask(usr,chan,msg,args)
if not args[1] then return usr.fullhost end
local user = getUserFromNick(args[1])
if not user then
return "Invalid User"
end
return user.fullhost
end
add_cmd(getHostmask,"hostmask",0,"The hostmask for a user, '/hostmask <name>' Use /host for short host",false)
--username, for nesting
local function getName(usr,chan,msg,args)
return usr.nick
end
add_cmd(getName,"nick",0,"Your nick, '/nick'",false)
--channel name, for nesting
local function getChan(usr,chan,msg,args)
return chan
end
add_cmd(getChan,"chan",0,"The current channel, '/chan'",false)
--LUA full access
local function lua2(usr,chan,msg,args)
local e,err = loadstring(msg, "..")
if e then
debug.sethook(infhook,"l")
local s,r = pcall(e)
debug.sethook()
stepcount=0
if s then
local str = tostring(r)
return str:gsub("[\r\n]"," ")
else
return "ERROR: " .. r
end
return
end
return "ERROR: " .. err
end
add_cmd(lua2,"..",101,"Runs full lua code, '/lua <code>'",false)
--HELP
local function help(usr,chan,msg)
msg = msg or "help"
msg = msg:lower()
if commands[msg] then
if commands[msg].helptext then
return msg ..": ".. commands[msg].helptext
end
end
return "No help for "..msg.." found!"
end
add_cmd(help,"help",0,"Returns hopefully helpful information, '/help <cmd>'",true)
--UNHELP, no idea
local function unhelp(usr,chan,msg)
msg = msg or "unhelp"
msg = msg:lower()
if commands[msg] then
if commands[msg].helptext then
return msg ..": ".. string.reverse(commands[msg].helptext)
end
end
if msg==string.reverse(usr.nick) then
ircSendChatQ(usr.nick,"1 point gained")
end
return "No help for "..msg.." found!"
end
add_cmd(unhelp,"unhelp",0,"'>dmc< plehnu/' ,noitamrofni lufplehnu yllufepoh snruteR",true)
--TIMER
local function timer(usr,chan,msg,args)
if #timers > 10 then
return "Error: too many timers already"
end
local num = tonumber(args[1])
local perms = getPerms(usr.host, chan)
if num and num==num and (num<108000 or perms >= 101) and args[2] then
local t={}
for i=2,#args do
table.insert(t,args[i])
end
local pstring, seconds = table.concat(t," "), tonumber(args[1])
addTimer(ircSendChatQ[chan][pstring],seconds,chan,usr.nick)
return "Timer will go off in "..seconds.." second"..(seconds ~= 1 and "s" or "")
else
return "Bad timer"
end
end
add_cmd(timer,"timer",0,"Time until a print is done, '/timer <time(seconds)> <text>'",true)
--BUG, report something to me in a file
local function rbug(usr,chan,msg,args)
if not msg then error("No msg") end
local f = io.open("bug.txt","a")
f:write("["..os.date().."] ".. usr.host..": "..msg.."\r\n")
f:close()
return "Reported bug"
end
add_cmd(rbug,"bug",0,"Report something to "..config.owner.nick..", '/bug <msg>'",true)
--SEEN, display last message by a user
local function seen(usr,chan,msg,args)
if not args[1] then return commands["seen"].helptext end
local nick = args[1]
if args[1]:sub(1,1) == "#" then
if not args[2] then return commands["seen"].helptext end
chan, nick = args[1], args[2]
end
if not irc.channels[chan] then
return "not a channel: "..chan
elseif not irc.channels[chan].users[nick] or not irc.channels[chan].users[nick].lastSaid then
return "I have not seen "..nick
end
local sssss = function(moo) return moo == 1 and "" or "s" end
local difference = os.difftime(os.time(), irc.channels[chan].users[nick].lastSaid.time) or 0
local time = os.date("!*t", difference)
local msg = time.sec.." second"..sssss(time.sec).." ago"
if time.min ~= 0 or difference > 86400 then msg = time.min.." minute"..sssss(time.min).." and "..msg end
if time.hour ~= 0 or difference > 86400 then msg = time.hour.." hour"..sssss(time.hour)..", "..msg end
time.day = time.day - 1
if time.day ~= 0 then msg = (time.day%7).." day"..sssss(time.day%7)..", "..msg end
if time.day >= 7 then msg = math.floor(time.day/7).." week"..sssss(time.day/7)..", "..msg end
if time.year-1970 ~= 0 then msg = (time.year-1970).." year"..sssss(time.year-1970)..", "..msg end
msg = nick.." was last seen in "..chan.." "..msg..": <"..nick.."> "..irc.channels[chan].users[nick].lastSaid.msg
return msg
end
add_cmd(seen,"seen",0,"Display a last seen message '/seen [<chan>] <nick>'",true)