| | 370 | /** Sets each configuration key in \emph map to the value associated with its |
| | 371 | * key. */ |
| | 372 | bool |
| | 373 | TorControl::setConf(QHash<QString,QString> map, QString *errmsg) |
| | 374 | { |
| | 375 | ControlCommand cmd("SETCONF"); |
| | 376 | ControlReply reply; |
| | 377 | QString arg, value; |
| | 378 | |
| | 379 | /* Add each keyvalue to the argument list */ |
| | 380 | foreach (QString key, map.keys()) { |
| | 381 | arg = key; |
| | 382 | value = map.value(key); |
| | 383 | if (value.length() > 0) { |
| | 384 | arg += "=" + value; |
| | 385 | } |
| | 386 | cmd.addArgument(arg); |
| | 387 | } |
| | 388 | |
| | 389 | /* Send the new configuration values to Tor */ |
| | 390 | if (!send(cmd, reply, errmsg)) { |
| | 391 | return false; |
| | 392 | } else { |
| | 393 | if (reply.getStatus() != "250") { |
| | 394 | if (errmsg) { |
| | 395 | *errmsg = reply.getLine().getMessage(); |
| | 396 | } |
| | 397 | return false; |
| | 398 | } |
| | 399 | } |
| | 400 | return true; |
| | 401 | } |
| | 402 | |
| | 403 | /** Sets a single configuration key to the given value. */ |
| | 404 | bool |
| | 405 | TorControl::setConf(QString key, QString value, QString *errmsg) |
| | 406 | { |
| | 407 | QHash<QString,QString> map; |
| | 408 | map.insert(key, value); |
| | 409 | return setConf(map, errmsg); |
| | 410 | } |
| | 411 | |
| | 412 | /** Gets a set of configuration keyvalues and stores them in \emph map. */ |
| | 413 | bool |
| | 414 | TorControl::getConf(QHash<QString,QString> &map, QString *errmsg) |
| | 415 | { |
| | 416 | ControlCommand cmd("GETCONF"); |
| | 417 | ControlReply reply; |
| | 418 | |
| | 419 | /* Add the keys as arguments to the GETINFO message */ |
| | 420 | foreach (QString key, map.keys()) { |
| | 421 | cmd.addArgument(key); |
| | 422 | } |
| | 423 | |
| | 424 | /* Ask Tor for the specified info values */ |
| | 425 | if (send(cmd, reply, errmsg)) { |
| | 426 | /* Parse the response for the returned values */ |
| | 427 | foreach (ReplyLine line, reply.getLines()) { |
| | 428 | if (line.getStatus() != "250") { |
| | 429 | if (errmsg) { |
| | 430 | *errmsg = line.getMessage(); |
| | 431 | } |
| | 432 | return false; |
| | 433 | } |
| | 434 | |
| | 435 | /* Split the "key=val" line and map them */ |
| | 436 | QStringList keyval = line.getMessage().split("="); |
| | 437 | if (keyval.size() == 2) { |
| | 438 | map.insert(keyval.at(0), keyval.at(1)); |
| | 439 | } |
| | 440 | } |
| | 441 | } else { |
| | 442 | /* Sending the control command failed */ |
| | 443 | return false; |
| | 444 | } |
| | 445 | return true; |
| | 446 | } |
| | 447 | |
| | 448 | /** Gets a single configuration keyvalue. */ |
| | 449 | bool |
| | 450 | TorControl::getConf(QString key, QString &value, QString *errmsg) |
| | 451 | { |
| | 452 | QHash<QString,QString> map; |
| | 453 | map.insert(key, ""); |
| | 454 | |
| | 455 | if (getConf(map, errmsg)) { |
| | 456 | value = map.value(key); |
| | 457 | return true; |
| | 458 | } |
| | 459 | return false; |
| | 460 | } |
| | 461 | |
| | 462 | /** Asks Tor to save the current configuration to its torrc. */ |
| | 463 | bool |
| | 464 | TorControl::saveConf(QString *errmsg) |
| | 465 | { |
| | 466 | ControlCommand cmd("SAVECONF"); |
| | 467 | ControlReply reply; |
| | 468 | |
| | 469 | if(!send(cmd, reply, errmsg)) { |
| | 470 | return false; |
| | 471 | } else { |
| | 472 | if (reply.getStatus() != "250") { |
| | 473 | if (errmsg) { |
| | 474 | *errmsg = reply.getMessage(); |
| | 475 | } |
| | 476 | return false; |
| | 477 | } |
| | 478 | } |
| | 479 | return true; |
| | 480 | } |
| | 481 | |
| | 482 | /** Tells Tor to reset the given configuration keys back to defaults. */ |
| | 483 | bool |
| | 484 | TorControl::resetConf(QList<QString> keys, QString *errmsg) |
| | 485 | { |
| | 486 | ControlCommand cmd("RESETCONF"); |
| | 487 | ControlReply reply; |
| | 488 | |
| | 489 | /* Add each key to the argument list */ |
| | 490 | foreach (QString key, keys) { |
| | 491 | cmd.addArgument(key); |
| | 492 | } |
| | 493 | |
| | 494 | if(!send(cmd, reply, errmsg)) { |
| | 495 | return false; |
| | 496 | } else { |
| | 497 | if (reply.getStatus() != "250") { |
| | 498 | if (errmsg) { |
| | 499 | *errmsg = reply.getMessage(); |
| | 500 | } |
| | 501 | return false; |
| | 502 | } |
| | 503 | } |
| | 504 | return true; |
| | 505 | } |
| | 506 | |