Vidalia  0.3.1
TorControlPrototype.cpp
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If you
4 ** did not receive the LICENSE file with this file, you may obtain it from the
5 ** Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file TorControlPrototype.cpp
13 ** \brief Prototype for TorControl class
14 */
15 
16 #include "TorControlPrototype.h"
17 
18 #define GET_AND_CALL(type, func, res) \
19  type obj = qscriptvalue_cast<type>(thisObject()); \
20  if(obj) \
21  res = obj->func;
22 
23 #define MERGE2(result, errmsg) \
24  QVariant(QList<QVariant>() << result << errmsg);
25 
26 #define DEF_TYPE0(type, retType, func, call) \
27 retType \
28 type##Prototype::func \
29 { \
30  type *obj = qscriptvalue_cast<type *>(thisObject()); \
31  if(obj) \
32  return obj->call; \
33 }
34 
35 #define DEF_TYPE1(type, resType, func, call) \
36 QVariant \
37 type##Prototype::func \
38 { \
39  resType res; \
40  QString errmsg; \
41 \
42  type *obj = qscriptvalue_cast<type *>(thisObject()); \
43  if(obj) \
44  res = obj->call; \
45  QList<QVariant> vals; \
46  vals << res << QVariant(errmsg); \
47 \
48  return vals; \
49 }
50 
51 #define DEF_TYPE2(type, resType, ansType, func, call) \
52 QVariant \
53 type##Prototype::func \
54 { \
55  resType res; \
56  ansType ans; \
57  QString errmsg; \
58 \
59  type *obj = qscriptvalue_cast<type *>(thisObject()); \
60  if(obj) \
61  res = obj->call; \
62  QList<QVariant> vals; \
63  vals << QVariant(ans) << res << QVariant(errmsg); \
64 \
65  return vals; \
66 }
67 
69  : QObject(), QScriptable() {}
70 
71 int
73  return qMetaTypeId<TorControl *>();
74 }
75 
76 QString
78  return QString("TorControl");
79 }
80 
81 DEF_TYPE0(TorControl, void,
82  start(const QString &tor, const QStringList &args),
83  start(tor, args))
84 
86  stop(),
87  stop(&errmsg))
88 
89 DEF_TYPE0(TorControl, bool,
90  isRunning(),
91  isRunning())
92 
93 DEF_TYPE0(TorControl, bool,
96 
100 
101 DEF_TYPE0(TorControl, void,
102  connect(const QHostAddress &address, quint16 port),
103  connect(address, port))
104 
105 DEF_TYPE0(TorControl, void,
106  connect(const QString &path),
107  connect(path))
108 
109 DEF_TYPE0(TorControl, void,
110  disconnect(),
111  disconnect())
112 
113 DEF_TYPE0(TorControl, bool,
114  isConnected(),
115  isConnected())
116 
117 DEF_TYPE1(TorControl, bool,
118  authenticate(const QByteArray cookie),
119  authenticate(cookie, &errmsg))
120 
121 DEF_TYPE1(TorControl, bool,
122  authenticate(const QString &password),
123  authenticate(password, &errmsg))
124 
125 // TODO: make a QVariant for this two
126 //QVariant
127 //TorControlPrototype::protocolInfo()
128 //{
129 // ProtocolInfo info;
130 // QString errmsg;
131 
132 // GET_AND_CALL(TorControl *, protocolInfo(&errmsg), info)
133 
134 // return MERGE2(info, errmsg);
135 //}
136 
137 //BootstrapStatus
138 //TorControlPrototype::bootstrapStatus(QString *errmsg)
139 //{
140 // BootstrapStatus status;
141 // QString errmsg;
142 
143 // GET_AND_CALL(TorControl *, protocolInfo(&errmsg), status)
144 
145 // return MERGE2(status, errmsg);
146 //}
147 
148 DEF_TYPE0(TorControl, bool,
151 
152 DEF_TYPE1(TorControl, bool,
153  getInfo(QHash<QString,QString> &map),
154  getInfo(map, &errmsg))
155 
156 // TODO: this one may be useless
157 //QVariant
158 //TorControlPrototype::getInfo(QString key)
159 //{
160 // TorControl *obj = qscriptvalue_cast<TorControl *>(thisObject());
161 // QString val, *errmsg = new QString();
162 // bool res = false;
163 // QList<QVariant> vals;
164 
165 // if(obj)
166 // res = obj->getInfo(key, val, errmsg);
167 
168 // vals.append(QVariant(res));
169 // vals.append(QVariant(val));
170 // vals.append(QVariant(*errmsg));
171 
172 // return QVariant(vals);
173 //}
174 
175 // TODO: There is no StringList, this may be useless
176 //DEF_TYPE1(TorControl, QVariantMap,
177 // getInfo(const QStringList &keys),
178 // getInfo(keys, &errmsg))
179 
181  getInfo(const QString &key),
182  getInfo(key, &errmsg))
183 
184 DEF_TYPE1(TorControl, bool,
185  signal(TorSignal::Signal sig),
186  signal(sig, &errmsg))
187 
188 // TODO: QVariant don't like QHostAddress
189 //DEF_TYPE1(TorControl, QHostAddress,
190 // getSocksAddress(),
191 // getSocksAddress(&errmsg))
192 
193 // TODO: make it a QVariant(QList<QVariant>() << QVariant(QString) <<
194 // QVariant(QString) ...
195 QStringList
197 {
198  TorControl *obj = qscriptvalue_cast<TorControl *>(thisObject());
199 
200  if(obj)
201  return obj->getSocksAddressList(errmsg);
202 }
203 
205  getSocksPort(),
206  getSocksPort(&errmsg))
207 
208 // TODO: same as getSocksAddressList but with quint16
209 QList<quint16>
210 TorControlPrototype::getSocksPortList(QString *errmsg)
211 {
212  TorControl *obj = qscriptvalue_cast<TorControl *>(thisObject());
213 
214  if(obj)
215  return obj->getSocksPortList(errmsg);
216 }
217 
218 DEF_TYPE0(TorControl, QString,
221 
223  getTorVersion(),
224  getTorVersion())
225 
226 DEF_TYPE1(TorControl, bool,
227  setEvent(TorEvents::Event e, bool add, bool set),
228  setEvent(e, add, set, &errmsg))
229 
230 DEF_TYPE1(TorControl, bool,
231  setEvents(),
232  setEvents(&errmsg))
233 
234 DEF_TYPE1(TorControl, bool,
235  setConf(QHash<QString,QString> map),
236  setConf(map, &errmsg))
237 
238 DEF_TYPE1(TorControl, bool,
239  setConf(QString key, QString value),
240  setConf(key, value, &errmsg))
241 
242 DEF_TYPE1(TorControl, bool,
243  setConf(QString keyAndValue),
244  setConf(keyAndValue, &errmsg))
245 
246 // TODO: macros don't like template variables
247 // do this one by hand
248 //DEF_TYPE2(TorControl, bool, QHash<QString,QString>,
249 // getConf(QHash<QString,QString> &map),
250 // getConf(map, &errmsg))
251 bool
252 TorControlPrototype::getConf(QHash<QString,QString> &map, QString *errmsg)
253 {
254  TorControl *obj = qscriptvalue_cast<TorControl *>(thisObject());
255 
256  if(obj)
257  return obj->getConf(map, errmsg);
258 }
259 
260 // TODO: this one too
261 bool
262 TorControlPrototype::getConf(QHash<QString,QStringList> &map, QString *errmsg)
263 {
264  TorControl *obj = qscriptvalue_cast<TorControl *>(thisObject());
265 
266  if(obj)
267  return obj->getConf(map, errmsg);
268 }
269 
270 DEF_TYPE2(TorControl, bool, QString,
271  getConf(QString key),
272  getConf(key, ans, &errmsg))
273 
274 // TODO: same as the last one with StringList
275 bool
276 TorControlPrototype::getConf(QString key, QStringList &value, QString *errmsg)
277 {
278  TorControl *obj = qscriptvalue_cast<TorControl *>(thisObject());
279 
280  if(obj)
281  return obj->getConf(key, value, errmsg);
282 }
283 
284 // TODO: idem
285 QVariantMap
286 TorControlPrototype::getConf(const QStringList &keys, QString *errmsg)
287 {
288  TorControl *obj = qscriptvalue_cast<TorControl *>(thisObject());
289 
290  if(obj)
291  return obj->getConf(keys, errmsg);
292 }
293 
294 // TODO: possibly useless
295 //QVariant
296 //TorControlPrototype::getConf(const QString &key, QString *errmsg)
297 //{
298 // TorControl *obj = qscriptvalue_cast<TorControl *>(thisObject());
299 
300 // if(obj)
301 // return obj->getConf(key, errmsg);
302 //}
303 
304 DEF_TYPE1(TorControl, QString,
305  getHiddenServiceConf(const QString &key),
306  getHiddenServiceConf(key, &errmsg))
307 
308 DEF_TYPE1(TorControl, bool,
309  saveConf(),
310  saveConf(&errmsg))
311 
312 // TODO: another stringlist one
313 bool
314 TorControlPrototype::resetConf(QStringList keys, QString *errmsg)
315 {
316  TorControl *obj = qscriptvalue_cast<TorControl *>(thisObject());
317 
318  if(obj)
319  return obj->resetConf(keys, errmsg);
320 }
321 
323  resetConf(QString key),
324  resetConf(key, &errmsg))
325 
326 // TODO: you know
327 QStringList
328 TorControlPrototype::getRouterDescriptorText(const QString &id, QString *errmsg)
329 {
330  TorControl *obj = qscriptvalue_cast<TorControl *>(thisObject());
331 
332  if(obj)
333  return obj->getRouterDescriptorText(id, errmsg);
334 }
335 
336 // TODO: QVariantize RouterDescriptor
338 TorControlPrototype::getRouterDescriptor(const QString &id, QString *errmsg)
339 {
340  TorControl *obj = qscriptvalue_cast<TorControl *>(thisObject());
341 
342  if(obj)
343  return obj->getRouterDescriptor(id, errmsg);
344 }
345 
346 // TODO: QVariantize RouterStatus
348 TorControlPrototype::getRouterStatus(const QString &id, QString *errmsg)
349 {
350  TorControl *obj = qscriptvalue_cast<TorControl *>(thisObject());
351 
352  if(obj)
353  return obj->getRouterStatus(id, errmsg);
354 }
355 
356 // TODO: QVariantize NetworkStatus
359 {
360  TorControl *obj = qscriptvalue_cast<TorControl *>(thisObject());
361 
362  if(obj)
363  return obj->getNetworkStatus(errmsg);
364 }
365 
366 // TODO: QVariantize DescriptorAnnotations
368 TorControlPrototype::getDescriptorAnnotations(const QString &id, QString *errmsg)
369 {
370  TorControl *obj = qscriptvalue_cast<TorControl *>(thisObject());
371 
372  if(obj)
373  return obj->getDescriptorAnnotations(id, errmsg);
374 }
375 
376 // TODO: QVariantize CircuitList
379 {
380  TorControl *obj = qscriptvalue_cast<TorControl *>(thisObject());
381 
382  if(obj)
383  return obj->getCircuits(errmsg);
384 }
385 
386 // TODO: QVariantize StreamList
387 StreamList
389 {
390  TorControl *obj = qscriptvalue_cast<TorControl *>(thisObject());
391 
392  if(obj)
393  return obj->getStreams(errmsg);
394 }
395 
396 // TODO: QVariantize AddressMap
397 AddressMap
399 {
400  TorControl *obj = qscriptvalue_cast<TorControl *>(thisObject());
401 
402  if(obj)
403  return obj->getAddressMap(type, errmsg);
404 }
405 
407  ipToCountry(const QHostAddress &ip),
408  ipToCountry(ip, &errmsg))
409 
410 // TODO: migrate CircuitId
411 bool
412 TorControlPrototype::closeCircuit(const CircuitId &circId, bool ifUnused, QString *errmsg)
413 {
414  TorControl *obj = qscriptvalue_cast<TorControl *>(thisObject());
415 
416  if(obj)
417  return obj->closeCircuit(circId, ifUnused, errmsg);
418 }
419 
420 // TODO: migrate StreamId
421 bool
422 TorControlPrototype::closeStream(const StreamId &streamId, QString *errmsg)
423 {
424  TorControl *obj = qscriptvalue_cast<TorControl *>(thisObject());
425 
426  if(obj)
427  return obj->closeStream(streamId, errmsg);
428 }
bool getConf(QHash< QString, QString > &map, QString *errmsg=0)
Definition: TorControl.cpp:766
QList< quint16 > getSocksPortList(QString *errmsg=0)
Definition: TorControl.cpp:620
Q_INVOKABLE CircuitList getCircuits(QString *errmsg=0)
bool closeStream(const StreamId &streamId, QString *errmsg=0)
stop errmsg QVariant
Q_INVOKABLE QVariant authenticate(const QByteArray cookie)
Q_INVOKABLE void start(const QString &tor, const QStringList &args)
StreamList getStreams(QString *errmsg=0)
NetworkStatus getNetworkStatus(QString *errmsg=0)
Definition: TorControl.cpp:981
Q_INVOKABLE quint32 getTorVersion()
Q_INVOKABLE QList< quint16 > getSocksPortList(QString *errmsg=0)
Q_INVOKABLE QVariant getInfo(QHash< QString, QString > &map)
Q_INVOKABLE QStringList getRouterDescriptorText(const QString &id, QString *errmsg=0)
Q_INVOKABLE bool isVidaliaRunningTor()
Q_INVOKABLE QVariant stop()
Q_INVOKABLE NetworkStatus getNetworkStatus(QString *errmsg=0)
#define DEF_TYPE2(type, resType, ansType, func, call)
Q_INVOKABLE bool isRunning()
Q_INVOKABLE QVariant setEvent(TorEvents::Event e, bool add=true, bool set=true)
QStringList getRouterDescriptorText(const QString &id, QString *errmsg=0)
Definition: TorControl.cpp:953
#define DEF_TYPE1(type, resType, func, call)
bool closeStream(const StreamId &streamId, QString *errmsg=0)
AddressMap getAddressMap(AddressMap::AddressMapType type=AddressMap::AddressMapAll, QString *errmsg=0)
QList< Stream > StreamList
Definition: Stream.h:97
Q_INVOKABLE QVariant getHiddenServiceConf(const QString &key)
Q_INVOKABLE RouterStatus getRouterStatus(const QString &id, QString *errmsg=0)
bool closeCircuit(const CircuitId &circId, bool ifUnused=false, QString *errmsg=0)
Q_INVOKABLE QVariant ipToCountry(const QHostAddress &ip)
DescriptorAnnotations getDescriptorAnnotations(const QString &id, QString *errmsg=0)
Q_INVOKABLE bool isConnected()
QString StreamId
Definition: Stream.h:28
RouterDescriptor getRouterDescriptor(const QString &id, QString *errmsg=0)
Definition: TorControl.cpp:962
RouterStatus getRouterStatus(const QString &id, QString *errmsg=0)
Definition: TorControl.cpp:971
Q_INVOKABLE RouterDescriptor getRouterDescriptor(const QString &id, QString *errmsg=0)
QStringList getSocksAddressList(QString *errmsg=0)
Definition: TorControl.cpp:596
Q_INVOKABLE QVariant saveConf()
QList< Circuit > CircuitList
Definition: Circuit.h:81
bool closeCircuit(const CircuitId &circId, bool ifUnused=false, QString *errmsg=0)
Q_INVOKABLE QVariant signal(TorSignal::Signal sig)
Q_INVOKABLE QVariant setEvents()
Q_INVOKABLE QVariant getSocksPort()
Q_INVOKABLE bool isCircuitEstablished()
CircuitList getCircuits(QString *errmsg=0)
bool resetConf(QStringList keys, QString *errmsg=0)
Definition: TorControl.cpp:929
Q_INVOKABLE void connect(const QHostAddress &address, quint16 port)
QHash< QString, QString > DescriptorAnnotations
Definition: TorControl.h:42
QString CircuitId
Definition: Circuit.h:24
Q_INVOKABLE StreamList getStreams(QString *errmsg=0)
Q_INVOKABLE DescriptorAnnotations getDescriptorAnnotations(const QString &id, QString *errmsg=0)
Q_INVOKABLE void closeTorStdout()
Q_INVOKABLE void disconnect()
Q_INVOKABLE QVariant setConf(QHash< QString, QString > map)
QList< RouterStatus > NetworkStatus
Definition: RouterStatus.h:97
Q_INVOKABLE QStringList getSocksAddressList(QString *errmsg=0)
#define DEF_TYPE0(type, retType, func, call)
Q_INVOKABLE bool resetConf(QStringList keys, QString *errmsg=0)
Q_INVOKABLE QString getTorVersionString()
Q_INVOKABLE bool getConf(QHash< QString, QString > &map, QString *errmsg)
Q_INVOKABLE AddressMap getAddressMap(AddressMap::AddressMapType type=AddressMap::AddressMapAll, QString *errmsg=0)