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
42 ModuleSQLutils(InspIRCd* Me)
45 ServerInstance->PublishInterface("SQLutils", this);
48 virtual ~ModuleSQLutils()
50 ServerInstance->UnpublishInterface("SQLutils", this);
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->GetId()) == 0)
62 AssociateUser* req = (AssociateUser*)request;
64 ServerInstance->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->GetId()) == 0)
72 AssociateChan* req = (AssociateChan*)request;
74 ServerInstance->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->GetId()) == 0)
82 UnAssociate* req = (UnAssociate*)request;
84 /* Unassociate a given query ID with all users and channels
85 * it is associated with.
88 ServerInstance->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->GetId()) == 0)
95 GetAssocUser* req = (GetAssocUser*)request;
97 IdUserMap::iterator iter = iduser.find(req->id);
99 ServerInstance->Log(DEBUG, "Looking up user associated with ID %lu", req->id);
101 if(iter != iduser.end())
103 ServerInstance->Log(DEBUG, "Found user %s", iter->second->nick);
104 req->user = iter->second;
107 else if(strcmp(SQLUTILGC, request->GetId()) == 0)
109 GetAssocChan* req = (GetAssocChan*)request;
111 IdChanMap::iterator iter = idchan.find(req->id);
113 ServerInstance->Log(DEBUG, "Looking up channel associated with ID %lu", req->id);
115 if(iter != idchan.end())
117 ServerInstance->Log(DEBUG, "Found channel %s", iter->second->name);
118 req->chan = iter->second;
123 ServerInstance->Log(DEBUG, "Got unsupported API version string: %s", request->GetId());
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 ServerInstance->Log(DEBUG, "Erased query from map associated with quitting user %s", user->nick);
154 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);
161 ServerInstance->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 ServerInstance->Log(DEBUG, "Removed query %lu from map and removed references to it on value", id);
223 ServerInstance->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 ServerInstance->Log(DEBUG, "Erased query from map associated with dying channnel %s", chan->name);
251 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);
258 ServerInstance->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, 1, 0, 0, VF_VENDOR|VF_SERVICEPROVIDER, API_VERSION);
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;