Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ai/src/libraries/core/command/IRC.cc @ 8895

Last change on this file since 8895 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
Line 
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
29/**
30    @file
31    @brief Implementation of the IRC class and the IRC console commands.
32*/
33
34#include "IRC.h"
35
36#include <cpptcl/cpptcl.h>
37
38#include "util/Convert.h"
39#include "util/Exception.h"
40#include "util/StringUtils.h"
41#include "ConsoleCommand.h"
42#include "TclThreadManager.h"
43
44namespace orxonox
45{
46    static const unsigned int IRC_TCL_THREADID  = 1421421421;   ///< The ID of the thread in TclThreadManager that is used for the IRC connection
47
48    SetConsoleCommand("IRC", "say",  &IRC::say);
49    SetConsoleCommand("IRC", "msg",  &IRC::msg);
50    SetConsoleCommand("IRC", "nick", &IRC::nick);
51
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    */
66    IRC::IRC()
67    {
68        this->interpreter_ = 0;
69    }
70
71    /**
72        @brief Creates and initializes a new multithreaded Tcl-interpreter and defines some callbacks to display IRC-messages in the console.
73    */
74    void IRC::initialize()
75    {
76        unsigned int threadID = IRC_TCL_THREADID;
77        this->interpreter_ = TclThreadManager::createWithId(threadID);
78
79        try
80        {
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());
85        }
86        catch (Tcl::tcl_error const &e)
87        {   COUT(1) << "Tcl (IRC) error: " << e.what();   }
88
89        this->nickname_ = "orx" + multi_cast<std::string>(static_cast<unsigned int>(rand()));
90        TclThreadManager::execute(threadID, "set nickname " + this->nickname_);
91        TclThreadManager::execute(threadID, "source irc.tcl");
92    }
93
94    /**
95        @brief Executes a Tcl-command on the Tcl-interpreter.
96    */
97    bool IRC::eval(const std::string& command)
98    {
99        if (!IRC::getInstance().interpreter_)
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        {
108            IRC::getInstance().interpreter_->eval(command);
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
117    /// Console command: Sends a message to the current channel on the IRC server.
118    void IRC::say(const std::string& message)
119    {
120        if (IRC::eval("irk::say $conn #orxonox {" + message + '}'))
121            IRC::tcl_say(Tcl::object(), Tcl::object(IRC::getInstance().nickname_), Tcl::object(message));
122    }
123
124    /// Console command: Sends a message to a given channel or nickname on the IRC server.
125    void IRC::msg(const std::string& channel, const std::string& message)
126    {
127        if (IRC::eval("irk::say $conn " + channel + " {" + message + '}'))
128            IRC::tcl_privmsg(Tcl::object(channel), Tcl::object(IRC::getInstance().nickname_), Tcl::object(message));
129    }
130
131    /// Console command: Changes the nickname on the IRC server.
132    void IRC::nick(const std::string& nickname)
133    {
134        if (IRC::eval("irk::nick $conn " + nickname))
135            IRC::getInstance().nickname_ = nickname;
136    }
137
138    /// Tcl-callback: Prints a message that was received from the current IRC channel to the console.
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
144    /// Tcl-callback: Prints a private message that was received from a user to the console.
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
150    /// Tcl-callback: Prints an action-message (usually /me ...) that was received from the current IRC channel to the console.
151    void IRC::tcl_action(Tcl::object const &channel, Tcl::object const &nick, Tcl::object const &args)
152    {
153        COUT(0) << "IRC> * " << nick.get() << ' ' << stripEnclosingBraces(args.get()) << std::endl;
154    }
155
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.
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.