forked from dreamcat4/urlbuffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlbuffer.cpp
408 lines (358 loc) · 13 KB
/
urlbuffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* Copyright (C) 2011 uberspot
*
* Compiling: znc-buildmod urlbuffer.cpp
* Dependencies: curl, wget, sed and a unix environment.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation (https://www.gnu.org/licenses/gpl-3.0.txt).
*/
#include <znc/Client.h>
#include <znc/Chan.h>
#include <znc/User.h>
#include <znc/Modules.h>
#include <znc/FileUtils.h>
#include <znc/IRCNetwork.h>
#include <climits>
#include <fstream>
#define MAX_EXTS 8
#define MAX_CHARS 16
class CUrlBufferModule : public CModule
{
private:
/* Vectors containing the last urls posted + the nicknames of the users posting them. */
VCString lastUrls, nicks;
/* Supported file extensions in links that should be downloaded. */
static const CString supportedExts[MAX_EXTS];
/* Unsupported characters that should be ignored when found in strings describing file directories. */
static const char unSupportedChars[MAX_CHARS];
/* Executes the given command in a unix pipeline and returns the result. */
static inline CString getStdoutFromCommand(const CString& cmd);
/* Load the default options if no other were found. */
inline void LoadDefaults();
inline bool isValidExtension(CString ext)
{
ext.MakeLower();
for(int i=0; i< MAX_EXTS; i++)
{
if( ext == supportedExts[i])
return true;
}
return false;
}
inline bool isValidDir(const CString& dir)
{
for(int i=0; i< MAX_CHARS; i++)
{
if (dir.find(unSupportedChars[i]) !=CString::npos)
return false;
}
return true;
}
inline void CheckLineForLink(const CString& sMessage, const CString& sOrigin);
inline void CheckLineForTrigger(const CString& sMessage, const CString& sTarget);
public:
MODCONSTRUCTOR(CUrlBufferModule) {}
bool OnLoad(const CString& sArgs, CString& sErrorMsg);
virtual ~CUrlBufferModule();
EModRet OnUserMsg(CString& sTarget, CString& sMessage);
EModRet OnPrivMsg(CNick& Nick, CString& sMessage);
EModRet OnChanMsg(CNick& Nick, CChan& Channel, CString& sMessage);
void OnModCommand(const CString& sCommand);
};
const CString CUrlBufferModule::supportedExts[MAX_EXTS] =
{"jpg", "png", "gif", "jpeg", "bmp", "tiff", "webm", "gifv"} ;
const char CUrlBufferModule::unSupportedChars[MAX_CHARS] =
{'|', ';', '!', '@', '#', '(', ')', '<', '>', '"', '\'', '`', '~', '=', '&', '^'};
bool CUrlBufferModule::OnLoad(const CString& sArgs, CString& sErrorMsg)
{
LoadDefaults();
return true;
}
CUrlBufferModule::~CUrlBufferModule() {}
CUrlBufferModule::EModRet CUrlBufferModule::OnUserMsg(CString& sTarget, CString& sMessage)
{
CheckLineForLink(sMessage, "");
CheckLineForTrigger(sMessage, m_pUser->GetUserName() );
return CONTINUE;
}
CUrlBufferModule::EModRet CUrlBufferModule::OnPrivMsg(CNick& Nick, CString& sMessage)
{
CheckLineForLink(sMessage, Nick.GetNick());
CheckLineForTrigger(sMessage, Nick.GetNick());
return CONTINUE;
}
CUrlBufferModule::EModRet CUrlBufferModule::OnChanMsg(CNick& Nick, CChan& Channel, CString& sMessage)
{
CheckLineForLink(sMessage, Nick.GetNick());
CheckLineForTrigger(sMessage, Nick.GetNick());
return CONTINUE;
}
void CUrlBufferModule::OnModCommand(const CString& sCommand)
{
CString command = sCommand.Token(0).AsLower().Trim_n();
if (command == "help")
{
CTable CmdTable;
CmdTable.AddColumn("Command");
CmdTable.AddColumn("Description");
CmdTable.AddRow();
CmdTable.SetCell("Command", "ENABLE");
CmdTable.SetCell("Description", "Activates link buffering.");
CmdTable.AddRow();
CmdTable.SetCell("Command", "DISABLE");
CmdTable.SetCell("Description", "Deactivates link buffering.");
CmdTable.AddRow();
CmdTable.SetCell("Command", "ENABLELOCAL");
CmdTable.SetCell("Description", "Enables downloading of each link to local directory.");
CmdTable.AddRow();
CmdTable.SetCell("Command", "DISABLELOCAL");
CmdTable.SetCell("Description", "Disables downloading of each link to local directory.");
CmdTable.AddRow();
CmdTable.SetCell("Command","ENABLEPUBLIC");
CmdTable.SetCell("Description", "Enables the usage of !showlinks publicly, by other users.");
CmdTable.AddRow();
CmdTable.SetCell("Command","DISABLEPUBLIC");
CmdTable.SetCell("Description", "Disables the usage of !showlinks publicly, by other users.");
CmdTable.AddRow();
CmdTable.SetCell("Command", "DIRECTORY <#directory>");
CmdTable.SetCell("Description", "Sets the local directory where the links will be saved.");
CmdTable.AddRow();
CmdTable.SetCell("Command", "CLEARBUFFER");
CmdTable.SetCell("Description", "Empties the link buffer.");
CmdTable.AddRow();
CmdTable.SetCell("Command", "BUFFERSIZE <#size>");
CmdTable.SetCell("Description", "Sets the size of the link buffer. Only integers >=0.");
CmdTable.AddRow();
CmdTable.SetCell("Command", "SHOWSETTINGS");
CmdTable.SetCell("Description", "Prints all the settings.");
CmdTable.AddRow();
CmdTable.SetCell("Command", "BUFFERALLLINKS");
CmdTable.SetCell("Description", "Toggles the buffering of all links or only image links.");
CmdTable.AddRow();
CmdTable.SetCell("Command", "REUPLOAD");
CmdTable.SetCell("Description", "Toggles the reuploading of image links to imgur.");
CmdTable.AddRow();
CmdTable.SetCell("Command", "SHOWLINKS <#number>");
CmdTable.SetCell("Description", "Prints <#number> or <#buffersize> number of cached links.");
CmdTable.AddRow();
CmdTable.SetCell("Command", "HELP");
CmdTable.SetCell("Description", "This help.");
PutModule(CmdTable);
return;
} else if (command == "enable")
{
SetNV("enable","true",true);
PutModule("Enabled buffering");
} else if (command == "disable")
{
SetNV("enable","false",true);
PutModule("Disabled buffering");
} else if (command == "enablelocal")
{
if(GetNV("directory") == "")
{
PutModule("Directory is not set. First set a directory and then enable local caching");
return;
}
SetNV("enablelocal","true",true);
PutModule("Enabled local caching");
} else if (command == "disablelocal")
{
SetNV("enablelocal", "false", true);
PutModule("Disabled local caching");
} else if (command == "enablepublic")
{
SetNV("enablepublic", "true", true);
PutModule("Enabled public usage of showlinks.");
} else if (command == "disablepublic")
{
SetNV("enablepublic", "false", true);
PutModule("Disabled public usage of showlinks.");
} else if (command == "directory")
{
CString dir=sCommand.Token(1).Replace_n("//", "/").TrimRight_n("/") + "/";
if (!isValidDir(dir))
{
PutModule("Error in directory name. Avoid using: | ; ! @ # ( ) < > \" ' ` ~ = & ^ <space> <tab>");
return;
}
// Check if file exists and is directory
if (dir.empty() || !CFile::Exists(dir) || !CFile::IsDir(dir, false))
{
PutModule("Invalid path or no write access to ["+ sCommand.Token(1) +"].");
return;
}
SetNV("directory", dir, true);
PutModule("Directory for local caching set to " + GetNV("directory"));
} else if (command == "clearbuffer")
{
lastUrls.clear();
nicks.clear();
} else if (command == "buffersize")
{
unsigned int bufSize = sCommand.Token(1).ToUInt();
if(bufSize==0 || bufSize==UINT_MAX)
{
PutModule("Error in buffer size. Use only integers >= 0.");
return;
}
SetNV("buffersize", CString(bufSize), true);
PutModule("Buffer size set to " + GetNV("buffersize"));
} else if (command == "showsettings")
{
for(MCString::iterator it = BeginNV(); it != EndNV(); it++)
{
PutModule(it->first.AsUpper() + " : " + it->second);
}
} else if(command == "bufferalllinks"){
SetNV("bufferalllinks", CString(!GetNV("bufferalllinks").ToBool()), true);
PutModule( CString(GetNV("bufferalllinks").ToBool()?"Enabled":"Disabled") + " buffering of all links.");
} else if(command == "reupload"){
SetNV("reupload", CString(!GetNV("reupload").ToBool()), true);
PutModule( CString(GetNV("reupload").ToBool()?"Enabled":"Disabled") + " reuploading of images.");
} else if (command == "showlinks")
{
if(lastUrls.empty())
PutModule("No links were found...");
else
{
unsigned int maxLinks = GetNV("buffersize").ToUInt();
unsigned int size = sCommand.Token(1).ToUInt();
if(size!=0 && size<UINT_MAX) //if it was a valid number
maxLinks = size;
unsigned int maxSize = lastUrls.size()-1;
for(unsigned int i=0; i<=maxSize && i< maxLinks; i++)
{
PutModule(nicks[maxSize-i] + ": " + lastUrls[maxSize-i]);
}
}
} else
{
PutModule("Unknown command! Try HELP.");
}
}
void CUrlBufferModule::LoadDefaults()
{
if(GetNV("enable")=="")
SetNV("enable", "true", true);
if(GetNV("enablelocal")=="")
SetNV("enablelocal", "false", true);
if(GetNV("buffersize")== "")
SetNV("buffersize", "5", true);
if(GetNV("enablepublic")=="")
SetNV("enablepublic", "true", true);
if(GetNV("bufferalllinks")=="")
SetNV("bufferalllinks", "false", true);
if(GetNV("reupload")=="")
SetNV("reupload", "true", true);
}
void CUrlBufferModule::CheckLineForLink(const CString& sMessage, const CString& sOrigin)
{
if(sOrigin != m_pUser->GetUserName() && GetNV("enable").ToBool() )
{
VCString words;
CString output;
sMessage.Split(" ", words, false, "", "", true, true);
for (size_t a = 0; a < words.size(); a++)
{
CString& word = words[a];
if(word.Left(4) == "http" || word.Left(4) == "www.")
{
//if you find an image download it, save it in the www directory and keep the new link in buffer
VCString tokens;
word.Split("/", tokens, false, "", "", true, true);
CString name = tokens[tokens.size()-1];
word.Split(".", tokens, false, "", "", true, true);
//if it's an image link download/upload it else just keep the link
time_t curtime;
time(&curtime);
CString dir = GetNV("directory") + CUtils::FormatTime(curtime,"%Y-%m-%d", m_pUser->GetTimezone()) + "/";
CString nickname = (sOrigin.empty())? m_pUser->GetUserName() : sOrigin;
if(!CFile::Exists(dir) && !CFile::IsDir(dir, false))
{
CDir::MakeDir(dir, 0775);
}
if(isValidExtension( tokens[tokens.size()-1] ))
{
std::stringstream ss;
if( GetNV("enablelocal").ToBool())
{
ss << "wget -b -O " << dir.c_str() << name <<" -q " << word.c_str() << " 2>&1";
getStdoutFromCommand(ss.str());
}
ss.str("");
if (!word.WildCmp("*imgur*") && GetNV("reupload").ToBool()) {
ss << "curl -d \"image=" << word.c_str() << "\" -d \"key=5ce86e7f95d8e58b18931bf290f387be\" http://api.imgur.com/2/upload.xml | sed -n 's/.*<original>\\(.*\\)<\\/original>.*/\\1/p' 2>&1";
output = getStdoutFromCommand(ss.str());
lastUrls.push_back(output);
} else {
lastUrls.push_back(word);
}
} else if(GetNV("bufferalllinks").ToBool()){
lastUrls.push_back(word);
//append nick:link to file
CString filename = dir + "links.txt";
std::ofstream log(filename.c_str() , std::ios_base::app | std::ios_base::out);
log << nickname << ": " << word << "\n";
}
nicks.push_back( nickname );
}
}
}
}
CString CUrlBufferModule::getStdoutFromCommand(const CString& cmd)
{
CString data="";
char buffer[128];
FILE* stream = popen(cmd.c_str(), "r");
if (stream == NULL || !stream || ferror(stream))
{
return "Error!";
}
while (!feof(stream))
{
if (fgets(buffer, 128, stream) != NULL)
data.append(buffer);
}
pclose(stream);
return data;
}
void CUrlBufferModule::CheckLineForTrigger(const CString& sMessage, const CString& sTarget)
{
if(GetNV("enablepublic").ToBool())
{
VCString words;
sMessage.Split(" ", words, false, "", "", true, true);
for (size_t a = 0; a < words.size(); a++)
{
CString& word = words[a];
if(word.AsLower() == "!showlinks")
{
if(lastUrls.empty())
PutIRC("PRIVMSG " + sTarget + " :No links were found...");
else
{
unsigned int maxLinks = GetNV("buffersize").ToUInt();
if (a+1 < words.size())
{
unsigned int size = words[a+1].ToUInt();
if(size!=0 && size<UINT_MAX) //if it was a valid number
maxLinks = size;
}
unsigned int maxSize = lastUrls.size()-1;
for(unsigned int i=0; i<=maxSize && i<maxLinks; i++)
{
sleep(1);
PutIRC("PRIVMSG " + sTarget + " :" + nicks[maxSize-i] + ": "+ lastUrls[maxSize-i]);
}
}
}
}
}
}
template<> void TModInfo<CUrlBufferModule>(CModInfo& Info) {
Info.SetWikiPage("urlbuffer");
Info.SetHasArgs(false);
}
USERMODULEDEFS(CUrlBufferModule, "Module that caches locally images/links posted on irc channels.")