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 * ---------------------------------------------------
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 */
25 /* $CompileFlags: -Wno-variadic-macros */
27 typedef std::map<unsigned long, User*> IdUserMap;
28 typedef std::map<unsigned long, Channel*> IdChanMap;
29 typedef std::list<unsigned long> AssocIdList;
31 class ModuleSQLutils : public Module
38 ModuleSQLutils(InspIRCd* Me)
41 ServerInstance->Modules->PublishInterface("SQLutils", this);
44 virtual ~ModuleSQLutils()
46 ServerInstance->Modules->UnpublishInterface("SQLutils", this);
49 void Implements(char* List)
51 List[I_OnChannelDelete] = List[I_OnUnloadModule] = List[I_OnRequest] = List[I_OnUserDisconnect] = 1;
54 virtual char* OnRequest(Request* request)
56 if(strcmp(SQLUTILAU, request->GetId()) == 0)
58 AssociateUser* req = (AssociateUser*)request;
60 iduser.insert(std::make_pair(req->id, req->user));
62 AttachList(req->user, req->id);
64 else if(strcmp(SQLUTILAC, request->GetId()) == 0)
66 AssociateChan* req = (AssociateChan*)request;
68 idchan.insert(std::make_pair(req->id, req->chan));
70 AttachList(req->chan, req->id);
72 else if(strcmp(SQLUTILUA, request->GetId()) == 0)
74 UnAssociate* req = (UnAssociate*)request;
76 /* Unassociate a given query ID with all users and channels
77 * it is associated with.
80 DoUnAssociate(iduser, req->id);
81 DoUnAssociate(idchan, req->id);
83 else if(strcmp(SQLUTILGU, request->GetId()) == 0)
85 GetAssocUser* req = (GetAssocUser*)request;
87 IdUserMap::iterator iter = iduser.find(req->id);
89 if(iter != iduser.end())
91 req->user = iter->second;
94 else if(strcmp(SQLUTILGC, request->GetId()) == 0)
96 GetAssocChan* req = (GetAssocChan*)request;
98 IdChanMap::iterator iter = idchan.find(req->id);
100 if(iter != idchan.end())
102 req->chan = iter->second;
106 return SQLUTILSUCCESS;
109 virtual void OnUserDisconnect(User* user)
111 /* A user is disconnecting, first we need to check if they have a list of queries associated with them.
112 * Then, if they do, we need to erase each of them from our IdUserMap (iduser) so when the module that
113 * associated them asks to look them up then it gets a NULL result and knows to discard the query.
117 if(user->GetExt("sqlutils_queryids", il))
119 for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
121 IdUserMap::iterator iter;
123 iter = iduser.find(*listiter);
125 if(iter != iduser.end())
127 if(iter->second != user)
129 ServerInstance->Log(DEBUG, "BUG: ID associated with user %s doesn't have the same User* associated with it in the map (erasing anyway)", user->nick);
136 ServerInstance->Log(DEBUG, "BUG: user %s was extended with sqlutils_queryids but there was nothing matching in the map", user->nick);
140 user->Shrink("sqlutils_queryids");
145 void AttachList(Extensible* obj, unsigned long id)
149 if(!obj->GetExt("sqlutils_queryids", il))
151 /* Doesn't already exist, create a new list and attach it. */
152 il = new AssocIdList;
153 obj->Extend("sqlutils_queryids", il);
156 /* Now either way we have a valid list in il, attached. */
160 void RemoveFromList(Extensible* obj, unsigned long id)
164 if(obj->GetExt("sqlutils_queryids", il))
166 /* Only do anything if the list exists... (which it ought to) */
171 /* If we just emptied it.. */
173 obj->Shrink("sqlutils_queryids");
178 template <class T> void DoUnAssociate(T &map, unsigned long id)
180 /* For each occurence of 'id' (well, only one..it's not a multimap) in 'map'
181 * remove it from the map, take an Extensible* value from the map and remove
182 * 'id' from the list of query IDs attached to it.
184 typename T::iterator iter = map.find(id);
186 if(iter != map.end())
188 /* Found a value indexed by 'id', call RemoveFromList()
189 * on it with 'id' to remove 'id' from the list attached
192 RemoveFromList(iter->second, id);
196 virtual void OnChannelDelete(Channel* chan)
198 /* A channel is being destroyed, first we need to check if it has a list of queries associated with it.
199 * Then, if it does, we need to erase each of them from our IdChanMap (idchan) so when the module that
200 * associated them asks to look them up then it gets a NULL result and knows to discard the query.
204 if(chan->GetExt("sqlutils_queryids", il))
206 for(AssocIdList::iterator listiter = il->begin(); listiter != il->end(); listiter++)
208 IdChanMap::iterator iter;
210 iter = idchan.find(*listiter);
212 if(iter != idchan.end())
214 if(iter->second != chan)
216 ServerInstance->Log(DEBUG, "BUG: ID associated with channel %s doesn't have the same Channel* associated with it in the map (erasing anyway)", chan->name);
222 ServerInstance->Log(DEBUG, "BUG: channel %s was extended with sqlutils_queryids but there was nothing matching in the map", chan->name);
226 chan->Shrink("sqlutils_queryids");
231 virtual Version GetVersion()
233 return Version(1, 1, 0, 0, VF_VENDOR|VF_SERVICEPROVIDER, API_VERSION);
238 MODULE_INIT(ModuleSQLutils)