Advertisement
Guest User

Untitled

a guest
Jul 24th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. function cprint(...)
  2. local s = "&1"
  3. for k, v in ipairs(arg) do
  4. s = s .. v
  5. end
  6. s = s .. "&0"
  7.  
  8. local fields = {}
  9. local lastcolor, lastpos = "0", 0
  10. for pos, clr in s:gmatch"()&(%x)" do
  11. table.insert(fields, {s:sub(lastpos + 2, pos - 1), lastcolor})
  12. lastcolor, lastpos = clr , pos
  13. end
  14.  
  15. for i = 2, #fields do
  16. if term.isColor() then
  17. term.setTextColor(2 ^ (tonumber(fields[i][2], 16)))
  18. end
  19. io.write(fields[i][1])
  20. end
  21. end
  22.  
  23.  
  24.  
  25.  
  26.  
  27. local key = "0ec2eb25b6166c0c27a394ae118ad829"
  28. local username = "noUsername"
  29. local password = "noPassword"
  30.  
  31. if fs.exists(".userpastebin.developerapikey") then
  32. local h = fs.open(".userpastebin.developerapikey","r")
  33. key = h.readAll()
  34. h.close()
  35. else
  36. local h = fs.open(".userpastebin.developerapikey","w")
  37. h.write(key)
  38. h.close()
  39. end
  40.  
  41. if fs.exists(".userpastebin.username") then
  42. local h = fs.open(".userpastebin.username", "r")
  43. username = h.readAll()
  44. h.close()
  45. end
  46.  
  47. if fs.exists(".userpastebin.password") then
  48. local h = fs.open(".userpastebin.password", "r")
  49. password = h.readAll()
  50. h.close()
  51. end
  52.  
  53. local function printUsage()
  54. cprint( "&1Usages&0:\n" )
  55. cprint( "&1userpastebin &3put &1<&5filename&1>\n" )
  56. cprint( "&1userpastebin &3get &1<&5code&1> <&5filename&1>\n" )
  57. cprint( "&1userpastebin &3run &1<&5code&1> <&5arguments&1>\n" )
  58. cprint( "&1userpastebin &3login &1<&5username&1>\n" )
  59. cprint( "&1userpastebin &3login logout &1\n" )
  60. cprint( "&1userpastebin &3setkey &1<&5key&1>\n" )
  61. cprint( "&1userpastebin &3setkey default\n\n" )
  62. cprint( "&1The Current Key Is&0: &d"..key.."\n\n" )
  63. cprint( "&1userpastebin &3key help\n" )
  64. end
  65.  
  66. local tArgs = { ... }
  67. if #tArgs < 2 then
  68. printUsage()
  69. return
  70. end
  71.  
  72. if not http then
  73. printError( "Pastebin requires http API" )
  74. printError( "Set http_enable to true in ComputerCraft.cfg" )
  75. return
  76. end
  77.  
  78. local function get(paste)
  79. cprint( "&1Connecting to pastebin.com... " )
  80. local response = http.get(
  81. "http://pastebin.com/raw.php?i="..textutils.urlEncode( paste )
  82. )
  83.  
  84. if response then
  85. cprint( "&1Success.\n" )
  86.  
  87. local sResponse = response.readAll()
  88. response.close()
  89. return sResponse
  90. else
  91. printError( "Failed." )
  92. end
  93. end
  94.  
  95. local sCommand = tArgs[1]
  96. if sCommand == "put" then
  97. local sFile = tArgs[2]
  98. local sPath = shell.resolve( sFile )
  99. if not fs.exists( sPath ) or fs.isDir( sPath ) then
  100. cprint( "&1No such file\n" )
  101. return
  102. end
  103.  
  104. local sName = fs.getName( sPath )
  105. local file = fs.open( sPath, "r" )
  106. local sText = file.readAll()
  107. file.close()
  108.  
  109. cprint( "&1Connecting to pastebin.com... \n" )
  110. local skey = "0ec2eb25b6166c0c27a394ae118ad829"
  111. if key ~= skey then
  112. if username == "noUsername" then
  113. cprint("&5Please Enter Your Username: ")
  114. username = read()
  115. cprint("\n&5Password: ")
  116. password = read("*")
  117. cprint("&1\n")
  118. else
  119. cprint("&1Getting User Key...\n")
  120. local skeyr = http.post(
  121. "http://pastebin.com/api/api_login.php",
  122. "api_dev_key="..textutils.urlEncode(key)..
  123. "&api_user_name="..textutils.urlEncode(username)..
  124. "&api_user_password="..textutils.urlEncode(password)
  125. )
  126. skey = skeyr.readAll()
  127. cprint("&1Received User Key...\n")
  128. cprint("&1Username: &5"..username.."&1\n")
  129. end
  130. end
  131. local response = http.post(
  132. "http://pastebin.com/api/api_post.php",
  133. "api_option=paste&"..
  134. "api_dev_key="..key.."&"..
  135. "api_paste_format=lua&"..
  136. "api_paste_name="..textutils.urlEncode(sName).."&"..
  137. "api_paste_code="..textutils.urlEncode(sText).."&"..
  138. "api_user_key="..textutils.urlEncode(skey)
  139. )
  140.  
  141. if response then
  142. cprint( "&1Success.\n" )
  143.  
  144. local sResponse = response.readAll()
  145. response.close()
  146.  
  147. local sCode = string.match( sResponse, "[^/]+$" )
  148. cprint( "&1Uploaded as "..sResponse.."\n" )
  149. cprint( "&1Run \"userpastebin get "..sCode.."\" to download anywhere\n" )
  150.  
  151. else
  152. cprint( "&1Failed.\n" )
  153. end
  154.  
  155. elseif sCommand == "get" then
  156. if #tArgs < 3 then
  157. printUsage()
  158. return
  159. end
  160.  
  161. local sCode = tArgs[2]
  162. local sFile = tArgs[3]
  163. local sPath = shell.resolve( sFile )
  164. if fs.exists( sPath ) then
  165. cprint( "&1File already exists. If you want to replace the existing file, type 'YES'. Otherwise, type 'NO'.\n" )
  166. local replaceb = read()
  167. if replaceb == "NO" then
  168. return
  169. elseif replaceb == "YES" then
  170. cprint( "&1Okay, replacing file...\n" )
  171. else
  172. return
  173. end
  174. end
  175.  
  176. local res = get(sCode)
  177. if res then
  178. local file = fs.open( sPath, "w" )
  179. file.write( res )
  180. file.close()
  181.  
  182. cprint( "&1Downloaded as "..sFile.."\n" )
  183. end
  184. elseif sCommand == "run" then
  185. local sCode = tArgs[2]
  186.  
  187. local res = get(sCode)
  188. if res then
  189. local func, err = loadstring(res)
  190. if not func then
  191. printError( err )
  192. return
  193. end
  194. setfenv(func, getfenv())
  195. local success, msg = pcall(func, unpack(tArgs, 3))
  196. if not success then
  197. printError( msg )
  198. end
  199. end
  200.  
  201. elseif sCommand == "setkey" then
  202. local sCode = tArgs[2]
  203. if sCode == "default" then
  204. sCode = "0ec2eb25b6166c0c27a394ae118ad829"
  205. end
  206. h = fs.open(".userpastebin.developerapikey","w")
  207. h.write(sCode)
  208. h.close()
  209. h = fs.open(".userpastebin.developerapikey","r")
  210. if h.readAll() == sCode then
  211. cprint("&1Key Updated Successfully&0.&1 New Key&0:\n&d"..sCode.."\n")
  212. else
  213. cprint("&eFailed&0!")
  214. end
  215. h.close()
  216. elseif sCommand == "key" then
  217. local sCode = tArgs[2]
  218. if sCode == "help" then
  219. cprint("\n&1Using an API key will let you upload right to your account&0.&1 Go to &3pastebin.com/api &1after logging in to find your key&0.\n")
  220. end
  221.  
  222. elseif sCommand == "login" then
  223. local sName = tArgs[2]
  224. if sName == "logout" then
  225. if username == "noUsername" then
  226. cprint("&1You are not logged in!\n")
  227. return
  228. else
  229. username = "noUsername"
  230. password = "noPassword"
  231. local h = fs.open(".userpastebin.username", "w")
  232. h.write("noUsername")
  233. h.close()
  234. local h = fs.open(".userpastebin.password", "w")
  235. h.write("noPassword")
  236. h.close()
  237. return
  238. end
  239. else
  240. cprint("&5Password: ")
  241. sPass = read("*")
  242. cprint("&1\n")
  243. local h = fs.open(".userpastebin.username", "w")
  244. h.write(sName)
  245. h.close()
  246. local h = fs.open(".userpastebin.password", "w")
  247. h.write(sPass)
  248. h.close()
  249. return
  250. end
  251. else
  252. printUsage()
  253. return
  254. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement