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_options = {
ca: this.wtvshared.getServiceDep('wtv-news/localserver_ca.pem'),
key: this.wtvshared.getServiceDep('wtv-news/localserver_key.pem'),
cert: this.wtvshared.getServiceDep('wtv-news/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 '%y", 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 (message_body.indexOf("
${(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 += ` |
|
|
|
| Subject:
| ${(response.article.headers.SUBJECT) ? wtvshared.htmlEntitize(response.article.headers.SUBJECT) : '(No subject)'}
| |
|
|
`;
var allow_html = false;
var body_data = '';
var attachment_data = '';
var signature_data = '';
if (message_body) {
if (message_body.indexOf("") >= 0) {
allow_html = true;
}
body_data += (allow_html) ? wtvshared.sanitizeSignature(message_body) : wtvshared.htmlEntitize(message_body, true)
body_data += `
`;
}
if (signature) signature_data += wtvshared.sanitizeSignature(signature);
if (attachments) {
if (attachments[0]) {
if (attachments[0].filename == "message.html") {
body_data += wtvshared.sanitizeSignature(attachments[0].data);
delete attachments[0];
}
}
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))
attachment_data += `
`;
else if (v.content_type.match(supported_audio))
attachment_data += `
${(v.filename) ? (v.filename) : "Audio file"} (${v.content_type.split('/')[1]} attachment)
|
|
`;
else if (v.content_type.match("text/html"))
attachment_data += wtvshared.sanitizeSignature(v.data);
else
attachment_data += `";
sendToClient(socket, headers, data);
}).catch((e) => {
// no such article
var post_unavailable_file = this.wtvshared.getServiceDep('wtv-news/post-unavailable.html');
console.log(e);
if (fs.existsSync(post_unavailable_file)) {
headers = "200 OK\nContent-type: text/html";
data = fs.readFileSync(post_unavailable_file).toString('ascii').replace("${group}", group).replace("${minisrv_config.config.service_logo}", minisrv_config.config.service_logo).replace("${message_colors.bgcolor}",session_data.mailstore.defaultColors.bgcolor);
sendToClient(socket, headers, data);
} else {
throwError(e);
}
});
}).catch((e) => {
// no such group
throwError(e);
});
}).catch((e) => {
//no connection
throwError(e);
});
}
function WebTVSearchGroups(search) {
wtvnews.connectUsenet().then(() => {
wtvnews.listGroups(search).then((response) => {
wtvnews.quitUsenet();
headers = `200 OK
Content-type: text/html
wtv-expire-all: wtv-news:/news?search=`;
data = `
${(response.length == 0) ? "No " : ""}Discussion groups found
|
|
|
${(response.length == 0) ? "No " : ""}Discussion groups found
|
|
|
|
| `;
if (response.length == 0) {
data += `There are no discussion groups that match your request. Do you want to look for something else?`;
} else {
response.forEach((group) => {
data += `

|
|
|
`
if (group.description) {
data += ` | | | ${group.description}`
}
data += " | ";
});
}
data += `
|
|
|
|
Do you want to look for something else?
|
`;
sendToClient(socket, headers, data);
}).catch((e) => {
// listGroups error
throwError(e);
});
}).catch((e) => {
// no connection
throwError(e);
});
}
if (!wtvnews.client) {
var errpage = doErrorPage();
headers = errpage[0];
data = errpage[1];
} else {
var request_is_async = true;
if (request_headers.query.search) {
WebTVSearchGroups(request_headers.query.search)
} else if (request_headers.query.group) {
if (request_headers.query.article) {
WebTVShowMessage(request_headers.query.group, request_headers.query.article);
} else {
WebTVListGroup(request_headers.query.group);
}
} else {
// redirect to lobby if no understandable queries passed
headers = "300 OK\nLocation: wtv-news:/lobby";
sendToClient(socket, headers, null);
}
}
| | |