Commit 6d74112b6458b2b33c75242d9f0a782adfc05a9a

Authored by Niklas
0 parents
Exists in master

init

Showing 17 changed files with 357 additions and 0 deletions Side-by-side Diff

... ... @@ -0,0 +1,5 @@
  1 +*~
  2 +\#*
  3 +.#*
  4 +.sass-cache
  5 +*.sqlite
... ... @@ -0,0 +1,24 @@
  1 +# Require any additional compass plugins here.
  2 +
  3 +# Set this to the root of your project when deployed:
  4 +http_path = "/"
  5 +css_dir = "public/stylesheets"
  6 +sass_dir = "sass"
  7 +images_dir = "public/images"
  8 +javascripts_dir = "public/javascripts"
  9 +
  10 +# You can select your preferred output style here (can be overridden via the command line):
  11 +# output_style = :expanded or :nested or :compact or :compressed
  12 +
  13 +# To enable relative paths to assets via compass helper functions. Uncomment:
  14 +# relative_assets = true
  15 +
  16 +# To disable debugging comments that display the original location of your selectors. Uncomment:
  17 +# line_comments = false
  18 +
  19 +
  20 +# If you prefer the indented syntax, you might want to regenerate this
  21 +# project again passing --syntax sass, or you can uncomment this:
  22 +# preferred_syntax = :sass
  23 +# and then run:
  24 +# sass-convert -R --from scss --to sass sass scss && rm -rf sass && mv scss sass
... ... @@ -0,0 +1,47 @@
  1 +# -*- mode:ruby -*-
  2 +
  3 +require 'sinatra'
  4 +require 'rack-flash'
  5 +require 'haml'
  6 +
  7 +require 'models'
  8 +
  9 +class App < Sinatra::Base
  10 + enable :sessions
  11 + use Rack::Flash
  12 +
  13 + get '/filters' do
  14 + @filters = Filter.find(:all)
  15 + haml :filters
  16 + end
  17 +
  18 + get '/filter/:id' do
  19 + if params[:id] == 'new'
  20 + @filter = Filter.new
  21 + else
  22 + @filter = Filter.find(params[:id])
  23 + end
  24 + haml :filter
  25 + end
  26 +
  27 + post '/filters' do
  28 + @filter = Filter.new(params[:filter])
  29 + @filter.save!
  30 + flash[:notice] = "Filter created."
  31 + redirect("/filters")
  32 + end
  33 +
  34 + post '/filter/:id' do
  35 + @filter = Filter.find(params[:id])
  36 + @filter.update_attributes(params[:filter])
  37 + @filter.save!
  38 + end
  39 +
  40 + helpers do
  41 + def partial(template, locals={}, options={})
  42 + haml(template, options.merge(:locals => locals, :layout => false))
  43 + end
  44 + end
  45 +end
  46 +
  47 +run App
... ... @@ -0,0 +1,7 @@
  1 +#!/usr/bin/env ruby
  2 +
  3 +require 'rubygems'
  4 +require 'models'
  5 +require 'irb'
  6 +
  7 +IRB.start
0 8 \ No newline at end of file
maildrop_serializer.rb
... ... @@ -0,0 +1,50 @@
  1 +class MaildropSerializer
  2 + def serialize(filters)
  3 + filters.map do |filter|
  4 + ["# filter '#{filter.name}'",
  5 + "if (#{filter_expressions(filter)})",
  6 + "{",
  7 + " exception {",
  8 + " to #{dest_folder(filter)}",
  9 + " }",
  10 + "}"].join("\n")
  11 + end.join("\n\n")
  12 + end
  13 +
  14 + private
  15 +
  16 + def dest_folder(filter)
  17 + '$DEFAULT/' << filter.destination_folder.sub(Regexp.new("(INBOX)(.*)"), '\2') << "/"
  18 + end
  19 +
  20 + def escape_expr_value(text)
  21 + text.gsub(".", "\\.").gsub("*", "\\*").gsub("[", "\\[").gsub("]", "\\]").gsub("(", "\\(").gsub(")", "\\)").
  22 + gsub("?", "\\?")
  23 + end
  24 +
  25 + def filter_expressions(filter)
  26 + fe = ""
  27 + for exp in filter.expressions
  28 + post_flag = "h"
  29 + fe << " && " unless fe == ""
  30 + if exp.field_name == "^Body"
  31 + fe << "/"
  32 + post_flag = "b"
  33 + else
  34 + fe << "/#{exp.field_name}:"
  35 + end
  36 + if exp.operator == 'contains'
  37 + fe << ".*(#{escape_expr_value(exp.expr_value)})/"
  38 + else
  39 + # starts with
  40 + fe << "[ ]*(#{escape_expr_value(exp.expr_value)}).*/"
  41 + end
  42 + if exp.case_sensitive == 1
  43 + fe << "D" << post_flag
  44 + else
  45 + fe << post_flag
  46 + end
  47 + end
  48 + fe
  49 + end
  50 +end
... ... @@ -0,0 +1,77 @@
  1 +require 'active_record'
  2 +
  3 +ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => "db.sqlite")
  4 +
  5 +class Expression < ActiveRecord::Base
  6 + belongs_to :filter
  7 +
  8 + validates_presence_of :field_name
  9 + validates_presence_of :operator
  10 + validates_presence_of :expr_value
  11 + validates_presence_of :filter_id
  12 +
  13 + def self.migration
  14 + Class.new(ActiveRecord::Migration).tap { |c|
  15 + c.class_eval do
  16 + def self.up
  17 + create_table :expressions do |t|
  18 + t.string :field_name
  19 + t.string :operator
  20 + t.string :expr_value
  21 + t.belongs_to(:filter)
  22 + t.boolean :case_sensitive
  23 + t.timestamps
  24 + end
  25 + end
  26 +
  27 + def self.down
  28 + drop_table :expressions
  29 + end
  30 + end
  31 + }
  32 + end
  33 +end
  34 +
  35 +class Filter < ActiveRecord::Base
  36 + has_many :expressions
  37 +
  38 + validates_associated :expressions
  39 + validates_presence_of :name
  40 + validates_presence_of :destination_folder
  41 +
  42 + def expressions=(hash_or_list)
  43 + if hash_or_list.kind_of?(Hash)
  44 + hash_or_list.each_pair do |k, v|
  45 + if k =~ /^\d+$/
  46 + expr = expressions.find(k)
  47 + else
  48 + expr = expressions.build
  49 + end
  50 + v.each_pair do |_k, _v|
  51 + expr.write_attribute(_k, _v)
  52 + end
  53 + end
  54 + else
  55 + super(hash_or_list)
  56 + end
  57 + end
  58 +
  59 + def self.migration
  60 + Class.new(ActiveRecord::Migration).tap { |c|
  61 + c.class_eval do
  62 + def self.up
  63 + create_table :filters do |t|
  64 + t.string :name
  65 + t.string :destination_folder
  66 + t.integer :order_num
  67 + t.timestamps
  68 + end
  69 + end
  70 +
  71 + def self.down
  72 + drop_table :filters
  73 + end
  74 + end
  75 + }
  76 + end
  77 +end
public/stylesheets/ie.css
... ... @@ -0,0 +1,5 @@
  1 +/* Welcome to Compass. Use this file to write IE specific override styles.
  2 + * Import this file using the following HTML or equivalent:
  3 + * <!--[if IE]>
  4 + * <link href="/stylesheets/ie.css" media="screen, projection" rel="stylesheet" type="text/css" />
  5 + * <![endif]--> */
public/stylesheets/print.css
... ... @@ -0,0 +1,3 @@
  1 +/* Welcome to Compass. Use this file to define print styles.
  2 + * Import this file using the following HTML or equivalent:
  3 + * <link href="/stylesheets/print.css" media="print" rel="stylesheet" type="text/css" /> */
public/stylesheets/screen.css
... ... @@ -0,0 +1,68 @@
  1 +/* Welcome to Compass.
  2 + * In this file you should write your main styles. (or centralize your imports)
  3 + * Import this file using the following HTML or equivalent:
  4 + * <link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css" /> */
  5 +/* line 17, ../../../../../var/lib/gems/1.8/gems/compass-0.11.5/frameworks/compass/stylesheets/compass/reset/_utilities.scss */
  6 +html, body, div, span, applet, object, iframe,
  7 +h1, h2, h3, h4, h5, h6, p, blockquote, pre,
  8 +a, abbr, acronym, address, big, cite, code,
  9 +del, dfn, em, img, ins, kbd, q, s, samp,
  10 +small, strike, strong, sub, sup, tt, var,
  11 +b, u, i, center,
  12 +dl, dt, dd, ol, ul, li,
  13 +fieldset, form, label, legend,
  14 +table, caption, tbody, tfoot, thead, tr, th, td,
  15 +article, aside, canvas, details, embed,
  16 +figure, figcaption, footer, header, hgroup,
  17 +menu, nav, output, ruby, section, summary,
  18 +time, mark, audio, video {
  19 + margin: 0;
  20 + padding: 0;
  21 + border: 0;
  22 + font-size: 100%;
  23 + font: inherit;
  24 + vertical-align: baseline;
  25 +}
  26 +
  27 +/* line 20, ../../../../../var/lib/gems/1.8/gems/compass-0.11.5/frameworks/compass/stylesheets/compass/reset/_utilities.scss */
  28 +body {
  29 + line-height: 1;
  30 +}
  31 +
  32 +/* line 22, ../../../../../var/lib/gems/1.8/gems/compass-0.11.5/frameworks/compass/stylesheets/compass/reset/_utilities.scss */
  33 +ol, ul {
  34 + list-style: none;
  35 +}
  36 +
  37 +/* line 24, ../../../../../var/lib/gems/1.8/gems/compass-0.11.5/frameworks/compass/stylesheets/compass/reset/_utilities.scss */
  38 +table {
  39 + border-collapse: collapse;
  40 + border-spacing: 0;
  41 +}
  42 +
  43 +/* line 26, ../../../../../var/lib/gems/1.8/gems/compass-0.11.5/frameworks/compass/stylesheets/compass/reset/_utilities.scss */
  44 +caption, th, td {
  45 + text-align: left;
  46 + font-weight: normal;
  47 + vertical-align: middle;
  48 +}
  49 +
  50 +/* line 28, ../../../../../var/lib/gems/1.8/gems/compass-0.11.5/frameworks/compass/stylesheets/compass/reset/_utilities.scss */
  51 +q, blockquote {
  52 + quotes: none;
  53 +}
  54 +/* line 101, ../../../../../var/lib/gems/1.8/gems/compass-0.11.5/frameworks/compass/stylesheets/compass/reset/_utilities.scss */
  55 +q:before, q:after, blockquote:before, blockquote:after {
  56 + content: "";
  57 + content: none;
  58 +}
  59 +
  60 +/* line 30, ../../../../../var/lib/gems/1.8/gems/compass-0.11.5/frameworks/compass/stylesheets/compass/reset/_utilities.scss */
  61 +a img {
  62 + border: none;
  63 +}
  64 +
  65 +/* line 114, ../../../../../var/lib/gems/1.8/gems/compass-0.11.5/frameworks/compass/stylesheets/compass/reset/_utilities.scss */
  66 +article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section, summary {
  67 + display: block;
  68 +}
