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 * ---------------------------------------------------
20 #include "configreader.h"
21 #include "m_sqlutils.h"
23 /* $ModDesc: Provides some utilities to SQL client modules, such as mapping queries to users and channels */
24 /* $ModDep: m_sqlutils.h */
26 typedef std::map<unsigned long, userrec*> IdUserMap;
27 typedef std::map<unsigned long, chanrec*> IdChanMap;
28 typedef std::list<unsigned long> AssocIdList;
30 class ModuleSQLutils : public Module
37 ModuleSQLutils(InspIRCd* Me)
40 ServerInstance->PublishInterface("SQLutils", this);
43 virtual ~ModuleSQLutils()
45 ServerInstance->UnpublishInterface("SQLutils", this);
48 void Implements(char* List)
50 List[I_OnChannelDelete] = List[I_OnUnloadModule] = List[I_OnRequest] = List[I_OnUserDisconnect] = 1;
53 virtual char* OnRequest(Request* request)
55 if(strcmp(SQLUTILAU, request->GetId()) == 0)
57 AssociateUser* req = (AssociateUser*)request;
59 iduser.insert(std::make_pair(req->id, req->user));
61 AttachList(req->user, req->id);
63 else if(strcmp(SQLUTILAC, request->GetId()) == 0)
65 AssociateChan* req = (AssociateChan*)request;
67 idchan.insert(std::make_pair(req->id, req->chan));
69 AttachList(req->chan, req->id);
71 else if(strcmp(SQLUTILUA, request->GetId()) == 0)
73 UnAssociate* req = (UnAssociate*)request;
75 /* Unassociate a given query ID with all users and channels
76 * it is associated with.
79 DoUnAssociate(iduser, req->id);
80 DoUnAssociate(idchan, req->id);
82 else if(strcmp(SQLUTILGU, request->GetId()) == 0)
84 GetAssocUser* req = (GetAssocUser*)request;
86 IdUserMap::iterator iter = iduser.find(req->id);
88 if(iter != iduser.end())
90 req->user = iter->second;
93 else if(strcmp(SQLUTILGC, request->GetId()) == 0)
95 GetAssocChan* req = (GetAssocChan*)request;
97 IdChanMap::iterator iter = idchan.find(req->id);
99 if(iter != idchan.end())
101 req->chan = iter->second;
105 return SQLUTILSUCCESS;
108 virtual void OnUserDisconnect(userrec* user)
110 /* A user is disconnecting, first we need to check if they have a list of queries associated with them.
111 * Then, if they do, we need to erase each of them from our IdUserMap (iduser) so when the module that
112 * associated them asks to look them up then it gets a NULL result and knows to discard the query.
116 if(user->GetExt("sqlutils_queryids", il))
118 for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
120 IdUserMap::iterator iter;
122 iter = iduser.find(*listiter);
124 if(iter != iduser.end())
126 if(iter->second != user)
128 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);
135 ServerInstance->Log(DEBUG, "BUG: user %s was extended with sqlutils_queryids but there was nothing matching in the map", user->nick);
139 user->Shrink("sqlutils_queryids");
144 void AttachList(Extensible* obj, unsigned long id)
148 if(!obj->GetExt("sqlutils_queryids", il))
150 /* Doesn't already exist, create a new list and attach it. */
151 il = new AssocIdList;
152 obj->Extend("sqlutils_queryids", il);
155 /* Now either way we have a valid list in il, attached. */
159 void RemoveFromList(Extensible* obj, unsigned long id)
163 if(obj->GetExt("sqlutils_queryids", il))
165 /* Only do anything if the list exists... (which it ought to) */
170 /* If we just emptied it.. */
172 obj->Shrink("sqlutils_queryids");
177 template <class T> void DoUnAssociate(T &map, unsigned long id)
179 /* For each occurence of 'id' (well, only one..it's not a multimap) in 'map'
180 * remove it from the map, take an Extensible* value from the map and remove
181 * 'id' from the list of query IDs attached to it.
183 typename T::iterator iter = map.find(id);
185 if(iter != map.end())
187 /* Found a value indexed by 'id', call RemoveFromList()
188 * on it with 'id' to remove 'id' from the list attached
191 RemoveFromList(iter->second, id);
195 virtual void OnChannelDelete(chanrec* chan)
197 /* A channel is being destroyed, first we need to check if it has a list of queries associated with it.
198 * Then, if it does, we need to erase each of them from our IdChanMap (idchan) so when the module that
199 * associated them asks to look them up then it gets a NULL result and knows to discard the query.
203 if(chan->GetExt("sqlutils_queryids", il))
205 for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
207 IdChanMap::iterator iter;
209 iter = idchan.find(*listiter);
211 if(iter != idchan.end())
213 if(iter->second != chan)
215 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);
221 ServerInstance->Log(DEBUG, "BUG: channel %s was extended with sqlutils_queryids but there was nothing matching in the map", chan->name);
225 chan->Shrink("sqlutils_queryids");
230 virtual Version GetVersion()
232 return Version(1, 1, 0, 0, VF_VENDOR|VF_SERVICEPROVIDER, API_VERSION);
237 class ModuleSQLutilsFactory : public ModuleFactory
240 ModuleSQLutilsFactory()
244 ~ModuleSQLutilsFactory()
248 virtual Module * CreateModule(InspIRCd* Me)
250 return new ModuleSQLutils(Me);
255 extern "C" void * init_module( void )
257 return new ModuleSQLutilsFactory;