Vidalia 0.3.1
ControlCommand.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
4** you did not receive the LICENSE file with this file, you may obtain it
5** from the 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 ControlCommand.cpp
13** \brief A command sent to Tor's control interface
14*/
15
16#include "ControlCommand.h"
17
18
19/** Default constructor. */
21{
22}
23
24/** Creates a command using the specified keyword. */
25ControlCommand::ControlCommand(const QString &keyword)
26{
28}
29
30/** Creates a control command using the specified keyword and argument. */
31ControlCommand::ControlCommand(const QString &keyword, const QString &arg)
32{
34 addArgument(arg);
35}
36
37/** Creates a control command using the specified keyword and list of
38 * arguments. */
39ControlCommand::ControlCommand(const QString &keyword, const QStringList &args)
40{
42 _arguments = args;
43}
44
45/** Sets the keyword for this command. */
46void
47ControlCommand::setKeyword(const QString &keyword)
48{
50}
51
52/** Adds an argument to this command's argument list. */
53void
54ControlCommand::addArgument(const QString &arg)
55{
56 _arguments << arg;
57}
58
59/** Adds all arguments in <b>args</b> to this control command. */
60void
61ControlCommand::addArguments(const QStringList &args)
62{
63 foreach (QString arg, args) {
64 addArgument(arg);
65 }
66}
67
68/** Adds data to the end of this command. */
69void
70ControlCommand::appendData(const QString &data)
71{
72 _data << data;
73}
74
75/** Escapes any special characters in this command. */
76QString
77ControlCommand::escape(const QString &unescaped) const
78{
79 QString str = unescaped;
80 if (str.startsWith(".")) {
81 str.prepend(".");
82 }
83 if (str.endsWith("\r")) {
84 str.append("\n");
85 } else {
86 str.append("\r\n");
87 }
88 return str;
89}
90
91/** Formats a command according to Tor's Control Protocol V1. The proper
92 * format of a command is as follows:
93 *
94 * Command = Keyword Arguments CRLF / "+" Keyword Arguments CRLF Data
95 * Keyword = 1*ALPHA
96 * Arguments = *(SP / VCHAR)
97 */
98QString
100{
101 int i;
102 QString str;
103
104 /* If this command contains data, then a "+" is prepended to the keyword */
105 if (_data.size() > 0) {
106 str = "+";
107 }
108 str += _keyword + " ";
109
110 /* Append all specified arguments separated by a space */
111 str += _arguments.join(" ");
112
113 /* Append whatever data lines have been specified */
114 if (_data.size() > 0) {
115 str += "\r\n";
116 for (i = 0; i < _data.size(); i++) {
117 str += escape(_data.at(i));
118 }
119 str += ".";
120 }
121 return str.append("\r\n");
122}
123
void appendData(const QString &data)
QString escape(const QString &str) const
void addArgument(const QString &arg)
QStringList _data
void addArguments(const QStringList &args)
QString keyword() const
void setKeyword(const QString &keyword)
QString toString() const
QStringList _arguments
QString i(QString str)
Definition: html.cpp:32