summaryrefslogtreecommitdiffstats
path: root/src/pedro/pedroconfig.cpp
blob: 1197a6f1faff9dc97cdfb48793e68b65a511ccbb (plain)
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
221
222
223
224
225
226
227
228
229
230
/*
 * Implementation the Pedro mini-XMPP client
 *
 * Authors:
 *   Bob Jamison
 *
 * Copyright (C) 2005-2006 Bob Jamison
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */


/*
====================================================
We are expecting an xml file with this format:

<pedro>

    <!-- zero to many of these -->
    <account>
        <name>Jabber's Main Server</name>
        <host>jabber.org</host>
        <port>5222</port>
        <username>myname</username>
        <password>mypassword</password>
    </account>

</pedro>


====================================================
*/



#include "pedroconfig.h"
#include "pedrodom.h"

#include <stdio.h>

namespace Pedro
{


static long  getInt(const DOMString &s)
{
    char *start = (char *) s.c_str();
    char *end;
    long val = strtol(start, &end, 10);
    if (end == start) // did we read more than 1 digit?
        val = 0L;
    return val;
}



bool XmppConfig::read(const DOMString &buffer)
{
    Parser parser;

    Element *root = parser.parse(buffer);

    if (!root)
        {
        error("Error in configuration syntax");
        return false;
        }

    accounts.clear();

    std::vector<Element *> accountElems = root->findElements("account");

    for (unsigned int i=0 ; i<accountElems .size() ; i++)
        {
        XmppAccount account;
        Element *elem = accountElems [i];

        DOMString str = elem ->getTagValue("name");
        if (str.size()==0)
            str = "unnamed account";
        account.setName(str);

        str = elem->getTagValue("host");
        if (str.size()==0)
            str = "jabber.org";
        account.setName(str);

        str = elem->getTagValue("port");
        int port = (int) getInt(str);
        if (port == 0)
            port = 5222;
        account.setPort(port);

        str = elem->getTagValue("username");
        if (str.size()==0)
            str = "noname";
        account.setUsername(str);

        str = elem->getTagValue("password");
        if (str.size()==0)
            str = "nopass";
        account.setPassword(str);

        accounts.push_back(account);
        }


    delete root;

    return true;
}






bool XmppConfig::readFile(const DOMString &fileName)
{

    FILE *f = fopen(fileName.c_str(), "rb");
    if (!f)
        {
        error("Could not open configuration file '%s' for reading",
              fileName.c_str());
        return false;
        }

    DOMString buffer;
    while (!feof(f))
        {
        char ch = (char) fgetc(f);
        buffer.push_back(ch);
        }
    fclose(f);

    if (!read(buffer))
        return false;

    return true;
}


DOMString XmppConfig::toXmlBuffer()
{

    DOMString buf;

    char fmtbuf[32];

    buf.append("<pedro>\n");

    for (unsigned int i = 0 ; i<accounts.size() ; i++)
        {
        XmppAccount acc = accounts[i];
        buf.append("    <account>\n");
        buf.append("        <name>");
        buf.append(acc.getName());
        buf.append("</name>\n");
        buf.append("        <host>");
        buf.append(acc.getHost());
        buf.append("</host>\n");
        buf.append("        <port>");
        snprintf(fmtbuf, 31, "%d", acc.getPort());
        buf.append(fmtbuf);
        buf.append("</port>\n");
        buf.append("        <username>");
        buf.append(acc.getUsername());
        buf.append("</username>\n");
        buf.append("        <password>");
        buf.append(acc.getPassword());
        buf.append("</password>\n");
        buf.append("    </account>\n");
        }

    buf.append("</pedro>\n");

    return buf;
}




bool XmppConfig::writeFile(const DOMString &fileName)
{

    FILE *f = fopen(fileName.c_str(), "wb");
    if (!f)
        {
        error("Could not open configuration file '%s' for writing",
              fileName.c_str());
        return false;
        }

    DOMString buffer = toXmlBuffer();
    char *s = (char *)buffer.c_str();
    size_t len = (size_t) strlen(s);  //in case we have wide chars

    if (fwrite(s, 1, len, f) != len)
        {
        return false;
        }
    fclose(f);

    if (!read(buffer))
        return false;

    return true;
}





} //namespace Pedro
//########################################################################
//# E N D    O F     F I L E
//########################################################################