/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - NewScryer: Post and read Usenet news with Scryer Prolog. Copyright (c) 2020-2023 Markus Triska (triska@metalevel.at) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ :- module(newscryer, [connect//1, send//1, send//2, send_phrase//1, command//1, command//2, crlf//0, ok//0, ok//1, slurp//1, done//0, nntp_sequence/1]). :- use_module(library(sockets)). :- use_module(library(dcgs)). :- use_module(library(format)). :- use_module(library(lists)). :- use_module(library(iso_ext)). :- use_module(library(charsio)). :- use_module(library(tls)). /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Communication using the Network News Transfer Protocol (NNTP) consists of a series of requests and responses. First, we define a domain-specific language (DSL) that lets us conveniently describe an NNTP interaction. We will use DCGs to describe NNTP requests and responses using: Name --> Request1, Response1, Request2, Response2, ... . where Request_n is one of: -) connect(Host:Port) Connect to Host:Port via SSL. -) send(FormatString, Args) Send the output of format(FormatString, Args) to the server. -) done Close the connection to the server. and Response_n is: -) ok Succeeds iff the command completed OK, or is OK so far. -) ok(Chars) Like ok, unifying Chars with the response line. -) slurp(File) Output is written to File until CRLF . CRLF is encountered. If File already exists, it is overwritten. The secure streams for communicating with the server are threaded through implicitly. The main interface predicate of this library is: nntp_sequence(+Name) It performs the described sequence of commands. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Building blocks that constitute the basic elements of the DSL. The DCG describes a list with a single element: The encrypted stream. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ stream(S), [S] --> [S]. % connect//1: connect to server % Negotiate TLS connection, obtaining a secure stream for reading/writing. connect(Host:Port), [S] --> { socket_client_open(Host:Port, S0, []), atom_chars(Host, Chars), tls_client_context(Context, [hostname(Chars)]), tls_client_negotiate(Context, S0, S) }. % send//2: Send a string. send(Format, Args) --> stream(S), { phrase(format_(Format, Args), Chars), silent_authinfo(Chars), format(S, "~s", [Chars]), flush_output(S) }. silent_authinfo(Chars) :- ( append("authinfo", _, Chars) -> debug(newscryer, "", []) ; debug(newscryer, "~s", [Chars]) ). ok(Chars) --> stream(S), { get_line_to_chars(S, Chars, []), debug(newscryer, "received: ~s\n", [Chars]), ok_(Chars) }. ok --> ok(_). %debug(newscryer, Str, Args) :- !, format(Str, Args). debug(newscryer, _, _). ok_([X|Xs]) :- ( memberchk(X, "23") -> true ; atom_chars(Atom, [X|Xs]), throw(nntp_error(Atom)) ). slurp(File) --> stream(S), { setup_call_cleanup(open(File, write, Out), slurp_into(S, Out), close(Out)) }. slurp_into(S, Out) :- get_line_to_chars(S, Chars, []), ( Chars == ".\r\n" -> true ; format(Out, "~s", [Chars]), slurp_into(S, Out) ). % done//0: end of session done --> stream(S), { close(S) }. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - For complete encapsulation of streams, we need the interface predicate nntp_sequence/1. It actually performs an NNTP sequence. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ :- meta_predicate(nntp_sequence(2)). nntp_sequence(Name) :- phrase(Name, _, _). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - From here onwards, direct stream IO is completely abstracted away. This means that everything is expressed in terms of the basic building blocks above, and the streams need not be accessed directly. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - First use of the DSL: Express send(Chars) in terms of send(Format, Args) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ send(Cs) --> send("~s", [Cs]). /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - send_phrase//1 sends chars that are described by a DCG nonterminal. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ :- meta_predicate(send_phrase(2, ?, ?)). send_phrase(Goal) --> { phrase(Goal, Cs) }, send("~s", [Cs]). /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Next, define command(Cmd) and command(Cmd, Args) to send the command to the server, followed by \r\n. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ command(Cmd) --> command("~s", [Cmd]). command(Cmd, Args) --> send(Cmd, Args), send_phrase(crlf). crlf --> "\r\n".