1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include "m_sqlutils.h"
19 /* $ModDesc: Provides some utilities to SQL client modules, such as mapping queries to users and channels */
20 /* $ModDep: m_sqlutils.h */
22 typedef std::map<unsigned long, User*> IdUserMap;
23 typedef std::map<unsigned long, Channel*> IdChanMap;
24 typedef std::list<unsigned long> AssocIdList;
26 class ModuleSQLutils : public Module
35 ServerInstance->Modules->PublishInterface("SQLutils", this);
36 Implementation eventlist[] = { I_OnChannelDelete, I_OnUnloadModule, I_OnRequest, I_OnUserDisconnect };
37 ServerInstance->Modules->Attach(eventlist, this, 4);
40 virtual ~ModuleSQLutils()
42 ServerInstance->Modules->UnpublishInterface("SQLutils", this);
46 virtual const char* OnRequest(Request* request)
48 if(strcmp(SQLUTILAU, request->GetId()) == 0)
50 AssociateUser* req = (AssociateUser*)request;
52 iduser.insert(std::make_pair(req->id, req->user));
54 AttachList(req->user, req->id);
56 else if(strcmp(SQLUTILAC, request->GetId()) == 0)
58 AssociateChan* req = (AssociateChan*)request;
60 idchan.insert(std::make_pair(req->id, req->chan));
62 AttachList(req->chan, req->id);
64 else if(strcmp(SQLUTILUA, request->GetId()) == 0)
66 UnAssociate* req = (UnAssociate*)request;
68 /* Unassociate a given query ID with all users and channels
69 * it is associated with.
72 DoUnAssociate(iduser, req->id);
73 DoUnAssociate(idchan, req->id);
75 else if(strcmp(SQLUTILGU, request->GetId()) == 0)
77 GetAssocUser* req = (GetAssocUser*)request;
79 IdUserMap::iterator iter = iduser.find(req->id);
81 if(iter != iduser.end())
83 req->user = iter->second;
86 else if(strcmp(SQLUTILGC, request->GetId()) == 0)
88 GetAssocChan* req = (GetAssocChan*)request;
90 IdChanMap::iterator iter = idchan.find(req->id);
92 if(iter != idchan.end())
94 req->chan = iter->second;
98 return SQLUTILSUCCESS;
101 virtual void OnUserDisconnect(User* user)
103 /* A user is disconnecting, first we need to check if they have a list of queries associated with them.
104 * Then, if they do, we need to erase each of them from our IdUserMap (iduser) so when the module that
105 * associated them asks to look them up then it gets a NULL result and knows to discard the query.
109 if(user->GetExt("sqlutils_queryids", il))
111 for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
113 IdUserMap::iterator iter;
115 iter = iduser.find(*listiter);
117 if(iter != iduser.end())
119 if(iter->second != user)
121 ServerInstance->Logs->Log("m_sqlutils",DEBUG, "BUG: ID associated with user %s doesn't have the same User* associated with it in the map (erasing anyway)", user->nick.c_str());
128 ServerInstance->Logs->Log("m_sqlutils",DEBUG, "BUG: user %s was extended with sqlutils_queryids but there was nothing matching in the map", user->nick.c_str());
132 user->Shrink("sqlutils_queryids");
137 void AttachList(Extensible* obj, unsigned long id)
141 if(!obj->GetExt("sqlutils_queryids", il))
143 /* Doesn't already exist, create a new list and attach it. */
144 il = new AssocIdList;
145 obj->Extend("sqlutils_queryids", il);
148 /* Now either way we have a valid list in il, attached. */
152 void RemoveFromList(Extensible* obj, unsigned long id)
156 if(obj->GetExt("sqlutils_queryids", il))
158 /* Only do anything if the list exists... (which it ought to) */
163 /* If we just emptied it.. */
165 obj->Shrink("sqlutils_queryids");
170 template <class T> void DoUnAssociate(T &map, unsigned long id)
172 /* For each occurence of 'id' (well, only one..it's not a multimap) in 'map'
173 * remove it from the map, take an Extensible* value from the map and remove
174 * 'id' from the list of query IDs attached to it.
176 typename T::iterator iter = map.find(id);
178 if(iter != map.end())
180 /* Found a value indexed by 'id', call RemoveFromList()
181 * on it with 'id' to remove 'id' from the list attached
184 RemoveFromList(iter->second, id);
188 virtual void OnChannelDelete(Channel* chan)
190 /* A channel is being destroyed, first we need to check if it has a list of queries associated with it.
191 * Then, if it does, we need to erase each of them from our IdChanMap (idchan) so when the module that
192 * associated them asks to look them up then it gets a NULL result and knows to discard the query.
196 if(chan->GetExt("sqlutils_queryids", il))
198 for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
200 IdChanMap::iterator iter;
202 iter = idchan.find(*listiter);
204 if(iter != idchan.end())
206 if(iter->second != chan)
208 ServerInstance->Logs->Log("m_sqlutils",DEBUG, "BUG: ID associated with channel %s doesn't have the same Channel* associated with it in the map (erasing anyway)", chan->name.c_str());
214 ServerInstance->Logs->Log("m_sqlutils",DEBUG, "BUG: channel %s was extended with sqlutils_queryids but there was nothing matching in the map", chan->name.c_str());
218 chan->Shrink("sqlutils_queryids");
223 virtual Version GetVersion()
225 return Version("Provides some utilities to SQL client modules, such as mapping queries to users and channels", VF_VENDOR | VF_SERVICEPROVIDER, API_VERSION);
230 MODULE_INIT(ModuleSQLutils)