A Haskell News Reposter

Steffen Mazanek Steffen.Mazanek@unibw-muenchen.de
Sat, 17 May 2003 11:16:25 +0200


--HlL+5n6rz5pIUxbD
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Hello,

after a reinstallation of our newsserver we had problems to
import some vitally old articles due to its incompatibility.
So I have written a little Haskell program which converts
news articles into expect scripts, which repost them.
This is really dirty. Is there a Haskell library which allows
direct communication like telnet? It would be quite cool to
have a library which provides e.g. a kind of monad for the
conversation with such servers, which takes care for errors
and the protocol, so that you can roughly write:

do
   s<-connectNews server
   groups<-listOfGroups s
   haskell<-searchFor "haskell" groups
   articles<-listOfArticles haskell 
   article<-searchFor "obfuscated" articles
   reply article (Art "Steffen.Mazanek@UniBw-Muenchen.de" (reSubj article) haskell "These programms are really cool")
   disconnect s

Is there such a library? Can someone provide an example of its usage?

Ciao,
Steffen Mazanek
   


-- 
Haskell: 
that's where I just curry until fail, unwords any error,
drop all undefined, maybe break, otherwise in
sequence span isControl and take max $, id: (d:[])

--HlL+5n6rz5pIUxbD
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="RepostArticle.hs"

module Main (main) where
import System(getArgs)

server = "news.studfb.unibw-muenchen.de"
port = "119"

prefix  = "#!/usr/local/bin/expect\n"++
          "spawn /usr/bin/telnet "++server++' ':port++"\n"++
          "expect 200\n"++
          "send \"POST\\r\"\n"++
          "expect 340\n"
postfix = "\n"++
          "send \"\\r\"\n"++
          "send \".\\r\"\n"++
          "expect 240\n"++
          "send \"q\\r\"\n"


main = getArgs >>= runMain

runMain :: [String] -> IO ()
runMain args = 
  do
    let source = args!!0
    let destination = source++".sh" 
    c<-readFile source 
    let text = prefix ++ (unlines.walkThrough.lines $ c) ++ postfix
    writeFile destination text 

walkThrough::[String]->[String]
walkThrough [] = []
walkThrough (x:xs) | x=="" = map (\x->"send \""++tagSpecialChars x++"\\r\"") (x:xs)
                   | startsWith x "From:" || startsWith x "Newsgroups:" || startsWith x "Subject:" = 
                        ("send \""++handle x++"\\r\""):walkThrough xs
                   | otherwise = walkThrough xs --omit other headers

startsWith x [] = True
startsWith (x:xs) (y:ys) | x == y = startsWith xs ys
                         | otherwise = False

--unfortunately there are some problems with signatures, they are not accepted
--will look at the protocol spec later
tagSpecialChars ('-':'-':xs) = []
--a point represents end of message -> must not occur in the message
tagSpecialChars (".") = ("..")
tagSpecialChars xs = handle xs
--probably some special cases are still missing

--tag brackets
handle [] = []
handle ('[':xs) = "\\["++handle xs
handle (']':xs) = "\\]"++handle xs
handle ('\"':xs) = "\\\""++handle xs
handle (x:xs) = x:handle xs

                         


--HlL+5n6rz5pIUxbD--