Skip to content
This repository was archived by the owner on Jun 5, 2018. It is now read-only.

Commit 887d013

Browse files
committed
Merge pull request #11 from ben51/master
Ask rhonrhon for vieille URLs
2 parents 7bc46f0 + 333c88c commit 887d013

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

rhonrhon.py

+47-11
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def has_expected_mode(path, mode):
5858

5959
configFile = os.path.expanduser("~") + '/.rhonrhonrc'
6060
if not has_expected_mode(configFile, 0o600):
61-
print("err: invalid mode on configFile", configFile)
61+
print("err: invalid mode on configFile, should be 600", configFile)
6262
sys.exit(2)
6363

6464
exec(open(configFile).read())
@@ -283,14 +283,8 @@ def on_pubmsg(self, serv, ev):
283283
urls = re.findall('(https?://[^\s]+)', pl)
284284
urls_copy = list(urls)
285285
for url in urls_copy:
286-
urlbody = {
287-
'query': {
288-
'match_phrase': {'urls': url}
289-
},
290-
'size': 1
291-
}
292-
res = es.search(index=es_idx, doc_type=channel, body=urlbody)
293-
for rep in res['hits']['hits']:
286+
(vieille, rep) = self.vieille(url, channel)
287+
if vieille:
294288
try:
295289
msg = '{0}: VIEUX ! The URL [ {1} ] has been posted '
296290
msg = msg + 'by {2} the {3} at {4}.'
@@ -311,7 +305,6 @@ def on_pubmsg(self, serv, ev):
311305
pass
312306

313307
urls.remove(url)
314-
break
315308

316309
has_nick = False
317310
tonick = []
@@ -350,13 +343,39 @@ def handle_noauth_privcmd(self, serv, ev, s):
350343
self.showrage(serv, ev, 'priv')
351344
return True
352345

346+
# Ask rhonrhon weither a list of URLs are old or not.
347+
# Syntax: urls?:? (#channel)? text containing URLs.
348+
# The default channel is #gcu.
349+
# Ex: url: #gcu c'est bon la rhonrhon ? https://www.google.com
350+
# Ex: urls http://www.bonjourmadame.fr ou alors
351+
# http://bonjourlesroux.tumblr.com
352+
if re.match('^urls?:?$', s[0]):
353+
i = 1
354+
if re.match('^#.*$', s[1]):
355+
channel = s[1].replace('#', '')
356+
i += 1
357+
else:
358+
channel = 'gcu'
359+
360+
for url in [x for x in s[i:] if re.match('(https?://[^\s]+)', x)]:
361+
if len(url) > 262:
362+
msg = 'SAYTROPLONG [ {0} ]'
363+
else:
364+
(vieille, rep) = self.vieille(url, channel)
365+
if vieille:
366+
msg = 'VIEUX ! [ {0} ]'
367+
else:
368+
msg = 'SAYBON [ {0} ]'
369+
serv.privmsg(ev.source.nick, msg.format(url))
370+
return True
371+
353372
return False
354373

355374
def on_privmsg(self, serv, ev):
356375
pl = ev.arguments[0]
357376
s = pl.split(' ')
358377
if not s:
359-
return # no command passed (is it even possible ? :)
378+
return # no command passed (is it even possible ? :) )
360379
if self.handle_noauth_privcmd(serv, ev, s) is True:
361380
return # a publicly accessible command was provided
362381
if not ev.source.nick in auth.keys():
@@ -461,6 +480,23 @@ def on_quit(self, serv, ev):
461480
self._refresh_all_chans() # quit doesn't set any target
462481
self._user_unregister(ev.source)
463482

483+
def vieille(self, url, channel):
484+
urlbody = {
485+
'query': {
486+
'match_phrase': {'urls': url}
487+
},
488+
'size': 1
489+
}
490+
try:
491+
res = es.search(index=es_idx, doc_type=channel, body=urlbody)
492+
for rep in res['hits']['hits']:
493+
return (True, rep)
494+
except Exception as e:
495+
logger.warn(e)
496+
pass
497+
return (False, [])
498+
499+
464500

465501
foreground = False
466502

0 commit comments

Comments
 (0)