aboutsummaryrefslogtreecommitdiffstats
path: root/fetchmailconf
blob: e0493d8d7fec934d7aa33fe46cecbd94dde3cca7 (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
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
<!doctype HTML public "-//W3O//DTD W3 HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Fetchmail Feature List</TITLE>
<link rev=made href="mailto:esr@snark.thyrsus.com">
<meta name="description" content="The fetchmail brag sheet.">
<meta name="keywords" content="fetchmail, POP, POP3, IMAP, IMAP2bis, IMAP4"> 
</HEAD>
<BODY>
<table width="100%" cellpadding=0><tr>
<td width="30%">Back to <a href="index.html">Fetchmail Home Page</a>
<td width="30%" align=center>To <a href="/~esr/sitemap.html">Site Map</a>
<td width="30%" align=right>$Date: 2001/07/25 09:06:23 $
</table>
<HR>

<H1 ALIGN=CENTER>Fetchmail Feature List</H1>

<H2>Since 5.0:</H2>
<UL>
<LI>Has the capability of adding trace information to the Received header to
faciliate mail filtering by mailserver and remote account.

<LI>Fetchmail now has options to handle SSL certificate validation.

<LI>Fetchmail now falls back to delivering via local MDA if it can't
open port 25.

<LI>Support for AUTH=CRAM-MD5 under POP3, a la RFC2195.

<LI>Support for ODMR (On-Demand Mail Relay), RFC 2645 (untested).

<LI>It's now easy to deliver mail to a local LMTP socket.

<LI>The interface option now checks both local and remote interface IPs.

<LI>
The plugin facility has been enhanced; %h and %p options are now
available to pass in the hostname and service port number.

<LI>
Added a dropdelivered option to discard Delivered-To headers. This
addresses a problem with using fetchmail and postfix as a relay inside
a domain; when postfix sees incoming messages with delivered-to
headers looking exactly the same as the ones it adds himself, it
bounces the message.

<LI>
Added --smtpname to set username and domain portion of SMTP "RCPT TO"
command. &gt;fetchmail@mail.julianhaight.com&gt;.

<LI>
Added "from" server's IP address to inserted Received line
&gt;fetchmail@mail.julianhaight.com&lt;.

<LI>
Fetchmail now runs on BeOS, thanks to David Reid &lt;david@jetnet.co.uk&gt;.

<LI>
In IMAP, unseen-message counting and indexing is now done by SEARCH
UNSEEN at the beginning of each poll or re-poll (rather than with
the UNSEEN and RECENT responses and FLAGS queries on individual
messages). This significantly cuts down on traffic to and from the
server, and gives more reliable results.

<LI>
The aka option now matches hostname suffixes, so (for example) saying
`aka netaxs.com' will match not just netaxs.com but also (say)
pop3.netaxs.com and mail.netaxs.com.

<LI>
Fetchmail can optionally use the RFC 2177 IDLE extension on an IMAP 
server that supports it.

<LI>
Fetchmail now recognizes the RFC 2449 extended responses [IN-USE] and
[LOGIN-DELAY].

<LI>
Fetchmail running in daemon mode now restarts itself quietly when the
rc file is touched.

<LI>Following recent court decisions and changes in U.S. federal
regulatory policy, hooks for Secure Sockets Layer (SSL) are now part
of the main fetchmail distribution.  The distribution still contains
no actual cryptographic code.

<LI>NTLM support under IMAP, so fetchmail can query Microsoft Exchange servers.

<LI>Expunge option can now be used to break POP3 retrieval into subsessions. 

<LI>Support for AUTH=CRAM-MD5 under IMAP, a la RFC2195.
</UL>

<H2>Since 4.0:</H2>
<UL>
<LI>The interface and monitor options now work with freeBSD.

<LI> Fetchmail now sends RFC1894-conformant bouncemail on SMTP and LMTP errors.

<LI> Full support for LMTP according to RFC2033.

<LI> True multi-language support using GNU gettext.

<LI> Support for use of HESIOD with Kerberos.

<LI> The --bsmtp option supports recording fetched mail as a BSMTP batch.

<LI> The --limit option can now be used in daemon mode, with oversized-message
notifications being mailed to the calling user.

<LI> 
Configurable support for the <a
href="http://www.demon.net/info/helpdesk/demon_products/mail/sdps-tech.shtml">SDPS
extensions</a> in <a href="http://www.demon.net/">www.demon.net</a>'s
POP3 service.

<LI> There is now an interactive GUI fetchmail configurator, fetchmailconf.

<LI> Code is 64-bit clean and Y2K-safe.

<LI> Automatically decodes armored 7-bit MIME into 8 bits (this can be
suppressed).

<LI> You can specify which SMTP error is recognized as a spam block.

<LI> Support for Kerberos V authentication.

<LI> Support for IMAP-OTP authentication using Craig Metz's patches 
     for UW IMAP.

<LI> Support for IPv6 (using glibc or Craig Metz's inet6-apps library).

<Li> Support for IPSEC (using Craig Metz's inet6-apps library).

<LI> Support for IMAP with RFC1731-conformant GSSAPI authentication.

<LI> Fixed and verified support for Cyrus IMAP server, M$ Exchange,
     and Post Office/NT.

<LI> Support for responding with a one-time password when a POP3 server
     issues an RFC1938-conforming OTP challenge.

<LI> Support for Compuserve's RPA authentication protocol for POP3
     (not compiled in by default, but configurable).
</UL>

<H2>Since 3.0:</H2>
<UL>
<LI> Support for IMAP RFC 1731 authentication with Kerberos v4. 

<LI> Support for multiple-folder retrieval in a single session 
     under IMAP. 

<LI> Following SMTP 571 response to a From line, fetchmail no longer 
     downloads the bodies of spam messages. 

<LI> Support for a `hunt list' of SMTP hosts. 

<LI> Support for ESMTP 8BITMIME and SIZE options. 

<LI> Support for ESMTP ETRN command. 

<LI> The stripcr & forcecr options to explicitly control carriage-return 
     stripping and LF-&gt;CRLF mapping before mail forwarding. 
</UL>

<H2>Since 2.0:</H2>
<UL>
<LI> Support for secure use with ssh. 

<LI> Mailserver passwords can be parsed out of your .netrc file. 

<LI> When forwarding mail via SMTP, fetchmail respects the 571 
     "spam filter" response and discards any mail that triggers it. 

<LI> Transaction and error logging may optionally be done via syslog. 

<LI> (Linux only) Security option to permit fetchmail to poll a host  
     only when a point-to-point link to a particular IP address is
     up. 

<LI> RPOP support (restored; had been removed in 1.8). 
</UL>

<H2>2.0 and earlier versions:</H2>
<UL>
<LI> Support POP2, APOP, RPOP, IMAP2, IMAP2bis, IMAP3, IMAP4, IMAP4rev1. .

<LI> Support for Kerberos V4 user authentication (either MIT or Cygnus). 

<LI> Host is auto-probed for a working server if no protocol is
     specified for the connection.  Thus you don't need to know
     what servers are running on your mail host in advance; the
     verbose option will tell you which one succeeds. 

<LI> Delivery via SMTP to the client machine's port 25.  This
     means the retrieved mail automatically goes to the system
     default MDA as if it were normal sender-initiated SMTP mail. 

<LI> Configurable timeout to detect if server connection is dropped. 

<LI> Support for retrieving and forwarding from multi-drop mailboxes 
     that is guaranteed not to cause mail loops. 

<LI> Large user community -- fetchmail has a large user base (the
     author's beta list includes well over two hundred people).  This 
     means feedback is rapid, bugs get found and fixed rapidly. 

<LI> Carefully written, comprehensive and up-to-date man page describing
     not only modes of operation but also how to diagnose the most
     common kinds of problems and what to do about deficient servers.

<LI> Rugged, simple, and well-tested code -- the author relies on it
     every day and it has never lost mail, not even in experimental
     versions.  (In the project's entire history there has only been
     one recorded instance of lost mail, and that was due to a quirk
     in some Microsoft code.)

<LI> Strict conformance to relevant RFCs and good debugging options.
     You could use fetchmail to test and debug server implementatations.

<LI> For anybody who cares, fetchmail is Y2K safe.
</UL>

<H2>Features in common with other remote-mail retrieval programs:</H2>

The other programs I have checked include fetchpop1.9, PopTart-0.9.3,
get-mail, gwpop, pimp-1.0, pop-perl5-1.2, popc, popmail-1.6 and upop.

<UL>
<LI> Support for POP3.

<LI> Easy control via command line or free-format run control file.

<LI> Daemon mode -- fetchmail can be run in background to poll 
     one or more hosts at a specified interval.

<LI> From:, To:, Cc:, and Reply-To: headers are rewritten so that 
     usernames relative to the fetchmail host become fully-qualified
     Internet addresses.  This enables replies to work correctly.
     (Would be unique to fetchmail if I hadn't added it to fetchpop.)

<LI> Message and header processing are 8-bit clean.
</UL>

<HR>
<table width="100%" cellpadding=0><tr>
<td width="30%">Back to <a href="index.html">Fetchmail Home Page</a>
<td width="30%" align=center>To <a href="/~esr/sitemap.html">Site Map</a>
<td width="30%" align=right>$Date: 2001/07/25 09:06:23 $
</table>

<P><ADDRESS>Eric S. Raymond <A HREF="mailto:esr@thyrsus.com">&lt;esr@snark.thyrsus.com&gt;</A></ADDRESS>
</BODY>
</HTML>
#n1061'>1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
#!/usr/bin/env python
#
# A GUI configurator for generating fetchmail configuration files.
# by Eric S. Raymond, <esr@snark.thyrsus.com>.
# Requires Python with Tkinter, and the following OS-dependent services:
#	posix, posixpath, socket
version = "1.11"

from Tkinter import *
from Dialog import *
import sys, time, os, string, socket, getopt

#
# Define the data structures the GUIs will be tossing around
#
class Configuration:
    def __init__(self):
	self.poll_interval = 0		# Normally, run in foreground
	self.syslog = FALSE		# Use syslogd for logging?
	self.logfile = None		# No logfile, initially
	self.idfile = os.environ["HOME"] + "/.fetchids"		# Default idfile, initially
        self.postmaster = None		# No last-resort address, initially
	self.invisible = FALSE		# Suppress Received line & spoof?
	self.servers = []		# List of included sites
	Configuration.typemap = (
	    ('poll_interval',	'Int'),
	    ('syslog',		'Boolean'),
	    ('logfile',		'String'),
	    ('idfile',		'String'),
	    ('postmaster',	'String'),
	    ('invisible',	'Boolean'))

    def __repr__(self):
	str = "";
	if self.syslog != ConfigurationDefaults.syslog:
	   str = str + ("set syslog\n")
	elif self.logfile:
	    str = str + ("set logfile \"%s\"\n" % (self.logfile,));
	if self.idfile != ConfigurationDefaults.idfile:
	    str = str + ("set idfile \"%s\"\n" % (self.idfile,));
	if self.postmaster != ConfigurationDefaults.postmaster:
	    str = str + ("set postmaster \"%s\"\n" % (self.postmaster,));
	if self.poll_interval > 0:
	    str = str + "set daemon " + `self.poll_interval` + "\n"
	for site in self.servers:
	    str = str + repr(site)
	return str

    def __str__(self):
	return "[Configuration: " + repr(self) + "]"

class Server:
    def __init__(self):
	self.pollname = None		# Poll label
	self.via = None			# True name of host
	self.active = TRUE		# Poll status
	self.interval = 0		# Skip interval
	self.protocol = 'auto'		# Default to auto protocol
	self.port = 0			# Port number to use
	self.uidl = FALSE		# Don't use RFC1725 UIDLs by default
	self.auth = 'password'		# Default to password authentication
	self.timeout = 300		# 5-minute timeout
	self.envelope = 'Received'	# Envelope-address header
	self.envskip = 0		# Number of envelope headers to skip
	self.qvirtual = None		# Name prefix to strip
	self.aka = []			# List of DNS aka names
	self.dns = TRUE			# Enable DNS lookup on multidrop
	self.localdomains = []		# Domains to be considered local
	self.interface = None		# IP address and range
	self.monitor = None		# IP address and range
	self.plugin = None		# Plugin command for going to server
	self.plugout = None		# Plugin command for going to listener
	self.netsec = None		# IPV6 security options
	self.users = []			# List of user entries for site
	Server.typemap = (
	    ('pollname',  'String'),
	    ('via',       'String'),
	    ('active',    'Boolean'),
	    ('interval',  'Int'),
	    ('protocol',  'String'),
	    ('interval',  'Int'),
	    ('port',      'Int'),
	    ('uidl',      'Boolean'),
	    ('auth',      'String'),
	    ('timeout',   'Int'),
	    ('envelope',  'String'),
	    ('envskip',   'Int'),
	    ('qvirtual',  'String'),
	    # leave aka out
	    ('dns',       'Boolean'),
	    # leave localdomains out
	    ('interface', 'String'),
	    ('monitor',   'String'),
	    ('plugin',   'String'),
	    ('plugout',  'String'),
	    ('netsec',   'String'))

    def dump(self, folded):
	str = ""
	if self.active:   str = str + "poll"
	else:             str = str + "skip"
	str = str + (" " + self.pollname)
	if self.via:
	    str = str + (" via \"%s\"\n" % (self.via,));
	if self.protocol != ServerDefaults.protocol:
	    str = str + " with proto " + self.protocol 
	if self.port != defaultports[self.protocol] and self.port != 0:
	    str = str + " port " + `self.port`
	if self.timeout != ServerDefaults.timeout:
	    str = str + " timeout " + `self.timeout`
	if self.interval != ServerDefaults.interval: 
	    str = str + " interval " + `self.interval`
	if self.envelope != ServerDefaults.envelope or self.envskip != ServerDefaults.envskip:
	    if self.envskip:
		str = str + " envelope " + self.envskip + " " + self.envelope
	    else:
		str = str + " envelope " + self.envelope
	if self.qvirtual:
	    str = str + (" qvirtual \"%s\"\n" % (self.qvirtual,));
	if self.auth != ServerDefaults.auth:
	    str = str + " auth " + self.auth
	if self.dns != ServerDefaults.dns or self.uidl != ServerDefaults.uidl:
	    str = str + " and options"
	if self.dns != ServerDefaults.dns:
	    str = str + flag2str(self.dns, 'dns')
	if self.uidl != ServerDefaults.uidl:
	    str = str + flag2str(self.uidl, 'uidl')
	if folded:        str = str + "\n    "
	else:             str = str + " "

	if self.aka:
	     str = str + "aka"
	     for x in self.aka:
		str = str + " " + x
	if self.aka and self.localdomains: str = str + " "
	if self.localdomains:
	     str = str + ("localdomains")
	     for x in self.localdomains:
		str = str + " " + x
        if (self.aka or self.localdomains):
	    if folded:
		str = str + "\n    "
	    else:
		str = str + " "

	if self.interface:
            str = str + "interface \"" + self.interface + "\""
	if self.monitor:
            str = str + "monitor \"" + self.monitor + "\""
	if self.netsec:
            str = str + "netsec \"" + self.netsec + "\""
	if self.interface or self.monitor or self.netsec:
	    if folded:
		str = str + "\n"

	if str[-1] == " ": str = str[0:-1]

	for user in self.users:
	    str = str + repr(user)
	str = str + "\n"
	return str;

    def __repr__(self):
	return self.dump(TRUE)

    def __str__(self):
	return "[Server: " + self.dump(FALSE) + "]"

class User:
    def __init__(self):
        if os.environ.has_key("USER"):
            self.remote = os.environ["USER"]	# Remote username
        elif os.environ.has_key("LOGNAME"):
            self.remote = os.environ["LOGNAME"]
        else:
            print "Can't get your username!"
            sys.exit(1)
	self.remote = os.environ["USER"]# Remote username
	self.localnames = [self.remote,]# Local names
	self.password = None		# Password for mail account access
	self.mailboxes = []		# Remote folders to retrieve from
	self.smtphunt = []		# Hosts to forward to
	self.smtpaddress = None		# Append this to MAIL FROM line
	self.preconnect = None		# Connection setup
	self.postconnect = None		# Connection wrapup
	self.mda = None			# Mail Delivery Agent
	self.bsmtp = None		# BSMTP output file
        self.lmtp = FALSE		# Use LMTP rather than SMTP?
	self.antispam = "571 550 501"	# Listener's spam-block code
	self.keep = FALSE		# Keep messages
	self.flush = FALSE		# Flush messages
	self.fetchall = FALSE		# Fetch old messages
	self.rewrite = TRUE		# Rewrite message headers
	self.forcecr = FALSE		# Force LF -> CR/LF
	self.stripcr = FALSE		# Strip CR
	self.pass8bits = FALSE		# Force BODY=7BIT
	self.mimedecode = FALSE		# Undo MIME armoring
	self.dropstatus = FALSE		# Drop incoming Status lines
	self.limit = 0			# Message size limit
        self.warnings = 0		# Size warning interval
	self.fetchlimit = 0		# Max messages fetched per batch
	self.batchlimit = 0		# Max message forwarded per batch
	self.expunge = 1		# Interval between expunges (IMAP)
        self.properties = None		# Extension properties
	User.typemap = (
	    ('remote',      'String'),
	    # leave out mailboxes and localnames
	    ('password',    'String'),
            # Leave out smtphunt
	    ('smtpaddress', 'String'),
	    ('preconnect',  'String'),
	    ('postconnect', 'String'),
	    ('mda',         'String'),
	    ('bsmtp',       'String'),
            ('lmtp',        'Boolean'),
	    ('antispam',    'String'),
	    ('keep',        'Boolean'),
	    ('flush',       'Boolean'),
	    ('fetchall',    'Boolean'),
	    ('rewrite',     'Boolean'),
	    ('forcecr',     'Boolean'),
	    ('stripcr',     'Boolean'),
	    ('pass8bits',   'Boolean'),
	    ('mimedecode',  'Boolean'),
	    ('dropstatus',  'Boolean'),
	    ('limit',       'Int'),
	    ('warnings',    'Int'),
	    ('fetchlimit',  'Int'),
	    ('batchlimit',  'Int'),
	    ('expunge',     'Int'),
            ('properties',   'String'))

    def __repr__(self):
	str = "    "
	str = str + "user \"" + self.remote + "\" there ";
	if self.password:
            str = str + "with password \"" + self.password + '" '
	if self.localnames:
            str = str + "is"
            for x in self.localnames:
		str = str + " " + x
            str = str + " here"
	if (self.keep != UserDefaults.keep
		or self.flush != UserDefaults.flush
		or self.fetchall != UserDefaults.fetchall
		or self.rewrite != UserDefaults.rewrite 
		or self.forcecr != UserDefaults.forcecr 
		or self.stripcr != UserDefaults.stripcr 
		or self.pass8bits != UserDefaults.pass8bits
		or self.mimedecode != UserDefaults.mimedecode
		or self.dropstatus != UserDefaults.dropstatus):
	    str = str + " options"
	if self.keep != UserDefaults.keep:
	    str = str + flag2str(self.keep, 'keep')
	if self.flush != UserDefaults.flush:
	    str = str + flag2str(self.flush, 'flush')
	if self.fetchall != UserDefaults.fetchall:
	    str = str + flag2str(self.fetchall, 'fetchall')
	if self.rewrite != UserDefaults.rewrite:
	    str = str + flag2str(self.rewrite, 'rewrite')
	if self.forcecr != UserDefaults.forcecr:
	    str = str + flag2str(self.forcecr, 'forcecr')
	if self.stripcr != UserDefaults.stripcr:
	    str = str + flag2str(self.stripcr, 'stripcr')
	if self.pass8bits != UserDefaults.pass8bits:
	    str = str + flag2str(self.pass8bits, 'pass8bits')
	if self.mimedecode != UserDefaults.mimedecode:
	    str = str + flag2str(self.mimedecode, 'mimedecode')
	if self.dropstatus != UserDefaults.dropstatus:
	    str = str + flag2str(self.dropstatus, 'dropstatus')
	if self.limit != UserDefaults.limit:
	    str = str + " limit " + `self.limit`
	if self.warnings != UserDefaults.warnings:
	    str = str + " warnings " + `self.warnings`
	if self.fetchlimit != UserDefaults.fetchlimit:
	    str = str + " fetchlimit " + `self.fetchlimit`
	if self.batchlimit != UserDefaults.batchlimit:
	    str = str + " batchlimit " + `self.batchlimit`
	if self.expunge != UserDefaults.expunge:
	    str = str + " expunge " + `self.expunge`
        str = str + "\n"
        trimmed = self.smtphunt;
        if trimmed != [] and trimmed[len(trimmed) - 1] == "localhost":
            trimmed = trimmed[0:len(trimmed) - 1]
        if trimmed != [] and trimmed[len(trimmed) - 1] == hostname:
            trimmed = trimmed[0:len(trimmed) - 1]
        if trimmed != []:
            str = str + "    smtphost "
            for x in trimmed:
                str = str + " " + x
                str = str + "\n"
	if self.mailboxes:
	     str = str + "    folder"
	     for x in self.mailboxes:
		str = str + " " + x
	     str = str + "\n"
        for fld in ('smtpaddress', 'preconnect', 'postconnect', 'mda', 'bsmtp', 'properties'):
            if getattr(self, fld):
                str = str + " %s %s\n" % (fld, `getattr(self, fld)`)
	if self.lmtp != UserDefaults.lmtp:
	    str = str + flag2str(self.lmtp, 'lmtp')
        if self.antispam != UserDefaults.antispam:
            str = str + "    antispam " + self.antispam + "\n"
	return str;

    def __str__(self):
	return "[User: " + repr(self) + "]"

#
# Helper code
#

defaultports = {"auto":0,
    		"POP2":109, 
		"POP3":110,
                "APOP":110,
                "KPOP":1109,
                "IMAP":143,
		"IMAP-GSS":143,
		"IMAP-K4":143,
		"ETRN":25}

authlist = ("password", "kerberos")

listboxhelp = {
    'title' : 'List Selection Help',
    'banner': 'List Selection',
    'text' : """
You must select an item in the list box (by clicking on it). 
"""}

def flag2str(value, string):
# make a string representation of a .fetchmailrc flag or negated flag
    str = ""
    if value != None:
	str = str + (" ")
	if value == FALSE: str = str + ("no ")
	str = str + string;
    return str

class LabeledEntry(Frame):
# widget consisting of entry field with caption to left
    def bind(self, key, action):
	self.E.bind(key, action)
    def focus_set(self):
	self.E.focus_set()
    def __init__(self, Master, text, textvar, lwidth, ewidth=12):
	Frame.__init__(self, Master)
	self.L = Label(self, {'text':text, 'width':lwidth, 'anchor':'w'})
	self.E = Entry(self, {'textvar':textvar, 'width':ewidth})
	self.L.pack({'side':'left'})
	self.E.pack({'side':'left', 'expand':'1', 'fill':'x'})

def ButtonBar(frame, legend, ref, alternatives, depth, command):
# array of radio buttons, caption to left, picking from a string list
    bar = Frame(frame)
    width = len(alternatives) / depth;
    Label(bar, text=legend).pack(side=LEFT)
    for column in range(width):
	subframe = Frame(bar)
	for row in range(depth):
	    ind = width * row + column
	    Radiobutton(subframe,
			{'text':alternatives[ind], 
			 'variable':ref,
			 'value':alternatives[ind],
			 'command':command}).pack(side=TOP, anchor=W)
	subframe.pack(side=LEFT)
    bar.pack(side=TOP);
    return bar

def helpwin(helpdict):
# help message window with a self-destruct button
    helpwin = Toplevel()
    helpwin.title(helpdict['title']) 
    helpwin.iconname(helpdict['title'])
    Label(helpwin, text=helpdict['banner']).pack()
    textwin = Message(helpwin, text=helpdict['text'], width=600)
    textwin.pack()
    Button(helpwin, text='Done', 
	   command=lambda x=helpwin: Widget.destroy(x),
	   relief=SUNKEN, bd=2).pack()

def make_icon_window(base, image):
    try:
        # Some older pythons will error out on this
        icon_image = PhotoImage(data=image)
        icon_window = Toplevel()
        Label(icon_window, image=icon_image, bg='black').pack()
        base.master.iconwindow(icon_window)
	# Avoid TkInter brain death. PhotoImage objects go out of
        # scope when the enclosing function returns.  Therefore
        # we have to explicitly link them to something.
        base.keepalive.append(icon_image)
    except:
        pass

class ListEdit(Frame):
# edit a list of values (duplicates not allowed) with a supplied editor hook 
    def __init__(self, newlegend, list, editor, master, helptxt):
	self.editor = editor
	self.list = list

	# Set up a widget to accept new elements
	self.newval = StringVar(master)
	newwin = LabeledEntry(master, newlegend, self.newval, '12')
	newwin.bind('<Double-1>', self.handleNew)
	newwin.bind('<Return>', self.handleNew)
	newwin.pack(side=TOP, fill=X, anchor=E)

	# Edit the existing list
	listframe = Frame(master)
	scroll = Scrollbar(listframe)
	self.listwidget = Listbox(listframe, height=0, selectmode='browse')
        if self.list:
            for x in self.list:
                self.listwidget.insert(END, x)
	listframe.pack(side=TOP, expand=YES, fill=BOTH)
	self.listwidget.config(yscrollcommand=scroll.set, relief=SUNKEN)
	self.listwidget.pack(side=LEFT, expand=YES, fill=BOTH)
	scroll.config(command=self.listwidget.yview, relief=SUNKEN)
	scroll.pack(side=RIGHT, fill=BOTH)
	self.listwidget.config(selectmode=SINGLE, setgrid=TRUE)
	self.listwidget.bind('<Double-1>', self.handleList);
	self.listwidget.bind('<Return>', self.handleList);

	bf = Frame(master);
	if self.editor:
	    Button(bf, text='Edit',   command=self.editItem).pack(side=LEFT)
	Button(bf, text='Delete', command=self.deleteItem).pack(side=LEFT)
	if helptxt:
	    self.helptxt = helptxt
	    Button(bf, text='Help', fg='blue',
		   command=self.help).pack(side=RIGHT)
	bf.pack(fill=X)

    def help(self):
	helpwin(self.helptxt)

    def handleList(self, event):
	self.editItem();

    def handleNew(self, event):
	item = self.newval.get()
	entire = self.listwidget.get(0, self.listwidget.index('end'));
	if item and (not entire) or (not item in self.listwidget.get(0, self.listwidget.index('end'))):
	    self.listwidget.insert('end', item)
	    if self.list != None: self.list.append(item)
	self.newval.set('')

    def editItem(self):
	select = self.listwidget.curselection()
	if not select:
	    helpwin(listboxhelp)
	else:
	    index = select[0]
	    if index and self.editor:
		label = self.listwidget.get(index);
		apply(self.editor, (label,))

    def deleteItem(self):
	select = self.listwidget.curselection()
	if not select:
	    helpwin(listboxhelp)
	else:
            index = string.atoi(select[0])
            self.listwidget.delete(index)
            if self.list != None: del self.list[index]

def ConfirmQuit(frame, context):
    ans = Dialog(frame, 
		 title = 'Quit?',
		 text = 'Really quit ' + context + ' without saving?',
		 bitmap = 'question',
		 strings = ('Yes', 'No'),
		 default = 1)
    return ans.num == 0

def dispose_window(master, legend, help):
    dispose = Frame(master, relief=RAISED, bd=5)
    Label(dispose, text=legend).pack(side=TOP,pady=10)
    Button(dispose, text='Save', fg='blue',
           command=master.save).pack(side=LEFT)
    Button(dispose, text='Quit', fg='blue',
           command=master.nosave).pack(side=LEFT)
    Button(dispose, text='Help', fg='blue',
           command=lambda x=help: helpwin(x)).pack(side=RIGHT)
    dispose.pack(fill=X)
    return dispose

class MyWidget:
# Common methods for Tkinter widgets -- deals with Tkinter declaration
    def post(self, widgetclass, field):
	for x in widgetclass.typemap:
	    if x[1] == 'Boolean':
		setattr(self, x[0], BooleanVar(self))
	    elif x[1] == 'String':
		setattr(self, x[0], StringVar(self))
	    elif x[1] == 'Int':
		setattr(self, x[0], IntVar(self))
	    source = getattr(getattr(self, field), x[0])
            if source:
                getattr(self, x[0]).set(source)

    def fetch(self, widgetclass, field):
	for x in widgetclass.typemap:
	    setattr(getattr(self, field), x[0], getattr(self, x[0]).get())

#
# First, code to set the global fetchmail run controls.
#

configure_novice_help = {
    'title' : 'Fetchmail novice configurator help',
    'banner': 'Novice configurator help',
    'text' : """
In the `Novice Configurator Controls' panel, you can:

Press `Save' to save the new fetchmail configuration you have created.
Press `Quit' to exit without saving.
Press `Help' to bring up this help message.

In the `Novice Configuration' panels, you will set up the basic data
needed to create a simple fetchmail setup.  These include:

1. The name of the remote site you want to query.

2. Your login name on that site.

3. Your password on that site.

4. A protocol to use (POP, IMAP, ETRN, etc.)

5. A polling interval.

6. Options to fetch old messages as well as new, uor to suppress
   deletion of fetched message.

The novice-configuration code will assume that you want to forward mail
to a local sendmail listener with no special options.
"""}

configure_expert_help = {
    'title' : 'Fetchmail expert configurator help',
    'banner': 'Expert configurator help',
    'text' : """
In the `Expert Configurator Controls' panel, you can:

Press `Save' to save the new fetchmail configuration you have edited.
Press `Quit' to exit without saving.
Press `Help' to bring up this help message.

In the `Run Controls' panel, you can set the following options that
control how fetchmail runs:

Poll interval
        Number of seconds to wait between polls in the background.
        If zero, fetchmail will run in foreground.

Logfile
        If empty, emit progress and error messages to stderr.
        Otherwise this gives the name of the files to write to.
        This field is ignored if the "Log to syslog?" option is on.

Idfile
        If empty, store seen-message IDs in .fetchids under user's home
        directory.  If nonempty, use given file name.

Postmaster
        Who to send multidrop mail to as a last resort if no address can
        be matched.  Normally empty; in this case, fetchmail treats the
        invoking user as the address of last resort unless that user is
        root.  If that user is root, fetchmail sends to `postmaster'.

Invisible
        If false (the default) fetchmail generates a Received line into
        each message and generates a HELO from the machine it is running on.
        If true, fetchmail generates no Received line and HELOs as if it were
        the remote site.

In the `Remote Mail Configurations' panel, you can:

1. Enter the name of a new remote mail server you want fetchmail to query.

To do this, simply enter a label for the poll configuration in the
`New Server:' box.  The label should be a DNS name of the server (unless
you are using ssh or some other tunneling method and will fill in the `via'
option on the site configuration screen).

2. Change the configuration of an existing site.

To do this, find the site's label in the listbox and double-click it.
This will take you to a site configuration dialogue.
"""}


class ConfigurationEdit(Frame, MyWidget):
    def __init__(self, configuration, outfile, master=None):
        self.subwidgets = {}
	self.configuration = configuration
        self.outfile = outfile
        self.container = master
        ConfigurationEdit.mode_to_help = {
            'novice':configure_novice_help, 'expert':configure_expert_help
            }

    def server_edit(self, sitename):
         self.subwidgets[sitename] = ServerEdit(sitename, self).edit(self.mode, Toplevel())

    def edit(self, mode):
        self.mode = mode
	Frame.__init__(self, self.container)
	self.master.title('fetchmail ' + self.mode + ' configurator');
	self.master.iconname('fetchmail ' + self.mode + ' configurator');
        self.keepalive = []	# Use this to anchor the PhotoImage object
        make_icon_window(self, fetchmail_gif)
	Pack.config(self)
        self.post(Configuration, 'configuration')

	dispose_window(self,
                       'Configurator ' + self.mode + ' Controls',
                       ConfigurationEdit.mode_to_help[self.mode])

	gf = Frame(self, relief=RAISED, bd = 5)
	Label(gf,
		text='Fetchmail Run Controls', 
		bd=2).pack(side=TOP, pady=10)

        df = Frame(gf)

        ff = Frame(df)
        if self.mode != 'novice':
            # Set the postmaster
            log = LabeledEntry(ff, '     Postmaster:', self.postmaster, '14')
            log.pack(side=RIGHT, anchor=E)
        # Set the poll interval
        de = LabeledEntry(ff, '     Poll interval:', self.poll_interval, '14')
        de.pack(side=RIGHT, anchor=E)
        ff.pack()

        df.pack()

        if self.mode != 'novice':
            sf = Frame(gf)
            Checkbutton(sf,
		{'text':'Log to syslog?',
		'variable':self.syslog,
		'relief':GROOVE}).pack(side=LEFT, anchor=W)
            log = LabeledEntry(sf, '     Logfile:', self.logfile, '14')
            log.pack(side=RIGHT, anchor=E)
            sf.pack(fill=X)

            Checkbutton(gf,
		{'text':'Invisible mode?',
		'variable':self.invisible,
                 'relief':GROOVE}).pack(side=LEFT, anchor=W)
            # Set the idfile
            log = LabeledEntry(gf, '     Idfile:', self.idfile, '14')
            log.pack(side=RIGHT, anchor=E)

	gf.pack(fill=X)

        # Expert mode allows us to edit multiple sites
	lf = Frame(self, relief=RAISED, bd=5)
	Label(lf,
	      text='Remote Mail Server Configurations', 
	      bd=2).pack(side=TOP, pady=10)
	ListEdit('New Server:', 
		map(lambda x: x.pollname, self.configuration.servers),
		lambda site, self=self: self.server_edit(site),
                lf, remotehelp)
	lf.pack(fill=X)

    def destruct(self):
        for sitename in self.subwidgets.keys():
            self.subwidgets[sitename].destruct()        
        self.quit()

    def nosave(self):
	if ConfirmQuit(self, self.mode + " configuration editor"):
	    self.destruct()

    def save(self):
        for sitename in self.subwidgets.keys():
            self.subwidgets[sitename].save()
        self.fetch(Configuration, 'configuration')
        fm = None
        if not self.outfile:
            fm = sys.stdout
        elif not os.path.isfile(self.outfile) or Dialog(self, 
		 title = 'Overwrite existing run control file?',
		 text = 'Really overwrite existing run control file?',
		 bitmap = 'question',
		 strings = ('Yes', 'No'),
		 default = 1).num == 0:
            fm = open(self.outfile, 'w')
        if fm:
            fm.write("# Configuration created %s by fetchmailconf\n" % time.ctime(time.time()))
            fm.write(`self.configuration`)
            if self.outfile:
                fm.close()
            if fm != sys.stdout:
                os.chmod(self.outfile, 0600)
            self.destruct()

#
# Server editing stuff.
#
remotehelp = {
    'title' : 'Remote site help',
    'banner': 'Remote sites',
    'text' : """
When you add a site name to the list here, 
you initialize an entry telling fetchmail
how to poll a new site.

When you select a sitename (by double-
clicking it, or by single-clicking to
select and then clicking the Edit button),
you will open a window to configure that
site.
"""}

serverhelp = {
    'title' : 'Server options help',
    'banner': 'Server Options',
    'text' : """
The server options screen controls fetchmail 
options that apply to one of your mailservers.

Once you have a mailserver configuration set
up as you like it, you can select `Save' to
store it in the server list maintained in
the main configuration window.

If you wish to discard changes to a server 
configuration, select `Quit'.
"""}

controlhelp = {
    'title' : 'Run Control help',
    'banner': 'Run Controls',
    'text' : """
If the `Poll normally' checkbox is on, the host is polled as part of
the normal operation of fetchmail when it is run with no arguments.
If it is off, fetchmail will only query this host when it is given as
a command-line argument.

The `True name of server' box should specify the actual DNS name
to query. By default this is the same as the poll name.

Normally each host described in the file is queried once each 
poll cycle. If `Cycles to skip between polls' is greater than 0,
that's the number of poll cycles that are skipped between the
times this post is actually polled.

The `Server timeout' is the number of seconds fetchmail will wait
for a reply from the mailserver before concluding it is hung and
giving up.
"""}

protohelp = {
    'title' : 'Protocol and Port help',
    'banner': 'Protocol and Port',
    'text' : """
These options control the remote-mail protocol
and TCP/IP service port used to query this
server.

If you click the `Probe for supported protocols'
button, fetchmail will try to find you the most
capable server on the selected host (this will
only work if you're conncted to the Internet).
The probe only checks for ordinary IMAP and POP
protocols; fortunately these are the most
frequently supported.

The `Protocol' button bar offers you a choice of
all the different protocols available.  The `auto'
protocol is the default mode; it probes the host
ports for POP3 and IMAP to see if either is
available.

Normally the TCP/IP service port to use is 
dictated by the protocol choice.  The `Port'
field (only present in expert mode) lets you
set a non-standard port.
"""}

sechelp = {
    'title' : 'Security option help',
    'banner': 'Security',
    'text' : """
The `interface' option allows you to specify a range
of IP addresses to monitor for activity.  If these
addresses are not active, fetchmail will not poll.
Specifying this may protect you from a spoofing attack
if your client machine has more than one IP gateway
address and some of the gateways are to insecure nets.

The `monitor' option, if given, specifies the only
device through which fetchmail is permitted to connect
to servers.  This option may be used to prevent
fetchmail from triggering an expensive dial-out if the
interface is not already active.

The `interface' and `monitor' options are available
only for Linux systems.  See the fetchmail manual page
for details on these.

The `netsec' option will be configurable only if fetchmail
was compiled with IPV6 support.  If you need to use it,
you probably know what to do.
"""}

multihelp = {
    'title' : 'Multidrop option help',
    'banner': 'Multidrop',
    'text' : """
These options are only useful with multidrop mode.
See the manual page for extended discussion.
"""}

suserhelp = {
    'title' : 'User list help',
    'banner': 'User list',
    'text' : """
When you add a user name to the list here, 
you initialize an entry telling fetchmail
to poll the site on behalf of the new user.

When you select a username (by double-
clicking it, or by single-clicking to
select and then clicking the Edit button),
you will open a window to configure the
user's options on that site.
"""}

class ServerEdit(Frame, MyWidget):
    def __init__(self, host, parent):
        self.parent = parent
	self.server = None
        self.subwidgets = {}
	for site in parent.configuration.servers:
	    if site.pollname == host:
		self.server = site
	if (self.server == None):
		self.server = Server()
		self.server.pollname = host
		self.server.via = None
		parent.configuration.servers.append(self.server)

    def edit(self, mode, master=None):
	Frame.__init__(self, master)
	Pack.config(self)
	self.master.title('Fetchmail host ' + self.server.pollname);
	self.master.iconname('Fetchmail host ' + self.server.pollname);
	self.post(Server, 'server')
	self.makeWidgets(self.server.pollname, mode)
        self.keepalive = []	# Use this to anchor the PhotoImage object
        make_icon_window(self, fetchmail_gif)
#	self.grab_set()
#	self.focus_set()
#	self.wait_window()
	return self

    def destruct(self):
        for username in self.subwidgets.keys():
            self.subwidgets[username].destruct()        
        del self.parent.subwidgets[self.server.pollname]
        Widget.destroy(self.master)

    def nosave(self):
	if ConfirmQuit(self, 'server option editing'):
	    self.destruct()

    def save(self):
	self.fetch(Server, 'server')
        for username in self.subwidgets.keys():
            self.subwidgets[username].save()        
	self.destruct()

    def refreshPort(self):
	proto = self.protocol.get()
	self.port.set(defaultports[proto])
	if not proto in ("POP3", "APOP", "KPOP"): self.uidl.state = DISABLED 

    def user_edit(self, username, mode):
        self.subwidgets[username] = UserEdit(username, self).edit(mode, Toplevel())

    def makeWidgets(self, host, mode):
	topwin = dispose_window(self, "Server options for querying " + host, serverhelp)

	leftwin = Frame(self);
	leftwidth = '25';

        if mode != 'novice':
            ctlwin = Frame(leftwin, relief=RAISED, bd=5)
            Label(ctlwin, text="Run Controls").pack(side=TOP)
            Checkbutton(ctlwin, text='Poll ' + host + ' normally?', variable=self.active).pack(side=TOP)
            LabeledEntry(ctlwin, 'True name of ' + host + ':',
		      self.via, leftwidth).pack(side=TOP, fill=X)
            LabeledEntry(ctlwin, 'Cycles to skip between polls:',
		      self.interval, leftwidth).pack(side=TOP, fill=X)
            LabeledEntry(ctlwin, 'Server timeout (seconds):',
		      self.timeout, leftwidth).pack(side=TOP, fill=X)
            Button(ctlwin, text='Help', fg='blue',
	       command=lambda: helpwin(controlhelp)).pack(side=RIGHT)
            ctlwin.pack(fill=X)

        # Compute the available protocols from the compile-time options
        protolist = ['auto']
        if 'pop2' in feature_options:
            protolist.append("POP2")
        if 'pop3' in feature_options:
            protolist = protolist + ["POP3", "APOP", "KPOP"]
        if 'sdps' in feature_options:
            protolist.append("SDPS")
        if 'imap' in feature_options:
            protolist.append("IMAP")
        if 'imap-gss' in feature_options:
            protolist.append("IMAP-GSS")
        if 'imap-k4' in feature_options:
            protolist.append("IMAP-K4")
        if 'etrn' in feature_options:
            protolist.append("ETRN")

	protwin = Frame(leftwin, relief=RAISED, bd=5)
	Label(protwin, text="Protocol").pack(side=TOP)
	ButtonBar(protwin, '',
                  self.protocol, protolist, 2,
                  self.refreshPort) 
        if mode != 'novice':
            LabeledEntry(protwin, 'On server TCP/IP port:',
		      self.port, leftwidth).pack(side=TOP, fill=X)
            self.refreshPort()
            Checkbutton(protwin,
		text="POP3: track `seen' with client-side UIDLs?",
		variable=self.uidl).pack(side=TOP)   
	Button(protwin, text='Probe for supported protocols', fg='blue',
	       command=self.autoprobe).pack(side=LEFT)
	Button(protwin, text='Help', fg='blue',
	       command=lambda: helpwin(protohelp)).pack(side=RIGHT)
	protwin.pack(fill=X)

	userwin = Frame(leftwin, relief=RAISED, bd=5)
	Label(userwin, text="User entries for " + host).pack(side=TOP)
	ListEdit("New user: ",
                 map(lambda x: x.remote, self.server.users),
                 lambda u, m=mode, s=self: s.user_edit(u, m),
                 userwin, suserhelp)
	userwin.pack(fill=X)

	leftwin.pack(side=LEFT, anchor=N, fill=X);

        if mode != 'novice':
            rightwin = Frame(self);

            mdropwin = Frame(rightwin, relief=RAISED, bd=5)
            Label(mdropwin, text="Multidrop options").pack(side=TOP)
            LabeledEntry(mdropwin, 'Envelope address header:',
		      self.envelope, '22').pack(side=TOP, fill=X)
            LabeledEntry(mdropwin, 'Envelope headers to skip:',
		      self.envskip, '22').pack(side=TOP, fill=X)
            LabeledEntry(mdropwin, 'Name prefix to strip:',
		      self.qvirtual, '22').pack(side=TOP, fill=X)
            Checkbutton(mdropwin, text="Enable multidrop DNS lookup?",
		    variable=self.dns).pack(side=TOP)
            Label(mdropwin, text="DNS aliases").pack(side=TOP)
            ListEdit("New alias: ", self.server.aka, None, mdropwin, None)
            Label(mdropwin, text="Domains to be considered local").pack(side=TOP)
            ListEdit("New domain: ",
		 self.server.localdomains, None, mdropwin, multihelp)
            mdropwin.pack(fill=X)

            if os_type == 'linux' or 'netsec' in feature_options:
                secwin = Frame(rightwin, relief=RAISED, bd=5)
                Label(secwin, text="Security").pack(side=TOP)
                # Don't actually let users set this.  KPOP sets it implicitly
                #	ButtonBar(secwin, 'Authorization mode:',
                #		  self.auth, authlist, 1, None).pack(side=TOP)
                if os_type == 'linux' or 'interface' in dictmembers:
                    LabeledEntry(secwin, 'IP range to check before poll:',
			 self.interface, leftwidth).pack(side=TOP, fill=X)
                if os_type == 'linux' or 'monitor' in dictmembers:
                    LabeledEntry(secwin, 'Interface to monitor:',
			 self.monitor, leftwidth).pack(side=TOP, fill=X)
                if 'netsec' in feature_options or 'netsec' in dictmembers:
                    LabeledEntry(secwin, 'IPV6 security options:',
			 self.netsec, leftwidth).pack(side=TOP, fill=X)
                Button(secwin, text='Help', fg='blue',
                       command=lambda: helpwin(sechelp)).pack(side=RIGHT)
                secwin.pack(fill=X)

            rightwin.pack(side=LEFT, anchor=N);

    def autoprobe(self):
        # Note: this only handles case (1) near fetchmail.c:892
        # We're assuming people smart enough to set up ssh tunneling
        # won't need autoprobing.
        if self.server.via != None:
            realhost = self.server.via
        else:
            realhost = self.server.pollname
        greetline = None
        for (protocol, port) in (("IMAP",143), ("POP3",110), ("POP2",109)):
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            try:
                sock.connect(realhost, port)
                greetline = sock.recv(1024)
                sock.close()
            except:
                pass
            else:
                break
        confwin = Toplevel()
        if greetline == None:
            title = "Autoprobe of " + realhost + " failed"
            confirm = """
Fetchmailconf didn't find any mailservers active.
This could mean the host doesn't support any,
or that your Internet connection is down, or
that the host is so slow that the probe timed
out before getting a response.
"""
        else:
            warnings = ''
            # OK, now try to recognize potential problems

            if protocol == "POP2":
                warnings = warnings + """
It appears you have somehow found a mailserver running only POP2.
Congratulations.  Have you considered a career in archaeology?

Unfortunately, stock fetchmail binaries don't include POP2 support anymore.
Unless the first line of your fetchmail -V output includes the string "POP2",
you'll have to build it from sources yourself with the configure
switch --enable-POP2.

"""
            if string.find(greetline, "1.003") > 0 or string.find(greetline, "1.004") > 0:
                warnings = warnings + """
This appears to be an old version of the UC Davis POP server.  These are
dangerously unreliable (among other problems, they may drop your mailbox
on the floor if your connection is interrupted during the session).

It is strongly recommended that you find a better POP3 server.  The fetchmail
FAQ includes pointers to good ones.

"""
            if string.find(greetline, "usa.net") > 0:
                warnings = warnings + """
You appear to be using USA.NET's free mail service.  Their POP3 servers
(at least as of the 2.2 version in use mid-1998) are quite flaky, but
fetchmail can compensate.  They seem to require that fetchall be switched on
(otherwise you won't necessarily see all your mail, not even new mail).
They also botch the TOP command the fetchmail normally uses for retrieval
(it only retrieves about 10 lines rather than the number specified).
Turning on fetchall will disable the use of TOP.

Therefore, it is strongly recommended that you turn on `fetchall' on all
user entries associated with this server.  

"""

# Steve VanDevender <stevev@efn.org> writes:
# The only system I have seen this happen with is cucipop-1.31
# under SunOS 4.1.4.  cucipop-1.31 runs fine on at least Solaris
# 2.x and probably quite a few other systems.  It appears to be a
# bug or bad interaction with the SunOS realloc() -- it turns out
# that internally cucipop does allocate a certain data structure in
# multiples of 16, using realloc() to bump it up to the next
# multiple if it needs more.
# 
# The distinctive symptom is that when there are 16 messages in the
# inbox, you can RETR and DELE all 16 messages successfully, but on
# QUIT cucipop returns something like "-ERR Error locking your
# mailbox" and aborts without updating it.
# 
# The cucipop banner looks like:
# 
# +OK Cubic Circle's v1.31 1998/05/13 POP3 ready <6229000062f95036@wakko>
#
            if string.find(greetline, "Cubic Circle") > 0:
                warnings = warnings + """
I see your server is running cucipop.  Better make sure the server box
isn't a SunOS 4.1.4 machine; cucipop tickles a bug in SunOS realloc()
under that version, and doesn't cope with the result gracefully.  Newer
SunOS and Solaris machines run cucipop OK.
"""
            if string.find(greetline, "QPOP") > 0:
                warnings = warnings + """
This appears to be a version of Eudora qpopper.  That's good.  Fetchmail
knows all about qpopper.

"""
            closebrak = string.find(greetline, ">")
            if  closebrak > 0 and greetline[closebrak+1] == "\r":
                warnings = warnings + """
It looks like you could use APOP on this server and avoid sending it your
password in clear.  You should talk to the mailserver administrator about
this.

"""
            if string.find(greetline, "csi.com") > 0:
                warnings = warnings + """
It appears you're talking to CompuServe.  You can use their special RPA
service for authentication, but only if your fetchmail -V output's first
line contains the string "RPA".  This is not included in stock fetchmail
binaries; to compile it in, rebuild from sources with the configure
option --enable-RPA.
"""
            if string.find(greetline, "IMAP2bis") > 0:
                warnings = warnings + """
IMAP2bis servers have a minor problem; they can't peek at messages without
marking them seen.  If you take a line hit during the retrieval, the 
interrupted message may get left on the server, marked seen.

To work around this, it is recommended that you set the `fetchall'
option on all user entries associated with this server, so any stuck
mail will be retrieved next time around.

"""
            if string.find(greetline, "IMAP4rev1") > 0:
                warnings = warnings + """
I see an IMAP4rev1 server.  Excellent.  This is (a) the best kind of
remote-mail server, and (b) the one the fetchmail author uses.  Fetchmail
has therefore been extremely well tested with this class of server.
"""
            if warnings == '':
                warnings = warnings + """
Fetchmail doesn't know anything special about this server type.
"""

            # Display success window with warnings
            title = "Autoprobe of " + realhost + " succeeded"
            confirm = "The " + protocol + " server said:\n\n" + greetline + warnings
            self.protocol.set(protocol)
        confwin.title(title) 
        confwin.iconname(title)
        Label(confwin, text=title).pack()
        Message(confwin, text=confirm, width=600).pack()
        Button(confwin, text='Done', 
                   command=lambda x=confwin: Widget.destroy(x),
                   relief=SUNKEN, bd=2).pack()
        
#
# User editing stuff
#

userhelp = {
    'title' : 'User option help',
    'banner': 'User options',
    'text' : """
You may use this panel to set options
that may differ between individual
users on your site.

Once you have a user configuration set
up as you like it, you can select `Save' to
store it in the server list maintained in
the main configuration window.

If you wish to discard the changes you have
made to user options, select `Quit'.
"""}

localhelp = {
    'title' : 'Local name help',
    'banner': 'Local names',
    'text' : """
The local name(s) in a user entry are the
people on the client machine who should
receive mail from the poll described.

Note: if a user entry has more than one
local name, messages will be retrieved
in multidrop mode.  This complicates
the configuration issues; see the manual
page section on multidrop mode.
"""}

class UserEdit(Frame, MyWidget):
    def __init__(self, username, parent):
        self.parent = parent
	self.user = None
	for user in server.users:
	    if user.remote == username:
		self.user = user
	if self.user == None:
	    self.user = User()
	    self.user.remote = username
	    self.user.localnames = [username]
	    server.users.append(self.user)

    def edit(self, mode, master=None):
	Frame.__init__(self, master)
	Pack.config(self)
	self.master.title('Fetchmail user ' + self.user.remote
                          + ' querying ' + parent.server.pollname);
	self.master.iconname('Fetchmail user ' + self.user.remote);
	self.post(User, 'user')
	self.makeWidgets(mode, parent.server.pollname)
        self.keepalive = []	# Use this to anchor the PhotoImage object
        make_icon_window(self, fetchmail_gif)
#	self.grab_set()
#	self.focus_set()
#	self.wait_window()
	return self

    def destruct(self):
        del self.parent.subwidgets[self.user.remote]
        Widget.destroy(self.master)

    def nosave(self):
	if ConfirmQuit(self, 'user option editing'):
            self.destruct()

    def save(self):
	self.fetch(User, 'user')
	self.destruct()

    def makeWidgets(self, mode, servername):
	dispose_window(self,
			"User options for " + self.user.remote + " querying " + servername,
			userhelp)

        if mode != 'novice':
            leftwin = Frame(self);
        else:
            leftwin = self

        secwin = Frame(leftwin, relief=RAISED, bd=5)
        Label(secwin, text="Authentication").pack(side=TOP)
        LabeledEntry(secwin, 'Password:',
		      self.password, '12').pack(side=TOP, fill=X)
        secwin.pack(fill=X, anchor=N)

        names = Frame(leftwin, relief=RAISED, bd=5)
        Label(names, text="Local names").pack(side=TOP)
        ListEdit("New name: ",
                     self.user.localnames, None, names, localhelp)
        names.pack(fill=X, anchor=N)

        if mode != 'novice':
            targwin = Frame(leftwin, relief=RAISED, bd=5)
            Label(targwin, text="Forwarding Options").pack(side=TOP)
            Label(targwin, text="Listeners to forward to").pack(side=TOP)
            ListEdit("New listener:", self.user.smtphunt, None, targwin, None)
            LabeledEntry(targwin, 'Append to MAIL FROM line:',
		     self.smtpaddress, '26').pack(side=TOP, fill=X)
            LabeledEntry(targwin, 'Connection setup command:',
		     self.preconnect, '26').pack(side=TOP, fill=X)
            LabeledEntry(targwin, 'Connection wrapup command:',
		     self.postconnect, '26').pack(side=TOP, fill=X)
            LabeledEntry(targwin, 'Local delivery agent:',
		     self.mda, '26').pack(side=TOP, fill=X)
            LabeledEntry(targwin, 'BSMTP output file:',
		     self.bsmtp, '26').pack(side=TOP, fill=X)
            LabeledEntry(targwin, 'Listener spam-block codes:',
		     self.antispam, '26').pack(side=TOP, fill=X)
            LabeledEntry(targwin, 'Pass-through properties:',
		     self.properties, '26').pack(side=TOP, fill=X)
            Checkbutton(targwin, text="Use LMTP?",
                        variable=self.lmtp).pack(side=TOP, fill=X)
            targwin.pack(fill=X, anchor=N)

        if mode != 'novice':
            leftwin.pack(side=LEFT, fill=X, anchor=N)
            rightwin = Frame(self)
        else:
            rightwin = self

	optwin = Frame(rightwin, relief=RAISED, bd=5)
	Label(optwin, text="Processing Options").pack(side=TOP)
	Checkbutton(optwin, text="Suppress deletion of messages after reading",
		    variable=self.keep).pack(side=TOP, anchor=W)
	Checkbutton(optwin, text="Fetch old messages as well as new",
		    variable=self.fetchall).pack(side=TOP, anchor=W)
        if mode != 'novice':
            Checkbutton(optwin, text="Flush seen messages before retrieval", 
		    variable=self.flush).pack(side=TOP, anchor=W)
            Checkbutton(optwin, text="Rewrite To/Cc/Bcc messages to enable reply", 
		    variable=self.rewrite).pack(side=TOP, anchor=W)
            Checkbutton(optwin, text="Force CR/LF at end of each line",
		    variable=self.forcecr).pack(side=TOP, anchor=W)
            Checkbutton(optwin, text="Strip CR from end of each line",
		    variable=self.stripcr).pack(side=TOP, anchor=W)
            Checkbutton(optwin, text="Pass 8 bits even though SMTP says 7BIT",
		    variable=self.pass8bits).pack(side=TOP, anchor=W)
            Checkbutton(optwin, text="Undo MIME armoring on header and body",
		    variable=self.mimedecode).pack(side=TOP, anchor=W)
            Checkbutton(optwin, text="Drop Status lines from forwarded messages", 
		    variable=self.dropstatus).pack(side=TOP, anchor=W)
	optwin.pack(fill=X)

        if mode != 'novice':
            limwin = Frame(rightwin, relief=RAISED, bd=5)
            Label(limwin, text="Resource Limits").pack(side=TOP)
            LabeledEntry(limwin, 'Message size limit:',
		      self.limit, '30').pack(side=TOP, fill=X)
            LabeledEntry(limwin, 'Size warning interval:',
		      self.warnings, '30').pack(side=TOP, fill=X)
            LabeledEntry(limwin, 'Max messages to fetch per poll:',
		      self.fetchlimit, '30').pack(side=TOP, fill=X)
            LabeledEntry(limwin, 'Max messages to forward per poll:',
		      self.batchlimit, '30').pack(side=TOP, fill=X)
            if parent.server.protocol in ('IMAP', 'IMAP-K4', 'IMAP-GSS'):
                LabeledEntry(limwin, 'Interval between expunges (IMAP):',
                             self.expunge, '30').pack(side=TOP, fill=X)
            limwin.pack(fill=X)

            if parent.server.protocol in ('IMAP', 'IMAP-K4', 'IMAP-GSS'):
                foldwin = Frame(rightwin, relief=RAISED, bd=5)
                Label(foldwin, text="Remote folders (IMAP only)").pack(side=TOP)
                ListEdit("New folder:", self.user.mailboxes, None, foldwin, None)
                foldwin.pack(fill=X, anchor=N)

        if mode != 'novice':
            rightwin.pack(side=LEFT)
        else:
            self.pack()


#
# Top-level window that offers either novice or expert mode
# (but not both at once; it disappears when one is selected).
#

class MainWindow(Frame):
    def __init__(self, outfile, master=None):
	Frame.__init__(self, master)
        self.outfile = outfile
	self.master.title('fetchmail configurator main');
	self.master.iconname('fetchmail configurator main');
	Pack.config(self)
	Label(self,
		text='Fetchmailconf ' + version, 
		bd=2).pack(side=TOP, pady=10)
        self.keepalive = []	# Use this to anchor the PhotoImage object
        make_icon_window(self, fetchmail_gif)

	Message(self, text="""
Use `Novice Configuration' for basic fetchmail setup;
with this, you can easily set up a single-drop connection
to one remote mail server.
""", width=600).pack(side=TOP)
	Button(self, text='Novice Configuration',
				fg='blue', command=self.novice).pack()

	Message(self, text="""
Use `Expert Configuration' for advanced fetchmail setup,
including multiple-site or multidrop connections.
""", width=600).pack(side=TOP)
	Button(self, text='Expert Configuration',
				fg='blue', command=self.expert).pack()

	Message(self, text="""
Or you can just select `Quit' to leave the configurator now.
""", width=600).pack(side=TOP)
	Button(self, text='Quit', fg='blue', command=self.leave).pack()

    def novice(self):
	self.destroy()
	ConfigurationEdit(Fetchmailrc, self.outfile).edit('novice')

    def expert(self):
	self.destroy()
	ConfigurationEdit(Fetchmailrc, self.outfile).edit('expert')

    def leave(self):
        self.quit()

# Functions for turning a dictionary into an instantiated object tree.

def intersect(list1, list2):
# Compute set intersection of lists
    res = []
    for x in list1:
	if x in list2:
	    res.append(x)
    return res

def setdiff(list1, list2):
# Compute set difference of lists
    res = []
    for x in list1:
	if not x in list2:
	    res.append(x)
    return res

def copy_instance(toclass, fromdict):
# Initialize a class object of given type from a conformant dictionary.
    for fld in fromdict.keys():
        if not fld in dictmembers:
            dictmembers.append(fld)
# The `optional' fields are the ones we can ignore for purposes of
# conformability checking; they'll still get copied if they are
# present in the dictionary.
    optional = ('interface', 'monitor', 'netsec')
    class_sig = setdiff(toclass.__dict__.keys(), optional)
    class_sig.sort()
    dict_keys = setdiff(fromdict.keys(), optional)
    dict_keys.sort()
    common = intersect(class_sig, dict_keys)
    if 'typemap' in class_sig: 
	class_sig.remove('typemap')
    if tuple(class_sig) != tuple(dict_keys):
	print "Fields don't match what fetchmailconf expected:"
#	print "Class signature: " + `class_sig`
#	print "Dictionary keys: " + `dict_keys`
	diff = setdiff(class_sig, common)
        if diff:
            print "Not matched in class `" + toclass.__class__.__name__ + "' signature: " + `diff`
        diff = setdiff(dict_keys, common)
        if diff:
            print "Not matched in dictionary keys: " + `diff`
	sys.exit(1)
    else:
	for x in fromdict.keys():
	    setattr(toclass, x, fromdict[x])

#
# And this is the main sequence.  How it works:  
#
# First, call `fetchmail --configdump' and trap the output in a tempfile.
# This should fill it with a Python initializer for a variable `fetchmailrc'.
# Run execfile on the file to pull fetchmailrc into Python global space.
# You don't want static data, though; you want, instead, a tree of objects
# with the same data members and added appropriate methods.
#
# This is what the copy_instance function() is for.  It tries to copy a
# dictionary field by field into a class, aborting if the class and dictionary
# have different data members (except for any typemap member in the class;
# that one is strictly for use by the MyWidget supperclass).
#
# Once the object tree is set up, require user to choose novice or expert
# mode and instantiate an edit object for the configuration.  Class methods
# will take it all from there.
#
# Options (not documented because they're for fetchmailconf debuggers only):
# -d: Read the configuration and dump it to stdout before editing.  Dump
#     the edited result to stdout as well.
# -f: specify the run control file to read.

if __name__ == '__main__': 

    fetchmail_gif = """
R0lGODdhPAAoAPcAAP///wgICBAQEISEhIyMjJSUlKWlpa2trbW1tcbGxs7Ozufn5+/v7//39yEY
GNa9tUoxKZyEe1o5KTEQAN7OxpyMhIRjUvfn3pxSKYQ5EO/Wxv/WvWtSQrVzSmtCKWspAMatnP/e
xu+1jIxSKaV7Wt6ca5xSGK2EY8aUa72MY86UY617UsaMWrV7SpRjOaVrOZRaKYxSIXNCGGs5EIRC
CJR7Y/+UMdbOxnNrY97Ove/Wvd7GrZyEa961jL2Ua9alc86ca7WEUntSKcaMSqVjGNZ7GGM5CNa1
jPfOnN6tc3taMffeve/WtWtaQv/OjGtSMYRzWv/erda1hM6te7WUY62MWs61jP/vzv/ntda9jL2l
czEhAO/n1oyEc//elDEpGEo5EOfexpyUe+/epefevffvxnNrQpyUStbWzsbGvZyclN7ezmNjWv//
5/f33qWllNbWve/vzv//1ufnve/vvf//xvf3vefnrf//taWlc0pKMf//pbW1Y///jKWlWq2tWsbG
Y///c97eUvf3Ut7nc+/3a87We8bOjOfv1u/37/f//621tb3Gxtbn52Nra87n53uUlJTv/6W9xuf3
/8bW3iExOXu11tbv/5TW/4TO/63e/zmt/1KUxlK1/2u9/wCM/73GzrXG1gBKjACE/87e72NzhCkx
OaXO92OMtUql/xCE/wApUtbe57W9xnN7hHut52Ot/xBSnABKnABavQB7/2ul7zF71gBr77XO73Oc
1lqc9yFSlBApSimE/wAYOQApY0J7zlKM5wAxhABS1gBj/6W95wAhWgA5nAAYSgBS7wBS/wBK9wAp
jABC5wBK/wApnABC/wApxgAhtYSMtQAQYwAp/3OE74SMxgAYxlpjvWNr70pS/wgQ3sbGzs7O1qWl
3qWl70pKe0JC/yEhlCkp/wgI/wAAEAAAIQAAKQAAOQAASgAAUgAAYwAAawAAlAAAnAAApQAArQAA
zgAA1gAA5wAA9wAA/0pC/xgQ52Na9ykhe4R7zikhYxgQSjEpQgAAACwAAAAAPAAoAAAI/wABCBxI
sKDBgwgTKiRIYKHDhxARIvgXsaLFhGgEUBSYoKPHjyBDihxJkuS/kwNLqlzJcuTJjQBaypxpEiVH
mjhxvkyZs2fLnTd9ehxAtKjRo0ZrwhTasUsENhYHKOUpk1E3j11mxCBiQVLEBlJd2owp9iVRjwUs
zMCQ5IcLD4saPVxjIKxIoGTvvqSoyFEFGTBeqEhyxAoSFR/USGKVcEGBAwDshsSr1OYTEyhQpJiS
ZcoUKWOQtJDRJFSaggzUGBgoGSTlsjahlPCRIkWVKT16THHRIoqIISBIEUgAYIGBhgRbf3ytFygU
FZp9UDmxQkkMCRwyZKDBQy4aApABhP8XqNwj88l7BVpQYZtF5iArWgwAgGZBq24HU7OeGhQ90PVA
aKZZCiiUMJ9ArSTEwGqR8ZeXfzbV0MIIMQTBwoUdxDDfAm8sZFyDZVEF4UYSKBEBD0+k6IEFPMxH
3FzldXSea+kBgANJSOWIlIMhXZXAXv+c1WM3PuJEpH8iuhbAkv+MdENPRHaTRkdF/jiWSKCAwlKW
VbbkY5Q0LgUSKExgoYBKCjCxARpdltQNKHaUoYAddnR53lVRnJLKBWh4RIEGCZx5FSOv1OLNDUVe
deZHaWiZAB35fIOGNtbEUeV5oGAByzPOrBPFGt3kwEgxITACSg5oLGGLMg60oQAjaNz/oAAcN4Ai
a0c3kHFDK3jYsw4g9sRzBgPLXdkRrBrQ8gsWQUxCCRZX9IJNBQ1s8IgCdeBCzBYN6IBIN2TUsQYd
dXhDBxdzlAHOHHKEcocZdWwDjx8MTCmjsR2FMAstw1RyiSzHqPLALaOwk8QmzCzDCSi0xJKMMk4E
Yw8389iTDT32GAKOPf7YY0Aa9tATyD3w/EGsefgmgEYUtPiChLKWQDMBJtEUgYkzH2RiTgGfTMCI
Mlu0Yc85hNiDziH2tMqOGL72QY47gshLb7Fi4roELcjoQIsxWpDwQyfS2OCJMkLI4YUmyhgxSTVg
CP2FHPZ80UDcieBjStNPD5LPOyZT/y0iHGiMwswexDSzRiRq6KIMJBc4M8skwKAyChia2KPH3P24
YU8/lFhOTj152OPOHuXMU4g48vCRiN/9rZGLMdS4csUu1JzDgxuipOMDHMKsAwEnq/ByzTrrZMNO
OtO0k84+7KjzBjzplMJOOOOoo8846/ATxqJWinkkGUyEkMAaIezABQM3bMAEK1xEsUMDGjARRxhY
xEGGHfPjEcccca6BRxhyuEMY7FCHMNDhf9140r2qRiVvdENQ3liUArzREW/0qRsRVIAGFfBADnLw
gUSiYASJpMEHhilJTEnhAlGoQqYAZQ1AiqEMZ0jDGtqQImhwwA13yMMevoQAGvGhEAWHGMOAAAA7
"""
# Note on making icons: the above was generated by the following procedure:
#
# import base64
# data = open("fetchmail.gif", "rb").read()
# print "fetchmail_gif =\\"
# print repr(base64.encodestring(data))
#

    # Process options
    (options, arguments) = getopt.getopt(sys.argv[1:], "df:")
    dump = rcfile = None;
    for (switch, val) in options:
        if (switch == '-d'):
            dump = TRUE
        elif (switch == '-f'):
            rcfile = val

    # Get client host's FQDN
    hostname = socket.gethostbyaddr(socket.gethostname())[0]

    # Compute defaults
    ConfigurationDefaults = Configuration()
    ServerDefaults = Server()
    UserDefaults = User()

    # Read the existing configuration
    tmpfile = "/tmp/fetchmailconf." + `os.getpid()`
    if rcfile:
        cmd = "fetchmail -f " + rcfile + " --configdump >" + tmpfile
    else:
        cmd = "fetchmail --configdump >" + tmpfile
        
    try:
        s = os.system(cmd)
	if s != 0:
            print "`" + cmd + "' run failure, status " + `s`
            raise SystemExit
    except:
        print "Unknown error while running fetchmail --configdump"
        os.remove(tmpfile)
        sys.exit(1)

    try:
        execfile(tmpfile)
    except:
        print "Can't read configuration output of fetchmail --configdump."
        os.remove(tmpfile)
        sys.exit(1)
        
    os.remove(tmpfile)

    # The tricky part -- initializing objects from the configuration global
    # `Configuration' is the top level of the object tree we're going to mung.
    # The dictmembers list is used to track the set of fields the dictionary
    # contains; in particular, we can use it to tell whether things like the
    # monitor, interface, and netsec fields are present.
    dictmembers = []
    Fetchmailrc = Configuration()
    copy_instance(Fetchmailrc, fetchmailrc)
    Fetchmailrc.servers = [];
    for server in fetchmailrc['servers']:
	Newsite = Server()
	copy_instance(Newsite, server)
	Fetchmailrc.servers.append(Newsite)
	Newsite.users = [];
	for user in server['users']:
	    Newuser = User()
	    copy_instance(Newuser, user)
	    Newsite.users.append(Newuser)

    # We may want to display the configuration and quit
    if dump:
        print "This is a dump of the configuration we read:\n"+`Fetchmailrc`

    # The theory here is that -f alone sets the rcfile location,
    # but -d and -f together mean the new configuration should go to stdout.
    if not rcfile and not dump:
        rcfile = os.environ["HOME"] + "/.fetchmailrc"

    # OK, now run the configuration edit
    root = MainWindow(rcfile)
    root.mainloop()

# The following sets edit modes for GNU EMACS
# Local Variables:
# mode:python
# End: