Vidalia
0.2.17
|
00001 /* 00002 ** This file is part of Vidalia, and is subject to the license terms in the 00003 ** LICENSE file, found in the top level directory of this distribution. If 00004 ** you did not receive the LICENSE file with this file, you may obtain it 00005 ** from the Vidalia source package distributed by the Vidalia Project at 00006 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia, 00007 ** including this file, may be copied, modified, propagated, or distributed 00008 ** except according to the terms described in the LICENSE file. 00009 */ 00010 00011 /* 00012 ** \file ControlCommand.cpp 00013 ** \brief A command sent to Tor's control interface 00014 */ 00015 00016 #include "ControlCommand.h" 00017 00018 00019 /** Default constructor. */ 00020 ControlCommand::ControlCommand() 00021 { 00022 } 00023 00024 /** Creates a command using the specified keyword. */ 00025 ControlCommand::ControlCommand(const QString &keyword) 00026 { 00027 _keyword = keyword; 00028 } 00029 00030 /** Creates a control command using the specified keyword and argument. */ 00031 ControlCommand::ControlCommand(const QString &keyword, const QString &arg) 00032 { 00033 _keyword = keyword; 00034 addArgument(arg); 00035 } 00036 00037 /** Creates a control command using the specified keyword and list of 00038 * arguments. */ 00039 ControlCommand::ControlCommand(const QString &keyword, const QStringList &args) 00040 { 00041 _keyword = keyword; 00042 _arguments = args; 00043 } 00044 00045 /** Sets the keyword for this command. */ 00046 void 00047 ControlCommand::setKeyword(const QString &keyword) 00048 { 00049 _keyword = keyword; 00050 } 00051 00052 /** Adds an argument to this command's argument list. */ 00053 void 00054 ControlCommand::addArgument(const QString &arg) 00055 { 00056 _arguments << arg; 00057 } 00058 00059 /** Adds all arguments in <b>args</b> to this control command. */ 00060 void 00061 ControlCommand::addArguments(const QStringList &args) 00062 { 00063 foreach (QString arg, args) { 00064 addArgument(arg); 00065 } 00066 } 00067 00068 /** Adds data to the end of this command. */ 00069 void 00070 ControlCommand::appendData(const QString &data) 00071 { 00072 _data << data; 00073 } 00074 00075 /** Escapes any special characters in this command. */ 00076 QString 00077 ControlCommand::escape(const QString &unescaped) const 00078 { 00079 QString str = unescaped; 00080 if (str.startsWith(".")) { 00081 str.prepend("."); 00082 } 00083 if (str.endsWith("\r")) { 00084 str.append("\n"); 00085 } else { 00086 str.append("\r\n"); 00087 } 00088 return str; 00089 } 00090 00091 /** Formats a command according to Tor's Control Protocol V1. The proper 00092 * format of a command is as follows: 00093 * 00094 * Command = Keyword Arguments CRLF / "+" Keyword Arguments CRLF Data 00095 * Keyword = 1*ALPHA 00096 * Arguments = *(SP / VCHAR) 00097 */ 00098 QString 00099 ControlCommand::toString() const 00100 { 00101 int i; 00102 QString str; 00103 00104 /* If this command contains data, then a "+" is prepended to the keyword */ 00105 if (_data.size() > 0) { 00106 str = "+"; 00107 } 00108 str += _keyword + " "; 00109 00110 /* Append all specified arguments separated by a space */ 00111 str += _arguments.join(" "); 00112 00113 /* Append whatever data lines have been specified */ 00114 if (_data.size() > 0) { 00115 str += "\r\n"; 00116 for (i = 0; i < _data.size(); i++) { 00117 str += escape(_data.at(i)); 00118 } 00119 str += "."; 00120 } 00121 return str.append("\r\n"); 00122 } 00123