summaryrefslogtreecommitdiff
blob: d2f902dd03fe868b3c2d066dc4e1dcab3ccb9da4 (plain)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
"""
Provides a form to send an email to the blog-owner.

If you make any changes to this plugin, please send a patch to 
<sa+pyblosxom at c-area dot ch> so I can incorporate them.
Thanks!


To install:
1) Put contact.py in your plugin directory.
2) In config.py add 'contact' to py['load_plugins']
3) Add the following variables to config.py:
    py['contact_urltrigger'] = "/contact" # optional, this is the default
    py['contact_smtp_server'] = "localhost" # required
    py['contact_smtp_to'] = "you@example.com" # required
4) Optionally create a template named contact.<flavour> in your flavour directory.
   If no contact.<flavour> exists a simple default is used. See _default_template below.


Dependencies:
    - My compatibility plugin if you're not using pyblosxom 1.2+.


$Id: contact.py,v 1.6 2005/08/25 13:21:48 sar Exp $
"""
__author__ = "Steven Armstrong <sa at c-area dot ch>"
__version__ = "$Revision: 1.6 $ $Date: 2005/08/25 13:21:48 $"
__url__ = "http://www.c-area.ch/code/"
__description__ = "Provides a form to send an email to the blog-owner."
__license__ = "GPL 2+"


# Python imports
import urlparse

# Pyblosxom imports
from Pyblosxom.renderers.blosxom import Renderer
from Pyblosxom import tools


# Variables
TRIGGER = "/contact"
TRIGGER_KEY = "contact_urltrigger"
INIT_KEY = "contact_initiated"
MESSAGE_KEY = "contact_error_message"
_form_fields = ['name', 'email', 'subject', 'message']

_default_template = """
<div>
<h2>Contact me</h2>
<div style="display:block;">$contact_error_message</div>
<form name="contactForm" id="contactForm" method="post" action="$base_url$contact_urltrigger">
<div class="contactLine">
  <div class="contactLabel">
    <label class="contactLine" for="name" title="Your name">Name</label>
  </div>
  <input type="text" name="name" id="name" value="$contact_name" /><br />
</div>
<div class="contactLine">
  <div class="contactLabel">
    <label class="contactLine" for="email" title="Your email address">Email</label>
  </div>
  <input type="text" name="email" id="email" value="$contact_email" /><br />
</div>
<div class="contactLine">
  <div class="contactLabel">
    <label class="contactLine" for="subject" title="Subject of your message">Subject</label>
  </div>
  <input class="contactLine" type="text" name="subject" id="subject" value="$contact_subject" /><br />
</div>
<div class="contactText">
  <div class="contactLabel">
    <label class="contactText" for="message" title="Your message">Message</label>
  </div>
  <textarea name="message" id="message" style="height:150px;">$contact_message</textarea><br />
</div>
<div class="contactSubmit">
  <div class="contactButton">
    <input class="contactSubmit" type="submit" value="Send" style="width:auto; margin-right:0;" />
  </div>
  <div class="contactButton">
    <input class="contactSubmit" type="reset" value="Reset" style="width:auto; margin-right:0;" />
  </div>
</div>
</form>
</div>
"""

################################################################################
##
## Helper functions
##
################################################################################

rfc822_specials = '()<>@,;:\\"[]'

def isAddressValid(addr):
    '''
    Taken from

    http://www.secureprogramming.com/?action=view&feature=recipes&recipeid=1

    Posted by Matt Messier on Tue, Sep 02, 2003 (06:19 PM) GMT

    >>> isAddressValid('djfhdfh')
    0
    >>> isAddressValid('djfhdfh@test.com')
    8
    >>> isAddressValid('dj@fhdfh@test.com')
    0
    >>> isAddressValid('dj\@fhdfh@test.com')
    0
    >>> isAddressValid('dj"@"fhdfh@test.com')
    0
    >>> isAddressValid('dj" "fhdfh@test.com')
    0
    >>> isAddressValid('dj\" \"fhdfh@test.com')
    0
    >>> isAddressValid('dj." ".fhdfh@test.com')
    13
    >>> isAddressValid('dj."@ ".fhdfh@test.com')
    14
    >>> isAddressValid('dj."@<> ".fhdfh@test.com')
    16
    >>> isAddressValid('dj."@<>ü ".fhdfh@test.com')
    0
    >>> isAddressValid('dj<>fhdfh@test.com')
    0
    >>> isAddressValid('dj\<\>fhdfh@test.com')
    0
    >>> isAddressValid('dj\ fhdfh@test.com')
    0
    >>> isAddressValid('dj\\ fhdfh@test.com')
    0
    >>> isAddressValid('djfhdfh@test.com.de')
    8
    >>> isAddressValid('djfhdfh@test.co<m.de')
    0
    '''
    # Ported from Recipe 3.9 in Secure Programming Cookbook for C and C++ by
    # John Viega and Matt Messier (O'Reilly 2003)

    # First we validate the name portion (name@domain)
    c = 0
    while c < len(addr):
        if addr[c] == '"' and (not c or addr[c - 1] == '.' or addr[c - 1] == '"'):
            c = c + 1
            while c < len(addr):
                if addr[c] == '"':
                    c = c + 1
                    break
                if addr[c] == '\\' and addr[c + 1] == ' ':
                    c = c + 2
                    continue
                if ord(addr[c]) < 32 or ord(addr[c]) >= 127: return 0
                c = c + 1
            else: return 0
            if addr[c] == '@': break
            if addr[c] != '.': return 0
            c = c + 1
            continue
        if addr[c] == '@': break
        if ord(addr[c]) <= 32 or ord(addr[c]) >= 127: return 0
        if addr[c] in rfc822_specials: return 0
        c = c + 1
    if not c or addr[c - 1] == '.': return 0

    # Next we validate the domain portion (name@domain)
    domain = c = c + 1
    if domain >= len(addr): return 0
    count = 0
    while c < len(addr):
        if addr[c] == '.':
            if c == domain or addr[c - 1] == '.': return 0
            count = count + 1
        if ord(addr[c]) <= 32 or ord(addr[c]) >= 127: return 0
        if addr[c] in rfc822_specials: return 0
        c = c + 1

    ## The final return statement was modified to return the split point
    ## (position of @) so that the email can split in its two subsections.
    if count >= 1:
        return domain

def verify_installation(request):
    config = request.getConfiguration()
    retval = 1

    if not 'contact_urltrigger' in config:
        print "Missing optional property: 'contact_urltrigger'"
        print "Using the default of '%s'" % TRIGGER
    
    _required = ['contact_smtp_server', 'contact_smtp_to']
    for prop in _required:
        if not prop in config:
            print "Missing required property: '%s'" % prop
            retval = 0
    
    return retval



class ContactRenderer(Renderer):
    
    def render(self, header=1):
        config = self._request.getConfiguration()
        data = self._request.getData()
        http = self._request.getHttp()

        # path_info and root_datadir are normaly set after the cb_pathinfo callback.
        # but if a plugin implements cb_handle (as this one does), 
        # cb_pathinfo is never called.
        # if root_datadir is not set and flavourdir is not set in config.py
        # an exception is thrown in BlosxomRenderer._getFlavour.
        # so set root_datadir here explicitly to prevent that.
        if not 'path_info' in data:
            if http.get('PATH_INFO', ''):
                path_info = http['PATH_INFO'].split('/')
            path_info = [x for x in path_info if x != '']
            data['path_info'] = list(path_info)

        if not 'root_datadir' in data:
            data['root_datadir'] = config['datadir']


        # initialize flavour
        self.flavour = self._getFlavour(data.get('flavour', 'html'))
        data['content-type'] = self.flavour['content_type'].strip()
        if header:
            self.addHeader("Status", "200 OK")
            if self._needs_content_type and data['content-type'] != "":
                self.addHeader('Content-type', '%s; charset=%s' % (data['content-type'], config.get('blog_encoding', 'iso-8859-1')))
            self.showHeaders()

        d = {}
        d.update(config)
        d.update(data)

        if 'head' in self.flavour:
            self._outputFlavour(d, 'head')

        if not 'contact' in self.flavour:
            self.flavour['contact'] = _default_template
        self._outputFlavour(d, 'contact')

        if 'foot' in self.flavour: 
            self._outputFlavour(d, 'foot')                
        
        self.rendered = 1



def _update_config(config):
    if TRIGGER_KEY in config:
        global TRIGGER
        TRIGGER = config[TRIGGER_KEY]
    else:
        # set this explicitly so it's available in templates
        config[TRIGGER_KEY] = TRIGGER


def _handle_post(request):
    form = request.getForm()
    data = request.getData()
    http = request.getHttp()
    config = request.getConfiguration()

    email = {}
    error = False
    error_messages = []

    if not 'HTTP_REFERER' in http or \
           not http['HTTP_REFERER'].startswith('://'.join(urlparse.urlsplit(config['base_url'])[0:1])):
        data[MESSAGE_KEY] = "Posting from foreign hosts not allowed.<br />\nUse the form below to send your message."
        return

    for field in _form_fields:
        if not field in form:
            error_messages.append("Missing required field '%s'." % field)
            error = True
        else:
            # strip markup
            parser = tools.Stripper()
            parser.feed(form[field].value)
            email[field] = parser.gettext()

    if 'email' in email.keys() and not isAddressValid(email['email']):
            error = True
	    error_messages.append("Invalid email address '%s'. Cannot deliver your message!" % email['email'])

    if error:
        data[MESSAGE_KEY] = "<br />\n".join(error_messages)
        _remember_email(email, data)
    else:
        success, data[MESSAGE_KEY] = _send_email(email, config)
        if success:
            _forget_email(data)
        else:
            _remember_email(email, data)


def _remember_email(email, data):
    """
    Stores form fields in the data dict so they can be used to 
    populate the form in the template.
    """
    for key in email:
        data["contact_%s" % key] = email[key]


def _forget_email(data):
    """
    Resets/forgets any stored form field values.
    """
    for key in _form_fields:
        key = "contact_%s" % key
        if key in data:
            del data[key]


def _send_email(email, config):
    try:
        try: from email.Utils import formatdate
        except ImportError: from rfc822 import formatdate
        import smtplib
    
        smtp = smtplib.SMTP(config['contact_smtp_server'])

        email['to'] = config["contact_smtp_to"]
        email['date'] = formatdate(localtime=True)

        msg = """\
From: %(name)s <%(email)s>
To: %(to)s
Date: %(date)s
Subject: %(subject)s

%(message)s

""" % email

        smtp.sendmail(
            from_addr=email['email'],
            to_addrs=config['contact_smtp_to'],
            msg=msg
        )
        smtp.quit()
    except:
        if hasattr(tools, "log_exception"):
            tools.log_exception()
        return (False, "Error: Problem sending email.")
    else:
        return (True, "Thanks for feeding my mailbox ;-)")




#******************************
# Callbacks
#******************************

def cb_start(args):
    request = args['request']
    http = request.getHttp()
    config = request.getConfiguration()
    _update_config(config)

    if http['PATH_INFO'].startswith(TRIGGER):
        data = request.getData()
        data[INIT_KEY] = True


def cb_handle(args):
    request = args['request']
    data = request.getData()

    if INIT_KEY in data:
        http = request.getHttp()
        if http['REQUEST_METHOD'].upper() == "POST":
            _handle_post(request)
        else:
            _forget_email(data)

        ContactRenderer(request, request.getResponse()).render()

        return 1


def cb_end(args):
    request = args['request']
    data = request.getData()
    if INIT_KEY in data:
        del data[INIT_KEY]
    if MESSAGE_KEY in data:
        del data[MESSAGE_KEY]