]> git.sommitrealweird.co.uk Git - rss2maildir.git/blob - rss2maildir.py
Create Maildirs and Maildir root if possible for the feeds
[rss2maildir.git] / rss2maildir.py
1 #!/usr/bin/python
2
3 import mailbox
4 import sys
5 import os
6 import stat
7 import urllib
8
9 from optparse import OptionParser
10 from ConfigParser import SafeConfigParser
11
12 # first off, parse the command line arguments
13
14 oparser = OptionParser()
15 oparser.add_option(
16     "-c", "--conf", dest="conf",
17     help="location of config file"
18     )
19
20 (options, args) = oparser.parse_args()
21
22 # check for the configfile
23
24 configfile = None
25
26 if options.conf != None:
27     # does the file exist?
28     try:
29         os.stat(options.conf)
30         configfile = options.conf
31     except:
32         # should exit here as the specified file doesn't exist
33         sys.stderr.write("Config file %s does not exist. Exiting.\n" %(options.conf,))
34         sys.exit(2)
35 else:
36     # check through the default locations
37     try:
38         os.stat("%s/.rss2maildir.conf" %(os.environ["HOME"],))
39         configfile = "%s/.rss2maildir.conf" %(os.environ["HOME"],)
40     except:
41         try:
42             os.stat("/etc/rss2maildir.conf")
43             configfile = "/etc/rss2maildir.conf"
44         except:
45             sys.stderr.write("No config file found. Exiting.\n")
46             sys.exit(2)
47
48 # Right - if we've got this far, we've got a config file, now for the hard
49 # bits...
50
51 scp = SafeConfigParser()
52 scp.read(configfile)
53
54 maildir_root = "RSSMaildir"
55
56 if scp.has_option("general", "maildir_root"):
57     maildir_root = scp.get("general", "maildir_root")
58
59 try:
60     mode = os.stat(maildir_root)[stat.ST_MODE]
61     if not stat.S_ISDIR(mode):
62         sys.stderr.write("Maildir Root %s is not a directory\n" %(maildir_root))
63         sys.exit(1)
64 except:
65     try:
66         os.mkdir(maildir_root)
67     except:
68         sys.stderr.write("Couldn't create Maildir Root %s\n" %(maildir_root))
69         sys.exit(1)
70
71 feeds = scp.sections()
72 try:
73     feeds.remove("general")
74 except:
75     pass
76
77 for section in feeds:
78     # check if the directory exists
79     maildir = None
80     try:
81         maildir = scp.get(section, "maildir")
82     except:
83         maildir = section
84
85     maildir = urllib.urlencode(((section, maildir),)).split("=")[1]
86     maildir = os.path.join(maildir_root, maildir)
87
88     try:
89         exists = os.stat(maildir)
90         if stat.S_ISDIR(exists[stat.ST_MODE]):
91             # check if there's a new, cur and tmp directory
92             try:
93                 mode = os.stat(os.path.join(maildir, "cur"))[stat.ST_MODE]
94             except:
95                 os.mkdir(os.path.join(maildir, "cur"))
96                 if not stat.S_ISDIR(mode):
97                     sys.stderr.write("Broken maildir: %s" %(maildir))
98             try:
99                 mode = os.stat(os.path.join(maildir, "tmp"))[stat.ST_MODE]
100             except:
101                 os.mkdir(os.path.join(maildir, "tmp"))
102                 if not stat.S_ISDIR(mode):
103                     sys.stderr.write("Broken maildir: %s" %(maildir))
104             try:
105                 mode = os.stat(os.path.join(maildir, "new"))[stat.ST_MODE]
106                 if not stat.S_ISDIR(mode):
107                     sys.stderr.write("Broken maildir: %s" %(maildir))
108             except:
109                 os.mkdir(os.path.join(maildir, "new"))
110         else:
111             sys.stderr.write("Broken maildir: %s" %(maildir))
112     except:
113         os.mkdir(maildir)
114         os.mkdir(os.path.join(maildir, "new"))
115         os.mkdir(os.path.join(maildir, "cur"))
116         os.mkdir(os.path.join(maildir, "tmp"))