var minisrv_service_file = true;
const wtvnews = new WTVNews(minisrv_config, service_name);
var service_config = minisrv_config.services[service_name];
if (service_config.local_nntp_port && wtvnewsserver) {
var tls_path = this.wtvshared.getAbsolutePath(this.minisrv_config.config.ServiceDeps + '/wtv-news');
var tls_options = {
ca: this.fs.readFileSync(tls_path + '/localserver_ca.pem'),
key: this.fs.readFileSync(tls_path + '/localserver_key.pem'),
cert: this.fs.readFileSync(tls_path + '/localserver_cert.pem'),
checkServerIdentity: () => { return null; }
}
if (wtvnewsserver.username)
wtvnews.initializeUsenet("127.0.0.1", service_config.local_nntp_port, tls_options, wtvnewsserver.username, wtvnewsserver.password);
else
wtvnews.initializeUsenet("127.0.0.1", service_config.local_nntp_port, tls_options);
} else {
if (service_config.upstream_auth)
wtvnews.initializeUsenet(service_config.upstream_address, service_config.upstream_port, service_config.upstream_tls || null, service_config.upstream_auth.username || null, service_config.upstream_auth.password || null);
else
wtvnews.initializeUsenet(service_config.upstream_address, service_config.upstream_port, service_config.upstream_tls || null);
}
async function throwError(e) {
console.log(e);
var errpage = wtvshared.doErrorPage(400, null, e.toString());
sendToClient(socket, errpage[0], errpage[1]);
}
function isToday (chkdate) {
const today = new Date()
return chkdate.getDate() == today.getDate() &&
chkdate.getMonth() == today.getMonth() &&
chkdate.getFullYear() == today.getFullYear()
}
async function WebTVListGroup(group) {
var page_limit_default = 100;
wtvnews.connectUsenet().then(() => {
wtvnews.selectGroup(group).then((response) => {
var limit_per_page = (request_headers.query.limit) ? parseInt(request_headers.query.limit) : page_limit_default;
var page = (request_headers.query.chunk) ? parseInt(request_headers.query.chunk) : 0;
console.log(response);
var page_start = (limit_per_page * page) + 1;
var page_end = (page + 1) * limit_per_page;
if (page_end > response.group.high) {
page_end = response.group.high;
limit_per_page = (page_end - (limit_per_page / (page + 1))) + limit_per_page;
}
wtvnews.listGroup(group, page, limit_per_page).then((response) => {
if (response.code == 211) {
NGCount = response.group.number;
NGArticles = response.group.articleNumbers;
page_start = (limit_per_page * page) + 1;
page_end = (page + 1) * limit_per_page;
wtvnews.getHeaderObj(NGArticles).then((messages) => {
messages = wtvnews.sortByResponse(messages);
wtvnews.quitUsenet();
headers = `200 OK
Connection: Keep-Alive
Content-Type: text/html
wtv-expire: wtv-news:/news?group=${request_headers.query.group}`
data = `
${request_headers.query.group}
|
|
|
|
|
Group: ${request_headers.query.group}
|
|
`
if (NGCount == 0) {
data += `This group has no postings`;
} else {
data += NGCount + " posting";
if (NGCount != 1)
data += "s"
}
data += `
`;
if (NGCount > 0) {
data += `
|
${(page > 0) ? ` ` : ` `}
|
|
|
|
${(page_end < NGCount) ? ` ` : ` `}
|
|
|
|
${page_start}-${page_end}
|
|
|
| `;
}
data += ` |
`
if (NGCount > 0) {
Object.keys(messages).forEach(function (k) {
var message = messages[k].article;
var has_relation = (messages[k].relation !== null) ? true : false;
var date_obj = new Date(Date.parse(message.headers.DATE));
var date = (isToday(date_obj)) ? strftime("%I:%M %p", date_obj) : strftime("%b %d", date_obj)
data += `
|
|
${(has_relation) ? `| ` : ''}
|
${(message.headers.SUBJECT) ? wtvshared.htmlEntitize(message.headers.SUBJECT) : "(No Subject)"}
|
${(message.headers.FROM.indexOf(' ') > 0) ? message.headers.FROM.split(' ')[0] : message.headers.FROM}, ${date}
| |
|
| `;
});
}
data += `

|
|
|
|
|
|
|
`;
sendToClient(socket, headers, data);
}).catch((e) => {
// getHeaderObj err
throwError(e)
});;
}
}).catch((e) => {
// listGroup error
throwError(e)
});;
}).catch((e) => {
// selectGroup error
throwError(e)
});
}).catch((e) => {
// connect error
throwError(e)
});
}
async function WebTVShowMessage(group, article) {
var article = parseInt(article);
wtvnews.connectUsenet().then(() => {
wtvnews.selectGroup(group).then((response) => {
wtvnews.getArticle(article).then((response) => {
wtvnews.quitUsenet();
headers = `200 OK
Content-type: text/html
wtv-expire-all: wtv-news:/news?group=${group}&article=`;
var signature = null;
var message_colors = session_data.mailstore.defaultColors;
var display_signature = true; // todo make a toggle
var message = wtvnews.parseAttachments(response);
var message_body = message.text;
var attachments = null;
var signature_index = null;
wtvnews.debug(message);
if (message.attachments) attachments = message.attachments;
if (attachments) {
if (Object.keys(attachments).length > 0) {
Object.keys(attachments).forEach((k) => {
if (attachments[k].filename == "wtv_signature.html" && attachments[k].content_type.match(/text\/html/)) {
signature = attachments[k].data;
signature_index = k;
return false;
}
});
if (signature_index) attachments.splice(signature_index, 1);
}
}
if (signature) message_colors = session_data.mailstore.getSignatureColors(signature);
data = `
${(response.article.headers.SUBJECT) ? wtvshared.htmlEntitize(response.article.headers.SUBJECT) : '(No subject)'}
|
|
|
|
|
|
Post
|
Group:
|
${wtvshared.htmlEntitize(response.article.headers.NEWSGROUPS)}
|
|
Date: |
${strftime("%a, %b %e, %Y, %I:%M%P", new Date(Date.parse(response.article.headers.DATE)))}
|
|
From:
| `;
// if (message.from_name != message.from_addr) {
// data += `${wtvshared.htmlEntitize(message.from_addr)} `;
// } else {
data += `${wtvshared.htmlEntitize(response.article.headers.FROM)}`;
// }
data += ` |
|
|
| |
|
|
${(response.article.headers.SUBJECT) ? wtvshared.htmlEntitize(response.article.headers.SUBJECT) : '(No subject)'}
|
|
`
data += `
${wtvshared.htmlEntitize(message_body, true)}
`;
if (signature) data += wtvshared.sanitizeSignature(signature);
data += "";
if (attachments) {
var supported_images = /image\/(jpe?g|png|gif|x-wtv-bitmap)/;
var supported_audio = /audio\/(mp[eg|2|3]|midi?|wav|x-wav|mod|x-mod)/;
attachments.forEach((v, k) => {
if (v.content_type) {
if (v.content_type.match(supported_images))
data += `
`;
else if (v.content_type.match(supported_audio))
data += `
${(v.filename) ? (v.filename) : "Audio file"} (${v.content_type.split('/')[1]} attachment)
|
|
`;
else
data += ` | | |