Skip to content

Commit d15a62e

Browse files
committed
ask for trusting plugin
1 parent 908ff9d commit d15a62e

File tree

2 files changed

+56
-26
lines changed

2 files changed

+56
-26
lines changed

script/client.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,19 @@ end
8787
---@param message string
8888
---@param titles string[]
8989
---@return string action
90+
---@return integer index
9091
function m.awaitRequestMessage(type, message, titles)
9192
proto.notify('window/logMessage', {
9293
type = define.MessageType[type] or 3,
9394
message = message,
9495
})
96+
local map = {}
9597
local actions = {}
9698
for i, title in ipairs(titles) do
9799
actions[i] = {
98100
title = title,
99101
}
102+
map[title] = i
100103
end
101104
local item = proto.awaitRequest('window/showMessageRequest', {
102105
type = type,
@@ -106,7 +109,7 @@ function m.awaitRequestMessage(type, message, titles)
106109
if not item then
107110
return nil
108111
end
109-
return item.title
112+
return item.title, map[item.title]
110113
end
111114

112115
---@param type message.type

script/plugin.lua

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local config = require 'config'
22
local util = require 'utility'
33
local client = require 'client'
44
local lang = require 'language'
5+
local await = require 'await'
56

67
---@class plugin
78
local m = {}
@@ -49,34 +50,60 @@ local function resetFiles()
4950
end
5051
end
5152

52-
function m.init()
53-
local ws = require 'workspace'
54-
m.interface = {}
55-
56-
local pluginPath = ws.getAbsolutePath(config.get 'Lua.runtime.plugin')
57-
log.info('plugin path:', pluginPath)
58-
if not pluginPath then
59-
return
60-
end
61-
local pluginLua = util.loadFile(pluginPath)
62-
if not pluginLua then
63-
return
64-
end
65-
m.pluginPath = pluginPath
66-
local env = setmetatable(m.interface, { __index = _ENV })
67-
local f, err = load(pluginLua, '@'..pluginPath, "t", env)
68-
if not f then
69-
log.error(err)
70-
m.showError(err)
71-
return
53+
local function checkTrustLoad()
54+
local trusted = util.loadFile(LOGPATH / 'trusted') or ''
55+
local lines = {}
56+
for line in util.eachLine(trusted) do
57+
lines[#lines+1] = line
58+
if line == m.pluginPath then
59+
return true
60+
end
7261
end
73-
local suc, err = xpcall(f, log.error, f)
74-
if not suc then
75-
m.showError(err)
76-
return
62+
local _, index = client.awaitRequestMessage('Warning', lang.script('PLUGIN_TRUST_LOAD', m.pluginPath), {
63+
lang.script('PLUGIN_TRUST_YES'),
64+
lang.script('PLUGIN_TRUST_NO'),
65+
})
66+
if not index then
67+
return false
7768
end
69+
lines[#lines+1] = m.pluginPath
70+
util.saveFile(LOGPATH / 'trusted', table.concat(trusted, '\n'))
71+
return true
72+
end
73+
74+
function m.init()
75+
await.call(function ()
76+
local ws = require 'workspace'
77+
m.interface = {}
78+
79+
local pluginPath = ws.getAbsolutePath(config.get 'Lua.runtime.plugin')
80+
log.info('plugin path:', pluginPath)
81+
if not pluginPath then
82+
return
83+
end
84+
local pluginLua = util.loadFile(pluginPath)
85+
if not pluginLua then
86+
return
87+
end
88+
m.pluginPath = pluginPath
89+
local env = setmetatable(m.interface, { __index = _ENV })
90+
local f, err = load(pluginLua, '@'..pluginPath, "t", env)
91+
if not f then
92+
log.error(err)
93+
m.showError(err)
94+
return
95+
end
96+
if not checkTrustLoad() then
97+
return
98+
end
99+
local suc, err = xpcall(f, log.error, f)
100+
if not suc then
101+
m.showError(err)
102+
return
103+
end
78104

79-
resetFiles()
105+
resetFiles()
106+
end)
80107
end
81108

82109
return m

0 commit comments

Comments
 (0)