8a135cdb16c8453b2d87d0f09b2c1c8fb409b587
[NeonServV5.git] / src / HandleInfoHandler.c
1 /* HandleInfoHandler.c - NeonServ v5.2
2  * Copyright (C) 2011  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 "HandleInfoHandler.h"
19 #include "ClientSocket.h"
20 #include "UserNode.h"
21 #include "IRCEvents.h"
22 #include "tools.h"
23
24 #define AUTHSERV_NICK "AuthServ"
25
26 #define MAXCALLBACKS 3
27
28 struct HandleInfoQueueEntry {
29     char *auth;
30     void *callback[MAXCALLBACKS];
31     void *data[MAXCALLBACKS];
32     
33     struct HandleInfoQueueEntry *next;
34 };
35
36 static struct HandleInfoQueueEntry* addHandleInfoQueueEntry(struct ClientSocket *client) {
37     struct HandleInfoQueueEntry *entry = malloc(sizeof(*entry));
38     if (!entry)
39     {
40         perror("malloc() failed");
41         return NULL;
42     }
43     entry->next = NULL;
44     if(client->handleinfo_last)
45         client->handleinfo_last->next = entry;
46     else
47         client->handleinfo_first = entry;
48     client->handleinfo_last = entry;
49     return entry;
50 }
51
52 static struct HandleInfoQueueEntry* getNextHandleInfoQueueEntry(struct ClientSocket *client, int freeEntry) {
53     if(!client->handleinfo_first) return NULL;
54     struct HandleInfoQueueEntry *entry = client->handleinfo_first;
55     if(freeEntry) {
56         client->handleinfo_first = entry->next;
57         if(entry == client->handleinfo_last) {
58             client->handleinfo_last = NULL;
59         }
60     }
61     return entry;
62 }
63
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) {
68         next = entry->next;
69         free(entry);
70     }
71     client->handleinfo_last = NULL;
72 }
73
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)) {
80                 int i = 0;
81                 for(i = 1; i < MAXCALLBACKS; i++) {
82                     if(!entry->callback[i]) {
83                         entry->callback[i] = callback;
84                         entry->data[i] = data;
85                         return;
86                     }
87                 }
88             }
89         }
90         if(bot->flags & SOCKET_FLAG_PREFERRED)
91             break;
92     }
93     if(bot == NULL) return;
94     entry = addHandleInfoQueueEntry(bot);
95     int i;
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);
104 }
105
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;
111     }
112     if(!bot) return;
113     char *auth = NULL;
114     int do_match = 0, exists = 0;
115     char *tmp;
116     //messages to parse:
117     //  Account * has not been registered.
118     //  Account information for Skynet:
119     if(!match("Account * has not been registered.", message)) {
120         do_match = 1;
121         tmp = strstr(message, "\002");
122         auth = tmp+1;
123         tmp = strstr(auth, "\002");
124         *tmp = '\0';
125     }
126     if(!match("Account information for *", message)) {
127         do_match = 1;
128         exists = 1;
129         tmp = strstr(message, "\002");
130         auth = tmp+1;
131         tmp = strstr(auth, "\002");
132         *tmp = '\0';
133     }
134     if(do_match) {
135         struct HandleInfoQueueEntry* entry = getNextHandleInfoQueueEntry(bot, 1);
136         if(entry) {
137             authlookup_callback_t *callback;
138             int i;
139             for(i = 0; i < MAXCALLBACKS; i++) {
140                 callback = entry->callback[i];
141                 if(!callback) break;
142                 callback(auth, exists, entry->data[i]);
143             }
144             free(entry->auth);
145             free(entry);
146         }
147     }
148 }
149
150 void init_handleinfohandler() {
151     bind_privnotice(recv_notice);
152 }
153
154 void free_handleinfohandler() {
155     
156 }