There's already CGI support in YAGS, but now I'm working on scripting inside YAGS. I was thinking about lua, but right now, I'm experimenting with RHAI.
It looks promising, here's a sample script, that is supposed to add "mention" functionality on every uri, containing `/alice` (in reality `/gemlog`:
fn handle_trackback_link(env) {
let response = blob();
if ! ("TLS_CLIENT_HASH" in env) {
response += "60 Currently certificate is required to send mention link.\r\n";
return response;
}
if ! ("PATH_INFO" in env) || !env.PATH_INFO.starts_with("/") || env.PATH_INFO.bytes() <= 1 {
response += "59 Invalid request, missing mentioned link.\r\n";
return response;
}
if ! ("QUERY_STRING" in env) {
// NOTE: rhai does not allow \r\n inside ``, so they need to be added in ""
response += `10 Enter gemini link containing the mention:` + "\r\n";
return response;
}
if env.QUERY_STRING.is_empty() {
response += "20 text/gemini\r\n";
response += "# Cancelled\n";
response += "Assuming operation has been cancelled, not sending any link\n";
return response;
}
let mentioned_link = url_unquote(env.PATH_INFO.sub_string(1));
response += "20 text/gemini\r\n";
response += "# This was a triumph,\n";
response += "I'm making a note here: huge-success!\n\n";
response += "Following mention link has been sent to admin:\n";
response += `${env.QUERY_STRING}`;
// TODO: write to file
return response;
}
fn add_trackback_link(env, response) {
/// those prints will be visible only on the server
// print(response.status);
// print(response.meta);
let full_path = env.PATH_INFO;
if "QUERY_STRING" in env {
full_path += '?' + env.QUERY_STRING;
}
if response.status == 20 && response.meta == "text/gemini" && response.is_body_accessible {
// response.body += would work as well, you can pick whatever looks better for you
response.body.append("\n\n---\n=> /trackback/");
response.body.append(url_quote(full_path));
response.body.append(" Notify me about mention (cert required) ");
}
/// just an example:
// if !response.meta.contains("; charset") {
// response.meta.append("; charset=utf-8");
// }
return response;
}
register_uri_handler("/trackback", handle_trackback_link);
// Note, this deliberately contains "/alice", so that generated trackback
// link won't trigger `add_trackback_link` callback again.
// Generated link will look like: /trackback/something%3Fsomething%3Falice
register_post_hook("/alice", add_trackback_link);
⛄️ gim
2025-09-01 · 8 months ago