aboutsummaryrefslogtreecommitdiffstats
path: root/fetchmailconf
blob: 3ea9f8ead9f82c1e7f9c3d2c020fc805c0e23f9c (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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
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
#!/usr/bin/python
#
# A GUI configurator for generating Fetchmail configuration files
# by Eric S. Raymond, <esr@snark.thyrsus.com>.  Requires Python with Tkinter.

from Tkinter import *
from Dialog import *
import sys
import time
import os
import string

#
# Define the data structures the GUIs will be tossing around
#
class Controls:
    def __init__(self):
	self.poll_interval = 300	# Default to 5-minute timeout
	self.syslog = FALSE		# Use syslogd for logging?
	self.logfile = None		# No logfile, initially
	self.idfile = os.environ["HOME"] + "/.fetchids"		# Default idfile, initially
	self.invisible = FALSE		# Suppress Received line & spoof?
	self.servers = []		# List of included sites

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

    def __str__(self):
	return "[Controls: " + 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.users = []			# List of user entries for site
	self.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'))

    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 " + 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 != ServerDefaults.qvirtual:
	    str = str + " qvirtual " + 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.interface or self.monitor):
	    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):
	self.remote = ""		# Remote username
	self.localnames = None		# Local names
	self.password = ""		# Password for mail account access
	self.mailboxes = None		# Remote folders to retrieve from
	self.smtphunt = None		# Hosts to forward to
	self.smtpaddress = None;	# Append this to MAIL FROM line
	self.preconnect = ""		# Connection setup
	self.postconnect = ""		# Connection wrapup
	self.mda = ""			# Mail Delivery Agent
	self.antispam = 571;		# 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.fetchlimit = 0		# Max messages fetched per batch
	self.batchlimit = 0		# Max message forwarded per batch
	self.expunge = 1		# Interval between expunges (IMAP)
	self.typemap = (
	    ('remote',      'String'),
	    # leave out mailboxes and localnames
	    ('password',    'String'),
            # Leave out smtphunt
	    ('smtpaddress',  'String'),
	    ('preconnect',  'String'),
	    ('postconnect', 'String'),
	    ('mda',         'String'),
	    ('antispam',    'Int'),
	    ('keep',        'Boolean'),
	    ('flush',       'Boolean'),
	    ('fetchall',    'Boolean'),
	    ('rewrite',     'Boolean'),
	    ('forcecr',     'Boolean'),
	    ('stripcr',     'Boolean'),
	    ('pass8bits',   'Boolean'),
	    ('mimedecode',  'Boolean'),
	    ('dropstatus',  'Boolean'),
	    ('limit',       'Int'),
	    ('fetchlimit',  'Int'),
	    ('batchlimit',  'Int'),
	    ('expunge',     'Int'))

    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.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`
	if self.mailboxes:
	     str = str + "\n    folder"
	     for x in self.mailboxes:
		str = str + " " + x
	     str = str + " here"
	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-K4":143,
		"ETRN":25}

protolist = ("auto", "POP2", "POP3", "APOP", "KPOP", "IMAP", "IMAP-K4", "ETRN")

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()

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 = select[0]
	    if index:
		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
#
# First, code to set the global fetchmail run controls.
#

novicehelp = {
    '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' panel, 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.

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

experthelp = {
    '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.

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):
    def __init__(self, configuration, mode, master=None):
	Frame.__init__(self, master)
	self.master.title('fetchmail " + mode + " configurator');
	self.master.iconname('fetchmail " + mode + configurator');
	self.configuration = configuration
	Pack.config(self)
        if mode == 'expert':
            self.expertEdit()
        elif mode == 'novice':
            self.noviceEdit()
        self.mode = mode

    def expertEdit(self):
	self.poll_interval = StringVar(self)
	self.poll_interval.set(`self.configuration.poll_interval`)
	self.syslog = BooleanVar(self)
	self.syslog.set(self.configuration.syslog)
	self.logfile = StringVar(self)

	if self.configuration.logfile: self.logfile.set(self.configuration.logfile);
	self.idfile = StringVar(self)
	if self.configuration.idfile: self.idfile.set(self.configuration.idfile);
	self.invisible = BooleanVar(self)
	self.invisible.set(self.configuration.invisible)
	dispose_window(self, 'Expert Configurator Controls', experthelp)

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

	# Set the poll interval
	de = LabeledEntry(df, '     Poll interval:', self.poll_interval, '14')

	de.pack(side=RIGHT, anchor=E)
	df.pack();

	sf = Frame(gf, relief=RAISED, bd=2)
	# Use syslog for logging?
	Checkbutton(sf,
		{'text':'Log to syslog?',
		'variable':self.syslog,
		'relief':GROOVE}).pack(side=LEFT, anchor=W)
	# Set the logfile
	log = LabeledEntry(sf, '     Logfile:', self.logfile, '14')
	log.pack(side=RIGHT, anchor=E)
	sf.pack(fill=X)

	# Invisible mode?
	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),
		self.editSite, lf, remotehelp)
	lf.pack(fill=X)

    def editSite(self, site):
	ServerEdit(site, self.configuration.servers, Toplevel())

    #XXX This method doesn't work yet.
    def noviceEdit(self):
	self.novice_host = StringVar(self)
	self.novice_host.set(`self.configuration.novice_host`)
	self.novice_name = StringVar(self)
	self.novice_name.set(`self.configuration.novice_name`)
	self.novice_passwd = StringVar(self)
	self.novice_passwd.set(`self.configuration.passwd`)
	self.novice_interval = IntVar(self)
	self.novice_interval.set(`self.configuration.novice_interval`)

        dispose_window(self, 'Novice Configurator Controls', novicehelp)
	novice = Frame(self, relief=RAISED, bd=5)
	Label(novice,
		text='Novice Configuration Window', 
		bd=2).pack(side=TOP, pady=10)
	LabeledEntry(novice, 'Remote host to query:',
		      self.novice_host).pack(side=TOP, fill=X)
	LabeledEntry(novice, 'Your login name on the remote host:',
		      self.novice_name).pack(side=TOP, fill=X)
	LabeledEntry(novice, 'Your password on the remote host:',
		      self.novice_passwd).pack(side=TOP, fill=X)
	ButtonBar(novice, '', self.novice_protocol, protolist, 2, None) 
	LabeledEntry(novice, 'Seconds between background polls:',
		      self.novice_interval).pack(side=TOP, fill=X)
	novice.pack(side=TOP, fill=X);

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

    # XXX Someday this must go to the actual config file location
    def save(self):
        if self.mode == 'expert':
            self.configuration.poll_interval = self.poll_interval.get()
            self.configuration.logfile = self.logfile.get()
            self.configuration.idfile = self.idfile.get()
            self.configuration.syslog = self.syslog.get()
            self.configuration.invisible = self.invisible.get()
	sys.stdout.write("# Configuration created %s\n" % time.ctime(time.time()))
	sys.stdout.write(`self.configuration`)
	self.quit()

#
# 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.

The `Protocol' button bar offers you a choice of
all the different protocols available.  The `auto'
protocol is a special mode that 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 lets you set a non-standard port.
"""}

sechelp = {
    'title' : 'Security option help',
    'banner': 'Security',
    'text' : """
The `interface' option, if given, specifies the only
device through which fetchmail is permitted to connect
to servers.  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 allows you to specify a range
of IP addresses to monitor for activity.  If these
addresses are not active, fetchmail will not poll.
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.
"""}

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.
"""}

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: helpwin(help)).pack(side=RIGHT)
    dispose.pack(fill=X)
    return dispose

