-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnote.rb
136 lines (120 loc) · 3.36 KB
/
note.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env ruby
require 'rubygems'
require "pp"
require 'net/imap'
require 'time'
# Net::IMAP.debug = true
trap("SIGINT") { throw :ctrl_c }
trap("INT") { throw :ctrl_c }
#
# Notes is a siri interface
#
# we use siri to create notes. the default storeage for notes
# must be the imap server (icloud or an other) we connect to.
#
# we connect once and use the idle feature in imap to wait for
# other commands/messages.
#
# the notes includes commands we can define
# is a string found, we can bind it to a command that will be executed
#
class Notes
def initialize(username, passwd, host = 'imap.mail.me.com', port = 993, ssl = true )
@imap = Net::IMAP.new host, port, ssl
@imap.login username, passwd
@imap.select 'Notes'
@commands ||= []
end
# ----------------------------------------
# some dsl calling
# ----------------------------------------
def build &block
return unless block_given?
instance_eval( &block )
self
end
# ----------------------------------------
# only avaliable command for dsl
# ----------------------------------------
def add_command( func, commands )
@commands.push( { func => commands } )
end
# ----------------------------------------
# did we receive an command we've created
# ----------------------------------------
def find_and_exec_command( subject, text )
commands = @commands.select do | cmd |
f = cmd.values.flatten.select { | c | text.match( /#{c}/ ) }
f += cmd.values.flatten.select { | c | subject.match( /#{c}/ ) } if subject
cmd if f.size > 0
end.each do | item |
return eval( "#{item.keys.first}()" )
end
false
end
# ----------------------------------------
# after all initialisation is done, we run
# ----------------------------------------
def run( keyword = 'iphone' &block )
loop do
@imap.idle do | resp |
if resp.kind_of?( Net::IMAP::UntaggedResponse ) and resp.name == "EXISTS"
@imap.idle_done
end
end
# @list = @imap.sort(['DATE'], ['ALL'], 'US-ASCII').reverse
@list = @imap.uid_search( ["SUBJECT", keyword ] )
if block_given?
@list.each do | id |
get id, &block
end
end
@imap.expunge
end
self
end
def delete( uid )
@imap.uid_store( uid, "+FLAGS", [:Deleted] )
end
def unseen( uid )
@imap.uid_store( uid, "+FLAGS", [:Seen] )
@imap.uid_store( uid, "-FLAGS", [:Deleted] )
end
def close
@imap.logout
@imap.disconnect
end
def get( num, &block )
begin
if note = @imap.uid_fetch( num, [ 'UID', 'RFC822.TEXT', 'RFC822.HEADER'])
note = note.first
uid = note.attr[ "UID" ]
text = note.attr[ "RFC822.TEXT" ]
header = note.attr[ "RFC822.HEADER" ]
res = header.split(/\r\n/).find { | line | line.match(/\ASubject:/) }
if res and data = res.split(/:\s+/)
subject = data[ 1 ]
else
subject = nil
end
if block_given?
param = { :uid => uid, :text => text, :header => header }
yield param
end
ret = find_and_exec_command( subject, text )
if ret
delete( uid )
else
unseen( uid )
end
end
rescue Exception => e
puts "- in rescue"
note = nil
pp e
end
end
end
#catch :ctrl_c do
# note.close
#end