Forward File Content of Files on FTP Server via SMS

ftp2sms.are
printf("This script does not speak here, see syslog instead");
nb_syslog('starting');
SITE = "https://share.netmodule.com/router/test";
 
USR = "anonymous";
PWD = "anonymous";
FTPINTERVAL = 30;
MAXSERVERITEMS = 100;
MAXSENTITEMS = 200;
ALARMNUMBER = "+4178949****";
 
// processed SMS
sentSMS = mkarray();
 
// main
while (1) {
    nb_syslog("checking %s for messages", SITE);
    nb_syslog("number of sent items is %s", length(sentSMS));
    if (length(sentSMS) > MAXSENTITEMS) {
        for (i = 0; i < length(sentSMS); i++) {
            if (i < length(sentSMS)-MAXSENTITEMS) { 
                nb_syslog("setting element %s to void", i);
                sentSMS[i] = (); // set oldest elements to void, i.e. delete them
            }
        }
        sentSMS = array_compact(sentSMS); // remove voids from array
        nb_syslog("number of sent items is %s after compacting", length(sentSMS));
    }
    files = nb_transfer_list(USR, PWD, SITE);
 
    if (files) {
        if (length(files) > MAXSERVERITEMS) {
            id = nb_sms_send(ALARMNUMBER, sprintf("Caution, more than %s files on server", MAXSERVERITEMS));
        }
        filesarray = mkarray();   
        for (i = 0; i < length(files); i++) {
            file = files[i];
            filesarray[i] = file.name;
            url = sprintf("%s/%s", SITE, filesarray[i]);
            path = "/tmp/tempmsg";
            if (nb_transfer_get(USR, PWD, url, path) == 0) {
                fp = fopen(path, "r");
                if (fp) {
                    phonenumber = trim(fgets(fp)); // first line is phone number
                    content = fread(fp,file.size); // read rest of file  
                    // check new SMS
                    if (is_void(array_search(sentSMS, filesarray[i]))){
                        // send SMS
                        id = nb_sms_send(phonenumber, content);
                        if (id) {
                            nb_syslog("SMS to %s (origin %s) has been spooled\n", phonenumber, filesarray[i]);
                                sentSMS = array_merge(sentSMS, filesarray[i]);
                                nb_syslog("added %s to sent items", filesarray[i]);
                        } else {
                            nb_syslog("Unable to send SMS to %s\n", phonenumber);
                        }       
                    } else {
                        nb_syslog("file %s already processed, ignoring it", filesarray[i]);
                    }
                    unlink(path);
                }
                else {
                    nb_syslog("download of '%s' failed", url);
                }
            }
        }
    } else {
        nb_syslog("could not access '%s' or no files in directory", SITE);
    }
    sleep(FTPINTERVAL);
}
exit(0);