added cmd_netinfo
authorpk910 <philipp@zoelle1.de>
Fri, 9 Sep 2011 05:06:25 +0000 (07:06 +0200)
committerpk910 <philipp@zoelle1.de>
Fri, 9 Sep 2011 05:11:38 +0000 (07:11 +0200)
13 files changed:
ChanNode.c
ChanNode.h
ClientSocket.c
ClientSocket.h
UserNode.c
UserNode.h
bot_NeonServ.c
bot_NeonServ.h
bots.c
bots.h
cmd_neonserv_netinfo.c [new file with mode: 0644]
main.c
main.h

index 4568e63faf51d22a1873c976c80f50ee764c40de..6869a954fb7e9833ae82971a4a74c9a6e05b84e6 100644 (file)
@@ -60,7 +60,8 @@ void init_ChanNode() {
      ---------------------------
      = 47
     */
-    chanList = calloc(47, sizeof(*chanList));
+    #define CHANNEL_LIST_SIZE 47
+    chanList = calloc(CHANNEL_LIST_SIZE, sizeof(*chanList));
     unsigned int *mode, flag = 1;
     modes_with_strarg = 0;
     modes_with_intarg = 0;
@@ -83,7 +84,7 @@ void free_ChanNode() {
     int i;
     struct ChanNode *chan, *next;
     struct ChanUser *chanuser, *next_chanuser;
-    for(i = 0; i < 47; i++) {
+    for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
         for(chan = chanList[i]; chan; chan = next) {
             next = chan->next;
             for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next_chanuser) {
@@ -173,6 +174,28 @@ struct ChanNode* addChannel(const char *name) {
     return chan;
 }
 
+int getChannelCount() {
+    int i, count = 0;
+    struct ChanNode *chan;
+    for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
+        for(chan = chanList[i]; chan; chan = chan->next) {
+            count++;
+        }
+    }
+    return count;
+}
+
+int getChanUserCount() {
+    int i, count = 0;
+    struct ChanNode *chan;
+    for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
+        for(chan = chanList[i]; chan; chan = chan->next) {
+            count += chan->usercount;
+        }
+    }
+    return count;
+}
+
 void delChannel(struct ChanNode* chan, int freeChan) {
     int chanListIndex = get_chanlist_entry(chan->name[1]);
     if(chanListIndex == -1) return;
index 213f8aea193815e5ce844d7bdba4f09bdd9cf659..3bc007566809a9c20079f08ef752bb683aefb9e7 100644 (file)
@@ -32,6 +32,8 @@ void free_ChanNode();
 int is_valid_chan(const char *name);
 struct ChanNode* getChanByName(const char *name);
 struct ChanNode* addChannel(const char *chan);
+int getChannelCount();
+int getChanUserCount();
 void delChannel(struct ChanNode* chan, int freeChan);
 void freeChanNode(struct ChanNode* chan);
 void checkChannelVisibility(struct ChanNode* chan);
index 59f272a2fbd11a3fbe92db7825c24cc86f6ba0ec..62e367d7b9ce9ca4d21458211348aed287b675e9 100644 (file)
@@ -38,6 +38,9 @@ struct ClientSocket* create_socket(char *host, int port, char *pass, struct User
     client->user = user;
     client->flags = 0;
     client->bufferpos = 0;
+    client->traffic_in = 0;
+    client->traffic_out = 0;
+    client->connection_time = 0;
        client->botid = 0;
     client->clientid = 0;
     client->next = sockets->data;
@@ -78,6 +81,7 @@ int connect_socket(struct ClientSocket *client) {
 
     client->sock = sock;
     client->flags |= SOCKET_FLAG_CONNECTED;
+    client->connection_time = time(0);
 
     //send the IRC Headers
     char sendBuf[512];
@@ -120,6 +124,7 @@ int write_socket(struct ClientSocket *client, char* msg, int len) {
     if(!(client->flags & SOCKET_FLAG_CONNECTED)) return 0;
     printf("[send %d] %s", len, msg);
     write(client->sock, msg, len);
+    client->traffic_out += len;
     return 1;
 }
 
@@ -162,6 +167,7 @@ void socket_loop(int timeout_seconds) {
                 sock->flags &= ~(SOCKET_FLAG_CONNECTED | SOCKET_FLAG_READY);
                 bot_disconnect(sock);
             } else {
+                sock->traffic_in += bytes;
                 int used = parse_lines(sock, sock->buffer, sock->bufferpos);
                 if(used == sock->bufferpos + 1) {
                     //used all bytes so just reset the bufferpos
@@ -198,7 +204,7 @@ struct ClientSocket* getBots(int flags, struct ClientSocket* last_bot) {
     struct ClientSocket *sock = (last_bot ? last_bot->next : sockets->data);
     if(sock == NULL) return NULL;
     for (; sock; sock = sock->next) {
-        if((sock->flags & flags) == flags)
+        if(!flags || (sock->flags & flags) == flags)
             return sock;
     }
     return NULL;
index 494505b15451d9530f0d75184232e029c309123e..380e051951522ebaef2a5f602f23e9c67e52f493 100644 (file)
@@ -22,6 +22,9 @@ struct ClientSocket {
     int port;
     char *pass;
     struct UserNode *user;
+    unsigned long traffic_in;
+    unsigned long traffic_out;
+    time_t connection_time;
        
        int botid : 16;
     int clientid : 16;
index dccdad9d7a14579c958159af4225b100924de22d..2ced2eb5b88d3f3129173b9b867a0fbf3823d048 100644 (file)
@@ -143,6 +143,17 @@ struct UserNode* getAllUsers(struct UserNode *last) {
         return last->next;
 }
 
+int getUserCount() {
+    int i, count = 0;
+    struct UserNode *user;
+    for(i = 0; i < VALID_NICK_CHARS_FIRST_LEN+1; i++) {
+        for(user = userList[i]; user; user = user->next) {
+            count++;
+        }
+    }
+    return count;
+}
+
 struct UserNode* addUser(const char *nick) {
     int userListIndex = get_nicklist_entry(*nick);
     if(userListIndex == -1 || !is_valid_nick(nick))
index 0048398f4a38b63f354aea0e58cdfb1caa720569..f905437aae542a80341aafa035dafb29f4d625a4 100644 (file)
@@ -43,6 +43,7 @@ int countUsersWithHost(char *host);
 char *getAuthFakehost(char *auth);
 struct UserNode* searchUserByNick(const char *nick);
 struct UserNode* getAllUsers(struct UserNode *last);
+int getUserCount();
 struct UserNode* addUser(const char *nick);
 struct UserNode* addUserMask(const char *mask);
 struct UserNode* createTempUser(const char *mask);
index 5305913595233ceeff03b77f85410c4fa6a77ce7..fae9f0e588cdd372b5468abb347c380f41d4af41 100644 (file)
@@ -130,6 +130,13 @@ static const struct default_language_entry msgtab[] = {
     {"NS_DELBAN_BANNED_BY", "%s is banned by %s."},
     {"NS_DELBAN_FAIL", "Sorry, no ban found for \002%s\002."},
     {"NS_DELBAN_DONE", "Removed \002%s\002 from the %s ban list."},
+    {"NS_NETINFO_HEADER", "\002Network information\002"},
+    {"NS_NETINFO_UPTIME", "Uptime:"},
+    {"NS_NETINFO_TRAFFIC", "Traffic:"},
+    {"NS_NETINFO_CACHE", "Cache:"},
+    {"NS_NETINFO_CHANNEL", "  Channel:"},
+    {"NS_NETINFO_USER", "  User:"},
+    {"NS_NETINFO_CHANUSER", "  Channel-User:"},
     {NULL, NULL}
 };
 
@@ -178,7 +185,7 @@ INCLUDE ALL CMD's HERE
 //#include "cmd_neonserv_mode.c"
 //#include "cmd_neonserv_invite.c"
 //#include "cmd_neonserv_info.c"
-//#include "cmd_neonserv_netinfo.c"
+#include "cmd_neonserv_netinfo.c"
 //#include "cmd_neonserv_peek.c"
 #include "cmd_neonserv_set.c" /* TODO: parse, check and set modelock */
 //#include "cmd_neonserv_events.c"
@@ -314,6 +321,7 @@ void init_NeonServ() {
     register_command(BOTID, "addban",       neonserv_cmd_addban,    1, CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH,  "#channel_staticban",   0);
     register_command(BOTID, "bans",         neonserv_cmd_bans,      0, CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH,  "1",                    0);
     register_command(BOTID, "delban",       neonserv_cmd_delban,    1, CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH,  "#channel_staticban",   0);
+    register_command(BOTID, "netinfo",      neonserv_cmd_netinfo,   0, 0,                                                                                           NULL,                   0);
     
     register_command(BOTID, "trace",        neonserv_cmd_trace,     1, CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH,                                                   NULL,                   400);
     
@@ -325,6 +333,20 @@ void init_NeonServ() {
     register_default_language_table(msgtab);
 }
 
+void loop_NeonServ() {
+    //check channel bans
+    check_mysql();
+    MYSQL_RES *res;
+    MYSQL_ROW row;
+    printf_mysql_query("SELECT `ban_mask`, `ban_id` FROM `bans` WHERE `ban_expire` != '0' AND `ban_expire` <= UNIX_TIMESTAMP()");
+    res = mysql_use();
+    while ((row = mysql_fetch_row(res)) != NULL) {
+        printf_mysql_query("DELETE FROM `bans` WHERE `ban_id` = '%s'", row[1]);
+        //TODO: remove the ban from the channel
+    }
+    mysql_free();
+}
+
 void free_NeonServ() {
     
 }
index a42a2d5aa1a5eab27a09ad11a4de3a3a8e95f47b..8e33e2312da5eee05f3cc89a4ce181448599b66a 100644 (file)
@@ -4,6 +4,7 @@
 #include "main.h"
 
 void init_NeonServ();
+void loop_NeonServ();
 void free_NeonServ();
 
 #endif
\ No newline at end of file
diff --git a/bots.c b/bots.c
index a5c82bf99a5af4880725189ab4cd6053d2dda71c..b957a194c1261921feb7bae78af34358e1d8587f 100644 (file)
--- a/bots.c
+++ b/bots.c
@@ -7,6 +7,10 @@ void init_bots() {
     init_NeonServ();
 }
 
+void loop_bots() {
+    loop_NeonServ();
+}
+
 void free_bots() {
     free_NeonServ();
 }
diff --git a/bots.h b/bots.h
index 38371e42c2cb90cce142bbb60d6a0ac82e8d4e6c..a24b158603090e5ee64491a291cc2a5d6926ebc9 100644 (file)
--- a/bots.h
+++ b/bots.h
@@ -4,6 +4,7 @@
 #include "main.h"
 
 void init_bots();
+void loop_bots();
 void free_bots();
 
 #endif
\ No newline at end of file
diff --git a/cmd_neonserv_netinfo.c b/cmd_neonserv_netinfo.c
new file mode 100644 (file)
index 0000000..baf926e
--- /dev/null
@@ -0,0 +1,70 @@
+
+/*
+* no args
+*/
+static CMD_BIND(neonserv_cmd_netinfo) {
+    reply(getTextBot(), user, "NS_NETINFO_HEADER");
+    char tmp[MAXLEN];
+    struct Table *table;
+    table = table_init(2, 5, 0);
+    char *content[2];
+    
+    content[0] = get_language_string(user, "NS_NETINFO_UPTIME");
+    content[1] = timeToStr(user, (time(0) - start_time), 3, tmp);
+    table_add(table, content);
+    
+    content[0] = get_language_string(user, "NS_NETINFO_BOTS");
+    struct ClientSocket *cclient;
+    int bot_count = 0, connected_bot_count = 0;
+    unsigned long traffic_in = 0, traffic_out = 0;
+    for(cclient = getBots(0, NULL); cclient; cclient = getBots(0, cclient)) {
+        bot_count++;
+        if(cclient->flags & SOCKET_FLAG_READY)
+            connected_bot_count++;
+        traffic_in += cclient->traffic_in;
+        traffic_out += cclient->traffic_out;
+    }
+    sprintf(tmp, "%d (%d connected)", bot_count, connected_bot_count);
+    content[1] = tmp;
+    table_add(table, content);
+    
+    content[0] = get_language_string(user, "NS_NETINFO_TRAFFIC");
+    sprintf(tmp, "in: %lu kb  out: %lu kb", traffic_in / 1024, traffic_out / 1024);
+    content[1] = tmp;
+    table_add(table, content);
+    
+    int channel_count = getChannelCount();
+    float channel_memory = channel_count * sizeof(struct ChanNode);
+    int user_count = getUserCount();
+    float user_memory = user_count * sizeof(struct UserNode);
+    int chanuser_count = getChanUserCount();
+    float chanuser_memory = chanuser_count * sizeof(struct ChanUser);
+    float total_memory = channel_memory + user_memory + chanuser_memory;
+    
+    content[0] = get_language_string(user, "NS_NETINFO_CACHE");
+    sprintf(tmp, "%f kB (%f MB)", total_memory / 1024, total_memory / 1024 / 1024);
+    content[1] = tmp;
+    table_add(table, content);
+    
+    content[0] = get_language_string(user, "NS_NETINFO_CHANNEL");
+    sprintf(tmp, "%d    %f kB (%d * %lu B = %f kB)", channel_count, channel_memory / 1024, channel_count, sizeof(struct ChanNode), channel_memory / 1024);
+    content[1] = tmp;
+    table_add(table, content);
+    
+    content[0] = get_language_string(user, "NS_NETINFO_USER");
+    sprintf(tmp, "%d    %f kB (%d * %lu B = %f kB)", user_count, user_memory / 1024, user_count, sizeof(struct UserNode), user_memory / 1024);
+    content[1] = tmp;
+    table_add(table, content);
+    
+    content[0] = get_language_string(user, "NS_NETINFO_CHANUSER");
+    sprintf(tmp, "%d    %f kB (%d * %lu B = %f kB)", chanuser_count, chanuser_memory / 1024, chanuser_count, sizeof(struct ChanUser), chanuser_memory / 1024);
+    content[1] = tmp;
+    table_add(table, content);
+    
+    char **table_lines = table_end(table);
+    int i;
+    for(i = 0; i < table->entrys; i++) {
+        reply(getTextBot(), user, table_lines[i]);
+    }
+    table_free(table);
+}
diff --git a/main.c b/main.c
index bcf2fb22a2112baeb9887c9aa67d37aacad1edcd..c6814a15ef1897daa094cbc7ec3aa34b172529ad 100644 (file)
--- a/main.c
+++ b/main.c
@@ -14,6 +14,8 @@
 #include "tools.h"
 #include "timeq.h"
 
+time_t start_time;
+
 void cleanup() {
     free_sockets();
     free_parser();
@@ -30,6 +32,8 @@ void cleanup() {
 
 int main(void)
 {
+    start_time = time(0);
+    
     init_mysql();
     init_lang();
     init_parser();
@@ -48,6 +52,7 @@ int main(void)
             socket_loop(SOCKET_SELECT_TIME);
         } while(time(0) < socket_wait);
         timeq_tick();
+        loop_bots();
         clearTempUsers();
     }
 }
diff --git a/main.h b/main.h
index e355d2375fad94349cae3030d24bde2bb93d2584..bf72d283d5dd345a76771d234ba4045f5ec60a5e 100644 (file)
--- a/main.h
+++ b/main.h
@@ -56,6 +56,8 @@
 
 #define TEMPUSER_LIST_INDEX VALID_NICK_CHARS_FIRST_LEN
 
+extern time_t start_time;
+
 int stricmp (const char *s1, const char *s2);
 int stricmplen (const char *s1, const char *s2, int len);