b5b1010
#!/usr/bin/perl
b5b1010
# The above Perl path may vary on your system; fix it!!! -*- perl -*-
b5b1010
b5b1010
# dnssd - Search for network printers with the avahi-browse command
b5b1010
#         (Zeroconf, DNS-SD)
b5b1010
b5b1010
# Printer discovery CUPS backend (like the SNMP backend)
b5b1010
# See also http://qa.mandriva.com/show_bug.cgi?id=21812
b5b1010
b5b1010
# Copyright 2007 Till Kamppeter <till.kamppeter@gmail.com>
b5b1010
#
b5b1010
#  This program is free software; you can redistribute it and/or modify it
b5b1010
#  under the terms of the GNU General Public License as published by the
b5b1010
#  Free Software Foundation; either version 2 of the License, or (at your
b5b1010
#  option) any later version.
b5b1010
#
b5b1010
#  This program is distributed in the hope that it will be useful, but
b5b1010
#  WITHOUT ANY WARRANTY; without even the implied warranty of
b5b1010
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
b5b1010
#  Public License for more details.
b5b1010
#
b5b1010
#  You should have received a copy of the GNU General Public License
b5b1010
#  along with this program; if not, write to the Free Software
b5b1010
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
b5b1010
#  USA.
b5b1010
b5b1010
# Usage: 
b5b1010
#
b5b1010
# cp dnssd /usr/lib/cups/backend/
b5b1010
# chmod 755 /usr/lib/cups/backend/dnssd
b5b1010
# killall -HUP cupsd (or "/etc/init.d/cups restart", CUPS 1.1.x only)
b5b1010
# lpinfo -v (or use any printer setup tool)
b5b1010
b5b1010
#use strict;
b5b1010
b5b1010
$0 =~ m!^(.*)/([^/]+)\s*$!;
b5b1010
my $progname = ($2 || $0 || "dnssd");
b5b1010
my $progpath = ($1 || "/usr/lib/cups/backend");
b5b1010
b5b1010
if ($ARGV[0]){
b5b1010
    die "This backend is only for printer discovery, not for actual printing.\n";
b5b1010
}
b5b1010
b5b1010
my $avahicmd = "avahi-browse -k -t -v -r -a 2> /dev/null";
b5b1010
b5b1010
# IPs which are for computers, consider their printer entries as queues
b5b1010
# set up with the local printing system (CUPS, LPD, Windows/Samba SMB, ...)
b5b1010
my @ipblacklist = ();
b5b1010
my $output;
b5b1010
my ($interface, $nettype, $ip, $host, $make, $model, $description, $cmd, $makemodel, $deviceid, $protocol, $port, $uriext, $uri);
b5b1010
b5b1010
open (AVAHI, "$avahicmd |") or die "Could not call \"$avahicmd\"\n";
b5b1010
while (my $line = <AVAHI>) {
b5b1010
    chomp ($line);
b5b1010
    if ($line =~ /^\s*=\s+(\S+)\s+(\S+)\s+(.*?)\s+(\S+)\s+(\S+)\s*$/) {
b5b1010
	# New item
b5b1010
	$interface = $1;
b5b1010
	$nettype = $2;
b5b1010
	my $itemname = $3;
b5b1010
	my $protocolinfo = $4;
b5b1010
        if ($protocolinfo =~ /_workstation/) {
b5b1010
	    $protocol = "computer";
b5b1010
	} elsif ($protocolinfo =~ /_pdl-datastream/) {
b5b1010
	    $protocol = "socket";
b5b1010
	} elsif ($protocolinfo =~ /_printer/) {
b5b1010
	    $protocol = "lpd";
b5b1010
	} elsif ($protocolinfo =~ /_ipp/) {
b5b1010
	    $protocol = "ipp";
b5b1010
	}
b5b1010
    } elsif ($line =~ /^\s*hostname\s*=\s*\[([^\]]+)\]\s*$/) {
b5b1010
	$host = $1;
b5b1010
	$host =~ s/\.local\.?$//;
b5b1010
    } elsif ($line =~ /^\s*address\s*=\s*\[([^\]]+)\]\s*$/) {
b5b1010
	$ip = $1;
b5b1010
	if ($protocol eq "computer") {
b5b1010
	    push (@ipblacklist, $ip);
b5b1010
	    $protocol = "";
b5b1010
	}
b5b1010
    } elsif ($line =~ /^\s*port\s*=\s*\[([^\]]+)\]\s*$/) {
b5b1010
	$port = $1;
b5b1010
    } elsif ($line =~ /^\s*txt\s*=\s*\[(.+)\]\s*$/) {
b5b1010
	my $info = $1;
b5b1010
	if ($protocol) {
b5b1010
	    my ($ty, $product, $pdls, $usb_MFG, $usb_MDL, $usb_DES, $usb_CMD) = 
b5b1010
		("", "", "", "", "", "", "");
b5b1010
	    while ($info =~ s/^\s*\"([^\"]+)\"\s*//) {
b5b1010
		my $infoitem = $1;
b5b1010
		if ($infoitem =~ /^([^=]*)=(.*)$/) {
b5b1010
		    my $field = $1;
b5b1010
		    my $content = $2;
b5b1010
		    if ($field eq "ty") {
b5b1010
			$ty = $content;
b5b1010
		    } elsif ($field eq "product") {
b5b1010
			$product = $content;
b5b1010
			$product =~ s/^\((.*)\)$/$1/;
b5b1010
		    } elsif ($field eq "usb_MFG") {
b5b1010
			$usb_MFG = $content;
b5b1010
		    } elsif ($field eq "usb_MDL") {
b5b1010
			$usb_MDL = $content;
b5b1010
		    } elsif ($field eq "usb_DES") {
b5b1010
			$usb_DES = $content;
b5b1010
		    } elsif ($field eq "usb_CMD") {
b5b1010
			$usb_CMD = $content;
b5b1010
		    } elsif ($field eq "rp") {
b5b1010
			$uriext = $content;
b5b1010
		    } elsif ($field eq "pdl") {
b5b1010
			while ($content =~ s/^\s*([^\,]+?)\s*\,\s*//) {
b5b1010
			    my $i = $1;
b5b1010
			    if ($i =~ m!\b(postscript|ps)\b!i) {
b5b1010
				$pdls .= "POSTSCRIPT,";
b5b1010
			    } elsif ($i =~ m!\b(pdf)\b!i) {
b5b1010
				$pdls .= "PDF,";
b5b1010
			    } elsif ($i =~ m!\b(pcl6|pclxl|pxl)\b!i) {
b5b1010
				$pdls .= "PCLXL,";
b5b1010
			    } elsif ($i =~ m!\b(pcl[345][ce]?|pcl)\b!i) {
b5b1010
				$pdls .= "PCL,";
b5b1010
			    }
b5b1010
			}
b5b1010
			$pdls =~ s/\,$//;
b5b1010
		    }
b5b1010
		}
b5b1010
	    }
b5b1010
	    $usb_MDL ||= $ty;
b5b1010
	    $usb_DES ||= $product;
b5b1010
	    if ($usb_MFG) {
b5b1010
		$make = $usb_MFG;
b5b1010
	    } elsif ($usb_DES =~ /^KONICA\s+MINOLTA\b/i) { 
b5b1010
		$make = "KONICA MINOLTA";
b5b1010
	    } elsif ($usb_DES) {
b5b1010
		$usb_DES =~ /^\s*(\S*)\b/;
b5b1010
		$make = $1;
b5b1010
	    }
b5b1010
	    $model = $usb_MDL;
b5b1010
	    if (!$model) {
b5b1010
		$usb_DES =~ /^\s*\S*\s*(.*)$/;
b5b1010
		$model = $1;
b5b1010
	    }
b5b1010
	    $usb_CMD ||= $pdls;
b5b1010
	    my $extra;
b5b1010
	    if ($protocol eq "socket") {
b5b1010
		$uri = "socket://$ip:$port";
b5b1010
		$extra = "Port $port";
b5b1010
	    } elsif ($protocol eq "lpd") {
b5b1010
		$uri = "lpd://$ip" . ($uriext ? "/$uriext" : "");
b5b1010
		$extra = ($uriext ? "Queue: $uriext" : "Default queue");
b5b1010
	    } elsif ($protocol eq "ipp") {
b5b1010
		$uri = "ipp://$ip:$port" . ($uriext ? "/$uriext" : "");
b5b1010
		$extra = ($uriext ? "Queue: $uriext" : "Default queue");
b5b1010
	    }
b5b1010
	    if ($make && $model) {
b5b1010
		$make =~ s/Hewlett.?Packard/HP/i;
b5b1010
		$make =~ s/Lexmark.?International/Lexmark/i;
b5b1010
		$model =~ s/Hewlett.?Packard/HP/i;
b5b1010
		$model =~ s/Lexmark.?International/Lexmark/i;
b5b1010
		while ($model =~ s/^\s*$make\s*//i) {};
b5b1010
		$makemodel = "$make $model";
b5b1010
	    } elsif ($usb_DES) {
b5b1010
		$makemodel = $usb_DES;
b5b1010
	    } else {
b5b1010
		$makemodel = "Unknown";
b5b1010
	    }
b5b1010
	    $deviceid = ($usb_MFG ? "MFG:$usb_MFG;" : "") .
b5b1010
		($usb_MDL ? "MDL:$usb_MDL;" : "") .
b5b1010
		($usb_DES ? "DES:$usb_DES;" : "") .
b5b1010
		($usb_CMD ? "CMD:$usb_CMD;" : "");
b5b1010
	    $deviceid .= "CLS:PRINTER;" if $deviceid;
b5b1010
	    $output->{$ip}{$protocol}{$extra} =
b5b1010
		"network $uri \"$makemodel\" \"$makemodel $ip ($extra)\" \"$deviceid\"\n";
b5b1010
	    ($interface, $nettype, $ip, $host, $make, $model, $description, $cmd, $makemodel, $deviceid, $protocol, $port, $uriext, $uri) =
b5b1010
		("", "", "", "", "", "", "", "", "", "", "", "", "", "");
b5b1010
	}
b5b1010
    }
b5b1010
}
b5b1010
b5b1010
foreach my $ip (keys(%{$output})) {
b5b1010
    next if member($ip, @ipblacklist);
b5b1010
    if ($output->{$ip}{"socket"}) {
b5b1010
	foreach my $extra (keys(%{$output->{$ip}{"socket"}})) {
b5b1010
	    if (keys(%{$output->{$ip}{"socket"}}) = 1) {
b5b1010
		$output->{$ip}{"socket"}{$extra} =~
b5b1010
		    s/^(\s*\S*\s*\S*\s*\"[^\"]*\"\s*\"[^\"\(]*?)\s*\([^\)]*\)\s*(\"\s*.*)$/$1$2/;
b5b1010
	    }
b5b1010
	    print $output->{$ip}{"socket"}{$extra};
b5b1010
	}
b5b1010
    } elsif ($output->{$ip}{"lpd"}) {
b5b1010
	foreach my $extra (keys(%{$output->{$ip}{"lpd"}})) {
b5b1010
	    if (keys(%{$output->{$ip}{"lpd"}}) = 1) {
b5b1010
		$output->{$ip}{"lpd"}{$extra} =~
b5b1010
		    s/^(\s*\S*\s*\S*\s*\"[^\"]*\"\s*\"[^\"\(]*?)\s*\([^\)]*\)\s*(\"\s*.*)$/$1$2/;
b5b1010
	    }
b5b1010
	    print $output->{$ip}{"lpd"}{$extra};
b5b1010
	}
b5b1010
    } elsif ($output->{$ip}{"ipp"}) {
b5b1010
	foreach my $extra (keys(%{$output->{$ip}{"ipp"}})) {
b5b1010
	    if (keys(%{$output->{$ip}{"ipp"}}) == 1) {
b5b1010
		$output->{$ip}{"ipp"}{$extra} =~
b5b1010
		    s/^(\s*\S*\s*\S*\s*\"[^\"]*\"\s*\"[^\"]*?)\s*\([^\)]*\)\s*(\"\s*.*)$/$1$2/;
b5b1010
	    }
b5b1010
	    print $output->{$ip}{"ipp"}{$extra};
b5b1010
	}
b5b1010
    }
b5b1010
}
b5b1010
b5b1010
exit 0;
b5b1010
b5b1010
# member( $a, @b ) returns 1 if $a is in @b, 0 otherwise.
b5b1010
sub member { my $e = shift; foreach (@_) { $e eq $_ and return 1 } 0 };