Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.69 KB | None | 0 0
  1. local lfs = require("lfs")
  2. local json = require("JSON")
  3. local cqueues = require("cqueues")
  4.  
  5. _G.irc = require("irc")
  6. _G.ulib = assert(loadfile("ulib.lua"))() --useful stuff
  7.  
  8. _G.current_dir = lfs.currentdir() --have this here for later
  9. _G.loaded_modules = {}
  10. _G.commands = {}
  11. _G.callbacks = {}
  12.  
  13. _G.history = {}
  14.  
  15. local loop = cqueues.new()
  16.  
  17. if(ulib.file_exists(current_dir.."/_sconfig.lua")) then
  18. local _sconfig = require("_sconfig")
  19. print("Secret config loaded.")
  20. _G.s_pass = _sconfig.pass
  21. _G.s_ns_pass = _sconfig.nickserv_pass
  22. _G.google_apikey = _sconfig.google_api_key
  23. end
  24.  
  25. local function do_callback(name, ...)
  26. if(type(callbacks[name]) ~= "table") then
  27. return nil
  28. end
  29. for k,v in pairs(callbacks[name]) do
  30. if(type(v) == "function") then
  31. local ok, err = pcall(v, ...)
  32. if not ok then
  33. print("[DEBUG]: Callback "..k.." errored with error \""..err.."\".")
  34. end
  35. else
  36. print(k.." wasn't func, it was "..type(v))
  37. end
  38. end
  39. end
  40.  
  41. function _G.do_ignore(name)
  42. for k,v in pairs(config.ignore_list) do
  43. if string.lower(name) == string.lower(v) then
  44. return true
  45. end
  46. end
  47. local ignore_list = json:decode(ulib.readAll(_G.current_dir.."/ignore_list.json") or "{}") or {}
  48. if(ulib.in_table(ignore_list, string.lower(name))) then
  49. return true
  50. end
  51. return false
  52. end
  53.  
  54. local function module_unloaded(name)
  55. local ul = ulib.readAll(current_dir.."/disable_modules.json") or "{}"
  56. for k,v in pairs((json:decode(ul) or "{}")) do
  57. if string.lower(name) == string.lower(v) then
  58. return true
  59. end
  60. end
  61. return false
  62. end
  63.  
  64.  
  65. function _G.join_channel(channel)
  66. irc.join(channel)
  67. _G.history[channel] = {}
  68. print("Joining channel "..channel.."!")
  69. end
  70.  
  71. function _G.is_auth(name, chan)
  72. return (ulib.in_table(_G.config.authorized_users, string.lower(tostring(name))) or ulib.in_table(chan:allops(), string.lower(tostring(name))))
  73. end
  74.  
  75. irc.register_callback("connect", function()
  76. if(s_ns_pass) then
  77. print("Identifying with NickServ!")
  78. irc.say("NickServ", "IDENTIFY "..s_ns_pass)
  79. end
  80. for k,v in pairs(config.channels) do
  81. join_channel(v)
  82. end
  83. do_callback("connect", {irc = irc, conf = config})
  84. end)
  85.  
  86. irc.register_callback("channel_msg", function(channel, from, message)
  87. print("["..channel.."] "..from..": "..message)
  88. if(type(_G.history[channel]) ~= "table") then
  89. _G.history[channel] = {}
  90. end
  91. table.insert(_G.history[channel], {
  92. user = from,
  93. data = message
  94. })
  95. if(do_ignore(from) == false) then
  96. if(string.sub(message,1,1) == config.command_prefix) then
  97. local message_split = ulib.split(message, " ")
  98. if(type(message_split[1]) == "string") then
  99. local command = string.gsub(message_split[1], config.command_prefix, "", string.len(config.command_prefix))
  100. print("found command: "..command)
  101. if(type(commands[command]) == "function") then
  102. local args = ulib.split(message, " ")
  103. table.remove(args, 1)
  104. local ok, err = pcall(commands[command], {
  105. irc = irc,
  106. conf = config,
  107. from = from,
  108. channel = channel,
  109. message = message,
  110. params = args
  111. })
  112. if not ok then
  113. irc.say(channel, "Command '"..command.."' errored with error: \""..err.."\"")
  114. print("[DEBUG]: Command '"..command.."' errored with error: \""..err.."\"")
  115. end
  116. else
  117. print(command.." wasn't a function.")
  118. end
  119. end
  120. end
  121. do_callback("channel_msg", {
  122. irc = irc,
  123. conf = config,
  124. from = from,
  125. channel = channel,
  126. message = message
  127. })
  128. end
  129. end)
  130.  
  131. irc.register_callback("private_msg", function(from, message)
  132. print(from.." -> Bot: "..message)
  133. if(string.sub(message,1,1) == config.command_prefix) then
  134. local message_split = ulib.split(message, " ")
  135. if(type(message_split[1]) == "string") then
  136. local command = string.gsub(message_split[1], config.command_prefix, "", string.len(config.command_prefix))
  137. print("found command: "..command)
  138. if(type(commands[command]) == "function") then
  139. local args = ulib.split(message, " ")
  140. table.remove(args, 1)
  141. local ok, err = pcall(commands[command], {
  142. irc = irc,
  143. conf = config,
  144. from = from,
  145. channel = from,
  146. message = message,
  147. params = args
  148. })
  149. if not ok then
  150. irc.say(channel, "Command '"..command.."' errored with error: \""..err.."\"")
  151. print("[DEBUG]: Command '"..command.."' errored with error: \""..err.."\"")
  152. end
  153. else
  154. print(command.." wasn't a function.")
  155. end
  156. end
  157. end
  158. if(do_ignore(from) == false) then
  159. do_callback("private_msg", {
  160. irc = irc,
  161. conf = config,
  162. from = from,
  163. message = message
  164. })
  165. end
  166. end)
  167.  
  168. loop:wrap(function()
  169. irc.connect{
  170. network = config.server,
  171. nick = config.nick,
  172. port = config.port,
  173. username = config.username,
  174. realname = config.realname,
  175. pass = s_pass
  176. }
  177. end)
  178.  
  179. --Here we just load all callback registers and external commands
  180. function _G.load_modules()
  181. loaded_modules = {}
  182. for file in lfs.dir( current_dir.."/modules" ) do
  183. if (file ~= "." and file ~= "..") then
  184. local trimmed_name = string.gsub(file, ".lua", "")
  185. if(not module_unloaded(trimmed_name)) then
  186. local module_func = assert(loadfile(current_dir.."/modules/"..file))
  187. loaded_modules[trimmed_name] = module_func()
  188. print("Loaded module '"..trimmed_name.."'! "..current_dir.."/modules/"..file)
  189. else
  190. print("Skipped unloaded module '"..trimmed_name.."'! "..current_dir.."/modules/"..file)
  191. end
  192. end
  193. end
  194. callbacks = {}
  195. commands = {}
  196. for k,v in pairs(loaded_modules) do
  197. if(type(v.callbacks) == "table") then
  198. for a,b in pairs(v.callbacks) do
  199. if(type(callbacks[a]) ~= "table") then
  200. callbacks[a] = {}
  201. end
  202. table.insert(callbacks[a], b)
  203. end
  204. end
  205. for a,b in pairs(v) do
  206. if(type(b) == "function") then
  207. commands[a] = b
  208. end
  209. end
  210. end
  211. end
  212.  
  213. local function load_loops()
  214. for k,v in pairs(loaded_modules) do
  215. if(type(v.loops) == "table") then
  216. for a,b in pairs(v.loops) do
  217. if(type(b) == "function") then
  218. loop:wrap( b )
  219. print(tostring(b))
  220. print(k)
  221. end
  222. end
  223. end
  224. end
  225. end
  226.  
  227. function _G.load_config()
  228. _G.config = nil
  229. _G.config = require("config")
  230. end
  231.  
  232.  
  233. load_modules()
  234. load_loops()
  235. load_config()
  236.  
  237. irc.DEBUG = config.DEBUG
  238.  
  239. while not loop:empty() do
  240. local ok, err = loop:step()
  241.  
  242. if not ok then
  243. error("cqueue: " .. err)
  244. end
  245. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement