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 * ---------------------------------------------------
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 */
36 typedef std::map<unsigned long, userrec*> IdUserMap;
37 typedef std::map<unsigned long, chanrec*> IdChanMap;
38 typedef std::list<unsigned long> AssocIdList;
40 class ModuleSQLutils : public Module
49 ModuleSQLutils(InspIRCd* Me)
52 ServerInstance->Log(DEBUG, "%s 'SQLutils' feature", ServerInstance->PublishFeature("SQLutils", this) ? "Published" : "Couldn't publish");
55 void Implements(char* List)
57 List[I_OnChannelDelete] = List[I_OnUnloadModule] = List[I_OnRequest] = List[I_OnUserDisconnect] = 1;
60 virtual char* OnRequest(Request* request)
62 if(strcmp(SQLUTILAU, request->GetId()) == 0)
64 AssociateUser* req = (AssociateUser*)request;
66 ServerInstance->Log(DEBUG, "Associated ID %lu with user %s", req->id, req->user->nick);
68 iduser.insert(std::make_pair(req->id, req->user));
70 AttachList(req->user, req->id);
72 else if(strcmp(SQLUTILAC, request->GetId()) == 0)
74 AssociateChan* req = (AssociateChan*)request;
76 ServerInstance->Log(DEBUG, "Associated ID %lu with channel %s", req->id, req->chan->name);
78 idchan.insert(std::make_pair(req->id, req->chan));
80 AttachList(req->chan, req->id);
82 else if(strcmp(SQLUTILUA, request->GetId()) == 0)
84 UnAssociate* req = (UnAssociate*)request;
86 /* Unassociate a given query ID with all users and channels
87 * it is associated with.
90 ServerInstance->Log(DEBUG, "Unassociating ID %lu with all users and channels", req->id);
92 DoUnAssociate(iduser, req->id);
93 DoUnAssociate(idchan, req->id);
95 else if(strcmp(SQLUTILGU, request->GetId()) == 0)
97 GetAssocUser* req = (GetAssocUser*)request;
99 IdUserMap::iterator iter = iduser.find(req->id);
101 ServerInstance->Log(DEBUG, "Looking up user associated with ID %lu", req->id);
103 if(iter != iduser.end())
105 ServerInstance->Log(DEBUG, "Found user %s", iter->second->nick);
106 req->user = iter->second;
109 else if(strcmp(SQLUTILGC, request->GetId()) == 0)
111 GetAssocChan* req = (GetAssocChan*)request;
113 IdChanMap::iterator iter = idchan.find(req->id);
115 ServerInstance->Log(DEBUG, "Looking up channel associated with ID %lu", req->id);
117 if(iter != idchan.end())
119 ServerInstance->Log(DEBUG, "Found channel %s", iter->second->name);
120 req->chan = iter->second;
125 ServerInstance->Log(DEBUG, "Got unsupported API version string: %s", request->GetId());
129 return SQLUTILSUCCESS;
132 virtual void OnUserDisconnect(userrec* user)
134 /* A user is disconnecting, first we need to check if they have a list of queries associated with them.
135 * Then, if they do, we need to erase each of them from our IdUserMap (iduser) so when the module that
136 * associated them asks to look them up then it gets a NULL result and knows to discard the query.
140 if(user->GetExt("sqlutils_queryids", il))
142 for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
144 IdUserMap::iterator iter;
146 iter = iduser.find(*listiter);
148 if(iter != iduser.end())
150 if(iter->second == user)
152 ServerInstance->Log(DEBUG, "Erased query from map associated with quitting user %s", user->nick);
156 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);
163 ServerInstance->Log(DEBUG, "BUG: user %s was extended with sqlutils_queryids but there was nothing matching in the map", user->nick);
167 user->Shrink("sqlutils_queryids");
172 void AttachList(Extensible* obj, unsigned long id)
176 if(!obj->GetExt("sqlutils_queryids", il))
178 /* Doesn't already exist, create a new list and attach it. */
179 il = new AssocIdList;
180 obj->Extend("sqlutils_queryids", il);
183 /* Now either way we have a valid list in il, attached. */
187 void RemoveFromList(Extensible* obj, unsigned long id)
191 if(obj->GetExt("sqlutils_queryids", il))
193 /* Only do anything if the list exists... (which it ought to) */
198 /* If we just emptied it.. */
200 obj->Shrink("sqlutils_queryids");
205 template <class T> void DoUnAssociate(T &map, unsigned long id)
207 /* For each occurence of 'id' (well, only one..it's not a multimap) in 'map'
208 * remove it from the map, take an Extensible* value from the map and remove
209 * 'id' from the list of query IDs attached to it.
211 typename T::iterator iter = map.find(id);
213 if(iter != map.end())
215 /* Found a value indexed by 'id', call RemoveFromList()
216 * on it with 'id' to remove 'id' from the list attached
219 RemoveFromList(iter->second, id);
221 ServerInstance->Log(DEBUG, "Removed query %lu from map and removed references to it on value", id);
225 ServerInstance->Log(DEBUG, "Nothing associated with query %lu", id);
229 virtual void OnChannelDelete(chanrec* chan)
231 /* A channel is being destroyed, first we need to check if it has a list of queries associated with it.
232 * Then, if it does, we need to erase each of them from our IdChanMap (idchan) so when the module that
233 * associated them asks to look them up then it gets a NULL result and knows to discard the query.
237 if(chan->GetExt("sqlutils_queryids", il))
239 for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
241 IdChanMap::iterator iter;
243 iter = idchan.find(*listiter);
245 if(iter != idchan.end())
247 if(iter->second == chan)
249 ServerInstance->Log(DEBUG, "Erased query from map associated with dying channnel %s", chan->name);
253 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);
260 ServerInstance->Log(DEBUG, "BUG: channel %s was extended with sqlutils_queryids but there was nothing matching in the map", chan->name);
264 chan->Shrink("sqlutils_queryids");
269 virtual Version GetVersion()
271 return Version(1, 0, 0, 0, VF_STATIC|VF_VENDOR|VF_SERVICEPROVIDER);
274 virtual ~ModuleSQLutils()
279 class ModuleSQLutilsFactory : public ModuleFactory
282 ModuleSQLutilsFactory()
286 ~ModuleSQLutilsFactory()
290 virtual Module * CreateModule(InspIRCd* Me)
292 return new ModuleSQLutils(Me);
297 extern "C" void * init_module( void )
299 return new ModuleSQLutilsFactory;