On Fri, Jan 28, 2011 at 5:03 AM, botp <botpena / gmail.com> wrote:
> On Fri, Jan 28, 2011 at 5:11 AM, Doug Blackman <wdblackman / bellsouth.net> wrote:
>
>> http://www.ruby-forum.com/attachment/5800/GPID_Error_doc_1-25.doc
>> http://www.ruby-forum.com/attachment/5801/Copy_of_gpid_mail_utils.rb
>
> refine your code.
>
> 1 you assume that gets always return something other than nil
> 2 if you accept nils, then convert it first to string before chomping
> it or whatever..

Even better: drop method readline completely and use a loop with block:

def send_mail(to, subj, msg, filename="<EMPTY>")

    # establish socket
    s = TCPSocket.new($host, $port)

    ntlm = false

    # handle host greeting
    readline s
    writeline s, "EHLO #{$host}"
    s.each |line|
      line.chomp!
      ntlm ||= /^250-AUTH.+NTLM.*/ =~ line
      break if /^250 / =~ line
    end

Note also that you then must declare variable "ntlm" outside the loop body.

You also need to change the way you work with the socket because it
will only be properly closed if all goes well.  Your current code will
leak open sockets in case of errors and may eventually run out of file
descriptors if this is running longer.  You either need to do

    # establish socket
    s = TCPSocket.new($host, $port)
    begin
      ...
    ensure
      s.close
    end


or simply use TCPSocket.open with block:

TCPSocket.open($host, $port) do |s|
 ...
end

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/