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 #include <time.h>
21 
23 {
29 };
30 
31 const int C4NetIRCMaxLogLength = 300000; // Maximum total length of log - if log gets longer, it will be cleared even if messages have never been read
32 const int C4NetIRCMaxReadLogLength = 1000; // Maximum log messages kept even after they've been read
33 
35 {
36  friend class C4Network2IRCClient;
37 public:
38  C4Network2IRCMessage(C4Network2IRCMessageType enType, const char *szSource, const char *szTarget, const char *szData)
39  : iTimestamp(time(nullptr)), eType(enType), Source(szSource), Target(szTarget), Data(szData), Next(0)
40  { }
41 
42 private:
43  time_t iTimestamp;
45  StdCopyStrBuf Source, Target, Data;
46 
47  // used by C4Network2IRCClient
49 
50 public:
51  time_t getTimestamp() const { return iTimestamp; }
52  C4Network2IRCMessageType getType() const { return eType; }
53  const char *getSource() const { return Source.getData(); }
54  const char *getTarget() const { return Target.getData(); }
55  const char *getData() const { return Data.getData(); }
56  bool isChannel() const { return Target.getLength() && (*Target.getData() == '#' || *Target.getData() == '+'); }
57 
58  C4Network2IRCMessage *getNext() const { return Next; }
59 };
60 
62 {
63  friend class C4Network2IRCChannel;
64 public:
65  C4Network2IRCUser(const char *szName);
66 
67 private:
68  StdCopyStrBuf Prefix;
69  StdCopyStrBuf Name;
70 
71  // used by C4Network2IRCChannel
72  C4Network2IRCUser *Next;
73 
74 public:
75  const char *getPrefix() const { return Prefix.getData(); }
76  const char *getName() const { return Name.getData(); }
77  C4Network2IRCUser *getNext() const { return Next; }
78 
79 private:
80  // called by C4Network2IRCChannel
81  void SetPrefix(const char *szPrefix) { Prefix = szPrefix; }
82 
83 };
84 
86 {
87  friend class C4Network2IRCClient;
88 public:
89  C4Network2IRCChannel(const char *szName);
91 
92 private:
93  StdCopyStrBuf Name;
94  StdCopyStrBuf Topic;
95  C4Network2IRCUser *pUsers;
96 
97  bool fReceivingUsers;
98 
99  // used by C4Network2IRCClient
100  C4Network2IRCChannel *Next;
101 
102 public:
103  const char *getName() const { return Name.getData(); }
104  const char *getTopic() const { return Topic.getData(); }
105  C4Network2IRCUser *getUsers() const { return pUsers; }
106  C4Network2IRCUser *getUser(const char *szName) const;
107  bool isUsersLocked() const { return fReceivingUsers; }
108 
109 private:
110  // called by C4Network2IRCClient
111  void OnUsers(const char *szUsers, const char *szPrefixes);
112  void OnUsersEnd();
113  void OnJoin(const char *szUser);
114  void OnPart(const char *szUser, const char *szComment);
115  void OnKick(const char *szUser, const char *szComment);
116  void OnTopic(const char *szTopic);
117 
118  C4Network2IRCUser *AddUser(const char *szName);
119  void DeleteUser(C4Network2IRCUser *pUser);
120  void ClearUsers();
121 };
122 
124 {
125 public:
128 
129 private:
130 
131  // Connection information
132  C4NetIO::addr_t ServerAddr, PeerAddr;
133  bool fConnecting, fConnected;
134 
135  // Status information
136  StdCopyStrBuf Nick, RealName, Password;
137  StdCopyStrBuf AutoJoin;
138  C4Network2IRCChannel *pChannels;
139 
140  // User mode/prefix map
141  StdCopyStrBuf Prefixes;
142 
143  // Message log
144  C4Network2IRCMessage *pLog, *pLogLastRead, *pLogEnd;
145  int32_t iLogLength, iUnreadLogLength;
146 
147  // Event queue for notify
148  class C4InteractiveThread *pNotify;
149 
150  // Critical section for data
151  CStdCSec CSec;
152 
153 private:
154 
155  // Overridden
156  virtual void PackPacket(const C4NetIOPacket &rPacket, StdBuf &rOutBuf);
157  virtual size_t UnpackPacket(const StdBuf &rInBuf, const C4NetIO::addr_t &addr);
158 
159  // Callbacks
160  bool OnConn(const C4NetIO::addr_t &AddrPeer, const C4NetIO::addr_t &AddrConnect, const addr_t *pOwnAddr, C4NetIO *pNetIO);
161  void OnDisconn(const C4NetIO::addr_t &AddrPeer, C4NetIO *pNetIO, const char *szReason);
162  void OnPacket(const class C4NetIOPacket &rPacket, C4NetIO *pNetIO);
163 
164 public:
165 
166  CStdCSec *getCSec() { return &CSec; }
167 
168  // only call these while holding a lock on the above critical section!
171  C4Network2IRCChannel *getChannel(const char *szName) const;
172  C4Network2IRCMessage *getMessageLog() const { return pLog; }
173  C4Network2IRCMessage *getUnreadMessageLog() const { return pLogLastRead ? pLogLastRead->getNext() : pLog; }
174  void ClearMessageLog();
175  void MarkMessageLogRead();
176  const char *getUserName() const { return Nick.getData(); }
177 
178  using C4NetIOTCP::Connect;
179  // Simple network communication
180  bool Connect(const char *szServer, const char *szNick, const char *szRealName, const char *szPassword = nullptr, const char *szChannel = nullptr);
181  using C4NetIOTCP::Close;
182  bool Close();
183  using C4NetIOTCP::Send;
184  bool Send(const char *szCommand, const char *szParameters = nullptr);
185 
186  // Notfiy interface
187  void SetNotify(class C4InteractiveThread *pnNotify) { pNotify = pnNotify; }
188 
189  // Special IRC commands
190  bool Quit(const char *szReason);
191  bool Join(const char *szChannel);
192  bool Part(const char *szChannel);
193  bool Message(const char *szTarget, const char *szText);
194  bool Notice(const char *szTarget, const char *szText);
195  bool Action(const char *szTarget, const char *szText);
196  bool ChangeNick(const char *szNewNick);
197  bool RegisterNick(const char *szPassword, const char *szMail);
198 
199  // Status
200  bool IsActive() const { return fConnecting || fConnected; }
201  bool IsConnected() const { return fConnected; }
202 
203 private:
204  void OnCommand(const char *szSender, const char *szCommand, const char *szParameters);
205  void OnNumericCommand(const char *szSender, int iCommand, const char *szParameters);
206  void OnConnected();
207  void OnMessage(bool fNotice, const char *szSource, const char *szTarget, const char *szText);
208 
209  void PopMessage();
210  void PushMessage(C4Network2IRCMessageType eType, const char *szSource, const char *szTarget, const char *szText);
211 
212  C4Network2IRCChannel *AddChannel(const char *szName);
213  void DeleteChannel(C4Network2IRCChannel *pChannel);
214 };
215 
216 #endif // C4NETWORK2IRC_H
const char * getData() const
Definition: StdBuf.h:450
C4Network2IRCMessageType getType() const
Definition: C4Network2IRC.h:52
C4Network2IRCUser * getUser(const char *szName) const
const char * getName() const
Definition: C4Network2IRC.h:76
Definition: StdBuf.h:37
bool Send(const char *szCommand, const char *szParameters=nullptr)
C4Network2IRCUser * getUsers() const
const int C4NetIRCMaxLogLength
Definition: C4Network2IRC.h:31
bool RegisterNick(const char *szPassword, const char *szMail)
C4Network2IRCMessageType
Definition: C4Network2IRC.h:22
virtual bool Connect(const addr_t &addr)
Definition: C4NetIO.cpp:1167
void SetNotify(class C4InteractiveThread *pnNotify)
bool isChannel() const
Definition: C4Network2IRC.h:56
bool Action(const char *szTarget, const char *szText)
bool IsConnected() const
C4Network2IRCChannel * getNextChannel(C4Network2IRCChannel *pPrevChan) const
C4Network2IRCChannel * getChannel(const char *szName) const
time_t getTimestamp() const
Definition: C4Network2IRC.h:51
C4Network2IRCMessage * getMessageLog() const
const char * getName() const
C4Network2IRCChannel * getFirstChannel() const
C4Network2IRCUser * getNext() const
Definition: C4Network2IRC.h:77
bool Connect(const char *szServer, const char *szNick, const char *szRealName, const char *szPassword=nullptr, const char *szChannel=nullptr)
C4Network2IRCUser(const char *szName)
const char * getTopic() const
bool isUsersLocked() const
bool Part(const char *szChannel)
virtual bool Close()
Definition: C4NetIO.cpp:870
C4Network2IRCMessage(C4Network2IRCMessageType enType, const char *szSource, const char *szTarget, const char *szData)
Definition: C4Network2IRC.h:38
bool ChangeNick(const char *szNewNick)
const char * getPrefix() const
Definition: C4Network2IRC.h:75
const char * getTarget() const
Definition: C4Network2IRC.h:54
const char * getData() const
Definition: C4Network2IRC.h:55
bool Message(const char *szTarget, const char *szText)
bool Quit(const char *szReason)
const char * getUserName() const
virtual bool Send(const C4NetIOPacket &rPacket)
Definition: C4NetIO.cpp:1266
bool Join(const char *szChannel)
const int C4NetIRCMaxReadLogLength
Definition: C4Network2IRC.h:32
size_t getLength() const
Definition: StdBuf.h:453
C4Network2IRCMessage * getUnreadMessageLog() const
bool Notice(const char *szTarget, const char *szText)
C4Network2IRCMessage * getNext() const
Definition: C4Network2IRC.h:58
C4Network2IRCChannel(const char *szName)
const char * getSource() const
Definition: C4Network2IRC.h:53
CStdCSec * getCSec()
bool IsActive() const