efreesms 0.0.1

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.
data/bin/sms ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'efreesms/bin'
4
+
5
+ EFreeSMS::Bin.run
@@ -0,0 +1,84 @@
1
+ require 'RMagick'
2
+ require 'tempfile'
3
+ require 'nokogiri'
4
+ require 'httpclient'
5
+
6
+ require 'efreesms/version'
7
+
8
+ class EFreeSMS
9
+ class Captcha
10
+ def self.download (browser, dest)
11
+ File.open(dest, 'w') {|file|
12
+ browser.get('http://www.e-freesms.com/captcha.php', {}, {'Referer' => 'http://www.e-freesms.com/sms.php'}) {|data|
13
+ file.write(data)
14
+ }
15
+ }
16
+ end
17
+
18
+ def self.resolve (browser)
19
+ res = ''
20
+
21
+ Dir.mktmpdir {|d|
22
+ Dir.chdir(d) {|d|
23
+ download(browser, File.join(File.realpath(d), 'captcha.jpg'))
24
+ Magick::Image.read('captcha.jpg')[0].resize(130, 50).negate.despeckle.write('captcha.tif')
25
+ system('tesseract', 'captcha.tif', 'captcha', 2 => '/dev/null')
26
+ res = File.read('captcha.txt').strip
27
+ %w{captcha.jpg captcha.tif captcha.txt}.each {|f|
28
+ File.unlink(f)
29
+ }
30
+ }
31
+ }
32
+
33
+ return res
34
+ end
35
+ end
36
+
37
+ class SMS
38
+ class << self
39
+ def send (user, country, number, text)
40
+ $stderr.sync = true
41
+ txts = text.split(//).each_slice(120 - user.size - 2).map(&:join)
42
+
43
+ txts.each_with_index {|txt, i|
44
+ begin
45
+ $stderr.print "\rsending #{i + 1}/#{txts.size}"
46
+ real_send(country, number, ("%s: %s" % [user, txt]))
47
+ rescue Exception => e
48
+ $stderr.puts e
49
+ retry
50
+ end
51
+ }
52
+
53
+ $stderr.puts "\nSent."
54
+ end
55
+
56
+ protected
57
+ def real_send (country, number, text)
58
+ browser = HTTPClient.new(agent_name: EFreeSMS::USERAGENT)
59
+
60
+ countries = Hash[Nokogiri::HTML(browser.get('http://www.e-freesms.com/sms.php').body).xpath('//select[@name="country"]/option').select {|x| !x['value'].empty? }.map {|x| [x.text.gsub(/\s*\(.+?\)\s*/, '').downcase, x['value']] }]
61
+
62
+ country = country.to_s.downcase
63
+ country = countries[country] if countries[country]
64
+ raise "Country not valid" unless countries.values.include?(country)
65
+
66
+ body = browser.post('http://www.e-freesms.com/s4b8usba.php', {
67
+ 'country' => country,
68
+ 'phone' => (country + number),
69
+ 'text' => text,
70
+ 'vercode' => EFreeSMS::Captcha.resolve(browser),
71
+ 'submit' => 'SEND SMS'
72
+ },
73
+ {
74
+ 'Referer' => 'http://www.e-freesms.com/sms.php'
75
+ }).body
76
+
77
+ unless body =~ /Your SMS has been sent/i
78
+ $stderr.puts " ERROR, retrying."
79
+ raise
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,95 @@
1
+ require 'optparse'
2
+ require 'singleton'
3
+ require 'shellwords'
4
+
5
+ require 'efreesms'
6
+
7
+ class EFreeSMS
8
+ class Bin
9
+ include Singleton
10
+
11
+ def run
12
+ reset
13
+ parse_opts
14
+
15
+ unless @editor and @country and @number
16
+ $stderr.puts "Please, set up the client"
17
+ exit 1
18
+ end
19
+
20
+ EFreeSMS::SMS.send(@user, @country, @number, edit)
21
+ end
22
+
23
+ def reset
24
+ @editor, @country, @number, @user = ENV['EDITOR'], ENV['COUNTRY'], nil, ENV['SMSUSER']
25
+ end
26
+
27
+ def parse_opts
28
+ OptionParser.new {|opts|
29
+ opts.banner = "Usage: #$0 [options] phone"
30
+
31
+ opts.on('-e', '--editor EDITOR', 'Select editor') {|ed|
32
+ @editor = ed
33
+ }
34
+
35
+ opts.on('-c', '--country COUNTRY', 'Select recipients\' country') {|c|
36
+ @country = c
37
+ }
38
+
39
+ opts.on('-u', '--user USER', 'Select your name in sms') {|u|
40
+ @user = u
41
+ }
42
+
43
+ opts.on_tail('-v', '--version', 'Show version') {
44
+ puts "e-freesms sender #{EFreeSMS::VERSION}"
45
+ exit 0
46
+ }
47
+ }.tap {|opts|
48
+ o = opts.parse!(ARGV)
49
+
50
+ if o.size != 1
51
+ $stderr.puts opts
52
+ exit 1
53
+ end
54
+
55
+ @number = o.first
56
+ @user ||= ENV['USER']
57
+ }
58
+ end
59
+
60
+ def edit
61
+ editor = Shellwords.shellwords(@editor)
62
+ file = Tempfile.new(['sms', 'txt']).tap {|x| x.close }.path
63
+ Exec.system(*editor, file)
64
+
65
+ File.read(file).tap {
66
+ File.unlink(file)
67
+ }
68
+ end
69
+
70
+ private :reset
71
+
72
+ def self.run
73
+ instance.run
74
+ end
75
+
76
+ module Exec
77
+ module Java
78
+ def system(file, *args)
79
+ require 'spoon'
80
+ Process.waitpid(Spoon.spawnp(file, *args))
81
+ rescue Errno::ECHILD => e
82
+ raise "error exec'ing #{file}: #{e}"
83
+ end
84
+ end
85
+
86
+ module MRI
87
+ def system(file, *args)
88
+ Kernel::system(file, *args) #or raise "error exec'ing #{file}: #{$?}"
89
+ end
90
+ end
91
+
92
+ extend RUBY_PLATFORM =~ /java/ ? Java : MRI
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,4 @@
1
+ class EFreeSMS
2
+ USERAGENT = 'Mozilla/5.0 (X11; Linux x86_64; rv:6.0) Gecko/20100101 Firefox/6.0'
3
+ VERSION = '0.0.1'
4
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: efreesms
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - shura
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-09-05 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: httpclient
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: rmagick
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :runtime
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: nokogiri
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ type: :runtime
58
+ version_requirements: *id003
59
+ description: send sms via e-freesms.com
60
+ email: shura1991@gmail.com
61
+ executables:
62
+ - sms
63
+ extensions: []
64
+
65
+ extra_rdoc_files: []
66
+
67
+ files:
68
+ - lib/efreesms.rb
69
+ - lib/efreesms/bin.rb
70
+ - lib/efreesms/version.rb
71
+ - bin/sms
72
+ has_rdoc: true
73
+ homepage: http://github.com/shurizzle/ruby-efreesms
74
+ licenses: []
75
+
76
+ post_install_message:
77
+ rdoc_options: []
78
+
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ requirements: []
98
+
99
+ rubyforge_project:
100
+ rubygems_version: 1.3.7
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: send sms via e-freesms.com
104
+ test_files: []
105
+