]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sqlutils.cpp
Reset the already_sent IDs during slow garbage collection
[user/henk/code/inspircd.git] / src / modules / m_sqlutils.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include <sstream>
16 #include <list>
17 #include "m_sqlutils.h"
18
19 /* $ModDesc: Provides some utilities to SQL client modules, such as mapping queries to users and channels */
20 /* $ModDep: m_sqlutils.h */
21
22 typedef std::map<unsigned long, User*> IdUserMap;
23 typedef std::map<unsigned long, Channel*> IdChanMap;
24 typedef std::list<unsigned long> AssocIdList;
25
26 class ModuleSQLutils : public Module
27 {
28 private:
29         IdUserMap iduser;
30         IdChanMap idchan;
31         SimpleExtItem<AssocIdList> idExt;
32
33 public:
34         ModuleSQLutils() : idExt("sqlutils_list", this)
35         {
36                 Implementation eventlist[] = { I_OnChannelDelete, I_OnUnloadModule, I_OnUserDisconnect };
37                 ServerInstance->Modules->Attach(eventlist, this, 3);
38         }
39
40         ~ModuleSQLutils()
41         {
42         }
43
44
45         void OnRequest(Request& request)
46         {
47                 if(strcmp(SQLUTILAU, request.id) == 0)
48                 {
49                         AssociateUser* req = (AssociateUser*)&request;
50
51                         iduser.insert(std::make_pair(req->id, req->user));
52
53                         AttachList(req->user, req->id);
54                 }
55                 else if(strcmp(SQLUTILAC, request.id) == 0)
56                 {
57                         AssociateChan* req = (AssociateChan*)&request;
58
59                         idchan.insert(std::make_pair(req->id, req->chan));
60
61                         AttachList(req->chan, req->id);
62                 }
63                 else if(strcmp(SQLUTILUA, request.id) == 0)
64                 {
65                         UnAssociate* req = (UnAssociate*)&request;
66
67                         /* Unassociate a given query ID with all users and channels
68                          * it is associated with.
69                          */
70
71                         DoUnAssociate(iduser, req->id);
72                         DoUnAssociate(idchan, req->id);
73                 }
74                 else if(strcmp(SQLUTILGU, request.id) == 0)
75                 {
76                         GetAssocUser* req = (GetAssocUser*)&request;
77
78                         IdUserMap::iterator iter = iduser.find(req->id);
79
80                         if(iter != iduser.end())
81                         {
82                                 req->user = iter->second;
83                         }
84                 }
85                 else if(strcmp(SQLUTILGC, request.id) == 0)
86                 {
87                         GetAssocChan* req = (GetAssocChan*)&request;
88
89                         IdChanMap::iterator iter = idchan.find(req->id);
90
91                         if(iter != idchan.end())
92                         {
93                                 req->chan = iter->second;
94                         }
95                 }
96         }
97
98         void OnUserDisconnect(LocalUser* user)
99         {
100                 /* A user is disconnecting, first we need to check if they have a list of queries associated with them.
101                  * Then, if they do, we need to erase each of them from our IdUserMap (iduser) so when the module that
102                  * associated them asks to look them up then it gets a NULL result and knows to discard the query.
103                  */
104                 AssocIdList* il = idExt.get(user);
105
106                 if(il)
107                 {
108                         for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
109                         {
110                                 IdUserMap::iterator iter;
111
112                                 iter = iduser.find(*listiter);
113
114                                 if(iter != iduser.end())
115                                 {
116                                         if(iter->second != user)
117                                         {
118                                                 ServerInstance->Logs->Log("m_sqlutils",DEBUG, "BUG: ID associated with user %s doesn't have the same User* associated with it in the map (erasing anyway)", user->nick.c_str());
119                                         }
120
121                                         iduser.erase(iter);
122                                 }
123                                 else
124                                 {
125                                         ServerInstance->Logs->Log("m_sqlutils",DEBUG, "BUG: user %s was extended with sqlutils_queryids but there was nothing matching in the map", user->nick.c_str());
126                                 }
127                         }
128
129                         idExt.unset(user);
130                 }
131         }
132
133         void AttachList(Extensible* obj, unsigned long id)
134         {
135                 AssocIdList* il = idExt.get(obj);
136
137                 if (!il)
138                 {
139                         /* Doesn't already exist, create a new list and attach it. */
140                         il = new AssocIdList;
141                         idExt.set(obj, il);
142                 }
143
144                 /* Now either way we have a valid list in il, attached. */
145                 il->push_back(id);
146         }
147
148         void RemoveFromList(Extensible* obj, unsigned long id)
149         {
150                 AssocIdList* il = idExt.get(obj);
151
152                 if (il)
153                 {
154                         /* Only do anything if the list exists... (which it ought to) */
155                         il->remove(id);
156
157                         if(il->empty())
158                         {
159                                 /* If we just emptied it.. */
160                                 idExt.unset(obj);
161                         }
162                 }
163         }
164
165         template <class T> void DoUnAssociate(T &map, unsigned long id)
166         {
167                 /* For each occurence of 'id' (well, only one..it's not a multimap) in 'map'
168                  * remove it from the map, take an Extensible* value from the map and remove
169                  * 'id' from the list of query IDs attached to it.
170                  */
171                 typename T::iterator iter = map.find(id);
172
173                 if(iter != map.end())
174                 {
175                         /* Found a value indexed by 'id', call RemoveFromList()
176                          * on it with 'id' to remove 'id' from the list attached
177                          * to the value.
178                          */
179                         RemoveFromList(iter->second, id);
180                 }
181         }
182
183         void OnChannelDelete(Channel* chan)
184         {
185                 /* A channel is being destroyed, first we need to check if it has a list of queries associated with it.
186                  * Then, if it does, we need to erase each of them from our IdChanMap (idchan) so when the module that
187                  * associated them asks to look them up then it gets a NULL result and knows to discard the query.
188                  */
189                 AssocIdList* il = idExt.get(chan);
190
191                 if (il)
192                 {
193                         for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
194                         {
195                                 IdChanMap::iterator iter;
196
197                                 iter = idchan.find(*listiter);
198
199                                 if(iter != idchan.end())
200                                 {
201                                         if(iter->second != chan)
202                                         {
203                                                 ServerInstance->Logs->Log("m_sqlutils",DEBUG, "BUG: ID associated with channel %s doesn't have the same Channel* associated with it in the map (erasing anyway)", chan->name.c_str());
204                                         }
205                                         idchan.erase(iter);
206                                 }
207                                 else
208                                 {
209                                         ServerInstance->Logs->Log("m_sqlutils",DEBUG, "BUG: channel %s was extended with sqlutils_queryids but there was nothing matching in the map", chan->name.c_str());
210                                 }
211                         }
212
213                         idExt.unset(chan);
214                 }
215         }
216
217         Version GetVersion()
218         {
219                 return Version("Provides some utilities to SQL client modules, such as mapping queries to users and channels", VF_VENDOR);
220         }
221
222 };
223
224 MODULE_INIT(ModuleSQLutils)