本文整理汇总了Golang中net/textproto.Conn.DotReader方法的典型用法代码示例。如果您正苦于以下问题:Golang Conn.DotReader方法的具体用法?Golang Conn.DotReader怎么用?Golang Conn.DotReader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/textproto.Conn
的用法示例。
在下文中一共展示了Conn.DotReader方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: handleIHave
func handleIHave(args []string, s *session, c *textproto.Conn) error {
if !s.backend.AllowPost() {
return NotWanted
}
// XXX: See if we have it.
article, err := s.backend.GetArticle(nil, args[0])
if article != nil {
return NotWanted
}
c.PrintfLine("335 send it")
article = new(nntp.Article)
article.Header, err = c.ReadMIMEHeader()
if err != nil {
return PostingFailed
}
article.Body = c.DotReader()
err = s.backend.Post(article)
if err != nil {
return err
}
c.PrintfLine("235 article received OK")
return nil
}
示例2: requestArticle
// ask for an article from the remote server
// feed it to the daemon if we get it
func (self *nntpConnection) requestArticle(daemon NNTPDaemon, conn *textproto.Conn, msgid string) (err error) {
log.Println(self.name, "asking for", msgid)
// send command
err = conn.PrintfLine("ARTICLE %s", msgid)
// read response
code, line, err := conn.ReadCodeLine(-1)
if code == 220 {
// awwww yeh we got it
var hdr textproto.MIMEHeader
// read header
hdr, err = conn.ReadMIMEHeader()
if err == nil {
// prepare to read body
dr := conn.DotReader()
// check header and decide if we want this
reason, err := self.checkMIMEHeader(daemon, hdr)
if err == nil {
if len(reason) > 0 {
log.Println(self.name, "discarding", msgid, reason)
// we don't want it, discard
io.Copy(ioutil.Discard, dr)
daemon.database.BanArticle(msgid, reason)
} else {
// yeh we want it open up a file to store it in
f := daemon.store.CreateTempFile(msgid)
if f == nil {
// already being loaded elsewhere
} else {
// write header to file
writeMIMEHeader(f, hdr)
// write article body to file
_, _ = io.Copy(f, dr)
// close file
f.Close()
log.Println(msgid, "obtained via reader from", self.name)
// tell daemon to load article via infeed
daemon.infeed_load <- msgid
}
}
} else {
// error happened while processing
log.Println(self.name, "error happend while processing MIME header", err)
}
} else {
// error happened while reading header
log.Println(self.name, "error happened while reading MIME header", err)
}
} else if code == 430 {
// they don't know it D:
log.Println(msgid, "not known by", self.name)
} else {
// invalid response
log.Println(self.name, "invald response to ARTICLE:", code, line)
}
return
}
示例3: outboundHandshake
// outbound setup, check capabilities and set mode
// returns (supports stream, supports reader) + error
func (self nntpConnection) outboundHandshake(conn *textproto.Conn) (stream, reader bool, err error) {
log.Println(self.name, "outbound handshake")
var code int
var line string
for err == nil {
code, line, err = conn.ReadCodeLine(-1)
log.Println(self.name, line)
if err == nil {
if code == 200 {
// send capabilities
log.Println(self.name, "ask for capabilities")
err = conn.PrintfLine("CAPABILITIES")
if err == nil {
// read response
dr := conn.DotReader()
r := bufio.NewReader(dr)
for {
line, err = r.ReadString('\n')
if err == io.EOF {
// we are at the end of the dotreader
// set err back to nil and break out
err = nil
break
} else if err == nil {
// we got a line
if line == "MODE-READER\n" || line == "READER\n" {
log.Println(self.name, "supports READER")
reader = true
} else if line == "STREAMING\n" {
stream = true
log.Println(self.name, "supports STREAMING")
} else if line == "POSTIHAVESTREAMING\n" {
stream = true
reader = false
log.Println(self.name, "is SRNd")
}
} else {
// we got an error
log.Println("error reading capabilities", err)
break
}
}
// return after reading
return
}
} else if code == 201 {
log.Println("feed", self.name, "does not allow posting")
// we don't do auth yet
break
} else {
continue
}
}
}
return
}
示例4: requestArticle
// ask for an article from the remote server
// feed it to the daemon if we get it
func (self *nntpConnection) requestArticle(daemon *NNTPDaemon, conn *textproto.Conn, msgid string) (err error) {
// send command
err = conn.PrintfLine("ARTICLE %s", msgid)
// read response
code, line, err := conn.ReadCodeLine(-1)
if code == 220 {
// awwww yeh we got it
var hdr textproto.MIMEHeader
// read header
hdr, err = readMIMEHeader(conn.R)
if err == nil {
// prepare to read body
dr := conn.DotReader()
// check header and decide if we want this
reason, ban, err := self.checkMIMEHeaderNoAuth(daemon, hdr)
if err == nil {
if len(reason) > 0 {
log.Println(self.name, "discarding", msgid, reason)
// we don't want it, discard
io.Copy(ioutil.Discard, dr)
if ban {
daemon.database.BanArticle(msgid, reason)
}
} else {
// yeh we want it open up a file to store it in
err = self.storeMessage(daemon, hdr, dr)
if err != nil {
log.Println(self.name, "failed to obtain article", err)
// probably an invalid signature or format
daemon.database.BanArticle(msgid, err.Error())
}
}
} else {
// error happened while processing
log.Println(self.name, "error happend while processing MIME header", err)
}
} else {
// error happened while reading header
log.Println(self.name, "error happened while reading MIME header", err)
}
} else if code == 430 {
// they don't know it D:
} else {
// invalid response
log.Println(self.name, "invald response to ARTICLE:", code, line)
}
return
}
示例5: handlePost
func handlePost(args []string, s *session, c *textproto.Conn) error {
if !s.backend.AllowPost() {
return PostingNotPermitted
}
c.PrintfLine("340 Go ahead")
var err error
var article nntp.Article
article.Header, err = c.ReadMIMEHeader()
if err != nil {
return PostingFailed
}
article.Body = c.DotReader()
err = s.backend.Post(&article)
if err != nil {
return err
}
c.PrintfLine("240 article received OK")
return nil
}
示例6: handleLine
func (self nntpConnection) handleLine(daemon NNTPDaemon, code int, line string, conn *textproto.Conn) (err error) {
parts := strings.Split(line, " ")
var msgid string
if code == 0 && len(parts) > 1 {
msgid = parts[1]
} else {
msgid = parts[0]
}
if code == 238 {
if ValidMessageID(msgid) {
log.Println("sending", msgid, "to", self.name)
// send the article to us
self.take <- msgid
}
} else if code == 239 {
// successful TAKETHIS
log.Println(msgid, "sent via", self.name)
// TODO: remember success
} else if code == 431 {
// CHECK said we would like this article later
log.Println("defer sending", msgid, "to", self.name)
go self.articleDefer(msgid)
} else if code == 439 {
// TAKETHIS failed
log.Println(msgid, "was not sent to", self.name, "denied:", line)
// TODO: remember denial
} else if code == 438 {
// they don't want the article
// TODO: remeber rejection
} else {
// handle command
parts := strings.Split(line, " ")
if len(parts) == 2 {
cmd := parts[0]
if cmd == "MODE" {
if parts[1] == "READER" {
// reader mode
self.mode = "READER"
log.Println(self.name, "switched to reader mode")
conn.PrintfLine("201 No posting Permitted")
} else if parts[1] == "STREAM" {
// wut? we're already in streaming mode
log.Println(self.name, "already in streaming mode")
conn.PrintfLine("203 Streaming enabled brah")
} else {
// invalid
log.Println(self.name, "got invalid mode request", parts[1])
conn.PrintfLine("501 invalid mode variant:", parts[1])
}
} else if cmd == "QUIT" {
// quit command
conn.PrintfLine("")
// close our connection and return
conn.Close()
return
} else if cmd == "CHECK" {
// handle check command
msgid := parts[1]
// have we seen this article?
if daemon.database.HasArticle(msgid) {
// yeh don't want it
conn.PrintfLine("438 %s", msgid)
} else if daemon.database.ArticleBanned(msgid) {
// it's banned we don't want it
conn.PrintfLine("438 %s", msgid)
} else {
// yes we do want it and we don't have it
conn.PrintfLine("238 %s", msgid)
}
} else if cmd == "TAKETHIS" {
// handle takethis command
var hdr textproto.MIMEHeader
var reason string
// read the article header
hdr, err = conn.ReadMIMEHeader()
if err == nil {
// check the header
reason, err = self.checkMIMEHeader(daemon, hdr)
dr := conn.DotReader()
if len(reason) > 0 {
// discard, we do not want
code = 439
log.Println(self.name, "rejected", msgid, reason)
_, err = io.Copy(ioutil.Discard, dr)
err = daemon.database.BanArticle(msgid, reason)
} else {
// check if we don't have the rootpost
reference := hdr.Get("References")
newsgroup := hdr.Get("Newsgroups")
if reference != "" && ValidMessageID(reference) && !daemon.store.HasArticle(reference) && !daemon.database.IsExpired(reference) {
log.Println(self.name, "got reply to", reference, "but we don't have it")
daemon.ask_for_article <- ArticleEntry{reference, newsgroup}
}
f := daemon.store.CreateTempFile(msgid)
if f == nil {
log.Println(self.name, "discarding", msgid, "we are already loading it")
// discard
io.Copy(ioutil.Discard, dr)
} else {
// write header
//.........这里部分代码省略.........
示例7: scrapeServer
// grab every post from the remote server, assumes outbound connection
func (self *nntpConnection) scrapeServer(daemon NNTPDaemon, conn *textproto.Conn) (err error) {
log.Println(self.name, "scrape remote server")
success := true
if success {
// send newsgroups command
err = conn.PrintfLine("NEWSGROUPS %d 000000 GMT", timeNow())
if err == nil {
// read response line
code, _, err := conn.ReadCodeLine(231)
if code == 231 {
var groups []string
// valid response, we expect a multiline
dr := conn.DotReader()
// read lines
sc := bufio.NewScanner(dr)
for sc.Scan() {
line := sc.Text()
idx := strings.Index(line, " ")
if idx > 0 {
groups = append(groups, line[:idx])
} else {
// invalid line? wtf.
log.Println(self.name, "invalid line in newsgroups multiline response:", line)
}
}
err = sc.Err()
if err == nil {
log.Println(self.name, "got list of newsgroups")
// for each group
for _, group := range groups {
var banned bool
// check if the newsgroup is banned
banned, err = daemon.database.NewsgroupBanned(group)
if banned {
// we don't want it
} else if err == nil {
// scrape the group
err = self.scrapeGroup(daemon, conn, group)
if err != nil {
log.Println(self.name, "did not scrape", group, err)
break
}
} else {
// error while checking for ban
log.Println(self.name, "checking for newsgroup ban failed", err)
break
}
}
} else {
// we got a bad multiline block?
log.Println(self.name, "bad multiline response from newsgroups command", err)
}
} else if err == nil {
// invalid response no error
log.Println(self.name, "gave us invalid response to newsgroups command", code)
} else {
// invalid response with error
log.Println(self.name, "error while reading response from newsgroups command", err)
}
} else {
log.Println(self.name, "failed to send newsgroups command", err)
}
} else if err == nil {
// failed to switch mode to reader
log.Println(self.name, "does not do reader mode, bailing scrape")
} else {
// failt to switch mode because of error
log.Println(self.name, "failed to switch to reader mode when scraping", err)
}
return
}
示例8: scrapeGroup
// scrape all posts in a newsgroup
// download ones we do not have
func (self *nntpConnection) scrapeGroup(daemon NNTPDaemon, conn *textproto.Conn, group string) (err error) {
log.Println(self.name, "scrape newsgroup", group)
// send GROUP command
err = conn.PrintfLine("GROUP %s", group)
if err == nil {
// read reply to GROUP command
code := 0
code, _, err = conn.ReadCodeLine(211)
// check code
if code == 211 {
// success
// send XOVER command, dummy parameter for now
err = conn.PrintfLine("XOVER 0")
if err == nil {
// no error sending command, read first line
code, _, err = conn.ReadCodeLine(224)
if code == 224 {
// maps message-id -> references
articles := make(map[string]string)
// successful response, read multiline
dr := conn.DotReader()
sc := bufio.NewScanner(dr)
for sc.Scan() {
line := sc.Text()
parts := strings.Split(line, "\t")
if len(parts) > 5 {
// probably valid line
msgid := parts[4]
// msgid -> reference
articles[msgid] = parts[5]
} else {
// probably not valid line
// ignore
}
}
err = sc.Err()
if err == nil {
// everything went okay when reading multiline
// for each article
for msgid, refid := range articles {
// check the reference
if len(refid) > 0 && ValidMessageID(refid) {
// do we have it?
if daemon.database.HasArticle(refid) {
// we have it don't do anything
} else if daemon.database.ArticleBanned(refid) {
// thread banned
} else {
// we don't got root post and it's not banned, try getting it
err = self.requestArticle(daemon, conn, refid)
if err != nil {
// something bad happened
log.Println(self.name, "failed to obtain root post", refid, err)
return
}
}
}
// check the actual message-id
if len(msgid) > 0 && ValidMessageID(msgid) {
// do we have it?
if daemon.database.HasArticle(msgid) {
// we have it, don't do shit
} else if daemon.database.ArticleBanned(msgid) {
// this article is banned, don't do shit yo
} else {
// we don't have it but we want it
err = self.requestArticle(daemon, conn, msgid)
if err != nil {
// something bad happened
log.Println(self.name, "failed to obtain article", msgid, err)
return
}
}
}
}
} else {
// something bad went down when reading multiline
log.Println(self.name, "failed to read multiline for", group, "XOVER command")
}
}
}
} else if err == nil {
// invalid response code no error
log.Println(self.name, "says they don't have", group, "but they should")
} else {
// error recving response
log.Println(self.name, "error recving response from GROUP command", err)
}
}
return
}
示例9: handleLine
func (self *nntpConnection) handleLine(daemon NNTPDaemon, code int, line string, conn *textproto.Conn) (err error) {
parts := strings.Split(line, " ")
var msgid string
if code == 0 && len(parts) > 1 {
msgid = parts[1]
} else {
msgid = parts[0]
}
if code == 238 {
if ValidMessageID(msgid) {
self.stream <- nntpTAKETHIS(msgid)
}
return
} else if code == 239 {
// successful TAKETHIS
log.Println(msgid, "sent via", self.name)
return
// TODO: remember success
} else if code == 431 {
// CHECK said we would like this article later
log.Println("defer sending", msgid, "to", self.name)
go self.articleDefer(msgid)
} else if code == 439 {
// TAKETHIS failed
log.Println(msgid, "was not sent to", self.name, "denied:", line)
// TODO: remember denial
} else if code == 438 {
// they don't want the article
// TODO: remeber rejection
} else {
// handle command
parts := strings.Split(line, " ")
if len(parts) > 1 {
cmd := parts[0]
if cmd == "MODE" {
if parts[1] == "READER" {
// reader mode
self.mode = "READER"
log.Println(self.name, "switched to reader mode")
conn.PrintfLine("201 No posting Permitted")
} else if parts[1] == "STREAM" {
// wut? we're already in streaming mode
log.Println(self.name, "already in streaming mode")
conn.PrintfLine("203 Streaming enabled brah")
} else {
// invalid
log.Println(self.name, "got invalid mode request", parts[1])
conn.PrintfLine("501 invalid mode variant:", parts[1])
}
} else if cmd == "QUIT" {
// quit command
conn.PrintfLine("")
// close our connection and return
conn.Close()
return
} else if cmd == "CHECK" {
// handle check command
msgid := parts[1]
// have we seen this article?
if daemon.database.HasArticle(msgid) {
// yeh don't want it
conn.PrintfLine("438 %s", msgid)
} else if daemon.database.ArticleBanned(msgid) {
// it's banned we don't want it
conn.PrintfLine("438 %s", msgid)
} else {
// yes we do want it and we don't have it
conn.PrintfLine("238 %s", msgid)
}
} else if cmd == "TAKETHIS" {
// handle takethis command
var hdr textproto.MIMEHeader
var reason string
// read the article header
hdr, err = conn.ReadMIMEHeader()
if err == nil {
// check the header
reason, err = self.checkMIMEHeader(daemon, hdr)
dr := conn.DotReader()
if len(reason) > 0 {
// discard, we do not want
code = 439
log.Println(self.name, "rejected", msgid, reason)
_, err = io.Copy(ioutil.Discard, dr)
err = daemon.database.BanArticle(msgid, reason)
} else {
// check if we don't have the rootpost
reference := hdr.Get("References")
newsgroup := hdr.Get("Newsgroups")
if reference != "" && ValidMessageID(reference) && !daemon.store.HasArticle(reference) && !daemon.database.IsExpired(reference) {
log.Println(self.name, "got reply to", reference, "but we don't have it")
daemon.ask_for_article <- ArticleEntry{reference, newsgroup}
}
f := daemon.store.CreateTempFile(msgid)
if f == nil {
log.Println(self.name, "discarding", msgid, "we are already loading it")
// discard
io.Copy(ioutil.Discard, dr)
} else {
// write header
//.........这里部分代码省略.........
示例10: handleLine
//.........这里部分代码省略.........
conn.PrintfLine("501 error while logging in")
}
}
}
} else {
// wut ?
// wrong legnth of parametrs
}
} else if cmd == "CHECK" {
// handle check command
msgid := parts[1]
if self.mode != "STREAM" {
// we can't we are not in streaming mode
conn.PrintfLine("431 %s", msgid)
return
}
// have we seen this article?
if daemon.database.HasArticle(msgid) {
// yeh don't want it
conn.PrintfLine("438 %s", msgid)
} else if daemon.database.ArticleBanned(msgid) {
// it's banned we don't want it
conn.PrintfLine("438 %s", msgid)
} else {
// yes we do want it and we don't have it
conn.PrintfLine("238 %s", msgid)
}
} else if cmd == "TAKETHIS" {
// handle takethis command
var hdr textproto.MIMEHeader
var reason string
var ban bool
// read the article header
r := bufio.NewReader(conn.DotReader())
hdr, err = readMIMEHeader(r)
if err == nil {
// check the header
reason, ban, err = self.checkMIMEHeader(daemon, hdr)
if len(reason) > 0 {
// discard, we do not want
code = 439
log.Println(self.name, "rejected", msgid, reason)
_, err = io.Copy(ioutil.Discard, r)
if ban {
err = daemon.database.BanArticle(msgid, reason)
}
} else if err == nil {
// check if we don't have the rootpost
reference := hdr.Get("References")
newsgroup := hdr.Get("Newsgroups")
if reference != "" && ValidMessageID(reference) && !daemon.store.HasArticle(reference) && !daemon.database.IsExpired(reference) {
log.Println(self.name, "got reply to", reference, "but we don't have it")
go daemon.askForArticle(ArticleEntry{reference, newsgroup})
}
// store message
err = self.storeMessage(daemon, hdr, r)
if err == nil {
code = 239
reason = "gotten"
} else {
code = 439
reason = err.Error()
}
} else {
// error?
// discard, we do not want
示例11: outboundHandshake
// outbound setup, check capabilities and set mode
// returns (supports stream, supports reader, supports tls) + error
func (self *nntpConnection) outboundHandshake(conn *textproto.Conn, conf *FeedConfig) (stream, reader, tls bool, err error) {
log.Println(self.name, "outbound handshake")
var line string
var code int
for err == nil {
code, line, err = conn.ReadCodeLine(-1)
log.Println(self.name, line)
if err == nil {
if code == 200 {
// send capabilities
log.Println(self.name, "ask for capabilities")
err = conn.PrintfLine("CAPABILITIES")
if err == nil {
// read response
dr := conn.DotReader()
r := bufio.NewReader(dr)
for {
line, err = r.ReadString('\n')
if err == io.EOF {
// we are at the end of the dotreader
// set err back to nil and break out
err = nil
break
} else if err == nil {
if line == "STARTTLS\n" {
log.Println(self.name, "supports STARTTLS")
tls = true
} else if line == "MODE-READER\n" || line == "READER\n" {
log.Println(self.name, "supports READER")
reader = true
} else if line == "STREAMING\n" {
stream = true
log.Println(self.name, "supports STREAMING")
} else if line == "POSTIHAVESTREAMING\n" {
stream = true
reader = false
log.Println(self.name, "is SRNd")
}
} else {
// we got an error
log.Println("error reading capabilities", err)
break
}
}
// return after reading
break
}
} else if code == 201 {
log.Println("feed", self.name, "does not allow posting")
// we don't do auth yet
break
} else {
continue
}
}
}
if conf != nil && len(conf.username) > 0 && len(conf.passwd) > 0 {
log.Println(self.name, "authenticating...")
err = conn.PrintfLine("AUTHINFO USER %s", conf.username)
if err == nil {
var code int
code, line, err = conn.ReadCodeLine(381)
if code == 381 {
err = conn.PrintfLine("AUTHINFO PASS %s", conf.passwd)
if err == nil {
code, line, err = conn.ReadCodeLine(281)
if code == 281 {
log.Println(self.name, "Auth Successful")
} else {
log.Println(self.name, "Auth incorrect", line)
conn.PrintfLine("QUIT")
conn.Close()
return false, false, false, io.EOF
}
}
}
}
}
return
}