1 /* HandleInfoHandler.c - NeonServ v5.2
2 * Copyright (C) 2011 Philipp Kreil (pk910)
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.
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.
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/>.
18 #include "HandleInfoHandler.h"
19 #include "ClientSocket.h"
21 #include "IRCEvents.h"
24 #define AUTHSERV_NICK "AuthServ"
26 #define MAXCALLBACKS 3
28 struct HandleInfoQueueEntry {
30 void *callback[MAXCALLBACKS];
31 void *data[MAXCALLBACKS];
33 struct HandleInfoQueueEntry *next;
36 static struct HandleInfoQueueEntry* addHandleInfoQueueEntry(struct ClientSocket *client) {
37 struct HandleInfoQueueEntry *entry = malloc(sizeof(*entry));
40 perror("malloc() failed");
44 if(client->handleinfo_last)
45 client->handleinfo_last->next = entry;
47 client->handleinfo_first = entry;
48 client->handleinfo_last = entry;
52 static struct HandleInfoQueueEntry* getNextHandleInfoQueueEntry(struct ClientSocket *client, int freeEntry) {
53 if(!client->handleinfo_first) return NULL;
54 struct HandleInfoQueueEntry *entry = client->handleinfo_first;
56 client->handleinfo_first = entry->next;
57 if(entry == client->handleinfo_last) {
58 client->handleinfo_last = NULL;
64 void clear_handleinfoqueue(struct ClientSocket *client) {
65 if(!client->handleinfo_first) return;
66 struct HandleInfoQueueEntry *entry, *next;
67 for(entry = client->handleinfo_first; entry; entry = next) {
71 client->handleinfo_last = NULL;
74 void lookup_authname(char *auth, authlookup_callback_t callback, void *data) {
75 struct ClientSocket *bot;
76 struct HandleInfoQueueEntry* entry;
77 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
78 for(entry = bot->handleinfo_first; entry; entry = entry->next) {
79 if(!stricmp(entry->auth, auth)) {
81 for(i = 1; i < MAXCALLBACKS; i++) {
82 if(!entry->callback[i]) {
83 entry->callback[i] = callback;
84 entry->data[i] = data;
90 if(bot->flags & SOCKET_FLAG_PREFERRED)
93 if(bot == NULL) return;
94 entry = addHandleInfoQueueEntry(bot);
96 entry->auth = strdup(auth);
97 entry->callback[0] = callback;
98 for(i = 1; i < MAXCALLBACKS; i++)
99 entry->callback[i] = NULL;
100 entry->data[0] = data;
101 for(i = 1; i < MAXCALLBACKS; i++)
102 entry->data[i] = NULL;
103 putsock(bot, "PRIVMSG " AUTHSERV_NICK " :ACCOUNTINFO *%s", auth);
106 static void recv_notice(struct UserNode *user, struct UserNode *target, char *message) {
107 if(stricmp(user->nick, AUTHSERV_NICK)) return;
108 struct ClientSocket *bot;
109 for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
110 if(bot->user == target) break;
114 int do_match = 0, exists = 0;
117 // Account * has not been registered.
118 // Account information for Skynet:
119 if(!match("Account * has not been registered.", message)) {
121 tmp = strstr(message, "\002");
123 tmp = strstr(auth, "\002");
126 if(!match("Account information for *", message)) {
129 tmp = strstr(message, "\002");
131 tmp = strstr(auth, "\002");
135 struct HandleInfoQueueEntry* entry = getNextHandleInfoQueueEntry(bot, 1);
137 authlookup_callback_t *callback;
139 for(i = 0; i < MAXCALLBACKS; i++) {
140 callback = entry->callback[i];
142 callback(auth, exists, entry->data[i]);
150 void init_handleinfohandler() {
151 bind_privnotice(recv_notice);
154 void free_handleinfohandler() {