SHOW:
|
|
- or go back to the newest paste.
1 | - | -- We require our libraries and modules here |
1 | + | -- We require our libraries and modules here |
2 | - | local component = require("component") |
2 | + | local component = require("component") |
3 | - | local modem = component.modem |
3 | + | local modem = component.modem |
4 | - | local io = require("io") |
4 | + | local io = require("io") |
5 | - | local event = require("event") |
5 | + | local event = require("event") |
6 | - | local fs = require("filesystem") |
6 | + | local fs = require("filesystem") |
7 | - | |
7 | + | |
8 | - | -- This line will allow the user to easily specify how many |
8 | + | -- This line will allow the user to easily specify how many |
9 | - | -- ports, and messages, we want to use with the program. |
9 | + | -- ports, and messages, we want to use with the program. |
10 | - | local ports, msgs = ... |
10 | + | local ports, msgs = ... |
11 | - | |
11 | + | |
12 | - | -- This ensures that the proper amount of arguments are being used, |
12 | + | -- This ensures that the proper amount of arguments are being used, |
13 | - | -- and if not, will display proper usage of running the program |
13 | + | -- and if not, will display proper usage of running the program |
14 | - | if #({...}) > 2 then |
14 | + | |
15 | - | error("Usage: Logger (# of ports to listen to, starting at 1) (# of messages to write to file before program ends.", 0) |
15 | + | -- if #({...}) > 2 then -- Uncomment this when the mechanics of function runtokill() works. |
16 | - | return |
16 | + | if #({...}) ~= 2 then |
17 | - | end |
17 | + | error("Usage: Logger (# of ports to listen to, starting at 1) (# of messages to write to file before program ends.", 0) |
18 | - | |
18 | + | return |
19 | - | if #({...}) == 1 then |
19 | + | end |
20 | - | runtokill() |
20 | + | |
21 | - | return |
21 | + | --[[ if #({...}) == 1 then |
22 | - | end |
22 | + | runtokill() |
23 | - | |
23 | + | return |
24 | - | function runtokill() |
24 | + | end |
25 | - | while true do |
25 | + | |
26 | - | |
26 | + | function runtokill() |
27 | - | |
27 | + | while true do |
28 | - | -- You can ignore this snippet of code, unless you're |
28 | + | local h = io.open("Logs.txt", "a") |
29 | - | -- ping. Or unless you really wanna wait half an hour |
29 | + | local _, _, from, port, _, message = event.pull("modem_message") |
30 | - | -- for all ports to open. Luckily if you're running this |
30 | + | print("Got a message from " .. from .. " on port " .. port .. ": " .. tostring(message)) |
31 | - | -- on a Minecraft server, you only need to do this once. |
31 | + | h:write("Got a message from " .. from .. " on port " .. port .. ": " .. tostring(message) .. "\n") |
32 | - | if ports == "potato" then |
32 | + | local _, _, _, char, _, _ = event.pull("key_down") |
33 | - | ports = 65535 |
33 | + | if char == keyboard.keys.q then |
34 | - | end |
34 | + | break |
35 | - | |
35 | + | end |
36 | - | -- This is to check whether or not the argument being passed |
36 | + | end ]]-- |
37 | - | -- as ports is actually a number, and not words or symbols. |
37 | + | |
38 | - | if not tonumber(ports) then |
38 | + | -- You can ignore this snippet of code, unless you're |
39 | - | error("The port needs to be a number!",0) |
39 | + | -- ping. Or unless you really wanna wait half an hour |
40 | - | return |
40 | + | -- for all ports to open. Luckily if you're running this |
41 | - | end |
41 | + | -- on a Minecraft server, you only need to do this once. |
42 | - | |
42 | + | if ports == "potato" then |
43 | - | -- Same thing as our first check, only here we're checking the |
43 | + | ports = 65535 |
44 | - | -- messages amount argument. |
44 | + | end |
45 | - | if not tonumber(msgs) then |
45 | + | |
46 | - | error("The amount of messages needs to be a number!", 0) |
46 | + | -- This is to check whether or not the argument being passed |
47 | - | return |
47 | + | -- as ports is actually a number, and not words or symbols. |
48 | - | end |
48 | + | if not tonumber(ports) then |
49 | - | |
49 | + | error("The port needs to be a number!",0) |
50 | - | -- First, we have to open our ports that we want to listen to. |
50 | + | return |
51 | - | -- Note the maximum on our range is the variable we defined in our |
51 | + | end |
52 | - | -- first argument. |
52 | + | |
53 | - | for prt = 1,ports do |
53 | + | -- Same thing as our first check, only here we're checking the |
54 | - | modem.open(prt) |
54 | + | -- messages amount argument. |
55 | - | end |
55 | + | if not tonumber(msgs) then |
56 | - | |
56 | + | error("The amount of messages needs to be a number!", 0) |
57 | - | -- This is where the magic happens. This is where we open our log file, or |
57 | + | return |
58 | - | -- create one if it doesn't exist, and listen for messages to write to our |
58 | + | end |
59 | - | -- file until we've caught enough messages to satisfy our second argument. |
59 | + | |
60 | - | local h = io.open("Logs.txt", "a") |
60 | + | -- First, we have to open our ports that we want to listen to. |
61 | - | for n = 1, tonumber(msgs) do |
61 | + | -- Note the maximum on our range is the variable we defined in our |
62 | - | local _, _, from, port, _, message = event.pull("modem_message") |
62 | + | -- first argument. |
63 | - | print("Got a message from " .. from .. " on port " .. port .. ": " .. tostring(message)) |
63 | + | for prt = 1,ports do |
64 | - | h:write("Got a message from " .. from .. " on port " .. port .. ": " .. tostring(message) .. "\n") |
64 | + | modem.open(prt) |
65 | - | end |
65 | + | end |
66 | - | |
66 | + | |
67 | - | -- Now that we have enough messages, we can close the ports we opened earlier on. |
67 | + | -- This is where the magic happens. This is where we open our log file, or |
68 | - | modem.close() |
68 | + | -- create one if it doesn't exist, and listen for messages to write to our |
69 | - | -- Notice I didn't specify a port? That's because |
69 | + | -- file until we've caught enough messages to satisfy our second argument. |
70 | - | -- by default in OC, when a port isn't defined, all |
70 | + | local h = io.open("Logs.txt", "a") |
71 | - | -- ports are closed when this method is called. |
71 | + | for n = 1, tonumber(msgs) do |
72 | - | |
72 | + | local _, _, from, port, _, message = event.pull("modem_message") |
73 | - | -- Now we must close our log file to ensure that it saves. |
73 | + | print("Got a message from " .. from .. " on port " .. port .. ": " .. tostring(message)) |
74 | - | h:close() |
74 | + | h:write("Got a message from " .. from .. " on port " .. port .. ": " .. tostring(message) .. "\n") |
75 | - | |
75 | + | end |
76 | - | -- This lets the user know that the logs were successfully saved. |
76 | + | |
77 | - | -- Huzzah! |
77 | + | -- Now that we have enough messages, we can close the ports we opened earlier on. |
78 | modem.close() | |
79 | -- Notice I didn't specify a port? That's because | |
80 | -- by default in OC, when a port isn't defined, all | |
81 | -- ports are closed when this method is called. | |
82 | ||
83 | -- Now we must close our log file to ensure that it saves. | |
84 | h:close() | |
85 | ||
86 | -- This lets the user know that the logs were successfully saved. | |
87 | -- Huzzah! | |
88 | print("Saved logs to Logs.txt.") |