Skip to content

Commit

Permalink
Add imap_processor and imap_to_rss
Browse files Browse the repository at this point in the history
[git-p4: depot-paths = "//src/imap_to_rss/dev/": change = 4922]
  • Loading branch information
drbrain committed May 12, 2009
0 parents commit c22244e
Show file tree
Hide file tree
Showing 13 changed files with 650 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .autotest
@@ -0,0 +1,23 @@
# -*- ruby -*-

require 'autotest/restart'

# Autotest.add_hook :initialize do |at|
# at.extra_files << "../some/external/dependency.rb"
#
# at.libs << ":../some/external"
#
# at.add_exception 'vendor'
#
# at.add_mapping(/dependency.rb/) do |f, _|
# at.files_matching(/test_.*rb$/)
# end
#
# %w(TestA TestB).each do |klass|
# at.extra_class_map[klass] = "test/test_misc.rb"
# end
# end

# Autotest.add_hook :run_command do |at|
# system "rake build"
# end
6 changes: 6 additions & 0 deletions History.txt
@@ -0,0 +1,6 @@
=== 1.0.0 / 2009-04-28

* 1 major enhancement

* Birthday!

13 changes: 13 additions & 0 deletions Manifest.txt
@@ -0,0 +1,13 @@
.autotest
History.txt
Manifest.txt
README.txt
Rakefile
bin/imap_to_rss
imap_to_rss.rss
lib/imap_to_rss.rb
lib/imap_to_rss/handler.rb
lib/imap_to_rss/handler/amazon.rb
lib/imap_to_rss/handler/hsbc.rb
lib/imap_to_rss/handler/ups.rb
test/test_imap_to_rss.rb
48 changes: 48 additions & 0 deletions README.txt
@@ -0,0 +1,48 @@
= imap_to_rss

* FIX (url)

== DESCRIPTION:

FIX (describe your package)

== FEATURES/PROBLEMS:

* FIX (list of features or problems)

== SYNOPSIS:

FIX (code sample of usage)

== REQUIREMENTS:

* FIX (list of requirements)

== INSTALL:

* FIX (sudo gem install, anything else)

== LICENSE:

(The MIT License)

Copyright (c) 2009 FIX

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13 changes: 13 additions & 0 deletions Rakefile
@@ -0,0 +1,13 @@
# -*- ruby -*-

require 'rubygems'
require 'hoe'
$:.unshift 'lib'
require 'imap_to_rss'

Hoe.new 'imap_to_rss', IMAPToRSS::VERSION do |itor|
itor.rubyforge_name = 'seattlerb'
itor.developer 'Eric Hodel', 'drbrain@example.com'
end

# vim: syntax=Ruby
6 changes: 6 additions & 0 deletions bin/imap_to_rss
@@ -0,0 +1,6 @@
#!/usr/bin/env ruby

require 'imap_to_rss'

IMAPToRSS.run ARGV

10 changes: 10 additions & 0 deletions imap_to_rss.rss
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>IMAP to RSS</title>
<link>imap://izmunuti.segment7.net</link>
<description>An RSS feed built from your IMAP server</description>
<generator>imap_to_rss version 1.0</generator>
<docs>http://cyber.law.harvard.edu/rss/rss.html</docs>
</channel>
</rss>
165 changes: 165 additions & 0 deletions lib/imap_to_rss.rb
@@ -0,0 +1,165 @@
require 'rubygems'
require 'imap_processor'
require 'tmail'
require 'nokogiri'

##
# IMAPToRSS takes messages from your mailboxes, runs them through handlers,
# then turns them into an RSS feed. Handlers can be added via a plugin system
# so long as they subclass IMAPToRSS::Handler and live in the
# imap_to_rss/handler/ directory.

class IMAPToRSS < IMAPProcessor

##
# The version of IMAPToRSS you are using

VERSION = '1.0'

##
# An RSS item for the RSS feed.

RSSItem = Struct.new :title, :description, :author, :pub_date,
:link, :category

##
# All added RSS items

attr_reader :rss_items

##
# Processes command-line options