class ServerEdit(Frame):
    def __init__(self, host, servers, master=None):
	Frame.__init__(self, master)
	Pack.config(self)
	self.master.title('Fetchmail host ' + host);
	self.master.iconname('Fetchmail host ' + host);

	self.server = None
	for site in servers:
	    if site.pollname == host:
		self.server = site
	if (self.server == None):
		self.server = Server()
		self.server.pollname = host
		self.server.via = host
		servers.append(self.server)

	self.post()
	self.expertEdit(host)
#	self.grab_set()
#	self.focus_set()
#	self.wait_window()

    def post(self):
	# we can't abstract this away, execs would happen in the wrong scope
	for x in self.server.typemap:
	    target = "self." + x[0]
	    source = "self.server." + x[0]
	    if x[1] == 'Boolean':
		exec target + " = BooleanVar(self)"
		if eval(source):
		    exec target + ".set(" + source + ")"
	    elif x[1] == 'String':
		exec target + " = StringVar(self)"
		if eval(source):
		    exec target + ".set(" + source + ")"
	    elif x[1] == 'Int':
		exec target + " = IntVar(self)"
		if eval(source):
		    exec target + ".set(" + source + ")"

    def gather(self):
	for x in self.server.typemap:
	    setattr(self.server, x[0], getattr(self, x[0]).get())

    def nosave(self):
	if ConfirmQuit(self, 'server option editing'):
	    Widget.destroy(self.master)

    def save(self):
	self.gather()
	Widget.destroy(self.master)

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

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

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

	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)

	protwin = Frame(leftwin, relief=RAISED, bd=5)
	Label(protwin, text="Protocol and Port").pack(side=TOP)
	pb = ButtonBar(protwin, '', self.protocol, protolist, 2, self.refreshPort) 
	LabeledEntry(protwin, 'On server TCP/IP port:',
		      self.port, leftwidth).pack(side=TOP, fill=X)
	Checkbutton(protwin,
		text="POP3: track `seen' with client-side UIDLs?",
		variable=self.uidl).pack(side=TOP)   
	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),
                 self.edituser, userwin, suserhelp)
	userwin.pack(fill=X)

	leftwin.pack(side=LEFT, anchor=N, fill=X);
	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)

	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.popen("uname").readlines()[0] == 'Linux\n':
	    LabeledEntry(secwin, 'Interface to check before poll:',
			 self.interface, leftwidth).pack(side=TOP, fill=X)
	    LabeledEntry(secwin, 'IP range to monitor:',
			 self.monitor, 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 edituser(self, user):
	UserEdit(user, self.server, Toplevel())

#
# 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.

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

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):
    def __init__(self, username, server, master=None):
	Frame.__init__(self, master)
	Pack.config(self)
	self.master.title('Fetchmail user ' + username + ' querying ' + server.pollname);
	self.master.iconname('Fetchmail user ' + username);

	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 = user
	    self.user.localnames = [user]
	    server.users.append(self.user)

	self.post()
	self.expertEdit()
