malm 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,93 +0,0 @@
1
- # based on copy + paste from http://snippets.dzone.com/posts/show/5152
2
- require 'gserver'
3
-
4
- class MalmSMTPServer < GServer
5
-
6
- attr_accessor :mail_log
7
- attr_accessor :message_db
8
-
9
- class Session
10
- attr_accessor :data_mode, :data_mode, :email_body, :mail_from, :rcpt_to, :subject
11
- alias :data_mode? :data_mode
12
-
13
- def initialize
14
- @data_mode = false
15
- @email_body = ""
16
- end
17
-
18
- end
19
-
20
- def serve(io)
21
- session = Session.new
22
- puts "Connected"
23
- io.print "220 hello\r\n"
24
- loop do
25
- data = io.gets
26
- puts ">>" + data
27
- ok, op = process_line(data, session)
28
- puts "<<" + op
29
- io.print op
30
- break unless ok
31
- break if io.closed?
32
- end
33
- begin
34
- io.close
35
- db_insert(session)
36
- rescue => e
37
- log "something screwed up..."
38
- log e.backtrace
39
- end
40
- end
41
-
42
- def process_line(line, session)
43
- if (session.data_mode?) && (line.chomp =~ /^\.$/)
44
- session.data_mode = false
45
- return true, "250 OK\r\n"
46
- elsif session.data_mode?
47
- session.email_body += line
48
- return true, ""
49
- elsif (line =~ /^(HELO|EHLO)/)
50
- return true, "250 and..?\r\n"
51
- elsif (line =~ /^QUIT/)
52
- return false, "221 bye\r\n"
53
- elsif (line =~ /^MAIL FROM\:/)
54
- session.mail_from = (/^MAIL FROM\:<(.+)>.*$/).match(line)[1]
55
- return true, "250 OK\r\n"
56
- elsif (line =~ /^RCPT TO\:/)
57
- session.rcpt_to = (/^RCPT TO\:<(.+)>.*$/).match(line)[1]
58
- return true, "250 OK\r\n"
59
- elsif (line =~ /^DATA/)
60
- session.data_mode = true
61
- return true, "354 Enter message, ending with \".\" on a line by itself\r\n"
62
- else
63
- return true, "500 ERROR\r\n"
64
- end
65
- end
66
-
67
- def db_insert(session)
68
- subject_regex = /^Subject\: (.+)$/
69
-
70
- subject_match = subject_regex.match(session.email_body)
71
- subject = subject_match ? subject_match[1] : ""
72
- subject.strip!
73
-
74
- message = {:subject => subject, :from => session.mail_from, :to => session.rcpt_to, :body => session.email_body}
75
-
76
- @mail_log_fd.puts(message.inspect) if @mail_log_fd
77
- @message_db.create(message) if @message_db
78
-
79
- log("Message received: #{message.inspect}")
80
- end
81
-
82
- protected
83
- def starting
84
- @mail_log_fd = File.open(@mail_log, "a") if @mail_log
85
- super
86
- end
87
-
88
- def stopping
89
- @mail_log_fd.close if @mail_log_fd
90
- super
91
- end
92
-
93
- end
data/lib/malm_web.rb DELETED
@@ -1,20 +0,0 @@
1
- require 'thin'
2
- require 'sinatra/base'
3
- require 'json'
4
-
5
- class MalmWeb < Sinatra::Base
6
- get "/" do
7
- "Hello World\n"
8
- end
9
-
10
- get "/messages" do
11
- content_type :json
12
- settings.message_db.find_all.to_json
13
- end
14
-
15
- get "/messages/:id" do
16
- content_type :json
17
- id = Integer(params[:id])
18
- settings.message_db.find(id).to_json
19
- end
20
- end
data/lib/message_db.rb DELETED
@@ -1,31 +0,0 @@
1
- require 'thread'
2
-
3
- class MessageDb
4
-
5
- def initialize
6
- @sempahore = Mutex.new
7
-
8
- @message_db = []
9
- end
10
-
11
- def create(message)
12
- @sempahore.synchronize do
13
- message[:id] = @message_db.size
14
- @message_db << message
15
- @message_db.size
16
- end
17
- end
18
-
19
- def find_all
20
- @sempahore.synchronize do
21
- @message_db.dup
22
- end
23
- end
24
-
25
- def find(id)
26
- @sempahore.synchronize do
27
- @message_db[id]
28
- end
29
- end
30
-
31
- end