Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/command/IRC.cc @ 7401

Last change on this file since 7401 was 7401, checked in by landauf, 14 years ago

merged doc branch back to trunk

  • Property svn:eol-style set to native
File size: 5.8 KB
RevLine 
[1505]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
[7401]29/**
30    @file
31    @brief Implementation of the IRC class and the IRC console commands.
32*/
33
[1792]34#include "IRC.h"
35
[3196]36#include <cpptcl/cpptcl.h>
37
38#include "util/Convert.h"
[5747]39#include "util/Exception.h"
[6417]40#include "util/StringUtils.h"
[1505]41#include "ConsoleCommand.h"
42#include "TclThreadManager.h"
43
44namespace orxonox
45{
[7401]46    static const unsigned int IRC_TCL_THREADID  = 1421421421;   ///< The ID of the thread in TclThreadManager that is used for the IRC connection
[1784]47
[7236]48    SetConsoleCommand("IRC", "say",  &IRC::say);
49    SetConsoleCommand("IRC", "msg",  &IRC::msg);
50    SetConsoleCommand("IRC", "nick", &IRC::nick);
[1505]51
[7401]52    /**
53        @brief Returns the only existing instance of IRC.
54    */
55    IRC& IRC::getInstance()
56    {
57        static IRC instance;
58        return instance;
59    }
60
61    /**
62        @brief Constructor: Doesn't yet connect to IRC nor does it create a Tcl interpreter.
63        The IRC object will automatically connect to the IRC server if one of the registered
64        console commands is used the first time.
65    */
[1505]66    IRC::IRC()
67    {
[3304]68        this->interpreter_ = 0;
[1505]69    }
70
[7401]71    /**
72        @brief Creates and initializes a new multithreaded Tcl-interpreter and defines some callbacks to display IRC-messages in the console.
73    */
[1505]74    void IRC::initialize()
75    {
[3301]76        unsigned int threadID = IRC_TCL_THREADID;
[3318]77        this->interpreter_ = TclThreadManager::createWithId(threadID);
[1505]78
79        try
80        {
[3370]81            this->interpreter_->def("::orxonox::irc::say", IRC::tcl_say, Tcl::variadic());
82            this->interpreter_->def("::orxonox::irc::privmsg", IRC::tcl_privmsg, Tcl::variadic());
83            this->interpreter_->def("::orxonox::irc::action", IRC::tcl_action, Tcl::variadic());
84            this->interpreter_->def("::orxonox::irc::info", IRC::tcl_info, Tcl::variadic());
[1505]85        }
86        catch (Tcl::tcl_error const &e)
87        {   COUT(1) << "Tcl (IRC) error: " << e.what();   }
88
[3280]89        this->nickname_ = "orx" + multi_cast<std::string>(static_cast<unsigned int>(rand()));
[1505]90        TclThreadManager::execute(threadID, "set nickname " + this->nickname_);
91        TclThreadManager::execute(threadID, "source irc.tcl");
92    }
93
[7401]94    /**
95        @brief Executes a Tcl-command on the Tcl-interpreter.
96    */
[1505]97    bool IRC::eval(const std::string& command)
98    {
[3304]99        if (!IRC::getInstance().interpreter_)
[1505]100        {
101            IRC::getInstance().initialize();
102            COUT(1) << "Error: IRC client wasn't yet initialized, please try again." << std::endl;
103            return false;
104        }
105
106        try
107        {
[3304]108            IRC::getInstance().interpreter_->eval(command);
[1505]109            return true;
110        }
111        catch (Tcl::tcl_error const &e)
112        {   COUT(1) << "Tcl (IRC) error: " << e.what();   }
113
114        return false;
115    }
116
[7401]117    /// Console command: Sends a message to the current channel on the IRC server.
[1505]118    void IRC::say(const std::string& message)
119    {
[6417]120        if (IRC::eval("irk::say $conn #orxonox {" + message + '}'))
[1505]121            IRC::tcl_say(Tcl::object(), Tcl::object(IRC::getInstance().nickname_), Tcl::object(message));
122    }
123
[7401]124    /// Console command: Sends a message to a given channel or nickname on the IRC server.
[1505]125    void IRC::msg(const std::string& channel, const std::string& message)
126    {
[6417]127        if (IRC::eval("irk::say $conn " + channel + " {" + message + '}'))
[1505]128            IRC::tcl_privmsg(Tcl::object(channel), Tcl::object(IRC::getInstance().nickname_), Tcl::object(message));
129    }
130
[7401]131    /// Console command: Changes the nickname on the IRC server.
[1505]132    void IRC::nick(const std::string& nickname)
133    {
134        if (IRC::eval("irk::nick $conn " + nickname))
135            IRC::getInstance().nickname_ = nickname;
136    }
137
[7401]138    /// Tcl-callback: Prints a message that was received from the current IRC channel to the console.
[1505]139    void IRC::tcl_say(Tcl::object const &channel, Tcl::object const &nick, Tcl::object const &args)
140    {
141        COUT(0) << "IRC> " << nick.get() << ": " << stripEnclosingBraces(args.get()) << std::endl;
142    }
143
[7401]144    /// Tcl-callback: Prints a private message that was received from a user to the console.
[1505]145    void IRC::tcl_privmsg(Tcl::object const &query, Tcl::object const &nick, Tcl::object const &args)
146    {
147        COUT(0) << "IRC (" << query.get() << ")> " << nick.get() << ": " << stripEnclosingBraces(args.get()) << std::endl;
148    }
149
[7401]150    /// Tcl-callback: Prints an action-message (usually /me ...) that was received from the current IRC channel to the console.
[1505]151    void IRC::tcl_action(Tcl::object const &channel, Tcl::object const &nick, Tcl::object const &args)
152    {
[6417]153        COUT(0) << "IRC> * " << nick.get() << ' ' << stripEnclosingBraces(args.get()) << std::endl;
[1505]154    }
155
[7401]156    /// Tcl-callback: Prints all kinds of information that were received from the IRC server or channel (connection info, join, part, modes, ...) to the console.
[1505]157    void IRC::tcl_info(Tcl::object const &channel, Tcl::object const &args)
158    {
159        COUT(0) << "IRC> --> " << stripEnclosingBraces(args.get()) << std::endl;
160    }
161}
Note: See TracBrowser for help on using the repository browser.