*** VERSION 5.4.0 ***
[NeonServV5.git] / src / modules / NeonHelp.mod / cmd_neonhelp_next.c
1 /* cmd_neonhelp_next.c - NeonServ v5.4
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_neonhelp.h"
19
20 /*
21 * argv[0]   (optional) suppid
22 */
23
24 CMD_BIND(neonhelp_cmd_next) {
25     //check permissions
26     MYSQL_RES *res;
27     MYSQL_ROW row, row2;
28     int caccess = 0;
29     int userid;
30     printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern` FROM `helpserv_settings` WHERE `helpserv_botid` = '%d'", client->clientid);
31     res = mysql_use();
32     if (!(row = mysql_fetch_row(res))) return;
33     //check if the user is a supporter (access in the support channel)
34     if((user->flags & USERFLAG_ISAUTHED)) {
35         if(user->flags & USERFLAG_HAS_USERID)
36             userid = user->user_id;
37         else {
38             printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
39             res = mysql_use();
40             if ((row2 = mysql_fetch_row(res)) != NULL) {
41                 userid = atoi(row2[0]);
42                 user->user_id = userid;
43                 user->flags |= USERFLAG_HAS_USERID;
44             } else
45                 userid = 0;
46         }
47         printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags` FROM `chanusers` LEFT JOIN `channels` ON `chanuser_cid` = `channel_id` WHERE `chanuser_uid` = '%d' AND `channel_name` = '%s'", userid, escape_string(row[0]));
48         res = mysql_use();
49         if ((row2 = mysql_fetch_row(res)) != NULL) {
50             int cflags = atoi(row2[1]);
51             if(!(cflags & DB_CHANUSER_SUSPENDED))
52                 caccess = atoi(row2[0]);
53         }
54     }
55     if(!caccess) {
56         reply(getTextBot(), user, "MODCMD_ACCESS_DENIED");
57         return;
58     }
59     if(!(client->flags & SOCKET_HAVE_HELPNODE) || client->botclass_helpnode == NULL) {
60         reply(getTextBot(), user, "NH_NEXT_NONE");
61         return;
62     }
63     struct NeonHelpNode *helpnode, *next_helpnode = NULL;
64     int lowest_id = -1;
65     for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
66         if(helpnode->status == 0) {
67             if(argc) {
68                 if(atoi(argv[0]) == helpnode->suppid) {
69                     next_helpnode = helpnode;
70                     break;
71                 }
72             } else {
73                 if(helpnode->suppid < lowest_id || lowest_id == -1) {
74                     lowest_id = helpnode->suppid;
75                     next_helpnode = helpnode;
76                 }
77             }
78         }
79     }
80     if(!next_helpnode) {
81         reply(getTextBot(), user, "NH_NEXT_NOT_FOUND");
82         return;
83     }
84     printf_mysql_query("SELECT `text` FROM `helpserv_requests` WHERE `id` = '%d'", next_helpnode->suppid);
85     res = mysql_use();
86     if (!(row2 = mysql_fetch_row(res))) return;
87     reply(getTextBot(), user, "NH_NEXT_HEADER", next_helpnode->suppid, next_helpnode->user->nick);
88     char *a, *b = row2[0];
89     do {
90         a = strstr(b, "\n");
91         if(a) *a = '\0';
92         reply(getTextBot(), user, "  %s", b);
93         if(a) {
94             *a = '\n';
95             b = a+1;
96         }
97     } while(a);
98     if(row[1])
99         putsock(client, "INVITE %s %s", next_helpnode->user->nick, row[0]);
100     else
101         putsock(client, "MODE %s +v %s", row[0], next_helpnode->user->nick);
102     char sendbuf1[MAXLEN];
103     char sendbuf2[MAXLEN];
104     char *join_now = (row[1] ? build_language_string(user, sendbuf2, "NH_NEXT_JOIN", row[0]) : "");
105     putsock(client, "PRIVMSG %s :%s %s", next_helpnode->user->nick, build_language_string(user, sendbuf1, "NH_NEXT_HELPER", next_helpnode->suppid, user->auth, user->nick), join_now);
106     next_helpnode->status = 1;
107     printf_mysql_query("UPDATE `helpserv_requests` SET `status` = '1', `supporter` = '%d' WHERE `id` = '%d'", userid, next_helpnode->suppid);
108 }