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