shironeko/send.lua

by hugeblank
127 days agolua
COPY
1
local user, slots, fromLabel, toLabel = ...
2
local sn = require("api")
3

4
if not (user or slots) then
5
    local out = {
6
        {colors.lightGray, "Quickly send items from one Shironeko box to another. Requires having at least 1 box. See ", colors.white, "\\sn", colors.lightGray, " for more info.", colors.white, " Usage:"},
7
        {colors.lightGray, "send", colors.yellow, " <username> <slots...|\"all\">", colors.lime, " [fromSlot=\"main\"] [toSlot=\"main\"]"},
8
        {colors.lightGray, "Examples:"},
9
        {colors.lightGray, "send", colors.yellow, " hugeblank all", colors.lightGray, " - Sends all contents of your ", colors.lightBlue, "\"main\"", colors.lightGray, " box to hugeblank's ", colors.lightBlue, "\"main\"", colors.lightGray, " box."},
10
        {colors.lightGray, "send", colors.yellow, " hypergenome", colors.yellow, " 1,2,3", colors.lime, " stone", colors.lightGray, " - Sends slots 1, 2, and 3 of your ", colors.lightBlue, "\"stone\"", colors.lightGray, " box to hypergenome's ", colors.lightBlue, "\"main\"", colors.lightGray, " box."},
11
        {colors.lightGray, "send", colors.yellow, " ssh2", colors.yellow, " 1", colors.lime, " leather books", colors.lightGray, " - Sends slot 1 of your ", colors.lightBlue, "\"leather\"", colors.lightGray, " box to ssh2's ", colors.lightBlue, "\"books\"", colors.lightGray, " box."},
12
        {colors.lightGray, "send", colors.yellow, " gplv3", colors.yellow, " 1=5,5=10,11=32", colors.lightGray, " - Sends 5 items from slot 1, 10 items from slot 5, and 32 items from slot 11 of your ", colors.lightBlue, "\"main\"", colors.lightGray, " box to gplv3's ", colors.lightBlue, "\"main\"", colors.lightGray, " box."},
13
        {colors.lightGray, "Note that all labels besides ", colors.lightBlue, "\"main\"", colors.lightGray, " are hypothetical for the sake of demonstration."}
14
    }
15
    for i = 1, #out do
16
        local row = out[i]
17
        for i = 1, #row, 2 do
18
            term.setTextColor(row[i])
19
            write(row[i+1])
20
        end
21
        print()
22
    end
23
    return
24
end
25

26
fromLabel, toLabel = fromLabel or "main", toLabel or "main"
27

28
if slots == "all" then
29
    slots = {}
30
    for i = 1, 27 do
31
        slots[i] = 64
32
    end
33
else
34
    local tslots = {}
35
    local _, suc = slots:gsub("(%d+)=?(%d*),?", function(k, v)
36
        local num = tonumber(k)
37
        if not num then
38
            printError("Cannot convert "..str.." to a slot number.")
39
            return
40
        end
41
        tslots[num] = v and tonumber(v) or 64
42
    end)
43
    if suc == 0 then
44
        printError("Cannot parse slot listing '"..slots.."'.")
45
    end
46
    slots = tslots
47
end
48

49
local modem = settings.get("shironeko.send.modem")
50
settings.define("shironeko.send.modem", {
51
    description = "Name of the modem that the send CLI scripts use. If unset, scripts will automatically find a suitable wireless modem.",
52
    type = "string"
53
})
54

55

56
if modem then
57
    if not peripheral.isPresent(modem) then
58
        printError("Modem "..modem.." from 'shironeko.send.modem' does not exist. Please set a valid modem or unset the setting.")
59
        return
60
    end
61
else
62
    for _, m in ipairs(table.pack(peripheral.find("modem"))) do
63
        if m.isWireless() then
64
            modem = peripheral.getName(m)
65
            break
66
        end
67
    end
68
    if not (modem and peripheral.isPresent(modem)) then
69
        printError("Suitable modem not found! Either place a wireless modem, or set a modem using 'shironeko.send.modem'.")
70
        return
71
    end
72
end
73

74
local errmap = {
75
    no_such_label_sender = "No such sender label \""..(fromLabel or "main").."\".",
76
    no_outbound_storage = "Label \""..(fromLabel or "main").."\" lacks a tagged outbound storage.",
77
    no_such_label_recipient = "No such recipient label \""..(toLabel or "main").."\".",
78
    no_inbound_storage = "Label \""..(toLabel or "main").."\" lacks a tagged inbound storage.",
79
    no_such_user = "User "..user.." is not a registered Shironeko user.",
80
    blocked = "You have been blocked from sending to this recipient and label.",
81
    recipient_full = "Recipient box is full and cannot accept any more items.",
82
    same_storage = "Cannot send to the same storage.",
83
    version = "Please update shironeko! Use the same command you used to install the programs (found with \\sn me).",
84
    timeout = "No response from Shironeko."
85
}
86

87
local api = sn.init(modem)
88
local ok, res = api.send(user, slots, fromLabel, toLabel)
89
if not ok then
90
    printError(errmap[res] or "Unknown error "..res)
91
else
92
    local total = 0
93
    for _, item in ipairs(res.items) do
94
        total = total+item.count
95
    end
96
    print("Sent "..total.." items to "..user.."'s "..(fromLabel or "main").." label from your "..(toLabel or "main").." label.")
97
end
98
api.close()