neufelry-twitter-sms 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 4
3
- :patch: 0
3
+ :patch: 1
4
4
  :major: 0
data/bin/twitter-sms CHANGED
@@ -2,6 +2,6 @@
2
2
  require 'rubygems'
3
3
  require 'twitter-sms'
4
4
 
5
- program = TwitterSms.new
5
+ program = TwitterSms::Checker.new
6
6
  program.run
7
7
 
data/lib/logger.rb ADDED
@@ -0,0 +1,32 @@
1
+ require 'fileutils'
2
+ module TwitterSms
3
+ class Logger
4
+ def initialize(filename,log_size=5)
5
+ @log_size=log_size
6
+ @raw_filename = filename
7
+ @file = File.new(filename, "a") # 2x check mode
8
+
9
+ write_intro
10
+ end
11
+
12
+ def log(message)
13
+ @file.puts("#{Time.now.strftime("(%b %d - %H:%M:%S)")} #{message}")
14
+
15
+ if `du -sm #{@raw_filename}`.split[0].to_i > @log_size #mb
16
+ remake_file
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def write_intro
23
+ str = "Twitter-sms log file of messages since #{Time.now}"
24
+ @file.puts(str)
25
+ end
26
+
27
+ def remake_file
28
+ FileUtils.rm @raw_filename
29
+ initialize
30
+ end
31
+ end
32
+ end
data/lib/twitter-sms.rb CHANGED
@@ -6,226 +6,248 @@ require 'cgi'
6
6
  require 'net/pop'
7
7
  require 'rmail'
8
8
 
9
+ require 'logger.rb'
10
+
9
11
  if RUBY_VERSION < "1.9"
10
12
  raise "Version too low. Please get Ruby 1.9"
11
13
  end
12
14
 
13
- SEC_PER_HOUR = 3600 # Seconds per hour (don't like this...)
14
-
15
- class TwitterSms
15
+ module TwitterSms
16
+ SEC_PER_HOUR = 3600 # Seconds per hour (don't like this...)
16
17
 
17
- def initialize(config_file="#{ENV['HOME'] + '/.twitter-sms.conf'}", args=ARGV)
18
- load_config(config_file)
19
- load_opts(args) # Loaded options are intended to overide defaults
20
-
21
- # Required for gmail smtp (and not a part of standalone Net::SMTP)
22
- Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
18
+ # RMail doesn't like parsing body properly if there is a mistaken ' ' between body and header
19
+ def self.space_stripper!(msg)
20
+ msg.gsub!(/^\s+$/,'')
23
21
  end
24
22
 
25
- # Run the twitter-sms bot once or repeatedly, depending on config
26
- def run
27
- begin # do-while
28
- load_config if config_stale? unless @config['dont_refresh']
23
+ class Checker
29
24
 
30
- if @config['active']
31
- tweets = update
32
- send tweets unless tweets.nil?
33
- end
25
+ def initialize(config_file="#{ENV['HOME']}/.twitter-sms.conf", args=ARGV)
26
+ load_config(config_file)
27
+ load_opts(args) # Loaded options are intended to overide defaults
34
28
 
35
- if @config['keep_alive']
36
- putd "Waiting for ~#{@config['wait']/60.0} minutes..."
37
- Kernel.sleep @config['wait']
38
- end
39
- end while @config['keep_alive']
40
- tweets # Return the last set of tweets we get
41
- end
29
+ # Required for gmail smtp (and not a part of standalone Net::SMTP)
30
+ Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
31
+ end
42
32
 
43
- # Collect a list of recent tweets on a user's timeline (that are not their own)
44
- def update
45
- twitter = Twitter::Base.new(@user['name'],@user['password'])
33
+ # Run the twitter-sms bot once or repeatedly, depending on config
34
+ def run
35
+ begin # do-while
36
+ load_config if config_stale? unless @config['dont_refresh']
46
37
 
47
- begin
48
- if twitter.rate_limit_status.remaining_hits > 0
49
- now = Time.now
50
- tweets = twitter.timeline(:friends, :since => @last_check)
51
- @last_check = now
52
- else
53
- putd "Your account has run out of API calls; call not made."
54
- end
55
- rescue
56
- putd "Error occured retreiving timeline. Perhaps Internet is down?"
57
- end
58
- end
38
+ receive_messages
59
39
 
60
- def reduce_tweets(tweets)
61
- # Block own tweets if specified via settings
62
- tweets.reject! {|t| t.user.screen_name == @user['name']} unless @config['own_tweets']
63
- # Don't send messages from users under no_follow
64
- tweets.reject! {|t| @config['no_follow'].member?(t.user.screen_name) }
65
- tweets.reverse # reverse-chronological
66
- end
40
+ if @config['active']
41
+ tweets = update
42
+ send tweets unless tweets.nil?
43
+ end
67
44
 
68
- # Email via Gmail SMTP any tweets to the desired cell phone
69
- def send(tweets)
70
- putd "Sending received tweets..."
71
- begin
72
- # Start smtp connection to provided bot email
73
- Net::SMTP.start('smtp.gmail.com', 587, 'gmail.com',
74
- @bot['email'], @bot['password'], :login) do |smtp|
75
-
76
- tweets.each do |tweet|
77
- smtp.send_message(content(tweet),@bot['email'],@user['phone']) rescue putd "Error occured sending message:"
78
- putd "\tSent: #{tweet.user.screen_name}: #{tweet.text[0..20]}..."
45
+ if @config['keep_alive']
46
+ putd "Waiting for ~#{@config['wait']/60.0} minutes..."
47
+ Kernel.sleep @config['wait']
79
48
  end
49
+ end while @config['keep_alive']
50
+ tweets # Return the last set of tweets we get
51
+ end
80
52
 
53
+ # Collect a list of recent tweets on a user's timeline (that are not their own)
54
+ def update
55
+ twitter = Twitter::Base.new(@user['name'],@user['password'])
56
+
57
+ begin
58
+ if twitter.rate_limit_status.remaining_hits > 0
59
+ now = Time.now
60
+ tweets = twitter.timeline(:friends, :since => @last_check)
61
+ @last_check = now
62
+ else
63
+ putd "Your account has run out of API calls; call not made."
64
+ end
65
+ rescue
66
+ putd "Error occured retreiving timeline. Perhaps Internet is down?"
81
67
  end
82
- putd "Messages sent."
83
- rescue
84
- putd "Error occured starting smtp. Perhaps account info is incorrect" +
85
- " or Internet is down?"
68
+ return tweets
86
69
  end
87
- end
88
70
 
89
- def receive_messages
90
- Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE) # maybe verify...
91
- Net::POP3.start('pop.gmail.com',995, @bot['email'], @bot['password']) do |pop|
92
- pop.each_mail do |mail|
93
- process_pop_message(mail.pop)
94
- end
71
+ def reduce_tweets(tweets)
72
+ # Block own tweets if specified via settings
73
+ tweets.reject! {|t| t.user.screen_name == @user['name']} unless @config['own_tweets']
74
+ # Don't send messages from users under no_follow
75
+ tweets.reject! {|t| @config['no_follow'].member?(t.user.screen_name) }
76
+ tweets.reverse # reverse-chronological
95
77
  end
96
- end
97
78
 
98
- private
79
+ # Email via Gmail SMTP any tweets to the desired cell phone
80
+ def send(tweets)
81
+ putd "Sending received tweets..."
82
+ begin
83
+ # Start smtp connection to provided bot email
84
+ Net::SMTP.start('smtp.gmail.com', 587, 'gmail.com',
85
+ @bot['email'], @bot['password'], :login) do |smtp|
99
86
 
100
- # Put Debug (prepend time) if debug printouts turned on
101
- def putd (message)
102
- puts "#{Time.now.strftime("(%b %d - %H:%M:%S)")} #{message}" if @config['debug']
103
- end
87
+ tweets.each do |tweet|
88
+ smtp.send_message(content(tweet),@bot['email'],@user['phone']) rescue putd "Error occured sending message:"
89
+ putd "\tSent: #{tweet.user.screen_name}: #{tweet.text[0..20]}..."
90
+ end
104
91
 
105
- # Verify syntax for dissecting pop message
106
- def process_pop_message(msg)
107
- TwitterSms::space_stripper!(msg) # Remove sometimes broken extra spaces
108
- r_msg = RMail::Parser.read(msg)
92
+ end
93
+ putd "Messages sent."
94
+ rescue
95
+ putd "Error occured starting smtp. Perhaps account info is incorrect" +
96
+ " or Internet is down?"
97
+ putd " Message was: #{$!}"
98
+ end
99
+ end
109
100
 
110
- #must be extracted before we reduce to plaintext
111
- from = r_msg.header.from[0].address
101
+ def receive_messages
102
+ Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE) # maybe verify...
103
+ Net::POP3.start('pop.gmail.com',995, @bot['email'], @bot['password']) do |pop|
104
+ pop.each_mail do |mail|
105
+ process_pop_message(mail.pop)
106
+ end
107
+ end
108
+ end
112
109
 
113
- plain = reduce_to_plaintext(r_msg)
114
- body = plain.body
110
+ private
115
111
 
