17 from optparse import OptionParser
18 from ConfigParser import SafeConfigParser
21 def parse_and_deliver(maildir, url, statedir):
22 md = mailbox.Maildir(maildir)
23 fp = feedparser.parse(url)
24 for item in fp["items"]:
25 # things that we need in the message
26 msg = email.message_from_string("")
27 msg.add_header("Subject", item["title"])
28 msg.set_unixfrom("Brett Parker <iDunno@sommitrealweird.co.uk>")
29 msg.add_header("Date", datetime.datetime(*item["created_parsed"][0:6]).strftime("%a, %e %b %Y %T -0000"))
30 msg.add_header("To", url)
31 msg.set_payload(item["content"][0]["value"])
32 msg.set_charset("utf8")
33 msg.set_default_type("text/plain")
35 # start by working out the filename we should be writting to, we do
36 # this following the normal maildir style rules
37 fname = str(os.getpid()) + "".join([random.choice(string.ascii_letters + string.digits) for a in range(0,10)]) + datetime.datetime.now().strftime('%s')
38 fn = os.path.join(maildir, "tmp", fname)
40 fh.write(msg.as_string())
42 # now move it in to the new directory
43 newfn = os.path.join(maildir, "new", fname)
47 # first off, parse the command line arguments
49 oparser = OptionParser()
51 "-c", "--conf", dest="conf",
52 help="location of config file"
55 "-s", "--statedir", dest="statedir",
56 help="location of directory to store state in"
59 (options, args) = oparser.parse_args()
61 # check for the configfile
65 if options.conf != None:
66 # does the file exist?
69 configfile = options.conf
71 # should exit here as the specified file doesn't exist
72 sys.stderr.write("Config file %s does not exist. Exiting.\n" %(options.conf,))
75 # check through the default locations
77 os.stat("%s/.rss2maildir.conf" %(os.environ["HOME"],))
78 configfile = "%s/.rss2maildir.conf" %(os.environ["HOME"],)
81 os.stat("/etc/rss2maildir.conf")
82 configfile = "/etc/rss2maildir.conf"
84 sys.stderr.write("No config file found. Exiting.\n")
87 # Right - if we've got this far, we've got a config file, now for the hard
90 scp = SafeConfigParser()
93 maildir_root = "RSSMaildir"
96 if options.statedir != None:
97 state_dir = options.statedir
99 mode = os.stat(state_dir)[stat.ST_MODE]
100 if not stat.S_ISDIR(mode):
101 sys.stderr.write("State directory (%s) is not a directory\n" %(state_dir))
104 # try to make the directory
108 sys.stderr.write("Couldn't create statedir %s" %(state_dir))
110 elif scp.has_option("general", "state_dir"):
111 new_state_dir = scp.get("general", "state_dir")
113 mode = os.stat(state_dir)[stat.ST_MODE]
114 if not stat.S_ISDIR(mode):
115 sys.stderr.write("State directory (%s) is not a directory\n" %(state_dir))
120 os.mkdir(new_state_dir)
121 state_dir = new_state_dir
123 sys.stderr.write("Couldn't create state directory %s\n" %(new_state_dir))
126 if scp.has_option("general", "maildir_root"):
127 maildir_root = scp.get("general", "maildir_root")
130 mode = os.stat(maildir_root)[stat.ST_MODE]
131 if not stat.S_ISDIR(mode):
132 sys.stderr.write("Maildir Root %s is not a directory\n" %(maildir_root))
136 os.mkdir(maildir_root)
138 sys.stderr.write("Couldn't create Maildir Root %s\n" %(maildir_root))
141 feeds = scp.sections()
143 feeds.remove("general")
147 for section in feeds:
148 # check if the directory exists
151 maildir = scp.get(section, "maildir")
155 maildir = urllib.urlencode(((section, maildir),)).split("=")[1]
156 maildir = os.path.join(maildir_root, maildir)
159 exists = os.stat(maildir)
160 if stat.S_ISDIR(exists[stat.ST_MODE]):
161 # check if there's a new, cur and tmp directory
163 mode = os.stat(os.path.join(maildir, "cur"))[stat.ST_MODE]
165 os.mkdir(os.path.join(maildir, "cur"))
166 if not stat.S_ISDIR(mode):
167 sys.stderr.write("Broken maildir: %s\n" %(maildir))
169 mode = os.stat(os.path.join(maildir, "tmp"))[stat.ST_MODE]
171 os.mkdir(os.path.join(maildir, "tmp"))
172 if not stat.S_ISDIR(mode):
173 sys.stderr.write("Broken maildir: %s\n" %(maildir))
175 mode = os.stat(os.path.join(maildir, "new"))[stat.ST_MODE]
176 if not stat.S_ISDIR(mode):
177 sys.stderr.write("Broken maildir: %s\n" %(maildir))
179 os.mkdir(os.path.join(maildir, "new"))
181 sys.stderr.write("Broken maildir: %s\n" %(maildir))
186 sys.stderr.write("Couldn't create root maildir %s\n" %(maildir))
189 os.mkdir(os.path.join(maildir, "new"))
190 os.mkdir(os.path.join(maildir, "cur"))
191 os.mkdir(os.path.join(maildir, "tmp"))
193 sys.stderr.write("Couldn't create required maildir directories for %s\n" %(section,))
196 # right - we've got the directories, we've got the section, we know the
199 parse_and_deliver(maildir, section, state_dir)