[go: up one dir, main page]

File: EmailPatch.hs

package info (click to toggle)
darcs-monitor 0.3.4-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 124 kB
  • ctags: 56
  • sloc: haskell: 438; makefile: 28
file content (153 lines) | stat: -rw-r--r-- 6,249 bytes parent folder | download
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
{-  darcs-monitor - Darcs repository monitor
    Copyright © 2007 Antti-Juhani Kaijanaho
    Copyright © 2007 Benjamin Franksen

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-}
module EmailPatch (emailPatch,defaultSendmail) where

import Control.Monad.Trans
import System.Directory
import System.Exit
import System.IO
import System.Process

import Config
import Darcs
import PatchData

import Paths_darcs_monitor

defaultSendmail = "/usr/sbin/sendmail"

sendMail :: (MonadConfig m, MonadIO m) =>
            String -> String -> m Bool
sendMail recipient text = do
  sendmail <- asks confSendmailPath
  liftIO $ do (inh, outh, errh, ph) <-
                  runInteractiveProcess sendmail [recipient] Nothing Nothing
              hClose outh
              hPutStr inh text
              hClose inh
              err <- hGetContents errh
              hPutStr stderr err
              ec <- waitForProcess ph
              return (ec == ExitSuccess)

emailPatch :: (MonadConfig m, MonadIO m) =>
              String -> PatchData -> m Bool
emailPatch rec pd = do
  diff <- invokeDarcs ["diff"
                      ,"--match=hash " ++ patchHash pd
                      ,"--repo=" ++ patchRepo pd
                      ,"-u"]
  changes <- invokeDarcs ["changes"
                      ,"--match=hash " ++ patchHash pd
                      ,"--repo=" ++ patchRepo pd
                      ,"-s"]
  menc <- asks confCharset
  met <- asks confEmailTemplate
  et <- liftIO $ case met of
          Just s  -> return s
          Nothing -> do let s = patchRepoDir pd ++ "email-template"
                        ex <- doesFileExist s
                        case ex of
                          True  -> return s
                          False -> getDataFileName "default-template"
  tmpl <- liftIO $ readFile et
  let f hdr ('%':'%':r) = let r' = dropWhile (/= '\n') r
                              r'' = case r' of ('\n':r'') -> r''
                                               _          -> r'
                          in f hdr r''
      f hdr ('%':r) = let (k,('%':r')) = span (/= '%') r
                          he s = if hdr
                                 then headerEncode menc s 
                                 else nhe s
                          nhe = showString
                          repl = case k of
                                   "RECIPIENT" -> nhe rec
                                   "DIFF"      -> he diff
                                   "CHANGES"   -> he changes
                                   "REPO"      -> he (patchRepo pd)
                                   "SHORTREPO" -> he (shortRepo pd)
                                   "AUTHOR"    -> he (patchAuthor pd ++ " ") .
                                                  showChar '<' .
                                                  nhe (patchAuthorEmail pd) .
                                                  showChar '>'
                                   "DATE"      -> nhe (patchDate pd)
                                   "HASH"      -> nhe (patchHash pd)
                                   "TITLE"     -> he (patchTitle pd)
                                   "COMMENT"   -> he (patchComment pd)
                                   _           -> nhe ('%' : k ++ "%")
                      in repl . f hdr r'
      f True ('\n':'\n':r) = showString "\n\n" . f False r
      f hdr (c:r) = showChar c . f hdr r
      f _ [] = id
      text' = f True tmpl
  let text = case menc of
               Just enc -> showString "MIME-Version: 1.0\n\
                                      \Content-Transfer-Encoding: 8bit\n\
                                      \Content-Type: text/plain; charset=" .
                           showString enc .
                           showChar '\n' .
                           text'
               Nothing -> text'
  liftIO $ putStrLn $ "Sending mail to " ++ rec ++ " about " ++ patchTitle pd
  sendMail rec (text "")


headerEncode :: Maybe String -> String -> ShowS
headerEncode menc str = do 
  case menc of
    Nothing  -> showString str
    Just enc -> let ws = words str
                    ws' = map (\s ->
                               if areSafe s
                               then s
                               else "=?" ++
                                    enc ++
                                    "?Q?" ++
                                    qEnc s "" ++
                                    "?="
                              ) ws
                in showString (unwords ws')
        where qEnc [] = id
              qEnc (' ':r) = showChar '_' . qEnc r
              qEnc ('_':r) = showString "=5F" . qEnc r
              qEnc (c:r)
                  | c `elem` safe = showChar c . qEnc r
                  | otherwise            = showChar '=' .
                                           showChar c1 .
                                           showChar c2 .
                                           qEnc r
                  where a = fromEnum c
                        a1 = a `div` 16
                        a2 = a `mod` 16
                        c1 = hds !! a1
                        c2 = hds !! a2
                        hds = "0123456789ABCDEF"


safe = "<>@-+.abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXY0123456789\
       \!\"#$%&\'()*,:;[]\\^_`{}|~/"

areSafe = all (`elem` safe)

shortRepo he 
    | last repo == '/' = reverse $ takeWhile (/= '/') $ tail $ reverse repo
    | '/' `elem` repo  = reverse $ takeWhile (/= '/') $ reverse repo
    | otherwise        = repo
      where repo = patchRepo he