... ... @@ -0,0 +1,9 @@
  1 +#!/usr/bin/env ruby
  2 +
  3 +require 'rubygems'
  4 +require 'models'
  5 +
  6 +[Expression, Filter].each do |model|
  7 + model.migration.down
  8 + model.migration.up
  9 +end
... ... @@ -0,0 +1,5 @@
  1 +/* Welcome to Compass. Use this file to write IE specific override styles.
  2 + * Import this file using the following HTML or equivalent:
  3 + * <!--[if IE]>
  4 + * <link href="/stylesheets/ie.css" media="screen, projection" rel="stylesheet" type="text/css" />
  5 + * <![endif]--> */
... ... @@ -0,0 +1,3 @@
  1 +/* Welcome to Compass. Use this file to define print styles.
  2 + * Import this file using the following HTML or equivalent:
  3 + * <link href="/stylesheets/print.css" media="print" rel="stylesheet" type="text/css" /> */
... ... @@ -0,0 +1,6 @@
  1 +/* Welcome to Compass.
  2 + * In this file you should write your main styles. (or centralize your imports)
  3 + * Import this file using the following HTML or equivalent:
  4 + * <link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css" /> */
  5 +
  6 +@import "compass/reset";
views/expression.haml
... ... @@ -0,0 +1,10 @@
  1 +
  2 +- expr = "expression[#{expression.id}]"
  3 +%div{:id => "expression_#{expression.id}"}
  4 + %select{:name => expr + "[field_name]"}
  5 + - %w(From To CC Subject Body).each do |field|
  6 + %option{:name => field, :value => field, :selected => expression.field_name == field ? "selected" : ""}/
  7 + %select{:name => expr + "[operator]"}
  8 + - ["contains", "starts with"].each do |op|
  9 + %option{:name => op, :value => op, :selected => expression.operator == op ? "selected" : ""}/
  10 + %input{:name => expr + "[expr_value]", :value => expression.expr_value}/
... ... @@ -0,0 +1,15 @@
  1 +
  2 +%form{:method => "post", :action => @filter.new_record? ? "/filters" : "/filter/#{@filter.id}"}
  3 + %label{:for => "filter[name]"}
  4 + Name
  5 + %input{:type => "text", :name => "filter[name]", :value => @filter.name}
  6 + %label{:for => "filter[destination_folder]"}
  7 + Destination Folder
  8 + %input{:type => "text", :name => "filter[destination_folder]", :value => @filter.destination_folder}
  9 + %label{:for => "expressions"}
  10 + Expressions:
  11 + %div#expressions
  12 + - @filter.expressions.each do |expression|
  13 + = partial(:expression, :expression => expression)
  14 +
  15 + %input{:type => "submit"}
0 16 \ No newline at end of file
... ... @@ -0,0 +1,7 @@
  1 +
  2 +%a{:href => "/filter/new"} New Filter
  3 +
  4 +%ul
  5 + - @filters.each do |filter|
  6 + %li
  7 + %a{:href => "/filter/#{filter.id}"}= filter.name
... ... @@ -0,0 +1,16 @@
  1 +!!!
  2 +%html
  3 + %head
  4 + %title Mail Filter
  5 + %link{:href => "/stylesheets/screen.css", :media => "screen, projection", :rel => "stylesheet", :type => "text/css"}/
  6 + %link{:href => "/stylesheets/print.css", :media => "print", :rel => "stylesheet", :type => "text/css"}/
  7 + /[if IE]
  8 + %link{:href => "/stylesheets/ie.css", :media => "screen, projection", :rel => "stylesheet", :type => "text/css"}
  9 +
  10 + %body
  11 + - %w(notice error success).each do |key|
  12 + - if flash[key.to_sym]
  13 + %p{:class => key}= flash[key.to_sym]
  14 + %div.container
  15 + = yield
  16 +