I recommend putting in some display type statements to help debug your function. Print out the first line of the file, so you can see if it has the text you are looking for. Then print something showing that you made it inside the if statement. Then print out the new URL you are trying to match to see if it looks correct. And then print something after the downloadFile call to make sure it happened.
Granted, you can also simplify that function a bit. Calling string.match with a pattern that doesn't match the text will return nil, so you can just try to match right away, rather than looking to see if you need to get perform a match. So you could make your function look something like this:
local handlerId = registerAnonymousEventHandler( "sysDownloadDone",
function(_, filename)
print(filename) -- debug
local f, s, result = io.open(filename)
if f then result = f:read("*a"); io.close(f) end
print(result) -- debug
local redirectUrl = result:match([[The document has moved <A HREF="(.+)">here</A>]])
print(redirectUrl) -- debug
if redirectUrl then
downloadFile( filename, redirectUrl )
print("Downloading") -- debug
else
killAnonymousEventHandler(handlerId)
print("Done") -- debug
end
end,
true
)
Note that I threw in a ton of debug messages, so you should know if your function gets called, and how far it is able to progress before something fails. Also note that I find using handlerId within the function, before it is declared to be suspicious, as you generally can't do things that way in Lua. Normally, you need to declare the variable first, then use it, but in this case the function is created before the variable is declared (right side goes first, and then is assigned to left side, so the function will reference the global handlerId, which I assume will be nil).