DaZeus  2.0
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
network.h
Go to the documentation of this file.
1 
6 #ifndef NETWORK_H
7 #define NETWORK_H
8 
9 #include <vector>
10 #include <sys/select.h> /* fd_set */
11 #include <string>
12 #include <map>
13 
14 namespace dazeus {
15 
16 class Network;
17 class Server;
18 
19 struct ServerConfig;
20 
21 struct NetworkConfig {
22  NetworkConfig(std::string n = std::string(), std::string d = std::string(),
23  std::string nick = std::string("DaZeus"), std::string user = std::string("dazeus"),
24  std::string full = std::string("DaZeus"), std::string p = std::string(), bool a =
25  false, time_t ct = 10, time_t pt = 30) : name(n), displayName(d), nickName(nick), userName(user),
26  fullName(full), password(p), servers(), autoConnect(a), connectTimeout(ct),
27  pongTimeout(pt) {}
33  const NetworkConfig &operator=(const NetworkConfig &s) { name = s.name;
37  pongTimeout = s.pongTimeout; return *this; }
38 
39  std::string name;
40  std::string displayName;
41  std::string nickName;
42  std::string userName;
43  std::string fullName;
44  std::string password;
45  std::vector<ServerConfig*> servers;
48  time_t pongTimeout;
49 };
50 
52 {
53  public:
54  virtual ~NetworkListener() {}
55  virtual void ircEvent(const std::string &event, const std::string &origin,
56  const std::vector<std::string> &params, Network *n ) = 0;
57 };
58 
59 class Network
60 {
61 
62  friend class Server;
63 
64  public:
65  Network( const NetworkConfig *c );
66  ~Network();
67 
68  static std::string toString(const Network *n);
70  networkListeners_.push_back(nl);
71  }
72 
81  };
82 
83  enum ChannelMode {
84  UserMode = 0,
85  OpMode = 1,
87  VoiceMode = 4,
90  };
91 
92  bool autoConnectEnabled() const;
93  const std::vector<ServerConfig*> &servers() const;
94  std::string nick() const;
95  Server *activeServer() const;
96  const NetworkConfig *config() const;
97  int serverUndesirability( const ServerConfig *sc ) const;
98  std::string networkName() const;
99  std::vector<std::string> joinedChannels() const;
100  std::map<std::string,std::string> topics() const;
101  std::map<std::string,ChannelMode> usersInChannel(std::string channel) const;
102  bool isIdentified(const std::string &user) const;
103  bool isKnownUser(const std::string &user) const;
104 
105  void connectToNetwork( bool reconnect = false );
107  void checkTimeouts();
108  void joinChannel( std::string channel );
109  void leaveChannel( std::string channel );
110  void say( std::string destination, std::string message );
111  void action( std::string destination, std::string message );
112  void names( std::string channel );
113  void ctcp( std::string destination, std::string message );
114  void sendWhois( std::string destination );
115  void flagUndesirableServer( const ServerConfig *sc );
116  void serverIsActuallyOkay( const ServerConfig *sc );
117  void addDescriptors(fd_set *in_set, fd_set *out_set, int *maxfd);
118  void processDescriptors(fd_set *in_set, fd_set *out_set);
119  void run();
120  static void run(std::vector<Network*> networks);
121 
122  private:
123  // explicitly disable copy constructor
124  Network(const Network&);
125  void operator=(const Network&);
126 
127  void connectToServer( ServerConfig *conf, bool reconnect );
128 
129  Server *activeServer_;
130  const NetworkConfig *config_;
131  std::map<const ServerConfig*,int> undesirables_;
132  bool deleteServer_;
133  std::vector<std::string> identifiedUsers_;
134  std::map<std::string,std::vector<std::string> > knownUsers_;
135  std::map<std::string,std::string> topics_;
136  std::vector<NetworkListener*> networkListeners_;
137  std::string nick_;
138  time_t deadline_;
139  time_t nextPongDeadline_;
140 
141  void onFailedConnection();
142  void joinedChannel(const std::string &user, const std::string &receiver);
143  void kickedChannel(const std::string &user, const std::string&, const std::string&, const std::string &receiver);
144  void partedChannel(const std::string &user, const std::string &, const std::string &receiver);
145  void slotQuit(const std::string &origin, const std::string&, const std::string &receiver);
146  void slotWhoisReceived(const std::string &origin, const std::string &nick, bool identified);
147  void slotNickChanged( const std::string &origin, const std::string &nick, const std::string &receiver );
148  void slotNamesReceived(const std::string&, const std::string&, const std::vector<std::string> &names, const std::string &receiver );
149  void slotTopicChanged(const std::string&, const std::string&, const std::string&);
150  void slotIrcEvent(const std::string&, const std::string&, const std::vector<std::string>&);
151 };
152 
153 }
154 
155 #endif