Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/keckslevelHS14/src/libraries/core/command/IRC.cc @ 10256

Last change on this file since 10256 was 8858, checked in by landauf, 13 years ago

merged output branch back to trunk.

Changes:

  • you have to include util/Output.h instead of util/Debug.h
  • COUT(x) is now called orxout(level)
  • output levels are now defined by an enum instead of numbers. see util/Output.h for the definition
  • it's possible to use output contexts with orxout(level, context). see util/Output.h for some common contexts. you can define more contexts
  • you must use 'endl' at the end of an output message, '\n' does not flush the message

Output levels:

  • instead of COUT(0) use orxout()
  • instead of COUT(1) use orxout(user_error) or orxout(internal_error)
  • instead of COUT(2) use orxout(user_warning) or orxout(internal_warning)
  • instead of COUT(3) use orxout(user_status/user_info) or orxout(internal_status/internal_info)
  • instead of COUT(4) use orxout(verbose)
  • instead of COUT(5) use orxout(verbose_more)
  • instead of COUT(6) use orxout(verbose_ultra)

Guidelines:

  • user_* levels are for the user, visible in the console and the log-file
  • internal_* levels are for developers, visible in the log-file
  • verbose_* levels are for debugging, only visible if the context of the output is activated

Usage in C++:

  • orxout() << "message" << endl;
  • orxout(level) << "message" << endl;
  • orxout(level, context) << "message" << endl;

Usage in Lua:

  • orxout("message")
  • orxout(orxonox.level.levelname, "message")
  • orxout(orxonox.level.levelname, "context", "message")

Usage in Tcl (and in the in-game-console):

  • orxout levelname message
  • orxout_context levelname context message
  • shortcuts: log message, error message, warning message, status message, info message, debug message
  • Property svn:eol-style set to native
File size: 5.9 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        {   orxout(user_error, context::tcl) << "Tcl (IRC) error: " << e.what() << endl;   }
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            orxout(user_error) << "IRC client wasn't yet initialized, please try again." << 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        {   orxout(user_error, context::tcl) << "Tcl (IRC) error: " << e.what() << endl;   }
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        orxout(message) << "IRC> " << nick.get() << ": " << stripEnclosingBraces(args.get()) << 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        orxout(message) << "IRC (" << query.get() << ")> " << nick.get() << ": " << stripEnclosingBraces(args.get()) << 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        orxout(message) << "IRC> * " << nick.get() << ' ' << stripEnclosingBraces(args.get()) << 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        orxout(message) << "IRC> --> " << stripEnclosingBraces(args.get()) << endl;
160    }
161}
Note: See TracBrowser for help on using the repository browser.