]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/snomasks.cpp
Header update: 2007 -> 2008
[user/henk/code/inspircd.git] / src / snomasks.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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 /* $Core: libIRCDsnomasks */
15
16 #include "inspircd.h"
17 #include <stdarg.h>
18 #include "snomasks.h"
19
20 SnomaskManager::SnomaskManager(InspIRCd* Instance) : ServerInstance(Instance)
21 {
22         SnoMasks.clear();
23         this->SetupDefaults();
24 }
25
26 SnomaskManager::~SnomaskManager()
27 {
28 }
29
30 void SnomaskManager::FlushSnotices()
31 {
32         // stub.. not yet written XXX
33         for (std::map<char, Snomask *>::iterator i = SnoMasks.begin(); i != SnoMasks.end(); i++)
34         {
35                 i->second->Flush();
36         }
37 }
38
39 bool SnomaskManager::EnableSnomask(char letter, const std::string &type)
40 {
41         if (SnoMasks.find(letter) == SnoMasks.end())
42         {
43                 Snomask *s = new Snomask(ServerInstance, letter, type);
44                 SnoMasks[letter] = s;
45                 return true;
46         }
47         return false;
48 }
49
50 bool SnomaskManager::DisableSnomask(char letter)
51 {
52         SnoList::iterator n = SnoMasks.find(letter);
53         if (n != SnoMasks.end())
54         {
55                 delete n->second; // destroy the snomask
56                 SnoMasks.erase(n);
57                 return true;
58         }
59         return false;
60 }
61
62 void SnomaskManager::WriteToSnoMask(char letter, const std::string &text)
63 {
64         /* Only send to snomask chars which are enabled */
65         SnoList::iterator n = SnoMasks.find(letter);
66         if (n != SnoMasks.end())
67         {
68                 n->second->SendMessage(text);
69         }
70 }
71
72 void SnomaskManager::WriteToSnoMask(char letter, const char* text, ...)
73 {
74         char textbuffer[MAXBUF];
75         va_list argsPtr;
76
77         va_start(argsPtr, text);
78         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
79         va_end(argsPtr);
80
81         this->WriteToSnoMask(letter, std::string(textbuffer));
82 }
83
84 bool SnomaskManager::IsEnabled(char letter)
85 {
86         return (SnoMasks.find(letter) != SnoMasks.end());
87 }
88
89 void SnomaskManager::SetupDefaults()
90 {
91         this->EnableSnomask('c',"CONNECT");                     /* Local connect notices */
92         this->EnableSnomask('C',"REMOTECONNECT");       /* Remote connect notices */
93         this->EnableSnomask('q',"QUIT");                        /* Local quit notices */
94         this->EnableSnomask('Q',"REMOTEQUIT");          /* Remote quit notices */
95         this->EnableSnomask('k',"KILL");                        /* Kill notices */
96         this->EnableSnomask('K',"REMOTEKILL");          /* Remote kill notices */
97         this->EnableSnomask('l',"LINK");                        /* Link notices */
98         this->EnableSnomask('o',"OPER");                        /* Oper up/down notices */
99         this->EnableSnomask('d',"DEBUG");                       /* Debug notices */
100         this->EnableSnomask('x',"XLINE");                       /* Xline notice (g/z/q/k/e) */
101         this->EnableSnomask('t',"STATS");                       /* Local or remote stats request */
102         this->EnableSnomask('f',"FLOOD");                       /* Flooding notices */
103 }
104
105 /*************************************************************************************/
106
107 void Snomask::SendMessage(const std::string &message)
108 {
109         if (message != LastMessage)
110         {
111                 this->Flush();
112                 LastMessage = message;
113         }
114         else
115         {
116                 Count++;
117         }
118 }
119
120 void Snomask::Flush()
121 {
122         if (this->LastMessage.empty())
123                 return;
124
125         ServerInstance->Log(DEBUG, "Flusing snomask %s", this->Description.c_str());
126
127         /* Only opers can receive snotices, so we iterate the oper list */
128         for (std::list<User*>::iterator i = ServerInstance->all_opers.begin(); i != ServerInstance->all_opers.end(); i++)
129         {
130                 User* a = *i;
131                 if (IS_LOCAL(a) && a->IsModeSet('s') && a->IsModeSet('n') && a->IsNoticeMaskSet(MySnomask))
132                 {
133                         a->WriteServ("NOTICE %s :*** %s: %s", a->nick, this->Description.c_str(), this->LastMessage.c_str());
134                         if (Count > 1)
135                         {
136                                 a->WriteServ("NOTICE %s :*** %s: (last message repeated %u times)", a->nick, this->Description.c_str(), Count);
137                         }
138                 }
139         }
140
141         LastMessage = "";
142         Count = 1;
143 }