-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
171 lines (125 loc) · 4.09 KB
/
main.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
local socket = require("socket")
local args = {...}
realTime = function(s)
local secs = math.floor(s%60)
local mins = math.floor(s%3600/60)
local hours = math.floor(s%(3600*24)/3600)
local days = math.floor(s%(3600*24*7)/3600/24)
local weeks = math.floor(s/3600/24/7)
local ans = ''
ans = ans .. (weeks>0 and weeks..' weeks, ' or '')
ans = ans .. (days >0 and days.. ' days, ' or '')
ans = ans .. (hours>0 and hours..' hours, ' or '')
ans = ans .. (mins >0 and mins.. ' minutes, ' or '')
ans = ans .. (secs >0 and secs.. ' seconds, ' or '')
ans = ans:sub(1, -3)
return ans
end
dofile(args[1] or "settings.lua")
dofile 'bot.lua'
event = {}
event.next = function(self) table.remove(self, 1) end
event.push = table.insert
event.clear = function(self) for i in ipairs(self) do self[i] = nil end end
settings.control = settings.control or ('.' .. args[1] .. '.fifo')
local control = io.open(settings.control, 'r')
--[[
remind = {}
remind.add = function(self, osTime, message, target)
if not self[osTime] then
self[osTime] = {}
end
table.insert(self[osTime], {message, target})
end--]]
cron = {}
cron.add = function(time, func)
cron[time] = cron[time] or {}
print(time)
table.insert(cron[time], func)
end
ls = function(path)
local f = io.popen('ls '..path, 'r')
local files = {}
for file in f:lines() do
if not (file:match'.+%.lua$' or file:match '.+%.txt' or file == 'bac') then
table.insert(files, file)
end
end
f:close()
return files
end
bot.print("LoveBot IRC BOT is running!")
load = function()
irc = socket.tcp()
local ok = irc:connect(bot.address,bot.port)
if ok == 1 then
bot.print("Connected to the network!")
else
bot.print("Couldn't connect!")
end
irc:send("NICK "..bot.nick.."\r\n")
irc:send("USER "..bot.uname.." 8 * :"..bot.uname.."\r\n")
irc:send("JOIN "..bot.channel.."\r\n")
if not bot.startup then
dofile("startup.lua")
else
bot.startup()
end
irc:settimeout(0.1)
math.randomseed(os.time())
if control then
os.execute("echo '' > \"" .. settings.control .. "\" &")
else
print("Warning: couldn't locate control fifo. Bot will start without control module.")
end
update()
end
update = function()
local t, ot = 0, 6/0
while true do
t = os.time()
if love and love.event then
love.event.pump()
for e in love.event.poll() do
if e == "quit" then
os.exit()
end
end
end
if event[1] then
success, err = pcall(event[1].func, t, ot, event[1])
if not success then
sendNotice("Error in event: "..err, event[1].sender)
end
event:next()
end
for rt = ot+1, t do
if cron[rt] then
print(rt, "ok")
for i, v in ipairs(cron[rt]) do
v()
end
end
end
if control then
local line = control:read '*l'
if line and line ~= '' then
bot.PRIVMSG:fire("!MASTER!", bot.nick, '!' .. line)
end
end
local line, err = irc:receive("*l")
if line then
process(line)
elseif quit then
irc:close()
return
elseif reload then
irc:close()
reload = nil
bot.print "Reloading sequence initialized."
dofile 'main.lua'
end
ot = t
end
end
load()