116
- if from == @user['phone'] # must come from phone
117
- # Extract this log later
118
- if body =~ /^off\w*/i
119
- @config['active'] = false
120
- elsif body =~ /^on\w*/i
121
- @config['active'] = true
112
+ # Put Debug (prepend time) if debug printouts turned on
113
+ def putd (message)
114
+ if @config['debug']
115
+ if @config['log_to'] == 'file'
116
+ @logger ||= TwitterSms::Logger.new("#{ENV['HOME']}/.twitter_sms-log")
117
+ @logger.log(message)
118
+ elsif @config['log_to'] == 'console'
119
+ puts "#{Time.now.strftime("(%b %d - %H:%M:%S)")} #{message}"
120
+ end
122
121
  else
123
- body.scan(/ignore (\w+)/) {|_| @config['no_follow'] << $1 }
124
- body.scan(/follow (\w+)/) {|_| @config['no_follow'] -= [$1] } # also set follow
122
+ puts message
125
123
  end
126
124
  end
127
- end
128
125
 
129
- # Sometimes Rmail messages are multipart, we only want plaintext
130
- def reduce_to_plaintext(r_msg)
131
- if r_msg.multipart?
132
- r_msg = r_msg.body.find do |part|
133
- part.header.content_type == "text/plain"
126
+ # Verify syntax for dissecting pop message
127
+ def process_pop_message(msg)
128
+ TwitterSms::space_stripper!(msg) # Remove sometimes broken extra spaces
129
+ r_msg = RMail::Parser.read(msg)
130
+
131
+ #must be extracted before we reduce to plaintext
132
+ from = r_msg.header.from[0].address
133
+
134
+ plain = reduce_to_plaintext(r_msg)
135
+ body = plain.body
136
+
137
+ if from == @user['phone'] # must come from phone
138
+ # Extract this log later
139
+ if body =~ /^off\w*/i
140
+ @config['active'] = false
141
+ putd "Received command to disable texting"
142
+ elsif body =~ /^on\w*/i
143
+ @config['active'] = true
144
+ putd "Received command to enable texting"
145
+ else
146
+ body.scan(/ignore (\w+)/i) {|_| @config['no_follow'] << $1 }
147
+ body.scan(/follow (\w+)/i) {|_| @config['no_follow'] -= [$1] } # also set follow
148
+ end
134
149
  end
135
- else
136
- r_msg
137
150
  end
138
- end
139
151
 
140
- # Parse and store a config file (either as an initial load or as
141
- # an update)
142
- def load_config(config_file=@config_file['name'])
143
- # Load config file -- Add try block here
144
- loaded_config = YAML.load_file(config_file)
145
- @config_file = { 'name' => config_file,
146
- 'modified_at' => File.mtime(config_file) }
147
-
148
- # Seperate config hashes into easier to use parts
149
- @user = loaded_config['user']
150
- @bot = loaded_config['bot']
151
-
152
- config_defaults = { 'own_tweets' => false,
153
- 'keep_alive' => true,
154
- 'per_hour' => 30,
155
- 'debug' => false,
156
- 'dont_refresh' => false,
157
- 'active' => true}
158
-
159
- # Merge specified config onto defaults
160
- @config = config_defaults.merge(loaded_config['config'])
161
-
162
- set_wait
163
- @last_check = Time.now - @config['wait']
164
- putd "Loaded config file"
165
- end
152
+ # Sometimes Rmail messages are multipart, we only want plaintext
153
+ def reduce_to_plaintext(r_msg)
154
+ if r_msg.multipart?
155
+ r_msg = r_msg.body.find do |part|
156
+ part.header.content_type == "text/plain"
157
+ end
158
+ else
159
+ r_msg
160
+ end
161
+ end
166
162
 
167
- # Set wait time based on times per hour
168
- def set_wait
169
- @config['wait'] = SEC_PER_HOUR / @config['per_hour']
170
- end
163
+ # Parse and store a config file (either as an initial load or as
164
+ # an update)
165
+ def load_config(config_file=@config_file['name'])
166
+ # Load config file -- Add try block here
167
+ loaded_config = YAML.load_file(config_file)
168
+ @config_file = { 'name' => config_file,
169
+ 'modified_at' => File.mtime(config_file) }
170
+
171
+ # Seperate config hashes into easier to use parts
172
+ @user = loaded_config['user']
173
+ @bot = loaded_config['bot']
174
+
175
+ config_defaults = { 'own_tweets' => false,
176
+ 'keep_alive' => true,
177
+ 'per_hour' => 30,
178
+ 'debug' => false,
179
+ 'dont_refresh' => false,
180
+ 'active' => true,
181
+ 'log_to' => 'file'}
182
+
183
+ # Merge specified config onto defaults
184
+ @config = config_defaults.merge(loaded_config['config'])
185
+
186
+ set_wait
187
+ @last_check = Time.now - @config['wait']
188
+ putd "Loaded config file"
189
+ end
171
190
 
