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
31 SimpleExtItem<AssocIdList> idExt;
34 ModuleSQLutils() : idExt("sqlutils_list", this)
36 ServerInstance->Modules->PublishInterface("SQLutils", this);
37 Implementation eventlist[] = { I_OnChannelDelete, I_OnUnloadModule, I_OnRequest, I_OnUserDisconnect };
38 ServerInstance->Modules->Attach(eventlist, this, 4);
43 ServerInstance->Modules->UnpublishInterface("SQLutils", this);
47 const char* OnRequest(Request* request)
49 if(strcmp(SQLUTILAU, request->GetId()) == 0)
51 AssociateUser* req = (AssociateUser*)request;
53 iduser.insert(std::make_pair(req->id, req->user));
55 AttachList(req->user, req->id);
57 else if(strcmp(SQLUTILAC, request->GetId()) == 0)
59 AssociateChan* req = (AssociateChan*)request;
61 idchan.insert(std::make_pair(req->id, req->chan));
63 AttachList(req->chan, req->id);
65 else if(strcmp(SQLUTILUA, request->GetId()) == 0)
67 UnAssociate* req = (UnAssociate*)request;
69 /* Unassociate a given query ID with all users and channels
70 * it is associated with.
73 DoUnAssociate(iduser, req->id);
74 DoUnAssociate(idchan, req->id);
76 else if(strcmp(SQLUTILGU, request->GetId()) == 0)
78 GetAssocUser* req = (GetAssocUser*)request;
80 IdUserMap::iterator iter = iduser.find(req->id);
82 if(iter != iduser.end())
84 req->user = iter->second;
87 else if(strcmp(SQLUTILGC, request->GetId()) == 0)
89 GetAssocChan* req = (GetAssocChan*)request;
91 IdChanMap::iterator iter = idchan.find(req->id);
93 if(iter != idchan.end())
95 req->chan = iter->second;
99 return SQLUTILSUCCESS;
102 void OnUserDisconnect(User* user)
104 /* A user is disconnecting, first we need to check if they have a list of queries associated with them.
105 * Then, if they do, we need to erase each of them from our IdUserMap (iduser) so when the module that
106 * associated them asks to look them up then it gets a NULL result and knows to discard the query.
108 AssocIdList* il = idExt.get(user);
112 for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
114 IdUserMap::iterator iter;
116 iter = iduser.find(*listiter);
118 if(iter != iduser.end())
120 if(iter->second != user)
122 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());
129 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());
137 void AttachList(Extensible* obj, unsigned long id)
139 AssocIdList* il = idExt.get(obj);
143 /* Doesn't already exist, create a new list and attach it. */
144 il = new AssocIdList;
148 /* Now either way we have a valid list in il, attached. */
152 void RemoveFromList(Extensible* obj, unsigned long id)
154 AssocIdList* il = idExt.get(obj);
158 /* Only do anything if the list exists... (which it ought to) */
163 /* If we just emptied it.. */
169 template <class T> void DoUnAssociate(T &map, unsigned long id)
171 /* For each occurence of 'id' (well, only one..it's not a multimap) in 'map'
172 * remove it from the map, take an Extensible* value from the map and remove
173 * 'id' from the list of query IDs attached to it.
175 typename T::iterator iter = map.find(id);
177 if(iter != map.end())
179 /* Found a value indexed by 'id', call RemoveFromList()
180 * on it with 'id' to remove 'id' from the list attached
183 RemoveFromList(iter->second, id);
187 void OnChannelDelete(Channel* chan)
189 /* A channel is being destroyed, first we need to check if it has a list of queries associated with it.
190 * Then, if it does, we need to erase each of them from our IdChanMap (idchan) so when the module that
191 * associated them asks to look them up then it gets a NULL result and knows to discard the query.
193 AssocIdList* il = idExt.get(chan);
197 for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
199 IdChanMap::iterator iter;
201 iter = idchan.find(*listiter);
203 if(iter != idchan.end())
205 if(iter->second != chan)
207 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());
213 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());
223 return Version("Provides some utilities to SQL client modules, such as mapping queries to users and channels", VF_VENDOR | VF_SERVICEPROVIDER, API_VERSION);
228 MODULE_INIT(ModuleSQLutils)