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
32 void write_dtd(FILE *fp
)
35 "<?xml version=\"1.0\"?>\n"
36 "<!DOCTYPE accounts [\n"
37 "!ELEMENT accounts (account*)>\n"
38 "!ELEMENT account (name, username?, password?, smtp?, pop?, imap?)>\n"
39 "!ELEMENT name (#PCDATA)>\n"
40 "!ELEMENT username (#PCDATA)>\n"
41 "!ELEMENT password (#PCDATA)>\n"
42 "!ELEMENT smtp (host, port, ssl)>\n"
43 "!ELEMENT pop (host, port, ssl)>\n"
44 "!ELEMENT imap (host, port, ssl)>\n"
45 "!ELEMENT host (#PCDATA)>\n"
46 "!ELEMENT port (#PCDATA)>\n"
47 "!ELEMENT ssl (#PCDATA)>\n"
52 int find_account(xmlTextReaderPtr reader
, xmlChar
*name
)
57 while (xmlTextReaderRead(reader
) == 1) {
58 if ((n
= xmlTextReaderCurrentNode(reader
)) == NULL
)
62 * "name" is the element that holds the account name.
64 if (xmlTextReaderDepth(reader
) == 1 &&
65 xmlStrcmp(n
->name
, (xmlChar
*)"name") &&
66 xmlTextReaderNodeType(reader
) == XML_READER_TYPE_ELEMENT
) {
68 if (xmlTextReaderNext(reader
) != 1)
70 type
= xmlTextReaderNodeType(reader
);
71 } while (type
!= -1 && type
!= XML_READER_TYPE_TEXT
);
73 if ((n
= xmlTextReaderCurrentNode(reader
)) == NULL
)
76 if (xmlStrcmp(n
->content
, name
) == 0)
84 int find_element(xmlTextReaderPtr reader
, xmlChar
*e
, int more
)
89 while (xmlTextReaderRead(reader
) == 1) {
90 if ((n
= xmlTextReaderCurrentNode(reader
)) == NULL
)
93 if (xmlStrcmp(n
->name
, e
) == 0 &&
94 xmlTextReaderNodeType(reader
) == XML_READER_TYPE_ELEMENT
) {
99 * FIXME account/service without details (account/service/host).
102 if (xmlTextReaderNext(reader
) != 1)
104 type
= xmlTextReaderNodeType(reader
);
105 } while (type
!= -1 && type
!= XML_READER_TYPE_TEXT
);
106 return (type
== XML_READER_TYPE_TEXT
) ? 0 : 1;
113 xmlNodePtr
find_node(xmlNodePtr root
, xmlChar
*name
)
117 for (n
= root
->children
; n
; n
= n
->next
) {
118 if (strcmp((char *)name
, (char *)n
->name
) == 0)
125 xmlNodePtr
create_doc(xmlDocPtr
*doc
, const char *root
)
127 xmlDocPtr d
= xmlNewDoc((xmlChar
*)"1.0");
128 xmlNodePtr n
= xmlNewNode(NULL
, (xmlChar
*)root
);
131 xmlDocSetRootElement(d
, n
);
135 int open_xml(const char *data
, int size
, xmlDocPtr
*doc
, xmlNodePtr
*root
,
136 xmlTextReaderPtr
*reader
)
139 if ((*doc
= xmlReadMemory(data
, size
, NULL
, NULL
, 0)) == NULL
)
143 if ((*doc
= xmlReadFile(data
, NULL
, 0)) == NULL
)
147 *root
= xmlDocGetRootElement(*doc
);
149 if ((*reader
= xmlReaderWalker(*doc
)) == NULL
)