Paste server written in Haskell. Fork of Hpaste, fully freedom and privacy respecting and generally improved. At the time of writing there's an instance at <http://paste.rel4tion.org>.
Clone
HTTPS:
git clone https://vervis.peers.community/repos/aoqmo
SSH:
git clone USERNAME@vervis.peers.community:aoqmo
Branches
Tags
Paste.hs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | {-# LANGUAGE BangPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS -Wall -fno-warn-name-shadowing #-}
-- | Paste model.
module Hpaste.Model.Paste
(getLatestPastes
,getPasteById
,getPrivatePasteById
,createOrUpdate
,deletePaste
,createPaste
,getAnnotations
,getRevisions
,getPaginatedPastes
,countPublicPastes
,generateHints
,getHints
,validNick)
where
import Hpaste.Types
import Hpaste.Model.Announcer
import Hpaste.Model.Spam
import Data.Pagination
import Control.Applicative ((<$>),(<|>))
import Control.Exception as E
import Control.Monad
import Control.Monad.Env
import Control.Monad.IO
import Data.Char
import Data.List (find,intercalate)
import Data.Maybe
import Data.Monoid.Operator ((++))
import Data.Text (Text,unpack,pack)
import qualified Data.Text as T
import Data.Text.IO as T (writeFile)
import Data.Text.Lazy (fromStrict)
import qualified FunBot.ExtEvents as F (Paste (..))
import Language.Haskell.HLint
import Prelude hiding ((++))
import Snap.App
import System.Directory
import System.FilePath
deletePaste :: Integer -> HPModel ()
deletePaste pid = void (exec ["DELETE FROM paste WHERE id = ?"] (Only pid))
-- | Count public pastes.
countPublicPastes :: Maybe String -> HPModel Integer
countPublicPastes mauthor = do
rows <- single ["SELECT COUNT(*)"
,"FROM public_toplevel_paste"
,"WHERE (? IS NULL) OR (author = ?) AND spamrating < ?"]
(mauthor,mauthor,spamMinLevel)
return $ fromMaybe 0 rows
-- | Get the latest pastes.
getLatestPastes :: Maybe ChannelId -> HPModel [Paste]
getLatestPastes channel =
query ["SELECT ",pasteFields
,"FROM public_toplevel_paste"
,"WHERE spamrating < ?"
,"AND channel = ? or ? is null"
,"ORDER BY created DESC"
,"LIMIT 20"]
(spamMinLevel,channel,channel)
-- | Get some paginated pastes.
getPaginatedPastes :: Maybe String -> Pagination -> HPModel (Pagination,[Paste])
getPaginatedPastes mauthor pn@Pagination{..} = do
total <- countPublicPastes mauthor
rows <- query ["SELECT",pasteFields
,"FROM public_toplevel_paste"
,"WHERE (? IS NULL) OR (author = ?) AND spamrating < ?"
,"ORDER BY created DESC"
,"OFFSET " ++ show (max 0 (pnCurrentPage - 1) * pnPerPage)
,"LIMIT " ++ show pnPerPage]
(mauthor,mauthor,spamMinLevel)
return (pn { pnTotal = total },rows)
-- | Get a paste by its id.
getPasteById :: PasteId -> HPModel (Maybe Paste)
getPasteById pid =
listToMaybe <$> query ["SELECT ",pasteFields
,"FROM public_paste"
,"WHERE id = ?"]
(Only pid)
-- | Get a private paste by its id, regardless of any status.
getPrivatePasteById :: PasteId -> HPModel (Maybe Paste)
getPrivatePasteById pid =
listToMaybe <$> query ["SELECT",pasteFields
,"FROM private_paste"
,"WHERE id = ?"]
(Only pid)
-- | Get annotations of a paste.
getAnnotations :: PasteId -> HPModel [Paste]
getAnnotations pid =
query ["SELECT",pasteFields
,"FROM public_paste"
,"WHERE annotation_of = ?"
,"ORDER BY created ASC"]
(Only pid)
-- | Get revisions of a paste.
getRevisions :: PasteId -> HPModel [Paste]
getRevisions pid = do
query ["SELECT",pasteFields
,"FROM public_paste"
,"WHERE revision_of = ? or id = ?"
,"ORDER BY created DESC"]
(pid,pid)
-- | Create a paste, or update an existing one.
createOrUpdate :: [Language] -> [Channel] -> PasteSubmit -> Integer -> Bool -> HPModel (Maybe PasteId)
createOrUpdate langs chans paste@PasteSubmit{..} spamrating public = do
case pasteSubmitId of
Nothing -> createPaste langs chans paste spamrating public
Just pid -> do updatePaste pid paste
return $ Just pid
-- | Create a new paste (possibly annotating an existing one).
createPaste :: [Language] -> [Channel] -> PasteSubmit -> Integer -> Bool -> HPModel (Maybe PasteId)
createPaste langs chans ps@PasteSubmit{..} spamrating public = do
pid <- generatePasteId public
res <- single ["INSERT INTO paste"
,"(id,title,author,content,channel,language,annotation_of,revision_of,spamrating,public)"
,"VALUES"
,"(?,?,?,?,?,?,?,?,?,?)"
,"returning id"]
(pid,pasteSubmitTitle,pasteSubmitAuthor,pasteSubmitPaste
,pasteSubmitChannel,pasteSubmitLanguage,ann_pid,rev_pid,spamrating,public)
when (lang == Just "haskell") $ just res $ createHints ps
just (pasteSubmitChannel >>= lookupChan) $ \chan ->
just res $ \pid -> do
when (spamrating < spamMinLevel && public) $
announcePaste pasteSubmitType (channelName chan) ps pid
return (pasteSubmitId <|> res)
where lookupChan cid = find ((==cid).channelId) chans
lookupLang lid = find ((==lid).languageId) langs
lang = pasteSubmitLanguage >>= (fmap languageName . lookupLang)
just j m = maybe (return ()) m j
ann_pid = case pasteSubmitType of AnnotationOf pid -> Just pid; _ -> Nothing
rev_pid = case pasteSubmitType of RevisionOf pid -> Just pid; _ -> Nothing
-- | Generate a fresh unique paste id.
generatePasteId :: Bool -> HPModel PasteId
generatePasteId public = do
result <- if public
then single ["SELECT NEXTVAL('paste_id_seq')"] ()
else single ["SELECT (RANDOM()*9223372036854775807) :: BIGINT"] ()
case result of
Just pid@(PasteId i) -> do
result <- single ["SELECT TRUE FROM paste WHERE id = ?"] (Only pid)
case result :: Maybe PasteId of
Just pid -> generatePasteId public
_ -> return pid
-- | Create the hints for a paste.
createHints :: PasteSubmit -> PasteId -> HPModel ()
createHints ps pid = do
hints <- generateHintsForPaste ps pid
forM_ hints $ \hint ->
exec ["INSERT INTO hint"
,"(paste,type,content)"
,"VALUES"
,"(?,?,?)"]
(pid
,suggestionSeverity hint
,show hint)
-- | Announce the paste.
announcePaste :: PasteType -> Text -> PasteSubmit -> PasteId -> HPModel ()
announcePaste ptype channel PasteSubmit{..} pid = do
conf <- env modelStateConfig
verb <- getVerb
unless (seemsLikeSpam pasteSubmitTitle || seemsLikeSpam pasteSubmitAuthor) $ do
announcer <- env modelStateAnns
io $ announce announcer $ F.Paste
{ F.pasteAuthor = unpack nick
, F.pasteVerb = unpack verb
, F.pasteTitle = unpack $ pasteSubmitTitle
, F.pasteUrl = unpack $ link conf
, F.pasteChannel = unpack channel
}
where nick | validNick (unpack pasteSubmitAuthor) = pasteSubmitAuthor
| otherwise = "“" ++ pasteSubmitAuthor ++ "”"
link Config{..} = "http://" ++ pack configDomain ++ "/" ++ pid'
pid' = case ptype of
NormalPaste -> showPid pid
AnnotationOf apid -> showPid apid ++ "#a" ++ showPid pid
RevisionOf apid -> showPid apid
getVerb = case ptype of
NormalPaste -> return $ "pasted"
AnnotationOf pid -> do
paste <- getPasteById pid
return $ case paste of
Just Paste{..} -> "annotated “" ++ pasteTitle ++ "” with"
Nothing -> "annotated a paste with"
RevisionOf pid -> do
paste <- getPasteById pid
return $ case paste of
Just Paste{..} -> "revised “" ++ pasteTitle ++ "”:"
Nothing -> "revised a paste:"
showPid (PasteId p) = pack $ show $ (p :: Integer)
seemsLikeSpam = T.isInfixOf "http://"
-- | Is a nickname valid? Digit/letter or one of these: -_/\\;()[]{}?`'
validNick :: String -> Bool
validNick s = first && all ok s && length s > 0 where
ok c = isDigit c || isLetter c || elem c "-_/\\;()[]{}?`'"
first = all (\c -> isDigit c || isLetter c) $ take 1 s
-- | Get hints for a Haskell paste from hlint.
generateHintsForPaste :: PasteSubmit -> PasteId -> HPModel [Suggestion]
generateHintsForPaste PasteSubmit{..} (PasteId pid) = io $
E.catch (generateHints (show pid) pasteSubmitPaste)
(\SomeException{} -> return [])
-- | Get hints for a Haskell paste from hlint.
generateHints :: FilePath -> Text -> IO [Suggestion]
generateHints pid contents = io $ do
tmpdir <- getTemporaryDirectory
let tmp = tmpdir </> pid ++ ".hs"
exists <- doesFileExist tmp
unless exists $ T.writeFile tmp $ contents
!hints <- hlint [tmp,"--quiet","--ignore=Parse error"]
removeFile tmp
return hints
getHints :: PasteId -> HPModel [Hint]
getHints pid =
query ["SELECT type,content"
,"FROM hint"
,"WHERE paste = ?"]
(Only pid)
-- | Update an existing paste.
updatePaste :: PasteId -> PasteSubmit -> HPModel ()
updatePaste pid PasteSubmit{..} = do
_ <- exec (["UPDATE paste"
,"SET"]
++
[intercalate ", " (map set (words fields))]
++
["WHERE id = ?"])
(pasteSubmitTitle
,pasteSubmitAuthor
,pasteSubmitPaste
,pasteSubmitLanguage
,pasteSubmitChannel
,pid)
return ()
where fields = "title author content language channel"
set key = unwords [key,"=","?"]
pasteFields = "id,title,content,author,created,views,language,channel,annotation_of,revision_of"
|