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