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 */
33 /* $ModDep: m_sqlutils.h */
35 typedef std::map<unsigned long, userrec*> IdUserMap;
36 typedef std::map<unsigned long, chanrec*> IdChanMap;
37 typedef std::list<unsigned long> AssocIdList;
39 class ModuleSQLutils : public Module
48 ModuleSQLutils(InspIRCd* Me)
51 ServerInstance->Log(DEBUG, "%s 'SQLutils' feature", ServerInstance->PublishFeature("SQLutils", this) ? "Published" : "Couldn't publish");
54 void Implements(char* List)
56 List[I_OnChannelDelete] = List[I_OnUnloadModule] = List[I_OnRequest] = List[I_OnUserDisconnect] = 1;
59 virtual char* OnRequest(Request* request)
61 if(strcmp(SQLUTILAU, request->GetId()) == 0)
63 AssociateUser* req = (AssociateUser*)request;
65 ServerInstance->Log(DEBUG, "Associated ID %lu with user %s", req->id, req->user->nick);
67 iduser.insert(std::make_pair(req->id, req->user));
69 AttachList(req->user, req->id);
71 else if(strcmp(SQLUTILAC, request->GetId()) == 0)
73 AssociateChan* req = (AssociateChan*)request;
75 ServerInstance->Log(DEBUG, "Associated ID %lu with channel %s", req->id, req->chan->name);
77 idchan.insert(std::make_pair(req->id, req->chan));
79 AttachList(req->chan, req->id);
81 else if(strcmp(SQLUTILUA, request->GetId()) == 0)
83 UnAssociate* req = (UnAssociate*)request;
85 /* Unassociate a given query ID with all users and channels
86 * it is associated with.
89 ServerInstance->Log(DEBUG, "Unassociating ID %lu with all users and channels", req->id);
91 DoUnAssociate(iduser, req->id);
92 DoUnAssociate(idchan, req->id);
94 else if(strcmp(SQLUTILGU, request->GetId()) == 0)
96 GetAssocUser* req = (GetAssocUser*)request;
98 IdUserMap::iterator iter = iduser.find(req->id);
100 ServerInstance->Log(DEBUG, "Looking up user associated with ID %lu", req->id);
102 if(iter != iduser.end())
104 ServerInstance->Log(DEBUG, "Found user %s", iter->second->nick);
105 req->user = iter->second;
108 else if(strcmp(SQLUTILGC, request->GetId()) == 0)
110 GetAssocChan* req = (GetAssocChan*)request;
112 IdChanMap::iterator iter = idchan.find(req->id);
114 ServerInstance->Log(DEBUG, "Looking up channel associated with ID %lu", req->id);
116 if(iter != idchan.end())
118 ServerInstance->Log(DEBUG, "Found channel %s", iter->second->name);
119 req->chan = iter->second;
124 ServerInstance->Log(DEBUG, "Got unsupported API version string: %s", request->GetId());
128 return SQLUTILSUCCESS;
131 virtual void OnUserDisconnect(userrec* user)
133 /* A user is disconnecting, first we need to check if they have a list of queries associated with them.
134 * Then, if they do, we need to erase each of them from our IdUserMap (iduser) so when the module that
135 * associated them asks to look them up then it gets a NULL result and knows to discard the query.
139 if(user->GetExt("sqlutils_queryids", il))
141 for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
143 IdUserMap::iterator iter;
145 iter = iduser.find(*listiter);
147 if(iter != iduser.end())
149 if(iter->second == user)
151 ServerInstance->Log(DEBUG, "Erased query from map associated with quitting user %s", user->nick);
155 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);
162 ServerInstance->Log(DEBUG, "BUG: user %s was extended with sqlutils_queryids but there was nothing matching in the map", user->nick);
166 user->Shrink("sqlutils_queryids");
171 void AttachList(Extensible* obj, unsigned long id)
175 if(!obj->GetExt("sqlutils_queryids", il))
177 /* Doesn't already exist, create a new list and attach it. */
178 il = new AssocIdList;
179 obj->Extend("sqlutils_queryids", il);
182 /* Now either way we have a valid list in il, attached. */
186 void RemoveFromList(Extensible* obj, unsigned long id)
190 if(obj->GetExt("sqlutils_queryids", il))
192 /* Only do anything if the list exists... (which it ought to) */
197 /* If we just emptied it.. */
199 obj->Shrink("sqlutils_queryids");
204 template <class T> void DoUnAssociate(T &map, unsigned long id)
206 /* For each occurence of 'id' (well, only one..it's not a multimap) in 'map'
207 * remove it from the map, take an Extensible* value from the map and remove
208 * 'id' from the list of query IDs attached to it.
210 typename T::iterator iter = map.find(id);
212 if(iter != map.end())
214 /* Found a value indexed by 'id', call RemoveFromList()
215 * on it with 'id' to remove 'id' from the list attached
218 RemoveFromList(iter->second, id);
220 ServerInstance->Log(DEBUG, "Removed query %lu from map and removed references to it on value", id);
224 ServerInstance->Log(DEBUG, "Nothing associated with query %lu", id);
228 virtual void OnChannelDelete(chanrec* chan)
230 /* A channel is being destroyed, first we need to check if it has a list of queries associated with it.
231 * Then, if it does, we need to erase each of them from our IdChanMap (idchan) so when the module that
232 * associated them asks to look them up then it gets a NULL result and knows to discard the query.
236 if(chan->GetExt("sqlutils_queryids", il))
238 for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
240 IdChanMap::iterator iter;
242 iter = idchan.find(*listiter);
244 if(iter != idchan.end())
246 if(iter->second == chan)
248 ServerInstance->Log(DEBUG, "Erased query from map associated with dying channnel %s", chan->name);
252 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);
259 ServerInstance->Log(DEBUG, "BUG: channel %s was extended with sqlutils_queryids but there was nothing matching in the map", chan->name);
263 chan->Shrink("sqlutils_queryids");
268 virtual Version GetVersion()
270 return Version(1, 1, 0, 0, VF_STATIC|VF_VENDOR|VF_SERVICEPROVIDER, API_VERSION);
273 virtual ~ModuleSQLutils()
278 class ModuleSQLutilsFactory : public ModuleFactory
281 ModuleSQLutilsFactory()
285 ~ModuleSQLutilsFactory()
289 virtual Module * CreateModule(InspIRCd* Me)
291 return new ModuleSQLutils(Me);
296 extern "C" void * init_module( void )
298 return new ModuleSQLutilsFactory;