1 /*
2 * Implementation the Pedro mini-XMPP client
3 *
4 * Authors:
5 * Bob Jamison
6 *
7 * Copyright (C) 2005-2007 Bob Jamison
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
25 /*
26 ====================================================
27 We are expecting an xml file with this format:
29 <pedro>
31 <!-- zero to many of these -->
32 <account>
33 <name>Jabber's Main Server</name>
34 <host>jabber.org</host>
35 <port>5222</port>
36 <username>myname</username>
37 <password>mypassword</password>
38 </account>
40 </pedro>
43 ====================================================
44 */
48 #include "pedroconfig.h"
49 #include "pedrodom.h"
51 #include <stdio.h>
53 namespace Pedro
54 {
57 static long getInt(const DOMString &s)
58 {
59 char *start = (char *) s.c_str();
60 char *end;
61 long val = strtol(start, &end, 10);
62 if (end == start) // did we read more than 1 digit?
63 val = 0L;
64 return val;
65 }
69 bool XmppConfig::read(const DOMString &buffer)
70 {
71 Parser parser;
73 Element *root = parser.parse(buffer);
75 if (!root)
76 {
77 error("Error in configuration syntax");
78 return false;
79 }
81 accounts.clear();
83 std::vector<Element *> mucElems = root->findElements("muc");
84 if (mucElems.size() > 0)
85 {
86 Element *elem = mucElems[0];
87 mucGroup = elem->getTagValue("group");
88 mucHost = elem->getTagValue("host");
89 mucNick = elem->getTagValue("nick");
90 mucPassword = elem->getTagValue("password");
91 }
93 std::vector<Element *> accountElems = root->findElements("account");
95 for (unsigned int i=0 ; i<accountElems .size() ; i++)
96 {
97 XmppAccount account;
98 Element *elem = accountElems [i];
100 DOMString str = elem->getTagValue("name");
101 if (str.size()==0)
102 str = "unnamed account";
103 account.setName(str);
105 str = elem->getTagValue("host");
106 if (str.size()==0)
107 str = "jabber.org";
108 account.setHost(str);
110 str = elem->getTagValue("port");
111 int port = (int) getInt(str);
112 if (port == 0)
113 port = 5222;
114 account.setPort(port);
116 str = elem->getTagValue("username");
117 if (str.size()==0)
118 str = "noname";
119 account.setUsername(str);
121 str = elem->getTagValue("password");
122 if (str.size()==0)
123 str = "nopass";
124 account.setPassword(str);
126 accounts.push_back(account);
127 }
130 delete root;
132 return true;
133 }
140 bool XmppConfig::readFile(const DOMString &fileName)
141 {
143 FILE *f = fopen(fileName.c_str(), "rb");
144 if (!f)
145 {
146 error("Could not open configuration file '%s' for reading",
147 fileName.c_str());
148 return false;
149 }
151 DOMString buffer;
152 while (!feof(f))
153 {
154 char ch = (char) fgetc(f);
155 buffer.push_back(ch);
156 }
157 fclose(f);
159 if (!read(buffer))
160 return false;
162 return true;
163 }
166 DOMString XmppConfig::toXmlBuffer()
167 {
169 DOMString buf;
171 char fmtbuf[32];
173 buf.append("<pedro>\n");
174 buf.append(" <muc>\n");
175 buf.append(" <group>");
176 buf.append(mucGroup);
177 buf.append("</group>\n");
178 buf.append(" <host>");
179 buf.append(mucHost);
180 buf.append("</host>\n");
181 buf.append(" <nick>");
182 buf.append(mucNick);
183 buf.append("</nick>\n");
184 buf.append(" <password>");
185 buf.append(mucPassword);
186 buf.append("</password>\n");
187 buf.append(" </muc>\n");
189 for (unsigned int i = 0 ; i<accounts.size() ; i++)
190 {
191 XmppAccount acc = accounts[i];
192 buf.append(" <account>\n");
193 buf.append(" <name>");
194 buf.append(acc.getName());
195 buf.append("</name>\n");
196 buf.append(" <host>");
197 buf.append(acc.getHost());
198 buf.append("</host>\n");
199 buf.append(" <port>");
200 snprintf(fmtbuf, 31, "%d", acc.getPort());
201 buf.append(fmtbuf);
202 buf.append("</port>\n");
203 buf.append(" <username>");
204 buf.append(acc.getUsername());
205 buf.append("</username>\n");
206 buf.append(" <password>");
207 buf.append(acc.getPassword());
208 buf.append("</password>\n");
209 buf.append(" </account>\n");
210 }
212 buf.append("</pedro>\n");
214 return buf;
215 }
220 bool XmppConfig::writeFile(const DOMString &fileName)
221 {
223 FILE *f = fopen(fileName.c_str(), "wb");
224 if (!f)
225 {
226 error("Could not open configuration file '%s' for writing",
227 fileName.c_str());
228 return false;
229 }
231 DOMString buffer = toXmlBuffer();
232 char *s = (char *)buffer.c_str();
233 size_t len = (size_t) strlen(s); //in case we have wide chars
235 if (fwrite(s, 1, len, f) != len)
236 {
237 return false;
238 }
239 fclose(f);
241 if (!read(buffer))
242 return false;
244 return true;
245 }
248 /**
249 *
250 */
251 DOMString XmppConfig::getMucGroup()
252 {
253 return mucGroup;
254 }
256 /**
257 *
258 */
259 void XmppConfig::setMucGroup(const DOMString &val)
260 {
261 mucGroup = val;
262 }
264 /**
265 *
266 */
267 DOMString XmppConfig::getMucHost()
268 {
269 return mucHost;
270 }
272 /**
273 *
274 */
275 void XmppConfig::setMucHost(const DOMString &val)
276 {
277 mucHost = val;
278 }
280 /**
281 *
282 */
283 DOMString XmppConfig::getMucNick()
284 {
285 return mucNick;
286 }
288 /**
289 *
290 */
291 void XmppConfig::setMucNick(const DOMString &val)
292 {
293 mucNick = val;
294 }
296 /**
297 *
298 */
299 DOMString XmppConfig::getMucPassword()
300 {
301 return mucPassword;
302 }
304 /**
305 *
306 */
307 void XmppConfig::setMucPassword(const DOMString &val)
308 {
309 mucPassword = val;
310 }
314 /**
315 *
316 */
317 std::vector<XmppAccount> &XmppConfig::getAccounts()
318 {
319 return accounts;
320 }
323 /**
324 *
325 */
326 bool XmppConfig::accountAdd(const XmppAccount &account)
327 {
328 DOMString name = account.getName();
329 if (name.size() < 1)
330 return false;
331 if (accountExists(name))
332 return false;
333 accounts.push_back(account);
334 return true;
335 }
338 /**
339 *
340 */
341 bool XmppConfig::accountExists(const DOMString &accountName)
342 {
343 if (accountName.size() < 1)
344 return false;
345 std::vector<XmppAccount>::iterator iter;
346 for (iter = accounts.begin() ; iter!= accounts.end() ; iter++)
347 {
348 if (iter->getName() == accountName)
349 return true;
350 }
351 return false;
352 }
356 /**
357 *
358 */
359 void XmppConfig::accountRemove(const DOMString &accountName)
360 {
361 if (accountName.size() < 1)
362 return;
363 std::vector<XmppAccount>::iterator iter;
364 for (iter = accounts.begin() ; iter!= accounts.end() ; )
365 {
366 if (iter->getName() == accountName)
367 iter = accounts.erase(iter);
368 else
369 iter++;
370 }
371 }
374 /**
375 *
376 */
377 bool XmppConfig::accountFind(const DOMString &accountName,
378 XmppAccount &retVal)
379 {
380 if (accountName.size() < 1)
381 return false;
382 std::vector<XmppAccount>::iterator iter;
383 for (iter = accounts.begin() ; iter!= accounts.end() ; iter++)
384 {
385 if (iter->getName() == accountName)
386 {
387 retVal = (*iter);
388 return true;
389 }
390 }
391 return false;
392 }
398 } //namespace Pedro
399 //########################################################################
400 //# E N D O F F I L E
401 //########################################################################