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