module ShowMessages where

import Database.HDBC
import Text.Regex.Posix

showMessagesWithLink (h:t) =
   showRowWithLink h ++ showMessagesWithLink t
showMessagesWithLink [] = ""

showMessages (h:t) =
    showRow h ++ showMessages t
showMessages [] = ""

showRowWithLink r =
    "<tr>" ++ showEntriesLink r ++ "</tr>"

showRow r =
    "<tr>" ++ showEntries r ++ "</tr>"

showEntriesLink (h:t) =
    showEntryLink h ++ showEntries t
showEntriesLink [] = ""

showEntries (h:t) =
    showEntry h ++ showEntries t
showEntries [] = ""

showEntryLink h =
    "<td>" ++ "<a href=\"show_message.cgi?digest=" ++ (fromSql h) ++ "\">" ++ (firstfew $ fromSql h) ++ "</a></td>"

showEntry h =
    "<td>" ++ fromSql h ++ "</td>"

firstfew :: String -> String
firstfew h = do
    let (before, match, after, groups) = h =~ "^(.{10})" :: (String, String, String, [String])
    match ++ "..."
