/* tag: irc header file for both irc and ircchannel Copyright (c) 2001,2002 David Roundy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef INCLUDE_IRC #define INCLUDE_IRC #include #include inline void irc_name(wxString &name) { name.Replace(" ","_"); } inline wxString display_name(wxString name) { name.Replace("_"," "); return name; } BEGIN_DECLARE_EVENT_TYPES() DECLARE_EVENT_TYPE(wxEVT_COMMAND_IRC_MESSAGE, 3) DECLARE_EVENT_TYPE(wxEVT_COMMAND_IRC_JOINED, 5) DECLARE_EVENT_TYPE(wxEVT_COMMAND_IRC_LEFT, 6) DECLARE_EVENT_TYPE(wxEVT_COMMAND_IRC_IJOINED, 8) END_DECLARE_EVENT_TYPES() class IrcMsgEvent : public wxCommandEvent { DECLARE_DYNAMIC_CLASS( IrcMsgEvent ) public: IrcMsgEvent( wxEvtHandler* win = (wxEvtHandler*) NULL, const wxString& from = "TEST", const wxString& type = "TEST", const wxString& message = "TEST"); wxEvent *Clone() const { return new IrcMsgEvent(*this); }; #if wxCHECK_VERSION(2, 2, 0) void CopyObject( wxObject& obj ) const; #endif const wxString GetFrom() { return m_from; }; const wxString GetType() { return m_type; }; const wxString GetMyMessage() { return m_message; }; private: wxString m_from, m_type, m_message; }; typedef void (wxEvtHandler::*IrcMsgEventFunction)(IrcMsgEvent&); #define EVT_IRC_MESSAGE(fn) DECLARE_EVENT_TABLE_ENTRY( \ wxEVT_COMMAND_IRC_MESSAGE, \ -1, \ -1, \ (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) \ (IrcMsgEventFunction) &fn, \ (wxObject *)NULL ), #define EVT_IRC_JOINED(fn) DECLARE_EVENT_TABLE_ENTRY( \ wxEVT_COMMAND_IRC_JOINED, \ -1, \ -1, \ (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) \ (IrcMsgEventFunction) &fn, \ (wxObject *)NULL ), #define EVT_IRC_IJOINED(fn) DECLARE_EVENT_TABLE_ENTRY( \ wxEVT_COMMAND_IRC_IJOINED, \ -1, \ -1, \ (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) \ (IrcMsgEventFunction) &fn, \ (wxObject *)NULL ), #define EVT_IRC_LEFT(fn) DECLARE_EVENT_TABLE_ENTRY( \ wxEVT_COMMAND_IRC_LEFT, \ -1, \ -1, \ (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) \ (IrcMsgEventFunction) &fn, \ (wxObject *)NULL ), class IrcChannel; // declare our list class. WX_DECLARE_LIST(IrcChannel, IrcChannelList); class Irc : wxEvtHandler { int m_state, m_connected; wxSocketClient *m_sock; public: Irc(); virtual ~Irc(); void OnSocketEvent(wxSocketEvent& event); virtual void SendBack(int len, const char *data); virtual void SendBack(const wxString &str); virtual void Connect(const wxString& hostname, unsigned short port, const wxString& username = "anonymous"); virtual void Disconnect(void); virtual bool IsConnected(void); void AskTopic(const wxString &channel, const wxString &asker = "none"); void SetTopic(const wxString &topic, const wxString &channel); virtual IrcChannel *Join(const wxString& room, wxEvtHandler* parent, wxTextCtrl* output); virtual void Leave(const wxString& room); virtual IrcChannel *AmInChannel(const wxString& room); void ProcessInput(const unsigned char *bytesRead, int buf); wxString Login() {return m_login;}; wxStringList &ListRooms(); private: void GotMessageCmd(const wxString &line); void GotJoinedCmd(const wxString &line); void GotLeftCmd(const wxString &line); void GotWhoCmd(const wxString &line); void GotTopic(const wxString &line); void GotNames(const wxString &line); IrcChannel *GetRoom(const wxString &room); wxString tail_end; // The cut-off part of a message. (due to buffer size) wxString m_login; IrcChannelList *m_rooms; IrcChannelList *m_topic_askers; DECLARE_EVENT_TABLE() }; // Now we have the channel class, which is linked to a user interface: // Warning!!! IrcChannels should ONLY be created by the Irc Join method! class IrcChannel : wxEvtHandler { public: IrcChannel(Irc *the_irc, wxEvtHandler* parent, wxTextCtrl* output, const wxString& room); virtual ~IrcChannel(); virtual void Send(const wxString &str); void SendTo(const wxString &who, const wxString &str); void SendMsg(const wxString &type, const wxString &msg, const wxString &who = "", const wxString &from = ""); void SendVersion(); // The following should probably be protected, as they should only be // called by the Irc object, m_irc. void GotMessageEvent(IrcMsgEvent my_event); void GotJoiner(const wxString &who); void GotLeftCmd(const wxString &line); void GotWhoCmd(const wxString &line); void GotNames(const wxString &names); void AskTopic(const wxString &channel = "none"); void SetTopic(const wxString &topic, const wxString &channel = "none"); void GotChat(const wxString &from, const wxString &msg); wxString Login() { return Mother()->Login(); }; int NumOthersInRoom(); wxStringList PeopleInRoom() { return m_logged_in; }; wxString TheRoom() {return m_room;}; Irc *Mother() {return m_irc;}; private: Irc *m_irc; wxString m_channel_name; wxStringList m_logged_in; wxTextCtrl* m_log; wxString m_room; wxString m_magic; // header for messages intended for parent. wxEvtHandler *m_parent_handler; }; #endif /* INCLUDE_IRC */