cinch-imap 0.0.7 → 0.0.8

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.
Files changed (4) hide show
  1. data/CHANGES.md +4 -0
  2. data/README.md +15 -11
  3. data/lib/cinch/plugins/imap.rb +40 -20
  4. metadata +3 -3
data/CHANGES.md CHANGED
@@ -1,4 +1,8 @@
1
1
  # CHANGES
2
+ ## 0.0.8
3
+ - autostart option added
4
+ - test command added
5
+ - improved status output
2
6
 
3
7
  ## 0.0.7
4
8
  - Documentation cleanup
data/README.md CHANGED
@@ -14,27 +14,31 @@ The password
14
14
 
15
15
  ## Optional Configuration
16
16
 
17
- The IMAP port. Default is 143.
18
17
  ### :port
19
- Use SSL? Default is false.
18
+ The IMAP port. Default is 143.
20
19
  ### :ssl
21
- Number of seconds between polling. Default is 300.
20
+ Use SSL? Default is false.
22
21
  ### :interval
23
- Sets the IMAP :Seen flag on polled messages. Default is true.
22
+ Number of seconds between polling. Default is 300.
24
23
  ### :mark_as_read
24
+ Sets the IMAP :Seen flag on polled messages. Default is true.
25
+ ### :autostart
26
+ The bot will start polling when it joins the channel
25
27
 
26
28
  ## Commands
27
29
 
28
- Enable/disable IMAP polling
29
30
  !monitor on/off
30
- Outputs status information to the channel
31
+ Enable/disable IMAP polling
31
32
  !monitor status
32
- Reset the number of messages seen to 0
33
+ Outputs status information to the channel
33
34
  !monitor clear
34
- Set polling interval in seconds. Default is 300.
35
+ Reset the number of messages seen to 0
35
36
  !monitor interval [n]
36
- Outputs configuration to channel. The password attribute is skipped.
37
+ Set polling interval in seconds. Default is 300.
37
38
  !monitor showconfig
39
+ Outputs configuration to channel. The password attribute is skipped.
40
+ !monitor test
41
+ Poll the IMAP mailbox
38
42
 
39
43
  ## Example Configuration
40
44
 
@@ -49,7 +53,8 @@ Outputs configuration to channel. The password attribute is skipped.
49
53
  > c.channels = ["#mychannel"]
50
54
  > c.plugins.plugins = [Cinch::Plugins::Imap]
