summaryrefslogtreecommitdiff
path: root/Data/BuiltIn/Libraries/lua-addons/addons/send/send.lua
blob: 8df543b72caac9954250c16b9ebf07de7c90b2f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
_addon.version = '1.0'
_addon.name = 'Send'
_addon.command = 'send'
_addon.author = 'Byrth'

windower.register_event('addon command',function (...)
    local term = table.concat({...}, ' ')

    term = term:gsub('<(%a+)id>', function(target_string)
        local entity = windower.ffxi.get_mob_by_target(target_string)
        return entity and entity.id or '<' .. target_string .. 'id>'
    end)

    local broken_init = split(term, ' ')
    local qual = table.remove(broken_init,1)
    local player = windower.ffxi.get_player()

    if qual:lower()==player['name']:lower() then
        if broken_init ~= nil then
        relevant_msg(table.concat(broken_init,' '))
        end
    elseif qual:lower()=='@all' or qual:lower()=='@'..player.main_job:lower() then
        if broken_init ~= nil then
            relevant_msg(table.concat(broken_init,' '))
        end
        windower.send_ipc_message('send ' .. term)
    else
        windower.send_ipc_message('send ' .. term)
    end
end)

windower.register_event('ipc message',function (msg)
    local broken = split(msg, ' ')

    local command = table.remove(broken, 1)
    if command ~= 'send' then
        return
    end

    if #broken < 2 then return end
    
    local qual = table.remove(broken,1)
    local player = windower.ffxi.get_player()
    if qual:lower()==player.name:lower() then
        relevant_msg(table.concat(broken,' '))
    end
    if string.char(qual:byte(1)) == '@' then
        local arg = string.char(qual:byte(2, qual:len()))
        if arg:upper() == player.main_job:upper() then
            if broken ~= nil then
                relevant_msg(table.concat(broken,' '))
            end
        elseif arg:upper() == 'ALL' then
            if broken ~= nil then
                relevant_msg(table.concat(broken,' '))
            end
        elseif arg:upper() == 'OTHERS' then
            if broken ~= nil then
                relevant_msg(table.concat(broken,' '))
            end
        end
    end
end)

function split(msg, match)
    if msg == nil then return '' end
    local length = msg:len()
    local splitarr = {}
    local u = 1
    while u <= length do
        local nextanch = msg:find(match,u)
        if nextanch ~= nil then
            splitarr[#splitarr+1] = msg:sub(u,nextanch-match:len())
            if nextanch~=length then
                u = nextanch+match:len()
            else
                u = length
            end
        else
            splitarr[#splitarr+1] = msg:sub(u,length)
            u = length+1
        end
    end
    return splitarr
end

function relevant_msg(msg)
    local player = windower.ffxi.get_player()
    
    if msg:sub(1,2)=='//' then
        windower.send_command(msg:sub(3))
    elseif msg:sub(1,1)=='/' then
        windower.send_command('input '..msg)
    elseif msg:sub(1,3)=='atc' then
        windower.add_to_chat(55,msg:sub(5))
    else
        windower.send_command(msg)
    end

end