mod_util.py

Mon, 11 Jan 2016 18:29:14 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Mon, 11 Jan 2016 18:29:14 +0200
changeset 164
e18f73e4c2e2
parent 162
d24fe5e3e420
permissions
-rw-r--r--

More Python 3 support

124
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
1 '''
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
2 Copyright 2014-2015 Teemu Piippo
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
3 All rights reserved.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
4
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
5 Redistribution and use in source and binary forms, with or without
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
6 modification, are permitted provided that the following conditions
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
7 are met:
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
8
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
9 1. Redistributions of source code must retain the above copyright
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
10 notice, this list of conditions and the following disclaimer.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
11 2. Redistributions in binary form must reproduce the above copyright
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
12 notice, this list of conditions and the following disclaimer in the
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
13 documentation and/or other materials provided with the distribution.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
14 3. The name of the author may not be used to endorse or promote products
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
15 derived from this software without specific prior written permission.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
16
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
27 '''
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
28
146
c17b82b1f573 Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents: 128
diff changeset
29 from __future__ import print_function
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 import math
76
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
31 import json
105
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
32 import re
154
df862cca1773 Added ability to define an IRC command with a function decorator instead of a manifest entry
Teemu Piippo <crimsondusk64@gmail.com>
parents: 153
diff changeset
33 import modulecore
121
ac07779f788d - reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
34 import utility
160
0ee4ff913747 Update to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 158
diff changeset
35 import calculator
153
497b7290977d More Python 3 rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 152
diff changeset
36 import urllib.parse
157
aaa5618e8dc2 Make cobalt not forget its pages the moment someone days something
Teemu Piippo <crimsondusk64@gmail.com>
parents: 155
diff changeset
37 import datetime
158
f96730dee026 Munge the committer's name when posting a new commit to avoid highlights
Teemu Piippo <crimsondusk64@gmail.com>
parents: 157
diff changeset
38 from munge import munge
160
0ee4ff913747 Update to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 158
diff changeset
39 from math import pi as π
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40
155
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
41 @modulecore.irc_command (args='<value> as <valuetype>')
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
42 def convert (bot, args, reply, error, **rest):
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
43 '''Performs numeric conversion.'''
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
44 try:
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
45 value = float (args['value'])
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
46 except Exception as e:
154
df862cca1773 Added ability to define an IRC command with a function decorator instead of a manifest entry
Teemu Piippo <crimsondusk64@gmail.com>
parents: 153
diff changeset
47 error (str (e))
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
48
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 valuetype = args['valuetype']
160
0ee4ff913747 Update to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 158
diff changeset
50
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 if valuetype in ['radians', 'degrees']:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 if valuetype == 'radians':
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 radvalue = value
160
0ee4ff913747 Update to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 158
diff changeset
54 degvalue = (value * 180.) / π
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 else:
160
0ee4ff913747 Update to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 158
diff changeset
56 radvalue = (value * π) / 180.
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 degvalue = value
160
0ee4ff913747 Update to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 158
diff changeset
58
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
59 reply ('%s radians, %s degrees (%s)' % (radvalue, degvalue, degvalue % 360.))
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 return
160
0ee4ff913747 Update to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 158
diff changeset
61
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 if valuetype in ['celsius', 'fahrenheit']:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 if valuetype == 'celsius':
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 celvalue = value
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 fahrvalue = value * 1.8 + 32
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 else:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 celvalue = (value - 32) / 1.8
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 fahrvalue = value
160
0ee4ff913747 Update to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 158
diff changeset
69
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
70 reply ('%s degrees celsius, %s degrees fahrenheit' % (celvalue, fahrvalue))
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 return
160
0ee4ff913747 Update to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 158
diff changeset
72
154
df862cca1773 Added ability to define an IRC command with a function decorator instead of a manifest entry
Teemu Piippo <crimsondusk64@gmail.com>
parents: 153
diff changeset
73 error ('unknown valuetype %s, expected one of: degrees, radians (angle conversion), ' +
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 'celsius, fahrenheit (temperature conversion)' % valuetype)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75
155
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
76 @modulecore.irc_command (args='<term...>')
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
77 def ud (bot, args, reply, error, **rest):
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
78 '''Looks up a term in urban dictionary.'''
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 try:
76
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
80 url = 'http://api.urbandictionary.com/v0/define?term=%s' % (args['term'].replace (' ', '%20'))
146
c17b82b1f573 Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents: 128
diff changeset
81 response = utility.read_url (url)
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 data = json.loads (response)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 if not 'list' in data \
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 or len(data['list']) == 0 \
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 or not 'word' in data['list'][0] \
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 or not 'definition' in data['list'][0]:
154
df862cca1773 Added ability to define an IRC command with a function decorator instead of a manifest entry
Teemu Piippo <crimsondusk64@gmail.com>
parents: 153
diff changeset
88 error ("couldn't find a definition of \002%s\002" % args['term'])
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 word = data['list'][0]['word']
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 definition = data['list'][0]['definition'].replace ('\r', ' ').replace ('\n', ' ').replace (' ', ' ')
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 up = data['list'][0]['thumbs_up']
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 down = data['list'][0]['thumbs_down']
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
94 reply ("\002%s\002: %s\0033 %d\003 up,\0035 %d\003 down" % (word, definition, up, down))
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 except Exception as e:
154
df862cca1773 Added ability to define an IRC command with a function decorator instead of a manifest entry
Teemu Piippo <crimsondusk64@gmail.com>
parents: 153
diff changeset
96 error ('Urban dictionary lookup failed: %s' % e)
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
97
155
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
98 @modulecore.irc_command()
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
99 def commands (bot, reply, ident, host, **rest):
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
100 '''Lists commands available to the calling user.'''
154
df862cca1773 Added ability to define an IRC command with a function decorator instead of a manifest entry
Teemu Piippo <crimsondusk64@gmail.com>
parents: 153
diff changeset
101 commandlist = modulecore.get_available_commands (ident, host)
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
102 partitioned=[]
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
103
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
104 while len (commandlist) > 0:
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
105 partitioned.append (commandlist[0:15])
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
106 commandlist = commandlist[15:]
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
107
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
108 for part in partitioned:
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
109 reply ('\002Available commands\002: %s' % (", ".join (part)))
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
110
155
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
111 @modulecore.irc_command (args='<command>')
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
112 def help (bot, reply, ident, host, args, error, **rest):
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
113 '''Prints help about a given command.'''
154
df862cca1773 Added ability to define an IRC command with a function decorator instead of a manifest entry
Teemu Piippo <crimsondusk64@gmail.com>
parents: 153
diff changeset
114 cmd = modulecore.get_command_by_name (args['command'])
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
115
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
116 if not cmd:
154
df862cca1773 Added ability to define an IRC command with a function decorator instead of a manifest entry
Teemu Piippo <crimsondusk64@gmail.com>
parents: 153
diff changeset
117 error ('unknown command \'%s\'' % args['command'])
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
118
154
df862cca1773 Added ability to define an IRC command with a function decorator instead of a manifest entry
Teemu Piippo <crimsondusk64@gmail.com>
parents: 153
diff changeset
119 if not modulecore.is_available (cmd, ident, host):
df862cca1773 Added ability to define an IRC command with a function decorator instead of a manifest entry
Teemu Piippo <crimsondusk64@gmail.com>
parents: 153
diff changeset
120 error ('you may not use %s' % cmd['name'])
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
121
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
122 reply ('%s %s: %s' % (cmd['name'], cmd['args'], cmd['description']))
105
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
123
155
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
124 @modulecore.irc_command()
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
125 def calcfunctions (bot, reply, **rest):
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
126 '''Lists the functions supported by .calc.'''
164
e18f73e4c2e2 More Python 3 support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 162
diff changeset
127 names = ', '.join (sorted (calculator.Functions.keys()))
162
d24fe5e3e420 Added cdf (aka Φ(x)) to the calculator, removed unneeded list comprehensions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 160
diff changeset
128 reply ('Available functions for .calc:', names)
105
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
129
155
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
130 @modulecore.irc_command (args='<expression...>')
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
131 def calc (bot, reply, args, **rest):
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
132 '''Calculates a mathematical expression.'''
160
0ee4ff913747 Update to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 158
diff changeset
133 reply (calculator.calc (args['expression']))
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
134
155
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
135 @modulecore.irc_command()
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
136 def more (commandObject, **rest):
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
137 '''Prints subsequent command result pages.'''
154
df862cca1773 Added ability to define an IRC command with a function decorator instead of a manifest entry
Teemu Piippo <crimsondusk64@gmail.com>
parents: 153
diff changeset
138 modulecore.print_responses (commandObject)
117
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 113
diff changeset
139
155
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
140 @modulecore.irc_command()
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
141 def yes (**k):
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
142 '''Confirms the previous command.'''
154
df862cca1773 Added ability to define an IRC command with a function decorator instead of a manifest entry
Teemu Piippo <crimsondusk64@gmail.com>
parents: 153
diff changeset
143 modulecore.confirm (k, True)
117
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 113
diff changeset
144
155
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
145 @modulecore.irc_command()
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
146 def no (**k):
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
147 '''Unconfirms the previous command.'''
154
df862cca1773 Added ability to define an IRC command with a function decorator instead of a manifest entry
Teemu Piippo <crimsondusk64@gmail.com>
parents: 153
diff changeset
148 modulecore.confirm (k, False)
121
ac07779f788d - reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
149
155
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
150 @modulecore.irc_command (args='<link...>')
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
151 def bitly (reply, args, **k):
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
152 '''Shortens a link using bit.ly.'''
121
ac07779f788d - reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
153 reply ('Result: %s' % utility.shorten_link (args['link']))
148
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
154
154
df862cca1773 Added ability to define an IRC command with a function decorator instead of a manifest entry
Teemu Piippo <crimsondusk64@gmail.com>
parents: 153
diff changeset
155 @modulecore.irc_command (args='<command...>')
df862cca1773 Added ability to define an IRC command with a function decorator instead of a manifest entry
Teemu Piippo <crimsondusk64@gmail.com>
parents: 153
diff changeset
156 def py (reply, args, **rest):
155
5a9b5065f53f Remove more manifests
Teemu Piippo <crimsondusk64@gmail.com>
parents: 154
diff changeset
157 '''Evaluates the given Python string using appspot.com.'''
153
497b7290977d More Python 3 rework
Teemu Piippo <crimsondusk64@gmail.com>
parents: 152
diff changeset
158 url = 'http://eval.appspot.com/eval?statement=' + urllib.parse.quote (args['command'])
154
df862cca1773 Added ability to define an IRC command with a function decorator instead of a manifest entry
Teemu Piippo <crimsondusk64@gmail.com>
parents: 153
diff changeset
159 result = utility.read_url (url, timeout=15).splitlines()
df862cca1773 Added ability to define an IRC command with a function decorator instead of a manifest entry
Teemu Piippo <crimsondusk64@gmail.com>
parents: 153
diff changeset
160
df862cca1773 Added ability to define an IRC command with a function decorator instead of a manifest entry
Teemu Piippo <crimsondusk64@gmail.com>
parents: 153
diff changeset
161 if not result:
df862cca1773 Added ability to define an IRC command with a function decorator instead of a manifest entry
Teemu Piippo <crimsondusk64@gmail.com>
parents: 153
diff changeset
162 reply ('No output.')
df862cca1773 Added ability to define an IRC command with a function decorator instead of a manifest entry
Teemu Piippo <crimsondusk64@gmail.com>
parents: 153
diff changeset
163 return
148
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
164
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
165 # \x0f is the 'reset colors' code, prepended to all reply lines to prevent other bots from
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
166 # reacting to this .py call.
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
167 if result[0] == 'Traceback (most recent call last):':
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
168 reply ('\x0f' + result[-1])
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
169 else:
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
170 for line in result:
157
aaa5618e8dc2 Make cobalt not forget its pages the moment someone days something
Teemu Piippo <crimsondusk64@gmail.com>
parents: 155
diff changeset
171 reply ('\x0f' + line)
aaa5618e8dc2 Make cobalt not forget its pages the moment someone days something
Teemu Piippo <crimsondusk64@gmail.com>
parents: 155
diff changeset
172
aaa5618e8dc2 Make cobalt not forget its pages the moment someone days something
Teemu Piippo <crimsondusk64@gmail.com>
parents: 155
diff changeset
173 @modulecore.irc_command (args='<format...>')
aaa5618e8dc2 Make cobalt not forget its pages the moment someone days something
Teemu Piippo <crimsondusk64@gmail.com>
parents: 155
diff changeset
174 def strftime (reply, args, **rest):
aaa5618e8dc2 Make cobalt not forget its pages the moment someone days something
Teemu Piippo <crimsondusk64@gmail.com>
parents: 155
diff changeset
175 reply ('\x0f' + datetime.datetime.utcnow().strftime (args['format']))

mercurial