From 0fd1d640464a2e7c6107c3739e10c3ff026f0ba0 Mon Sep 17 00:00:00 2001 From: Jonathan Kamens Date: Sat, 12 Aug 2023 11:51:34 -0400 Subject: [PATCH] Modernize and clean up scheduling functions config directory The add-on no longer supports exporting and importing dynamic scheduling functions, so convert the ".slj" files into text files. Update the README.md file to account for that. --- .../DaysBeforeNthWeekday.slj | 51 ----------- .../DaysBeforeNthWeekday.txt | 84 +++++++++++++++++++ .../FirstDaysOfEachMonth.txt | 42 ++++++++++ .../FirstFourDaysOfEachMonth.slj | 26 ------ contrib/scheduling-functions/NextChol.slj | 25 ------ contrib/scheduling-functions/NextChol.txt | 45 ++++++++++ contrib/scheduling-functions/README.md | 44 ++++------ 7 files changed, 187 insertions(+), 130 deletions(-) delete mode 100644 contrib/scheduling-functions/DaysBeforeNthWeekday.slj create mode 100644 contrib/scheduling-functions/DaysBeforeNthWeekday.txt create mode 100644 contrib/scheduling-functions/FirstDaysOfEachMonth.txt delete mode 100644 contrib/scheduling-functions/FirstFourDaysOfEachMonth.slj delete mode 100644 contrib/scheduling-functions/NextChol.slj create mode 100644 contrib/scheduling-functions/NextChol.txt diff --git a/contrib/scheduling-functions/DaysBeforeNthWeekday.slj b/contrib/scheduling-functions/DaysBeforeNthWeekday.slj deleted file mode 100644 index 4e339c82..00000000 --- a/contrib/scheduling-functions/DaysBeforeNthWeekday.slj +++ /dev/null @@ -1,51 +0,0 @@ -{"version":2,"name":"DaysBeforeNthWeekday","help":"Send an email n days before a specific day of the week on a specific week of the month, e.g., \"9:00am five days before the 4th Wednesday of the month.\" Arguments: -- Recurring -- true or false value indicating whether this is a recurring message (default false) -- Weekday to look for (0 is Sunday, 1 is Monday, etc.); default is Wednesday (3) -- Which week of the month to look for (first, second, third, etc.) (default is 4th) -- How many days in advance to send the message (default is 5) -- Hour at which to send the message (default is 9) -- Minutes after the hour at which to send the message (default is 0) -Specifying \"-\" for any argument is the same as not specifying the argument at all. -If today is the day the email should be sent, the message will be scheduled for today even if the hour and minute are earlier than the current time, which means that Send Later will send out ","body":" function getarg(num, deflt) { - if (args[num] == '-' || args[num] === undefined) - return deflt; - return args[num]; - } - - recurring = getarg(0, false); - weekday = getarg(1, 3); - week_number = getarg(2, 4); - days_in_advance = getarg(3, 5); - send_hour = getarg(5, 9); - send_minute = getarg(5, 0); - - one_day_value = 24 * 60 * 60 * 1000; - in_advance_value = one_day_value * days_in_advance; - - if (prev) - now = new Date(prev.valueOf() + one_day_value); - else - now = new Date(); - - while (true) { - then = new Date(now.valueOf() + in_advance_value); - if (then.getDay() == weekday && - Math.floor((then.getDate() - 1) / 7) == week_number - 1) { - break; - } - now = new Date(now.valueOf() + one_day_value); - } - - now.setHours(send_hour); - now.setMinutes(send_minute); - now.setSeconds(0); - now.setMilliseconds(0); - - next = now; - - if (recurring) { - nextspec = \"function \" + specname; - nextargs = [true, weekday, week_number, days_in_advance, - send_hour, send_minute]; - } -"} \ No newline at end of file diff --git a/contrib/scheduling-functions/DaysBeforeNthWeekday.txt b/contrib/scheduling-functions/DaysBeforeNthWeekday.txt new file mode 100644 index 00000000..4b0d5c25 --- /dev/null +++ b/contrib/scheduling-functions/DaysBeforeNthWeekday.txt @@ -0,0 +1,84 @@ +Function name: DaysBeforeNthWeekday + +Author: Jonathan Kamens + +Back story +---------- + +A user on the send-later-users mailing list +(https://groups.google.com/forum/#!forum/send-later-users) asked how to +generate an email a certain number of days before a particular weekday of the +month, e.g., "five days before the fourth Wednesday of the month." This +function demonstrates how to do that. It can be configured by passing in +arguments or by editing the defaults at the top of the function. + +Help text +--------- + +Send an email n days before a specific day of the week on a specific week of +the month, e.g., "9:00am five days before the 4th Wednesday of the month." +Arguments: + +- Recurring -- true or false value indicating whether this is a recurring + message (default false) +- Weekday to look for (0 is Sunday, 1 is Monday, etc.); default is Wednesday + (3) +- Which week of the month to look for (first, second, third, etc.) (default is + 4th) +- How many days in advance to send the message (default is 5) +- Hour at which to send the message (default is 9) +- Minutes after the hour at which to send the message (default is 0) + +Specifying "-" for any argument is the same as not specifying the argument at +all. + +If today is the day the email should be sent, the message will be scheduled for +today even if the specified hour and minute are earlier than the current time, +which means that Send Later will send out today's late but if you've specified +recurring then subsequent ones will be sent out on time. + +Code +---- + +function getarg(num, deflt) { + if (args[num] == '-' || args[num] === undefined) + return deflt; + return args[num]; +} + +recurring = getarg(0, false); +weekday = getarg(1, 3); +week_number = getarg(2, 4); +days_in_advance = getarg(3, 5); +send_hour = getarg(5, 9); +send_minute = getarg(5, 0); + +one_day_value = 24 * 60 * 60 * 1000; +in_advance_value = one_day_value * days_in_advance; + +if (prev) + now = new Date(prev.valueOf() + one_day_value); +else + now = new Date(); + +while (true) { + then = new Date(now.valueOf() + in_advance_value); + if (then.getDay() == weekday && + Math.floor((then.getDate() - 1) / 7) == week_number - 1) { + break; + } + now = new Date(now.valueOf() + one_day_value); +} + +now.setHours(send_hour); +now.setMinutes(send_minute); +now.setSeconds(0); +now.setMilliseconds(0); + +next = now; + +if (recurring) { + nextspec = "function " + specname; + nextargs = [true, weekday, week_number, days_in_advance, + send_hour, send_minute]; +} diff --git a/contrib/scheduling-functions/FirstDaysOfEachMonth.txt b/contrib/scheduling-functions/FirstDaysOfEachMonth.txt new file mode 100644 index 00000000..ebf17d83 --- /dev/null +++ b/contrib/scheduling-functions/FirstDaysOfEachMonth.txt @@ -0,0 +1,42 @@ +Function name: FirstDaysOfEachMonth + +Author: Jonathan Kamens + +Help text +--------- + +Schedule a recurring message to be sent at 9:00am on the first days of every +month. You can alter the behavior of the function with the following arguments +in this order: hour at which to send the message (default 9), minutes after the +hour to send the message (default 0), last day of each month to send the +message (default 4). + +Code +---- +function getarg(idx, deflt) { + if (args[idx] == null) + return deflt; + return args[idx]; +}; + +if (! prev) { + prev = new Date(); +} +hour = getarg(0, 9); +minute = getarg(1, 0); +lastDay = getarg(2, 4); +dayOfMonth = prev.getDate() + 1; +month = prev.getMonth(); +year = prev.getFullYear(); +if (dayOfMonth > lastDay) { + dayOfMonth = 1; + month++; + if (month > 11) { // month in JavaScript Date objects is 0-based + month = 0; + year++; + } +} +next = new Date(year, month, dayOfMonth, hour, minute); +nextspec = "function " + specname; +nextargs = args; + diff --git a/contrib/scheduling-functions/FirstFourDaysOfEachMonth.slj b/contrib/scheduling-functions/FirstFourDaysOfEachMonth.slj deleted file mode 100644 index 673ea6dc..00000000 --- a/contrib/scheduling-functions/FirstFourDaysOfEachMonth.slj +++ /dev/null @@ -1,26 +0,0 @@ -{"version":2,"name":"FirstFourDaysOfEachMonth","help":"Schedule a recurring message to be sent at 9:00am on the first four days of every month. You can alter the behavior of the function with the following arguments in this order: hour at which to send the message (default 9), minutes after the hour to send the message (default 0), last day of each month to send the message (default 4).","body":"function getarg(idx, deflt) { - if (args[idx] == null) - return deflt; - return args[idx]; -}; - -if (! prev) { - prev = new Date(); -} -hour = getarg(0, 9); -minute = getarg(1, 0); -lastDay = getarg(2, 4); -dayOfMonth = prev.getDate() + 1; -month = prev.getMonth(); -year = prev.getFullYear(); -if (dayOfMonth > lastDay) { - dayOfMonth = 1; - month++; - if (month > 11) { // month in JavaScript Date objects is 0-based - month = 0; - year++; - } -} -next = new Date(year, month, dayOfMonth, hour, minute); -nextspec = \"function \" + specname; -nextargs = args;"} \ No newline at end of file diff --git a/contrib/scheduling-functions/NextChol.slj b/contrib/scheduling-functions/NextChol.slj deleted file mode 100644 index 888356a6..00000000 --- a/contrib/scheduling-functions/NextChol.slj +++ /dev/null @@ -1,25 +0,0 @@ -{"version":2,"name":"NextChol","help":"Schedules the message to send a short time after the end of the current Sabbath or Jewish holiday, or the next Sabbath or Jewish Holiday if it isn't currently one (e.g., if you use this function on a regular Thursday the message will be scheduled to be sent Saturday night. Useful if you use email at these times but don't want to offend your Jewish friends who don't. -","body":" -// See https://jewish-holidays.kamens.us/next-chol?help' for supported cities. - -let city = 'Boston'; -let url = 'https://jewish-holidays.kamens.us/next-chol?force&city=' + city; -let req = new XMLHttpRequest(); -req.open('GET', url, false); -req.send(); -if (req.readyState != 4) - return; -if (req.status != 200) { - msg = 'Error fetching from ' + url; - throw(msg); -} -let matches = req.responseText.match(/^\\s*(\\d+)/); -if (matches.length < 1) { - msg = url + ' did not return a number'; - throw(msg); -} -let unix_time = matches[1]; -next = new Date(); -next.setTime(unix_time * 1000); -next.setMinutes(next.getMinutes() + Math.floor(Math.random() * 15)); -"} diff --git a/contrib/scheduling-functions/NextChol.txt b/contrib/scheduling-functions/NextChol.txt new file mode 100644 index 00000000..390d57c8 --- /dev/null +++ b/contrib/scheduling-functions/NextChol.txt @@ -0,0 +1,45 @@ +Function name: NextChol + +Author: Jonathan Kamens + +Help text +--------- + +Schedules the message to send a short time after the end of the current Sabbath +or Jewish holiday, or the next Sabbath or Jewish Holiday if it isn't currently +one (e.g., if you use this function on a regular Thursday the message will be +scheduled to be sent Saturday night). Useful if you use email at these times +but don't want to offend your Jewish friends who don't. + +Note that this function calls out to a little HTTP API endpoint I wrote to +calculate the end time of the Sabbath or holiday. + +You will probably want to edit the function after importing it to specify what +city you're in, so the times will be correct for you. See the API endpoint's +help page (https://jewish-holidays.kamens.us/next-chol?help) for a list of +supported cities. + +Code +---- + +// See https://jewish-holidays.kamens.us/next-chol?help for supported cities. +let city = 'Boston'; +let url = 'https://jewish-holidays.kamens.us/next-chol?force&city=' + city; +let req = new XMLHttpRequest(); +req.open('GET', url, false); +req.send(); +if (req.readyState != 4) + return; +if (req.status != 200) { + msg = 'Error fetching from ' + url; + throw(msg); +} +let matches = req.responseText.match(/^\s*(\d+)/); +if (matches.length < 1) { + msg = url + ' did not return a number'; + throw(msg); +} +let unix_time = matches[1]; +next = new Date(); +next.setTime(unix_time * 1000); +next.setMinutes(next.getMinutes() + Math.floor(Math.random() * 15)); diff --git a/contrib/scheduling-functions/README.md b/contrib/scheduling-functions/README.md index 09cfd7af..b62a743f 100644 --- a/contrib/scheduling-functions/README.md +++ b/contrib/scheduling-functions/README.md @@ -1,37 +1,25 @@ # Send Later dynamic scheduling function library -This directory contains dynamic scheduling functions for use with the [Send Later Thunderbird add-on](https://addons.thunderbird.net/thunderbird/addon/send-later-3/). For more information about dynamic scheduling functions, see the [Send Later user guide](https://extended-thunder.github.io/send-later/#dynamic). +This directory contains dynamic scheduling functions for use with the [Send +Later Thunderbird add-on][atn]. For more information about dynamic scheduling +functions, see the [Send Later user guide][guide]. -The preferred method for contributing a new function to this library is to fork [Send Later on Github](https://github.com/Extended-Thunder/send-later), copy your function from the dynamic function editor into a new file in this directory. Name your new file with an ".slj" extension, and follow the format conventions used in the existing examples. Finally, update this file (README.md) to describe your function (following the format of the existing entries), and submit a pull request. +The preferred method for contributing a new function to this library is: -Alternatively, you can just [email me](mailto:send-later-support@extended-thunder.org) your function as a plain text file attachment and let me know what it does. +* Fork [Send Later on Github][github]. +* Copy your function from the dynamic function editor into a new file in this + directory formatted to look like the other files. +* Commit your new file to the repository. +* Submit a pull request. + +Alternatively, you can just [email me][email] your function as a plain text +file attachment and let me know what it does. Thanks for your contributions! ## Contributed functions -### DaysBeforeNthWeekday.sjl - -By: Jonathan Kamens <[jik@extended-thunder.org](mailto:jik@extended-thunder.org)> - -A user on the [send-later-users mailing list](https://groups.google.com/forum/#!forum/send-later-users) asked how to generate an email a certain number of days before a particular weekday of a particular weekday of the month, e.g., "five days before the fourth Wednesday of the month." This function demonstrates how to do that. It can be configured by passing in arguments or by editing the defaults at the top of the function. - -### FirstFourDaysOfEachMonth.slj - -By: Jonathan Kamens <[jik@extended-thunder.org](mailto:jik@extended-thunder.org)> - -Schedules a recurring message that sends at 9:00am on the first four days of each month. - -You can use variables to control the send time as well as the last day of each month when the message is sent. See the documentation string for the function for details. - -### NextChol.slj - -By: Jonathan Kamens <[jik@extended-thunder.org](mailto:jik@extended-thunder.org)> - -This function is useful if you are Jewish and use email on the Sabbath and Jewish holidays but you have some Jewish friends who don't, and you'd rather not annoy them by sending them emails when they're offline. - -The NextChol function will schedule your message to be sent shortly after the end of the current Sabbath or holiday, or after the end of the next one if it is not currently one. - -Note that this function calls out to a little HTTP API endpoint I wrote to calculate the end time of the Sabbath or holiday. - -You will probably want to edit the function after importing it to specify what city you're in, so the times will be correct for you. See the [API endpoint's help page](https://jewish-holidays.kamens.us/next-chol?help) for a list of supported cities. +[atn]: https://addons.thunderbird.net/thunderbird/addon/send-later-3/ +[guide]: https://extended-thunder.github.io/send-later/#dynamic +[github]: https://github.com/Extended-Thunder/send-later +[email]: mailto:send-later-support@extended-thunder.org