def self.process_args(args)
extra_options = {
:Handler => nil,
}

add_move

super __FILE__, args, extra_options do |opts, options|
handlers = IMAPToRSS::Handler.handlers.map do |handler|
handler.name.split('::').last
end

opts.separator ''
opts.separator "Handlers: #{handlers.join ', '}"
opts.separator ''

opts.on( "--handler=HANDLER", handlers,
"Handler to run",
"Default: all handlers") do |handler|
options[:Handler] = handler
end
end
end

##
# Creates a new IMAPToRSS and connects to the selected server.

def initialize(options)
super

@handlers = []
@rss_items = []

connection = connect options[:Host], options[:Port], options[:SSL],
options[:Username], options[:Password], options[:Auth]

@imap = connection.imap
end

##
# Builds an RSS feed from rss_items

def build_rss
log 'Building RSS feed'
rss = Nokogiri::XML::Builder.new

rss_items = @rss_items # will work in Nokogiri > 1.2.3
host = options[:Host]

rss.rss :version => '2.0' do
rss.channel do
rss.title 'IMAP to RSS'
rss.link "imap://#{host}"
rss.description "An RSS feed built from your IMAP server"
rss.generator "imap_to_rss version #{IMAPToRSS::VERSION}"
rss.docs 'http://cyber.law.harvard.edu/rss/rss.html'

rss_items.each do |item|
rss.item do
rss.title item.title
rss.description item.description
rss.author item.author
rss.pubDate item.pub_date
rss.link item.link if item.link
rss.category item.category if item.category
end
end
end
end

open 'imap_to_rss.rss', 'w' do |io|
io.write rss.to_xml
end

log 'Saved RSS feed'
end

##
# Processes mailboxes with each handler then writes out the RSS file.

def run
handlers = IMAPToRSS::Handler.handlers

handlers.delete_if do |handler|
handler.name.split('::').last != options[:Handler]
end if options[:Handler]

handlers = handlers.map do |handler|
handler = handler.new
handler.setup self
handler
end

dest_mailbox = options[:MoveTo]

@boxes.each do |mailbox|
@imap.select mailbox
log "Selected mailbox #{mailbox}"

handlers.each do |handler|
log "Running handler #{handler.search.join ' '}"
messages = @imap.search [
'NOT', 'DELETED',
'NOT', 'KEYWORD', 'IMAP_TO_RSS',
*handler.search
]

next if messages.empty?

handled = handler.handle messages

@imap.store uids, '+FLAGS', %w[IMAP_TO_RSS]

if dest_mailbox then
@imap.copy handled, dest_mailbox
@imap.store handled, '+FLAGS', [:Deleted]
@imap.expunge
end
end

build_rss
end
end

end

plugins = Gem.find_files 'imap_to_rss/handler/*'

plugins.each do |plugin|
# strip path info to always require latest
require plugin.sub(/.*(imap_to_rss.handler.*)\.[^.]+$/, '\1')
end

63 changes: 63 additions & 0 deletions lib/imap_to_rss/handler.rb
@@ -0,0 +1,63 @@
require 'imap_to_rss'

##
# Base message handler class. Subclass this to define your own handlers.

class IMAPToRSS::Handler

##
# IMAP SEARCH command keywords to search

attr_accessor :search

@handlers = []

##
# Collect handler subclasses

def self.inherited(subclass)
@handlers << subclass
end

##
# List of found handlers

def self.handlers
@handlers
end

##
# Adds an item to the RSS feed with given parts

def add_item(title, description, author, pub_date, link = nil, category = nil)
item = IMAPToRSS::RSSItem.new title, description, author, pub_date, link,
category

@itor.rss_items << item
end

##
# Delegates to IMAPToRSS#each_message

def each_message(*args, &block)
@itor.each_message(*args, &block)
end

def handle(uids)
raise NotImplementedError, 'write me'
end

##
# Delegates to IMAPToRSS

def log(*args)
@itor.log(*args)
end

def setup(imap_to_rss)
@itor = imap_to_rss
@imap = imap_to_rss.imap
end

end

0 comments on commit c22244e

Please sign in to comment.