#	self.grab_set()
#	self.focus_set()
#	self.wait_window()

    def post(self):
	# we can't abstract this away, execs would happen in the wrong scope
	for x in self.user.typemap:
	    target = "self." + x[0]
	    source = "self.user." + x[0]
	    if x[1] == 'Boolean':
		exec target + " = BooleanVar(self)"
		if eval(source):
		    exec target + ".set(" + source + ")"
	    elif x[1] == 'String':
		exec target + " = StringVar(self)"
		if eval(source):
		    exec target + ".set(" + source + ")"
	    elif x[1] == 'Int':
		exec target + " = IntVar(self)"
		if eval(source):
		    exec target + ".set(" + source + ")"

    def gather(self):
	for x in self.user.typemap:
	    setattr(self.user, x[0], getattr(self, x[0]).get())

    def nosave(self):
	if ConfirmQuit(self, 'user option editing'):
	    Widget.destroy(self.master)

    def save(self):
	self.gather()
	Widget.destroy(self.master)

    def expertEdit(self):
	dispose_window(self,
			"User options for " + self.user.remote,
			userhelp)

	leftwin = Frame(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)

	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, 'Listener spam-block code:',
		     self.antispam, '26').pack(side=TOP, fill=X)
	targwin.pack(fill=X, anchor=N)

	leftwin.pack(side=LEFT, fill=X, anchor=N)
	rightwin = Frame(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="Flush seen messages before retrieval", 
		    variable=self.flush).pack(side=TOP, anchor=W)
	Checkbutton(optwin, text="Fetch old messages as well as new",
		    variable=self.fetchall).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 eacgh 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)

	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, '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)
	LabeledEntry(limwin, 'Interval between expunges (IMAP):',
		      self.expunge, '30').pack(side=TOP, fill=X)
	limwin.pack(fill=X)

	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)

	rightwin.pack(side=LEFT)


#
# And this is the main sequence
#

class MainWindow(Frame):
    def __init__(self, master=None):
	Frame.__init__(self, master)
	self.master.title('fetchmail configurator main');
	self.master.iconname('fetchmail configurator main');
	Pack.config(self)
	Label(self,
		text='Configurator Main Window', 
		bd=2).pack(side=TOP, pady=10)

	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(Configuration, 'novice')

    def expert(self):
	self.destroy()
	ConfigurationEdit(Configuration, 'expert')

    def leave(self):
	if ConfirmQuit(self, "configuration editor"):
	    self.quit()

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.
    class_sig = toclass.__dict__.keys(); class_sig.sort()
    dict_keys = fromdict.keys(); 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 "Conformability error"
#	print "Class signature: " + `class_sig`
#	print "Dictionary keys: " + `dict_keys`
	print "Not matched in class signature: " + `setdiff(class_sig, common)`
	print "Not matched in dictionary keys: " + `setdiff(dict_keys, common)`
	sys.exit(1)
    else:
	for x in dict_keys:
	    setattr(toclass, x, fromdict[x])

if __name__ == '__main__': 

    # Compute defaults
    ControlDefaults = Controls()
    ServerDefaults = Server()
    UserDefaults = User()

    # Read the existing configuration
    tmpfile = "/tmp/fetchmailconf." + `os.getpid()`
    sys.stdout.write("Reading configuration...")
    os.system("fetchmail --configdump >" + tmpfile)
    execfile(tmpfile)
    sys.stdout.write("Done.\n")
    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 
    Configuration = Controls()
    copy_instance(Configuration, configuration)
    Configuration.servers = [];
    for server in configuration['servers']:
	Newsite = Server()
	copy_instance(Newsite, server)
	Configuration.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 len(sys.argv) > 1 and sys.argv[1] == '-d':
        print "This is a dump of the configuration we read:\n"+`Configuration`
        sys.exit(0)

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

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