diff --git a/simplenntp/LICENSE b/simplenntp/LICENSE deleted file mode 100644 index e3ade37..0000000 --- a/simplenntp/LICENSE +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright (c) 2009 The nntp-go Authors. All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// Subject to the terms and conditions of this License, Google hereby -// grants to You a perpetual, worldwide, non-exclusive, no-charge, -// royalty-free, irrevocable (except as stated in this section) patent -// license to make, have made, use, offer to sell, sell, import, and -// otherwise transfer this implementation of nntp-go, where such license -// applies only to those patent claims licensable by Google that are -// necessarily infringed by use of this implementation of nntp-go. If You -// institute patent litigation against any entity (including a -// cross-claim or counterclaim in a lawsuit) alleging that this -// implementation of nntp-go or a Contribution incorporated within this -// implementation of nntp-go constitutes direct or contributory patent -// infringement, then any patent licenses granted to You under this -// License for this implementation of nntp-go shall terminate as of the date -// such litigation is filed. diff --git a/simplenntp/simplenntp.go b/simplenntp/simplenntp.go deleted file mode 100644 index fa127ac..0000000 --- a/simplenntp/simplenntp.go +++ /dev/null @@ -1,171 +0,0 @@ -// Copyright (c) 2009 The Go Authors. All rights reserved. -// See the LICENSE file. - -package simplenntp - -import ( - "bufio" - "crypto/tls" - "fmt" - "io" - "net" - "strconv" - "strings" - "time" -) - -// Connection timeout in seconds -var timeout = time.Duration(20) * time.Second - -// A ProtocolError represents responses from an NNTP server -// that seem incorrect for NNTP. -type ProtocolError string -func (p ProtocolError) Error() string { - return string(p) -} - -// An Error represents an error response from an NNTP server. -type Error struct { - Code uint - Msg string -} -func (e Error) Error() string { - return fmt.Sprintf("%03d %s", e.Code, e.Msg) -} - - -type Conn struct { - conn io.WriteCloser - r *bufio.Reader - close bool -} - -func newConn(c net.Conn) (res *Conn, err error) { - res = &Conn{ - conn: c, - r: bufio.NewReaderSize(c, 4096), - } - - _, err = res.r.ReadString('\n') - if err != nil { - return - } - - return -} - -// Dial connects to an NNTP server -func Dial(address string, port int, useTLS bool) (*Conn, error) { - conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", address, port), timeout) - if err != nil { - return nil, err - } - - if useTLS { - // Create and handshake a TLS connection - tlsConn := tls.Client(conn, nil) - err = tlsConn.Handshake() - if err != nil { - return nil, err - } - - return newConn(tlsConn) - } else { - return newConn(conn) - } -} - -// DialTLS connects to an NNTP server and handles TLS scariness -func DialTLS(address string, port int) (*Conn, error) { - conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", address, port), timeout) - if err != nil { - return nil, err - } - - - return newConn(conn) -} - -// cmd executes an NNTP command: -// It sends the command given by the format and arguments, and then -// reads the response line. If expectCode > 0, the status code on the -// response line must match it. 1 digit expectCodes only check the first -// digit of the status code, etc. -func (c *Conn) cmd(expectCode uint, format string, args ...interface{}) (code uint, line string, err error) { - if c.close { - return 0, "", ProtocolError("connection closed") - } - // if c.br != nil { - // if err := c.br.discard(); err != nil { - // return 0, "", err - // } - // c.br = nil - // } - if _, err := fmt.Fprintf(c.conn, format+"\r\n", args...); err != nil { - return 0, "", err - } - line, err = c.r.ReadString('\n') - if err != nil { - return 0, "", err - } - line = strings.TrimSpace(line) - if len(line) < 4 || line[3] != ' ' { - return 0, "", ProtocolError("short response: " + line) - } - i, err := strconv.ParseUint(line[0:3], 10, 0) - if err != nil { - return 0, "", ProtocolError("invalid response code: " + line) - } - code = uint(i) - line = line[4:] - if 1 <= expectCode && expectCode < 10 && code/100 != expectCode || - 10 <= expectCode && expectCode < 100 && code/10 != expectCode || - 100 <= expectCode && expectCode < 1000 && code != expectCode { - err = Error{code, line} - } - return -} - -// Authenticate logs in to the NNTP server. -// It only sends the password if the server requires one. -func (c *Conn) Authenticate(username, password string) error { - code, _, err := c.cmd(2, "AUTHINFO USER %s", username) - if code/100 == 3 { - _, _, err = c.cmd(2, "AUTHINFO PASS %s", password) - } - return err -} - -// Post posts an article -func (c *Conn) Post(p []byte) error { - if _, _, err := c.cmd(3, "POST"); err != nil { - return err - } - - start := 0 - end := len(p) - for { - n, err := c.conn.Write(p[start:end]) - if err != nil { - return err - } - - start += n - if start == end { - break - } - } - - if _, _, err := c.cmd(240, "."); err != nil { - return err - } - return nil -} - -// Quit sends the QUIT command and closes the connection to the server. -func (c *Conn) Quit() error { - _, _, err := c.cmd(0, "QUIT") - c.conn.Close() - c.close = true - return err -}