1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
11 * Written by Craig Edwards, Craig McLure, and others.
12 * This program is free but copyrighted software; see
13 * the file COPYING for details.
15 * ---------------------------------------------------
26 #include "helperfuncs.h"
28 #include "configreader.h"
30 #include "m_sqlutils.h"
32 /* $ModDesc: Provides some utilities to SQL client modules, such as mapping queries to users and channels */
34 typedef std::map<unsigned long, userrec*> IdUserMap;
35 typedef std::map<unsigned long, chanrec*> IdChanMap;
36 typedef std::list<unsigned long> AssocIdList;
38 class ModuleSQLutils : public Module
47 ModuleSQLutils(Server* Me)
48 : Module::Module(Me), Srv(Me)
50 log(DEBUG, "%s 'SQLutils' feature", Srv->PublishFeature("SQLutils", this) ? "Published" : "Couldn't publish");
53 void Implements(char* List)
55 List[I_OnChannelDelete] = List[I_OnUnloadModule] = List[I_OnRequest] = List[I_OnUserDisconnect] = 1;
58 virtual char* OnRequest(Request* request)
60 if(strcmp(SQLUTILAU, request->GetData()) == 0)
62 AssociateUser* req = (AssociateUser*)request;
64 log(DEBUG, "Associated ID %lu with user %s", req->id, req->user->nick);
66 iduser.insert(std::make_pair(req->id, req->user));
68 AttachList(req->user, req->id);
70 else if(strcmp(SQLUTILAC, request->GetData()) == 0)
72 AssociateChan* req = (AssociateChan*)request;
74 log(DEBUG, "Associated ID %lu with channel %s", req->id, req->chan->name);
76 idchan.insert(std::make_pair(req->id, req->chan));
78 AttachList(req->chan, req->id);
80 else if(strcmp(SQLUTILUA, request->GetData()) == 0)
82 UnAssociate* req = (UnAssociate*)request;
84 /* Unassociate a given query ID with all users and channels
85 * it is associated with.
88 log(DEBUG, "Unassociating ID %lu with all users and channels", req->id);
90 DoUnAssociate(iduser, req->id);
91 DoUnAssociate(idchan, req->id);
93 else if(strcmp(SQLUTILGU, request->GetData()) == 0)
95 GetAssocUser* req = (GetAssocUser*)request;
97 IdUserMap::iterator iter = iduser.find(req->id);
99 log(DEBUG, "Looking up user associated with ID %lu", req->id);
101 if(iter != iduser.end())
103 log(DEBUG, "Found user %s", iter->second->nick);
104 req->user = iter->second;
107 else if(strcmp(SQLUTILGC, request->GetData()) == 0)
109 GetAssocChan* req = (GetAssocChan*)request;
111 IdChanMap::iterator iter = idchan.find(req->id);
113 log(DEBUG, "Looking up channel associated with ID %lu", req->id);
115 if(iter != idchan.end())
117 log(DEBUG, "Found channel %s", iter->second->name);
118 req->chan = iter->second;
123 log(DEBUG, "Got unsupported API version string: %s", request->GetData());
127 return SQLUTILSUCCESS;
130 virtual void OnUserDisconnect(userrec* user)
132 /* A user is disconnecting, first we need to check if they have a list of queries associated with them.
133 * Then, if they do, we need to erase each of them from our IdUserMap (iduser) so when the module that
134 * associated them asks to look them up then it gets a NULL result and knows to discard the query.
138 if(user->GetExt("sqlutils_queryids", il))
140 for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
142 IdUserMap::iterator iter;
144 iter = iduser.find(*listiter);
146 if(iter != iduser.end())
148 if(iter->second == user)
150 log(DEBUG, "Erased query from map associated with quitting user %s", user->nick);
154 log(DEBUG, "BUG: ID associated with user %s doesn't have the same userrec* associated with it in the map (erasing anyway)", user->nick);
161 log(DEBUG, "BUG: user %s was extended with sqlutils_queryids but there was nothing matching in the map", user->nick);
165 user->Shrink("sqlutils_queryids");
170 void AttachList(Extensible* obj, unsigned long id)
174 if(!obj->GetExt("sqlutils_queryids", il))
176 /* Doesn't already exist, create a new list and attach it. */
177 il = new AssocIdList;
178 obj->Extend("sqlutils_queryids", il);
181 /* Now either way we have a valid list in il, attached. */
185 void RemoveFromList(Extensible* obj, unsigned long id)
189 if(obj->GetExt("sqlutils_queryids", il))
191 /* Only do anything if the list exists... (which it ought to) */
196 /* If we just emptied it.. */
198 obj->Shrink("sqlutils_queryids");
203 template <class T> void DoUnAssociate(T &map, unsigned long id)
205 /* For each occurence of 'id' (well, only one..it's not a multimap) in 'map'
206 * remove it from the map, take an Extensible* value from the map and remove
207 * 'id' from the list of query IDs attached to it.
209 typename T::iterator iter = map.find(id);
211 if(iter != map.end())
213 /* Found a value indexed by 'id', call RemoveFromList()
214 * on it with 'id' to remove 'id' from the list attached
217 RemoveFromList(iter->second, id);
219 log(DEBUG, "Removed query %lu from map and removed references to it on value", id);
223 log(DEBUG, "Nothing associated with query %lu", id);
227 virtual void OnChannelDelete(chanrec* chan)
229 /* A channel is being destroyed, first we need to check if it has a list of queries associated with it.
230 * Then, if it does, we need to erase each of them from our IdChanMap (idchan) so when the module that
231 * associated them asks to look them up then it gets a NULL result and knows to discard the query.
235 if(chan->GetExt("sqlutils_queryids", il))
237 for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
239 IdChanMap::iterator iter;
241 iter = idchan.find(*listiter);
243 if(iter != idchan.end())
245 if(iter->second == chan)
247 log(DEBUG, "Erased query from map associated with dying channnel %s", chan->name);
251 log(DEBUG, "BUG: ID associated with channel %s doesn't have the same chanrec* associated with it in the map (erasing anyway)", chan->name);
258 log(DEBUG, "BUG: channel %s was extended with sqlutils_queryids but there was nothing matching in the map", chan->name);
262 chan->Shrink("sqlutils_queryids");
267 virtual Version GetVersion()
269 return Version(1, 0, 0, 0, VF_STATIC|VF_VENDOR|VF_SERVICEPROVIDER);
272 virtual ~ModuleSQLutils()
277 class ModuleSQLutilsFactory : public ModuleFactory
280 ModuleSQLutilsFactory()
284 ~ModuleSQLutilsFactory()
288 virtual Module * CreateModule(Server* Me)
290 return new ModuleSQLutils(Me);
295 extern "C" void * init_module( void )
297 return new ModuleSQLutilsFactory;