+static void neonhelp_event_privmsg_async(struct ClientSocket *client, struct UserNode *user, struct UserNode *target, char *message);
+static USERAUTH_CALLBACK(neonhelp_event_privmsg_nick_lookup);
+struct neonhelp_event_privmsg_cache {
+ struct ClientSocket *client;
+ struct UserNode *user, *target;
+ char *message;
+};
+
+static void neonhelp_event_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
+ struct ClientSocket *client;
+ for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
+ if(client->user == target) {
+ if(client->botid != BOTID) return;
+ break;
+ }
+ }
+ if(!client) return; //we got the message but we have no client that could receive it???
+ if(user->flags & USERFLAG_ISAUTHED) {
+ neonhelp_event_privmsg_async(client, user, target, message);
+ } else {
+ struct neonhelp_event_privmsg_cache *cache = malloc(sizeof(*cache));
+ if(!cache) return;
+ cache->client = client;
+ cache->user = user;
+ cache->target = target;
+ cache->message = strdup(message);
+ get_userauth(user, neonhelp_event_privmsg_nick_lookup, cache);
+ }
+}
+
+static USERAUTH_CALLBACK(neonhelp_event_privmsg_nick_lookup) {
+ struct neonhelp_event_privmsg_cache *cache = data;
+ neonhelp_event_privmsg_async(cache->client, cache->user, cache->target, cache->message);
+ free(cache->message);
+ free(cache);
+}
+
+static TIMEQ_CALLBACK(neonhelp_remind_open_requests);
+
+static void neonhelp_event_privmsg_async(struct ClientSocket *client, struct UserNode *user, struct UserNode *target, char *message) {
+ MYSQL_RES *res;
+ MYSQL_ROW row, row2;
+ printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern`, `helpserv_intern_announce` FROM `helpserv_settings` WHERE `helpserv_botid` = '%d'", client->clientid);
+ res = mysql_use();
+ if (!(row = mysql_fetch_row(res))) return;
+ //check if the user is a supporter (access in the support channel)
+ if((user->flags & USERFLAG_ISAUTHED)) {
+ int caccess = 0;
+ int userid;
+ if(user->flags & USERFLAG_HAS_USERID)
+ userid = user->user_id;
+ else {
+ printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
+ res = mysql_use();
+ if ((row2 = mysql_fetch_row(res)) != NULL) {
+ userid = atoi(row2[0]);
+ user->user_id = userid;
+ user->flags |= USERFLAG_HAS_USERID;
+ } else
+ userid = 0;
+ }
+ 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]));
+ res = mysql_use();
+ if ((row2 = mysql_fetch_row(res)) != NULL) {
+ int cflags = atoi(row2[1]);
+ if(!(cflags & DB_CHANUSER_SUSPENDED))
+ caccess = atoi(row2[0]);
+ }
+ if(caccess) return; //ignore messages from supporters
+ }
+ //check if the user is in one of the bot's channels
+ struct ChanUser *chanuser;
+ struct ChanNode *chan;
+ for(chanuser = getUserChannels(target, NULL); chanuser; chanuser = getUserChannels(target, chanuser)) {
+ chan = chanuser->chan;
+ if((!stricmp(chan->name, row[0]) || (row[1] && !stricmp(chan->name, row[1]))) && isUserOnChan(user, chan))
+ break;
+ }
+ if(!chanuser) {
+ if(row[1])
+ reply(client, user, "NH_NOT_ON_CHAN_2", row[0], row[1]);
+ else
+ reply(client, user, "NH_NOT_ON_CHAN_1", row[0]);
+ return;
+ }
+ //check if there is already a support request
+ int others = 0;
+ if(client->flags & SOCKET_HAVE_HELPNODE) {
+ struct NeonHelpNode *helpnode;
+ for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
+ if(helpnode->user == user) {
+ //simply append the message to the database
+ printf_mysql_query("SELECT `text` FROM `helpserv_requests` WHERE `id` = %d", helpnode->suppid);
+ res = mysql_use();
+ if ((row2 = mysql_fetch_row(res)) != NULL) {
+ char *old_msg = escape_string(row2[0]);
+ char *new_msg = escape_string(message);
+ printf_long_mysql_query(1024 + strlen(old_msg) + strlen(new_msg), "UPDATE `helpserv_requests` SET `text` = '%s\n%s' WHERE `id` = %d", old_msg, new_msg, helpnode->suppid);
+ }
+ return;
+ }
+ others++;
+ }
+ }
+ //add new request
+ struct NeonHelpNode *helpnode = malloc(sizeof(*helpnode));
+ if(!helpnode) return;
+ helpnode->user = user;
+ helpnode->logchan = getChanByName(row[0]);
+ helpnode->status = 0;
+ helpnode->announce = (row[2] && strcmp(row[3], "0") ? 1 : 0);
+ if(helpnode->announce) {
+ char nameBuf[30];
+ sprintf(nameBuf, "neonhelp_%d", client->clientid);
+ if(!timeq_name_exists(nameBuf)) {
+ int *cidptr = malloc(sizeof(int));
+ *cidptr = client->clientid;
+ timeq_add_name(nameBuf, 300, neonhelp_remind_open_requests, cidptr);
+ }
+ }
+ printf_mysql_query("INSERT INTO `helpserv_requests` (`botid`, `host`, `hand`, `nick`, `status`, `supporter`, `time`, `text`) VALUES ('%d', '%s@%s', '%s', '%s', '0', '-1', UNIX_TIMESTAMP(), '%s')", client->clientid, escape_string(user->ident), escape_string(user->host), ((user->flags & USERFLAG_ISAUTHED) ? escape_string(user->auth) : "*"), escape_string(user->nick), escape_string(message));
+ helpnode->suppid = (int) mysql_insert_id(get_mysql_conn());
+ helpnode->log = NULL;
+ helpnode->next = ((client->flags & SOCKET_HAVE_HELPNODE) ? client->botclass_helpnode : NULL);
+ client->botclass_helpnode = helpnode;
+ client->flags |= SOCKET_HAVE_HELPNODE;
+ //build the user reply...
+ char user_reply[MAXLEN];
+ int user_reply_pos = 0;
+ char reply_buff[MAXLEN];
+ //1st part: NH_REQUEST_RECORDED
+ strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, "NH_REQUEST_RECORDED", helpnode->suppid));
+ user_reply_pos += strlen(reply_buff);
+ //2nd part: NH_REQUEST_OTHERS_0 / NH_REQUEST_OTHERS_1 / NH_REQUEST_OTHERS_2
+ user_reply[user_reply_pos++] = ' ';
+ if(others <= 1)
+ strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, (others ? "NH_REQUEST_OTHERS_1" : "NH_REQUEST_OTHERS_0")));
+ else
+ strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, "NH_REQUEST_OTHERS_2", others));
+ user_reply_pos += strlen(reply_buff);
+ //3th part: NH_REQUEST_FOOTER_1 / NH_REQUEST_FOOTER_2
+ user_reply[user_reply_pos++] = ' ';
+ if(row[1])
+ strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, "NH_REQUEST_FOOTER_2", row[0], row[1]));
+ else
+ strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, "NH_REQUEST_FOOTER_1", row[0]));
+ user_reply_pos += strlen(reply_buff);
+ reply(client, user, "%s", user_reply);
+ //sent a message to the internal channel / onotice to supp channel
+ build_language_string(user, reply_buff, "NH_NEW_REQUEST", helpnode->suppid, user->nick, message);
+ if(row[2]) {
+ putsock(client, "PRIVMSG %s :%s", row[2], reply_buff);
+ } else {
+ putsock(client, "NOTICE @%s :%s", row[0], reply_buff);
+ }
+}
+
+static TIMEQ_CALLBACK(neonhelp_remind_open_requests) {
+ int clientid = *((int*)data);
+ MYSQL_RES *res;
+ MYSQL_ROW row;
+ printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern`, `helpserv_intern_announce` FROM `helpserv_settings` WHERE `helpserv_botid` = '%d'", clientid);
+ res = mysql_use();
+ if (!(row = mysql_fetch_row(res)) || !row[2]) {
+ free(data);
+ return;
+ }
+ struct ClientSocket *client;
+ for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
+ if(client->clientid == clientid)
+ break;
+ }
+ if(!client) {
+ free(data);
+ return;
+ }
+ //count open requests
+ int requests = 0;
+ struct NeonHelpNode *helpnode;
+ if(client->flags & SOCKET_HAVE_HELPNODE) {
+ for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
+ if(helpnode->status == 0) {
+ requests++;
+ }
+ }
+ }
+ if(requests) {
+ char nameBuf[30];
+ sprintf(nameBuf, "neonhelp_%d", client->clientid);
+ if(!timeq_name_exists(nameBuf)) {
+ timeq_add_name(nameBuf, 300, neonhelp_remind_open_requests, data);
+ }
+ char replybuf[MAXLEN];
+ build_language_string(NULL, replybuf, (requests == 1 ? "NH_REMIND_OPEN_REQUESTS_1" : "NH_REMIND_OPEN_REQUESTS_2"), requests);
+ putsock(client, "PRIVMSG %s :%s", row[2], replybuf);
+ } else
+ free(data);
+}
+
+static void neonhelp_event_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
+ char logline[MAXLEN];
+ sprintf(logline, "<%s> %s", user->nick, message);
+ struct ClientSocket *client;
+ for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
+ if(client->botid == BOTID) {
+ struct NeonHelpNode *helpnode;
+ if(client->flags & SOCKET_HAVE_HELPNODE) {
+ for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
+ if(helpnode->logchan == chan && helpnode->status == 1) {
+ if(!helpnode->log) {
+ helpnode->log = calloc(LOGBUFFERLINES, sizeof(char*));
+ if(!helpnode->log) return;
+ }
+ int i;
+ for(i = 0; i < LOGBUFFERLINES; i++) {
+ if(!helpnode->log[i]) {
+ helpnode->log[i] = strdup(logline);
+ break;
+ }
+ }
+ if(i == LOGBUFFERLINES) {
+ //write buffer to database
+ char logbuff[MAXLEN * LOGBUFFERLINES];
+ int len = 0;
+ for(i = 0; i < LOGBUFFERLINES; i++) {
+ len += sprintf(logbuff + len, "%s\n", helpnode->log[i]);
+ free(helpnode->log[i]);
+ helpnode->log[i] = NULL;
+ }
+ printf_long_mysql_query(1024 + len, "UPDATE `helpserv_requests` SET `log` = CONCAT(`log`, '%s') WHERE `id` = %d", escape_string(logbuff), helpnode->suppid);
+ }
+ break;
+ }
+ }
+ }
+ }
+ }
+}
+
+static void destroy_support_request(struct ClientSocket *client, struct NeonHelpNode *helpnode, int do_reply) {
+ //write buffer to database
+ char logbuff[MAXLEN * LOGBUFFERLINES];
+ int len = 0;
+ int i;
+ if(helpnode->log) {
+ for(i = 0; i < LOGBUFFERLINES; i++) {
+ if(!helpnode->log[i]) break;
+ len += sprintf(logbuff + len, "%s\n", helpnode->log[i]);
+ free(helpnode->log[i]);
+ helpnode->log[i] = NULL;
+ }
+ free(helpnode->log);
+ } else
+ logbuff[0] = '\0';
+ printf_long_mysql_query(1024 + len, "UPDATE `helpserv_requests` SET `status`='2', `log` = CONCAT(`log`, '%s') WHERE `id` = %d", escape_string(logbuff), helpnode->suppid);
+ if(do_reply) {
+ reply(client, helpnode->user, "NH_DELETED", helpnode->suppid);
+ }
+ free(helpnode);
+}
+
+static void neonhelp_event_kick(struct UserNode *user, struct ChanUser *target, char *reason) {
+ struct ClientSocket *client;
+ MYSQL_RES *res;
+ MYSQL_ROW row;
+ struct ChanNode *support, *public;
+ int userHasRequest;
+ for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
+ if(client->botid == BOTID && isUserOnChan(client->user, target->chan)) {
+ userHasRequest = 0;
+ struct NeonHelpNode *helpnode, *prev_helpnode = NULL;
+ if(client->flags & SOCKET_HAVE_HELPNODE) {
+ for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
+ if(helpnode->user == target->user) {
+ userHasRequest = 1;
+ break;
+ } else
+ prev_helpnode = helpnode;
+ }
+ }
+ if(!userHasRequest) continue;
+ printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern` FROM `helpserv_settings` WHERE `helpserv_botid` = '%d'", client->clientid);
+ res = mysql_use();
+ if (!(row = mysql_fetch_row(res))) continue;
+ support = getChanByName(row[0]);
+ public = (row[1] ? getChanByName(row[1]) : NULL);
+ if(target->chan == support || !((support && isUserOnChan(target->user, support)) || (public && isUserOnChan(target->user, public)))) {
+ //free the user's support request
+ if(prev_helpnode)
+ prev_helpnode->next = helpnode->next;
+ else
+ client->botclass_helpnode = helpnode->next;
+ destroy_support_request(client, helpnode, 1);
+ }
+ }
+ }
+}
+
+static void neonhelp_event_part(struct ChanUser *target, char *reason) {
+ struct ClientSocket *client;
+ MYSQL_RES *res;
+ MYSQL_ROW row;
+ struct ChanNode *support, *public;
+ int userHasRequest;
+ for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
+ if(client->botid == BOTID && isUserOnChan(client->user, target->chan)) {
+ userHasRequest = 0;
+ struct NeonHelpNode *helpnode, *prev_helpnode = NULL;
+ if(client->flags & SOCKET_HAVE_HELPNODE) {
+ for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
+ if(helpnode->user == target->user) {
+ userHasRequest = 1;
+ break;
+ } else
+ prev_helpnode = helpnode;
+ }
+ }
+ if(!userHasRequest) continue;
+ printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern` FROM `helpserv_settings` WHERE `helpserv_botid` = '%d'", client->clientid);
+ res = mysql_use();
+ if (!(row = mysql_fetch_row(res))) continue;
+ support = getChanByName(row[0]);
+ public = (row[1] ? getChanByName(row[1]) : NULL);
+ if(target->chan == support || !((support && isUserOnChan(target->user, support)) || (public && isUserOnChan(target->user, public)))) {
+ //free the user's support request
+ if(prev_helpnode)
+ prev_helpnode->next = helpnode->next;
+ else
+ client->botclass_helpnode = helpnode->next;
+ destroy_support_request(client, helpnode, 1);
+ }
+ }
+ }
+}
+
+static void neonhelp_event_quit(struct UserNode *target, char *reason) {
+ struct ClientSocket *client;
+ int userHasRequest;
+ for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
+ if(client->botid == BOTID) {
+ userHasRequest = 0;
+ struct NeonHelpNode *helpnode, *prev_helpnode = NULL;
+ if(client->flags & SOCKET_HAVE_HELPNODE) {
+ for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
+ if(helpnode->user == target) {
+ userHasRequest = 1;
+ break;
+ } else
+ prev_helpnode = helpnode;
+ }
+ }
+ if(!userHasRequest) continue;
+ //free the user's support request
+ if(prev_helpnode)
+ prev_helpnode->next = helpnode->next;
+ else
+ client->botclass_helpnode = helpnode->next;
+ destroy_support_request(client, helpnode, 0);
+ }
+ }
+}
+