1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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, User*> IdUserMap;
27 typedef std::map<unsigned long, Channel*> IdChanMap;
28 typedef std::list<unsigned long> AssocIdList;
30 class ModuleSQLutils : public Module
37 ModuleSQLutils(InspIRCd* Me)
40 ServerInstance->Modules->PublishInterface("SQLutils", this);
41 Implementation eventlist[] = { I_OnChannelDelete, I_OnUnloadModule, I_OnRequest, I_OnUserDisconnect };
42 ServerInstance->Modules->Attach(eventlist, this, 4);
45 virtual ~ModuleSQLutils()
47 ServerInstance->Modules->UnpublishInterface("SQLutils", this);
51 virtual const char* OnRequest(Request* request)
53 if(strcmp(SQLUTILAU, request->GetId()) == 0)
55 AssociateUser* req = (AssociateUser*)request;
57 iduser.insert(std::make_pair(req->id, req->user));
59 AttachList(req->user, req->id);
61 else if(strcmp(SQLUTILAC, request->GetId()) == 0)
63 AssociateChan* req = (AssociateChan*)request;
65 idchan.insert(std::make_pair(req->id, req->chan));
67 AttachList(req->chan, req->id);
69 else if(strcmp(SQLUTILUA, request->GetId()) == 0)
71 UnAssociate* req = (UnAssociate*)request;
73 /* Unassociate a given query ID with all users and channels
74 * it is associated with.
77 DoUnAssociate(iduser, req->id);
78 DoUnAssociate(idchan, req->id);
80 else if(strcmp(SQLUTILGU, request->GetId()) == 0)
82 GetAssocUser* req = (GetAssocUser*)request;
84 IdUserMap::iterator iter = iduser.find(req->id);
86 if(iter != iduser.end())
88 req->user = iter->second;
91 else if(strcmp(SQLUTILGC, request->GetId()) == 0)
93 GetAssocChan* req = (GetAssocChan*)request;
95 IdChanMap::iterator iter = idchan.find(req->id);
97 if(iter != idchan.end())
99 req->chan = iter->second;
103 return SQLUTILSUCCESS;
106 virtual void OnUserDisconnect(User* user)
108 /* A user is disconnecting, first we need to check if they have a list of queries associated with them.
109 * Then, if they do, we need to erase each of them from our IdUserMap (iduser) so when the module that
110 * associated them asks to look them up then it gets a NULL result and knows to discard the query.
114 if(user->GetExt("sqlutils_queryids", il))
116 for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
118 IdUserMap::iterator iter;
120 iter = iduser.find(*listiter);
122 if(iter != iduser.end())
124 if(iter->second != user)
126 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());
133 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 user->Shrink("sqlutils_queryids");
142 void AttachList(Extensible* obj, unsigned long id)
146 if(!obj->GetExt("sqlutils_queryids", il))
148 /* Doesn't already exist, create a new list and attach it. */
149 il = new AssocIdList;
150 obj->Extend("sqlutils_queryids", il);
153 /* Now either way we have a valid list in il, attached. */
157 void RemoveFromList(Extensible* obj, unsigned long id)
161 if(obj->GetExt("sqlutils_queryids", il))
163 /* Only do anything if the list exists... (which it ought to) */
168 /* If we just emptied it.. */
170 obj->Shrink("sqlutils_queryids");
175 template <class T> void DoUnAssociate(T &map, unsigned long id)
177 /* For each occurence of 'id' (well, only one..it's not a multimap) in 'map'
178 * remove it from the map, take an Extensible* value from the map and remove
179 * 'id' from the list of query IDs attached to it.
181 typename T::iterator iter = map.find(id);
183 if(iter != map.end())
185 /* Found a value indexed by 'id', call RemoveFromList()
186 * on it with 'id' to remove 'id' from the list attached
189 RemoveFromList(iter->second, id);
193 virtual void OnChannelDelete(Channel* chan)
195 /* A channel is being destroyed, first we need to check if it has a list of queries associated with it.
196 * Then, if it does, we need to erase each of them from our IdChanMap (idchan) so when the module that
197 * associated them asks to look them up then it gets a NULL result and knows to discard the query.
201 if(chan->GetExt("sqlutils_queryids", il))
203 for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
205 IdChanMap::iterator iter;
207 iter = idchan.find(*listiter);
209 if(iter != idchan.end())
211 if(iter->second != chan)
213 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());
219 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 chan->Shrink("sqlutils_queryids");
228 virtual Version GetVersion()
230 return Version("$Id$", VF_VENDOR | VF_SERVICEPROVIDER, API_VERSION);
235 MODULE_INIT(ModuleSQLutils)