📜 ⬆️ ⬇️

Node.js on the Fidonet node: automating recurring publications

Some fidoshniki are faced with the need to periodically publish the same message (the same text file) once in a few days in one or another Fidonet ehoconference.

For example, a moderator (or a comoderator, depending on the distribution of their duties) has to put its rules into his ehoconference once a week or two . A slightly different (but still similar) example are those fidoshniki, who took the support of some FAQ and also publish it in one or more thematically relevant echo conferences. (In the Fidonet.History echo, its FAQ contains a peculiar chronicle of Fidonet’s history expressed in questions and answers, in the SU.IP.Point echo a list of nodes gaining new points, in the SU.FidoTech explanation of a number of technical terms and algorithms of several useful techniques. In ehokonferentsiyah dedicated to a particular software product, FAQ explains its setting. And so on.)

If a node (or point) works continuously on the same computer, this publication is automated in a simple, simple way: the publication of the file is entered into the task list for the cron daemon (on UNIX-like systems) or its analogue on other systems.
')
If the Fidonet system does not work on the same computer (there are fidoshhniki who dragged a set of Fidonet software from one workplace to another once on a diskette, and in the modern time they drag on a flash drive) or at least work non-stop (and from time to time, when the desire to send, receive and read feedmail is born in it, the regularity of publications is provided differently - not by a service (daemon), but by a simple program that checks whether so many days have passed the last one file was posted as needed to ensure that it is time vdrugoryad publish it.

Today we will look at what help the Node.js engine can become in the performance of this task.

First of all, we note that a significant part of this problem has already been solved.

In order to publish the file, it is enough to issue the hpt post command to the HPT echo processor from the Husky software suite. (Similar commands exist in some other popular Fidonet echo processors.)

In order to save (remember) the date and time of such publication, it is enough to issue the touch command with respect to some file, updating the date of its last change. UNIX-like operating systems usually contain this command in finished form, and for Windows it can be downloaded from unFutils site at SourceForge.

So, it remains to write only such a command that would perform the opposite task - would allow from the command line to read the age of the specified file, expressed in days, as well as compare it with some specified age. And for this purpose, here’s what JavaScript for Node.js will serve us :

var fs = require('fs'); var clog = console.log; if (process.argv.length < 3) { clog('Usage:'); clog(' node agedays "filename" [N]'); clog(''); clog('Parameters:'); clog(' filename -- name of the file which age (in days) is checked'); clog(' N (optional) -- if file is N days old (or older),'); clog(' errorlevel 1 is set'); } else if (process.argv.length == 3) { try { var msec = (new Date()).getTime() - fs.statSync(process.argv[2]).mtime.getTime(); var days = msec / 1000 / 60 / 60 / 24; clog('File "' + process.argv[2] + '" is ' + days + ' days old.'); } catch(e) { clog('File "' + process.argv[2] + '" cannot be opened.'); } } else { try { var msec = (new Date()).getTime() - fs.statSync(process.argv[2]).mtime.getTime(); var days = msec / 1000 / 60 / 60 / 24; if( days > (+process.argv[3]) ) { process.exit(1); } else { process.exit(0); } } catch(e) { clog('File "' + process.argv[2] + '" cannot be opened.'); process.exit(2); } } 

By saving this script and calling it agedays.js , we get the opportunity to run it to find out the age of the file and to compare this age with a predetermined number. Here is an example of such actions in Windows over the areas.cfg file:

[screenshot agedays]

By adding such a tool to the two previously available (to the echo processor and touch ), we finally get the opportunity to create a bahtnik (file with commands of the operating system) that will perform the task of periodically publishing one or more files in Fidonet. Here is an example of the batch file that is used on my Windows node:

 @echo off :checkmonth node agedays NodePost\monthly._flag 30 if errorlevel 1 goto monthly goto checkweek :monthly \utils\unxutils\touch NodePost\monthly._flag hpt post -nf "Mithgol's Wishlist Robot" -s "   " -e "Ru.GoldEd" -z "Mithgol's NodePost" -o "FGHI Global Headlight Ignited" -f loc NodePost\GED_Wish.txt goto checkweek :checkweek node agedays NodePost\weekly._flag 7 if errorlevel 1 goto weekly goto end :weekly \utils\unxutils\touch NodePost\weekly._flag hpt post -nf "Moderator of Ru.Fidonet.Yo" -s "*** Rules" -e "Ru.Fidonet.Yo" -z "Mithgol's NodePost" -o "FGHI Global Headlight Ignited" -f loc NodePost\YoRulez.txt hpt post -nf "Moderator of Ru.Russophobia" -s "*** Rules" -e "Ru.Russophobia" -z "Mithgol's NodePost" -o "FGHI Global Headlight Ignited" -f loc NodePost\PhobRule.txt hpt post -nf "Moderator of Ru.Russian.1916" -s "*** Rules" -e "Ru.Russian.1916" -z "Mithgol's NodePost" -o "FGHI Global Headlight Ignited" -f loc NodePost\rule1916.txt goto end :end 

This batch file (in my example, it is called NodePost.cmd ) is most appropriately located in the same directory as HPT, and run from the same place: this allows the hpt post command to find the HPT configuration file in its working directory. I put the files published by the batch file in a subdirectory called NodePost; Flags are also located there , the only purpose of which is to be touched by the touch command and checked by the agedays command.

You can see in the source code that this batch file provides a monthly (once every thirty days) publication of one file in Ru.GoldED, as well as a weekly (once every seven days) distribution of three files with the rules for three Fidonet echo conferences from the official name of the moderator (“ Moderator of ... ").

The command lines that begin with the words “hpt post” are quite long and will probably be automatically transferred when displayed in Habrahabr, but in reality each such command is written in one line. (So ​​that they do not merge with neighboring teams after such a transfer, I added one empty line at the top and bottom of each hpt post command for clarity.) After the “-nf” parameter , the sender's name is written under which the file will be sent to the echo conference , and after the parameter "-e" - the echotag of this echo conference. After the parameters “-s”, “-z” and “-o” , the desired message header, its tiarline and origin are written. The parameter “-f loc” is absolutely necessary for the message to be considered “local” (created exactly at this Fidonet station) and later on it was packed by the echo processor as outgoing to other Fidonet nodes. The last parameter is the name of the file being published.

(If instead of a popular HPT you use another echo processor in Fidonet, then all this information will allow you to easily replace the “hpt post” with a call to a command more suitable for your software.)

Most of the elements of the above solution (Node.js engine, agedays script, touch command, HPT echo processor) are also multi-system (cross-platform), so when transferring it from Windows to another operating system, you only have to rewrite the batch file.

I put the agedays.js script on Github under the free MIT license, and this is where my story ends.

Source: https://habr.com/ru/post/152873/


All Articles