8a531f00d0e5f15399974a5b64cf01dd088d8185
[NeonServV5.git] / src / modules / NeonServ.mod / cmd_neonserv_up.c
1 /* cmd_neonserv_up.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 struct neonserv_cmd_up_cache {
25     struct ClientSocket *client;
26     struct ClientSocket *textclient;
27     struct UserNode *user;
28     struct Event *event;
29 };
30
31 static void neonserv_cmd_up_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event);
32 static USERLIST_CALLBACK(neonserv_cmd_up_userlist_lookup);
33
34 CMD_BIND(neonserv_cmd_up) {
35     if(isModeSet(chan->modes, 'd') || isModeSet(chan->modes, 'D')) {
36         struct neonserv_cmd_up_cache *cache = malloc(sizeof(*cache));
37         if (!cache) {
38             perror("malloc() failed");
39             return;
40         }
41         cache->client = client;
42         cache->textclient = textclient;
43         cache->user = user;
44         cache->event = event;
45         get_userlist_if_invisible(chan, module_id, neonserv_cmd_up_userlist_lookup, cache);
46     } else {
47         neonserv_cmd_up_async1(client, textclient, user, chan, event);
48     }
49 }
50
51 static USERLIST_CALLBACK(neonserv_cmd_up_userlist_lookup) {
52     struct neonserv_cmd_up_cache *cache = data;
53     neonserv_cmd_up_async1(cache->client, cache->textclient, cache->user, chan, cache->event);
54     free(cache);
55 }
56
57 static void neonserv_cmd_up_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event) {
58     struct ChanUser *chanuser = getChanUser(user, chan);
59     if(!chanuser) {
60         reply(textclient, user, "NS_NOT_ON_CHANNEL_YOU", chan->name);
61         return;
62     }
63     loadChannelSettings(chan);
64     MYSQL_RES *res, *default_res;
65     MYSQL_ROW row, default_row;
66     int chan_getop, chan_getvoice, caccess;
67     printf_mysql_query("SELECT `channel_getop`, `channel_getvoice` FROM `channels` WHERE `channel_id` = '%d'", chan->channel_id);
68     res = mysql_use();
69     if ((row = mysql_fetch_row(res)) == NULL) return;
70     if(!row[0] || !row[1]) {
71         printf_mysql_query("SELECT `channel_getop`, `channel_getvoice` FROM `channels` WHERE `channel_name` = 'defaults'");
72         default_res = mysql_use();
73         if ((default_row = mysql_fetch_row(default_res)) == NULL) return;
74         chan_getop = (row[0] ? atoi(row[0]) : atoi(default_row[0]));
75         chan_getvoice = (row[1] ? atoi(row[1]) : atoi(default_row[1]));
76     } else {
77         chan_getop = atoi(row[0]);
78         chan_getvoice = atoi(row[1]);
79     }
80     caccess = getChannelAccess(user, chan);
81     if(caccess >= chan_getop) {
82         if(!(chanuser->flags & CHANUSERFLAG_OPPED)) {
83             putsock(client, "MODE %s +o %s", chan->name, user->nick);
84             logEvent(event);
85         } else
86             reply(textclient, user, "NS_UP_ALREADY_OP", chan->name);
87     } else if(caccess >= chan_getvoice) {
88         if(!(chanuser->flags & CHANUSERFLAG_VOICED)) {
89             putsock(client, "MODE %s +v %s", chan->name, user->nick);
90             logEvent(event);
91         } else
92             reply(textclient, user, "NS_UP_ALREADY_VOICE", chan->name);
93     } else
94         reply(textclient, user, "NS_NOT_ON_USERLIST_YOU", chan->name);
95 }