From 65027d35b433aef96e5329582a58cc783b7a88bd Mon Sep 17 00:00:00 2001
From: deuce <>
Date: Wed, 6 May 2015 21:36:35 +0000
Subject: [PATCH] More functions, fewer globals.

---
 exec/load/podcast_routines.js | 27 ++++++++++++++-
 exec/podcast.js               | 64 +++++++++++++++--------------------
 2 files changed, 54 insertions(+), 37 deletions(-)

diff --git a/exec/load/podcast_routines.js b/exec/load/podcast_routines.js
index 9242b5d21b..98657a9943 100644
--- a/exec/load/podcast_routines.js
+++ b/exec/load/podcast_routines.js
@@ -1,3 +1,5 @@
+if (js.global.HTTP == undefined)
+	js.global.load("http.js");
 if (js.global.MSG_DELETE == undefined)
 	js.global.load("sbbsdefs.js");
 
@@ -45,8 +47,31 @@ function podcast_get_info(base, hdr)
 		return;
 	ret.title = hdr.subject;
 	ret.description = m[1];
-	ret.enclosure = m[2];
+	ret.enclosure = {};
+	ret.enclosure.url = m[2];
 	ret.guid = hdr.id;
 	ret.pubDate = (new Date(hdr.when_written_time * 1000)).toUTCString();
 	return ret;
 }
+
+function podcast_get_enclosure_info(enc)
+{
+	var http;
+	var hdrs;
+
+	if (enc == undefined)
+		return false;
+	if (enc.url == undefined)
+		return false;
+	http = new HTTPRequest();
+	hdrs = http.Head(enc.url);
+	if (hdrs == undefined)
+		return false;
+	if (hdrs['Content-Type'] == undefined || hdrs['Content-Length'] == undefined) {
+		log("HEAD request of "+enc.url+" did not return either Content-Type or Content-Length");
+		return false;
+	}
+	enc.length = hdrs['Content-Length'][0] + 0;
+	enc.type = hdrs['Content-Type'][0].replace(/^\s*(.*?)\s*/, "$1");
+	return true;
+}
diff --git a/exec/podcast.js b/exec/podcast.js
index 096b531020..2b4e536aaa 100644
--- a/exec/podcast.js
+++ b/exec/podcast.js
@@ -1,24 +1,15 @@
-if (js.global.HTTP == undefined)
-	js.global.load("http.js");
 if (js.podcast_load_headers == undefined)
 	js.global.load("podcast_routines.js");
 
 var opts = load({}, "modopts.js", "Podcast");
 var base;
 var i;
-var hdr;
 var out;
 var hdrs;
 var img_title;
 var img_link;
 var item;
-var body;
-var link;
 var m;
-var http;
-var item_headers;
-var item_length;
-var item_type;
 var item_info;
 
 function encode_xml(str)
@@ -38,6 +29,30 @@ function add_channel_opt_attribute(name) {
 	}
 }
 
+function create_item_xml(base, hdr) {
+	var info;
+	var item;
+
+	info=podcast_get_info(base, hdr);
+	if (info == undefined)
+		return;
+	if (!podcast_get_enclosure_info(info.enclosure))
+		return;
+	item = '\t\t<item>\n';
+	item += '\t\t\t<title>'+encode_xml(info.title)+'</title>\n';
+	item += '\t\t\t<link>'+encode_xml(opts.EpisodeLink+parseInt(i+1))+'</link>\n';
+	item += '\t\t\t<description>'+encode_xml(info.description)+'</description>\n';
+	// TODO: author?
+	// TODO: category?
+	item += '\t\t\t<comments>'+encode_xml(opts.EpisodeLink+parseInt(i+1))+'</comments>\n';
+	item += '\t\t\t<enclosure url="'+encode_xml(info.enclosure.url)+'" length="'+encode_xml(info.enclosure.length)+'" type="'+encode_xml(info.enclosure.type)+'" />\n';
+	item += '\t\t\t<guid isPermaLink="false">'+encode_xml(info.guid)+'</guid>\n';
+	item += '\t\t\t<pubDate>'+encode_xml(info.pubDate)+'</pubDate>\n';
+	// TODO: source?
+	item += '\t\t</item>\n';
+	return item;
+}
+
 if (opts == undefined) {
 	log("Unable to load Podcast modopts");
 	exit(1);
@@ -139,38 +154,15 @@ if (opts.ImageURL != undefined) {
 		out.write('\t\t\t<width>'+encode_xml(opts.ImageWidth)+'</width>\n');
 }
 add_channel_opt_attribute('Rating');
-// TODO: textInput
+// TODO: textInput?
 add_channel_opt_attribute('SkipHours');
 add_channel_opt_attribute('SkipDays');
 out.write('\t\t<atom:link href="'+opts.FeedURI+'" rel="self" type="application/rss+xml" />\n');
 
 for (i=hdrs.length - 1; i >= 0; i--) {
-	item_info=podcast_get_info(base, hdrs[i]);
-	if (item_info == undefined)
-		continue;
-	http = new HTTPRequest();
-	item_headers = http.Head(item_info.enclosure);
-	if (item_headers == undefined)
-		continue;
-	if (item_headers['Content-Type'] == undefined || item_headers['Content-Length'] == undefined) {
-		log("HEAD request of "+m[2]+" did not return either Content-Type or Content-Length");
-		continue;
-	}
-	item_length = item_headers['Content-Length'][0] + 0;
-	item_type = item_headers['Content-Type'][0].replace(/^\s*(.*?)\s*/, "$1");
-	item = '\t\t<item>\n';
-	item += '\t\t\t<title>'+encode_xml(item_info.title)+'</title>\n';
-	item += '\t\t\t<link>'+encode_xml(opts.EpisodeLink+parseInt(i+1))+'</link>\n';
-	item += '\t\t\t<description>'+encode_xml(item_info.description)+'</description>\n';
-	// TODO: author?
-	// TODO: category?
-	item += '\t\t\t<comments>'+encode_xml(opts.EpisodeLink+parseInt(i+1))+'</comments>\n';
-	item += '\t\t\t<enclosure url="'+encode_xml(item_info.enclosure)+'" length="'+item_length+'" type="'+item_type+'" />\n';
-	item += '\t\t\t<guid isPermaLink="false">'+encode_xml(item_info.guid)+'</guid>\n';
-	item += '\t\t\t<pubDate>'+encode_xml(item_info.pubDate)+'</pubDate>\n';
-	// TODO: source?
-	item += '\t\t</item>\n';
-	out.write(item);
+	item = create_item_xml(base, hdrs[i]);
+	if (item != undefined)
+		out.write(item);
 }
 
 out.write('\t</channel>\n');
-- 
GitLab