]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delayjoin.cpp
9626d978038b1d56a8d8922fcaea82f58afba63f
[user/henk/code/inspircd.git] / src / modules / m_delayjoin.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: Allows for delay-join channels (+D) where users dont appear to join until they speak */
17
18 class DelayJoinMode : public ModeHandler
19 {
20  public:
21         DelayJoinMode(InspIRCd* Instance) : ModeHandler(Instance, 'D', 0, 0, false, MODETYPE_CHANNEL, false) { }
22
23         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
24         {
25                 if (channel->IsModeSet('D') != adding)
26                 {
27                         if (IS_LOCAL(source) && (channel->GetStatus(source) < STATUS_OP))
28                         {
29                                 source->WriteServ("482 %s %s :Only channel operators may %sset channel mode +D", source->nick, channel->name, adding ? "" : "un");
30                                 return MODEACTION_DENY;
31                         }
32                         else
33                         {
34                                 channel->SetMode('D', adding);
35                                 return MODEACTION_ALLOW;
36                         }
37                 }
38                 else
39                 {
40                         return MODEACTION_DENY;
41                 }
42         }
43 };
44
45 class ModuleDelayJoin : public Module
46 {
47  private:
48         DelayJoinMode* djm;
49         CUList nl;
50  public:
51         ModuleDelayJoin(InspIRCd* Me)
52                 : Module(Me)
53         {
54                 djm = new DelayJoinMode(ServerInstance);
55                 if (!ServerInstance->AddMode(djm))
56                         throw ModuleException("Could not add new modes!");
57         }
58         
59         virtual ~ModuleDelayJoin()
60         {
61                 ServerInstance->Modes->DelMode(djm);
62                 DELETE(djm);
63         }
64
65         Priority Prioritize()
66         {
67                 /* To ensure that we get priority over namesx for names list generation on +u channels */
68                 return (Priority)ServerInstance->Modules->PriorityBefore("m_namesx.so");
69         }
70
71         virtual Version GetVersion()
72         {
73                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
74         }
75
76         void Implements(char* List)
77         {
78                 List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserKick] = List[I_OnUserQuit] = List[I_OnUserList] = 
79                 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
80         }
81
82         virtual int OnUserList(User* user, Channel* Ptr, CUList* &nameslist)
83         {
84                 CUList* newlist = nameslist ? nameslist : Ptr->GetUsers();
85
86                 nl.clear();
87
88                 /* For +D channels ... */
89                 if (Ptr->IsModeSet('D'))
90                 {
91                         std::string key("delayjoin_");
92                         key.append(Ptr->name);
93                         ServerInstance->Log(DEBUG,"Key: %s", key.c_str());
94                         /* Modify the names list, erasing users with the delay join metadata
95                          * for this channel (havent spoken yet)
96                          */
97                         for (CUListIter n = newlist->begin(); n != newlist->end(); ++n)
98                         {
99                                 ServerInstance->Log(DEBUG,"Item: %s", n->first->nick);
100                                 if (!n->first->GetExt(key))
101                                 {
102                                         nl.insert(*n);
103                                         ServerInstance->Log(DEBUG,"Spoken: %s", n->first->nick);
104                                 }
105                         }
106                         ServerInstance->Log(DEBUG,"Insert self");
107                         nl[user] = user->nick;
108                         nameslist = &nl;
109                 }
110                 return 0;
111         }
112
113         virtual void OnUserJoin(User* user, Channel* channel, bool &silent)
114         {
115                 if (channel->IsModeSet('D'))
116                 {
117                         silent = true;
118                         /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
119                         user->WriteFrom(user, "JOIN %s", channel->name);
120
121                         /* This metadata tells the module the user is delayed join on this specific channel */
122                         user->Extend(std::string("delayjoin_")+channel->name);
123
124                         /* This metadata tells the module the user is delayed join on at least one (or more) channels.
125                          * It is only cleared when the user is no longer on ANY +D channels.
126                          */
127                         if (!user->GetExt("delayjoin"))
128                                 user->Extend("delayjoin");
129                 }
130         }
131
132         void OnUserPart(User* user, Channel* channel, const std::string &partmessage, bool &silent)
133         {
134                 if (channel->IsModeSet('D'))
135                 {
136                         if (user->GetExt(std::string("delayjoin_")+channel->name))
137                         {
138                                 silent = true;
139                                 /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
140                                 user->WriteFrom(user, "PART %s%s%s", channel->name, partmessage.empty() ? "" : " :", partmessage.empty() ? "" : partmessage.c_str());
141                         }
142                 }
143         }
144
145         void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
146         {
147                 if (chan->IsModeSet('D'))
148                 {
149                         /* Send silenced event only to the user being kicked and the user doing the kick */
150                         if (user->GetExt(std::string("delayjoin_")+chan->name))
151                         {
152                                 silent = true;
153                                 user->WriteFrom(source, "KICK %s %s %s", chan->name, user->nick, reason.c_str());
154                         }
155                 }
156         }
157
158         void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
159         {
160                 Command* parthandler = ServerInstance->Parser->GetHandler("PART");
161                 const char* parameters[2];
162                 if (parthandler && user->GetExt("delayjoin"))
163                 {
164                         for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
165                         {
166                                 parameters[0] = f->first->name;
167                                 /* This triggers our OnUserPart, above, making the PART silent */
168                                 parthandler->Handle(parameters, 1, user);
169                         }
170                 }
171         }
172
173         void OnText(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
174         {
175                 if (target_type != TYPE_CHANNEL)
176                         return;
177
178                 Channel* channel = (Channel*) dest;
179
180                 if (!user->GetExt(std::string("delayjoin_")+channel->name))
181                         return;
182
183                 /* Display the join to everyone else (the user who joined got it earlier) */
184                 channel->WriteAllExcept(user, false, 0, exempt_list, "JOIN %s", channel->name);
185
186                 /* Shrink off the neccessary metadata for a specific channel */
187                 user->Shrink(std::string("delayjoin_")+channel->name);
188
189                 /* Check if the user is left on any other +D channels, if so don't take away the
190                  * metadata that says theyre on one or more channels 
191                  */
192                 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
193                         if (f->first->IsModeSet('D'))
194                                 return;
195
196                 user->Shrink("delayjoin");
197         }
198 };
199
200 MODULE_INIT(ModuleDelayJoin)
201