[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 | |
---|
[1792] | 29 | #include "IRC.h" |
---|
| 30 | |
---|
[3196] | 31 | #include <cpptcl/cpptcl.h> |
---|
| 32 | |
---|
| 33 | #include "util/Convert.h" |
---|
[5747] | 34 | #include "util/Exception.h" |
---|
[6417] | 35 | #include "util/StringUtils.h" |
---|
[1505] | 36 | #include "ConsoleCommand.h" |
---|
| 37 | #include "TclThreadManager.h" |
---|
| 38 | |
---|
| 39 | namespace orxonox |
---|
| 40 | { |
---|
[1784] | 41 | static const unsigned int IRC_TCL_THREADID = 1421421421; |
---|
| 42 | |
---|
[7236] | 43 | SetConsoleCommand("IRC", "say", &IRC::say); |
---|
| 44 | SetConsoleCommand("IRC", "msg", &IRC::msg); |
---|
| 45 | SetConsoleCommand("IRC", "nick", &IRC::nick); |
---|
[1505] | 46 | |
---|
| 47 | IRC::IRC() |
---|
| 48 | { |
---|
[3304] | 49 | this->interpreter_ = 0; |
---|
[1505] | 50 | } |
---|
| 51 | |
---|
| 52 | void IRC::initialize() |
---|
| 53 | { |
---|
[3301] | 54 | unsigned int threadID = IRC_TCL_THREADID; |
---|
[3318] | 55 | this->interpreter_ = TclThreadManager::createWithId(threadID); |
---|
[1505] | 56 | |
---|
| 57 | try |
---|
| 58 | { |
---|
[3370] | 59 | this->interpreter_->def("::orxonox::irc::say", IRC::tcl_say, Tcl::variadic()); |
---|
| 60 | this->interpreter_->def("::orxonox::irc::privmsg", IRC::tcl_privmsg, Tcl::variadic()); |
---|
| 61 | this->interpreter_->def("::orxonox::irc::action", IRC::tcl_action, Tcl::variadic()); |
---|
| 62 | this->interpreter_->def("::orxonox::irc::info", IRC::tcl_info, Tcl::variadic()); |
---|
[1505] | 63 | } |
---|
| 64 | catch (Tcl::tcl_error const &e) |
---|
| 65 | { COUT(1) << "Tcl (IRC) error: " << e.what(); } |
---|
| 66 | |
---|
[3280] | 67 | this->nickname_ = "orx" + multi_cast<std::string>(static_cast<unsigned int>(rand())); |
---|
[1505] | 68 | TclThreadManager::execute(threadID, "set nickname " + this->nickname_); |
---|
| 69 | TclThreadManager::execute(threadID, "source irc.tcl"); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | IRC& IRC::getInstance() |
---|
| 73 | { |
---|
| 74 | static IRC instance; |
---|
| 75 | return instance; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | bool IRC::eval(const std::string& command) |
---|
| 79 | { |
---|
[3304] | 80 | if (!IRC::getInstance().interpreter_) |
---|
[1505] | 81 | { |
---|
| 82 | IRC::getInstance().initialize(); |
---|
| 83 | COUT(1) << "Error: IRC client wasn't yet initialized, please try again." << std::endl; |
---|
| 84 | return false; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | try |
---|
| 88 | { |
---|
[3304] | 89 | IRC::getInstance().interpreter_->eval(command); |
---|
[1505] | 90 | return true; |
---|
| 91 | } |
---|
| 92 | catch (Tcl::tcl_error const &e) |
---|
| 93 | { COUT(1) << "Tcl (IRC) error: " << e.what(); } |
---|
| 94 | |
---|
| 95 | return false; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | void IRC::say(const std::string& message) |
---|
| 99 | { |
---|
[6417] | 100 | if (IRC::eval("irk::say $conn #orxonox {" + message + '}')) |
---|
[1505] | 101 | IRC::tcl_say(Tcl::object(), Tcl::object(IRC::getInstance().nickname_), Tcl::object(message)); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | void IRC::msg(const std::string& channel, const std::string& message) |
---|
| 105 | { |
---|
[6417] | 106 | if (IRC::eval("irk::say $conn " + channel + " {" + message + '}')) |
---|
[1505] | 107 | IRC::tcl_privmsg(Tcl::object(channel), Tcl::object(IRC::getInstance().nickname_), Tcl::object(message)); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | void IRC::nick(const std::string& nickname) |
---|
| 111 | { |
---|
| 112 | if (IRC::eval("irk::nick $conn " + nickname)) |
---|
| 113 | IRC::getInstance().nickname_ = nickname; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | void IRC::tcl_say(Tcl::object const &channel, Tcl::object const &nick, Tcl::object const &args) |
---|
| 117 | { |
---|
| 118 | COUT(0) << "IRC> " << nick.get() << ": " << stripEnclosingBraces(args.get()) << std::endl; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | void IRC::tcl_privmsg(Tcl::object const &query, Tcl::object const &nick, Tcl::object const &args) |
---|
| 122 | { |
---|
| 123 | COUT(0) << "IRC (" << query.get() << ")> " << nick.get() << ": " << stripEnclosingBraces(args.get()) << std::endl; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | void IRC::tcl_action(Tcl::object const &channel, Tcl::object const &nick, Tcl::object const &args) |
---|
| 127 | { |
---|
[6417] | 128 | COUT(0) << "IRC> * " << nick.get() << ' ' << stripEnclosingBraces(args.get()) << std::endl; |
---|
[1505] | 129 | } |
---|
| 130 | |
---|
| 131 | void IRC::tcl_info(Tcl::object const &channel, Tcl::object const &args) |
---|
| 132 | { |
---|
| 133 | COUT(0) << "IRC> --> " << stripEnclosingBraces(args.get()) << std::endl; |
---|
| 134 | } |
---|
| 135 | } |
---|