/vp_plugins/server_side/vprn

http://cupsfilter.googlecode.com/ · Perl · 106 lines · 65 code · 14 blank · 27 comment · 12 complexity · 0d3dfbb49fa19349990c16c5c565dd22 MD5 · raw file

  1. #!/usr/bin/perl -w
  2. use strict;
  3. use locale;
  4. use File::Copy;
  5. use File::Temp;
  6. use Time::localtime;
  7. #----------------------- CONFIG ------------------------------------
  8. # For debuging and troubleshooting. Nice hunt !
  9. my $DEBUG=1;
  10. # enable log
  11. my $LOG=1;
  12. my @argv = @ARGV; #keep hands off the argument array
  13. my $SPOOL = "/var/tmp"; #where are my speedy memory drive?
  14. my $LOG_FILE = "/var/tmp/backend.log";
  15. my $VPRINTER = "/opt/vprn/debug/p_monitor";
  16. my $DATE_BIN = "/bin/date"; #path to date
  17. my $ENABLE ="/usr/sbin/cupsenable"; #path to enable binary
  18. my $FH_TEMP; #File handle to temp file
  19. my $DEVICE_URI =$ENV{DEVICE_URI};
  20. #--------------------------------------------------------------------------------------------------
  21. #print "Work it \n";
  22. my ($jobID,$userName,$jobTitle,$copies,$printOptions,$printFile) = @argv;
  23. #--------------------------------------------------------------------------------------------------
  24. # Check arguments and environment
  25. # Usage: $0 job-id user title copies options [file]
  26. #
  27. ## CUPS API says there must be an exact number of args
  28. # If no arguments, device discovery mode
  29. if (!$argv[0]){
  30. print STDERR ("direct vprn backend \"Unknown\" \"TechnoServ Filter \"\n");
  31. exit 0;
  32. }
  33. # If too many arguments, send error
  34. if (scalar(@argv) < 5 || scalar(@argv) > 6){
  35. print STDERR ("Usage: vprn job-id user title copies options [file]\n");
  36. exit 1;
  37. }
  38. save2log(join('_',@argv)) if ($LOG);
  39. ## get the uri CUPS has
  40. #exit 1 if (!defined $DEVICE_URI);
  41. #--------------------------------------------------------------------------------------------------
  42. #The hard, but fine work :)
  43. # Safe create temp file
  44. $FH_TEMP = File::Temp->new( TEMPLATE => 'cups_jobXXXXX',
  45. DIR => $SPOOL,
  46. SUFFIX => '.~tmp');
  47. # If we were called with 6 arguments, read from STDIN to a tempfile:
  48. if (!defined $printFile){
  49. $FH_TEMP = File::Temp->new( TEMPLATE => 'cups_jobXXXXX',
  50. DIR => $SPOOL,
  51. SUFFIX => '.~tmp');
  52. copy(\*STDIN,$FH_TEMP) or die "Copy failed: $!";
  53. }else{
  54. $FH_TEMP = $printFile;
  55. }
  56. #???????? ?????? ?? ??????
  57. save2log ("$VPRINTER $FH_TEMP \n") if ($LOG);
  58. #my $result =
  59. `$VPRINTER $FH_TEMP`;
  60. exit 0;
  61. #exit $result;
  62. #-----------------------------------------------------------------------------------
  63. sub datetime2string{
  64. #Arg:nothing
  65. #Returns: date time string
  66. my @months_names=("JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER");
  67. my $tm = localtime();
  68. my ($day,$month,$year,$hh,$mm) = ($tm->mday,$tm->mon,$tm->year,$tm->hour,$tm->min);
  69. $year+=1900;
  70. if (length($day) == 1){
  71. $day='0'.$day;
  72. }
  73. if (length($hh) == 1){
  74. $hh='0'.$hh;
  75. }
  76. if (length($mm) == 1){
  77. $mm='0'.$mm;
  78. }
  79. my $date =join('/',$year,$months_names[$month],$day);
  80. my $time =join(':',$hh,$mm);
  81. return join ('_',$date,$time);
  82. }
  83. sub save2log{
  84. #Arg: info_str
  85. #Returns: nothing
  86. my($info_str)= @_;
  87. open (LOG,">> $LOG_FILE") or die "can't open file $LOG_FILE: $!";
  88. print LOG "--------------------------Date ", datetime2string()," ---------------------\n";
  89. print LOG $info_str,"\n";
  90. print LOG "---------------------------------------------------------------------------\n";
  91. close (LOG);
  92. }