51
55
  > c.plugins.options[Cinch::Plugins::Imap] = {
52
- > :host => 'my.imapserver.tld',
56
+ :autostart => true,
57
+ > :host => 'my.imapserver.tld',
53
58
  > :user => 'me@fqdn.tld',
54
59
  > :password => "l3tm3out",
55
60
  > :port => 993,
@@ -78,4 +83,3 @@ eyes only.
78
83
 
79
84
  ## TODO
80
85
 
81
- - Figure out how to auto-start this thing
@@ -4,11 +4,14 @@ module Cinch
4
4
  module Plugins
5
5
  class Imap
6
6
 
7
- PLUGIN_VERSION='0.0.7'
7
+ PLUGIN_VERSION='0.0.8'
8
8
 
9
9
  include Cinch::Plugin
10
10
 
11
- match /monitor (on|off)/
11
+ listen_to :join
12
+
13
+ match /monitor (on|off)/, method: :monitor
14
+ match /monitor test/, method: :imap_test
12
15
  match /monitor status/, method: :status
13
16
  match /monitor showconfig/, method: :showconfig
14
17
  match /monitor clear/, method: :clear
@@ -30,17 +33,22 @@ module Cinch
30
33
  @from_rewrites = config[:from_rewrites] || {}
31
34
  @messages_seen = 0
32
35
  @started = Time.now
36
+ @monitor = config[:autostart] || false
33
37
  end
34
38
 
35
39
  def about(m)
36
40
  m.reply "Looks like I'm on #{PLUGIN_VERSION} #{m.user.nick}"
37
41
  end
38
42
 
39
- def interval (seconds)
40
- @interval = seconds
43
+ def interval(m, seconds)
44
+ @interval = seconds.to_i
41
45
  @monitor = @interval > 0
42
46
  end
43
47
 
48
+ def monitor(m, option)
49
+ @monitor = option == "on"
50
+ end
51
+
44
52
  def clear
45
53
  @messages_seen = 0
46
54
  end
@@ -56,14 +64,30 @@ module Cinch
56
64
  days = ((now - @started)/(3600 * 24)).to_i
57
65
  hours = ((now - @started)/3600).to_i
58
66
  minutes = ((now - @started)/60).to_i
59
- m.reply "Uptime: #{days} days, #{hours}:#{minutes} | Monitor: #{@monitor ? 'enabled' : 'disabled'} | Frequency: #{@interval.to_s} | Messages: #{@messages_seen}"
67
+ m.reply "Poller: #{@monitor ? 'on' : 'off'} | Uptime: #{days} days, #{hours}:#{minutes} | Interval: #{@interval.to_s} | # Seen: #{@messages_seen}"
60
68
  end
61
69
 
62
70
  def usage(m)
63
71
  m.user.send "Usage: !monitor <command>"
64
- m.user.send "Commands: on, off, status, clear, interval"
72
+ m.user.send "Commands: on, off, test, status, clear, interval <seconds>"
65
73
  end
66
74
 
75
+ def imap_test(m)
76
+ begin
77
+ imap = imap_connect
78
+ unseen = imap.search(["UNSEEN"]).length
79
+ old = imap.search(["NOT", "NEW"]).length
80
+ m.reply "Old: #{old} Unseen: #{unseen}"
81
+ imap.disconnect
82
+ end
83
+ end
84
+ def listen(m)
85
+ background_job do
86
+ imap = imap_connect
87
+ imap_poll(m, imap)
88
+ imap.disconnect
89
+ end if (@monitor && m.user == @bot)
90
+ end
67
91
  def get_messages(conn)
68
92
  conn.search(["UNSEEN"]).each do |message_id|
69
93
  envelope = conn.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"]
@@ -77,24 +101,22 @@ module Cinch
77
101
  yield from, subj
78
102
  end
79
103
  end
80
-
81
104
  def background_job
82
105
  loop do
83
106
  before = Time.now
84
107
  yield if @monitor
85
- interval = @interval - (Time.now - before)
86
- sleep(interval) if interval > 0
108
+ remaining = @interval - (Time.now - before)
109
+ sleep(remaining) if remaining > 0
87
110
  end
88
111
  end
89
-
90
- def execute(m, option)
91
- @monitor = option == "on"
92
-
93
- background_job do
94
- imap = Net::IMAP.new(@mail_host, @mail_port, @mail_ssl)
95
- imap.login(@mail_user, @mail_password)
96
- imap.select(@mail_folder)
97
- get_messages(imap) do |from, subj|
112
+ def imap_connect
113
+ connection = Net::IMAP.new(@mail_host, @mail_port, @mail_ssl)
114
+ connection.login(@mail_user, @mail_password)
115
+ connection.select(@mail_folder)
116
+ return connection
117
+ end
118
+ def imap_poll(m, connection)
119
+ get_messages(connection) do |from, subj|
98
120
  message_from, message_prefix = from, nil
99
121
  @from_rewrites.each do |k, v|
100
122
  message_from = "#{v}" if from =~ /#{k}/
@@ -104,8 +126,6 @@ module Cinch
104
126
  end
105
127
  m.reply "#{message_prefix} #{message_from}: #{subj}"
106
128
  end
107
- imap.disconnect
108
- end if @monitor
109
129
  end
110
130
  end
111
131
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 7
9
- version: 0.0.7
8
+ - 8
9
+ version: 0.0.8
10
10
  platform: ruby
11
11
  authors:
12
12
  - windowsrefund
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-02-03 00:00:00 -05:00
17
+ date: 2011-02-14 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency