fake_smtp 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fake_smtp.gemspec
4
+ gemspec
5
+
6
+ gem 'rspec'
7
+ gem 'activesupport'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ FakeSmtp
2
+ =======
3
+
4
+ This is a completely trivial SMTP server, written in Ruby and packaged as a gem.
5
+
6
+ Why?
7
+ ----
8
+
9
+ Sometimes you just want to be sure that the mail is getting formated and
10
+ sent the way you think it is.
11
+
12
+ Usage
13
+ -----
14
+
15
+ The gem comes with a simple script `fake_smtp` that will run the server.
16
+ To run the server, just run the script:
17
+
18
+ $ gem install fake_smtp
19
+
20
+ $ fake_smtp
21
+ FakeSmtp: Listening on port 2025
22
+
23
+ As you can see, by default the server runs on port 2025. You can change
24
+ the port by passing in the --port option:
25
+
26
+ $ fake_smtp --port 3333
27
+ FakeSmtp: Listening on port 3333
28
+
29
+ The server implements just enough of the SMTP protocol to convince
30
+ an SMTP client (well ActiveMailer anyway) that there is a *there* there.
31
+
32
+ Contributors
33
+ ------------
34
+
35
+ * Russ Olsen
36
+ * I also shamelessly borrowed bits, including some of this readme file
37
+ from `https://github.com/livinginthepast/fake_ftp.git`.
38
+
39
+ License
40
+ -------
41
+
42
+ The MIT License
43
+
44
+ Copyright (c) 2013 by Russ Olsen
45
+
46
+ Permission is hereby granted, free of charge, to any person obtaining a copy
47
+ of this software and associated documentation files (the "Software"), to deal
48
+ in the Software without restriction, including without limitation the rights
49
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
50
+ copies of the Software, and to permit persons to whom the Software is
51
+ furnished to do so, subject to the following conditions:
52
+
53
+ The above copyright notice and this permission notice shall be included in
54
+ all copies or substantial portions of the Software.
55
+
56
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
57
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
58
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
59
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
60
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
61
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
62
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core'
3
+ require 'rspec/core/rake_task'
4
+ require 'rake/clean'
5
+
6
+ CLEAN.include('pkg')
7
+
8
+ RSpec::Core::RakeTask.new('spec')
9
+
10
+ task :default => :build
data/bin/fake_smtp ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'fake_smtp'
5
+
6
+ options = {port: 2025}
7
+
8
+ parser = OptionParser.new do |opts|
9
+ opts.on("-p N", "--port N", Float, "Listen on port") do |n|
10
+ options[:port] = n.to_i
11
+ end
12
+ end
13
+
14
+ parser.parse!
15
+
16
+ FakeSmtp::Server.new(options).run
data/fake_smtp.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fake_smtp/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "fake_smtp"
8
+ gem.version = FakeSmtp::VERSION
9
+ gem.authors = ["Russ Olsen"]
10
+ gem.email = ["russ@russolsen.com"]
11
+ gem.description = %q{A minimal, do-nothing SMTP server}
12
+ gem.summary = %q{A fake SMTP server}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,100 @@
1
+ require 'socket'
2
+ require 'active_support/core_ext'
3
+
4
+ Thread::abort_on_exception = true
5
+
6
+ module FakeSmtp
7
+ class Server
8
+ def initialize(options={})
9
+ @port = options[:port]
10
+ end
11
+
12
+ def run
13
+ server = TCPServer.open(@port)
14
+ puts "FakeSmtp: Listening on port #{@port}"
15
+ loop {Thread.start(server.accept) {|socket| SmtpSession.new(socket).run}}
16
+ end
17
+ end
18
+
19
+ class Session
20
+ attr_accessor :done
21
+ attr_accessor :cmd
22
+
23
+ class_attribute :handlers
24
+
25
+ self.handlers = {}
26
+
27
+ def initialize(socket)
28
+ @done = false
29
+ @socket = socket
30
+ end
31
+
32
+ def self.on(regular_expression, &action)
33
+ handlers[regular_expression] = action
34
+ end
35
+
36
+ def run
37
+ log("New session: #{@socket}")
38
+
39
+ handlers[:start].call if handlers[:start]
40
+ until done
41
+ break unless cmd = read_line
42
+ handle_command cmd
43
+ end
44
+ handlers[:end].call if handlers[:end]
45
+
46
+ @socket.close
47
+ log("Session finished: #{@socket}")
48
+ end
49
+
50
+ def handle_command(command)
51
+ self.cmd = command
52
+ handlers.keys.each do |re|
53
+ next unless re.kind_of? Regexp
54
+ if re =~ command
55
+ instance_eval &handlers[re]
56
+ break
57
+ end
58
+ end
59
+ end
60
+
61
+ def log(*args)
62
+ puts args.join(' ')
63
+ end
64
+
65
+ def respond_with(msg)
66
+ @socket.puts msg
67
+ end
68
+
69
+ def read_line
70
+ line = @socket.gets
71
+ line = line ? line.rstrip : line
72
+ log('READ: ', line)
73
+ line
74
+ end
75
+ end
76
+
77
+ class SmtpSession < Session
78
+ on /^HELO|^EHLO/ do
79
+ words = cmd.split(' ')
80
+ respond_with "Hello #{words[1]} I am glad to meet you"
81
+ end
82
+
83
+ on /^QUIT/ do
84
+ respond_with('221 Bye')
85
+ self.done = true
86
+ end
87
+
88
+ on /^DATA/ do
89
+ respond_with '354 End data with <CR><LF>.<CR><LF>'
90
+ while line = read_line
91
+ break if line == '.'
92
+ end
93
+ respond_with("250 OK: queued as #{rand(1000)}")
94
+ end
95
+
96
+ on /.*/ do
97
+ respond_with("250 OK")
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,3 @@
1
+ module FakeSmtp
2
+ VERSION = "0.0.1"
3
+ end
data/lib/fake_smtp.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'fake_smtp/server'
2
+
3
+ module FakeSmtp
4
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fake_smtp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Russ Olsen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-10 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A minimal, do-nothing SMTP server
15
+ email:
16
+ - russ@russolsen.com
17
+ executables:
18
+ - fake_smtp
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - bin/fake_smtp
28
+ - fake_smtp.gemspec
29
+ - lib/fake_smtp.rb
30
+ - lib/fake_smtp/server.rb
31
+ - lib/fake_smtp/version.rb
32
+ homepage: ''
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 1.8.10
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: A fake SMTP server
56
+ test_files: []