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. */
26 {
27  _keyword = keyword;
28 }
29 
30 /** Creates a control command using the specified keyword and argument. */
31 ControlCommand::ControlCommand(const QString &keyword, const QString &arg)
32 {
33  _keyword = keyword;
34  addArgument(arg);
35 }
36 
37 /** Creates a control command using the specified keyword and list of
38  * arguments. */
39 ControlCommand::ControlCommand(const QString &keyword, const QStringList &args)
40 {
41  _keyword = keyword;
42  _arguments = args;
43 }
44 
45 /** Sets the keyword for this command. */
46 void
48 {
49  _keyword = keyword;
50 }
51 
52 /** Adds an argument to this command's argument list. */
53 void
54 ControlCommand::addArgument(const QString &arg)
55 {
56  _arguments << arg;
57 }
58 
59 /** Adds all arguments in <b>args</b> to this control command. */
60 void
61 ControlCommand::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. */
69 void
70 ControlCommand::appendData(const QString &data)
71 {
72  _data << data;
73 }
74 
75 /** Escapes any special characters in this command. */
76 QString
77 ControlCommand::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  */
98 QString
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 setKeyword(const QString &keyword)
QStringList _arguments
QString escape(const QString &str) const
QString toString() const
QString i(QString str)
Definition: html.cpp:32
void addArguments(const QStringList &args)
void appendData(const QString &data)
QStringList _data
QString keyword() const
void addArgument(const QString &arg)