Commit 63700421c7639fb350cf330427d876b90faf56cb

Authored by Marius Hanne
1 parent 5ef599486d
Exists in master

move code out of config.ru

Showing 2 changed files with 137 additions and 136 deletions Side-by-side Diff

1 1 # -*- mode:ruby -*-
2 2  
3 3 require 'rubygems'
4   -require 'sinatra'
5   -require 'sinatra/flash'
6   -require 'haml'
7 4  
8 5 require './models'
9 6 require './maildrop_serializer'
10   -require 'net/imap'
  7 +require "./maildropui.rb"
11 8  
12   -CONFIG = YAML.load_file("config.yml")
13   -MAILSERVER = CONFIG["mailserver"]
14   -MAILDROPRCDIR = CONFIG["maildroprcdir"]
15   -
16   -
17   -class App < Sinatra::Base
18   - enable :sessions
19   - register Sinatra::Flash
20   - set :public_dir, 'public'
21   -
22   - before do
23   - unless env['PATH_INFO'] =~ /^\/login/ || session[:user]
24   - redirect '/login'
25   - end
26   - end
27   -
28   - get '/' do
29   - redirect '/filters'
30   - end
31   -
32   - get '/filters' do
33   - @filters = filters
34   - haml :filters
35   - end
36   -
37   - get '/filter/:id' do
38   - if params[:id] == 'new'
39   - @filter = Filter.new
40   - else
41   - @filter = filters.find(params[:id])
42   - end
43   - haml :filter
44   - end
45   -
46   - post '/filters' do
47   - @filter = Filter.new(params[:filter])
48   - @filter.email = session[:user]
49   - @filter.save!
50   - write_maildroprc
51   - flash[:success] = "Filter created"
52   - redirect("/filters")
53   - end
54   -
55   - post '/filter/:id' do
56   - @filter = filters.find(params[:id])
57   - @filter.update_attributes(params[:filter])
58   - flash[:success] = "Filter updated"
59   - @filter.email = session[:user]
60   - @filter.save!
61   - write_maildroprc
62   - redirect("/filters")
63   - end
64   -
65   - post '/filter/:id/delete' do
66   - @filter = filters.find(params[:id])
67   - @filter.destroy
68   - write_maildroprc
69   - flash[:notice] = "Filter deleted"
70   - redirect('/filters')
71   - end
72   -
73   - get '/expression/new' do
74   - @expression = Expression.new
75   - partial(:expression, :expression => @expression)
76   - end
77   -
78   - get '/login' do
79   - haml :login
80   - end
81   -
82   - get '/logout' do
83   - session[:user] = session[:pass] = nil
84   - session[:notice] = "You're logged out now."
85   - redirect '/'
86   - end
87   -
88   - post '/login' do
89   - begin
90   - imap(false).login(params[:email], params[:password])
91   - session[:user] = params[:email]
92   - session[:pass] = params[:password]
93   - flash[:success] = "Authentication successful"
94   - redirect '/'
95   - rescue => exc
96   - flash.now[:error] = "Authentication failed"
97   - haml :login
98   - end
99   - end
100   -
101   - helpers do
102   - def partial(template, locals={}, options={})
103   - haml(template, options.merge(:locals => locals, :layout => false))
104   - end
105   -
106   - def option(value, label, selected)
107   - "<option value=\"#{value}\"#{selected && ' selected'}>#{label}</option>"
108   - end
109   -
110   - def filters
111   - Filter.where(:email => session[:user])
112   - end
113   -
114   - def folders(parent=nil)
115   - return @folders if @folders
116   - folders = (imap.list("", parent ? "#{parent}.%" : '%') || []).map { |f|
117   - [f.name] + folders(f.name)
118   - }.flatten
119   - unless parent
120   - @folders = folders
121   - end
122   - return folders
123   - end
124   -
125   - def imap(login=true)
126   - @imap ||= Net::IMAP.new(MAILSERVER).tap { |i|
127   - i.login(session[:user], session[:pass]) if login
128   - }
129   - end
130   -
131   - def write_maildroprc
132   - user, domain = session[:user].split('@')
133   - FileUtils.mkdir_p(File.join(MAILDROPRCDIR, domain))
134   - File.open(File.join(MAILDROPRCDIR, domain, user), 'w') do |f|
135   - f.write MaildropSerializer.new.serialize(filters)
136   - end
137   - end
138   -
139   - end
140   -
141   -end
142   -
143   -run App
  9 +run MailDropUI
  1 +require 'sinatra'
  2 +require 'sinatra/flash'
  3 +require 'haml'
  4 +
  5 +require 'net/imap'
  6 +
  7 +CONFIG = YAML.load_file("config.yml")
  8 +MAILSERVER = CONFIG["mailserver"]
  9 +MAILDROPRCDIR = CONFIG["maildroprcdir"]
  10 +
  11 +class MailDropUI < Sinatra::Base
  12 + enable :sessions
  13 + register Sinatra::Flash
  14 + set :public_dir, 'public'
  15 +
  16 + before do
  17 + unless env['PATH_INFO'] =~ /^\/login/ || session[:user]
  18 + redirect '/login'
  19 + end
  20 + end
  21 +
  22 + get '/' do
  23 + redirect '/filters'
  24 + end
  25 +
  26 + get '/filters' do
  27 + @filters = filters
  28 + haml :filters
  29 + end
  30 +
  31 + get '/filter/:id' do
  32 + if params[:id] == 'new'
  33 + @filter = Filter.new
  34 + else
  35 + @filter = filters.find(params[:id])
  36 + end
  37 + haml :filter
  38 + end
  39 +
  40 + post '/filters' do
  41 + @filter = Filter.new(params[:filter])
  42 + @filter.email = session[:user]
  43 + @filter.save!
  44 + write_maildroprc
  45 + flash[:success] = "Filter created"
  46 + redirect("/filters")
  47 + end
  48 +
  49 + post '/filter/:id' do
  50 + @filter = filters.find(params[:id])
  51 + @filter.update_attributes(params[:filter])
  52 + flash[:success] = "Filter updated"
  53 + @filter.email = session[:user]
  54 + @filter.save!
  55 + write_maildroprc
  56 + redirect("/filters")
  57 + end
  58 +
  59 + post '/filter/:id/delete' do
  60 + @filter = filters.find(params[:id])
  61 + @filter.destroy
  62 + write_maildroprc
  63 + flash[:notice] = "Filter deleted"
  64 + redirect('/filters')
  65 + end
  66 +
  67 + get '/expression/new' do
  68 + @expression = Expression.new
  69 + partial(:expression, :expression => @expression)
  70 + end
  71 +
  72 + get '/login' do
  73 + haml :login
  74 + end
  75 +
  76 + get '/logout' do
  77 + session[:user] = session[:pass] = nil
  78 + session[:notice] = "You're logged out now."
  79 + redirect '/'
  80 + end
  81 +
  82 + post '/login' do
  83 + begin
  84 + imap(false).login(params[:email], params[:password])
  85 + session[:user] = params[:email]
  86 + session[:pass] = params[:password]
  87 + flash[:success] = "Authentication successful"
  88 + redirect '/'
  89 + rescue => exc
  90 + flash.now[:error] = "Authentication failed"
  91 + haml :login
  92 + end
  93 + end
  94 +
  95 + helpers do
  96 + def partial(template, locals={}, options={})
  97 + haml(template, options.merge(:locals => locals, :layout => false))
  98 + end
  99 +
  100 + def option(value, label, selected)
  101 + "<option value=\"#{value}\"#{selected && ' selected'}>#{label}</option>"
  102 + end
  103 +
  104 + def filters
  105 + Filter.where(:email => session[:user])
  106 + end
  107 +
  108 + def folders(parent=nil)
  109 + return @folders if @folders
  110 + folders = (imap.list("", parent ? "#{parent}.%" : '%') || []).map { |f|
  111 + [f.name] + folders(f.name)
  112 + }.flatten
  113 + unless parent
  114 + @folders = folders
  115 + end
  116 + return folders
  117 + end
  118 +
  119 + def imap(login=true)
  120 + @imap ||= Net::IMAP.new(MAILSERVER).tap { |i|
  121 + i.login(session[:user], session[:pass]) if login
  122 + }
  123 + end
  124 +
  125 + def write_maildroprc
  126 + user, domain = session[:user].split('@')
  127 + FileUtils.mkdir_p(File.join(MAILDROPRCDIR, domain))
  128 + File.open(File.join(MAILDROPRCDIR, domain, user), 'w') do |f|
  129 + f.write MaildropSerializer.new.serialize(filters)
  130 + end
  131 + end
  132 +
  133 + end
  134 +
  135 +end