const minisrv_service_file = true; const wtvnews = new WTVNews(minisrv_config, service_name); const service_config = minisrv_config.services[service_name]; if (service_config.local_nntp_port && wtvnewsserver) { const 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); const errpage = wtvshared.doErrorPage(400 + " " + e.toString(), 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) { const page_limit_default = 100; wtvnews.connectUsenet().then(() => { wtvnews.selectGroup(group).then((response) => { let limit_per_page = (request_headers.query.limit) ? parseInt(request_headers.query.limit) : page_limit_default; const page = (request_headers.query.chunk) ? parseInt(request_headers.query.chunk) : 0; let page_start = (limit_per_page * page) + 1; let 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) { let NGCount = response.group.number; const NGArticles = response.group.articleNumbers; page_start = (limit_per_page * page) + 1; page_end = (page + 1) * limit_per_page; wtvnews.getHeaderObj(NGArticles).then((messages) => { NGCount = NGArticles.length; 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}
Lobby
Post
Help
Group: ${request_headers.query.group}
` if (NGCount === 0 || isNaN(NGCount)) { 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) { const message = messages[k].article; const has_relation = (messages[k].relation !== null) ? true : false; const date_obj = new Date(Date.parse(message.headers.DATE)); const 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("No such group"); }); }).catch((e) => { // selectGroup error throwError("No such group") }); }).catch((e) => { // connect error throwError(e) }); } async function WebTVShowMessage(group, article) { const theArticle = parseInt(article); wtvnews.connectUsenet().then(() => { wtvnews.selectGroup(group).then((response) => { wtvnews.getArticle(theArticle).then((response) => { wtvnews.quitUsenet(); headers = `200 OK Content-type: text/html wtv-expire-all: wtv-news:/news?group=${group}&article=`; let signature = null; let message_colors = session_data.mailstore.defaultColors; const display_signature = true; // todo make a toggle const message = wtvnews.parseAttachments(response); const message_body = message.text; let attachments = null; let 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)'}
Group
Previous
Next
Next New
Mail to
Respond
Help
  
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)'}


`; let allow_html = false; let body_data = ''; let attachment_data = ''; let 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]; } } const supported_images = /image\/(jpe?g|png|gif|x-wtv-bitmap)/; const 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 += `
A file ${(v.filename) ? `(${v.filename}) ` : ''}that WebTV cannot use, with type ${v.content_type} is attached to this message.` } }); } /* if (message.url) { data += `Included Page: ${wtvshared.htmlEntitize(message.url_title).replace(/'/gi, "'")}`; } */ data += body_data; data += signature_data + "

"; data += attachment_data; data += "

"; sendToClient(socket, headers, data); }).catch((e) => { // no such article const 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
All groups
Help
${(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 += `

${group.name}
` 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) { const errpage = wtvshared.doErrorPage(); headers = errpage[0]; data = errpage[1]; } else { 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); } }