File: listaccounts.cpp

package info (click to toggle)
passwordmaker-cli 1.5%2Bdfsg-5
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 636 kB
  • sloc: cpp: 6,768; javascript: 639; makefile: 15
file content (149 lines) | stat: -rwxr-xr-x 4,645 bytes parent folder | download | duplicates (5)
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
/**
 * PasswordMaker - Creates and manages passwords
 * Copyright (C) 2005 Eric H. Jung and LeahScape, Inc.
 * http://passwordmaker.org/
 * grimholtz@yahoo.com
 *
 * 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
 * FITNESSFOR 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Written by Miquel Burns <miquelfire@gmail.com> and Eric H. Jung
 * listaccounts.cpp by Dave Marotti <landshark666@gmail.com>
 */
#include <vector>
#include <algorithm>
#include <set>
#include "stdafx.h"
#include "tinyxml.h"
#include "listaccounts.h"

using namespace std;

static set<string> findSeq(string filepath, string about);
static string findDesc(string filepath, string about);


// Not the fastest nor most elegant method of parsing this thing, but it
// works. Well, it works for my password file. -dave
void listAccounts(string filepath, string about, int indent) {
	set<string> list;
	set<string>::iterator iter;

	list = findSeq(filepath, about);

	if(list.size()>0) {
		for(iter = list.begin(); iter!=list.end(); iter++) {
			set<string> sublist = findSeq(filepath, *iter);

			cout << string(indent, ' ') << findDesc(filepath, *iter) << endl;
			for(set<string>::iterator i2=sublist.begin(); i2!=sublist.end(); i2++) {
				listAccounts(filepath, *i2, indent+4);
			}
		}
	}
	else {
		cout << string(indent, ' ') << findDesc(filepath, about) << endl;
	}
}


// Gets the set of subnodes for a given RDF:Seq node
set<string> findSeq(string filepath, string about) {
	TiXmlDocument document(filepath);
	document.LoadFile();
	string ns;
	set<string> list;

	if (!document.Error()) {
		TiXmlHandle docHandle(&document);
		TiXmlElement *defaultNode = docHandle.FirstChildElement("RDF:RDF").Element();

		if (defaultNode) {
			TiXmlAttribute *attrib = defaultNode->FirstAttribute();
			// Find the namespace
			while (attrib) {
				if (!strcmp(attrib->Value(), "http://passwordmaker.mozdev.org/rdf#")) {
					ns = attrib->Name();
					ns.erase(ns.begin(), ns.begin() + (ns.find(':') + 1));
					break;
				}
				attrib = attrib->Next();
			}
			if (ns.empty()) {
				// Namespace was not found. Not a valid file?
				defaultNode = NULL;
			}
         else {
				defaultNode = docHandle.FirstChildElement("RDF:RDF").FirstChildElement("RDF:Seq").Element();
				while (defaultNode) {
					if(!strcmp(defaultNode->Attribute("RDF:about"), about.c_str())) {
						// dump all children
						TiXmlElement *liNode = defaultNode->FirstChildElement("RDF:li");
						while(liNode) {
							const char *res = liNode->Attribute("RDF:resource");
							if(res) {
								list.insert(string(res));
							}
							liNode = liNode->NextSiblingElement("RDF:li");
						}
						return list;
					}
					defaultNode = defaultNode->NextSiblingElement("RDF:Seq");
				}
			}
		} // end if(defaultNode)
	} // end if(!document.Error())

	return list;
}

// Returns the account name for a description node
string findDesc(string filepath, string about) {
	TiXmlDocument document(filepath);
	document.LoadFile();
	string ns;

	if (!document.Error()) {
		TiXmlHandle docHandle(&document);
		TiXmlElement *defaultNode = docHandle.FirstChildElement("RDF:RDF").Element();

		if (defaultNode) {
			TiXmlAttribute *attrib = defaultNode->FirstAttribute();
			// Find the namespace
			while (attrib) {
				if (!strcmp(attrib->Value(), "http://passwordmaker.mozdev.org/rdf#")) {
					ns = attrib->Name();
					ns.erase(ns.begin(), ns.begin() + (ns.find(':') + 1));
					break;
				}
				attrib = attrib->Next();
			}
			if (ns.empty()) {
				// Namespace was not found. Not a valid file?
				defaultNode = NULL;
			}
         else {
				defaultNode = docHandle.FirstChildElement("RDF:RDF").FirstChildElement("RDF:Description").Element();
				while (defaultNode) {
					if(!strcmp(defaultNode->Attribute("RDF:about"), about.c_str())) {
						return string(defaultNode->Attribute(ns+":name"));
					}
					defaultNode = defaultNode->NextSiblingElement("RDF:Description");
				}
			}
		} // end if(defaultNode)
	} // end if(!document.Error())

	return string("UNKNOWN");
}