% -*- mode: slang; mode: fold -*- % % headers.sl - a macro for setting custom headers containing the output of % external programs. % % Copyright (C) 2003 Emmanuele Bassi % % This file may be redistributed and / or modified under the terms of the % GNU General Public License, version 2, as published by the Free Software % Foundation. % % Version: 1.0.3 - Fri Jul 04 12:17:37 CEST 2003 #iffalse % Documentation {{{ * Description: This macro allows the creation of custom command-generated headers (for both new articles and followups), that is headers which payload is generated using the output of an external command. NOTE: The external command MUST be terminated by a newline character ('\n'). * Installation Make SLRN interpret this macro, adding this line inside your slrnrc file: interpret "/path/to/headers.sl" Change "/path/to" accordingly to the path where this macro is located. * Usage This macro comes with two already set headers: X-Uptime: `uptime` X-OS-Name: `uname -srv` New headers are set using the "set_header" function inside another, general purpose, macro: myheaders->set_header ("header_name", "command"); NOTE: this function might also be used to change the payload of any already set command-generated headers. Custom command-generated headers might be unset using the "unset_header" function: myheaders->unset_header ("header_name"); +++ History: v1.0.3 written some documentation; added the "unset_header" function. v1.0.2 initial release. #endif %}}} implements("myheaders"); % Variables {{{ private variable custom_headers_dfl, followup_headers_dfl; private variable Headers = Assoc_Type []; %}}} % Custom headers: the key in the associative array is the header name, % whilst the value is the command that should be used to generate the % header's payload. Headers["X-Uptime"] = "uptime"; Headers["X-OS-Name"] = "uname -srv"; static define set_header (header_name, command) %{{{ { Headers[header_name] = command; } %}}} static define unset_header (header_name) %{{{ { Headers[header_name] = NULL; } %}}} private define get_command_output(command) %{{{ { variable cmd_fp, output; if (0 == strlen(command)) return ""; cmd_fp = popen(command, "r"); if (NULL == cmd_fp) return ""; output = strjoin(fgetslines(cmd_fp), ""); return output; } %}}} static define retrieve_default() %{{{ { custom_headers_dfl = get_variable_value("custom_headers"); followup_headers_dfl = get_variable_value("followup_custom_headers"); } %}}} static define add_command_header() %{{{ { variable custom_headers = custom_headers_dfl; variable followup_headers = followup_headers_dfl; !if (0 == strlen(custom_headers)) custom_headers = custom_headers + "\n"; !if (0 == strlen(followup_headers)) followup_headers = followup_headers + "\n"; foreach (Headers) using ("keys") { variable name = (); variable payload = get_command_output(Headers[name]); !if (0 == strlen(payload)) { custom_headers = sprintf("%s%s: %s", custom_headers, name, payload); followup_headers = sprintf("%s%s: %s", followup_headers, name, payload); } } set_string_variable("custom_headers", custom_headers); set_string_variable("followup_custom_headers", followup_headers); } %}}} % Register hooks {{{ !if (1 == register_hook("startup_hook", "myheaders->retrieve_default")) error ("register_hook(startup_hook) for retrieve_default failed"); !if (1 == register_hook("post_hook", "myheaders->add_command_header")) error ("register_hook(post_hook) for add_command_header failed"); !if (1 == register_hook("followup_hook", "myheaders->add_command_header")) error ("register_hook(followup_hook) for add_command_header failed"); %}}}