Advertisement
craftyoyo

Web CC

May 13th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.41 KB | None | 0 0
  1. -- Config:
  2.  
  3. server = "http://morya.me/cc/web";
  4. config_file = "ccnfs.conf";
  5.  
  6. max_delay = 10
  7. min_delay = 2
  8.  
  9. cur_delay = min_delay
  10.  
  11. -- end config
  12.  
  13. url = string.format("%s/cc_callback.php", server);
  14. version = 1
  15. version_long = "0.2 BETA"
  16.  
  17. version_url_string = "version=" .. version
  18.  
  19. -- begin help functions
  20.  
  21. function load_config()
  22. local disk_file = "/disk/" .. config_file;
  23. if(fs.exists(disk_file)) then
  24. config_file = disk_file;
  25. end
  26. local file = fs.open(config_file, "r");
  27. if(file) then
  28. key = file.readLine();
  29. else
  30. key = nil;
  31. end
  32. end
  33.  
  34. -- from http://lua-users.org/wiki/SplitJoin
  35. -- @param p string
  36. -- @param d delimiter
  37. function split(p, d)
  38. local t, ll
  39. t={}
  40. ll=0
  41. if(#p == 1) then return {p} end
  42. while true do
  43. l=string.find(p,d,ll,true) -- find the next d in the string
  44. if l~=nil then -- if "not not" found then..
  45. table.insert(t, string.sub(p,ll,l-1)) -- Save it in our array.
  46. ll=l+1 -- save just after where we found it for searching next time.
  47. else
  48. table.insert(t, string.sub(p,ll)) -- Save what's left in our array.
  49. break -- Break at end, as it should be, according to the lua manual.
  50. end
  51. end
  52. return t
  53. end
  54.  
  55. function update()
  56. res = http.get(server .. "/lua/ccnfs.lua");
  57. if(not res) then
  58. error("Update: Failed to read ccnfs.lua from server");
  59. end
  60. fh = fs.open(shell.getRunningProgram(), "w");
  61. if(not fh) then
  62. error("Update: Can't open " .. shell.getRunningProgram() .. " for writing");
  63. else
  64. fh.write(res.readAll());
  65. fh.close();
  66. print("Program updated, rebooting system");
  67. sleep(3);
  68. os.reboot();
  69. end
  70. end
  71.  
  72. -- Call to server, does not append computer key
  73. -- @see call
  74. function call_raw(data)
  75. local res = http.post(url,data);
  76. if(not res) then
  77. error("Critical error: http.post returned nil, I'm out of here");
  78. else
  79. local op = res.readLine();
  80. local data = res.readAll();
  81. if(op == "OK") then
  82. res.close();
  83. return data;
  84. elseif(op == "ERR") then
  85. print(string.format("Server responded with error: %s", data));
  86. return nil;
  87. elseif(op == "UPDATE") then
  88. print("Local program out of date, updating!");
  89. update();
  90. else
  91. print(string.format("Unknown response operation from server: %s", op));
  92. return nil;
  93. end
  94. end
  95. end
  96.  
  97. -- does call to server and appends computer key
  98. -- @param data post data to send
  99. -- @return content on OK, nil on ERR or s on critical error
  100. function call(cmd, data)
  101. local new_string = string.format("cmd=%s&key=%s", cmd, key);
  102. if(data) then
  103. new_string = string.format("%s&%s", new_string, data);
  104. end
  105. return call_raw(new_string);
  106. end
  107.  
  108. function call_req(cmd, req_id, data)
  109. local new_string = string.format("id=%d", req_id);
  110. if(data) then
  111. new_string = string.format("%s&%s", new_string, data);
  112. end
  113. call(cmd, new_string);
  114. end
  115.  
  116. function req_error(req_id, msg)
  117. print("Error: " .. msg);
  118. call_req("err", req_id, "data=" .. msg);
  119. end
  120.  
  121. function lines(str)
  122. if(not str) then
  123. return nil;
  124. end
  125.  
  126. return split(str, "\n");
  127. end
  128.  
  129. function file_data(str)
  130. local file_id, filename = str:match("([0-9]+) (.+)");
  131. return file_id, filename;
  132. end
  133.  
  134. function space_split(str)
  135. local part1, part2 = str:match("(.+) (.+)");
  136. return part1, part2;
  137. end
  138.  
  139. function is_blank(x)
  140. return not not tostring(x):find("^%s*$")
  141. end
  142.  
  143. -- end help functions
  144.  
  145. current_write = {
  146. lines_left = 0,
  147. fh = nil,
  148. req_id = nil,
  149. }
  150.  
  151. -- begin remote call functions
  152.  
  153. function ls(req_id, file_id, filename)
  154. if(not fs.exists(filename)) then
  155. req_error(req_id, string.format("[ls] No such file or directory %s", filename));
  156. return;
  157. elseif(not fs.isDir(filename)) then
  158. req_error(req_id, string.format("[ls] %s is not a directory", filename));
  159. return;
  160. else
  161. local files = fs.list(filename);
  162. local data = "";
  163. for _, file in ipairs(files) do
  164. local type;
  165. if(fs.isDir(fs.combine(filename, file))) then
  166. type = "dir";
  167. else
  168. type = "file";
  169. end
  170. data = data .. string.format("%s %s\n", type, file);
  171. end
  172. call_req("ls", req_id, string.format("parent=%d&data=%s", file_id, textutils.urlEncode(data)));
  173. end
  174. end
  175.  
  176. function read(req_id, file_id, filename)
  177. if(not fs.exists(filename)) then
  178. req_error(req_id, string.format("[read] No such file or directory %s", filename));
  179. return;
  180. elseif(fs.isDir(filename)) then
  181. req_error(req_id, string.format("[read] %s is a directory", filename));
  182. call_req("err", req_id);
  183. return;
  184. else
  185. local file = fs.open(filename, "r");
  186. local data = file.readAll();
  187. file.close();
  188. call_req("read", req_id, string.format("file=%d&data=%s", file_id, textutils.urlEncode(data)));
  189. end
  190. end
  191.  
  192. function write(req_id, num_lines, filename)
  193. if(fs.isReadOnly(filename)) then
  194. req_error(req_id, string.format("[write] %s is read only\n", filename));
  195. return;
  196. else
  197. current_write.lines_left = tonumber(num_lines);
  198. current_write.fh = fs.open(filename, "w");
  199. current_write.req_id = req_id;
  200. end
  201. end
  202.  
  203. function mkdir(req_id, filename)
  204. if(fs.isReadOnly(filename)) then
  205. req_error(req_id, string.format("[mkdir] %s is read only\n", filename));
  206. return;
  207. elseif (fs.exists(filename)) then
  208. req_error(req_id, string.format("[mkdir] %s : file exists\n", filename));
  209. return;
  210. else
  211. fs.makeDir(filename);
  212. call_req("done", req_id);
  213. end
  214. end
  215.  
  216. function rm(req_id, filename)
  217. if(fs.isReadOnly(filename)) then
  218. req_error(req_id, string.format("[rm] %s is read only\n", filename));
  219. return;
  220. elseif (not fs.exists(filename)) then
  221. req_error(req_id, string.format("[rm] %s doesn't exist\n", filename));
  222. return;
  223. else
  224. fs.delete(filename);
  225. call_req("done", req_id);
  226. end
  227. end
  228.  
  229. function mv(req_id, old_filename, new_filename)
  230. if(fs.isReadOnly(old_filename)) then
  231. req_error(req_id, string.format("[mv] %s is read only\n", old_filename));
  232. return;
  233. elseif (not fs.exists(old_filename)) then
  234. req_error(req_id, string.format("[mv] %s doesn't exist\n", old_filename));
  235. return;
  236. elseif(fs.isReadOnly(new_filename)) then
  237. req_error(req_id, string.format("[mv] %s is read only\n", new_filename));
  238. return;
  239. elseif (fs.exists(new_filename)) then
  240. req_error(req_id, string.format("[mv] Target %s exist\n", new_filename));
  241. return;
  242. else
  243. fs.move(old_filename, new_filename);
  244. call_req("done", req_id);
  245. end
  246. end
  247.  
  248. function cp(req_id, old_filename, new_filename)
  249. if(fs.isReadOnly(old_filename)) then
  250. req_error(req_id, string.format("[cp] %s is read only\n", old_filename));
  251. return;
  252. elseif (not fs.exists(old_filename)) then
  253. req_error(req_id, string.format("[cp] %s doesn't exist\n", old_filename));
  254. return;
  255. elseif(fs.isReadOnly(new_filename)) then
  256. req_error(req_id, string.format("[cp] %s is read only\n", new_filename));
  257. return;
  258. elseif (fs.exists(new_filename)) then
  259. req_error(req_id, string.format("[cp] Target %s exist\n", new_filename));
  260. return;
  261. else
  262. fs.copy(old_filename, new_filename);
  263. call_req("done", req_id);
  264. end
  265. end
  266.  
  267. function run(req_id, filename)
  268. if(not fs.exists(filename)) then
  269. req_error(req_id, string.format("[read] No such file or directory %s", filename));
  270. return;
  271. else
  272. call_req("done", req_id);
  273. shell.run(filename);
  274. end
  275. end
  276.  
  277. function write_line(line)
  278. current_write.fh.writeLine(line);
  279.  
  280. current_write.lines_left = current_write.lines_left - 1;
  281. if(current_write.lines_left == 0) then
  282. current_write.fh.close();
  283. call_req("done",current_write.req_id);
  284. end
  285. end
  286.  
  287.  
  288. -- end remote call functions
  289.  
  290. remote_functions = {
  291. ls = function(req_id, data)
  292. local file_id, filename = file_data(data);
  293. ls(req_id, file_id, filename);
  294. end,
  295. read = function(req_id, data)
  296. local file_id, filename = file_data(data);
  297. read(req_id, file_id, filename);
  298. end,
  299. write = function(req_id, data)
  300. local lines, filename = file_data(data);
  301. write(req_id, lines, filename);
  302. end,
  303. mkdir = mkdir,
  304. rm = rm,
  305. run = run,
  306. mv = function(req_id, data)
  307. local old_filename, new_filename = space_split(data);
  308. mv(req_id, old_filename, new_filename);
  309. end,
  310. cp = function(req_id, data)
  311. local old_filename, new_filename = space_split(data);
  312. cp(req_id, old_filename, new_filename);
  313. end
  314.  
  315.  
  316. }
  317.  
  318. -- begin main code
  319.  
  320. print(string.format("ComputerCraft Network FileSystem %s\n Author: Torandi\n", version_long));
  321.  
  322. load_config();
  323.  
  324. -- initialize connection
  325.  
  326. if(not key) then
  327. local res = call_raw("cmd=hi&new=true&" .. version_url_string);
  328. if(res) then
  329. key = res;
  330. local fh = fs.open(config_file, "w");
  331. fh.writeLine(key);
  332. fh.close();
  333. ls(0, 0, "/");
  334. else
  335. -- error message should already been printed
  336. return;
  337. end
  338. else
  339. if(call("hi", version_url_string)) then
  340. ls(0, 0, "/");
  341. else
  342. return;
  343. end
  344. end
  345.  
  346. print(string.format("Go to %s to interface with this computer\nKEY: %s", server, key));
  347.  
  348. -- start main loop
  349.  
  350. while(true) do
  351. local poll = lines(call("poll"));
  352.  
  353. local any = false;
  354.  
  355. if(poll) then
  356. for index, line in ipairs(poll) do
  357. if(current_write.lines_left > 0) then
  358. write_line(line);
  359. elseif(not is_blank(line)) then
  360. print(string.format(">> %s", line))
  361. any = true;
  362. local req_id, cmd, data = line:match("([0-9]+) (%a+) ?(.*)")
  363. fn = remote_functions[cmd];
  364. if(fn) then
  365. fn(req_id, data);
  366. else
  367. req_error(req_id, "Unknown command " .. cmd);
  368. end
  369. end
  370. end
  371. end
  372.  
  373. if(any) then
  374. cur_delay = min_delay;
  375. elseif (cur_delay < max_delay) then
  376. cur_delay = cur_delay + 1;
  377. end
  378. sleep(cur_delay);
  379. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement