Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

# -*- coding: UTF-8 -*- 

# Copyright 2014 Luc Saffre 

# This file is part of the Lino project. 

# Lino 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 3 of the License, or 

# (at your option) any later version. 

# Lino 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 Lino; if not, see <http://www.gnu.org/licenses/>. 

 

""".. management_command:: recmail 

 

``recmail`` stands for "receive mail". Starts a configurable SMTP 

server which forwards incoming mails to your Lino application. For 

every incoming mail it sends a `mail_received` signal.  It is up to 

your application to decide what to with these mails. 

 

If you want to run this as a daemon, you must do:: 

 

  $ pip install python-daemon 

 

""" 

 

from os.path import join 

 

import smtpd 

import asyncore 

 

from django.conf import settings 

from django.core.management.base import CommandError 

from lino.utils import dblogger 

from lino.utils.daemoncommand import DaemonCommand 

 

from lino.modlib.smtpd.signals import mail_received 

 

 

class MySMTPServer(smtpd.SMTPServer): 

 

    def process_message(self, peer, mailfrom, rcpttos, data): 

        rv = mail_received.send( 

            self, peer=peer, mailfrom=mailfrom, rcpttos=rcpttos, data=data) 

        dblogger.info("20140703 process_message %s", rv) 

 

 

# DEFAULT_PORT = 25 

DEFAULT_PORT = 1025 

 

 

def main(*args, **options): 

    if len(args) == 0: 

            addr, port = '127.0.0.1', DEFAULT_PORT 

    elif len(args) != 1: 

        raise CommandError( 

            'Must specify ADDR or ADDR:PORT as argument') 

    else: 

        a = args[0].split(':') 

        if len(a) == 2: 

            addr, port = a 

            port = int(port) 

        elif len(a) == 1: 

            addr = a[0] 

            port = DEFAULT_PORT 

        else: 

            raise CommandError("Invalid ADDR, must be something " 

                               "like 127.0.0.1 or 127.0.0.1:1025") 

    settings.SITE.startup() 

    MySMTPServer((addr, port), None) 

    dblogger.info("20140703 start listening on %s:%d", addr, port) 

    asyncore.loop() 

    dblogger.info("20140703 stopped listening on %s:%d", addr, port) 

 

 

class Command(DaemonCommand): 

 

    help = "Starts a configurable SMTP server which forwards " \ 

           "incoming mails to your Lino application" 

 

    preserve_loggers = [dblogger.logger] 

 

    stdout = join(settings.SITE.project_dir, 'recmail', 'out.log') 

    stderr = join(settings.SITE.project_dir, 'recmail', 'err.log') 

    pidfile = join(settings.SITE.project_dir, 'recmail', 'pid') 

 

    def handle(self, *args, **options): 

        main(*args, **options)