anope

- supernets anope source code & configuration
git clone git://git.acid.vegas/anope.git
Log | Files | Refs | Archive | README

os_session.h (2547B)

      1 /*
      2  *
      3  * (C) 2011-2022 Anope Team
      4  * Contact us at team@anope.org
      5  *
      6  * Please read COPYING and README for further details.
      7  */
      8 
      9 #ifndef OS_SESSION_H
     10 #define OS_SESSION_H
     11 
     12 struct Session
     13 {
     14 	cidr addr;                      /* A cidr (sockaddrs + len) representing this session */
     15 	unsigned count;                 /* Number of clients with this host */
     16 	unsigned hits;                  /* Number of subsequent kills for a host */
     17 
     18 	Session(const sockaddrs &ip, int len) : addr(ip, len), count(1), hits(0) { }
     19 };
     20 
     21 struct Exception : Serializable
     22 {
     23 	Anope::string mask;		/* Hosts to which this exception applies */
     24 	unsigned limit;			/* Session limit for exception */
     25 	Anope::string who;		/* Nick of person who added the exception */
     26 	Anope::string reason;		/* Reason for exception's addition */
     27 	time_t time;			/* When this exception was added */
     28 	time_t expires;			/* Time when it expires. 0 == no expiry */
     29 
     30 	Exception() : Serializable("Exception") { }
     31 	void Serialize(Serialize::Data &data) const anope_override;
     32 	static Serializable* Unserialize(Serializable *obj, Serialize::Data &data);
     33 };
     34 
     35 class SessionService : public Service
     36 {
     37  public:
     38 	typedef TR1NS::unordered_map<cidr, Session *, cidr::hash> SessionMap;
     39 	typedef std::vector<Exception *> ExceptionVector;
     40 
     41 	SessionService(Module *m) : Service(m, "SessionService", "session") { }
     42 
     43 	virtual Exception *CreateException() = 0;
     44 
     45 	virtual void AddException(Exception *e) = 0;
     46 
     47 	virtual void DelException(Exception *e) = 0;
     48 
     49 	virtual Exception *FindException(User *u) = 0;
     50 
     51 	virtual Exception *FindException(const Anope::string &host) = 0;
     52 
     53 	virtual ExceptionVector &GetExceptions() = 0;
     54 
     55 	virtual Session *FindSession(const Anope::string &ip) = 0;
     56 
     57 	virtual SessionMap &GetSessions() = 0;
     58 };
     59 
     60 static ServiceReference<SessionService> session_service("SessionService", "session");
     61 
     62 void Exception::Serialize(Serialize::Data &data) const
     63 {
     64 	data["mask"] << this->mask;
     65 	data["limit"] << this->limit;
     66 	data["who"] << this->who;
     67 	data["reason"] << this->reason;
     68 	data["time"] << this->time;
     69 	data["expires"] << this->expires;
     70 }
     71 
     72 Serializable* Exception::Unserialize(Serializable *obj, Serialize::Data &data)
     73 {
     74 	if (!session_service)
     75 		return NULL;
     76 
     77 	Exception *ex;
     78 	if (obj)
     79 		ex = anope_dynamic_static_cast<Exception *>(obj);
     80 	else
     81 		ex = new Exception;
     82 	data["mask"] >> ex->mask;
     83 	data["limit"] >> ex->limit;
     84 	data["who"] >> ex->who;
     85 	data["reason"] >> ex->reason;
     86 	data["time"] >> ex->time;
     87 	data["expires"] >> ex->expires;
     88 
     89 	if (!obj)
     90 		session_service->AddException(ex);
     91 	return ex;
     92 }
     93 
     94 #endif