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