aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBlas Oliva <blasoliva@gmail.com>2015-02-17 16:56:58 -0300
committerBlas Oliva <blasoliva@gmail.com>2015-02-17 16:56:58 -0300
commitd81c2cb36a72c5df61f7ce63ced3996760ad28b5 (patch)
tree00d64a3ccab0eab55955072d98bab80949d3fd16
parentf938e17b9c09bd206db8a10a75691718a37a556a (diff)
downloadreebot-d81c2cb36a72c5df61f7ce63ced3996760ad28b5.tar.gz
reebot-d81c2cb36a72c5df61f7ce63ced3996760ad28b5.zip
Añadir funciones Reebot y parseador de comandos
-rw-r--r--messages.py4
-rwxr-xr-xreebot.py55
-rw-r--r--reefuncs.py55
3 files changed, 88 insertions, 26 deletions
diff --git a/messages.py b/messages.py
index 406043b..916840e 100644
--- a/messages.py
+++ b/messages.py
@@ -6,6 +6,6 @@ messages = {
6 "che" : "Che, I'm here!", 6 "che" : "Che, I'm here!",
7 "hello" : "Hola ", 7 "hello" : "Hola ",
8 "goodbye" : "Bye bye", 8 "goodbye" : "Bye bye",
9 "welcome" : "¡Hola! mi nombre es ReeBot y te doy la bienvenida al canal de chat de Reevo, la Red de Educación Alternativa. El chat es un espacio de comunicación frecuentado por las personas que colaboran a diario en este proyecto colectivo. Escribe tu mensaje en el canal y seguramente alguien te responderá muy pronto...", 9 "welcome" : ["¡Hola! mi nombre es ReeBot y te doy la bienvenida al canal de chat de Reevo, la Red de Educación Alternativa. El chat es un espacio de comunicación frecuentado por las personas que colaboran a diario en este proyecto colectivo. Escribe tu mensaje en el canal y seguramente alguien te responderá muy pronto...",
10 "welcome_part2" : "Por cierto, yo solo soy un robot con una inteligencia artificial muy limitada así que no te entusiasmes hablando conmigo :). Si quieres saber mas sobre mi, revisa mi perfil: http://evo.re/reebot" 10 "Por cierto, yo solo soy un robot con una inteligencia artificial muy limitada así que no te entusiasmes hablando conmigo :). Si quieres saber mas sobre mi, revisa mi perfil: http://evo.re/reebot"],
11} 11}
diff --git a/reebot.py b/reebot.py
index 899d7c5..58bd6d9 100755
--- a/reebot.py
+++ b/reebot.py
@@ -1,5 +1,6 @@
1#!/usr/bin/python 1#!/usr/bin/python
2""" rBot: Reevo IRC client 2nd generation""" 2# -*- coding: utf-8 -*-
3""" Reebot: Reevo IRC client 2nd generation"""
3 4
4import irclib 5import irclib
5irclib.DEBUG = False 6irclib.DEBUG = False
@@ -13,6 +14,7 @@ import re
13from config import * 14from config import *
14from messages import messages 15from messages import messages
15from reemongo import reemongo 16from reemongo import reemongo
17from reefuncs import *
16 18
17# Set default encoding 19# Set default encoding
18reload(sys) 20reload(sys)
@@ -44,10 +46,13 @@ class rBot:
44 46
45 # Register handlers 47 # Register handlers
46 self.irc.add_global_handler( 'ping', self.ponger, -42 ) 48 self.irc.add_global_handler( 'ping', self.ponger, -42 )
47 self.irc.add_global_handler( 'privmsg', self.handleprivmessage ) 49 self.irc.add_global_handler( 'privmsg', self.handlemessage )
48 self.irc.add_global_handler( 'pubmsg', self.handlepubmessage ) 50 self.irc.add_global_handler( 'pubmsg', self.handlemessage )
49 self.irc.add_global_handler( 'join', self.handlejoin ) 51 self.irc.add_global_handler( 'join', self.handlejoin )
50 52
53 # Reefunc instance
54 self.rf = reefuncs()
55
51 # Server connection checker 56 # Server connection checker
52 if self.server.is_connected(): 57 if self.server.is_connected():
53 self.feed_refresh() 58 self.feed_refresh()
@@ -63,19 +68,14 @@ class rBot:
63 """ Send pong command """ 68 """ Send pong command """
64 connection.pong(event.target()) 69 connection.pong(event.target())
65 70
66 def handleprivmessage (self, connection, event): 71 def reegex(self, argument):
67 """Handle private messages function 72 """ Check arguments """
68 73 r = re.compile('(^reebot (.*))')
69 argument -- message 74 m = re.match(r, argument)
70 source -- origin of the message (nickname) 75 if m:
71 """ 76 return m.group(2)
72 argument = event.arguments() [0].lower() 77
73 source = event.source().split( '!' ) [0] 78 def handlemessage (self, connection, event):
74
75 if argument.find ( 'hola ' + nickname ) == 0:
76 self.sendmessage( source, messages['hello'] + source )
77
78 def handlepubmessage (self, connection, event):
79 """ Handle public messages function 79 """ Handle public messages function
80 80
81 argument -- message 81 argument -- message
@@ -84,9 +84,18 @@ class rBot:
84 """ 84 """
85 argument = event.arguments() [0].lower() 85 argument = event.arguments() [0].lower()
86 source = event.source().split( '!' ) [0] 86 source = event.source().split( '!' ) [0]
87 target = event.target() 87 target = channels_list[0]
88 88
89 if argument.find ( 'hola ' + nickname ) == 0: 89 if self.reegex(argument):
90 try:
91 for lines in self.rf.argparser(self.reegex(argument)).splitlines():
92 self.sendmessage( target, lines )
93 time.sleep(1) # Prevent floods
94 except:
95 # Avoid stdout messages
96 # TODO: Reebot hangs when argparse prints help message (-h/--help)
97 pass
98 elif argument.find ( 'hola ' + nickname ) == 0:
90 self.sendmessage( target, messages['hello'] + source ) 99 self.sendmessage( target, messages['hello'] + source )
91 100
92 def handlejoin(self, connection, event): 101 def handlejoin(self, connection, event):
@@ -107,12 +116,10 @@ class rBot:
107 "channel" : [ target ] 116 "channel" : [ target ]
108 } ) 117 } )
109 # Send welcome message to user 118 # Send welcome message to user
110 self.sendmessage( source, messages['welcome'] ) 119 for m in messages['welcome']:
111 time.sleep(3) 120 self.sendmessage( source, m )
112 self.sendmessage( source, messages['welcome_part2'] ) 121 time.sleep(3)
113 else: 122
114 pass
115
116 def feed_refresh(self): 123 def feed_refresh(self):
117 """ Read feeds and sends the news to the channel """ 124 """ Read feeds and sends the news to the channel """
118 125
diff --git a/reefuncs.py b/reefuncs.py
new file mode 100644
index 0000000..64bd036
--- /dev/null
+++ b/reefuncs.py
@@ -0,0 +1,55 @@
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4import argparse
5import subprocess
6
7class reefuncs:
8 """ Parse arguments and execute functions
9
10 usage: reebot [-h] [-t TASK [TASK ...]] [-p SERVER [SERVER ...]] [-m MESSAGE [MESSAGE ...]]
11
12 Reebot functions
13
14 optional arguments:
15 -h, --help show this help message and exit
16 -t TASK [TASK ...], --task TASK [TASK ...]
17 show task status
18 -p SERVER [SERVER ...], --ping SERVER [SERVER ...]
19 send ping to server
20 -m MESSAGE [MESSAGE ...], --msg MESSAGE [MESSAGE ...]
21 send message
22 """
23 def __init__(self):
24 pass
25
26 def argparser(self,arguments):
27 # Create parser
28 parser = argparse.ArgumentParser(prog='reebot', description='Reebot functions')
29
30 # Adding arguments
31 parser.add_argument('-t', '--task', type=self.task, help='show task status', action='store', dest='task', nargs='+')
32 parser.add_argument('-p', '--ping', type=self.ping, help='send ping to server', action='store', dest='server', nargs='+')
33 parser.add_argument('-m', '--msg', help='send message', action='store', dest='message', nargs='+')
34
35 # Parsing arguments
36 args = parser.parse_args(arguments.split())
37
38 # Put responses in a list (the library may already do this...)
39 responses = [args.task, args.server, args.message]
40
41 # Return responses
42 for r in responses:
43 if r:
44 return ' ' . join(r)
45
46 def task(self, task):
47 return "Print the task number %s" % task
48
49 def ping(self, host):
50 ping = ['ping','-v', '-c 3', host]
51 process = subprocess.Popen(ping, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
52 out, err = process.communicate()
53 return out
54
55