fixed possible crash in cmd_NeonServ.mod/cmd_neonserv_mode.c
[NeonServV5.git] / src / modules / NeonServ.mod / cmd_neonserv_upall.c
1 /* cmd_neonserv_upall.c - NeonServ v5.6
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
18 #include "cmd_neonserv.h"
19
20 /*
21 * no arguments
22 */
23
24 CMD_BIND(neonserv_cmd_upall) {
25     MYSQL_RES *res, *default_res;
26     MYSQL_ROW row, default_row;
27     struct ChanUser *chanuser;
28     int userid, chan_getop, chan_getvoice, caccess;
29     int botid = client->botid;
30     printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", user->auth);
31     res = mysql_use();
32     if ((row = mysql_fetch_row(res)) == NULL)
33         return;
34     userid = atoi(row[0]);
35     printf_mysql_query("SELECT `chanuser_access`, `channel_getop`, `channel_getvoice`, `channel_name`, `channel_id` FROM `chanusers` LEFT JOIN `channels` ON `chanuser_cid` = `channel_id` WHERE `chanuser_uid` = '%d'", userid);
36     res = mysql_use();
37     while ((row = mysql_fetch_row(res)) != NULL) {
38         chan = getChanByName(row[3]);
39         if(!chan) continue;
40         printf_mysql_query("SELECT `botid` FROM `bot_channels` LEFT JOIN `bots` ON `bot_channels`.`botid` = `bots`.`id` WHERE `chanid` = '%s' AND `botclass` = '%d'", row[4], client->botid);
41         if (mysql_fetch_row(mysql_use()) == NULL) continue;
42         if(!(chanuser = getChanUser(user, chan))) continue;
43         if(!row[1] || !row[2]) {
44             printf_mysql_query("SELECT `channel_getop`, `channel_getvoice` FROM `channels` WHERE `channel_name` = 'defaults'");
45             default_res = mysql_use();
46             if ((default_row = mysql_fetch_row(default_res)) == NULL) return;
47             chan_getop = (row[1] ? atoi(row[1]) : atoi(default_row[0]));
48             chan_getvoice = (row[2] ? atoi(row[2]) : atoi(default_row[1]));
49         } else {
50             chan_getop = atoi(row[1]);
51             chan_getvoice = atoi(row[2]);
52         }
53         caccess = atoi(row[0]);
54         int done = 0;
55         client = getChannelBot(chan, botid);
56         if(!client) continue;
57         if(caccess >= chan_getop) {
58             if(!(chanuser->flags & CHANUSERFLAG_OPPED)) {
59                 putsock(client, "MODE %s +o %s", chan->name, user->nick);
60                 done = 1;
61             }
62         } else if(caccess >= chan_getvoice) {
63             if(!(chanuser->flags & CHANUSERFLAG_VOICED)) {
64                 putsock(client, "MODE %s +v %s", chan->name, user->nick);
65                 done = 1;
66             }
67         }
68         if(done) {
69             event->chan = chan;
70             logEvent(event);
71         }
72     }
73 }