Class: ManageSieve
- Inherits:
-
Object
- Object
- ManageSieve
- Defined in:
- lib/managesieve.rb
Overview
ManageSieve implements MANAGESIEVE, a protocol for remote management of Sieve scripts.
The following MANAGESIEVE commands are implemented:
-
CAPABILITY
-
DELETESCRIPT
-
GETSCRIPT
-
HAVESPACE
-
LISTSCRIPTS
-
LOGOUT
-
PUTSCRIPT
-
SETACTIVE
The AUTHENTICATE command is partially implemented. Currently the LOGIN
and PLAIN
authentication mechanisms are implemented.
Example
# Create a new ManageSieve instance
m = ManageSieve.new(
:host => 'sievehost.mydomain.com',
:port => 4190,
:user => 'johndoe',
:password => 'secret',
:auth => 'PLAIN'
)
# List installed scripts
m.scripts.sort do |name, active|
print name
print active ? " (active)\n" : "\n"
end
script = <<__EOF__
require "fileinto";
if header :contains ["to", "cc"] "[email protected]" {
fileinto "Ruby-talk";
}
__EOF__
# Test if there's enough space for script 'foobar'
puts m.have_space?('foobar', script.bytesize)
# Upload it
m.put_script('foobar', script)
# Show its contents
puts m.get_script('foobar')
# Close the connection
m.logout
Constant Summary collapse
- SIEVE_PORT =
4190
Instance Attribute Summary collapse
-
#capabilities ⇒ Object
readonly
Returns the value of attribute capabilities.
-
#euser ⇒ Object
readonly
Returns the value of attribute euser.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#login_mechs ⇒ Object
readonly
Returns the value of attribute login_mechs.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#tls ⇒ Object
readonly
Returns the value of attribute tls.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#delete_script(script) ⇒ Object
Deletes
script
from the server. -
#get_script(script) ⇒ Object
Returns the contents of
script
as a string. -
#have_space?(script, size) ⇒ Boolean
Returns true if there is space on the server to store
script
with sizesize
and false otherwise. -
#initialize(info) ⇒ ManageSieve
constructor
Create a new ManageSieve instance.
-
#logout ⇒ Object
Disconnect from the server.
-
#put_script(script, data) ⇒ Object
Uploads
script
to the server, usingdata
as its contents. -
#scripts ⇒ Object
(also: #each_script)
If a block is given, calls it for each script stored on the server, passing its name and status as parameters.
-
#set_active(script) ⇒ Object
Sets
script
as active. -
#supports_tls? ⇒ Boolean
Returns true if the server supports TLS and false otherwise.
Constructor Details
#initialize(info) ⇒ ManageSieve
Create a new ManageSieve instance. The info
parameter is a hash with the following keys:
- :host
-
the sieve server
- :port
-
the sieve port (defaults to 4190)
- :user
-
the name of the user
- :euser
-
the name of the effective user (defaults to
:user
) - :password
-
the password of the user
- :auth_mech
-
the authentication mechanism (defaults to “ANONYMOUS”)
- :tls
-
use TLS (defaults to use it if the server supports it)
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/managesieve.rb', line 125 def initialize(info) @host = info[:host] @port = info[:port] || 4190 @user = info[:user] @euser = info[:euser] || @user @password = info[:password] @auth_mech = info[:auth] || 'ANONYMOUS' @tls = info.has_key?(:tls) ? !!info[:tls] : nil @capabilities = [] @login_mechs = [] @implementation = '' @supports_tls = false @socket = TCPSocket.new(@host, @port) data = get_response server_features(data) if @tls and not supports_tls? raise SieveNetworkError, 'Server does not support TLS' @socket.close elsif @tls != false @tls = supports_tls? starttls if @tls end authenticate @password = nil end |
Instance Attribute Details
#capabilities ⇒ Object (readonly)
Returns the value of attribute capabilities.
112 113 114 |
# File 'lib/managesieve.rb', line 112 def capabilities @capabilities end |
#euser ⇒ Object (readonly)
Returns the value of attribute euser.
112 113 114 |
# File 'lib/managesieve.rb', line 112 def euser @euser end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
112 113 114 |
# File 'lib/managesieve.rb', line 112 def host @host end |
#login_mechs ⇒ Object (readonly)
Returns the value of attribute login_mechs.
112 113 114 |
# File 'lib/managesieve.rb', line 112 def login_mechs @login_mechs end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
112 113 114 |
# File 'lib/managesieve.rb', line 112 def port @port end |
#tls ⇒ Object (readonly)
Returns the value of attribute tls.
112 113 114 |
# File 'lib/managesieve.rb', line 112 def tls @tls end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
112 113 114 |
# File 'lib/managesieve.rb', line 112 def user @user end |
Instance Method Details
#delete_script(script) ⇒ Object
Deletes script
from the server.
189 190 191 |
# File 'lib/managesieve.rb', line 189 def delete_script(script) send_command('DELETESCRIPT', sieve_name(script)) end |
#get_script(script) ⇒ Object
Returns the contents of script
as a string.
172 173 174 175 176 177 178 179 |
# File 'lib/managesieve.rb', line 172 def get_script(script) begin data = send_command('GETSCRIPT', sieve_name(script)) rescue SieveCommandError => e raise e, "Cannot get script: #{e}" end return data.join.chomp end |
#have_space?(script, size) ⇒ Boolean
Returns true if there is space on the server to store script
with size size
and false otherwise.
200 201 202 203 204 205 206 207 208 |
# File 'lib/managesieve.rb', line 200 def have_space?(script, size) begin args = sieve_name(script) + ' ' + size.to_s send_command('HAVESPACE', args) return true rescue SieveCommandError return false end end |
#logout ⇒ Object
Disconnect from the server.
216 217 218 219 |
# File 'lib/managesieve.rb', line 216 def logout send_command('LOGOUT') @socket.close end |
#put_script(script, data) ⇒ Object
Uploads script
to the server, using data
as its contents.
182 183 184 185 186 |
# File 'lib/managesieve.rb', line 182 def put_script(script, data) args = sieve_name(script) args += ' ' + sieve_string(data) if data send_command('PUTSCRIPT', args) end |
#scripts ⇒ Object Also known as: each_script
If a block is given, calls it for each script stored on the server, passing its name and status as parameters. Else, and array of [ name
, status
] arrays is returned. The status is either ‘ACTIVE’ or nil.
160 161 162 163 164 165 166 167 168 |
# File 'lib/managesieve.rb', line 160 def scripts begin scripts = send_command('LISTSCRIPTS') rescue SieveCommandError => e raise e, "Cannot list scripts: #{e}" end return scripts unless block_given? scripts.each { |name, status| yield(name, status) } end |
#set_active(script) ⇒ Object
Sets script
as active.
194 195 196 |
# File 'lib/managesieve.rb', line 194 def set_active(script) send_command('SETACTIVE', sieve_name(script)) end |
#supports_tls? ⇒ Boolean
Returns true if the server supports TLS and false otherwise.
211 212 213 |
# File 'lib/managesieve.rb', line 211 def supports_tls? @supports_tls end |