WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Winsock
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Connect to a NNTP server

 Keywords:  

Question:

How to extract a thread from a newsgroup?

I want to extract a complete thread from a newsgroup folder from within Outlook Express.

  • each message into a single textfile?
  • all messages into one textfile?
  • with or without thread indentation?
  • sorted by datetime?
  • sorted by sender?
I need some guiding and concrete help.

Answer:

You could put together a basic script to access a NNTP server via the Winsock extender. (ie TELNET)

Here is an UNDEBUGGED pass at it.

The way that threading works is that each message has a unique id (Message-ID:) and if a message is a reply then the message-id of the parent is listed under References: (in reverse order)

There are a number of other commands but this should get you going for now. If you want to see a bit more about it have a look at RFC977 (and possibly RFC 2980).

AddExtender("WWWSK34i.DLL")
;--------------------------------------------------------------------------------;
;smtpConnect : Connects to a NNTP server                                         ;
;--------------------------------------------------------------------------------;
;server_address   : e-mail address as IP or resolveable name                     ;
;port             : port to connect on                                           ;
;user             : username or "" if not needed                                 ;
;pass             : password or "" if not needed                                 ;
;--------------------------------------------------------------------------------;
;Returns          : socket handle if succesful , False otherwise                 ;
;--------------------------------------------------------------------------------;
#DefineFunction nntpConnect(server_address,port,user,pass)
   If port == "" Then port = "119"
   max_line = 256
   handle = sOpen ()
   If !sConnect (handle, server_address, port) Then Return @FALSE
   line = sRecvLine (handle, max_line)
   If !nntpCheckReturn(line,200) Then Return StrCat(@FALSE,@TAB,line)
   If user <> "" Then
      sSendLine(handle,StrCat("AUTOINFO user ",user))
      line = sRecvLine (handle, max_line)
      If !nntpCheckReturn(line,381) Then Return StrCat(@FALSE,@TAB,line)
      sSendLine(handle,StrCat("AUTOINFO pass ",pass))
      line = sRecvLine (handle, max_line)
      If !nntpCheckReturn(line,281) Then Return StrCat(@FALSE,@TAB,line)
   EndIf
   Return handle
#EndFunction

;--------------------------------------------------------------------------------;
;nntpDisconect : Disconect from a NNTP server                                    ;
;--------------------------------------------------------------------------------;
;handle           : handle returned from nntpConnect                             ;
;force            : true to not try a nice disconect                             ;
;--------------------------------------------------------------------------------;
;Returns          : True if succesful , False otherwise                          ;
;--------------------------------------------------------------------------------;
#DefineFunction nntpDisconect(handle,force)
   If !force Then
      max_line = 256
      sSendLine(handle,"QUIT")
      line = sRecvLine (handle, max_line)
      If !nntpCheckReturn(line,205) Then Return StrCat(@FALSE,@TAB,line)
   EndIf
   Return sClose (handle)
#EndFunction

;--------------------------------------------------------------------------------;
;nntpSelectGroup  : Set current news group on NNTP server                        ;
;--------------------------------------------------------------------------------;
;handle           : handle returned from nntpConnect                             ;
;group            : group name to select                                         ;
;--------------------------------------------------------------------------------;
;Returns          : "210 MsgCount FirstMsg LastMsg GroupName Desc",False otherwise;
;--------------------------------------------------------------------------------;
#DefineFunction nntpSelectGroup(handle,group)
   max_line = 256
   sSendLine(handle,StrCat("group ",group))
   line = sRecvLine (handle, max_line)
   If !nntpCheckReturn(line,211) Then Return StrCat(@FALSE,@TAB,line)
   Return line
#EndFunction

;--------------------------------------------------------------------------------;
;nntpGetMessage   : get one message from the current group                       ;
;--------------------------------------------------------------------------------;
;handle           : handle returned from nntpConnect                             ;
;msg_id           : the artical id for the message to return                     ;
;--------------------------------------------------------------------------------;
;Returns          : True if succesful , False with error details otherwise       ;
;--------------------------------------------------------------------------------;
#DefineFunction nntpGetMessage(handle,msg_id)
   max_line = 512
   sSendLine(handle,StrCat("article ",msg_id))
   line = sRecvLine (handle, max_line)
   If !nntpCheckReturn(line,220) Then Return StrCat(@FALSE,@TAB,line)
   msg = ""
   line = sRecvLine (handle, max_line)
   While line <> "."
      msg = ItemInsert(line,-1,msg,@CR)
      line = sRecvLine (handle, max_line)
   EndWhile
   msg = StrReplace(msg,@CR,@CRLF)
   Return msg
#EndFunction


;--------------------------------------------------------------------------------;
;nntpCheckReturn : Check returned message for expected error number              ;
;--------------------------------------------------------------------------------;
;line             : string returned from e-mail server                           ;
;number           : expected error number                                        ;
;--------------------------------------------------------------------------------;
;Returns          : True if succesful , False otherwise                          ;
;--------------------------------------------------------------------------------;
#DefineFunction nntpCheckReturn(line,number)
   Return ItemExtract(1,line," ") == number
#EndFunction



test = nntpConnect("news.zen.co.uk","","","")

reply = nntpSelectGroup(test,"nz.net.admin")

start_msg = ItemExtract(3,reply," ")
end_msg = ItemExtract(4,reply," ")

For m = start_msg To end_msg
   msg = nntpGetMessage(test,m)
   Message(m,msg)
Next

nntpDisconect(test,0)

Article ID:   W15878
File Created: 2004:03:30:15:41:20
Last Updated: 2004:03:30:15:41:20