*** VERSION 5.5.0 ***
[NeonServV5.git] / src / modules / stats.mod / module.c
1 /* module.c - NeonServ v5.5
2  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17 #include "../module.h"
18 #include "../../timeq.h"
19 #include "../../ConfigParser.h"
20 #include "../../mysqlConn.h"
21 #include "../../ClientSocket.h"
22 #include "../../UserNode.h"
23
24 #define STATS_UPDATE_SECONDS  1800
25 #define STATS_UPDATE_HOST     "neonserv.krypton-bouncer.de"
26 #define STATS_UPDATE_PORT     1675
27 #define STATS_IDENTIFIER_LEN  10
28 #define STATS_IDENTIFIER_POOL "abcdefghijklmnopqrstuvwxyz0123456789#*+-_.:;,!ยง$%&/()=?[]{}<>|@"
29 #define STATS_IDENTIFIER_POOL_SIZE 63
30
31 static TIMEQ_CALLBACK(stats_timer_callback);
32
33 static int module_initialize() {
34     return 0;
35 }
36
37 static void module_start(int type) {
38     if(!timeq_name_exists("stats"))
39         timeq_add_name("stats", 60, module_id, stats_timer_callback, NULL);
40 }
41
42 static void module_loop() {
43     
44 }
45
46 static void module_stop(int type) {
47     timeq_del_name("stats");
48 }
49
50 static char *get_identifier() {
51     MYSQL_RES *res;
52     MYSQL_ROW row;
53     printf_mysql_query("SELECT `value` FROM `settings` WHERE `name` = 'identifier'");
54     res = mysql_use();
55     if(!(row = mysql_fetch_row(res))) {
56         srand(time(NULL));
57         char identifier[STATS_IDENTIFIER_LEN+1];
58         int i;
59         char *pool = STATS_IDENTIFIER_POOL;
60         for(i = 0; i < STATS_IDENTIFIER_LEN; i++) {
61             identifier[i] = pool[(rand() % STATS_IDENTIFIER_POOL_SIZE)];
62         }
63         identifier[i] = 0;
64         printf_mysql_query("INSERT INTO `settings` (`name`, `value`) VALUES ('identifier', '%s')", escape_string(identifier));
65         printf_mysql_query("SELECT `value` FROM `settings` WHERE `name` = 'identifier'");
66         res = mysql_use();
67         row = mysql_fetch_row(res);
68         if(!row) return NULL;
69     }
70     if(strlen(row[0]) < 10) return NULL;
71     return row[0];
72 }
73
74 static TIMEQ_CALLBACK(stats_timer_callback) {
75     timeq_add_name("stats", STATS_UPDATE_SECONDS, module_id, stats_timer_callback, NULL);
76     char tmp[200];
77     char pkgbuf[1024];
78     int pkgpos = 0;
79     char *modname = get_module_name(module_id);
80     char *bot_identifier = get_identifier();
81     if(!bot_identifier) return;
82     //build update package
83     pkgpos += sprintf(pkgbuf + pkgpos, "nsupdate\n%s\n%s.%d %s\n", bot_identifier, NEONSERV_VERSION, get_patchlevel(), (strcmp(get_revision(), "") ? get_revision() : "-"));
84     sprintf(tmp, "modules/%s/hide_networkname", modname);
85     if(get_int_field(tmp))
86         pkgpos += sprintf(pkgbuf + pkgpos, "*\n");
87     else {
88         struct ClientSocket *bot;
89         for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
90             if(bot->network_name) break;
91         }
92         if(bot)
93             pkgpos += sprintf(pkgbuf + pkgpos, "%s\n", bot->network_name);
94         else
95             pkgpos += sprintf(pkgbuf + pkgpos, "?\n");
96     }
97     sprintf(tmp, "modules/%s/hide_botnick", modname);
98     if(get_int_field(tmp))
99         pkgpos += sprintf(pkgbuf + pkgpos, "*\n");
100     else {
101         struct ClientSocket *bot, *bot1, *bot2, *bot3;
102         for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
103             if(bot->botid == 1 && (bot->flags & SOCKET_FLAG_PREFERRED))
104                 bot1 = bot;
105             else if((bot->flags & SOCKET_FLAG_PREFERRED))
106                 bot2 = bot;
107             else
108                 bot3 = bot;
109         }
110         if(bot1)
111             bot = bot1;
112         else if(bot2)
113             bot = bot2;
114         else
115             bot = bot3;
116         if(bot) {
117             pkgpos += sprintf(pkgbuf + pkgpos, "%s!%s@%s %d\n", (bot->user ? bot->user->nick : "*"), (bot->user ? bot->user->ident : "*"), (bot->host ? bot->host : "*"), bot->port);
118         } else
119             pkgpos += sprintf(pkgbuf + pkgpos, "?\n");
120     }
121     sprintf(tmp, "modules/%s/hide_chancount", modname);
122     if(get_int_field(tmp))
123         pkgpos += sprintf(pkgbuf + pkgpos, "*\n");
124     else {
125         int channel_count = getChannelCount();
126         pkgpos += sprintf(pkgbuf + pkgpos, "%d\n", channel_count);
127     }
128     sprintf(tmp, "modules/%s/hide_usercount", modname);
129     if(get_int_field(tmp))
130         pkgpos += sprintf(pkgbuf + pkgpos, "*\n");
131     else {
132         int user_count = getUserCount();
133         int chanuser_count = getChanUserCount();
134         pkgpos += sprintf(pkgbuf + pkgpos, "%d %d\n", user_count, chanuser_count);
135     }
136     pkgbuf[pkgpos] = 0;
137     //send package
138     #ifndef WIN32
139     struct sockaddr_in si_other;
140     int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
141     if (sock == -1) return;
142     memset((char *) &si_other, 0, sizeof(si_other));
143     si_other.sin_family = AF_INET;
144     si_other.sin_port = htons(STATS_UPDATE_PORT);
145     if (!inet_aton(STATS_UPDATE_HOST, &si_other.sin_addr)) {
146         struct hostent *host = gethostbyname(STATS_UPDATE_HOST);
147         if (!host) return;
148         si_other.sin_addr = *(struct in_addr*)host->h_addr;
149     }
150     #else
151     struct sockaddr_in si_other;
152     int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
153     if (sock == -1) return;
154     si_other.sin_family = AF_INET;
155     si_other.sin_port = htons(STATS_UPDATE_PORT);
156     si_other.sin_addr.s_addr = inet_addr(client->host);
157     if (si_other.sin_addr.s_addr == INADDR_NONE) {
158         struct hostent *host = gethostbyname(client->host);
159         if(!host) return;
160         memcpy(&(si_other.sin_addr), host->h_addr_list[0], 4);
161     }
162     #endif
163     sendto(sock, pkgbuf, pkgpos, 0, &si_other, sizeof(si_other));
164     close(sock);
165 }
166
167 MODULE_HEADER(module_initialize, module_start, module_loop, module_stop);