Advertisement
TheRockettek

Untitled

May 27th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.81 KB | None | 0 0
  1. -- KrapAPI
  2.  
  3. ChatBox = peripheral.find("chat_box")
  4.  
  5. KrapApi = {}
  6.  
  7. KrapApi.Blacklist = {}
  8.  
  9. function KrapApi.Blacklist.apply()
  10. KrapApi.Blacklist.file.write(KrapApi.Blacklist.blacklist)
  11. KrapApi.Blacklist.file.flush()
  12. end
  13. function KrapApi.Blacklist.deny(_sUser)
  14. KrapApi.Blacklist.blacklist[_sUser] = true
  15. if KrapApi.Blacklist.blacklist[_sUser] then return true else return false end
  16. KrapApi.Blacklist.apply()
  17. end
  18. function KrapApi.Blacklist.allow(_sUser)
  19. KrapApi.Blacklist.blacklist[_sUser] = false
  20. if KrapApi.Blacklist.blacklist[_sUser] then return false else return true end
  21. KrapApi.Blacklist.apply()
  22. end
  23. function KrapApi.Blacklist.isBlacklisted(_sUser)
  24. if KrapApi.Blacklist.blacklist[_sUser] then return true else return false end
  25. end
  26.  
  27. KrapApi.Chat = {}
  28.  
  29. function KrapApi.Chat.raw(_sTarget,_sText)
  30. return KrapApi.say(_sText,KrapBot.prefix)
  31. end
  32.  
  33. function KrapApi.Chat.say(_sText)
  34. return KrapApi.say(_sText,KrapBot.prefix)
  35. end
  36.  
  37. function KrapApi.Chat.tell(_sTarget,_sText)
  38. return KrapApi.say(_sText,KrapBot.prefix)
  39. end
  40.  
  41. function KrapApi.Chat.read()
  42. while true do
  43. local _sEvent,_sUser,_sMessage = os.pullEvent("chat")
  44. if not KrapApi.Blacklist.isBlacklisted(_sUser) then
  45. return _sMessage,_sUser
  46. end
  47. end
  48. end
  49.  
  50. function KrapApi.Chat.readCmd()
  51. while true do
  52. local _sEvent,_sUser,_sMessage = os.pullEvent("command")
  53. if (_sEvent == "command") and (not KrapApi.Blacklist.isBlacklisted(_sUser)) then
  54. return _sMessage,_sUser
  55. elseif (_sEvent == "command") and (_sMessage:sub(1,#KrapBot.commandPrefix) == KrapBot.commandPrefix) and (not KrapApi.Blacklist.isBlacklisted(_sUser)) then
  56. return _sMessage:sub(#KrapBot.commandPrefix+1,#_sMessage),_sUser
  57. end
  58. end
  59. end
  60.  
  61. function KrapApi.Chat.setLabel(_sLabel)
  62. if _sLabel then
  63. KrapBot.prefix = tostring(_sLabel)
  64. return true
  65. else
  66. return false
  67. end
  68. end
  69.  
  70. function KrapApi.Chat.getLabel()
  71. return KrapBot.prefix
  72. end
  73.  
  74. KrapApi.Core = {}
  75.  
  76. function KrapApi.Core.loadPlugin(_sFile)
  77. local _sPath = KrapApi.root .. "plugins/" .. _sFile
  78. local sName = fs.getName( _sPath )
  79. local tEnv = {}
  80. setmetatable( tEnv, { __index = _G } )
  81. local fnAPI, err = loadfile( _sPath, tEnv )
  82. if fnAPI then
  83. local ok, err = pcall( fnAPI )
  84. if not ok then
  85. return false
  86. end
  87. else
  88. return false end
  89. local tAPI = {}
  90. for k,v in pairs( tEnv ) do
  91. if k ~= "_ENV" then
  92. tAPI[k] = v
  93. end
  94. end
  95. KrapBot.loadedPlugins[sName] = tAPI
  96. return true
  97. end
  98.  
  99. function KrapApi.Core.version()
  100. return 1
  101. end
  102. KrapApi.Permissions = {}
  103.  
  104. function KrapApi.Permissions.reload()
  105. local fFile = fs.open(KrapBot.root .. "Permissions.tbl","r")
  106. KrapBot.permissions = textutils.unserialise(fFile.readAll())
  107. if type(KrapBot.permissions) == "table" then return true else return false end
  108. end
  109.  
  110. function KrapApi.Permissions.flush()
  111. local fFile = fs.open(KrapBot.root .. "Permissions.tbl","w")
  112. fFile.write(textutils.serialize(KrapBot.permissions))
  113. fFile.flush()
  114. end
  115.  
  116. function KrapApi.Permissions.check()
  117. local tPermissions = KrapBot.permissions
  118. if not tPermissions then
  119. tPermissions = {}
  120. end
  121. if not tPermissions.users then
  122. tPermissions.users = {}
  123. end
  124. if not tPermissions.groups then
  125. tPermissions.groups = {}
  126. end
  127. if not tPermissions.settings then
  128. tPermissions.settings = {}
  129. end
  130. if not tPermissions.settings.defaultGroup then
  131. tPermissions.settings.defaultGroup = "default"
  132. end
  133. if not tPermissions.groups.default then
  134. tPermissions.groups.default = {}
  135. end
  136. for i,k in pairs(tPermissions.users) do
  137. if not k.group then
  138. k.group = tPermissions.settings.defaultGroup
  139. end
  140. if not k.permissions then
  141. k.permissions = {}
  142. end
  143. end
  144. for i,k in pairs(tPermissions.groups) do
  145. if not k.permissions then
  146. k.permissions = {}
  147. end
  148. if not k.name then
  149. k.name = i
  150. end
  151. end
  152. if tPermissions ~= KrapBot.permissions then
  153. KrapBot.permissions = tPermissions
  154. KrapApi.Permissions.flush()
  155. end
  156. end
  157.  
  158. KrapApi.Permissions.User = {}
  159.  
  160. function KrapApi.Permissions.User.list()
  161. local fUsers = {}
  162. for i,k in pairs(KrapBot.permissions.users) do
  163. table.insert(fUsers,i)
  164. end
  165. return fUsers
  166. end
  167.  
  168. function KrapApi.Permissions.User.add(_sUser)
  169. if (KrapBot.permissions.users[_sUser]) then
  170. return false
  171. end
  172. KrapBot.permissions.users[_sUser] = {['group'] = KrapBot.permissions.settings.defaultGroup,['permissions'] = {}}
  173. KrapApi.Permissions.flush()
  174. return true
  175. end
  176.  
  177. function KrapApi.Permissions.User.remove(_sUser)
  178. if (not KrapBot.permissions.users[_sUser]) then
  179. return false
  180. end
  181. KrapBot.permissions.users[_sUser] = nil
  182. KrapApi.Permissions.flush()
  183. return true
  184. end
  185.  
  186. function KrapApi.Permissions.User.setPrefix(_sUser,_sNewPrefix)
  187. if (not KrapBot.permissions.users[_sUser]) then
  188. return false
  189. end
  190. KrapBot.permissions.users[_sUser]['prefix'] = _sNewPrefix
  191. KrapApi.Permissions.flush()
  192. return true
  193. end
  194.  
  195. function KrapApi.Permissions.User.setSuffix(_sUser,_sNewSuffix)
  196. if (not KrapBot.permissions.users[_sUser]) then
  197. return false
  198. end
  199. KrapBot.permissions.users[_sUser]['suffix'] = _sNewSuffix
  200. KrapApi.Permissions.flush()
  201. return true
  202. end
  203.  
  204. function KrapApi.Permissions.User.getPrefix(_sUser)
  205. return KrapBot.permissions.users[_sUser]['prefix']
  206. end
  207.  
  208. function KrapApi.Permissions.User.getSuffix(_sUser)
  209. return KrapBot.permissions.users[_sUser]['suffix']
  210. end
  211.  
  212. function KrapApi.Permissions.User.resetPrefix(_sUser)
  213. if (not KrapBot.permissions.users[_sUser]) then
  214. return false
  215. end
  216. KrapBot.permissions.users[_sUser]['prefix'] = nil
  217. KrapApi.Permissions.flush()
  218. return true
  219. end
  220.  
  221. function KrapApi.Permissions.User.resetSuffix(_sUser)
  222. if (not KrapBot.permissions.users[_sUser]) then
  223. return false
  224. end
  225. KrapBot.permissions.users[_sUser]['suffix'] = nil
  226. KrapApi.Permissions.flush()
  227. return true
  228. end
  229.  
  230. function KrapApi.Permissions.User.check(_sUser,_sPermission)
  231. if (not KrapBot.permissions.users[_sUser]) then
  232. return false
  233. end
  234. if (KrapBot.permissions.users[_sUser]['permissions'][_sPermission]) then
  235. return true
  236. else
  237. return false
  238. end
  239. end
  240.  
  241. function KrapApi.Permissions.User.addPermission(_sUser,_sPermission)
  242. if (not KrapBot.permissions.users[_sUser]) then
  243. return false
  244. end
  245. KrapBot.permissions.users[_sUser]['permissions'][_sPermission] = true
  246. KrapApi.Permissions.flush()
  247. return true
  248. end
  249. function KrapApi.Permissions.User.removePermission(_sUser,_sPermission)
  250. if (not KrapBot.permissions.users[_sUser]) or (not KrapBot.permissions.users[_sUser]['permissions'][_sPermission]) then
  251. return false
  252. end
  253. KrapBot.permissions.users[_sUser]['permissions'][_sPermission] = nil
  254. KrapApi.Permissions.flush()
  255. return true
  256. end
  257.  
  258. KrapApi.Permissions.Group = {}
  259.  
  260. function KrapApi.Permissions.Group.setDefaultGroup(_sTarget)
  261. KrapBot.permissions.settings.defaultGroup = _sTarget
  262. KrapApi.Permissions.flush()
  263. return true
  264. end
  265.  
  266. function KrapApi.Permissions.Group.list()
  267. local fGroups = {}
  268. for i,k in pairs(KrapBot.permissions.groups) do
  269. table.insert(fGroups,i)
  270. end
  271. KrapApi.Permissions.flush()
  272. return fGroups
  273. end
  274.  
  275. function KrapApi.Permissions.Group.createGroup(_sName)
  276. if (KrapBot.permissions.groups[_sName]) then
  277. return false
  278. end
  279. KrapBot.permissions.groups[_sName] = {['permissions'] = {},['name'] = _sName}
  280. return true
  281. end
  282.  
  283. function KrapApi.Permissions.Group.removeGroup(_sName)
  284. if (not KrapBot.permissions.groups[_sName]) then
  285. return false
  286. end
  287. KrapBot.permissions.groups[_sName] = nil
  288. KrapApi.Permissions.flush()
  289. return true
  290. end
  291.  
  292. function KrapApi.Permissions.Group.addUser(_sTarget,_sGroup)
  293. if (not KrapBot.permissions.groups[_sGroup]) or (not KrapBot.permissions.users[_sTarget]) or (KrapBot.permissions.users[_sTarget]['group']) then
  294. return false
  295. end
  296. KrapBot.permissions.users[_sTarget]['group'] = _sGroup
  297. KrapApi.Permissions.flush()
  298. return true
  299. end
  300.  
  301. function KrapApi.Permissions.Group.removeUser(_sTarget,_sGroup)
  302. if (not KrapBot.permissions.users[_sTarget]) then
  303. return false
  304. end
  305. KrapBot.permissions.users[_sTarget]['group'] = nil
  306. KrapApi.Permissions.flush()
  307. return true
  308. end
  309.  
  310. function KrapApi.Permissions.Group.setPrefix(_sGroup,_sNewPrefix)
  311. if (not KrapBot.permissions.groups[_sGroup]) then
  312. return false
  313. end
  314. KrapBot.permissions.groups[_sGroup]['prefix'] = _sNewPrefix
  315. KrapApi.Permissions.flush()
  316. return true
  317. end
  318.  
  319. function KrapApi.Permissions.Group.setSuffix(_sGroup,_sNewSuffix)
  320. if (not KrapBot.permissions.groups[_sGroup]) then
  321. return true
  322. end
  323. KrapBot.permissions.groups[_sGroup]['suffix'] = _sNewSuffix
  324. KrapApi.Permissions.flush()
  325. return true
  326. end
  327.  
  328. function KrapApi.Permissions.Group.getPrefix(_sGroup)
  329. return KrapBot.permissions.groups[_sGroup]['prefix']
  330. end
  331.  
  332. function KrapApi.Permissions.Group.getSuffix(_sGroup)
  333. return KrapBot.permissions.groups[_sGroup]['suffix']
  334. end
  335.  
  336. function KrapApi.Permissions.Group.resetPrefix(_sGroup)
  337. if (not KrapBot.permissions.groups[_sGroup]) then
  338. return false
  339. end
  340. KrapBot.permissions.grou[_sGroup]['prefix'] = nil
  341. KrapApi.Permissions.flush()
  342. return true
  343. end
  344.  
  345. function KrapApi.Permissions.Group.resetSuffix(_sGroup)
  346. if (not KrapBot.permissions.groups[_sGroup]) then
  347. return false
  348. end
  349. KrapBot.permissions.groups[_sGroup]['suffix'] = nil
  350. KrapApi.Permissions.flush()
  351. return true
  352. end
  353.  
  354. function KrapApi.Permissions.Group.addPermission(_sGroup,_sPermissions)
  355. if (not KrapBot.permissions.groups[_sGroup]) then
  356. return false
  357. end
  358. KrapBot.permissions.groups[_sGroup]['permissions'][_sPermission] = true
  359. KrapApi.Permissions.flush()
  360. return true
  361. end
  362.  
  363. function KrapApi.Permissions.Group.removePermission(_sGroup,_sPermission)
  364. if (not KrapBot.permissions.groups[_sGroup]) then
  365. return false
  366. end
  367. KrapBot.permissions.groups[_sGroup]['permissions'][_sPermission] = nil
  368. KrapApi.Permissions.flush()
  369. return true
  370. end
  371.  
  372. function KrapApi.Permissions.Group.check(_sGroup,_sPermission)
  373. if (not KrapBot.permissions.groups[_sGroup]) then
  374. return false
  375. end
  376. if KrapBot.permissions.groups[_sGroup]['permissions'][_sPermission] then
  377. return true
  378. else
  379. return false
  380. end
  381. end
  382.  
  383. -- Main
  384.  
  385. function KrapApi.say(_sText,_sPrefix)
  386. return ChatBox.say(_sText,_sPrefix)
  387. end
  388.  
  389.  
  390.  
  391. -- Program
  392.  
  393. KrapBot = {}
  394. KrapBot.command = peripheral.find("command")
  395. KrapBot.prefix = "&7<&dKrapBot&7> &r"
  396. KrapBot.commandPrefix = "!"
  397. KrapBot.root = "/KrapBot/"
  398. KrapBot.loadedPlugins = {}
  399. KrapBot.disabledPlugins = {}
  400. KrapBot.permissions = {}
  401.  
  402. if not fs.exists(KrapBot.root) then
  403. fs.makeDir(KrapBot.root)
  404. fs.makeDir(KrapBot.root .. "plugins/")
  405. local _fFile = fs.open(KrapBot.root .. "Blacklist.tbl","a")
  406. _fFile.write(textutils.serialize({}))
  407. _fFile.close()
  408. local _fFile = fs.open(KrapBot.root .. "Permissions.tbl","a")
  409. _fFile.write("{}")
  410. _fFile.close()
  411. end
  412.  
  413. KrapApi.Blacklist.file = fs.open(KrapBot.root .. "Blacklist.tbl","w")
  414. KrapApi.Blacklist.blacklist = textutils.serialize(fs.open(KrapBot.root .. "Blacklist.tbl","r").readAll())
  415.  
  416. KrapApi.Permissions.reload()
  417. KrapApi.Permissions.check()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement