]> git.r.bdr.sh - rbdr/ngx_http_office_hours_filter_module/commitdiff
Add support for office_hours_additional_information 1.1.0
authorRuben Beltran del Rio <redacted>
Tue, 6 Feb 2024 13:14:28 +0000 (13:14 +0000)
committerRuben Beltran del Rio <redacted>
Tue, 6 Feb 2024 13:14:28 +0000 (13:14 +0000)
CONTRIBUTING.md
README.md
ngx_http_office_hours_filter_module.c

index 0cbd8518037fd1eece2065256513d9db14dec827..af7da2500c6d88e1168f1f57eed63f98d722412a 100644 (file)
@@ -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/
index 658e64f17b54ce2c3ecea8e08bdd8a687b6826ca..d03baf71ecd40e110ff67325b6088b13a7f9c540 100644 (file)
--- 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 "<h1>Additional Information></h1>
+<p>Please do not email us asking to open the website 24/7. Send all complaints to friendship.quest/@ruben</p>"
+```
+
 ## 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
index 49fe50f55d66fbf8f9c4ff26fb43360d0857fcde..05c3ca0fd5067feec3aee1459a50a723ebe03e89 100644 (file)
 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("<!doctype html><html><head><title>This website is currently closed!</title></head><body><h1>This website is currently closed!</h1><p>This website has closed for the day, please check our office hours below</p><ul>");
-const ngx_str_t MIDDLE_HTML = ngx_string("</ul><p><em>Current Server Time is: ");
-const ngx_str_t FOOT_HTML = ngx_string("</em></p></body></html>");
+const ngx_str_t HEAD_HTML = ngx_string("<!doctype html><html><head><title>This website is currently closed!</title></head><body><h1>This website is currently closed!</h1><p>This website has closed for the day, please check our office hours below:</p><ul>");
+const ngx_str_t OPEN_SERVER_TIME_HTML = ngx_string("</ul><p><em>Current Server Time is: ");
+const ngx_str_t CLOSE_SERVER_TIME_HTML = ngx_string("</em></p>");
+const ngx_str_t FOOT_HTML = ngx_string("</body></html>");
 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);