1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 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
31 SimpleExtItem<AssocIdList> idExt;
34 ModuleSQLutils() : idExt("sqlutils_list", this)
36 Implementation eventlist[] = { I_OnChannelDelete, I_OnUnloadModule, I_OnUserDisconnect };
37 ServerInstance->Modules->Attach(eventlist, this, 3);
45 void OnRequest(Request& request)
47 if(strcmp(SQLUTILAU, request.id) == 0)
49 AssociateUser* req = (AssociateUser*)&request;
51 iduser.insert(std::make_pair(req->id, req->user));
53 AttachList(req->user, req->id);
55 else if(strcmp(SQLUTILAC, request.id) == 0)
57 AssociateChan* req = (AssociateChan*)&request;
59 idchan.insert(std::make_pair(req->id, req->chan));
61 AttachList(req->chan, req->id);
63 else if(strcmp(SQLUTILUA, request.id) == 0)
65 UnAssociate* req = (UnAssociate*)&request;
67 /* Unassociate a given query ID with all users and channels
68 * it is associated with.
71 DoUnAssociate(iduser, req->id);
72 DoUnAssociate(idchan, req->id);
74 else if(strcmp(SQLUTILGU, request.id) == 0)
76 GetAssocUser* req = (GetAssocUser*)&request;
78 IdUserMap::iterator iter = iduser.find(req->id);
80 if(iter != iduser.end())
82 req->user = iter->second;
85 else if(strcmp(SQLUTILGC, request.id) == 0)
87 GetAssocChan* req = (GetAssocChan*)&request;
89 IdChanMap::iterator iter = idchan.find(req->id);
91 if(iter != idchan.end())
93 req->chan = iter->second;
98 void OnUserDisconnect(LocalUser* user)
100 /* A user is disconnecting, first we need to check if they have a list of queries associated with them.
101 * Then, if they do, we need to erase each of them from our IdUserMap (iduser) so when the module that
102 * associated them asks to look them up then it gets a NULL result and knows to discard the query.
104 AssocIdList* il = idExt.get(user);
108 for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
110 IdUserMap::iterator iter;
112 iter = iduser.find(*listiter);
114 if(iter != iduser.end())
116 if(iter->second != user)
118 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());
125 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());
133 void AttachList(Extensible* obj, unsigned long id)
135 AssocIdList* il = idExt.get(obj);
139 /* Doesn't already exist, create a new list and attach it. */
140 il = new AssocIdList;
144 /* Now either way we have a valid list in il, attached. */
148 void RemoveFromList(Extensible* obj, unsigned long id)
150 AssocIdList* il = idExt.get(obj);
154 /* Only do anything if the list exists... (which it ought to) */
159 /* If we just emptied it.. */
165 template <class T> void DoUnAssociate(T &map, unsigned long id)
167 /* For each occurence of 'id' (well, only one..it's not a multimap) in 'map'
168 * remove it from the map, take an Extensible* value from the map and remove
169 * 'id' from the list of query IDs attached to it.
171 typename T::iterator iter = map.find(id);
173 if(iter != map.end())
175 /* Found a value indexed by 'id', call RemoveFromList()
176 * on it with 'id' to remove 'id' from the list attached
179 RemoveFromList(iter->second, id);
183 void OnChannelDelete(Channel* chan)
185 /* A channel is being destroyed, first we need to check if it has a list of queries associated with it.
186 * Then, if it does, we need to erase each of them from our IdChanMap (idchan) so when the module that
187 * associated them asks to look them up then it gets a NULL result and knows to discard the query.
189 AssocIdList* il = idExt.get(chan);
193 for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
195 IdChanMap::iterator iter;
197 iter = idchan.find(*listiter);
199 if(iter != idchan.end())
201 if(iter->second != chan)
203 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());
209 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());
219 return Version("Provides some utilities to SQL client modules, such as mapping queries to users and channels", VF_VENDOR);
224 MODULE_INIT(ModuleSQLutils)