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