]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sqlutils.cpp
Make this FindFeature once, and store the result. It was different dating back from...
[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         IdUserMap iduser;
39         IdChanMap idchan;
40
41 public:
42         ModuleSQLutils(InspIRCd* Me)
43         : Module::Module(Me)
44         {
45                 ServerInstance->PublishInterface("SQLutils", this);
46         }
47
48         virtual ~ModuleSQLutils()
49         {
50                 ServerInstance->UnpublishInterface("SQLutils", this);
51         }       
52
53         void Implements(char* List)
54         {
55                 List[I_OnChannelDelete] = List[I_OnUnloadModule] = List[I_OnRequest] =  List[I_OnUserDisconnect] = 1;
56         }
57
58         virtual char* OnRequest(Request* request)
59         {
60                 if(strcmp(SQLUTILAU, request->GetId()) == 0)
61                 {
62                         AssociateUser* req = (AssociateUser*)request;
63                         
64                         iduser.insert(std::make_pair(req->id, req->user));
65                         
66                         AttachList(req->user, req->id);
67                 }
68                 else if(strcmp(SQLUTILAC, request->GetId()) == 0)
69                 {
70                         AssociateChan* req = (AssociateChan*)request;
71                         
72                         idchan.insert(std::make_pair(req->id, req->chan));                      
73                         
74                         AttachList(req->chan, req->id);
75                 }
76                 else if(strcmp(SQLUTILUA, request->GetId()) == 0)
77                 {
78                         UnAssociate* req = (UnAssociate*)request;
79                         
80                         /* Unassociate a given query ID with all users and channels
81                          * it is associated with.
82                          */
83                         
84                         DoUnAssociate(iduser, req->id);
85                         DoUnAssociate(idchan, req->id);
86                 }
87                 else if(strcmp(SQLUTILGU, request->GetId()) == 0)
88                 {
89                         GetAssocUser* req = (GetAssocUser*)request;
90                         
91                         IdUserMap::iterator iter = iduser.find(req->id);
92                         
93                         if(iter != iduser.end())
94                         {
95                                 req->user = iter->second;
96                         }
97                 }
98                 else if(strcmp(SQLUTILGC, request->GetId()) == 0)
99                 {
100                         GetAssocChan* req = (GetAssocChan*)request;                     
101                         
102                         IdChanMap::iterator iter = idchan.find(req->id);
103                         
104                         if(iter != idchan.end())
105                         {
106                                 req->chan = iter->second;
107                         }
108                 }
109                 
110                 return SQLUTILSUCCESS;
111         }
112         
113         virtual void OnUserDisconnect(userrec* user)
114         {
115                 /* A user is disconnecting, first we need to check if they have a list of queries associated with them.
116                  * Then, if they do, we need to erase each of them from our IdUserMap (iduser) so when the module that
117                  * associated them asks to look them up then it gets a NULL result and knows to discard the query.
118                  */
119                 AssocIdList* il;
120                 
121                 if(user->GetExt("sqlutils_queryids", il))
122                 {
123                         for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
124                         {
125                                 IdUserMap::iterator iter;
126                         
127                                 iter = iduser.find(*listiter);
128                         
129                                 if(iter != iduser.end())
130                                 {
131                                         if(iter->second != user)
132                                         {
133                                                 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);
134                                         }
135
136                                         iduser.erase(iter);
137                                 }
138                                 else
139                                 {
140                                         ServerInstance->Log(DEBUG, "BUG: user %s was extended with sqlutils_queryids but there was nothing matching in the map", user->nick);
141                                 }
142                         }
143                         
144                         user->Shrink("sqlutils_queryids");
145                         delete il;
146                 }
147         }
148         
149         void AttachList(Extensible* obj, unsigned long id)
150         {
151                 AssocIdList* il;
152                 
153                 if(!obj->GetExt("sqlutils_queryids", il))
154                 {
155                         /* Doesn't already exist, create a new list and attach it. */
156                         il = new AssocIdList;
157                         obj->Extend("sqlutils_queryids", il);
158                 }
159                 
160                 /* Now either way we have a valid list in il, attached. */
161                 il->push_back(id);
162         }
163         
164         void RemoveFromList(Extensible* obj, unsigned long id)
165         {
166                 AssocIdList* il;
167                 
168                 if(obj->GetExt("sqlutils_queryids", il))
169                 {
170                         /* Only do anything if the list exists... (which it ought to) */
171                         il->remove(id);
172                         
173                         if(il->empty())
174                         {
175                                 /* If we just emptied it.. */
176                                 delete il;
177                                 obj->Shrink("sqlutils_queryids");
178                         }
179                 }
180         }
181         
182         template <class T> void DoUnAssociate(T &map, unsigned long id)
183         {
184                 /* For each occurence of 'id' (well, only one..it's not a multimap) in 'map'
185                  * remove it from the map, take an Extensible* value from the map and remove
186                  * 'id' from the list of query IDs attached to it.
187                  */
188                 typename T::iterator iter = map.find(id);
189                 
190                 if(iter != map.end())
191                 {
192                         /* Found a value indexed by 'id', call RemoveFromList()
193                          * on it with 'id' to remove 'id' from the list attached
194                          * to the value.
195                          */
196                         RemoveFromList(iter->second, id);
197                 }
198         }
199         
200         virtual void OnChannelDelete(chanrec* chan)
201         {
202                 /* A channel is being destroyed, first we need to check if it has a list of queries associated with it.
203                  * Then, if it does, we need to erase each of them from our IdChanMap (idchan) so when the module that
204                  * associated them asks to look them up then it gets a NULL result and knows to discard the query.
205                  */
206                 AssocIdList* il;
207                 
208                 if(chan->GetExt("sqlutils_queryids", il))
209                 {
210                         for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
211                         {
212                                 IdChanMap::iterator iter;
213                         
214                                 iter = idchan.find(*listiter);
215                         
216                                 if(iter != idchan.end())
217                                 {
218                                         if(iter->second != chan)
219                                         {
220                                                 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);
221                                         }
222                                         idchan.erase(iter);                                     
223                                 }
224                                 else
225                                 {
226                                         ServerInstance->Log(DEBUG, "BUG: channel %s was extended with sqlutils_queryids but there was nothing matching in the map", chan->name);
227                                 }
228                         }
229                         
230                         chan->Shrink("sqlutils_queryids");
231                         delete il;
232                 }
233         }
234                         
235         virtual Version GetVersion()
236         {
237                 return Version(1, 1, 0, 0, VF_VENDOR|VF_SERVICEPROVIDER, API_VERSION);
238         }
239         
240 };
241
242 class ModuleSQLutilsFactory : public ModuleFactory
243 {
244  public:
245         ModuleSQLutilsFactory()
246         {
247         }
248         
249         ~ModuleSQLutilsFactory()
250         {
251         }
252         
253         virtual Module * CreateModule(InspIRCd* Me)
254         {
255                 return new ModuleSQLutils(Me);
256         }
257 };
258
259
260 extern "C" void * init_module( void )
261 {
262         return new ModuleSQLutilsFactory;
263 }