config: Replace Config._setup() with Config.setup_html2text()
[rss2email.git] / rss2email / config.py
1 # Copyright (C) 2004-2013 Aaron Swartz
2 #                         Brian Lalor
3 #                         Dean Jackson
4 #                         Dmitry Bogatov <KAction@gnu.org>
5 #                         Erik Hetzner
6 #                         Etienne Millon <me@emillon.org>
7 #                         Joey Hess
8 #                         Lindsey Smith <lindsey.smith@gmail.com>
9 #                         Marcel Ackermann
10 #                         Martin 'Joey' Schulze
11 #                         Matej Cepl
12 #                         W. Trevor King <wking@tremily.us>
13 #
14 # This file is part of rss2email.
15 #
16 # rss2email is free software: you can redistribute it and/or modify it under
17 # the terms of the GNU General Public License as published by the Free Software
18 # Foundation, either version 2 of the License, or (at your option) version 3 of
19 # the License.
20 #
21 # rss2email is distributed in the hope that it will be useful, but WITHOUT ANY
22 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
23 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License along with
26 # rss2email.  If not, see <http://www.gnu.org/licenses/>.
27
28 """Per-user rss2email configuration
29 """
30
31 import collections as _collections
32 import configparser as _configparser
33
34 import html2text as _html2text
35
36
37 class Config (_configparser.ConfigParser):
38     def __init__(self, dict_type=_collections.OrderedDict,
39                  interpolation=_configparser.ExtendedInterpolation(),
40                  **kwargs):
41         super(Config, self).__init__(
42             dict_type=dict_type, interpolation=interpolation, **kwargs)
43
44     def setup_html2text(self, section='DEFAULT'):
45         """Setup html2text globals to match our configuration
46
47         Html2text unfortunately uses globals (instead of keyword
48         arguments) to configure its conversion.
49         """
50         if section not in self:
51             section = 'DEFAULT'
52         _html2text.UNICODE_SNOB = self.getboolean(
53             section, 'unicode-snob')
54         _html2text.LINKS_EACH_PARAGRAPH = self.getboolean(
55             section, 'links-after-each-paragraph')
56         _html2text.BODY_WIDTH = self.getint(section, 'body-width')
57
58
59 CONFIG = Config()
60
61 # setup defaults for feeds that don't customize
62 CONFIG['DEFAULT'] = _collections.OrderedDict((
63         ### Addressing
64         # The email address messages are from by default
65         ('from', 'user@rss2email.invalid'),
66         # Transfer-Encoding. For local mailing it is safe and
67         # convient to use 8bit.
68         ('use-8bit', str(False)),
69         # True: Only use the 'from' address.
70         # False: Use the email address specified by the feed, when possible.
71         ('force-from', str(False)),
72         # True: Use the publisher's email if you can't find the author's.
73         # False: Just use the 'from' email instead.
74         ('use-publisher-email', str(False)),
75         # Only use the feed email address rather than friendly name
76         # plus email address
77         ('friendly-name', str(True)),
78         # Set this to default To email addresses.
79         ('to', ''),
80
81         ### Fetching
82         # Set an HTTP proxy (e.g. 'http://your.proxy.here:8080/')
83         ('proxy', ''),
84         # Set the timeout (in seconds) for feed server response
85         ('feed-timeout', str(60)),
86
87         ### Processing
88         # True: Fetch, process, and email feeds.
89         # False: Don't fetch, process, or email feeds
90         ('active', str(True)),
91         # True: Generate Date header based on item's date, when possible.
92         # False: Generate Date header based on time sent.
93         ('date-header', str(False)),
94         # A comma-delimited list of some combination of
95         # ('issued', 'created', 'modified', 'expired')
96         # expressing ordered list of preference in dates
97         # to use for the Date header of the email.
98         ('date-header-order', 'modified, issued, created, expired'),
99         # Set this to add bonus headers to all emails
100         # Example: bonus-header = 'Approved: joe@bob.org'
101         ('bonus-header', ''),
102         # True: Receive one email per post.
103         # False: Receive an email every time a post changes.
104         ('trust-guid', str(True)),
105         # To most correctly encode emails with international
106         # characters, we iterate through the list below and use the
107         # first character set that works.
108         ('encodings', 'US-ASCII, ISO-8859-1, UTF-8, BIG5, ISO-2022-JP'),
109         # User processing hooks.  Note the space after the module name.
110         # Example: post-process = 'rss2email.post_process.downcase downcase_message'
111         ('post-process', ''),
112         ## HTML conversion
113         # True: Send text/html messages when possible.
114         # False: Convert HTML to plain text.
115         ('html-mail', str(False)),
116         # Optional CSS styling
117         ('use-css', str(False)),
118         ('css', (
119                 'h1 {\n'
120                 '  font: 18pt Georgia, "Times New Roman";\n'
121                 '}\n'
122                 'body {\n'
123                 '  font: 12pt Arial;\n'
124                 '}\n'
125                 'a:link {\n'
126                 '  font: 12pt Arial;\n'
127                 '  font-weight: bold;\n'
128                 '  color: #0000cc;\n'
129                 '}\n'
130                 'blockquote {\n'
131                 '  font-family: monospace;\n'
132                 '}\n'
133                 '.header {\n'
134                 '  background: #e0ecff;\n'
135                 '  border-bottom: solid 4px #c3d9ff;\n'
136                 '  padding: 5px;\n'
137                 '  margin-top: 0px;\n'
138                 '  color: red;\n'
139                 '}\n'
140                 '.header a {\n'
141                 '  font-size: 20px;\n'
142                 '  text-decoration: none;\n'
143                 '}\n'
144                 '.footer {\n'
145                 '  background: #c3d9ff;\n'
146                 '  border-top: solid 4px #c3d9ff;\n'
147                 '  padding: 5px;\n'
148                 '  margin-bottom: 0px;\n'
149                 '}\n'
150                 '#entry {\n'
151                 '  border: solid 4px #c3d9ff;\n'
152                 '}\n'
153                 '#body {\n'
154                 '  margin-left: 5px;\n'
155                 '  margin-right: 5px;\n'
156                 '}\n')),
157         ## html2text options
158         # Use Unicode characters instead of their ascii psuedo-replacements
159         ('unicode-snob', str(False)),
160         # Put the links after each paragraph instead of at the end.
161         ('links-after-each-paragraph', str(False)),
162         # Wrap long lines at position. 0 for no wrapping.
163         ('body-width', str(0)),
164
165         ### Mailing
166         # Select protocol from: sendmail, smtp, imap
167         ('email-protocol', 'sendmail'),
168         # True: Use SMTP_SERVER to send mail.
169         # Sendmail (or compatible) configuration
170         ('sendmail', '/usr/sbin/sendmail'),  # Path to sendmail (or compatible)
171         # SMTP configuration
172         ('smtp-auth', str(False)),      # set to True to use SMTP AUTH
173         ('smtp-username', 'username'),  # username for SMTP AUTH
174         ('smtp-password', 'password'),  # password for SMTP AUTH
175         ('smtp-server', 'smtp.yourisp.net:25'),
176         ('smtp-ssl', str(False)),       # Connect to the SMTP server using SSL
177         # IMAP configuration
178         ('imap-auth', str(False)),      # set to True to use IMAP auth.
179         ('imap-username', 'username'),  # username for IMAP authentication
180         ('imap-password', 'password'),  # password for IMAP authentication
181         ('imap-server', 'imap.yourisp.net'),
182         ('imap-port', str(143)),
183         ('imap-ssl', str(False)),       # connect to the IMAP server using SSL
184         ('imap-mailbox', 'INBOX'),      # where we should store new messages
185
186         ### Miscellaneous
187         # Verbosity (one of 'error', 'warning', 'info', or 'debug').
188         ('verbose', 'warning'),
189         ))