-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchimera.lua
More file actions
282 lines (229 loc) · 7.47 KB
/
chimera.lua
File metadata and controls
282 lines (229 loc) · 7.47 KB
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
END = "\r\n"
socket = require("socket")
tcpsock = socket.tcp()
bot_path = "" --Path to bot dir. EDIT THIS YO
server = "irc.toribash.com"
nickname = "Chimera"
masterpass = "9001"
masterauth = "1337-BC651D7C.nctv.com"
join_on_connect = "#test2"
startuptime = os.time()
authlist = {}
reaction = {}
target = ""
source = ""
oplist = {}
noisy = 1
color_mode = 1
verbose_mode = 0
hook_list = {}
module_list = {}
module_list.current = nil
module_list['dynamic'] = {}
reaction = nil
reaction = {}
socket_list = {}
alive = true
--[[CTCP Stuff:
There is alot of misnomer here, but I don't feel like fixing yet.
You can pass the ctcp_color function either a single color (sets only foreground)
or two colors (sets fore and back). The func returns the required string to
change the color in an IRC string. The table members are for changing other
things relating to appearance. The only properly-named thing in this table is
ctcp.norm, which represents the ansi char needed for ctcp replies and requests.
]]--
ctcp = {}
function ctcp_color(a,b)
toret = ""
if color_mode == 1 then
toret = string.char(3) .. a
if b ~= nil then toret = toret .. "," .. b end
end
return toret
end
ctcp.norm = string.char(1)
ctcp.plain = string.char(15)
ctcp.bold = string.char(2)
ctcp.underline = string.char(31)
ctcp.reverse = string.char(22)
--Use this function to handle (re)connecting.
function block_till_connect()
result = 0
while(result ~= 1) do
tcpsock:close()
tcpsock = nil
tcpsock = socket.tcp()
tcpsock:settimeout(5)
print("Attempting to connect (retry in 5 seconds)...")
result,err = tcpsock:connect(server, 6667)
result = result or ""
if err then
print("Error> " .. result .. " " .. err)
socket.sleep(5)
end
end
tcpsock:settimeout(0)
--------------------Need this---------------------------
require_mod('corefunc')
require_mod('timing')
require_mod('usertable')
---I want this to load on startup because I'm lazy------
require_mod('chanadmin')
require_mod('advanced')
require_mod('web')
require_mod("tablesave")
--------------------------------------------------------
end
--Takes a complete maskline ( nick!user@hostmask ) and returns only the nick.
function mask_to_nick(hmask)
loc = string.find(hmask,"!",1,true) or 0
return string.sub(hmask,1,loc-1)
end
--Takes a complete maskline ( nick!user@hostmask ) and returns only the mask.
function mask_to_end(hmask)
loc = string.find(hmask,"@",1,true)
return string.sub(hmask,loc + 1)
end
--Pushes out a line of text to the server and add the proper terminators (CRLF)
function push(tosend) tcpsock:send(tosend .. END) end
--Echos out a string to specified channel or nick. If no target specified, defaults to last
--source bot recieved text from.
function echo(apass, sendto)
if string.lower(target) == string.lower(nickname) then newtarget = mask_to_nick(source) else newtarget = target end
sendto = sendto or newtarget
push("PRIVMSG " .. sendto .. " " .. apass)
end
--Add a function hook. See modules for usage.
function add_hook(tohook,tag,cmd)
topush = {target = tohook, name = tag, link = cmd}
table.insert(hook_list, topush)
end
--Removes specified hook(s)
function remove_hook(tag)
for i,v in ipairs(hook_list) do
if v.name == tag then
table.remove(hook_list,i)
end
end
end
--Loads a module. I reccomend using require_mod instead so you don't have a chance
--of doubling up.
function load_module(mname)
local mhold = module_list.current
module_list.current = mname
print("Loading module " .. mname .. "...")
module_list[module_list.current] = {}
module_path = bot_path .. "modules/" .. mname .. ".lua"
loadstr = "dofile(module_path)"
callstate, callerror = pcall(loadstring(loadstr), function () end)
if callstate == false then
print("ERROR: ".. callerror)
echo(ctcp_color(1,4) .. ctcp.underline .. "ERROR:" .. ctcp.plain .. " " .. callerror)
module_list[module_list.current] = nil
else
echo(ctcp_color(12) .. "== Module " .. mname .. " loaded.")
end
module_list.current = mhold
end
function unload_module(mname)
local mhold = module_list.current
module_list.current = mname
print("Unloading module " .. mname .. "...")
if type(module_list[module_list.current]) ~= "table" then
echo(ctcp_color(12) .. "== " .. mname .. " is not a loaded module.")
else
for k,v in pairs(module_list[module_list.current]) do
for i,v in ipairs(reaction) do
if k == v.command then table.remove(reaction,i) end
end
end
nullstr = module_list[module_list.current].namespace .. " = nil"
callstate, callerror = pcall(loadstring(loadstr), function () end)
if callstate == false then
print("ERROR: ".. callerror)
echo(ctcp_color(1,4) .. ctcp.underline .. "ERROR:" .. ctcp.plain .. " " .. callerror)
module_list[module_list.current] = nil
echo(ctcp_color(12) .. "== Module " .. mname .. " partially unloaded.")
else
echo(ctcp_color(12) .. "== Module " .. mname .. " unloaded fully.")
end
module_list[module_list.current] = nil
end
module_list.current = mhold
end
--Loads a module only if it hasn't been loaded yet.
function require_mod(modname)
if module_list[modname] == nil then
load_module(modname)
else
echo(ctcp_color(12) .. "== Module " .. modname .. " already loaded.")
end
end
function allocate_namespace(nam)
module_list[module_list.current].namespace = nam
end
--Pushes a reaction onto the table.
function push_reaction(comd, needauth, argnum, passlft, cmdtyp, exestring, help)
toadd = {}
toadd = {
command = comd,
args = argnum,
passleft = passlft,
cmdtype = cmdtyp,
link = exestring,
auth = needauth
}
help = help or "No help available for this command."
table.insert(reaction,toadd)
module_list[module_list.current][comd] = help
end
--Main logic loop here
function run_logic()
while(alive==true) do
run_bot()
handle_timing()
for i,v in ipairs(hook_list) do if v.target == "cycle" then v.link() end end --hook caller
end
end
--I will make this stuff useful later. For now, it is not worth
--looking at.
chat = {}
chat[1] = { trigger = nickname, place = "any", reply = "hmm?"}
chat[2] = { trigger = "o/" ,place = "start", reply = "o/*\\o"}
--Process all the IRC stuff.
function run_bot()
--Grab a line
inlin,err = tcpsock:receive('*l')
--If content exists, lets process it.
if inlin ~= nil then
--Logs the bot in, will make less hackish later.
if string.find(inlin, "*** Found your hostname") then
push("USER " .. nickname .. " 8 * :" .. nickname)
push("NICK " .. nickname)
print("==Logged in==")
end
--Chop line up into components.
chop = core.slice(inlin)
if verbose_mode == 1 then print(inlin) end
--Will relocate this later. Keeps bot alive on server.
if chop.source == "PING" then
push("PONG " .. chop.text)
end
if string.sub(chop.text,1,1) == "!" then
core.handle_cmd(chop.text, chop.target, chop.source)
else
core.parsechat(chop.text, chop.target, chop.source)
end
for i,v in ipairs(hook_list) do if v.target == "parse_raw" then v.link(chop) end end --hook caller
else
--Otherwise our connection got dumped. If this is so, start handling it.
if err == 'closed' then
print('====DISCONNECTED====')
block_till_connect()
end
end
inlin = nil
end
--Bootstrap stuff.
block_till_connect()
run_logic()