PeachGaming

Untitled

Mar 17th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.58 KB | None | 0 0
  1. --Args
  2. args = {...}
  3. --[[
  4. optional arguments
  5. 1: username
  6. 2: channel (default will be 10)
  7. 3: autoupdate (false to disable)
  8. ]]--
  9. --changeable
  10. HistoryNum = 200 --how many lines of history to keep
  11. if args[3] and args[3] == "false" then
  12. autoupdate = false
  13. else
  14. autoupdate = true -- turn on or off auto update
  15. end
  16. --Non changeable!
  17. chat = {}
  18. lineCount = 1
  19. modifier = 0
  20. X, Y = term.getSize()
  21. Version = 1.51
  22. BottomText = "Message: "
  23. KeyCount = 0
  24. user = ""
  25. Header = "Welcome to darkchat "..Version.."!"
  26. function findPeripheral(Perihp)
  27. for _,s in ipairs(rs.getSides()) do
  28. if peripheral.isPresent(s) and peripheral.getType(s) == Perihp then
  29. return s
  30. end
  31. end
  32. return false
  33. end
  34. function gitUpdate(ProgramName, Filename, ProgramVersion)
  35. if http then
  36. status, getGit = pcall(http.get, "https://raw.github.com/darkrising/darkprograms/darkprograms/programVersions")
  37. if not status then
  38. return(getGit)
  39. end
  40. local getGit = getGit.readAll()
  41. NVersion = textutils.unserialize(getGit)
  42. if NVersion[ProgramName].Version > ProgramVersion then
  43. getGit = http.get(NVersion[ProgramName].GitURL)
  44. getGit = getGit.readAll()
  45. local file = fs.open(Filename, "w")
  46. file.write(getGit)
  47. file.close()
  48. return true
  49. end
  50. else
  51. return false
  52. end
  53. end
  54. function split(str, pattern) -- Splits string by pattern, returns table
  55. local t = { }
  56. local fpat = "(.-)" .. pattern
  57. local last_end = 1
  58. local s, e, cap = str:find(fpat, 1)
  59. while s do
  60. if s ~= 1 or cap ~= "" then
  61. table.insert(t,cap)
  62. end
  63. last_end = e+1
  64. s, e, cap = str:find(fpat, last_end)
  65. end
  66. if last_end <= #str then
  67. cap = str:sub(last_end)
  68. table.insert(t, cap)
  69. end
  70. return t
  71. end
  72. function cWrite(Text, bgcolor, tecolor, solid)
  73. if term.isColor() == true then
  74. term.setBackgroundColor(colors[bgcolor])
  75. term.setTextColor(colors[tecolor])
  76. if solid then
  77. term.write(string.rep(" ", #Text))
  78. else
  79. term.write(Text)
  80. end
  81. else
  82. term.write(Text)
  83. end
  84. end
  85. function bgReset()
  86. if term.isColor() == true then
  87. term.setTextColor(colors.white)
  88. term.setBackgroundColor(colors.black)
  89. end
  90. end
  91. function rSend(message, special, hidden)
  92. local messaget = {}
  93. messaget.user = user
  94. messaget.message = message
  95. messaget.darkchat = true
  96. messaget.id = os.getComputerID()
  97. if special then messaget.special = true end
  98. messaget = textutils.serialize(messaget)
  99. modem.transmit(channelN,channelN,messaget)
  100. if not hidden then
  101. if special then
  102. os.queueEvent("tableInsert", "yellow#* "..user.." "..message)
  103. else
  104. os.queueEvent("tableInsert", "white#"..user..": "..message)
  105. end
  106. end
  107. end
  108. function rReceive()
  109. local darkchat = false
  110. repeat
  111. _,_,C,rC,M,D = os.pullEvent("modem_message")
  112. M = textutils.unserialize(M)
  113. if type(M) == "table" and M.darkchat then
  114. darkchat = true
  115. elseif type(M) == "table" and M.list then
  116. local list = {}
  117. list.user = user
  118. list.reply = true
  119. list = textutils.serialize(list)
  120. modem.transmit(channelN,channelN,list)
  121. elseif type(M) == "table" and M.reply then
  122. os.queueEvent("user",M.user)
  123. end
  124. until darkchat == true
  125. return M
  126. end
  127. function modiferMod(operation, amount) -- if operation is true, number will be added else subtacted
  128. if operation == true then
  129. if lineCount - modifier > 1 then
  130. modifier = modifier + 1
  131. end
  132. else
  133. if modifier > 0 then
  134. modifier = modifier - 1
  135. end
  136. end
  137. end
  138. function clears()
  139. for i = 1, Y - 2 do
  140. term.setCursorPos(1,i)
  141. term.write(string.rep(" ", X))
  142. end
  143. end
  144. function getChat()
  145. draw()
  146. while true do
  147. m = rReceive()
  148. if m.id ~= os.getComputerID() and m.user ~= user and m.message then
  149. if m.special then
  150. os.queueEvent("tableInsert", "yellow#* ".. m.user .. " " .. m.message)
  151. else
  152. os.queueEvent("tableInsert", "white#" .. m.user ..": ".. m.message)
  153. end
  154. end
  155. end
  156. end
  157. function draw()
  158. bgReset() clears()
  159. if #chat > HistoryNum then table.remove(chat, 1) end
  160. if #chat > Y - 2 then
  161. lineCount = ((#chat) - (Y - 3))
  162. end
  163. for i = lineCount - modifier, #chat - modifier do
  164. chatN = split(chat[i], "#")
  165. term.setCursorPos(1,i - lineCount+1+modifier)
  166. cWrite(chatN[2],"black",chatN[1])
  167. end
  168. term.setCursorPos(1, Y - 1)
  169. cWrite(string.rep("-", X), "blue","blue", true)
  170. term.setTextColor(colors.white)
  171. local Text = "<User: "..user.."> <Channel: "..channelN..">"
  172. term.setCursorPos(X/2 - #Text/2, Y - 1)
  173. write(Text)
  174. if modifier > 0 and term.isColor() == true then
  175. paintutils.drawPixel(1, Y - 1, colors.yellow)
  176. paintutils.drawPixel(X, Y - 1, colors.yellow)
  177. end
  178. bgReset()
  179. term.setCursorPos(#BottomText + KeyCount + 1, Y)
  180. return true
  181. end
  182. function sendChat()
  183. while true do
  184. term.setCursorPos(1, Y)
  185. term.write(BottomText)
  186. message = read()
  187. if string.sub(message,1,1) == "/" then
  188. commandS = string.sub(message,2,#message)
  189. commandS = split(commandS, " ")
  190. if comD[commandS[1]] then
  191. comD[commandS[1]].run(commandS)
  192. else
  193. os.queueEvent("tableInsert", "red#Unknown Command")
  194. end
  195. elseif message == "" then
  196. draw()
  197. elseif message then
  198. rSend(message)
  199. draw()
  200. end
  201. end
  202. end
  203. function keyListen()
  204. while true do
  205. Type, KEY = os.pullEvent("key")
  206. if KEY == 28 then
  207. KeyCount = 0
  208. elseif KEY == 14 then
  209. if KeyCount ~= 0 then
  210. KeyCount = KeyCount - 1
  211. end
  212. elseif KEY then
  213. if KEY ~= 54 then
  214. KeyCount = KeyCount + 1
  215. end
  216. end
  217. end
  218. end
  219. function startUp()
  220. term.clear() term.setCursorPos(1,1)
  221. if autoupdate == true then
  222. print("Checking for updates...")
  223. local updateStatus = gitUpdate("chat", shell.getRunningProgram(), Version)
  224. if type(updateStatus) == "string" then
  225. print("Cannot check for updates:")
  226. print(updateStatus)
  227. sleep(1)
  228. elseif updateStatus == true then
  229. print("Downloaded new version, restarting...")
  230. sleep(1.5)
  231. os.reboot()
  232. else
  233. print("You're running the latest version")
  234. sleep(1)
  235. end
  236. end
  237. Side = findPeripheral("modem")
  238. if not Side then
  239. print("no modem.")
  240. return exit
  241. end
  242. modem = peripheral.wrap(Side)
  243. term.clear() term.setCursorPos(1,1)
  244. cWrite(string.rep("-", X), "blue","blue", true) bgReset()
  245. term.setCursorPos(X/2 - string.len(Header)/2,2)
  246. print(Header)
  247. cWrite(string.rep("-", X), "blue","blue", true) bgReset()
  248. end
  249. function privateMode()
  250. user = config.user
  251. channelN = config.channel
  252. end
  253. function publicMode()
  254. repeat
  255. write("Nickname: ")
  256. user = read()
  257. if user == "" then
  258. print("\nInvalid Name")
  259. end
  260. until user ~= ""
  261. print("\nDefault channel is 10")
  262. write("Channel: ")
  263. channelN = read()
  264. if not tonumber(channelN) then
  265. channelN = "10"
  266. end
  267. end
  268. function getOnlineList()
  269. while true do
  270. os.pullEvent("grabList")
  271. local com = {}
  272. local Users = ""
  273. com.list = true
  274. com = textutils.serialize(com)
  275. modem.transmit(channelN,channelN,com)
  276. os.startTimer(2)
  277. repeat
  278. local e, u = os.pullEvent()
  279. if e == "user" then
  280. Users = Users..u..", "
  281. end
  282. until e == "timer"
  283. Users = string.sub(Users, 1, #Users - 2) -- remove extra comma
  284. os.queueEvent("tableInsert", "yellow#Users in channel: "..Users)
  285. end
  286. end
  287. function scrollingEventListen()
  288. while true do
  289. local _,direction = os.pullEvent("mouse_scroll")
  290. if direction == -1 then
  291. modiferMod(true, 1)
  292. draw()
  293. else
  294. modiferMod(false, 1)
  295. draw()
  296. end
  297. end
  298. end
  299. function tableEventListen()
  300. while true do
  301. local e, mess = os.pullEvent("tableInsert")
  302. if modifier > 0 and HistoryNum ~= #chat then
  303. modifier = modifier + 1
  304. end
  305. table.insert(chat, mess)
  306. draw()
  307. end
  308. end
  309. function exitEventListen()
  310. while true do
  311. os.pullEvent("exit")
  312. term.clear()
  313. term.setCursorPos(1,1)
  314. modem.closeAll()
  315. return exit
  316. end
  317. end
  318. function helpGen()
  319. os.queueEvent("tableInsert", "lime#".. string.rep("-", X / 2 - 4) .. " help " .. string.rep("-", X / 2 - 3))
  320. for i, v in pairs(comD) do
  321. if v.help then
  322. os.queueEvent("tableInsert", "lime#/"..i.." ".. v.help)
  323. end
  324. end
  325. os.queueEvent("tableInsert", "lime#".. string.rep("-", X))
  326. end
  327. comD = {
  328. ["exit"] = {
  329. run = function()
  330. rSend("has quit.", true)
  331. os.queueEvent("exit")
  332. end,
  333. help = ""
  334. },
  335. ["quit"] = {
  336. run = function()
  337. rSend("has quit.", true)
  338. os.queueEvent("exit")
  339. end,
  340. help = ""
  341. },
  342. ["me"] = {
  343. run = function(Words)
  344. local StringRe = ""
  345. for i = 2, #Words do
  346. StringRe = StringRe..Words[i].." "
  347. end
  348. rSend(StringRe, true)
  349. end,
  350. help = "<text>"
  351. },
  352. ["channel"] = {
  353. run = function(Channels)
  354. local SetChan = Channels[2]
  355. if not tonumber(SetChan) then
  356. os.queueEvent("tableInsert", "red#Error: Must be a number.")
  357. else
  358. rSend("has changed channel.", true)
  359. channelN = tonumber(SetChan)
  360. modem.closeAll()
  361. modem.open(channelN)
  362. rSend("has entered the channel!", true, true)
  363. end
  364. end,
  365. help = "<number>"
  366. },
  367. ["list"] = {
  368. run = function()
  369. os.queueEvent("grabList")
  370. os.queueEvent("tableInsert", "lime#Gathering list...")
  371. end,
  372. help = ""
  373. },
  374. ["clear"] = {
  375. run = function()
  376. chat = {}
  377. lineCount = 1
  378. modifier = 0
  379. draw()
  380. end,
  381. help = ""
  382. },
  383. ["help"] = {
  384. run = helpGen,
  385. help = ""
  386. },
  387. ["nick"] = {
  388. run = function(Nickname)
  389. rSend("is now known as "..Nickname[2], true)
  390. user = Nickname[2]
  391. if config.private == true then
  392. config.user = Nickname[2]
  393. local F = fs.open(".darkChatConf", "w")
  394. local configString = textutils.serialize(config)
  395. F.write(configString)
  396. F.close()
  397. end
  398. draw()
  399. end,
  400. help = "<nickname>"
  401. },
  402. }
  403. --[[
  404. 28: enter
  405. 14: backspace
  406. ]]--
  407. term.clear() term.setCursorPos(1,1)
  408. if (fs.exists(".darkChatConf") == true) and (#args == 0) then
  409. F = fs.open(".darkChatConf", "r")
  410. Data = F.readAll()
  411. F.close()
  412. config = textutils.unserialize(Data)
  413. elseif #args == 0 then
  414. config = {}
  415. repeat
  416. print("Do you want your username to be stored? (Private Mode)")
  417. write("y / n : ")
  418. Qprivate = read()
  419. until Qprivate == "y" or Qprivate == "n"
  420. if Qprivate == "y" then
  421. config.private = true
  422. write("\nUsername: ")
  423. config.user = read()
  424. repeat
  425. write("Default channel number: ")
  426. Qchannel = read()
  427. until tonumber(Qchannel)
  428. config.channel = tonumber(Qchannel)
  429. else
  430. config.private = false
  431. end
  432. configString = textutils.serialize(config)
  433. F = fs.open(".darkChatConf", "w")
  434. F.write(configString)
  435. F.close()
  436. print("Setup Complete!")
  437. sleep(1)
  438. end
  439. if args[1] then
  440. config = {}
  441. config.private = true
  442. config.user = args[1]
  443. config.channel = 10
  444. end
  445. if args[2] and tonumber(args[2]) then
  446. config.channel = tonumber(args[2])
  447. end
  448.  
  449. startUp()
  450. if config.private == true then
  451. privateMode()
  452. else
  453. publicMode()
  454. end
  455. channelN = tonumber(channelN)
  456. modem.closeAll()
  457. modem.open(channelN)
  458. term.clear()
  459. term.setCursorPos(1,1)
  460. rSend("has joined the chat!", true, true)
  461. parallel.waitForAny(getChat, sendChat, keyListen, getOnlineList, scrollingEventListen, tableEventListen, exitEventListen)
Add Comment
Please, Sign In to add comment