nntp

package module
v0.0.0-...-707effb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2021 License: BSD-3-Clause, GooglePatentClause Imports: 14 Imported by: 0

README

nntp.go

An NNTP (news) Client package for go (golang). Forked from nntp.

  • Changed to using net/textproto
  • Added support for compressed XOVER responses

Example

  // connect to news server
  conn, err := nntp.NewTLS("tcp", "news.example.com:563", nil)
  if err != nil {
    log.Fatalf("connection failed: %v", err)
  }

  // auth
  if err := conn.Authenticate("user", "pass"); err != nil {
    log.Fatalf("Could not authenticate")
  }

  // connect to a news group
  grpName := "alt.binaries.pictures"
  group, err := conn.Group(grpName)
  if err != nil {
    log.Fatalf("Could not connect to group %s: %v %d", grpName, err)
  }

  // fetch an article
  id := "<4c1c18ec$0$8490$c3e8da3@news.astraweb.com>"
  article, err := conn.Article(id)
  if err != nil {
    log.Fatalf("Could not fetch article %s: %v", id, err)
  }

  // read the article contents
  body := strings.Join(article.Body,"")

Documentation

Overview

RFC5322 date parsing. Copied from net/mail Go standard library package.

Package nntp implements a client for the news protocol NNTP, as defined in RFC 3977.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Article

type Article struct {
	Header map[string][]string
	Body   []string
}

An Article represents an NNTP article.

func (*Article) String

func (a *Article) String() string

type Conn

type Conn struct {
	Banner string
	// contains filtered or unexported fields
}

A Conn represents a connection to an NNTP server. The connection with an NNTP server is stateful; it keeps track of what group you have selected, if any, and (if you have a group selected) which article is current, next, or previous.

Some methods that return information about a specific message take either a message-id, which is global across all NNTP servers, groups, and messages, or a message-number, which is an integer number that is local to the NNTP session and currently selected group.

For all methods that return an io.Reader (or an *Article, which contains an io.Reader), that io.Reader is only valid until the next call to a method of Conn.

func New

func New(network, addr string) (*Conn, error)

New connects to an NNTP server. The network and addr are passed to net.Dial to make the connection.

Example:

conn, err := nntp.Dial("tcp", "my.news:nntp")

func NewTLS

func NewTLS(net, addr string, cfg *tls.Config) (*Conn, error)

NewTLS connects with TLS

func (*Conn) Article

func (c *Conn) Article(id string) (*Article, error)

Article returns the article named by id as an *Article.

func (*Conn) ArticleText

func (c *Conn) ArticleText(id string) ([]string, error)

ArticleText returns the article named by id as a []string. The article is in plain text format, not NNTP wire format.

func (*Conn) Authenticate

func (c *Conn) Authenticate(username, password string) error

Authenticate logs in to the NNTP server. It only sends the password if the server requires one.

func (*Conn) Body

func (c *Conn) Body(id string) ([]string, error)

Body returns the body for the article named by id as an io.Reader.

func (*Conn) Capabilities

func (c *Conn) Capabilities() ([]string, error)

Capabilities returns a list of features this server performs. Not all servers support capabilities.

func (*Conn) Command

func (c *Conn) Command(cmd string, expectCode int) (int, string, error)

Command sends a low-level command and get a response.

This will return an error if the code doesn't match the expectCode prefix. For example, if you specify "200", the response code MUST be 200 or you'll get an error. If you specify "2", any code from 200 (inclusive) to 300 (exclusive) will be success. An expectCode of -1 disables this behavior.

func (*Conn) Date

func (c *Conn) Date() (time.Time, error)

Date returns the current time on the server. Typically the time is later passed to NewGroups or NewNews.

func (*Conn) Group

func (c *Conn) Group(group string) (*Group, error)

Group changes the current group.

func (*Conn) Head

func (c *Conn) Head(id string) (*Article, error)

Head returns the header for the article named by id as an *Article. The Body field in the Article is nil.

func (*Conn) HeadText

func (c *Conn) HeadText(id string) ([]string, error)

HeadText returns the header for the article named by id as []string. The article is in plain text format, not NNTP wire format.

func (*Conn) Help

func (c *Conn) Help() ([]string, error)

Help returns the server's help text.

func (*Conn) Last

func (c *Conn) Last() (number, msgid string, err error)

Last selects the previous article, returning its message number and id.

func (*Conn) List

func (c *Conn) List(a ...string) ([]string, error)

List returns a list of groups present on the server. Valid forms are:

List() - return active groups
List(keyword) - return different kinds of information about groups
List(keyword, pattern) - filter groups against a glob-like pattern called a wildmat

func (*Conn) ModeReader

func (c *Conn) ModeReader() error

ModeReader switches the NNTP server to "reader" mode, if it is a mode-switching server.

func (*Conn) MultilineCommand

func (c *Conn) MultilineCommand(cmd string, expectCode int) (int, []string, error)

MultilineCommand wraps the functionality to

func (*Conn) NewGroups

func (c *Conn) NewGroups(since time.Time) ([]*Group, error)

NewGroups returns a list of groups added since the given time.

func (*Conn) NewNews

func (c *Conn) NewNews(group string, since time.Time) ([]string, error)

NewNews returns a list of the IDs of articles posted to the given group since the given time.

func (*Conn) Next

func (c *Conn) Next() (number, msgid string, err error)

Next selects the next article, returning its message number and id.

func (*Conn) Overview

func (c *Conn) Overview(begin, end int64) ([]MessageOverview, error)

Overview returns overviews of all messages in the current group with message number between begin and end, inclusive.

func (*Conn) Quit

func (c *Conn) Quit() error

Quit sends the QUIT command and closes the connection to the server.

func (*Conn) RawPost

func (c *Conn) RawPost(r io.Reader) error

RawPost reads a text-formatted article from r and posts it to the server.

func (*Conn) SetCompression

func (c *Conn) SetCompression() error

SetCompression turns on compression for this connection

func (*Conn) Stat

func (c *Conn) Stat(id string) (number, msgid string, err error)

Stat looks up the message with the given id and returns its message number in the current group, and vice versa. The returned message number can be "0" if the current group isn't one of the groups the message was posted to.

type Group

type Group struct {
	Name string
	// High and low message-numbers
	High int64
	Low  int64
	// Estimated count of articles in the group
	Count int64
	// Status indicates if general posting is allowed --
	// typical values are "y", "n", or "m".
	Status string
}

A Group gives information about a single news group on the server.

type MessageOverview

type MessageOverview struct {
	MessageNumber int64     // Message number in the group
	Subject       string    // Subject header value. Empty if the header is missing.
	From          string    // From header value. Empty is the header is missing.
	Date          time.Time // Parsed Date header value. Zero if the header is missing or unparseable.
	MessageID     string    // Message-Id header value. Empty is the header is missing.
	References    []string  // Message-Id's of referenced messages (References header value, split on spaces). Empty if the header is missing.
	Bytes         int       // Message size in bytes, called :bytes metadata item in RFC3977.
	Lines         int       // Message size in lines, called :lines metadata item in RFC3977.
	Extra         []string  // Any additional fields returned by the server.
}

MessageOverview of a message returned by OVER/XOVER command.

func (*MessageOverview) Xref

func (m *MessageOverview) Xref() string

Xref returns the Xref header if set otherwise the empty string.

type ProtocolError

type ProtocolError string

A ProtocolError represents responses from an NNTP server that seem incorrect for NNTP.

func (ProtocolError) Error

func (p ProtocolError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL