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