ad_hoc.lua 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env lua
  2. --[[
  3. -- Copyright (c) 2013-2016 Marcus Rohrmoser, http://purl.mro.name/recorder
  4. --
  5. -- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  6. -- associated documentation files (the "Software"), to deal in the Software without restriction,
  7. -- including without limitation the rights to use, copy, modify, merge, publish, distribute,
  8. -- sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  9. -- furnished to do so, subject to the following conditions:
  10. --
  11. -- The above copyright notice and this permission notice shall be included in all copies or
  12. -- substantial portions of the Software.
  13. --
  14. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  15. -- NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. -- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  17. -- OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  18. -- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. --
  20. -- MIT License http://opensource.org/licenses/MIT
  21. ]]
  22. -- ensure Recorder.lua (next to this file) is found:
  23. package.path = arg[0]:gsub('/[^/]+/?$', '') .. '/../../app/?.lua;' .. package.path
  24. require('Recorder')
  25. Recorder.chdir2app_root( arg[0]:gsub('/[^/]+/?$', '') .. '/../../app/foo.lua' )
  26. if 'POST' ~= os.getenv('REQUEST_METHOD') then http_400_bad_request('need POST.') end
  27. local len = tonumber(os.getenv('CONTENT_LENGTH'))
  28. local body = io.read(len)
  29. local key,value = body:match('^([^=]+)=(.*)$')
  30. if 'referer' ~= value then http_400_bad_request('bad value') end
  31. local bc_http = os.getenv('HTTP_REFERER')
  32. if not bc_http then http_400_bad_request('Odd, no broadcast (HTTP_REFERER) set.') end
  33. local bc = Broadcast.from_file( bc_http:unescape_url() )
  34. if not bc then http_400_bad_request('No usable broadcast (HTTP_REFERER) set: \'', bc_http, '\'') end
  35. if bc:is_past() then http_400_bad_request('broadcast already past: \'', bc_http, '\'') end
  36. -- clean up env and set PATH + LANG
  37. local psx = require('posix')
  38. for k,v in pairs(psx.getenv()) do
  39. if k ~= 'PATH' and k ~= 'LANG' then psx.setenv(k,nil) end
  40. end
  41. if not psx.getenv('LANG') and not os.setlocale('en_US.UTF-8') and not os.setlocale('en_GB.UTF-8') and not os.setlocale('de_DE.UTF-8') then
  42. http_400_bad_request('Cannot set locale')
  43. end
  44. if not psx.getenv('LANG') then psx.setenv('LANG', 'en_US.UTF-8') end
  45. if not psx.getenv('PATH') then psx.setenv('PATH', '/bin:/usr/bin:/usr/local/bin') end
  46. local ad_hoc = assert(Podcast.from_id('ad_hoc'))
  47. if key == 'add' and 'none' == bc:enclosure().state then
  48. bc:add_podcast(ad_hoc)
  49. elseif key == 'remove' and 'pending' == bc:enclosure().state then
  50. bc:remove_podcast(ad_hoc)
  51. else
  52. bc:save() -- repair in case
  53. http_400_bad_request('Cannot modify broadcast \'', bc_http, '\' in state \'', bc:enclosure().state, '\' with action \'', key, '\'')
  54. end
  55. local ok,msg = bc:save()
  56. -- ok,msg = bc:save_podcast_json()
  57. if not ok then http_400_bad_request(msg, ' ', bc_http) end
  58. http_303_see_other(bc_http, 'Good choice, it\'s my pleasure to record that broadcast. Sending you back...')