diff --git a/action/news.txt b/action/news.txt new file mode 100644 index 0000000..aa36c4e --- /dev/null +++ b/action/news.txt @@ -0,0 +1,4 @@ +If you aren't already in Discord, join us @ https://discord.aq2world.com/ ! <<< +Don't forget to visit our forums @ https://forums.aq2world.com/ ! <<< +Congrats to matic for his Map Jam-winning map, torg! <<< +Watch for new events in Discord, such as our regular CTF matches! <<< diff --git a/plugins/cvarbans.lua b/plugins/cvarbans.lua index a4f29f9..371f065 100644 --- a/plugins/cvarbans.lua +++ b/plugins/cvarbans.lua @@ -91,7 +91,7 @@ function q2a_load(config) return 0 end - gi.dprintf("cvarbans.lua q2a_load(): Checking/Downloading new cvarbans... ") + gi.dprintf("cvarbans.lua q2a_load(): Checking/Downloading new cvarbans...\n") cvarbans_update = root_dir..'plugins/cvarbans_update.sh "'..url..'" "'..game..'"' cvarbans_os_exec(cvarbans_update) -- check for updated cvarbanlist on load end \ No newline at end of file diff --git a/plugins/news.lua b/plugins/news.lua new file mode 100644 index 0000000..b6910b6 --- /dev/null +++ b/plugins/news.lua @@ -0,0 +1,60 @@ +local version = "1.0" +gi.AddCommandString("set q2a_news "..version.."\n") +local game = gi.cvar("game", "").string +local broadcasts = {} + +function wait (millisecond) +end + +function load_news() + for line in io.lines('action/news.txt') do + table.insert(broadcasts, line) + end +end + +function news_os_exec(script) + os.execute(script) + wait(1000) -- wait 1s to make sure download is complete and went smooth + local last_update = os.date() + gi.AddCommandString('sets news_update '..last_update..'\n') -- write update time to cvar + load_news() +end + +function q2a_load(config) + gi.dprintf("news.lua q2a_load(): "..version.." for Action Quake 2 loaded.\n") + + root_dir = config.root_dir + url = config.url + if root_dir == nil then + gi.dprintf("news.lua q2a_load(): 'root_dir' not defined in the config.lua file... aborting\n") + return 0 + end + if url == nil then + gi.dprintf("news.lua q2a_load(): 'url' not defined in the config.lua file... aborting\n") + return 0 + end + + gi.dprintf("news.lua q2a_load(): Checking/Downloading news...\n") + news_update = root_dir..'plugins/news_update.sh "'..url..'" "'..game..'"' + news_os_exec(news_update) -- check for updated news on load +end + + +local current_index = 1 +local last_check = os.time() + +function RunFrame() + local now = os.time() + + if now - last_check >= 600 then -- 600 seconds = 10 minutes + local bcast = broadcasts[current_index] + gi.bprintf(PRINT_CHAT, '*** '..bcast..'\n') + + current_index = current_index + 1 + if current_index > #broadcasts then + current_index = 1 + end + + last_check = now + end +end diff --git a/plugins/news_update.sh b/plugins/news_update.sh new file mode 100755 index 0000000..82f52d2 --- /dev/null +++ b/plugins/news_update.sh @@ -0,0 +1,27 @@ +#!/usr/bin/bash +# a script that checks and downloads of the news master file + +# download URL +url="$1" +gamedir="$2" +# bypass modsecurity +usaragent="-d --user-agent=news-update-script" +# parse the filename from the download path +filename=$(echo $url | sed -n 's/^\(.*\/\)*\(.*\)/\2/p') + +# script to download latest news.txt from github +cd "${0%/*}" # cd to current dir +cd ../$gamedir/ # cd back and to game dir + +#### script +# download the file if updated since last time, and output wheter it updated or not to console + +if [[ "$url" == *"githubusercontent"* ]]; then + # not using -N switch for wget since github does not support "last update" parameter, the file will be downloaded on every check, wich is not optimal + wget -O $filename $url 2>&1 | tee | grep -E 'Omitting download|"news.txt" saved|ERROR 404: Not Found' +else + # using -N switch for wget since the file os not hosted on github, best solution, since file will only be downloaded if changed + wget $usaragent -N $url 2>&1 | tee | grep -E 'Omitting download|"news.txt" saved|ERROR 404: Not Found' +fi + +exit \ No newline at end of file