OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4Network2IRC.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2010-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 #ifndef C4NETWORK2IRC_H
17 #define C4NETWORK2IRC_H
18 
19 #include "network/C4NetIO.h"
20 
22 {
28 };
29 
30 const int C4NetIRCMaxLogLength = 300000; // Maximum total length of log - if log gets longer, it will be cleared even if messages have never been read
31 const int C4NetIRCMaxReadLogLength = 1000; // Maximum log messages kept even after they've been read
32 
34 {
35  friend class C4Network2IRCClient;
36 public:
37  C4Network2IRCMessage(C4Network2IRCMessageType enType, const char *szSource, const char *szTarget, const char *szData)
38  : iTimestamp(time(nullptr)), eType(enType), Source(szSource), Target(szTarget), Data(szData), Next(nullptr)
39  { }
40 
41 private:
42  time_t iTimestamp;
44  StdCopyStrBuf Source, Target, Data;
45 
46  // used by C4Network2IRCClient
48 
49 public:
50  time_t getTimestamp() const { return iTimestamp; }
51  C4Network2IRCMessageType getType() const { return eType; }
52  const char *getSource() const { return Source.getData(); }
53  const char *getTarget() const { return Target.getData(); }
54  const char *getData() const { return Data.getData(); }
55  bool isChannel() const { return Target.getLength() && (*Target.getData() == '#' || *Target.getData() == '+'); }
56 
57  C4Network2IRCMessage *getNext() const { return Next; }
58 };
59 
61 {
62  friend class C4Network2IRCChannel;
63 public:
64  C4Network2IRCUser(const char *szName);
65 
66 private:
67  StdCopyStrBuf Prefix;
68  StdCopyStrBuf Name;
69 
70  // used by C4Network2IRCChannel
71  C4Network2IRCUser *Next;
72 
73 public:
74  const char *getPrefix() const { return Prefix.getData(); }
75  const char *getName() const { return Name.getData(); }
76  C4Network2IRCUser *getNext() const { return Next; }
77 
78 private:
79  // called by C4Network2IRCChannel
80  void SetPrefix(const char *szPrefix) { Prefix = szPrefix; }
81 
82 };
83 
85 {
86  friend class C4Network2IRCClient;
87 public:
88  C4Network2IRCChannel(const char *szName);
90 
91 private:
92  StdCopyStrBuf Name;
93  StdCopyStrBuf Topic;
94  C4Network2IRCUser *pUsers;
95 
96  bool fReceivingUsers;
97 
98  // used by C4Network2IRCClient
100 
101 public:
102  const char *getName() const { return Name.getData(); }
103  const char *getTopic() const { return Topic.getData(); }
104  C4Network2IRCUser *getUsers() const { return pUsers; }
105  C4Network2IRCUser *getUser(const char *szName) const;
106  bool isUsersLocked() const { return fReceivingUsers; }
107 
108 private:
109  // called by C4Network2IRCClient
110  void OnUsers(const char *szUsers, const char *szPrefixes);
111  void OnUsersEnd();
112  void OnJoin(const char *szUser);
113  void OnPart(const char *szUser, const char *szComment);
114  void OnKick(const char *szUser, const char *szComment);
115  void OnTopic(const char *szTopic);
116 
117  C4Network2IRCUser *AddUser(const char *szName);
118  void DeleteUser(C4Network2IRCUser *pUser);
119  void ClearUsers();
120 };
121 
123 {
124 public:
126  ~C4Network2IRCClient() override;
127 
128 private:
129 
130  // Connection information
131  C4NetIO::addr_t ServerAddr, PeerAddr;
132  bool fConnecting{false}, fConnected{false};
133 
134  // Status information
135  StdCopyStrBuf Nick, RealName, Password;
136  StdCopyStrBuf AutoJoin;
137  C4Network2IRCChannel *pChannels{nullptr};
138 
139  // User mode/prefix map
140  StdCopyStrBuf Prefixes;
141 
142  // Message log
143  C4Network2IRCMessage *pLog{nullptr}, *pLogLastRead, *pLogEnd{nullptr};
144  int32_t iLogLength{0}, iUnreadLogLength{0};
145 
146  // Event queue for notify
147  class C4InteractiveThread *pNotify{nullptr};
148 
149  // Critical section for data
150  CStdCSec CSec;
151 
152 private:
153 
154  // Overridden
155  void PackPacket(const C4NetIOPacket &rPacket, StdBuf &rOutBuf) override;
156  size_t UnpackPacket(const StdBuf &rInBuf, const C4NetIO::addr_t &addr) override;
157 
158  // Callbacks
159  bool OnConn(const C4NetIO::addr_t &AddrPeer, const C4NetIO::addr_t &AddrConnect, const addr_t *pOwnAddr, C4NetIO *pNetIO) override;
160  void OnDisconn(const C4NetIO::addr_t &AddrPeer, C4NetIO *pNetIO, const char *szReason) override;
161  void OnPacket(const class C4NetIOPacket &rPacket, C4NetIO *pNetIO) override;
162 
163 public:
164 
165  CStdCSec *getCSec() { return &CSec; }
166 
167  // only call these while holding a lock on the above critical section!
170  C4Network2IRCChannel *getChannel(const char *szName) const;
171  C4Network2IRCMessage *getMessageLog() const { return pLog; }
172  C4Network2IRCMessage *getUnreadMessageLog() const { return pLogLastRead ? pLogLastRead->getNext() : pLog; }
173  void ClearMessageLog();
174  void MarkMessageLogRead();
175  const char *getUserName() const { return Nick.getData(); }
176 
177  using C4NetIOTCP::Connect;
178  // Simple network communication
179  bool Connect(const char *szServer, const char *szNick, const char *szRealName, const char *szPassword = nullptr, const char *szChannel = nullptr);
180  using C4NetIOTCP::Close;
181  bool Close() override;
182  using C4NetIOTCP::Send;
183  bool Send(const char *szCommand, const char *szParameters = nullptr);
184 
185  // Notfiy interface
186  void SetNotify(class C4InteractiveThread *pnNotify) { pNotify = pnNotify; }
187 
188  // Special IRC commands
189  bool Quit(const char *szReason);
190  bool Join(const char *szChannel);
191  bool Part(const char *szChannel);
192  bool Message(const char *szTarget, const char *szText);
193  bool Notice(const char *szTarget, const char *szText);
194  bool Action(const char *szTarget, const char *szText);
195  bool ChangeNick(const char *szNewNick);
196  bool RegisterNick(const char *szPassword, const char *szMail);
197 
198  // Status
199  bool IsActive() const { return fConnecting || fConnected; }
200  bool IsConnected() const { return fConnected; }
201 
202 private:
203  void OnCommand(const char *szSender, const char *szCommand, const char *szParameters);
204  void OnNumericCommand(const char *szSender, int iCommand, const char *szParameters);
205  void OnConnected();
206  void OnMessage(bool fNotice, const char *szSource, const char *szTarget, const char *szText);
207 
208  void PopMessage();
209  void PushMessage(C4Network2IRCMessageType eType, const char *szSource, const char *szTarget, const char *szText);
210 
211  C4Network2IRCChannel *AddChannel(const char *szName);
212  void DeleteChannel(C4Network2IRCChannel *pChannel);
213 };
214 
215 #endif // C4NETWORK2IRC_H
const char * getData() const
Definition: StdBuf.h:442
C4Network2IRCMessageType getType() const
Definition: C4Network2IRC.h:51
C4Network2IRCUser * getUser(const char *szName) const
bool Send(const C4NetIOPacket &rPacket) override
Definition: C4NetIO.cpp:1248
const char * getName() const
Definition: C4Network2IRC.h:75
Definition: StdBuf.h:29
bool Send(const char *szCommand, const char *szParameters=nullptr)
C4Network2IRCUser * getUsers() const
const int C4NetIRCMaxLogLength
Definition: C4Network2IRC.h:30
bool RegisterNick(const char *szPassword, const char *szMail)
C4Network2IRCMessageType
Definition: C4Network2IRC.h:21
void SetNotify(class C4InteractiveThread *pnNotify)
bool isChannel() const
Definition: C4Network2IRC.h:55
bool Action(const char *szTarget, const char *szText)
bool IsConnected() const
C4Network2IRCChannel * getNextChannel(C4Network2IRCChannel *pPrevChan) const
bool Close() override
C4Network2IRCChannel * getChannel(const char *szName) const
time_t getTimestamp() const
Definition: C4Network2IRC.h:50
C4Network2IRCMessage * getMessageLog() const
const char * getName() const
C4Network2IRCChannel * getFirstChannel() const
C4Network2IRCUser * getNext() const
Definition: C4Network2IRC.h:76
bool Connect(const char *szServer, const char *szNick, const char *szRealName, const char *szPassword=nullptr, const char *szChannel=nullptr)
C4Network2IRCUser(const char *szName)
struct C4NetIOTCP::ConnectWait nullptr
const char * getTopic() const
bool isUsersLocked() const
bool Part(const char *szChannel)
C4Network2IRCMessage(C4Network2IRCMessageType enType, const char *szSource, const char *szTarget, const char *szData)
Definition: C4Network2IRC.h:37
bool ChangeNick(const char *szNewNick)
const char * getPrefix() const
Definition: C4Network2IRC.h:74
const char * getTarget() const
Definition: C4Network2IRC.h:53
const char * getData() const
Definition: C4Network2IRC.h:54
bool Message(const char *szTarget, const char *szText)
bool Close() override
Definition: C4NetIO.cpp:852
bool Quit(const char *szReason)
const char * getUserName() const
bool Join(const char *szChannel)
const int C4NetIRCMaxReadLogLength
Definition: C4Network2IRC.h:31
size_t getLength() const
Definition: StdBuf.h:445
~C4Network2IRCClient() override
C4Network2IRCMessage * getUnreadMessageLog() const
bool Notice(const char *szTarget, const char *szText)
C4Network2IRCMessage * getNext() const
Definition: C4Network2IRC.h:57
C4Network2IRCChannel(const char *szName)
const char * getSource() const
Definition: C4Network2IRC.h:52
CStdCSec * getCSec()
bool Connect(const addr_t &addr) override
Definition: C4NetIO.cpp:1149
bool IsActive() const