From: Ruben Beltran del Rio Date: Tue, 6 Feb 2024 13:14:28 +0000 (+0000) Subject: Add support for office_hours_additional_information X-Git-Tag: 1.1.0 X-Git-Url: https://git.r.bdr.sh/rbdr/ngx_http_office_hours_filter_module/commitdiff_plain/8b830d844c57851cc92dd09a4f4e88492c2ef78a Add support for office_hours_additional_information --- diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 0cbd851..af7da25 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -23,7 +23,6 @@ are in line with the objective. Above All: Be nice, always. * Always make sure the code is properly formatted with `make format` -* Make the PRs according to [Git Flow][gitflow]: (features go to - develop, hotfixes go to master) +* Submit your patch to ruben at unlimited.pizza using [git-send-mail[git-send-mail] -[gitflow]: https://github.com/nvie/gitflow +[git-send-mail]: http://git-send-mail.io/ diff --git a/README.md b/README.md index 658e64f..d03baf7 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,17 @@ values for Friday, Saturday and Sunday. Uninstall nginx. +## Adding more information + +This plugin also provides an `office_hours_additional_information` directive +that lets you add informative HTML to the page that shows when outside +office hours. + +``` +office_hours_additional_information "

Additional Information>

+

Please do not email us asking to open the website 24/7. Send all complaints to friendship.quest/@ruben

" +``` + ## What you will need to get started * [astyle][astyle]: Used to format the code @@ -81,11 +92,12 @@ commit. Run `make setup_hooks` to install it. ## Further Improvements +* Add support for double shifts in the same day +* Add support for serving custom HTML when server is closed * Add support for timezones (currently only GMT) * Add support for public holidays * Add support to respect the time zone of the visitor and not just the server -* Add support for double shifts in the same day [astyle]: http://astyle.sourceforge.net [nginx]: https://nginx.org/en/download.html diff --git a/ngx_http_office_hours_filter_module.c b/ngx_http_office_hours_filter_module.c index 49fe50f..05c3ca0 100644 --- a/ngx_http_office_hours_filter_module.c +++ b/ngx_http_office_hours_filter_module.c @@ -24,9 +24,10 @@ const ngx_uint_t WEEK_LENGTH = 7; const char * CLOSED_TOKEN = "closed"; const ngx_str_t TIME_REGEX = ngx_string("([0-9]{1,2})(?:\\:([0-9]{2}))?\\-([0-9]{1,2})(?:\\:([0-9]{2}))?"); -const ngx_str_t HEAD_HTML = ngx_string("This website is currently closed!

This website is currently closed!

This website has closed for the day, please check our office hours below

Current Server Time is: "); -const ngx_str_t FOOT_HTML = ngx_string("

"); +const ngx_str_t HEAD_HTML = ngx_string("This website is currently closed!

This website is currently closed!

This website has closed for the day, please check our office hours below:

Current Server Time is: "); +const ngx_str_t CLOSE_SERVER_TIME_HTML = ngx_string("

"); +const ngx_str_t FOOT_HTML = ngx_string(""); const char * DAY_NAMES[7] = { "Monday", "Tuesday", @@ -41,6 +42,7 @@ const char * DAY_NAMES[7] = { typedef struct { ngx_array_t *office_hours; + ngx_str_t additional_information; } ngx_http_office_hours_conf_t; /* Lifecycle Functions For Module Context */ @@ -91,6 +93,14 @@ static ngx_command_t ngx_http_office_hours_commands[] = { offsetof(ngx_http_office_hours_conf_t, office_hours), NULL }, + { + ngx_string("office_hours_additional_information"), + NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1 | NGX_CONF_TAKE2 | NGX_CONF_TAKE3 | NGX_CONF_TAKE4 | NGX_CONF_TAKE5 | NGX_CONF_TAKE6 | NGX_CONF_TAKE7, + ngx_conf_set_str_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_office_hours_conf_t, additional_information), + NULL + }, ngx_null_command }; @@ -461,20 +471,42 @@ static ngx_str_t create_output_html(ngx_http_office_hours_conf_t * conf) ngx_uint_t i, seconds_of_day; ngx_uint_t ** parsed_office_hours; - output_buffer = malloc(1024 * sizeof(char)); + size_t base_size = 1024; + size_t additional_info_size = conf->additional_information.len; + size_t buffer_size = base_size + additional_info_size + 1; + output_buffer = malloc(buffer_size * sizeof(char)); + + /* UH-OH we couldn't allocate the bufer */ + if (!output_buffer) { + output_html.data = NULL; + output_html.len = 0; + return output_html; + } + parsed_office_hours = parse_office_hours(conf->office_hours); now = ngx_time(); seconds_of_day = get_seconds_of_day(now); - sprintf(output_buffer, "%s", (char *) HEAD_HTML.data); + int written = 0; + + written += snprintf(output_buffer + written, buffer_size - written, "%s", (char *) HEAD_HTML.data); for (i = 0; i < 7; ++i) { - sprintf(output_buffer + strlen(output_buffer), "%s", format_hours(parsed_office_hours[i], (char *) DAY_NAMES[i])); + written += snprintf(output_buffer + written, buffer_size - written, "%s", format_hours(parsed_office_hours[i], (char *) DAY_NAMES[i])); + } + + written += snprintf(output_buffer + written, buffer_size - written, "%s", (char *) OPEN_SERVER_TIME_HTML.data); + written += snprintf(output_buffer + written, buffer_size - written, "%s", format_seconds(seconds_of_day)); + written += snprintf(output_buffer + written, buffer_size - written, "%s", (char *) CLOSE_SERVER_TIME_HTML.data); + + if (conf->additional_information.len > 0) { + char additional_info[conf->additional_information.len + 1]; + ngx_memcpy(additional_info, conf->additional_information.data, conf->additional_information.len); + additional_info[conf->additional_information.len] = '\0'; + written += snprintf(output_buffer + written, buffer_size - written, "%s", additional_info); } - sprintf(output_buffer + strlen(output_buffer), "%s", (char *) MIDDLE_HTML.data); - sprintf(output_buffer + strlen(output_buffer), "%s", format_seconds(seconds_of_day)); - sprintf(output_buffer + strlen(output_buffer), "%s", (char *) FOOT_HTML.data); + snprintf(output_buffer + written, buffer_size - written, "%s", (char *) FOOT_HTML.data); output_html.data = (unsigned char *) output_buffer; output_html.len = strlen(output_buffer);