#include #include #include #include #include #include "printer.h" #include "printer.m" #include #include #include #include static HELP_FILE help_printjobs ("printer","printjobs"); #define _TLMP_walkprintjobs struct _F_walkprintjobs { #define _F_walkprintjobs_onejob(name) void name onejob(const char *job, int uid, int size, const char *date) virtual _F_walkprintjobs_onejob( )=0; }; static int walkprintjobs(_F_walkprintjobs &c, const char *printer) { bool header; _F_walkprintjobs *c; int ret; char cmd[strlen(printer)+10]; snprintf (cmd,sizeof(cmd)-1,"lpq -P%s",printer); glocal.header = true; glocal.c = &c; glocal.ret = 0; (cmd,20); if (glocal.header){ const char *pt = str_skip(line); if (strncmp(pt,"Rank",4)==0){ glocal.header = false; } }else{ glocal.ret++; const char *pt = str_skipword(line); pt = str_skip(pt); char owner[200],job[200]; pt = str_copyword (owner,pt); pt = str_skip(pt); pt = str_skipword(pt); // class pt = str_copyword (job,pt); pt = str_skip(pt); int uid = 0; struct passwd *p = getpwnam(owner); if (p != NULL) uid = p->pw_uid; pt = str_skipword (pt); // Skip the file name // Does not work if there are spaces pt = str_skip(pt); int size = atoi(pt); const char *endpt = strstr (line,"bytes"); if (endpt != NULL){ // Trick with old lpr system pt += 5; while (isspace(endpt[-1])) endpt--; while (isdigit(endpt[-1])) endpt--; size = atoi(endpt); } pt = str_skip(pt); glocal.c->onejob (job,uid,size,pt); } return 0; return glocal.ret; } /* Get the list of queue defined in /etc/printcap */ int queue_getlist(SSTRINGS &tb) { FILE_CFG *fin = cf_printcapfile.fopen ("/etc/printcap","r"); if (fin != NULL){ char buf[2000]; while (fgets_strip(buf,sizeof(buf)-1,fin,'\\','#',NULL)!=NULL){ // First, we split the queue name from the end of the line char *pt = strchr(buf,':'); if (pt != NULL) *pt = '\0'; // Then we only keep the primary queue name pt = strchr(buf,'|'); if (pt != NULL) *pt = '\0'; pt = str_skip(buf); if (*pt != '\0'){ tb.add (new SSTRING(pt)); } } fclose (fin); } return tb.getnb(); } static PRIVILEGES tb; static void queue_setprivi() { if (tb.getnb()==0){ SSTRINGS tbq; int n = queue_getlist (tbq); for (int i=0; iget(); char id[PATH_MAX]; sprintf (id,"lpqueue_%s",q); tb.add (new PRIVILEGE (id,q ,MSG_U(T_QUEUEMNG,"Printer queue management"))); } } } /* Return the co-admin privilege associated with a print queue */ PRIVILEGE *queue_lookuppriv (const char *p) { queue_setprivi(); char id[PATH_MAX]; sprintf (id,"lpqueue_%s",p); return privilege_lookup (id); } void queue_getstat ( const char *p, // Printer or queue name bool &accept, bool &print, int &nbjob) // Number of jobs in the spool { bool *accept; bool *print; print = accept = false; nbjob = 0; glocal.accept = &accept; glocal.print = &print; SSTRING arg; arg.setfromf ("status %s",p); ("lpc",arg.get(),20); if (strstr(line,"printing is enabled")!=NULL) glocal.print[0] = true; if (strstr(line,"queuing is enabled")!=NULL) glocal.accept[0] = true; return 0; nbjob = (p); } /* Manage the jobs for a printer queue */ void queue_jobs(const char *p) { const char *printer; bool header; SSTRINGS jobs; _F_editrecords *c; glocal.printer = p; (MSG_U(T_PRINTJOBS,"Print jobs") ,"" ,help_printjobs); newf_head (MSG_U(H_PRINTJOBS,"Job ID\tUser\tFull name\tSize\tDate")); setbutinfo (MENU_USR1,MSG_U(B_ERASEALL,"Erase all") ,MSG_R(B_ERASEALL)); sortable(); sortpolicy ("naan"); sethdispo ("rllr"); glocal.jobs.remove_all(); glocal.c = this; (glocal.printer); struct passwd *pw = getpwuid (uid); char line[200]; if (pw !=NULL){ snprintf (line,sizeof(line)-1,"%s\t%s\t%d\t%s" ,pw->pw_name ,pw->pw_gecos ,size ,date); }else{ snprintf (line,sizeof(line)-1,"%d\t\t%d\t%s" ,uid ,size ,date); } glocal.c->new_menuitem (job,line); glocal.jobs.add (new SSTRING(job)); const char *job = glocal.jobs.getitem(no)->get(); SSTRING msg; msg.setfromf(MSG_U(Q_REMOVEJOB ,"Do you want to remove the print job %s\n" "on printer queue %s") ,job,glocal.printer); if (xconf_yesno(MSG_U(T_CONFIRM,"Confirm"),msg.get(),help_nil)==MENU_YES){ SSTRING arg; arg.setfromf ("-P%s %s",glocal.printer,job); netconf_system_if ("lprm",arg.get()); } if (glocal.jobs.getnb()==0){ xconf_error (MSG_U(E_QUEUEEMPTY,"Queue is already empty")); }else if (xconf_yesno(MSG_R(T_CONFIRM) ,MSG_U(Q_ERASEALL,"Are you sure you want to erase\n" "all print jobs") ,help_nil)==MENU_YES){ SSTRING arg; arg.setfromf ("-P%s",glocal.printer); for (int i=0; iget()); } netconf_system_if ("lprm",arg.get()); } } static HELP_FILE help_privi ("printconf","queueprivi"); static REGISTER_PRIVI_HELP p (help_privi ,P_MSG_U(T_QUEUEPRIVI,"Privileges: Printer queue management")); static PRIVILEGE_DECLARATOR vdom_decl(queue_setprivi);