controlcommand.cpp

Go to the documentation of this file.
00001 /****************************************************************
00002  *  Vidalia is distributed under the following license:
00003  *
00004  *  Copyright (C) 2006,  Matt Edman, Justin Hipple
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; either version 2
00009  *  of the License, or (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00019  *  Boston, MA  02110-1301, USA.
00020  ****************************************************************/
00021 
00022 /** 
00023  * \file controlcommand.cpp
00024  * \version $Id: controlcommand.cpp 1563 2006-12-26 06:06:04Z edmanm $
00025  * \brief A command sent to Tor's control interface
00026  */
00027 
00028 
00029 #include "controlcommand.h"
00030 
00031 /** Default constructor. */
00032 ControlCommand::ControlCommand()
00033 {
00034 }
00035 
00036 /** Creates a command using the specified keyword. */
00037 ControlCommand::ControlCommand(QString keyword)
00038 {
00039   _keyword = keyword;
00040 }
00041 
00042 /** Creates a control command using the specified keyword and argument. */
00043 ControlCommand::ControlCommand(QString keyword, QString arg)
00044 {
00045   _keyword = keyword;
00046   addArgument(arg);
00047 }
00048 
00049 /** Creates a control command using the specified keyword and list of
00050  * arguments. */
00051 ControlCommand::ControlCommand(QString keyword, QStringList args)
00052 {
00053   _keyword = keyword;
00054   _arguments = args;
00055 }
00056 
00057 /** Sets the keyword for this command. */
00058 void
00059 ControlCommand::setKeyword(QString keyword)
00060 {
00061   _keyword = keyword;
00062 }
00063 
00064 /** Adds an argument to this command's argument list. */
00065 void
00066 ControlCommand::addArgument(QString arg)
00067 {
00068   _arguments << arg;
00069 }
00070 
00071 /** Adds data to the end of this command. */
00072 void
00073 ControlCommand::appendData(QString data)
00074 {
00075   _data << data;
00076 }
00077 
00078 /** Escapes any special characters in this command. */
00079 QString
00080 ControlCommand::escape(QString str)
00081 {
00082   if (str.startsWith(".")) {
00083     str.prepend(".");
00084   }
00085   if (str.endsWith("\r")) {
00086     str.append("\n");
00087   } else {
00088     str.append("\r\n");
00089   }
00090   return str;
00091 }
00092 
00093 /** Formats a command according to Tor's Control Protocol V1. The proper
00094  * format of a command is as follows:
00095  *
00096  *   Command = Keyword Arguments CRLF / "+" Keyword Arguments CRLF Data
00097  *   Keyword = 1*ALPHA
00098  *   Arguments = *(SP / VCHAR)
00099  */
00100 QString
00101 ControlCommand::toString()
00102 {
00103   int i;
00104   QString str;
00105   
00106   /* If this command contains data, then a "+" is prepended to the keyword */
00107   if (_data.size() > 0) {
00108     str = "+";
00109   }
00110   str += _keyword + " ";
00111   
00112   /* Append all specified arguments separated by a space */
00113   str += _arguments.join(" ");
00114 
00115   /* Append whatever data lines have been specified */
00116   if (_data.size() > 0) {
00117     str += "\r\n";
00118     for (i = 0; i < _data.size(); i++) {
00119       str += escape(_data.at(i));
00120     }
00121     str += ".";
00122   }
00123   return str.append("\r\n");
00124 }
00125 

Generated on Wed Sep 5 15:49:27 2007 for Vidalia by  doxygen 1.5.3