Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core4/src/core/IRC.cc @ 3607

Last change on this file since 3607 was 3265, checked in by rgrieder, 15 years ago
  • Removed superfluous convertToString and convertFromString (replaced them with multi_cast)
  • Replaced all getConvertedValue with multi_cast since it doesn't involve a potential implicit cast of the argument (template type deduction always leads to the right type)
  • Using NilValue<T>() instead of zeroise<T>() for better performance (detail, but just caught my eye)
  • Property svn:eol-style set to native
File size: 4.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#include "IRC.h"
30
31#include <cpptcl/cpptcl.h>
32
33#include "util/Convert.h"
34#include "ConsoleCommand.h"
35#include "CoreIncludes.h"
36#include "TclThreadManager.h"
37
38namespace orxonox
39{
40    static const unsigned int IRC_TCL_THREADID  = 1421421421;
41
42    SetConsoleCommand(IRC, say,  true).accessLevel(AccessLevel::User);
43    SetConsoleCommand(IRC, msg,  false).accessLevel(AccessLevel::User);
44    SetConsoleCommand(IRC, nick, false).accessLevel(AccessLevel::User);
45
46    IRC::IRC()
47    {
48        RegisterRootObject(IRC);
49        this->bundle_ = 0;
50    }
51
52    void IRC::initialize()
53    {
54        unsigned int threadID = (unsigned int)IRC_TCL_THREADID;
55        TclThreadManager::createID(threadID);
56        this->bundle_ = TclThreadManager::getInstance().getInterpreterBundle(threadID);
57
58        try
59        {
60            this->bundle_->interpreter_->def("orxonox::irc::say", IRC::tcl_say, Tcl::variadic());
61            this->bundle_->interpreter_->def("orxonox::irc::privmsg", IRC::tcl_privmsg, Tcl::variadic());
62            this->bundle_->interpreter_->def("orxonox::irc::action", IRC::tcl_action, Tcl::variadic());
63            this->bundle_->interpreter_->def("orxonox::irc::info", IRC::tcl_info, Tcl::variadic());
64        }
65        catch (Tcl::tcl_error const &e)
66        {   COUT(1) << "Tcl (IRC) error: " << e.what();   }
67        catch (std::exception const &e)
68        {   COUT(1) << "Error while initializing Tcl (IRC): " << e.what();   }
69
70        this->nickname_ = "orx" + multi_cast<std::string>(static_cast<unsigned int>(rand()));
71        TclThreadManager::execute(threadID, "set nickname " + this->nickname_);
72        TclThreadManager::execute(threadID, "source irc.tcl");
73    }
74
75    IRC& IRC::getInstance()
76    {
77        static IRC instance;
78        return instance;
79    }
80
81    bool IRC::eval(const std::string& command)
82    {
83        if (!IRC::getInstance().bundle_)
84        {
85            IRC::getInstance().initialize();
86            COUT(1) << "Error: IRC client wasn't yet initialized, please try again." << std::endl;
87            return false;
88        }
89
90        try
91        {
92            IRC::getInstance().bundle_->interpreter_->eval(command);
93            return true;
94        }
95        catch (Tcl::tcl_error const &e)
96        {   COUT(1) << "Tcl (IRC) error: " << e.what();   }
97        catch (std::exception const &e)
98        {   COUT(1) << "Error while executing Tcl (IRC): " << e.what();   }
99
100        return false;
101    }
102
103    void IRC::say(const std::string& message)
104    {
105        if (IRC::eval("irk::say $conn #orxonox {" + message + "}"))
106            IRC::tcl_say(Tcl::object(), Tcl::object(IRC::getInstance().nickname_), Tcl::object(message));
107    }
108
109    void IRC::msg(const std::string& channel, const std::string& message)
110    {
111        if (IRC::eval("irk::say $conn " + channel + " {" + message + "}"))
112            IRC::tcl_privmsg(Tcl::object(channel), Tcl::object(IRC::getInstance().nickname_), Tcl::object(message));
113    }
114
115    void IRC::nick(const std::string& nickname)
116    {
117        if (IRC::eval("irk::nick $conn " + nickname))
118            IRC::getInstance().nickname_ = nickname;
119    }
120
121    void IRC::tcl_say(Tcl::object const &channel, Tcl::object const &nick, Tcl::object const &args)
122    {
123        COUT(0) << "IRC> " << nick.get() << ": " << stripEnclosingBraces(args.get()) << std::endl;
124    }
125
126    void IRC::tcl_privmsg(Tcl::object const &query, Tcl::object const &nick, Tcl::object const &args)
127    {
128        COUT(0) << "IRC (" << query.get() << ")> " << nick.get() << ": " << stripEnclosingBraces(args.get()) << std::endl;
129    }
130
131    void IRC::tcl_action(Tcl::object const &channel, Tcl::object const &nick, Tcl::object const &args)
132    {
133        COUT(0) << "IRC> * " << nick.get() << " " << stripEnclosingBraces(args.get()) << std::endl;
134    }
135
136    void IRC::tcl_info(Tcl::object const &channel, Tcl::object const &args)
137    {
138        COUT(0) << "IRC> --> " << stripEnclosingBraces(args.get()) << std::endl;
139    }
140}
Note: See TracBrowser for help on using the repository browser.