<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,7 +1,7 @@
 #!/usr/bin/env ruby
 #
 #--
-# Copyright (c) 2004-2006 Andre Nathan &lt;andre@digirati.com.br&gt;
+# Copyright (c) 2004-2009 Andre Nathan &lt;andre@digirati.com.br&gt;
 #
 # Permission to use, copy, modify, and distribute this software for any
 # purpose with or without fee is hereby granted, provided that the above
@@ -25,13 +25,18 @@
 # See the ManageSieve class for documentation and examples.
 #
 #--
-# $Id: managesieve.rb,v 1.13 2006/08/30 14:23:58 andre Exp $
+# $Id: managesieve.rb,v 1.14 2009/01/13 19:22:08 andre Exp $
 #++
 #
 
 require 'base64'
 require 'socket'
 
+begin
+  require 'openssl'
+rescue LoadError
+end
+
 #
 # Define our own Base64.encode64 for compatibility with ruby &lt;= 1.8.1, which
 # defines encode64() at the top level.
@@ -104,7 +109,7 @@ class SieveResponseError &lt; Exception; end
 class ManageSieve
   SIEVE_PORT = 2000
 
-  attr_reader :host, :port, :user, :euser, :capabilities, :login_mechs
+  attr_reader :host, :port, :user, :euser, :capabilities, :login_mechs, :tls
 
   # Create a new ManageSieve instance. The +info+ parameter is a hash with the
   # following keys:
@@ -115,6 +120,7 @@ class ManageSieve
   # [&lt;i&gt;:euser&lt;/i&gt;]     the name of the effective user (defaults to +:user+)
   # [&lt;i&gt;:password&lt;/i&gt;]  the password of the user
   # [&lt;i&gt;:auth_mech&lt;/i&gt;] the authentication mechanism (defaults to +&quot;ANONYMOUS&quot;+)
+  # [&lt;i&gt;:tls&lt;/i&gt;]       use TLS (defaults to use it if the server supports it)
   #
   def initialize(info)
     @host      = info[:host]
@@ -123,15 +129,25 @@ class ManageSieve
     @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)
+    @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
@@ -261,7 +277,7 @@ class ManageSieve
   def send_command(cmd, args=nil, wait_response=true) # :nodoc:
     cmd += ' ' + args if args
     begin
-      @socket.send(cmd + &quot;\r\n&quot;, 0)
+      @socket.write(cmd + &quot;\r\n&quot;)
       resp = get_response if wait_response
     rescue SieveResponseError =&gt; e
       raise SieveCommandError, &quot;Command error: #{e}&quot;
@@ -326,4 +342,13 @@ class ManageSieve
     return &quot;{#{string.length}+}\r\n#{string}&quot;
   end
 
+  private
+  def starttls
+    send_command('STARTTLS')
+    @socket = OpenSSL::SSL::SSLSocket.new(@socket)
+    @socket.sync_close = true
+    @socket.connect
+    data = get_response
+    server_features(data)
+  end
 end</diff>
      <filename>lib/managesieve.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>d695fcae5416ebb1fabba25420d4b3e0ddebf37f</id>
    </parent>
  </parents>
  <author>
    <name>andre</name>
    <email>andre</email>
  </author>
  <url>http://github.com/andrenth/ruby-managesieve/commit/b7eb422e1f15b1ea9442e83e7a3f66a8b305c48a</url>
  <id>b7eb422e1f15b1ea9442e83e7a3f66a8b305c48a</id>
  <committed-date>2009-01-13T11:22:08-08:00</committed-date>
  <authored-date>2009-01-13T11:22:08-08:00</authored-date>
  <message>Add TLS support. Thanks to Alban Peignier for the implementation.</message>
  <tree>4eec375aa7d8f17cecc181d3baf02175ecfd1ad7</tree>
  <committer>
    <name>andre</name>
    <email>andre</email>
  </committer>
</commit>
