summaryrefslogtreecommitdiffstats
path: root/vmchooser/addPrinters.cxx
blob: 9505e90f8bdbbea43c2288f9068336501b4ac450 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220


#include "inc/functions.h"

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>

/**
 * function addPrinters(xmlNode* node, char* script)
 * ----------------------------------------------------------
 * runs content of script (absolute path of a script-file)
 *  -> this expects the script to print out printer information
 *    in the following format
 * 
 * printserver\tprinter\tprinter description
 * 
 * all other output has to be directed to /dev/null
 * 
 * then this function add some printer-nodes to the xml-file
 * in "settings/eintrag/printers" 
 * (which will be created also if needed.)
 * in the following form: <br/>
 * 
 * &lt;printer name=&quot;printer&quot; path=&quot;//printserver/printer&quot; &gt;
 *  Printerdescription
 * &lt;/printer&gt;
 */
bool addPrinters(xmlNode* node, char* script) {
  
  if(node == NULL) {
    return false;
  }
  
  bool printer = false;
  vector<string> info_printer;
  
  const int MAX_LENGTH = 300;
  char line[MAX_LENGTH];
  char delims[] = "\t";
  string strline;
  FILE* inp = 0;
  
  unsigned int tindex = 0;
  xmlNodePtr cur = node->children;
  xmlNodePtr printernode = NULL;
  
  // Get <printers> node
  while(cur != NULL) {
    if (!xmlStrcmp(cur->name, (const xmlChar *)"printers")){
      printer = true;
      printernode = cur;
      break;
    }
    cur = cur->next;
  }
  if(! printer) {
    printernode = xmlNewNode(NULL, (const xmlChar*) "printers");
    if(printernode != NULL ) {
      xmlAddChild(node, printernode);
    }
    else {
      cerr << "No <printers> node created" << endl;
    }
  }
  
  // Parse input of printer-Skript (called by "char* script")
  // and write into <printer> nodes
  if( (inp = popen(script, "r" )) ) {
    while(fgets(line, MAX_LENGTH, inp ) != NULL) {
      strline = string(line);
      if(strline.length() > 3) {
        
        queue<unsigned int> temp;
        temp.push( strline.find_first_of( delims , 0) );
        
        while( temp.back() != string::npos ) {
          temp.push( strline.find_first_of( delims, temp.back()+1 ) );
        }
        
        unsigned int t_front;
        string tstr = string("");
        while( tindex != string::npos ) {
          
          // build printer-info element
          t_front = temp.front();
          
          if(tindex == 0) {
            tstr = strline.substr(0, t_front);
          }
          else if(t_front != string::npos) {
            tstr = strline.substr(tindex+1, t_front-tindex-1) ;
          }
          else {
            tstr = strline.substr( tindex+1, strline.length() - tindex-2 );
          }
          if(tstr.length() > 2) {
            info_printer.push_back( tstr );
            // DEBUG
            cout << info_printer.back() << endl;
          }
          tindex = t_front;
          temp.pop();
        }
        
        // Construct <printer> nodes 
        xmlNodePtr pNode = xmlNewNode(NULL, (const xmlChar*) "printer");
        xmlNewProp(pNode, (const xmlChar*) "name", (const xmlChar*) info_printer.at(1).c_str());
        xmlNewProp ( pNode, (const xmlChar*) "path", (const xmlChar*)
            string( string( "\\\\" ) + info_printer.at(0) + string( "\\" ) + info_printer.at(1) ).c_str() );

        if(info_printer.size() > 2) {
          xmlAddChild( pNode, xmlNewText( (const xmlChar*) info_printer.at(2).c_str() ) );
        }
        
        if(pNode != NULL) {
          xmlAddChild( printernode, pNode);
        }
        
        info_printer.clear();
        tindex = 0;
      }
    }
    pclose(inp);
    return true;
  }
  fprintf(stderr, "Couldn't run \"%s\" script!", script);
  return false;
}


/******************************************
 * Adds user info and hostname to xml
 *
 * usernode: <username param="user" />
 * hostnamenode: <hostname param="host" />
 ******************************************/
void addInfo(xmlNode* node) {
  
  if(node == NULL) {
    return;
  }
  
  bool user = false;
  bool host = false;
  
  const int MAX_LENGTH = 200;
  char hostname[MAX_LENGTH];
  uid_t id;
  passwd *pwd;
  
  string strline;
  xmlNodePtr cur = node->children;
  xmlNodePtr usernode = NULL;
  xmlNodePtr hostnamenode = NULL;
 
  // just use some standard Linux functions here ...
  id = geteuid(); // gets effective user id
  pwd = getpwuid(id); // gets passwd struct (including username)
  gethostname(hostname, MAX_LENGTH-1); // gets hostname
 
  // Get <username> node and add "username#param" attribute
  while(cur != NULL) {
    if (!xmlStrcmp(cur->name, (const xmlChar *)"username")){
      user = true;
      usernode = cur;
      break;
    }
    cur = cur->next;
  }
  if(! user) {
    usernode = xmlNewNode(NULL, (const xmlChar*) "username");
    if(usernode != NULL ) {
      xmlNewProp(usernode, (const xmlChar*) "param", (const xmlChar*) pwd->pw_name);
      xmlAddChild(node, usernode);
    }
    else {
      cerr << "<username> node could not be created!" << endl;
    }
  }
  else {
    // set param attribute in <username>
    xmlSetProp(usernode, (const xmlChar*) "param", (const xmlChar*) pwd->pw_name);
  }

  cur = node->children;
 
  // Get <hostname> node and add "hostname#param" attribute
  while(cur != NULL) {
    if (!xmlStrcmp(cur->name, (const xmlChar *)"hostname")){
      host = true;
      hostnamenode = cur;
      break;
    }
    cur = cur->next;
  }
  if(! host) {
    hostnamenode = xmlNewNode(NULL, (const xmlChar*) "hostname");
    if(hostnamenode != NULL ) {
      xmlNewProp(hostnamenode, (const xmlChar*) "param", (const xmlChar*) hostname);
      xmlAddChild(node, hostnamenode);
    }
    else {
      cerr << "<hostname> node could not be created!" << endl;
    }
  }
  else {
    // add param value to existant hostname-node
    xmlSetProp(hostnamenode, (const xmlChar*) "param", (xmlChar*) hostname);
  }
 
  return;
}