15 from optparse import OptionParser
16 from ConfigParser import SafeConfigParser
19 def parse_and_deliver(maildir, url, statedir):
20 md = mailbox.Maildir(maildir)
21 fp = feedparser.parse(url)
22 for item in fp["items"]:
23 # things that we need in the message
24 msg = email.message_from_string("")
25 msg.add_header("Subject", item["title"])
26 msg.set_unixfrom("Brett Parker <iDunno@sommitrealweird.co.uk>")
27 msg.add_header("Date", datetime.datetime(*item["created_parsed"][0:6]).strftime("%a, %e %b %Y %T -0000"))
28 msg.add_header("To", url)
29 msg.set_payload(item["content"][0]["value"])
30 msg.set_charset("utf8")
31 msg.set_default_type("text/plain")
33 # open a temporary file in the maildir
34 fn = os.tempnam(os.path.join(maildir, "tmp"))
36 fh.write(msg.as_string())
38 # now move it in to the new directory
39 newfn = os.tempnam(os.path.join(maildir, "new"))
43 # first off, parse the command line arguments
45 oparser = OptionParser()
47 "-c", "--conf", dest="conf",
48 help="location of config file"
51 "-s", "--statedir", dest="statedir",
52 help="location of directory to store state in"
55 (options, args) = oparser.parse_args()
57 # check for the configfile
61 if options.conf != None:
62 # does the file exist?
65 configfile = options.conf
67 # should exit here as the specified file doesn't exist
68 sys.stderr.write("Config file %s does not exist. Exiting.\n" %(options.conf,))
71 # check through the default locations
73 os.stat("%s/.rss2maildir.conf" %(os.environ["HOME"],))
74 configfile = "%s/.rss2maildir.conf" %(os.environ["HOME"],)
77 os.stat("/etc/rss2maildir.conf")
78 configfile = "/etc/rss2maildir.conf"
80 sys.stderr.write("No config file found. Exiting.\n")
83 # Right - if we've got this far, we've got a config file, now for the hard
86 scp = SafeConfigParser()
89 maildir_root = "RSSMaildir"
92 if options.statedir != None:
93 state_dir = options.statedir
95 mode = os.stat(state_dir)[stat.ST_MODE]
96 if not stat.S_ISDIR(mode):
97 sys.stderr.write("State directory (%s) is not a directory\n" %(state_dir))
100 # try to make the directory
104 sys.stderr.write("Couldn't create statedir %s" %(state_dir))
106 elif scp.has_option("general", "state_dir"):
107 new_state_dir = scp.get("general", "state_dir")
109 mode = os.stat(state_dir)[stat.ST_MODE]
110 if not stat.S_ISDIR(mode):
111 sys.stderr.write("State directory (%s) is not a directory\n" %(state_dir))
116 os.mkdir(new_state_dir)
117 state_dir = new_state_dir
119 sys.stderr.write("Couldn't create state directory %s\n" %(new_state_dir))
122 if scp.has_option("general", "maildir_root"):
123 maildir_root = scp.get("general", "maildir_root")
126 mode = os.stat(maildir_root)[stat.ST_MODE]
127 if not stat.S_ISDIR(mode):
128 sys.stderr.write("Maildir Root %s is not a directory\n" %(maildir_root))
132 os.mkdir(maildir_root)
134 sys.stderr.write("Couldn't create Maildir Root %s\n" %(maildir_root))
137 feeds = scp.sections()
139 feeds.remove("general")
143 for section in feeds:
144 # check if the directory exists
147 maildir = scp.get(section, "maildir")
151 maildir = urllib.urlencode(((section, maildir),)).split("=")[1]
152 maildir = os.path.join(maildir_root, maildir)
155 exists = os.stat(maildir)
156 if stat.S_ISDIR(exists[stat.ST_MODE]):
157 # check if there's a new, cur and tmp directory
159 mode = os.stat(os.path.join(maildir, "cur"))[stat.ST_MODE]
161 os.mkdir(os.path.join(maildir, "cur"))
162 if not stat.S_ISDIR(mode):
163 sys.stderr.write("Broken maildir: %s\n" %(maildir))
165 mode = os.stat(os.path.join(maildir, "tmp"))[stat.ST_MODE]
167 os.mkdir(os.path.join(maildir, "tmp"))
168 if not stat.S_ISDIR(mode):
169 sys.stderr.write("Broken maildir: %s\n" %(maildir))
171 mode = os.stat(os.path.join(maildir, "new"))[stat.ST_MODE]
172 if not stat.S_ISDIR(mode):
173 sys.stderr.write("Broken maildir: %s\n" %(maildir))
175 os.mkdir(os.path.join(maildir, "new"))
177 sys.stderr.write("Broken maildir: %s\n" %(maildir))
182 sys.stderr.write("Couldn't create root maildir %s\n" %(maildir))
185 os.mkdir(os.path.join(maildir, "new"))
186 os.mkdir(os.path.join(maildir, "cur"))
187 os.mkdir(os.path.join(maildir, "tmp"))
189 sys.stderr.write("Couldn't create required maildir directories for %s\n" %(section,))
192 # right - we've got the directories, we've got the section, we know the
195 parse_and_deliver(maildir, section, state_dir)