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