172
- def load_opts(args)
173
- opts = OptionParser.new do |opts|
174
- opts.banner = "Twitter-sms bot help menu:\n"
175
- opts.banner += "==========================\n"
176
- opts.banner += "Usage #$0 [options]"
191
+ # Set wait time based on times per hour
192
+ def set_wait
193
+ @config['wait'] = SEC_PER_HOUR / @config['per_hour']
194
+ end
177
195
 
178
- # The problem with this is if the asked for config file doesn't exist
179
- opts.on('-c', '--config-file [FILE]',
180
- 'Location of the config file to be loaded') do |filename|
181
- load_config(filename)
182
- end
196
+ def load_opts(args)
197
+ options = OptionParser.new do |opts|
198
+ opts.banner = "Twitter-sms bot help menu:\n"
199
+ opts.banner += "==========================\n"
200
+ opts.banner += "Usage #$0 [options]"
183
201
 
184
- opts.on('-t', '--times-per-hour [TIMES]',
185
- 'Indicate the amount of times per hour to check (> 0)') do |times|
186
- times = 1 if times <= 0
187
- @config['per_hour'] = times
188
- end
202
+ # The problem with this is if the asked for config file doesn't exist
203
+ opts.on('-c', '--config-file [FILE]',
204
+ 'Location of the config file to be loaded') do |filename|
205
+ load_config(filename)
206
+ end
189
207
 
190
- opts.on('-o', '--own-tweets',
191
- "Makes your own tweets send in addition to followed account\'s tweets") do
192
- @config['own_tweets'] = true
193
- end
208
+ opts.on('-t', '--times-per-hour [TIMES]',
209
+ 'Indicate the amount of times per hour to check (> 0)') do |times|
210
+ times = 1 if times <= 0
211
+ @config['per_hour'] = times
212
+ end
194
213
 
195
- opts.on('-s', '--single-check',
196
- 'Forces the program to only check once, instead of continually') do
197
- @config['keep_alive'] = false
198
- end
214
+ opts.on('-o', '--own-tweets',
215
+ "Makes your own tweets send in addition to followed account\'s tweets") do
216
+ @config['own_tweets'] = true
217
+ end
199
218
 
200
- opts.on_tail('-h', '--help', 'display this help and exit') do
201
- puts opts
202
- exit
219
+ opts.on('-s', '--single-check',
220
+ 'Forces the program to only check once, instead of continually') do
221
+ @config['keep_alive'] = false
222
+ end
223
+
224
+ opts.on_tail('-h', '--help', 'display this help and exit') do
225
+ puts opts
226
+ exit
227
+ end
203
228
  end
204
- end
205
229
 
206
- opts.parse!(args)
207
- end
230
+ options.parse!(args)
231
+ end
208
232
 
209
- # Config is stale if a newer version exists in the filesystem than last checked
210
- def config_stale?
211
- return File.mtime(@config_file['name']) > @config_file['modified_at']
212
- end
233
+ # Config is stale if a newer version exists in the filesystem than last checked
234
+ def config_stale?
235
+ return File.mtime(@config_file['name']) > @config_file['modified_at']
236
+ end
213
237
 
214
- # Produce the content of the SMTP message to be sent
215
- def content(tweet)
216
- "From: #{@bot['email']}\n"+
217
- "To: #{@user['phone']}\n"+
218
- #On date line 2 '\n's are required for proper message
219
- "Date: #{Time.parse(tweet.created_at).rfc2822}\n\n"+
220
- "#{tweet.user.screen_name}: #{CGI.escapeHTML(tweet.text)}"
221
- end
238
+ # Produce the content of the SMTP message to be sent
239
+ def content(tweet)
240
+ "From: #{@bot['email']}\n"+
241
+ "To: #{@user['phone']}\n"+
242
+ #On date line 2 '\n's are required for proper message
243
+ "Date: #{Time.parse(tweet.created_at).rfc2822}\n\n"+
244
+ "#{tweet.user.screen_name}: #{CGI.escapeHTML(tweet.text)}"
245
+ end
222
246
 
223
- def self.filename(path)
224
- path.split('/')[-1]
225
- end
247
+ def self.filename(path)
248
+ path.split('/')[-1]
249
+ end
226
250
 
227
- # RMail doesn't like parsing body properly if there is a mistaken ' ' between body and header
228
- def self.space_stripper!(msg)
229
- msg.gsub!(/^\s+$/,'')
230
251
  end
231
252
  end
253
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neufelry-twitter-sms
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Neufeld
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-22 00:00:00 -08:00
12
+ date: 2009-01-27 00:00:00 -08:00
13
13
  default_executable: twitter-sms
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -42,6 +42,7 @@ files:
42
42
  - README.md
43
43
  - VERSION.yml
44
44
  - bin/twitter-sms
45
+ - lib/logger.rb
45
46
  - lib/twitter-sms.rb
46
47
  - test/test_helper.rb
47
48
  - test/twitter_sms_test.rb