Class: ManageSieve

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#capabilitiesObject (readonly)

Returns the value of attribute capabilities.



112
113
114
# File 'lib/managesieve.rb', line 112

def capabilities
  @capabilities
end

#euserObject (readonly)

Returns the value of attribute euser.



112
113
114
# File 'lib/managesieve.rb', line 112

def euser
  @euser
end

#hostObject (readonly)

Returns the value of attribute host.



112
113
114
# File 'lib/managesieve.rb', line 112

def host
  @host
end

#login_mechsObject (readonly)

Returns the value of attribute login_mechs.



112
113
114
# File 'lib/managesieve.rb', line 112

def 
  @login_mechs
end

#portObject (readonly)

Returns the value of attribute port.



112
113
114
# File 'lib/managesieve.rb', line 112

def port
  @port
end

#tlsObject (readonly)

Returns the value of attribute tls.



112
113
114
# File 'lib/managesieve.rb', line 112

def tls
  @tls
end

#userObject (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.

Returns:

  • (Boolean)


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

#logoutObject

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

#scriptsObject 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.

Returns:

  • (Boolean)


211
212
213
# File 'lib/managesieve.rb', line 211

def supports_tls?
  @supports_tls
end