1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include "pwmd_error.h"
37 gboolean
valid_xml_element(const xmlChar
*element
)
41 if (!element
|| !*element
|| isdigit(*element
))
44 for (p
= element
; *p
; p
++) {
49 if (xmlStrncasecmp(element
, (xmlChar
*)"?xml", 4) == 0)
55 gboolean
new_account(xmlDocPtr doc
, gchar
*name
)
57 xmlNodePtr root
= xmlDocGetRootElement(doc
);
64 n
= xmlNewNode(NULL
, (xmlChar
*)"account");
65 n
= xmlAddChild(root
, n
);
66 a
= xmlNewProp(n
, (xmlChar
*)"name", (xmlChar
*)name
);
73 "<?xml version=\"1.0\"?>\n"
74 "<!DOCTYPE accounts [\n"
75 "<!ELEMENT accounts (account*)>\n"
76 "<!ELEMENT account (username?, password?, smtp?, pop?, imap?)>\n"
77 "<!ATTLIST account name CDATA #REQUIRED>\n"
78 "<!ELEMENT username (#PCDATA)>\n"
79 "<!ELEMENT password (#PCDATA)>\n"
80 "<!ELEMENT smtp (hostname, port, ssl, sslfingerprint)>\n"
81 "<!ELEMENT pop (hostname, port, ssl, sslfingerprint)>\n"
82 "<!ELEMENT imap (hostname, port, ssl, sslfingerprint)>\n"
83 "<!ELEMENT hostname (#PCDATA)>\n"
84 "<!ELEMENT port (#PCDATA)>\n"
85 "<!ELEMENT ssl (#PCDATA)>\n"
86 "<!ELEMENT sslfingerprint (#PCDATA)>\n"
89 return g_strdup(line
);
92 gboolean
list_accounts(xmlTextReaderPtr reader
, gchar
**dst
, gint
*pwmd_errno
)
100 * "fast forward" to the first account.
102 while (xmlTextReaderRead(reader
) == 1) {
103 if ((n
= xmlTextReaderCurrentNode(reader
)) == NULL
) {
104 *pwmd_errno
= EPWMD_LIBXML_ERROR
;
108 if (xmlTextReaderDepth(reader
) == 1 &&
109 xmlStrcmp(n
->name
, (xmlChar
*)"account") == 0 &&
110 xmlTextReaderNodeType(reader
) == XML_READER_TYPE_ELEMENT
) {
111 xmlChar
*p
= xmlTextReaderGetAttribute(reader
, (xmlChar
*)"name");
116 str
= g_realloc(str
, (i
+ 2) * sizeof(gchar
*));
117 str
[i
++] = g_strdup((gchar
*)p
);
124 *pwmd_errno
= EPWMD_EMPTY_ELEMENT
;
128 line
= g_strjoinv("\n", str
);
134 gboolean
find_account(xmlTextReaderPtr reader
, gchar
*name
)
138 while (xmlTextReaderRead(reader
) == 1) {
139 if ((n
= xmlTextReaderCurrentNode(reader
)) == NULL
)
142 if (xmlTextReaderDepth(reader
) == 1 &&
143 xmlStrcmp(n
->name
, (xmlChar
*)"account") == 0 &&
144 xmlTextReaderNodeType(reader
) == XML_READER_TYPE_ELEMENT
) {
145 xmlChar
*p
= xmlTextReaderGetAttribute(reader
, (xmlChar
*)"name");
147 // Shouldn't happen (DTD).
151 if (xmlStrcmp(p
, (xmlChar
*)name
) == 0) {
163 gint
find_element(xmlTextReaderPtr reader
, gchar
*e
, gint more
)
168 while (xmlTextReaderRead(reader
) == 1) {
169 if ((n
= xmlTextReaderCurrentNode(reader
)) == NULL
)
173 * Stop processing after the closing account element to prevent
174 * finding element names that may exist in another account.
176 if (xmlTextReaderDepth(reader
) == 1 &&
177 xmlStrcmp(n
->name
, (xmlChar
*)"account") == 0 &&
178 xmlTextReaderNodeType(reader
) == XML_READER_TYPE_END_ELEMENT
)
181 if (xmlStrcmp(n
->name
, (xmlChar
*)e
) == 0 &&
182 xmlTextReaderNodeType(reader
) == XML_READER_TYPE_ELEMENT
) {
186 if (xmlTextReaderNext(reader
) != 1)
189 type
= xmlTextReaderNodeType(reader
);
191 if (type
!= XML_READER_TYPE_TEXT
)
201 xmlNodePtr
find_node(xmlNodePtr root
, xmlChar
*name
)
205 for (n
= root
->children
; n
; n
= n
->next
) {
206 if (g_ascii_strcasecmp((gchar
*)name
, (gchar
*)n
->name
) == 0)
213 gint
open_xml(const gchar
*data
, gsize size
, xmlDocPtr
*doc
,
214 xmlTextReaderPtr
*reader
)
216 if ((*doc
= xmlReadMemory(data
, size
, NULL
, "UTF-8",
217 XML_PARSE_NOBLANKS
)) == NULL
)
220 if ((*reader
= xmlReaderWalker(*doc
)) == NULL
)