neufelry-twitter-sms 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +2 -2
- data/lib/twitter-sms.rb +55 -17
- metadata +1 -1
data/VERSION.yml
CHANGED
data/lib/twitter-sms.rb
CHANGED
@@ -4,6 +4,7 @@ require 'tlsmail'
|
|
4
4
|
require 'optparse'
|
5
5
|
require 'cgi'
|
6
6
|
require 'net/pop'
|
7
|
+
require 'rmail'
|
7
8
|
|
8
9
|
if RUBY_VERSION < "1.9"
|
9
10
|
raise "Version too low. Please get Ruby 1.9"
|
@@ -48,12 +49,6 @@ class TwitterSms
|
|
48
49
|
now = Time.now
|
49
50
|
tweets = twitter.timeline(:friends, :since => @last_check)
|
50
51
|
@last_check = now
|
51
|
-
|
52
|
-
# Block own tweets if specified via settings
|
53
|
-
tweets.reject! {|t| t.user.screen_name == @user['name']} unless @config['own_tweets']
|
54
|
-
# Don't send messages from users under no_follow
|
55
|
-
tweets.reject! {|t| @config['no_follow'].member?(t.user.screen_name) }
|
56
|
-
tweets.reverse # reverse-chronological
|
57
52
|
else
|
58
53
|
putd "Your account has run out of API calls; call not made."
|
59
54
|
end
|
@@ -62,6 +57,14 @@ class TwitterSms
|
|
62
57
|
end
|
63
58
|
end
|
64
59
|
|
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
|
67
|
+
|
65
68
|
# Email via Gmail SMTP any tweets to the desired cell phone
|
66
69
|
def send(tweets)
|
67
70
|
putd "Sending received tweets..."
|
@@ -83,6 +86,15 @@ class TwitterSms
|
|
83
86
|
end
|
84
87
|
end
|
85
88
|
|
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
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
86
98
|
private
|
87
99
|
|
88
100
|
# Put Debug (prepend time) if debug printouts turned on
|
@@ -90,17 +102,39 @@ class TwitterSms
|
|
90
102
|
puts "#{Time.now.strftime("(%b %d - %H:%M:%S)")} #{message}" if @config['debug']
|
91
103
|
end
|
92
104
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
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)
|
109
|
+
|
110
|
+
#must be extracted before we reduce to plaintext
|
111
|
+
from = r_msg.header.from[0].address
|
112
|
+
|
113
|
+
plain = reduce_to_plaintext(r_msg)
|
114
|
+
body = plain.body
|
115
|
+
|
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
|
122
|
+
else
|
123
|
+
body.scan(/ignore (\w+)/) {|_| @config['no_follow'] << $1 }
|
124
|
+
body.scan(/follow (\w+)/) {|_| @config['no_follow'] -= [$1] } # also set follow
|
98
125
|
end
|
99
126
|
end
|
100
127
|
end
|
101
128
|
|
102
|
-
|
103
|
-
|
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"
|
134
|
+
end
|
135
|
+
else
|
136
|
+
r_msg
|
137
|
+
end
|
104
138
|
end
|
105
139
|
|
106
140
|
# Parse and store a config file (either as an initial load or as
|
@@ -172,6 +206,7 @@ class TwitterSms
|
|
172
206
|
opts.parse!(args)
|
173
207
|
end
|
174
208
|
|
209
|
+
# Config is stale if a newer version exists in the filesystem than last checked
|
175
210
|
def config_stale?
|
176
211
|
return File.mtime(@config_file['name']) > @config_file['modified_at']
|
177
212
|
end
|
@@ -185,9 +220,12 @@ class TwitterSms
|
|
185
220
|
"#{tweet.user.screen_name}: #{CGI.escapeHTML(tweet.text)}"
|
186
221
|
end
|
187
222
|
|
188
|
-
|
223
|
+
def self.filename(path)
|
224
|
+
path.split('/')[-1]
|
225
|
+
end
|
189
226
|
|
190
|
-
|
191
|
-
|
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
|
+
end
|
192
231
|
end
|
193
|
-
|