Re: Downloading messages

new topic     » goto parent     » topic index » view thread      » older message » newer message

Robert Craig wrote:
[snip]

> all messages for the current month are stored in ...

I wrote a tool to convert EUForum archive files into the .MBX format.
Many news readers and email clients can import messages stored
in that format.

procedure Usage(sequence pName)
puts  (1, "(c) 2005 Derek Parnell. Convert a EUforum archive to MBX
    format\n")
    printf(1, "Usage:- %s filename ...\n", {pName})
    puts  (1, "  For each input file, a new file of type '.mbx' is created.\n")
end procedure

function begins(sequence pString, sequence pSubStr)
    if length(pString) < length(pSubStr) then
        return 0
    end if

    if length(pSubStr) = 0 then
        return 0
    end if

    if equal(pString[1..length(pSubStr)], pSubStr) then
        return length(pSubStr) + 1
    else
        return 0
    end if

end function

function format_date(sequence pText)
    -- Returns a sequence of YYYY,MMM,dd,hh,mm,ss
    -- Input is in the form YYYY MMM dd hh:mm

    sequence lRetValue
    integer lPos
    integer lWS

    lRetValue = repeat("", 6)
    lPos = 1
    for i = 1 to length(pText) do
        if pText[i] != ' ' and pText[i] != ':' then
            lRetValue[lPos] &= pText[i]
            lWS = 1
        else
            if lWS = 1 then
                lPos += 1
                lWS = 0
            end if
        end if
    end for

    for i = 4 to 6 do
        if length(lRetValue[i]) = 1 then
            lRetValue[i] = '0' & lRetValue[i]
        end if
    end for
    return lRetValue
end function

constant
    kSpecHdrs = {"X-EUFORUM", "Date", "From"},
    kMessageId = 1,
    kDate = 2,
    kFrom = 3

procedure process_file(sequence pFileName)
    integer lInputFH
    integer lOutputFH
    object lLine
    integer lLineCnt
    sequence lOutName
    integer lPos
    sequence lHeaders
    integer lDoingHdrs
    object lTemp
    sequence lDate

    printf(1, "Processing '%s' ... ", {pFileName})

    lInputFH = open(pFileName, "r")
    if lInputFH = -1 then
        printf(2, "** Unable to open input file.\n", {})
        abort(1)
    end if

    lPos = length(pFileName)
    lOutName = pFileName
    while lPos > 0 do
        if pFileName[lPos] = '.' then
            lOutName = pFileName[1 .. lPos-1]
            exit
        end if
        lPos -= 1
    end while

    lOutName &= ".mbx"
    lOutputFH = open(lOutName, "w")
    if lOutputFH = -1 then
        printf(2, "** Unable to open output file '%s'.\n", {lOutName})
        abort(1)
    end if

    lLineCnt = 0
    lDoingHdrs = 1
    lLine = gets(lInputFH)
    while sequence(lLine) do
        lLineCnt += 1
        if length(lLine) > 0 then
            if lLine[$] = 10 then
                lLine = lLine[1..$-1]
            end if
        end if

        if equal(lLine, "-= B E G I N  =-") then
            lHeaders = repeat("", length(kSpecHdrs))
            lDoingHdrs = 1
        elsif length(lLine) = 0 then
            if lDoingHdrs = 1 then
                lDoingHdrs = 0
                -- Process the headers. --
                lDate = format_date(lHeaders[kDate])

                -- The 'MBX message start line'
                lPos = find('<', lHeaders[kFrom])
                lTemp = lHeaders[kFrom][lPos+1 .. $-1]
                for j = 1 to length(lTemp) do
                    if lTemp[j] = ' ' then
                        lTemp[j] = '.'
                    end if
                end for
                printf(lOutputFH, "From %s %s %s %s:%s:00 %s\n", {lTemp,
                                lDate[2],
                                lDate[3],
                                lDate[4],
                                lDate[5],
                                lDate[1]
                                    })

                -- The message id
                printf(lOutputFH, "Message-ID: <%s>\n", {lHeaders[kMessageId]})

                printf(lOutputFH, "From: %s\n", {lHeaders[kFrom]})
                printf(lOutputFH, "Date: Mon, %s %s %s %s:%s:00 +0000\n",
                            {
                                lDate[3],
                                lDate[2],
                                lDate[1],
                                lDate[4],
                                lDate[5]
                                })

                for j = length(kSpecHdrs)+1 to length(lHeaders) do
                    printf(lOutputFH, "%s\n", {lHeaders[j]})
                end for

                puts(lOutputFH, "\n")

                lHeaders = repeat("", length(kSpecHdrs))
            else
                printf(lOutputFH, "%s\n", {lLine})
            end if
        else
            if lDoingHdrs = 1 then
                for j = 1 to length(kSpecHdrs) do
                    lPos = begins(lLine, kSpecHdrs[j] & ": ")
                    if lPos > 0 then
                        lHeaders[j] = lLine[lPos .. $]
                        exit
                    end if
                    if j = length(kSpecHdrs) then
                        -- Not a special header.
                        lHeaders = append(lHeaders, lLine)
                    end if
                end for


            else
                printf(lOutputFH, "%s\n", {lLine})
            end if
        end if

        lLine = gets(lInputFH)
    end while

    printf(1, "( %d lines)\n", lLineCnt)
    close(lInputFH)
    close(lOutputFH)
end procedure

procedure main(sequence pArgs)
    if length(pArgs) < 3 then
        if equal(pArgs[1], pArgs[2]) then
            Usage(pArgs[1])
        else
            Usage("ex " & pArgs[2])
        end if
        abort(0)
    end if

    for i = 3 to length(pArgs) do
        process_file(pArgs[i])
    end for

end procedure

main(command_line())


-- 
Derek Parnell
Melbourne, Australia
irc://irc.sorcery.net:9000/euphoria

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu