]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/snomasks.cpp
2.0.1 release
[user/henk/code/inspircd.git] / src / snomasks.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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 #include <stdarg.h>
16
17 void SnomaskManager::FlushSnotices()
18 {
19         for (int i=0; i < 26; i++)
20                 masks[i].Flush();
21 }
22
23 void SnomaskManager::EnableSnomask(char letter, const std::string &type)
24 {
25         if (letter >= 'a' && letter <= 'z')
26                 masks[letter - 'a'].Description = type;
27 }
28
29 void SnomaskManager::WriteToSnoMask(char letter, const std::string &text)
30 {
31         if (letter >= 'a' && letter <= 'z')
32                 masks[letter - 'a'].SendMessage(text, letter);
33         if (letter >= 'A' && letter <= 'Z')
34                 masks[letter - 'A'].SendMessage(text, letter);
35 }
36
37 void SnomaskManager::WriteGlobalSno(char letter, const std::string& text)
38 {
39         WriteToSnoMask(letter, text);
40         letter = toupper(letter);
41         ServerInstance->PI->SendSNONotice(std::string(1, letter), text);
42 }
43
44 void SnomaskManager::WriteToSnoMask(char letter, const char* text, ...)
45 {
46         char textbuffer[MAXBUF];
47         va_list argsPtr;
48
49         va_start(argsPtr, text);
50         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
51         va_end(argsPtr);
52
53         this->WriteToSnoMask(letter, std::string(textbuffer));
54 }
55
56 void SnomaskManager::WriteGlobalSno(char letter, const char* text, ...)
57 {
58         char textbuffer[MAXBUF];
59         va_list argsPtr;
60
61         va_start(argsPtr, text);
62         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
63         va_end(argsPtr);
64
65         this->WriteGlobalSno(letter, std::string(textbuffer));
66 }
67
68 SnomaskManager::SnomaskManager()
69 {
70         EnableSnomask('c',"CONNECT");                   /* Local connect notices */
71         EnableSnomask('q',"QUIT");                      /* Local quit notices */
72         EnableSnomask('k',"KILL");                      /* Kill notices */
73         EnableSnomask('l',"LINK");                      /* Linking notices */
74         EnableSnomask('o',"OPER");                      /* Oper up/down notices */
75         EnableSnomask('a',"ANNOUNCEMENT");      /* formerly WriteOpers() - generic notices to all opers */
76         EnableSnomask('d',"DEBUG");                     /* Debug notices */
77         EnableSnomask('x',"XLINE");                     /* Xline notice (g/z/q/k/e) */
78         EnableSnomask('t',"STATS");                     /* Local or remote stats request */
79         EnableSnomask('f',"FLOOD");                     /* Flooding notices */
80 }
81
82 /*************************************************************************************/
83
84 void Snomask::SendMessage(const std::string &message, char mysnomask)
85 {
86         if (message != LastMessage || mysnomask != LastLetter)
87         {
88                 this->Flush();
89                 LastMessage = message;
90                 LastLetter = mysnomask;
91
92                 std::string desc = Description;
93                 if (desc.empty())
94                         desc = "SNO-" + tolower(mysnomask);
95                 if (isupper(mysnomask))
96                         desc = "REMOTE" + desc;
97                 ModResult MOD_RESULT;
98                 ServerInstance->Logs->Log("snomask", DEFAULT, "%s: %s", desc.c_str(), message.c_str());
99
100                 FIRST_MOD_RESULT(OnSendSnotice, MOD_RESULT, (mysnomask, desc, message));
101
102                 LastBlocked = (MOD_RESULT == MOD_RES_DENY);
103
104                 if (!LastBlocked)
105                 {
106                         /* Only opers can receive snotices, so we iterate the oper list */
107                         std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin();
108
109                         while (i != ServerInstance->Users->all_opers.end())
110                         {
111                                 User* a = *i;
112                                 if (IS_LOCAL(a) && a->IsModeSet('s') && a->IsNoticeMaskSet(mysnomask) && !a->quitting)
113                                 {
114                                         a->WriteServ("NOTICE %s :*** %s: %s", a->nick.c_str(), desc.c_str(), message.c_str());
115                                 }
116
117                                 i++;
118                         }
119                 }
120         }
121         Count++;
122 }
123
124 void Snomask::Flush()
125 {
126         if (Count > 1)
127         {
128                 std::string desc = Description;
129                 if (desc.empty())
130                         desc = "SNO-" + tolower(LastLetter);
131                 if (isupper(LastLetter))
132                         desc = "REMOTE" + desc;
133                 std::string mesg = "(last message repeated "+ConvToStr(Count)+" times)";
134
135                 ServerInstance->Logs->Log("snomask", DEFAULT, "%s: %s", desc.c_str(), mesg.c_str());
136
137                 FOREACH_MOD(I_OnSendSnotice, OnSendSnotice(LastLetter, desc, mesg));
138
139                 if (!LastBlocked)
140                 {
141                         /* Only opers can receive snotices, so we iterate the oper list */
142                         std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin();
143
144                         while (i != ServerInstance->Users->all_opers.end())
145                         {
146                                 User* a = *i;
147                                 if (IS_LOCAL(a) && a->IsModeSet('s') && a->IsNoticeMaskSet(LastLetter) && !a->quitting)
148                                 {
149                                         a->WriteServ("NOTICE %s :*** %s: %s", a->nick.c_str(), desc.c_str(), mesg.c_str());
150                                 }
151
152                                 i++;
153                         }
154                 }
155
156         }
157         LastMessage = "";
158         LastBlocked = false;
159         Count = 0;
160 }