11 from email.MIMEMultipart import MIMEMultipart
12 from email.MIMEText import MIMEText
20 from optparse import OptionParser
21 from ConfigParser import SafeConfigParser
23 from base64 import b64encode
30 def parse_and_deliver(maildir, url, statedir):
31 md = mailbox.Maildir(maildir)
32 fp = feedparser.parse(url)
33 db = dbm.open(os.path.join(statedir, "seen"), "c")
34 for item in fp["items"]:
35 # have we seen it before?
36 # need to work out what the content is first...
38 if item.has_key("content"):
39 content = item["content"][0]["value"]
41 content = item["summary"]
43 md5sum = md5.md5(content.encode("utf8")).hexdigest()
45 if db.has_key(item["link"]):
46 data = db[item["link"]]
47 data = cgi.parse_qs(data)
48 if data["contentmd5"][0] == md5sum:
52 author = item["author"]
56 # create a basic email message
57 msg = MIMEMultipart("alternative")
58 messageid = "<" + datetime.datetime.now().strftime("%Y%m%d%H%M") + "." + "".join([random.choice(string.ascii_letters + string.digits) for a in range(0,6)]) + "@" + socket.gethostname() + ">"
59 msg.add_header("Message-ID", messageid)
60 msg.set_unixfrom("\"%s\" <rss2maildir@localhost>" %(url))
61 msg.add_header("From", "\"%s\" <rss2maildir@localhost>" %(author))
62 msg.add_header("To", "\"%s\" <rss2maildir@localhost>" %(url))
63 createddate = datetime.datetime(*item["updated_parsed"][0:6]).strftime("%a, %e %b %Y %T -0000")
64 msg.add_header("Date", createddate)
65 msg.add_header("Subject", item["title"])
66 msg.set_default_type("text/plain")
68 htmlpart = MIMEText(content.encode("utf8"), "html", "utf8")
69 textpart = MIMEText(content.encode("utf8"), "plain", "utf8")
74 # start by working out the filename we should be writting to, we do
75 # this following the normal maildir style rules
76 fname = str(os.getpid()) + "." + socket.gethostname() + "." + "".join([random.choice(string.ascii_letters + string.digits) for a in range(0,10)]) + "." + datetime.datetime.now().strftime('%s')
77 fn = os.path.join(maildir, "tmp", fname)
79 fh.write(msg.as_string())
81 # now move it in to the new directory
82 newfn = os.path.join(maildir, "new", fname)
86 # now add to the database about the item
87 data = urllib.urlencode((("message-id", messageid), ("created", createddate), ("contentmd5", md5sum)))
88 db[item["link"]] = data
92 # first off, parse the command line arguments
94 oparser = OptionParser()
96 "-c", "--conf", dest="conf",
97 help="location of config file"
100 "-s", "--statedir", dest="statedir",
101 help="location of directory to store state in"
104 (options, args) = oparser.parse_args()
106 # check for the configfile
110 if options.conf != None:
111 # does the file exist?
113 os.stat(options.conf)
114 configfile = options.conf
116 # should exit here as the specified file doesn't exist
117 sys.stderr.write("Config file %s does not exist. Exiting.\n" %(options.conf,))
120 # check through the default locations
122 os.stat("%s/.rss2maildir.conf" %(os.environ["HOME"],))
123 configfile = "%s/.rss2maildir.conf" %(os.environ["HOME"],)
126 os.stat("/etc/rss2maildir.conf")
127 configfile = "/etc/rss2maildir.conf"
129 sys.stderr.write("No config file found. Exiting.\n")
132 # Right - if we've got this far, we've got a config file, now for the hard
135 scp = SafeConfigParser()
138 maildir_root = "RSSMaildir"
141 if options.statedir != None:
142 state_dir = options.statedir
144 mode = os.stat(state_dir)[stat.ST_MODE]
145 if not stat.S_ISDIR(mode):
146 sys.stderr.write("State directory (%s) is not a directory\n" %(state_dir))
149 # try to make the directory
153 sys.stderr.write("Couldn't create statedir %s" %(state_dir))
155 elif scp.has_option("general", "state_dir"):
156 new_state_dir = scp.get("general", "state_dir")
158 mode = os.stat(state_dir)[stat.ST_MODE]
159 if not stat.S_ISDIR(mode):
160 sys.stderr.write("State directory (%s) is not a directory\n" %(state_dir))
165 os.mkdir(new_state_dir)
166 state_dir = new_state_dir
168 sys.stderr.write("Couldn't create state directory %s\n" %(new_state_dir))
172 mode = os.stat(state_dir)[stat.ST_MODE]
173 if not stat.S_ISDIR(mode):
174 sys.stderr.write("State directory %s is not a directory\n" %(state_dir))
180 sys.stderr.write("State directory %s could not be created\n" %(state_dir))
183 if scp.has_option("general", "maildir_root"):
184 maildir_root = scp.get("general", "maildir_root")
187 mode = os.stat(maildir_root)[stat.ST_MODE]
188 if not stat.S_ISDIR(mode):
189 sys.stderr.write("Maildir Root %s is not a directory\n" %(maildir_root))
193 os.mkdir(maildir_root)
195 sys.stderr.write("Couldn't create Maildir Root %s\n" %(maildir_root))
198 feeds = scp.sections()
200 feeds.remove("general")
204 for section in feeds:
205 # check if the directory exists
208 maildir = scp.get(section, "maildir")
212 maildir = urllib.urlencode(((section, maildir),)).split("=")[1]
213 maildir = os.path.join(maildir_root, maildir)
216 exists = os.stat(maildir)
217 if stat.S_ISDIR(exists[stat.ST_MODE]):
218 # check if there's a new, cur and tmp directory
220 mode = os.stat(os.path.join(maildir, "cur"))[stat.ST_MODE]
222 os.mkdir(os.path.join(maildir, "cur"))
223 if not stat.S_ISDIR(mode):
224 sys.stderr.write("Broken maildir: %s\n" %(maildir))
226 mode = os.stat(os.path.join(maildir, "tmp"))[stat.ST_MODE]
228 os.mkdir(os.path.join(maildir, "tmp"))
229 if not stat.S_ISDIR(mode):
230 sys.stderr.write("Broken maildir: %s\n" %(maildir))
232 mode = os.stat(os.path.join(maildir, "new"))[stat.ST_MODE]
233 if not stat.S_ISDIR(mode):
234 sys.stderr.write("Broken maildir: %s\n" %(maildir))
236 os.mkdir(os.path.join(maildir, "new"))
238 sys.stderr.write("Broken maildir: %s\n" %(maildir))
243 sys.stderr.write("Couldn't create root maildir %s\n" %(maildir))
246 os.mkdir(os.path.join(maildir, "new"))
247 os.mkdir(os.path.join(maildir, "cur"))
248 os.mkdir(os.path.join(maildir, "tmp"))
250 sys.stderr.write("Couldn't create required maildir directories for %s\n" %(section,))
253 # right - we've got the directories, we've got the section, we know the
256 parse_and_deliver(maildir, section, state_dir)