1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
24 #include "configreader.h"
26 #include "m_sqlutils.h"
28 /* $ModDesc: Provides some utilities to SQL client modules, such as mapping queries to users and channels */
29 /* $ModDep: m_sqlutils.h */
31 typedef std::map<unsigned long, userrec*> IdUserMap;
32 typedef std::map<unsigned long, chanrec*> IdChanMap;
33 typedef std::list<unsigned long> AssocIdList;
35 class ModuleSQLutils : public Module
44 ModuleSQLutils(InspIRCd* Me)
47 ServerInstance->Log(DEBUG, "%s 'SQLutils' feature", ServerInstance->PublishFeature("SQLutils", this) ? "Published" : "Couldn't publish");
50 void Implements(char* List)
52 List[I_OnChannelDelete] = List[I_OnUnloadModule] = List[I_OnRequest] = List[I_OnUserDisconnect] = 1;
55 virtual char* OnRequest(Request* request)
57 if(strcmp(SQLUTILAU, request->GetId()) == 0)
59 AssociateUser* req = (AssociateUser*)request;
61 ServerInstance->Log(DEBUG, "Associated ID %lu with user %s", req->id, req->user->nick);
63 iduser.insert(std::make_pair(req->id, req->user));
65 AttachList(req->user, req->id);
67 else if(strcmp(SQLUTILAC, request->GetId()) == 0)
69 AssociateChan* req = (AssociateChan*)request;
71 ServerInstance->Log(DEBUG, "Associated ID %lu with channel %s", req->id, req->chan->name);
73 idchan.insert(std::make_pair(req->id, req->chan));
75 AttachList(req->chan, req->id);
77 else if(strcmp(SQLUTILUA, request->GetId()) == 0)
79 UnAssociate* req = (UnAssociate*)request;
81 /* Unassociate a given query ID with all users and channels
82 * it is associated with.
85 ServerInstance->Log(DEBUG, "Unassociating ID %lu with all users and channels", req->id);
87 DoUnAssociate(iduser, req->id);
88 DoUnAssociate(idchan, req->id);
90 else if(strcmp(SQLUTILGU, request->GetId()) == 0)
92 GetAssocUser* req = (GetAssocUser*)request;
94 IdUserMap::iterator iter = iduser.find(req->id);
96 ServerInstance->Log(DEBUG, "Looking up user associated with ID %lu", req->id);
98 if(iter != iduser.end())
100 ServerInstance->Log(DEBUG, "Found user %s", iter->second->nick);
101 req->user = iter->second;
104 else if(strcmp(SQLUTILGC, request->GetId()) == 0)
106 GetAssocChan* req = (GetAssocChan*)request;
108 IdChanMap::iterator iter = idchan.find(req->id);
110 ServerInstance->Log(DEBUG, "Looking up channel associated with ID %lu", req->id);
112 if(iter != idchan.end())
114 ServerInstance->Log(DEBUG, "Found channel %s", iter->second->name);
115 req->chan = iter->second;
120 ServerInstance->Log(DEBUG, "Got unsupported API version string: %s", request->GetId());
124 return SQLUTILSUCCESS;
127 virtual void OnUserDisconnect(userrec* user)
129 /* A user is disconnecting, first we need to check if they have a list of queries associated with them.
130 * Then, if they do, we need to erase each of them from our IdUserMap (iduser) so when the module that
131 * associated them asks to look them up then it gets a NULL result and knows to discard the query.
135 if(user->GetExt("sqlutils_queryids", il))
137 for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
139 IdUserMap::iterator iter;
141 iter = iduser.find(*listiter);
143 if(iter != iduser.end())
145 if(iter->second == user)
147 ServerInstance->Log(DEBUG, "Erased query from map associated with quitting user %s", user->nick);
151 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);
158 ServerInstance->Log(DEBUG, "BUG: user %s was extended with sqlutils_queryids but there was nothing matching in the map", user->nick);
162 user->Shrink("sqlutils_queryids");
167 void AttachList(Extensible* obj, unsigned long id)
171 if(!obj->GetExt("sqlutils_queryids", il))
173 /* Doesn't already exist, create a new list and attach it. */
174 il = new AssocIdList;
175 obj->Extend("sqlutils_queryids", il);
178 /* Now either way we have a valid list in il, attached. */
182 void RemoveFromList(Extensible* obj, unsigned long id)
186 if(obj->GetExt("sqlutils_queryids", il))
188 /* Only do anything if the list exists... (which it ought to) */
193 /* If we just emptied it.. */
195 obj->Shrink("sqlutils_queryids");
200 template <class T> void DoUnAssociate(T &map, unsigned long id)
202 /* For each occurence of 'id' (well, only one..it's not a multimap) in 'map'
203 * remove it from the map, take an Extensible* value from the map and remove
204 * 'id' from the list of query IDs attached to it.
206 typename T::iterator iter = map.find(id);
208 if(iter != map.end())
210 /* Found a value indexed by 'id', call RemoveFromList()
211 * on it with 'id' to remove 'id' from the list attached
214 RemoveFromList(iter->second, id);
216 ServerInstance->Log(DEBUG, "Removed query %lu from map and removed references to it on value", id);
220 ServerInstance->Log(DEBUG, "Nothing associated with query %lu", id);
224 virtual void OnChannelDelete(chanrec* chan)
226 /* A channel is being destroyed, first we need to check if it has a list of queries associated with it.
227 * Then, if it does, we need to erase each of them from our IdChanMap (idchan) so when the module that
228 * associated them asks to look them up then it gets a NULL result and knows to discard the query.
232 if(chan->GetExt("sqlutils_queryids", il))
234 for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
236 IdChanMap::iterator iter;
238 iter = idchan.find(*listiter);
240 if(iter != idchan.end())
242 if(iter->second == chan)
244 ServerInstance->Log(DEBUG, "Erased query from map associated with dying channnel %s", chan->name);
248 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);
255 ServerInstance->Log(DEBUG, "BUG: channel %s was extended with sqlutils_queryids but there was nothing matching in the map", chan->name);
259 chan->Shrink("sqlutils_queryids");
264 virtual Version GetVersion()
266 return Version(1, 1, 0, 0, VF_STATIC|VF_VENDOR|VF_SERVICEPROVIDER, API_VERSION);
269 virtual ~ModuleSQLutils()
274 class ModuleSQLutilsFactory : public ModuleFactory
277 ModuleSQLutilsFactory()
281 ~ModuleSQLutilsFactory()
285 virtual Module * CreateModule(InspIRCd* Me)
287 return new ModuleSQLutils(Me);
292 extern "C" void * init_module( void )
294 return new ModuleSQLutilsFactory;