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
|
/*
* libfritz++
*
* Copyright (C) 2007-2012 Joachim Wilke <libfritz@joachim-wilke.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "FritzFonbook.h"
#include <algorithm>
#include <sstream>
#include "Config.h"
#include "FritzClient.h"
#include "Tools.h"
#include <liblog++/Log.h>
#include <libconv++/CharsetConverter.h>
#include <libconv++/EntityConverter.h>
namespace fritz {
FritzFonbook::FritzFonbook()
:XmlFonbook(I18N_NOOP("Fritz!Box phone book"), "FRITZ", true), thread{nullptr}
{
setInitialized(false);
}
FritzFonbook::~FritzFonbook() {
if (thread) {
thread->join();
delete thread;
}
}
bool FritzFonbook::initialize() {
reload();
return true;
}
void FritzFonbook::run() {
DBG("FritzFonbook thread started");
setInitialized(false);
clear();
FritzClient *fc = gConfig->fritzClientFactory->create();
std::string msg = fc->requestFonbook();
delete fc;
if (msg.find("<?xml") == std::string::npos)
parseHtmlFonbook(&msg);
else {
parseXmlFonbook(&msg);
setWriteable(); // we can write xml back to the FB
}
setInitialized(true);
sort(FonbookEntry::ELEM_NAME, true);
DBG("FritzFonbook thread ended");
}
void FritzFonbook::parseHtmlFonbook(std::string *msg) {
DBG("Parsing fonbook using html parser.")
// determine charset (default for old firmware versions is iso-8859-15)
size_t pos;
std::string charset = "ISO-8859-15";
pos = msg->find("<meta http-equiv=content-type");
if (pos != std::string::npos) {
pos = msg->find("charset=", pos);
if (pos != std::string::npos)
charset = msg->substr(pos+8, msg->find('"', pos)-pos-8);
}
DBG("using charset " << charset);
std::string msgConv = convert::CharsetConverter::ConvertToLocalEncoding(*msg, charset);
// parse answer
pos = 0;
int count = 0;
// parser for old format
const std::string tag("(TrFon(");
while ((pos = msgConv.find(tag, pos)) != std::string::npos) {
pos += 7; // points to the first "
int nameStart = msgConv.find(',', pos) +3;
int nameStop = msgConv.find('"', nameStart) -1;
int numberStart = msgConv.find(',', nameStop) +3;
int numberStop = msgConv.find('"', numberStart) -1;
if (msgConv[nameStart] == '!') // skip '!' char, older firmware versions use to mark important
nameStart++;
std::string namePart = msgConv.substr(nameStart, nameStop - nameStart+1);
std::string namePart2 = convert::EntityConverter::DecodeEntities(namePart);
std::string numberPart = msgConv.substr(numberStart, numberStop - numberStart+1);
if (namePart2.length() && numberPart.length()) {
FonbookEntry fe(namePart2, false); // TODO: important is not parsed here
fe.addNumber(numberPart, FonbookEntry::TYPE_NONE);
addFonbookEntry(fe);
//DBG("(%s / %s)", fe.number.c_str(), fe.name.c_str());
}
pos += 10;
count++;
}
// parser for new format
pos = 0;
const std::string tagName("TrFonName(");
const std::string tagNumber("TrFonNr(" );
// iterate over all tagNames
while ((pos = msgConv.find(tagName, ++pos)) != std::string::npos) {
int nameStart = msgConv.find(',', pos+7) +3;
int nameStop = msgConv.find('"', nameStart) -1;
std::string namePart = msgConv.substr(nameStart, nameStop - nameStart+1);
std::string namePartConv = convert::EntityConverter::DecodeEntities(namePart);
FonbookEntry fe(namePartConv, false); // TODO: important is not parsed here
size_t posInner = pos;
// iterate over all tagNumbers between two tagNames
while ((posInner = msgConv.find(tagNumber, ++posInner)) != std::string::npos && posInner < msgConv.find(tagName, pos+1)) {
int typeStart = posInner + 9;
int numberStart = msgConv.find(',', posInner) +3;
int typeStop = numberStart - 5;
int numberStop = msgConv.find('"', numberStart) -1;
std::string numberPart = msgConv.substr(numberStart, numberStop - numberStart+1);
std::string typePart = msgConv.substr(typeStart, typeStop - typeStart+1);
FonbookEntry::eType type = FonbookEntry::TYPE_NONE;
if (typePart.compare("home") == 0)
type = FonbookEntry::TYPE_HOME;
else if (typePart.compare("mobile") == 0)
type = FonbookEntry::TYPE_MOBILE;
else if (typePart.compare("work") == 0)
type = FonbookEntry::TYPE_WORK;
if (namePartConv.length() && numberPart.length()) {
fe.addNumber(numberPart, type); // TODO: quickdial, vanity and priority not parsed here
//DBG("(%s / %s / %i)", fe.number.c_str(), fe.name.c_str(), fe.type);
}
count++;
}
addFonbookEntry(fe);
}
}
void FritzFonbook::reload() {
if (thread) {
thread->join();
delete thread;
}
thread = new std::thread(&FritzFonbook::run, this);
}
void FritzFonbook::write() {
if (isWriteable()) {
INF("Uploading phonebook to Fritz!Box.");
FritzClient *fc = gConfig->fritzClientFactory->create();
fc->writeFonbook(serializeToXml());
delete fc;
}
}
}
|