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