Chris@909: # $Id: dataset.rb 78 2006-04-26 02:57:34Z blackhedd $ Chris@909: # Chris@909: # Chris@909: #---------------------------------------------------------------------------- Chris@909: # Chris@909: # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved. Chris@909: # Chris@909: # Gmail: garbagecat10 Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or modify Chris@909: # it under the terms of the GNU General Public License as published by Chris@909: # the Free Software Foundation; either version 2 of the License, or Chris@909: # (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Chris@909: # Chris@909: #--------------------------------------------------------------------------- Chris@909: # Chris@909: # Chris@909: Chris@909: Chris@909: Chris@909: Chris@909: module Net Chris@909: class LDAP Chris@909: Chris@909: class Dataset < Hash Chris@909: Chris@909: attr_reader :comments Chris@909: Chris@909: Chris@909: def Dataset::read_ldif io Chris@909: ds = Dataset.new Chris@909: Chris@909: line = io.gets && chomp Chris@909: dn = nil Chris@909: Chris@909: while line Chris@909: io.gets and chomp Chris@909: if $_ =~ /^[\s]+/ Chris@909: line << " " << $' Chris@909: else Chris@909: nextline = $_ Chris@909: Chris@909: if line =~ /^\#/ Chris@909: ds.comments << line Chris@909: elsif line =~ /^dn:[\s]*/i Chris@909: dn = $' Chris@909: ds[dn] = Hash.new {|k,v| k[v] = []} Chris@909: elsif line.length == 0 Chris@909: dn = nil Chris@909: elsif line =~ /^([^:]+):([\:]?)[\s]*/ Chris@909: # $1 is the attribute name Chris@909: # $2 is a colon iff the attr-value is base-64 encoded Chris@909: # $' is the attr-value Chris@909: # Avoid the Base64 class because not all Ruby versions have it. Chris@909: attrvalue = ($2 == ":") ? $'.unpack('m').shift : $' Chris@909: ds[dn][$1.downcase.intern] << attrvalue Chris@909: end Chris@909: Chris@909: line = nextline Chris@909: end Chris@909: end Chris@909: Chris@909: ds Chris@909: end Chris@909: Chris@909: Chris@909: def initialize Chris@909: @comments = [] Chris@909: end Chris@909: Chris@909: Chris@909: def to_ldif Chris@909: ary = [] Chris@909: ary += (@comments || []) Chris@909: Chris@909: keys.sort.each {|dn| Chris@909: ary << "dn: #{dn}" Chris@909: Chris@909: self[dn].keys.map {|sym| sym.to_s}.sort.each {|attr| Chris@909: self[dn][attr.intern].each {|val| Chris@909: ary << "#{attr}: #{val}" Chris@909: } Chris@909: } Chris@909: Chris@909: ary << "" Chris@909: } Chris@909: Chris@909: block_given? and ary.each {|line| yield line} Chris@909: Chris@909: ary Chris@909: end Chris@909: Chris@909: Chris@909: end # Dataset Chris@909: Chris@909: end # LDAP Chris@909: end # Net Chris@909: Chris@909: