]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sqlutils.cpp
c423246cced8d9a4f053cd068a3853acc65c803c
[user/henk/code/inspircd.git] / src / modules / extra / m_sqlutils.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include <sstream>
15 #include <string>
16 #include <map>
17 #include <list>
18
19 #include "users.h"
20 #include "channels.h"
21 #include "modules.h"
22
23 #include "inspircd.h"
24 #include "configreader.h"
25
26 #include "m_sqlutils.h"
27
28 /* $ModDesc: Provides some utilities to SQL client modules, such as mapping queries to users and channels */
29 /* $ModDep: m_sqlutils.h */
30
31 typedef std::map<unsigned long, userrec*> IdUserMap;
32 typedef std::map<unsigned long, chanrec*> IdChanMap;
33 typedef std::list<unsigned long> AssocIdList;
34
35 class ModuleSQLutils : public Module
36 {
37 private:
38         
39
40         IdUserMap iduser;
41         IdChanMap idchan;
42
43 public:
44         ModuleSQLutils(InspIRCd* Me)
45         : Module::Module(Me)
46         {
47                 ServerInstance->Log(DEBUG, "%s 'SQLutils' feature", ServerInstance->PublishFeature("SQLutils", this) ? "Published" : "Couldn't publish");
48         }
49
50         void Implements(char* List)
51         {
52                 List[I_OnChannelDelete] = List[I_OnUnloadModule] = List[I_OnRequest] =  List[I_OnUserDisconnect] = 1;
53         }
54
55         virtual char* OnRequest(Request* request)
56         {
57                 if(strcmp(SQLUTILAU, request->GetId()) == 0)
58                 {
59                         AssociateUser* req = (AssociateUser*)request;
60                         
61                         ServerInstance->Log(DEBUG, "Associated ID %lu with user %s", req->id, req->user->nick);
62                         
63                         iduser.insert(std::make_pair(req->id, req->user));
64                         
65                         AttachList(req->user, req->id);
66                 }
67                 else if(strcmp(SQLUTILAC, request->GetId()) == 0)
68                 {
69                         AssociateChan* req = (AssociateChan*)request;
70
71                         ServerInstance->Log(DEBUG, "Associated ID %lu with channel %s", req->id, req->chan->name);
72                         
73                         idchan.insert(std::make_pair(req->id, req->chan));                      
74                         
75                         AttachList(req->chan, req->id);
76                 }
77                 else if(strcmp(SQLUTILUA, request->GetId()) == 0)
78                 {
79                         UnAssociate* req = (UnAssociate*)request;
80                         
81                         /* Unassociate a given query ID with all users and channels
82                          * it is associated with.
83                          */
84                         
85                         ServerInstance->Log(DEBUG, "Unassociating ID %lu with all users and channels", req->id);
86                         
87                         DoUnAssociate(iduser, req->id);
88                         DoUnAssociate(idchan, req->id);
89                 }
90                 else if(strcmp(SQLUTILGU, request->GetId()) == 0)
91                 {
92                         GetAssocUser* req = (GetAssocUser*)request;
93                         
94                         IdUserMap::iterator iter = iduser.find(req->id);
95                         
96                         ServerInstance->Log(DEBUG, "Looking up user associated with ID %lu", req->id);
97                         
98                         if(iter != iduser.end())
99                         {
100                                 ServerInstance->Log(DEBUG, "Found user %s", iter->second->nick);
101                                 req->user = iter->second;
102                         }
103                 }
104                 else if(strcmp(SQLUTILGC, request->GetId()) == 0)
105                 {
106                         GetAssocChan* req = (GetAssocChan*)request;                     
107                         
108                         IdChanMap::iterator iter = idchan.find(req->id);
109                         
110                         ServerInstance->Log(DEBUG, "Looking up channel associated with ID %lu", req->id);
111                         
112                         if(iter != idchan.end())
113                         {
114                                 ServerInstance->Log(DEBUG, "Found channel %s", iter->second->name);
115                                 req->chan = iter->second;
116                         }
117                 }
118                 else
119                 {
120                         ServerInstance->Log(DEBUG, "Got unsupported API version string: %s", request->GetId());
121                         return NULL;
122                 }
123                 
124                 return SQLUTILSUCCESS;
125         }
126         
127         virtual void OnUserDisconnect(userrec* user)
128         {
129                 /* A user is disconnecting, first we need to check if they have a list of queries associated with them.
130                  * Then, if they do, we need to erase each of them from our IdUserMap (iduser) so when the module that
131                  * associated them asks to look them up then it gets a NULL result and knows to discard the query.
132                  */
133                 AssocIdList* il;
134                 
135                 if(user->GetExt("sqlutils_queryids", il))
136                 {
137                         for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
138                         {
139                                 IdUserMap::iterator iter;
140                         
141                                 iter = iduser.find(*listiter);
142                         
143                                 if(iter != iduser.end())
144                                 {
145                                         if(iter->second == user)
146                                         {
147                                                 ServerInstance->Log(DEBUG, "Erased query from map associated with quitting user %s", user->nick);
148                                         }
149                                         else
150                                         {
151                                                 ServerInstance->Log(DEBUG, "BUG: ID associated with user %s doesn't have the same userrec* associated with it in the map (erasing anyway)", user->nick);
152                                         }
153
154                                         iduser.erase(iter);                                     
155                                 }
156                                 else
157                                 {
158                                         ServerInstance->Log(DEBUG, "BUG: user %s was extended with sqlutils_queryids but there was nothing matching in the map", user->nick);
159                                 }
160                         }
161                         
162                         user->Shrink("sqlutils_queryids");
163                         delete il;
164                 }
165         }
166         
167         void AttachList(Extensible* obj, unsigned long id)
168         {
169                 AssocIdList* il;
170                 
171                 if(!obj->GetExt("sqlutils_queryids", il))
172                 {
173                         /* Doesn't already exist, create a new list and attach it. */
174                         il = new AssocIdList;
175                         obj->Extend("sqlutils_queryids", il);
176                 }
177                 
178                 /* Now either way we have a valid list in il, attached. */
179                 il->push_back(id);
180         }
181         
182         void RemoveFromList(Extensible* obj, unsigned long id)
183         {
184                 AssocIdList* il;
185                 
186                 if(obj->GetExt("sqlutils_queryids", il))
187                 {
188                         /* Only do anything if the list exists... (which it ought to) */
189                         il->remove(id);
190                         
191                         if(il->empty())
192                         {
193                                 /* If we just emptied it.. */
194                                 delete il;
195                                 obj->Shrink("sqlutils_queryids");
196                         }
197                 }
198         }
199         
200         template <class T> void DoUnAssociate(T &map, unsigned long id)
201         {
202                 /* For each occurence of 'id' (well, only one..it's not a multimap) in 'map'
203                  * remove it from the map, take an Extensible* value from the map and remove
204                  * 'id' from the list of query IDs attached to it.
205                  */
206                 typename T::iterator iter = map.find(id);
207                 
208                 if(iter != map.end())
209                 {
210                         /* Found a value indexed by 'id', call RemoveFromList()
211                          * on it with 'id' to remove 'id' from the list attached
212                          * to the value.
213                          */
214                         RemoveFromList(iter->second, id);
215                         
216                         ServerInstance->Log(DEBUG, "Removed query %lu from map and removed references to it on value", id);
217                 }
218                 else
219                 {
220                         ServerInstance->Log(DEBUG, "Nothing associated with query %lu", id);
221                 }
222         }
223         
224         virtual void OnChannelDelete(chanrec* chan)
225         {
226                 /* A channel is being destroyed, first we need to check if it has a list of queries associated with it.
227                  * Then, if it does, we need to erase each of them from our IdChanMap (idchan) so when the module that
228                  * associated them asks to look them up then it gets a NULL result and knows to discard the query.
229                  */
230                 AssocIdList* il;
231                 
232                 if(chan->GetExt("sqlutils_queryids", il))
233                 {
234                         for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
235                         {
236                                 IdChanMap::iterator iter;
237                         
238                                 iter = idchan.find(*listiter);
239                         
240                                 if(iter != idchan.end())
241                                 {
242                                         if(iter->second == chan)
243                                         {
244                                                 ServerInstance->Log(DEBUG, "Erased query from map associated with dying channnel %s", chan->name);
245                                         }
246                                         else
247                                         {
248                                                 ServerInstance->Log(DEBUG, "BUG: ID associated with channel %s doesn't have the same chanrec* associated with it in the map (erasing anyway)", chan->name);
249                                         }
250
251                                         idchan.erase(iter);                                     
252                                 }
253                                 else
254                                 {
255                                         ServerInstance->Log(DEBUG, "BUG: channel %s was extended with sqlutils_queryids but there was nothing matching in the map", chan->name);
256                                 }
257                         }
258                         
259                         chan->Shrink("sqlutils_queryids");
260                         delete il;
261                 }
262         }
263                         
264         virtual Version GetVersion()
265         {
266                 return Version(1, 1, 0, 0, VF_STATIC|VF_VENDOR|VF_SERVICEPROVIDER, API_VERSION);
267         }
268         
269         virtual ~ModuleSQLutils()
270         {
271         }       
272 };
273
274 class ModuleSQLutilsFactory : public ModuleFactory
275 {
276  public:
277         ModuleSQLutilsFactory()
278         {
279         }
280         
281         ~ModuleSQLutilsFactory()
282         {
283         }
284         
285         virtual Module * CreateModule(InspIRCd* Me)
286         {
287                 return new ModuleSQLutils(Me);
288         }
289 };
290
291
292 extern "C" void * init_module( void )
293 {
294         return new ModuleSQLutilsFactory;
295 }