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 iduser.insert(std::make_pair(req->id, req->user));
66 AttachList(req->user, req->id);
68 else if(strcmp(SQLUTILAC, request->GetId()) == 0)
70 AssociateChan* req = (AssociateChan*)request;
72 idchan.insert(std::make_pair(req->id, req->chan));
74 AttachList(req->chan, req->id);
76 else if(strcmp(SQLUTILUA, request->GetId()) == 0)
78 UnAssociate* req = (UnAssociate*)request;
80 /* Unassociate a given query ID with all users and channels
81 * it is associated with.
84 DoUnAssociate(iduser, req->id);
85 DoUnAssociate(idchan, req->id);
87 else if(strcmp(SQLUTILGU, request->GetId()) == 0)
89 GetAssocUser* req = (GetAssocUser*)request;
91 IdUserMap::iterator iter = iduser.find(req->id);
93 if(iter != iduser.end())
95 req->user = iter->second;
98 else if(strcmp(SQLUTILGC, request->GetId()) == 0)
100 GetAssocChan* req = (GetAssocChan*)request;
102 IdChanMap::iterator iter = idchan.find(req->id);
104 if(iter != idchan.end())
106 req->chan = iter->second;
110 return SQLUTILSUCCESS;
113 virtual void OnUserDisconnect(userrec* user)
115 /* A user is disconnecting, first we need to check if they have a list of queries associated with them.
116 * Then, if they do, we need to erase each of them from our IdUserMap (iduser) so when the module that
117 * associated them asks to look them up then it gets a NULL result and knows to discard the query.
121 if(user->GetExt("sqlutils_queryids", il))
123 for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
125 IdUserMap::iterator iter;
127 iter = iduser.find(*listiter);
129 if(iter != iduser.end())
131 if(iter->second != user)
133 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);
140 ServerInstance->Log(DEBUG, "BUG: user %s was extended with sqlutils_queryids but there was nothing matching in the map", user->nick);
144 user->Shrink("sqlutils_queryids");
149 void AttachList(Extensible* obj, unsigned long id)
153 if(!obj->GetExt("sqlutils_queryids", il))
155 /* Doesn't already exist, create a new list and attach it. */
156 il = new AssocIdList;
157 obj->Extend("sqlutils_queryids", il);
160 /* Now either way we have a valid list in il, attached. */
164 void RemoveFromList(Extensible* obj, unsigned long id)
168 if(obj->GetExt("sqlutils_queryids", il))
170 /* Only do anything if the list exists... (which it ought to) */
175 /* If we just emptied it.. */
177 obj->Shrink("sqlutils_queryids");
182 template <class T> void DoUnAssociate(T &map, unsigned long id)
184 /* For each occurence of 'id' (well, only one..it's not a multimap) in 'map'
185 * remove it from the map, take an Extensible* value from the map and remove
186 * 'id' from the list of query IDs attached to it.
188 typename T::iterator iter = map.find(id);
190 if(iter != map.end())
192 /* Found a value indexed by 'id', call RemoveFromList()
193 * on it with 'id' to remove 'id' from the list attached
196 RemoveFromList(iter->second, id);
200 virtual void OnChannelDelete(chanrec* chan)
202 /* A channel is being destroyed, first we need to check if it has a list of queries associated with it.
203 * Then, if it does, we need to erase each of them from our IdChanMap (idchan) so when the module that
204 * associated them asks to look them up then it gets a NULL result and knows to discard the query.
208 if(chan->GetExt("sqlutils_queryids", il))
210 for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
212 IdChanMap::iterator iter;
214 iter = idchan.find(*listiter);
216 if(iter != idchan.end())
218 if(iter->second != chan)
220 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);
226 ServerInstance->Log(DEBUG, "BUG: channel %s was extended with sqlutils_queryids but there was nothing matching in the map", chan->name);
230 chan->Shrink("sqlutils_queryids");
235 virtual Version GetVersion()
237 return Version(1, 1, 0, 0, VF_VENDOR|VF_SERVICEPROVIDER, API_VERSION);
242 class ModuleSQLutilsFactory : public ModuleFactory
245 ModuleSQLutilsFactory()
249 ~ModuleSQLutilsFactory()
253 virtual Module * CreateModule(InspIRCd* Me)
255 return new ModuleSQLutils(Me);
260 extern "C" void * init_module( void )
262 return new ModuleSQLutilsFactory;