-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanto.c
171 lines (145 loc) · 4.16 KB
/
scanto.c
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
#include <stdio.h>
#include <stdlib.h>
#include <libxml/parser.h>
#include <libxml/nanohttp.h>
#include <glib/gstrfuncs.h>
#include <unistd.h>
#include <string.h>
#include "scanto.h"
int getScanToNotes(struct scantonotes *sn, char *phost) {
xmlDocPtr doc;
xmlNodePtr cur1, cur2;
char *key;
char url[1024];
snprintf(url, 1024, "http://%s/hp/device/notifications.xml", phost);
if ((doc = xmlParseFile(url)) == NULL) {
return(-1);
}
cur1 = xmlDocGetRootElement(doc);
if (xmlStrcmp(cur1->name, (const xmlChar *) "Notifications")) {
fprintf(stderr,"document of the wrong type, root node != Notifications");
xmlFreeDoc(doc);
return -1;
}
cur1 = cur1->xmlChildrenNode;
while (cur1 != NULL) {
if ((!xmlStrcmp(cur1->name, (const xmlChar *)"ScanToNotifications"))) {
cur2 = cur1->xmlChildrenNode;
while (cur2 != NULL) {
if ((!xmlStrcmp(cur2->name, (const xmlChar *)"ScanToDeviceDisplay"))) {
key = (char *) xmlNodeListGetString(doc, cur2->xmlChildrenNode, 1);
if (key != NULL) {
g_strlcpy(sn->display, key, 255);
} else {
sn->display[0] = '\0';
}
xmlFree(key);
} else if ((!xmlStrcmp(cur2->name, (const xmlChar *)"ScanToHostID"))) {
key = (char *) xmlNodeListGetString(doc, cur2->xmlChildrenNode, 1);
if (key != NULL) {
g_strlcpy(sn->host, key, 255);
} else {
sn->host[0] = '\0';
}
xmlFree(key);
} else if ((!xmlStrcmp(cur2->name, (const xmlChar *)"ScanToNotSetup"))) {
key = (char *) xmlNodeListGetString(doc, cur2->xmlChildrenNode, 1);
sn->notsetup = atoi(key);
xmlFree(key);
} else if ((!xmlStrcmp(cur2->name, (const xmlChar *)"ADFLoaded"))) {
key = (char *) xmlNodeListGetString(doc, cur2->xmlChildrenNode, 1);
sn->adf = atoi(key);
xmlFree(key);
}
cur2 = cur2->next;
}
}
cur1 = cur1->next;
}
xmlFreeDoc(doc);
return(0);
}
int getScanToDest(int mode, char *phost) {
xmlDocPtr doc;
xmlNodePtr cur1, cur2, cur3;
xmlChar *key;
int destcnt = 0;
char url[1024];
snprintf(url, 1024, "http://%s/hp/device/info_scanto_destinations.xml", phost);
if ((doc = xmlParseFile(url)) == NULL) {
return(-1);
}
cur1 = xmlDocGetRootElement(doc);
if (xmlStrcmp(cur1->name, (const xmlChar *) "ScanToDestinations")) {
fprintf(stderr,"document of the wrong type, root node != Notifications");
xmlFreeDoc(doc);
return -1;
}
cur1 = cur1->xmlChildrenNode;
while (cur1 != NULL) {
if ((!xmlStrcmp(cur1->name, (const xmlChar *)"ScanToDestinationList"))) {
cur2 = cur1->xmlChildrenNode;
while (cur2 != NULL) {
if ((!xmlStrcmp(cur2->name, (const xmlChar *)"ScanToDestination"))) {
cur3 = cur2->xmlChildrenNode;
while (cur3 != NULL) {
if ((!xmlStrcmp(cur3->name, (const xmlChar *)"DeviceDisplay"))) {
key = xmlNodeListGetString(doc, cur3->xmlChildrenNode, 1);
if (key != NULL) {
if (mode == 1) {
delScanToDest(phost, key);
} else {
printf("Dest: %s\n", key);
}
destcnt++;
}
xmlFree(key);
}
cur3 = cur3->next;
}
}
cur2 = cur2->next;
}
}
cur1 = cur1->next;
}
xmlFreeDoc(doc);
return(destcnt);
}
int delAllScanToDest(char *phost) {
return(getScanToDest(1, phost));
}
int myScanToDest(int mode, char *phost, char *dest) {
xmlNanoHTTPInit();
char * pContentType = 0;
void * Ctxt = 0;
char url[1024];
char host1[256];
char *host2;
gethostname(host1, 256);
host2 = strtok(host1, ".");
if(mode==1) {
snprintf(url, 1024, "http://%s/hp/device/set_config.html?RemoveScanToDest_1=%s", phost, dest);
} else {
snprintf(url, 1024, "http://%s/hp/device/set_config.html?AddScanToDest_1=127.0.0.1-%s%%5e%s%%5eDestFolder", phost, host2, dest);
}
Ctxt = xmlNanoHTTPOpen(url, &pContentType);
if (Ctxt == 0) {
fprintf(stderr, "ERROR: xmlNanoHTTPOpen() == 0\n");
return(-1);
}
if (xmlNanoHTTPReturnCode(Ctxt) != 200) {
fprintf(stderr, "ERROR: HTTP Code != OK\n");
return(-1);
}
xmlFree(pContentType);
xmlNanoHTTPClose(Ctxt);
xmlNanoHTTPCleanup();
return(0);
}
int addScanToDest(char *phost, char *dest) {
return(myScanToDest(0, phost, dest));
}
int delScanToDest(char *phost, char *dest) {
return(myScanToDest(1, phost, dest));
}