Compare commits
126 Commits
Author | SHA1 | Date |
---|---|---|
Brian S. Stephan | a1a256ca3b | |
Brian S. Stephan | ee6ae7080e | |
Brian S. Stephan | 333424025b | |
Brian S. Stephan | 98abab560e | |
Brian S. Stephan | 86e55cb812 | |
Brian S. Stephan | 420a7b1472 | |
Brian S. Stephan | a214d8acfd | |
Brian S. Stephan | 93c522037f | |
Brian S. Stephan | b7976c46d1 | |
Brian S. Stephan | d962b275ff | |
Brian S. Stephan | f898f35ce6 | |
Brian S. Stephan | 4289f95800 | |
Brian S. Stephan | 572ecddceb | |
Brian S. Stephan | 3aadde4b71 | |
Brian S. Stephan | c2d26f404e | |
Brian S. Stephan | ecaabbce89 | |
Brian S. Stephan | 051e656a82 | |
Brian S. Stephan | 0ea54a5ee2 | |
Brian S. Stephan | ffcdc3f8d8 | |
Brian S. Stephan | cff1a183cf | |
Brian S. Stephan | 68f7c80b7e | |
Brian S. Stephan | 7baa70d8f6 | |
Brian S. Stephan | 39290fb63c | |
Brian S. Stephan | 55d856b8fd | |
Brian S. Stephan | 88ea0dbbb4 | |
Brian S. Stephan | cfeddfdc4e | |
Brian S. Stephan | d516c1b08e | |
Brian S. Stephan | 667a85aa46 | |
Brian S. Stephan | 0227b74eee | |
Brian S. Stephan | 76a052e091 | |
Brian S. Stephan | 19cd23879f | |
Brian S. Stephan | f59dc35b25 | |
Brian S. Stephan | debf086b8d | |
Brian S. Stephan | ec1767e38b | |
Brian S. Stephan | 0bfe3f9549 | |
Brian S. Stephan | 363ec49097 | |
Brian S. Stephan | 8549c2ef8a | |
Brian S. Stephan | d24f74e53f | |
Brian S. Stephan | f812857d75 | |
Brian S. Stephan | 3d0be3c25a | |
Brian S. Stephan | 8b3caabb57 | |
Brian S. Stephan | 1cf0364268 | |
Brian S. Stephan | 337e4db650 | |
Brian S. Stephan | 95396802de | |
Brian S. Stephan | 4bcf06d5e7 | |
Brian S. Stephan | 54bf00b167 | |
Brian S. Stephan | 133c1df638 | |
Brian S. Stephan | 7c44becaa0 | |
Brian S. Stephan | 76bcdd5fef | |
Brian S. Stephan | 1f5dc50d89 | |
Brian S. Stephan | bc7108cfb7 | |
Brian S. Stephan | dfdd6d6dc5 | |
Brian S. Stephan | 75a1b8d7f3 | |
Brian S. Stephan | fd5c4dad1a | |
Brian S. Stephan | 7264deee2a | |
Brian S. Stephan | 76e5546bcb | |
Brian S. Stephan | 739d3fa2b7 | |
Brian S. Stephan | 6ab86f773c | |
Brian S. Stephan | 3d5e6754e8 | |
Brian S. Stephan | ab1d3456ad | |
Brian S. Stephan | a550bf07ac | |
Brian S. Stephan | 3fe0a8e59e | |
Brian S. Stephan | 0ccb49e7ed | |
Brian S. Stephan | 651399f5fc | |
Brian S. Stephan | 0b3386e183 | |
Brian S. Stephan | b21421a395 | |
Brian S. Stephan | 9368c8b823 | |
Brian S. Stephan | def5964658 | |
Brian S. Stephan | e43807fb27 | |
Brian S. Stephan | b3b8f832a2 | |
Brian S. Stephan | b971b72af8 | |
Brian S. Stephan | 40286eeafc | |
Brian S. Stephan | d888c5f03b | |
Brian S. Stephan | d7b7bdf73d | |
Brian S. Stephan | a03c69258f | |
Brian S. Stephan | 43f2b09057 | |
Brian S. Stephan | 3aa3fb14e4 | |
Brian S. Stephan | 1fc8af09f8 | |
Brian S. Stephan | 53c874dc21 | |
Brian S. Stephan | 1036c08147 | |
Brian S. Stephan | 9c1109107b | |
Brian S. Stephan | 6136127c5f | |
Brian S. Stephan | 44d8b7db00 | |
Brian S. Stephan | d518cb2b77 | |
Brian S. Stephan | cbbf6eb311 | |
Brian S. Stephan | 2f4156ce26 | |
Brian S. Stephan | e64af1a0a1 | |
Brian S. Stephan | ca8798453c | |
Brian S. Stephan | dc5f243608 | |
Brian S. Stephan | ab0d738851 | |
Brian S. Stephan | 9d94155f66 | |
Brian S. Stephan | a6f8fc5dc1 | |
Brian S. Stephan | bcc5f767ba | |
Brian S. Stephan | a0a1aa10f4 | |
Brian S. Stephan | d5e5343193 | |
Brian S. Stephan | da815a1fc3 | |
Brian S. Stephan | ef08cec0fb | |
Brian S. Stephan | 691ee7696b | |
Brian S. Stephan | 665f56a430 | |
Brian S. Stephan | 4d94322c55 | |
Brian S. Stephan | 10d73f570a | |
Brian S. Stephan | 819bbe74c6 | |
Brian S. Stephan | 1833430c5d | |
Brian S. Stephan | 42a1efed79 | |
Brian S. Stephan | c670072c86 | |
Brian S. Stephan | d5e1a2ed45 | |
Brian S. Stephan | 5f6e255ded | |
Brian S. Stephan | c0c0306419 | |
Brian S. Stephan | 56d0e26c6d | |
Brian S. Stephan | b42d0ac0e9 | |
Brian S. Stephan | 31758b80b6 | |
Brian S. Stephan | 802072caed | |
Brian S. Stephan | 9e4bc595a4 | |
Brian S. Stephan | d34fb18949 | |
Brian S. Stephan | abce0262f3 | |
Brian S. Stephan | b917f78ca5 | |
Brian S. Stephan | c2aa3df13f | |
Brian S. Stephan | 6b8dd1a645 | |
Brian S. Stephan | 649a148209 | |
Brian S. Stephan | 8fcc8365e3 | |
Brian S. Stephan | f78d407d4c | |
Brian S. Stephan | 8528152483 | |
Brian S. Stephan | f2fb0a26a4 | |
Brian S. Stephan | 0f88715ffd | |
Brian S. Stephan | 2f98a64cdd | |
Brian S. Stephan | 0589939137 |
|
@ -0,0 +1 @@
|
||||||
|
dr_botzo/_version.py export-subst
|
|
@ -1,14 +1,10 @@
|
||||||
*.facts
|
.idea/
|
||||||
*.json
|
build/
|
||||||
*.log
|
dist/
|
||||||
*.pyc
|
tags/
|
||||||
*.sqlite3
|
*.egg-info/
|
||||||
*.swo
|
.tox/
|
||||||
*.swp
|
.coverage
|
||||||
*.urls
|
|
||||||
*~
|
|
||||||
.idea
|
|
||||||
tags
|
|
||||||
dr.botzo.data
|
dr.botzo.data
|
||||||
dr.botzo.cfg
|
dr.botzo.cfg
|
||||||
localsettings.py
|
localsettings.py
|
||||||
|
@ -18,3 +14,11 @@ parsetab.py
|
||||||
megahal.*
|
megahal.*
|
||||||
dr.botzo.log
|
dr.botzo.log
|
||||||
dr.botzo.markov
|
dr.botzo.markov
|
||||||
|
*.facts
|
||||||
|
*.log
|
||||||
|
*.pyc
|
||||||
|
*.sqlite3
|
||||||
|
*.swo
|
||||||
|
*.swp
|
||||||
|
*.urls
|
||||||
|
*~
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
doc-warnings: true
|
|
||||||
strictness: high
|
|
||||||
ignore-paths:
|
|
||||||
- migrations
|
|
||||||
ignore-patterns:
|
|
||||||
- \.log$
|
|
||||||
- localsettings.py$
|
|
||||||
- parsetab.py$
|
|
||||||
pylint:
|
|
||||||
enable:
|
|
||||||
- relative-import
|
|
||||||
options:
|
|
||||||
max-line-length: 120
|
|
||||||
good-names: log
|
|
||||||
pep8:
|
|
||||||
options:
|
|
||||||
max-line-length: 120
|
|
||||||
pep257:
|
|
||||||
disable:
|
|
||||||
- D203
|
|
|
@ -1,10 +1,44 @@
|
||||||
## Contributing
|
# Contributing Guidelines
|
||||||
|
|
||||||
This is a pretty fun, non-serious hacking project, so if you're interested in
|
This is a pretty fun, non-serious hacking project, so if you're interested in
|
||||||
contributing, sign up, clone the project, and submit some merge requests!
|
contributing, sign up, clone the project, and submit some merge requests!
|
||||||
There's a lot to add and work on, so join in.
|
There's a lot to add and work on, so join in.
|
||||||
|
|
||||||
|
dr.botzo is made available under the GPLv3 license. Contributions are welcome via pull requests. This document outlines
|
||||||
|
the process to get your contribution accepted.
|
||||||
|
|
||||||
## Code Style
|
## Code Style
|
||||||
|
|
||||||
4 spaces per indent level. 120 character line length. Follow PEP8 as closely
|
4 spaces per indent level. 120 character line length. Follow PEP8 as closely
|
||||||
as reasonable. There's a prospector config, use it.
|
as reasonable. There's a prospector config, use it.
|
||||||
|
|
||||||
|
## Sign Offs/Custody of Contributions
|
||||||
|
|
||||||
|
I do not request the copyright of contributions be assigned to me or to the project, and I require no provision that I
|
||||||
|
be allowed to relicense your contributions. My personal oath is to maintain inbound=outbound in my open source projects,
|
||||||
|
and the expectation is authors are responsible for their contributions.
|
||||||
|
|
||||||
|
I am following the the [Developer Certificate of Origin (DCO)](https://developercertificate.org/), also available at
|
||||||
|
`DCO.txt`. The DCO is a way for contributors to certify that they wrote or otherwise have the right to license their
|
||||||
|
code contributions to the project. Contributors must sign-off that they adhere to these requirements by adding a
|
||||||
|
`Signed-off-by` line to their commit message, and/or, for frequent contributors, by signing off on their entry in
|
||||||
|
`MAINTAINERS.md`.
|
||||||
|
|
||||||
|
This process is followed by a number of open source projects, most notably the Linux kernel. Here's the gist of it:
|
||||||
|
|
||||||
|
```
|
||||||
|
[Your normal Git commit message here.]
|
||||||
|
|
||||||
|
Signed-off-by: Random J Developer <random@developer.example.org>
|
||||||
|
```
|
||||||
|
|
||||||
|
`git help commit` has more info on adding this:
|
||||||
|
|
||||||
|
```
|
||||||
|
-s, --signoff
|
||||||
|
Add Signed-off-by line by the committer at the end of the commit log
|
||||||
|
message. The meaning of a signoff depends on the project, but it typically
|
||||||
|
certifies that committer has the rights to submit this work under the same
|
||||||
|
license and agrees to a Developer Certificate of Origin (see
|
||||||
|
http://developercertificate.org/ for more information).
|
||||||
|
```
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
Developer Certificate of Origin
|
||||||
|
Version 1.1
|
||||||
|
|
||||||
|
Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
|
||||||
|
|
||||||
|
Everyone is permitted to copy and distribute verbatim copies of this
|
||||||
|
license document, but changing it is not allowed.
|
||||||
|
|
||||||
|
|
||||||
|
Developer's Certificate of Origin 1.1
|
||||||
|
|
||||||
|
By making a contribution to this project, I certify that:
|
||||||
|
|
||||||
|
(a) The contribution was created in whole or in part by me and I
|
||||||
|
have the right to submit it under the open source license
|
||||||
|
indicated in the file; or
|
||||||
|
|
||||||
|
(b) The contribution is based upon previous work that, to the best
|
||||||
|
of my knowledge, is covered under an appropriate open source
|
||||||
|
license and I have the right under that license to submit that
|
||||||
|
work with modifications, whether created in whole or in part
|
||||||
|
by me, under the same open source license (unless I am
|
||||||
|
permitted to submit under a different license), as indicated
|
||||||
|
in the file; or
|
||||||
|
|
||||||
|
(c) The contribution was provided directly to me by some other
|
||||||
|
person who certified (a), (b) or (c) and I have not modified
|
||||||
|
it.
|
||||||
|
|
||||||
|
(d) I understand and agree that this project and the contribution
|
||||||
|
are public and that a record of the contribution (including all
|
||||||
|
personal information I submit with it, including my sign-off) is
|
||||||
|
maintained indefinitely and may be redistributed consistent with
|
||||||
|
this project or the open source license(s) involved.
|
|
@ -1,7 +1,7 @@
|
||||||
GNU GENERAL PUBLIC LICENSE
|
GNU GENERAL PUBLIC LICENSE
|
||||||
Version 3, 29 June 2007
|
Version 3, 29 June 2007
|
||||||
|
|
||||||
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||||
Everyone is permitted to copy and distribute verbatim copies
|
Everyone is permitted to copy and distribute verbatim copies
|
||||||
of this license document, but changing it is not allowed.
|
of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
|
@ -645,7 +645,7 @@ the "copyright" line and a pointer to where the full notice is found.
|
||||||
GNU General Public License for more details.
|
GNU General Public License for more details.
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
Also add information on how to contact you by electronic and paper mail.
|
Also add information on how to contact you by electronic and paper mail.
|
||||||
|
|
||||||
|
@ -664,11 +664,11 @@ might be different; for a GUI interface, you would use an "about box".
|
||||||
You should also get your employer (if you work as a programmer) or school,
|
You should also get your employer (if you work as a programmer) or school,
|
||||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||||
For more information on this, and how to apply and follow the GNU GPL, see
|
For more information on this, and how to apply and follow the GNU GPL, see
|
||||||
<http://www.gnu.org/licenses/>.
|
<https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
The GNU General Public License does not permit incorporating your program
|
The GNU General Public License does not permit incorporating your program
|
||||||
into proprietary programs. If your program is a subroutine library, you
|
into proprietary programs. If your program is a subroutine library, you
|
||||||
may consider it more useful to permit linking proprietary applications with
|
may consider it more useful to permit linking proprietary applications with
|
||||||
the library. If this is what you want to do, use the GNU Lesser General
|
the library. If this is what you want to do, use the GNU Lesser General
|
||||||
Public License instead of this License. But first, please read
|
Public License instead of this License. But first, please read
|
||||||
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
|
<https://www.gnu.org/licenses/why-not-lgpl.html>.
|
|
@ -0,0 +1,10 @@
|
||||||
|
# Maintainers
|
||||||
|
|
||||||
|
This file contains information about people permitted to make major decisions and direction on the project.
|
||||||
|
|
||||||
|
## Contributing Under the DCO
|
||||||
|
|
||||||
|
By adding your name and email address to this section, you certify that all of your subsequent contributions to dr.botzo
|
||||||
|
are made under the terms of the Developer's Certificate of Origin 1.1, available at `DCO.txt`.
|
||||||
|
|
||||||
|
* Brian S. Stephan (<bss@incorporeal.org>)
|
|
@ -0,0 +1,8 @@
|
||||||
|
include versioneer.py
|
||||||
|
include dr_botzo/_version.py
|
||||||
|
graft dr_botzo/templates
|
||||||
|
graft facts/templates
|
||||||
|
graft ircbot/templates
|
||||||
|
graft karma/templates
|
||||||
|
graft markbot/templates
|
||||||
|
graft races/templates
|
|
@ -5,4 +5,10 @@ from django.contrib import admin
|
||||||
from countdown.models import CountdownItem
|
from countdown.models import CountdownItem
|
||||||
|
|
||||||
|
|
||||||
admin.site.register(CountdownItem)
|
class CountdownItemAdmin(admin.ModelAdmin):
|
||||||
|
"""Custom display for the countdown items."""
|
||||||
|
|
||||||
|
list_display = ('__str__', 'reminder_message')
|
||||||
|
|
||||||
|
|
||||||
|
admin.site.register(CountdownItem, CountdownItemAdmin)
|
||||||
|
|
|
@ -12,6 +12,7 @@ from django.utils import timezone
|
||||||
|
|
||||||
from countdown.models import CountdownItem
|
from countdown.models import CountdownItem
|
||||||
from ircbot.lib import Plugin, most_specific_message
|
from ircbot.lib import Plugin, most_specific_message
|
||||||
|
from ircbot.models import IrcChannel
|
||||||
|
|
||||||
log = logging.getLogger('countdown.ircplugin')
|
log = logging.getLogger('countdown.ircplugin')
|
||||||
|
|
||||||
|
@ -22,12 +23,14 @@ class Countdown(Plugin):
|
||||||
new_reminder_regex = (r'remind\s+(?P<who>[^\s]+)\s+(?P<when_type>at|in|on)\s+(?P<when>.*?)\s+'
|
new_reminder_regex = (r'remind\s+(?P<who>[^\s]+)\s+(?P<when_type>at|in|on)\s+(?P<when>.*?)\s+'
|
||||||
r'(and\s+every\s+(?P<recurring_period>.*?)\s+)?'
|
r'(and\s+every\s+(?P<recurring_period>.*?)\s+)?'
|
||||||
r'(until\s+(?P<recurring_until>.*?)\s+)?'
|
r'(until\s+(?P<recurring_until>.*?)\s+)?'
|
||||||
r'(to|that|about)\s+(?P<text>.*)')
|
r'(to|that|about|with)\s+(?P<text>.*?)'
|
||||||
|
r'(?=\s+\("(?P<name>.*)"\)|$)')
|
||||||
|
|
||||||
def __init__(self, bot, connection, event):
|
def __init__(self, bot, connection, event):
|
||||||
"""Initialize some stuff."""
|
"""Initialize some stuff."""
|
||||||
self.running_reminders = []
|
self.running_reminders = []
|
||||||
self.send_reminders = True
|
self.send_reminders = True
|
||||||
|
self.server_config = connection.server_config
|
||||||
|
|
||||||
t = threading.Thread(target=self.reminder_thread)
|
t = threading.Thread(target=self.reminder_thread)
|
||||||
t.daemon = True
|
t.daemon = True
|
||||||
|
@ -42,7 +45,7 @@ class Countdown(Plugin):
|
||||||
|
|
||||||
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!countdown\s+list$',
|
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!countdown\s+list$',
|
||||||
self.handle_item_list, -20)
|
self.handle_item_list, -20)
|
||||||
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!countdown\s+(\S+)$',
|
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!countdown\s+(.+)$',
|
||||||
self.handle_item_detail, -20)
|
self.handle_item_detail, -20)
|
||||||
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], self.new_reminder_regex,
|
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], self.new_reminder_regex,
|
||||||
self.handle_new_reminder, -50)
|
self.handle_new_reminder, -50)
|
||||||
|
@ -65,20 +68,22 @@ class Countdown(Plugin):
|
||||||
# TODO: figure out if we need this sleep, which exists so we don't send reminders while still connecting to IRC
|
# TODO: figure out if we need this sleep, which exists so we don't send reminders while still connecting to IRC
|
||||||
time.sleep(30)
|
time.sleep(30)
|
||||||
while self.send_reminders:
|
while self.send_reminders:
|
||||||
reminders = CountdownItem.objects.filter(is_reminder=True, sent_reminder=False, at_time__lte=timezone.now())
|
reminders = CountdownItem.objects.filter(is_reminder=True, sent_reminder=False,
|
||||||
|
at_time__lte=timezone.now(),
|
||||||
|
reminder_target_new__server=self.server_config)
|
||||||
|
|
||||||
for reminder in reminders:
|
for reminder in reminders:
|
||||||
log.debug("%s @ %s", reminder.reminder_message, reminder.at_time)
|
log.debug("%s @ %s", reminder.reminder_message, reminder.at_time)
|
||||||
if reminder.at_time <= timezone.now():
|
if reminder.at_time <= timezone.now():
|
||||||
log.info("sending %s to %s", reminder.reminder_message, reminder.reminder_target)
|
log.info("sending %s to %s", reminder.reminder_message, reminder.reminder_target_new.name)
|
||||||
self.bot.reply(None, reminder.reminder_message, explicit_target=reminder.reminder_target)
|
self.bot.reply(None, reminder.reminder_message, explicit_target=reminder.reminder_target_new.name)
|
||||||
|
|
||||||
# if recurring and not hit until, set a new at time, otherwise stop reminding
|
# if recurring and not hit until, set a new at time, otherwise stop reminding
|
||||||
if reminder.recurring_until is not None and timezone.now() >= reminder.recurring_until:
|
if reminder.recurring_until is not None and timezone.now() >= reminder.recurring_until:
|
||||||
reminder.sent_reminder = True
|
reminder.sent_reminder = True
|
||||||
elif reminder.recurring_period != '':
|
elif reminder.recurring_period != '':
|
||||||
calendar = pdt.Calendar()
|
calendar = pdt.Calendar()
|
||||||
when_t = calendar.parseDT(reminder.recurring_period, reminder.at_time,
|
when_t = calendar.parseDT(f'in one {reminder.recurring_period}', reminder.at_time,
|
||||||
tzinfo=reminder.at_time.tzinfo)[0]
|
tzinfo=reminder.at_time.tzinfo)[0]
|
||||||
if reminder.recurring_until is None or when_t <= reminder.recurring_until:
|
if reminder.recurring_until is None or when_t <= reminder.recurring_until:
|
||||||
reminder.at_time = when_t
|
reminder.at_time = when_t
|
||||||
|
@ -99,9 +104,16 @@ class Countdown(Plugin):
|
||||||
recurring_period = match.group('recurring_period')
|
recurring_period = match.group('recurring_period')
|
||||||
recurring_until = match.group('recurring_until')
|
recurring_until = match.group('recurring_until')
|
||||||
text = match.group('text')
|
text = match.group('text')
|
||||||
|
name = match.group('name')
|
||||||
log.debug("%s / %s / %s", who, when, text)
|
log.debug("%s / %s / %s", who, when, text)
|
||||||
|
|
||||||
|
if not name:
|
||||||
item_name = '{0:s}-{1:s}'.format(event.sender_nick, timezone.now().strftime('%s'))
|
item_name = '{0:s}-{1:s}'.format(event.sender_nick, timezone.now().strftime('%s'))
|
||||||
|
else:
|
||||||
|
if CountdownItem.objects.filter(name=name).count() > 0:
|
||||||
|
self.bot.reply(event, "item with name '{0:s}' already exists".format(name))
|
||||||
|
return 'NO MORE'
|
||||||
|
item_name = name
|
||||||
|
|
||||||
# parse when to send the notification
|
# parse when to send the notification
|
||||||
if when_type == 'in':
|
if when_type == 'in':
|
||||||
|
@ -135,8 +147,12 @@ class Countdown(Plugin):
|
||||||
|
|
||||||
log.debug("%s / %s / %s", item_name, when_t, message)
|
log.debug("%s / %s / %s", item_name, when_t, message)
|
||||||
|
|
||||||
|
# get the IrcChannel to send to
|
||||||
|
reminder_target, _ = IrcChannel.objects.get_or_create(name=event.sent_location,
|
||||||
|
server=connection.server_config)
|
||||||
|
|
||||||
countdown_item = CountdownItem.objects.create(name=item_name, at_time=when_t, is_reminder=True,
|
countdown_item = CountdownItem.objects.create(name=item_name, at_time=when_t, is_reminder=True,
|
||||||
reminder_message=message, reminder_target=event.sent_location)
|
reminder_message=message, reminder_target_new=reminder_target)
|
||||||
if recurring_period:
|
if recurring_period:
|
||||||
countdown_item.recurring_period = recurring_period
|
countdown_item.recurring_period = recurring_period
|
||||||
if recurring_until:
|
if recurring_until:
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# Generated by Django 1.10.5 on 2017-02-23 02:25
|
# Generated by Django 1.10.5 on 2017-02-23 02:25
|
||||||
from __future__ import unicode_literals
|
|
||||||
|
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# Generated by Django 1.10.5 on 2017-02-24 01:06
|
# Generated by Django 1.10.5 on 2017-02-24 01:06
|
||||||
from __future__ import unicode_literals
|
|
||||||
|
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# Generated by Django 1.10.5 on 2017-02-24 01:12
|
# Generated by Django 1.10.5 on 2017-02-24 01:12
|
||||||
from __future__ import unicode_literals
|
|
||||||
|
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
# Generated by Django 3.1.2 on 2020-10-25 17:16
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('countdown', '0005_countdownitem_recurring_until'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='countdownitem',
|
||||||
|
name='recurring_period',
|
||||||
|
field=models.CharField(blank=True, default='', max_length=64),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='countdownitem',
|
||||||
|
name='reminder_target',
|
||||||
|
field=models.CharField(blank=True, default='', max_length=64),
|
||||||
|
),
|
||||||
|
]
|
|
@ -0,0 +1,20 @@
|
||||||
|
# Generated by Django 3.1.2 on 2021-04-26 00:10
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('ircbot', '0018_ircserver_replace_irc_control_with_markdown'),
|
||||||
|
('countdown', '0006_auto_20201025_1716'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='countdownitem',
|
||||||
|
name='reminder_target_new',
|
||||||
|
field=models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.CASCADE, to='ircbot.ircchannel'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -1,14 +1,8 @@
|
||||||
"""Countdown item models."""
|
"""Countdown item models."""
|
||||||
|
|
||||||
import logging
|
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger('countdown.models')
|
|
||||||
|
|
||||||
|
|
||||||
class CountdownItem(models.Model):
|
class CountdownItem(models.Model):
|
||||||
"""Track points in time."""
|
"""Track points in time."""
|
||||||
|
|
||||||
|
@ -19,13 +13,15 @@ class CountdownItem(models.Model):
|
||||||
sent_reminder = models.BooleanField(default=False)
|
sent_reminder = models.BooleanField(default=False)
|
||||||
|
|
||||||
reminder_message = models.TextField(default="")
|
reminder_message = models.TextField(default="")
|
||||||
reminder_target = models.CharField(max_length=64, default='')
|
reminder_target = models.CharField(max_length=64, blank=True, default='')
|
||||||
|
reminder_target_new = models.ForeignKey('ircbot.IrcChannel', null=True, blank=True,
|
||||||
|
default=None, on_delete=models.CASCADE)
|
||||||
|
|
||||||
recurring_period = models.CharField(max_length=64, default='')
|
recurring_period = models.CharField(max_length=64, blank=True, default='')
|
||||||
recurring_until = models.DateTimeField(null=True, blank=True, default=None)
|
recurring_until = models.DateTimeField(null=True, blank=True, default=None)
|
||||||
|
|
||||||
created_time = models.DateTimeField(auto_now_add=True)
|
created_time = models.DateTimeField(auto_now_add=True)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""String representation."""
|
"""Summarize object."""
|
||||||
return "{0:s} @ {1:s}".format(self.name, timezone.localtime(self.at_time).strftime('%Y-%m-%d %H:%M:%S %Z'))
|
return "{0:s} @ {1:s}".format(self.name, timezone.localtime(self.at_time).strftime('%Y-%m-%d %H:%M:%S %Z'))
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
"""REST serializers for countdown items."""
|
||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
from countdown.models import CountdownItem
|
||||||
|
|
||||||
|
|
||||||
|
class CountdownItemSerializer(serializers.ModelSerializer):
|
||||||
|
"""Countdown item serializer for the REST API."""
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
"""Meta options."""
|
||||||
|
|
||||||
|
model = CountdownItem
|
||||||
|
fields = '__all__'
|
|
@ -0,0 +1,13 @@
|
||||||
|
"""URL patterns for the countdown views."""
|
||||||
|
from django.conf.urls import include
|
||||||
|
from django.urls import path
|
||||||
|
from rest_framework.routers import DefaultRouter
|
||||||
|
|
||||||
|
from countdown.views import CountdownItemViewSet
|
||||||
|
|
||||||
|
router = DefaultRouter()
|
||||||
|
router.register(r'items', CountdownItemViewSet)
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path('api/', include(router.urls)),
|
||||||
|
]
|
|
@ -0,0 +1,14 @@
|
||||||
|
"""Provide an interface to countdown items."""
|
||||||
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
from rest_framework.viewsets import ReadOnlyModelViewSet
|
||||||
|
|
||||||
|
from countdown.models import CountdownItem
|
||||||
|
from countdown.serializers import CountdownItemSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class CountdownItemViewSet(ReadOnlyModelViewSet):
|
||||||
|
"""Provide list and detail actions for countdown items."""
|
||||||
|
|
||||||
|
queryset = CountdownItem.objects.all()
|
||||||
|
serializer_class = CountdownItemSerializer
|
||||||
|
permission_classes = [IsAuthenticated]
|
|
@ -1,36 +1,31 @@
|
||||||
"""Roll dice when asked, intended for RPGs."""
|
"""Roll dice when asked, intended for RPGs."""
|
||||||
|
|
||||||
# this breaks yacc, but ply might be happy in py3
|
|
||||||
#from __future__ import unicode_literals
|
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import math
|
|
||||||
import re
|
|
||||||
import random
|
import random
|
||||||
|
import re
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
from irc.client import NickMask
|
from irc.client import NickMask
|
||||||
|
|
||||||
import ply.lex as lex
|
from dice.roller import DiceRoller
|
||||||
import ply.yacc as yacc
|
|
||||||
|
|
||||||
from ircbot.lib import Plugin
|
from ircbot.lib import Plugin
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Dice(Plugin):
|
class Dice(Plugin):
|
||||||
|
|
||||||
"""Roll simple or complex dice strings."""
|
"""Roll simple or complex dice strings."""
|
||||||
|
|
||||||
|
def __init__(self, bot, connection, event):
|
||||||
|
"""Set up the plugin."""
|
||||||
|
self.roller = DiceRoller()
|
||||||
|
|
||||||
|
super(Dice, self).__init__(bot, connection, event)
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
"""Set up the handlers."""
|
"""Set up the handlers."""
|
||||||
|
|
||||||
self.roller = DiceRoller()
|
|
||||||
|
|
||||||
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!roll\s+(.*)$',
|
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!roll\s+(.*)$',
|
||||||
self.handle_roll, -20)
|
self.handle_roll, -20)
|
||||||
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!ctech\s+(.*)$',
|
|
||||||
self.handle_ctech, -20)
|
|
||||||
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!random\s+(.*)$',
|
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!random\s+(.*)$',
|
||||||
self.handle_random, -20)
|
self.handle_random, -20)
|
||||||
|
|
||||||
|
@ -38,398 +33,48 @@ class Dice(Plugin):
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""Tear down handlers."""
|
"""Tear down handlers."""
|
||||||
|
|
||||||
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_roll)
|
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_roll)
|
||||||
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_ctech)
|
|
||||||
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_random)
|
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_random)
|
||||||
|
|
||||||
super(Dice, self).stop()
|
super(Dice, self).stop()
|
||||||
|
|
||||||
def handle_random(self, connection, event, match):
|
def handle_random(self, connection, event, match):
|
||||||
"""Handle the !random command which picks an item from a list."""
|
"""Handle the !random command which picks an item from a list."""
|
||||||
|
|
||||||
nick = NickMask(event.source).nick
|
nick = NickMask(event.source).nick
|
||||||
choices = match.group(1)
|
choices = match.group(1)
|
||||||
|
|
||||||
choices_list = choices.split(' ')
|
choices_list = choices.split(' ')
|
||||||
choice = random.choice(choices_list)
|
choice = random.SystemRandom().choice(choices_list)
|
||||||
|
|
||||||
logger.debug(event.recursing)
|
logger.debug(event.recursing)
|
||||||
if event.recursing:
|
if event.recursing:
|
||||||
reply = "{0:s}".format(choice)
|
reply = "{0:s}".format(choice)
|
||||||
else:
|
elif settings.DICE_PREFIX_ROLLER:
|
||||||
reply = "{0:s}: {1:s}".format(nick, choice)
|
reply = "{0:s}: {1:s}".format(nick, choice)
|
||||||
|
else:
|
||||||
|
reply = "{0:s}".format(choice)
|
||||||
return self.bot.reply(event, reply)
|
return self.bot.reply(event, reply)
|
||||||
|
|
||||||
def handle_roll(self, connection, event, match):
|
def handle_roll(self, connection, event, match):
|
||||||
"""Handle the !roll command which covers most common dice stuff."""
|
"""Handle the !roll command which covers most common dice stuff."""
|
||||||
|
|
||||||
nick = NickMask(event.source).nick
|
nick = NickMask(event.source).nick
|
||||||
dicestr = match.group(1)
|
dicestr = match.group(1)
|
||||||
|
|
||||||
logger.debug(event.recursing)
|
logger.debug(event.recursing)
|
||||||
|
try:
|
||||||
|
reply_str = self.roller.do_roll(dicestr)
|
||||||
|
except AssertionError as aex:
|
||||||
|
reply_str = f"Could not roll dice: {aex}"
|
||||||
|
except ValueError:
|
||||||
|
reply_str = "Unable to parse roll"
|
||||||
|
|
||||||
if event.recursing:
|
if event.recursing:
|
||||||
reply = "{0:s}".format(self.roller.do_roll(dicestr))
|
reply = "{0:s}".format(reply_str)
|
||||||
|
elif settings.DICE_PREFIX_ROLLER:
|
||||||
|
reply = "{0:s}: {1:s}".format(nick, reply_str)
|
||||||
else:
|
else:
|
||||||
reply = "{0:s}: {1:s}".format(nick, self.roller.do_roll(dicestr))
|
reply = "{0:s}".format(reply_str)
|
||||||
return self.bot.reply(event, re.sub(r'(\d+)(.*?\s+)(\(.*?\))', r'\1\214\3', reply))
|
return self.bot.reply(event, re.sub(r'(\d+)(.*?\s+)(\(.*?\))', r'\1\214\3', reply))
|
||||||
|
|
||||||
def handle_ctech(self, connection, event, match):
|
|
||||||
"""Handle cthulhutech dice rolls."""
|
|
||||||
|
|
||||||
nick = NickMask(event.source).nick
|
|
||||||
rollitrs = re.split(';\s*', match.group(1))
|
|
||||||
reply = ""
|
|
||||||
for count, roll in enumerate(rollitrs):
|
|
||||||
pattern = '^(\d+)d(?:(\+|\-)(\d+))?(?:\s+(.*))?'
|
|
||||||
regex = re.compile(pattern)
|
|
||||||
matches = regex.search(roll)
|
|
||||||
if matches is not None:
|
|
||||||
dice = int(matches.group(1))
|
|
||||||
modifier = 0
|
|
||||||
|
|
||||||
if matches.group(2) is not None and matches.group(3) is not None:
|
|
||||||
if str(matches.group(2)) == '-':
|
|
||||||
modifier = -1 * int(matches.group(3))
|
|
||||||
else:
|
|
||||||
modifier = int(matches.group(3))
|
|
||||||
|
|
||||||
result = roll + ': '
|
|
||||||
|
|
||||||
rolls = []
|
|
||||||
for d in range(dice):
|
|
||||||
rolls.append(random.randint(1, 10))
|
|
||||||
rolls.sort()
|
|
||||||
rolls.reverse()
|
|
||||||
|
|
||||||
# highest single die method
|
|
||||||
method1 = rolls[0]
|
|
||||||
|
|
||||||
# highest set method
|
|
||||||
method2 = 0
|
|
||||||
rolling_sum = 0
|
|
||||||
for i, r in enumerate(rolls):
|
|
||||||
# if next roll is same as current, sum and continue, else see if sum is best so far
|
|
||||||
if i+1 < len(rolls) and rolls[i+1] == r:
|
|
||||||
if rolling_sum == 0:
|
|
||||||
rolling_sum = r
|
|
||||||
rolling_sum += r
|
|
||||||
else:
|
|
||||||
if rolling_sum > method2:
|
|
||||||
method2 = rolling_sum
|
|
||||||
rolling_sum = 0
|
|
||||||
# check for set in progress (e.g. lots of 1s)
|
|
||||||
if rolling_sum > method2:
|
|
||||||
method2 = rolling_sum
|
|
||||||
|
|
||||||
# straight method
|
|
||||||
method3 = 0
|
|
||||||
rolling_sum = 0
|
|
||||||
count = 0
|
|
||||||
for i, r in enumerate(rolls):
|
|
||||||
# if next roll is one less as current, sum and continue, else check len and see if sum is best so far
|
|
||||||
if i+1 < len(rolls) and rolls[i+1] == r-1:
|
|
||||||
if rolling_sum == 0:
|
|
||||||
rolling_sum = r
|
|
||||||
count += 1
|
|
||||||
rolling_sum += r-1
|
|
||||||
count += 1
|
|
||||||
else:
|
|
||||||
if count >= 3 and rolling_sum > method3:
|
|
||||||
method3 = rolling_sum
|
|
||||||
rolling_sum = 0
|
|
||||||
# check for straight in progress (e.g. straight ending in 1)
|
|
||||||
if count >= 3 and rolling_sum > method3:
|
|
||||||
method3 = rolling_sum
|
|
||||||
|
|
||||||
# get best roll
|
|
||||||
best = max([method1, method2, method3])
|
|
||||||
|
|
||||||
# check for critical failure
|
|
||||||
botch = False
|
|
||||||
ones = 0
|
|
||||||
for r in rolls:
|
|
||||||
if r == 1:
|
|
||||||
ones += 1
|
|
||||||
if ones >= math.ceil(float(len(rolls))/2):
|
|
||||||
botch = True
|
|
||||||
|
|
||||||
if botch:
|
|
||||||
result += 'BOTCH'
|
|
||||||
else:
|
|
||||||
result += str(best + modifier)
|
|
||||||
rollres = ''
|
|
||||||
for i,r in enumerate(rolls):
|
|
||||||
rollres += str(r)
|
|
||||||
if i is not len(rolls)-1:
|
|
||||||
rollres += ','
|
|
||||||
result += ' [' + rollres
|
|
||||||
if modifier != 0:
|
|
||||||
if modifier > 0:
|
|
||||||
result += ' +' + str(modifier)
|
|
||||||
else:
|
|
||||||
result += ' -' + str(modifier * -1)
|
|
||||||
result += ']'
|
|
||||||
|
|
||||||
reply += result
|
|
||||||
if count is not len(rollitrs)-1:
|
|
||||||
reply += "; "
|
|
||||||
if reply is not "":
|
|
||||||
msg = "{0:s}: {1:s}".format(nick, reply)
|
|
||||||
return self.bot.reply(event, msg)
|
|
||||||
|
|
||||||
|
|
||||||
class DiceRoller(object):
|
|
||||||
|
|
||||||
tokens = ['NUMBER', 'TEXT', 'ROLLSEP']
|
|
||||||
literals = ['#', '/', '+', '-', 'd']
|
|
||||||
|
|
||||||
t_TEXT = r'\s+[^;]+'
|
|
||||||
t_ROLLSEP = r';\s*'
|
|
||||||
|
|
||||||
def build(self):
|
|
||||||
lex.lex(module=self)
|
|
||||||
yacc.yacc(module=self)
|
|
||||||
|
|
||||||
def t_NUMBER(self, t):
|
|
||||||
r'\d+'
|
|
||||||
t.value = int(t.value)
|
|
||||||
return t
|
|
||||||
|
|
||||||
def t_error(self, t):
|
|
||||||
t.lexer.skip(1)
|
|
||||||
|
|
||||||
precedence = (
|
|
||||||
('left', 'ROLLSEP'),
|
|
||||||
('left', '+', '-'),
|
|
||||||
('right', 'd'),
|
|
||||||
('left', '#'),
|
|
||||||
('left', '/')
|
|
||||||
)
|
|
||||||
|
|
||||||
output = ""
|
|
||||||
|
|
||||||
def roll_dice(self, keep, dice, size):
|
|
||||||
"""Takes the parsed dice string for a single roll (eg 3/4d20) and performs
|
|
||||||
the actual roll. Returns a string representing the result.
|
|
||||||
"""
|
|
||||||
|
|
||||||
a = list(range(dice))
|
|
||||||
for i in range(dice):
|
|
||||||
a[i] = random.randint(1, size)
|
|
||||||
if keep != dice:
|
|
||||||
b = sorted(a, reverse=True)
|
|
||||||
b = b[0:keep]
|
|
||||||
else:
|
|
||||||
b = a
|
|
||||||
total = sum(b)
|
|
||||||
outstr = "[" + ",".join(str(i) for i in a) + "]"
|
|
||||||
|
|
||||||
return (total, outstr)
|
|
||||||
|
|
||||||
def process_roll(self, trials, mods, comment):
|
|
||||||
"""Processes rolls coming from the parser.
|
|
||||||
|
|
||||||
This generates the inputs for the roll_dice() command, and returns
|
|
||||||
the full string representing the whole current dice string (the part
|
|
||||||
up to a semicolon or end of line).
|
|
||||||
"""
|
|
||||||
|
|
||||||
output = ""
|
|
||||||
repeat = 1
|
|
||||||
if trials != None:
|
|
||||||
repeat = trials
|
|
||||||
for i in range(repeat):
|
|
||||||
mode = 1
|
|
||||||
total = 0
|
|
||||||
curr_str = ""
|
|
||||||
if i > 0:
|
|
||||||
output += ", "
|
|
||||||
for m in mods:
|
|
||||||
keep = 0
|
|
||||||
dice = 1
|
|
||||||
res = 0
|
|
||||||
# if m is a tuple, then it is a die roll
|
|
||||||
# m[0] = (keep, num dice)
|
|
||||||
# m[1] = num faces on the die
|
|
||||||
if type(m) == tuple:
|
|
||||||
if m[0] != None:
|
|
||||||
if m[0][0] != None:
|
|
||||||
keep = m[0][0]
|
|
||||||
dice = m[0][1]
|
|
||||||
size = m[1]
|
|
||||||
if keep > dice or keep == 0:
|
|
||||||
keep = dice
|
|
||||||
if size < 1:
|
|
||||||
output = "# of sides for die is incorrect: %d" % size
|
|
||||||
return output
|
|
||||||
if dice < 1:
|
|
||||||
output = "# of dice is incorrect: %d" % dice
|
|
||||||
return output
|
|
||||||
res = self.roll_dice(keep, dice, size)
|
|
||||||
curr_str += "%d%s" % (res[0], res[1])
|
|
||||||
res = res[0]
|
|
||||||
elif m == "+":
|
|
||||||
mode = 1
|
|
||||||
curr_str += "+"
|
|
||||||
elif m == "-":
|
|
||||||
mode = -1
|
|
||||||
curr_str += "-"
|
|
||||||
else:
|
|
||||||
res = m
|
|
||||||
curr_str += str(m)
|
|
||||||
total += mode * res
|
|
||||||
if repeat == 1:
|
|
||||||
if comment != None:
|
|
||||||
output = "%d %s (%s)" % (total, comment.strip(), curr_str)
|
|
||||||
else:
|
|
||||||
output = "%d (%s)" % (total, curr_str)
|
|
||||||
else:
|
|
||||||
output += "%d (%s)" % (total, curr_str)
|
|
||||||
if i == repeat - 1:
|
|
||||||
if comment != None:
|
|
||||||
output += " (%s)" % (comment.strip())
|
|
||||||
return output
|
|
||||||
|
|
||||||
def p_roll_r(self, p):
|
|
||||||
# Chain rolls together.
|
|
||||||
|
|
||||||
# General idea I had when creating this grammar: A roll string is a chain
|
|
||||||
# of modifiers, which may be repeated for a certain number of trials. It can
|
|
||||||
# have a comment that describes the roll
|
|
||||||
# Multiple roll strings can be chained with semicolon
|
|
||||||
|
|
||||||
'roll : roll ROLLSEP roll'
|
|
||||||
global output
|
|
||||||
p[0] = p[1] + "; " + p[3]
|
|
||||||
output = p[0]
|
|
||||||
|
|
||||||
def p_roll(self, p):
|
|
||||||
# Parse a basic roll string.
|
|
||||||
|
|
||||||
'roll : trial modifier comment'
|
|
||||||
global output
|
|
||||||
mods = []
|
|
||||||
if type(p[2]) == list:
|
|
||||||
mods = p[2]
|
|
||||||
else:
|
|
||||||
mods = [p[2]]
|
|
||||||
p[0] = self.process_roll(p[1], mods, p[3])
|
|
||||||
output = p[0]
|
|
||||||
|
|
||||||
def p_roll_no_trials(self, p):
|
|
||||||
# Parse a roll string without trials.
|
|
||||||
|
|
||||||
'roll : modifier comment'
|
|
||||||
global output
|
|
||||||
mods = []
|
|
||||||
if type(p[1]) == list:
|
|
||||||
mods = p[1]
|
|
||||||
else:
|
|
||||||
mods = [p[1]]
|
|
||||||
p[0] = self.process_roll(None, mods, p[2])
|
|
||||||
output = p[0]
|
|
||||||
|
|
||||||
def p_comment(self, p):
|
|
||||||
# Parse a comment.
|
|
||||||
|
|
||||||
'''comment : TEXT
|
|
||||||
|'''
|
|
||||||
if len(p) == 2:
|
|
||||||
p[0] = p[1]
|
|
||||||
else:
|
|
||||||
p[0] = None
|
|
||||||
|
|
||||||
def p_modifier(self, p):
|
|
||||||
# Parse a modifier on a roll string.
|
|
||||||
|
|
||||||
'''modifier : modifier "+" modifier
|
|
||||||
| modifier "-" modifier'''
|
|
||||||
# Use append to prevent nested lists (makes dealing with this easier)
|
|
||||||
if type(p[1]) == list:
|
|
||||||
p[1].append(p[2])
|
|
||||||
p[1].append(p[3])
|
|
||||||
p[0] = p[1]
|
|
||||||
elif type(p[3]) == list:
|
|
||||||
p[3].insert(0, p[2])
|
|
||||||
p[3].insert(0, p[1])
|
|
||||||
p[0] = p[3]
|
|
||||||
else:
|
|
||||||
p[0] = [p[1], p[2], p[3]]
|
|
||||||
|
|
||||||
def p_die(self, p):
|
|
||||||
# Return the left side before the "d", and the number of faces.
|
|
||||||
|
|
||||||
'modifier : left NUMBER'
|
|
||||||
p[0] = (p[1], p[2])
|
|
||||||
|
|
||||||
def p_die_num(self, p):
|
|
||||||
'modifier : NUMBER'
|
|
||||||
p[0] = p[1]
|
|
||||||
|
|
||||||
def p_left(self, p):
|
|
||||||
# Parse the number of dice we are rolling, and how many we are keeping.
|
|
||||||
'left : keep dice'
|
|
||||||
if p[1] == None:
|
|
||||||
p[0] = [None, p[2]]
|
|
||||||
else:
|
|
||||||
p[0] = [p[1], p[2]]
|
|
||||||
|
|
||||||
def p_left_all(self, p):
|
|
||||||
'left : dice'
|
|
||||||
p[0] = [None, p[1]]
|
|
||||||
|
|
||||||
def p_left_e(self, p):
|
|
||||||
'left :'
|
|
||||||
p[0] = None
|
|
||||||
|
|
||||||
def p_total(self, p):
|
|
||||||
'trial : NUMBER "#"'
|
|
||||||
if len(p) > 1:
|
|
||||||
p[0] = p[1]
|
|
||||||
else:
|
|
||||||
p[0] = None
|
|
||||||
|
|
||||||
def p_keep(self, p):
|
|
||||||
'keep : NUMBER "/"'
|
|
||||||
if p[1] != None:
|
|
||||||
p[0] = p[1]
|
|
||||||
else:
|
|
||||||
p[0] = None
|
|
||||||
|
|
||||||
def p_dice(self, p):
|
|
||||||
'dice : NUMBER "d"'
|
|
||||||
p[0] = p[1]
|
|
||||||
|
|
||||||
def p_dice_one(self, p):
|
|
||||||
'dice : "d"'
|
|
||||||
p[0] = 1
|
|
||||||
|
|
||||||
def p_error(self, p):
|
|
||||||
# Provide the user with something (albeit not much) when the roll can't be parsed.
|
|
||||||
global output
|
|
||||||
output = "Unable to parse roll"
|
|
||||||
|
|
||||||
def get_result(self):
|
|
||||||
global output
|
|
||||||
return output
|
|
||||||
|
|
||||||
def do_roll(self, dicestr):
|
|
||||||
"""
|
|
||||||
Roll some dice and get the result (with broken out rolls).
|
|
||||||
|
|
||||||
Keyword arguments:
|
|
||||||
dicestr - format:
|
|
||||||
N#X/YdS+M label
|
|
||||||
N#: do the following roll N times (optional)
|
|
||||||
X/: take the top X rolls of the Y times rolled (optional)
|
|
||||||
Y : roll the die specified Y times (optional, defaults to 1)
|
|
||||||
dS: roll a S-sided die
|
|
||||||
+M: add M to the result (-M for subtraction) (optional)
|
|
||||||
"""
|
|
||||||
self.build()
|
|
||||||
yacc.parse(dicestr)
|
|
||||||
return self.get_result()
|
|
||||||
|
|
||||||
|
|
||||||
plugin = Dice
|
plugin = Dice
|
||||||
|
|
|
@ -0,0 +1,260 @@
|
||||||
|
"""Dice rollers used by the views, bots, etc."""
|
||||||
|
import logging
|
||||||
|
import random
|
||||||
|
|
||||||
|
import ply.lex as lex
|
||||||
|
import ply.yacc as yacc
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class DiceRoller(object):
|
||||||
|
|
||||||
|
tokens = ['NUMBER', 'TEXT', 'ROLLSEP']
|
||||||
|
literals = ['#', '/', '+', '-', 'd']
|
||||||
|
|
||||||
|
t_TEXT = r'\s+[^;]+'
|
||||||
|
t_ROLLSEP = r';\s*'
|
||||||
|
|
||||||
|
def build(self):
|
||||||
|
lex.lex(module=self)
|
||||||
|
yacc.yacc(module=self)
|
||||||
|
|
||||||
|
def t_NUMBER(self, t):
|
||||||
|
r'\d+'
|
||||||
|
t.value = int(t.value)
|
||||||
|
return t
|
||||||
|
|
||||||
|
def t_error(self, t):
|
||||||
|
t.lexer.skip(1)
|
||||||
|
|
||||||
|
precedence = (
|
||||||
|
('left', 'ROLLSEP'),
|
||||||
|
('left', '+', '-'),
|
||||||
|
('right', 'd'),
|
||||||
|
('left', '#'),
|
||||||
|
('left', '/')
|
||||||
|
)
|
||||||
|
|
||||||
|
output = ""
|
||||||
|
|
||||||
|
def roll_dice(self, keep, dice, size):
|
||||||
|
"""Takes the parsed dice string for a single roll (eg 3/4d20) and performs
|
||||||
|
the actual roll. Returns a string representing the result.
|
||||||
|
"""
|
||||||
|
|
||||||
|
a = list(range(dice))
|
||||||
|
for i in range(dice):
|
||||||
|
a[i] = random.randint(1, size)
|
||||||
|
if keep != dice:
|
||||||
|
b = sorted(a, reverse=True)
|
||||||
|
b = b[0:keep]
|
||||||
|
else:
|
||||||
|
b = a
|
||||||
|
total = sum(b)
|
||||||
|
outstr = "[" + ",".join(str(i) for i in a) + "]"
|
||||||
|
|
||||||
|
return (total, outstr)
|
||||||
|
|
||||||
|
def process_roll(self, trials, mods, comment):
|
||||||
|
"""Processes rolls coming from the parser.
|
||||||
|
|
||||||
|
This generates the inputs for the roll_dice() command, and returns
|
||||||
|
the full string representing the whole current dice string (the part
|
||||||
|
up to a semicolon or end of line).
|
||||||
|
"""
|
||||||
|
rolls = mods[0][0][1] if mods[0][0][1] else 1
|
||||||
|
if trials:
|
||||||
|
assert trials <= 10, "Too many rolls (max: 10)."
|
||||||
|
assert rolls <= 50, "Too many dice (max: 50) in roll."
|
||||||
|
|
||||||
|
output = ""
|
||||||
|
repeat = 1
|
||||||
|
if trials is not None:
|
||||||
|
repeat = trials
|
||||||
|
for i in range(repeat):
|
||||||
|
mode = 1
|
||||||
|
total = 0
|
||||||
|
curr_str = ""
|
||||||
|
if i > 0:
|
||||||
|
output += ", "
|
||||||
|
for m in mods:
|
||||||
|
keep = 0
|
||||||
|
dice = 1
|
||||||
|
res = 0
|
||||||
|
# if m is a tuple, then it is a die roll
|
||||||
|
# m[0] = (keep, num dice)
|
||||||
|
# m[1] = num faces on the die
|
||||||
|
if type(m) == tuple:
|
||||||
|
if m[0] is not None:
|
||||||
|
if m[0][0] is not None:
|
||||||
|
keep = m[0][0]
|
||||||
|
dice = m[0][1]
|
||||||
|
size = m[1]
|
||||||
|
if keep > dice or keep == 0:
|
||||||
|
keep = dice
|
||||||
|
assert size >= 1, f"Die must have at least one side."
|
||||||
|
assert dice >= 1, f"At least one die must be rolled."
|
||||||
|
res = self.roll_dice(keep, dice, size)
|
||||||
|
curr_str += "%d%s" % (res[0], res[1])
|
||||||
|
res = res[0]
|
||||||
|
elif m == "+":
|
||||||
|
mode = 1
|
||||||
|
curr_str += "+"
|
||||||
|
elif m == "-":
|
||||||
|
mode = -1
|
||||||
|
curr_str += "-"
|
||||||
|
else:
|
||||||
|
res = m
|
||||||
|
curr_str += str(m)
|
||||||
|
total += mode * res
|
||||||
|
if repeat == 1:
|
||||||
|
if comment is not None:
|
||||||
|
output = "%d %s (%s)" % (total, comment.strip(), curr_str)
|
||||||
|
else:
|
||||||
|
output = "%d (%s)" % (total, curr_str)
|
||||||
|
else:
|
||||||
|
output += "%d (%s)" % (total, curr_str)
|
||||||
|
if i == repeat - 1:
|
||||||
|
if comment is not None:
|
||||||
|
output += " (%s)" % (comment.strip())
|
||||||
|
return output
|
||||||
|
|
||||||
|
def p_roll_r(self, p):
|
||||||
|
# Chain rolls together.
|
||||||
|
|
||||||
|
# General idea I had when creating this grammar: A roll string is a chain
|
||||||
|
# of modifiers, which may be repeated for a certain number of trials. It can
|
||||||
|
# have a comment that describes the roll
|
||||||
|
# Multiple roll strings can be chained with semicolon
|
||||||
|
|
||||||
|
'roll : roll ROLLSEP roll'
|
||||||
|
global output
|
||||||
|
p[0] = p[1] + "; " + p[3]
|
||||||
|
output = p[0]
|
||||||
|
|
||||||
|
def p_roll(self, p):
|
||||||
|
# Parse a basic roll string.
|
||||||
|
|
||||||
|
'roll : trial modifier comment'
|
||||||
|
global output
|
||||||
|
mods = []
|
||||||
|
if type(p[2]) == list:
|
||||||
|
mods = p[2]
|
||||||
|
else:
|
||||||
|
mods = [p[2]]
|
||||||
|
p[0] = self.process_roll(p[1], mods, p[3])
|
||||||
|
output = p[0]
|
||||||
|
|
||||||
|
def p_roll_no_trials(self, p):
|
||||||
|
# Parse a roll string without trials.
|
||||||
|
|
||||||
|
'roll : modifier comment'
|
||||||
|
global output
|
||||||
|
mods = []
|
||||||
|
if type(p[1]) == list:
|
||||||
|
mods = p[1]
|
||||||
|
else:
|
||||||
|
mods = [p[1]]
|
||||||
|
p[0] = self.process_roll(None, mods, p[2])
|
||||||
|
output = p[0]
|
||||||
|
|
||||||
|
def p_comment(self, p):
|
||||||
|
# Parse a comment.
|
||||||
|
|
||||||
|
'''comment : TEXT
|
||||||
|
|'''
|
||||||
|
if len(p) == 2:
|
||||||
|
p[0] = p[1]
|
||||||
|
else:
|
||||||
|
p[0] = None
|
||||||
|
|
||||||
|
def p_modifier(self, p):
|
||||||
|
# Parse a modifier on a roll string.
|
||||||
|
|
||||||
|
'''modifier : modifier "+" modifier
|
||||||
|
| modifier "-" modifier'''
|
||||||
|
# Use append to prevent nested lists (makes dealing with this easier)
|
||||||
|
if type(p[1]) == list:
|
||||||
|
p[1].append(p[2])
|
||||||
|
p[1].append(p[3])
|
||||||
|
p[0] = p[1]
|
||||||
|
elif type(p[3]) == list:
|
||||||
|
p[3].insert(0, p[2])
|
||||||
|
p[3].insert(0, p[1])
|
||||||
|
p[0] = p[3]
|
||||||
|
else:
|
||||||
|
p[0] = [p[1], p[2], p[3]]
|
||||||
|
|
||||||
|
def p_die(self, p):
|
||||||
|
# Return the left side before the "d", and the number of faces.
|
||||||
|
|
||||||
|
'modifier : left NUMBER'
|
||||||
|
p[0] = (p[1], p[2])
|
||||||
|
|
||||||
|
def p_die_num(self, p):
|
||||||
|
'modifier : NUMBER'
|
||||||
|
p[0] = p[1]
|
||||||
|
|
||||||
|
def p_left(self, p):
|
||||||
|
# Parse the number of dice we are rolling, and how many we are keeping.
|
||||||
|
'left : keep dice'
|
||||||
|
if p[1] == None:
|
||||||
|
p[0] = [None, p[2]]
|
||||||
|
else:
|
||||||
|
p[0] = [p[1], p[2]]
|
||||||
|
|
||||||
|
def p_left_all(self, p):
|
||||||
|
'left : dice'
|
||||||
|
p[0] = [None, p[1]]
|
||||||
|
|
||||||
|
def p_left_e(self, p):
|
||||||
|
'left :'
|
||||||
|
p[0] = None
|
||||||
|
|
||||||
|
def p_total(self, p):
|
||||||
|
'trial : NUMBER "#"'
|
||||||
|
if len(p) > 1:
|
||||||
|
p[0] = p[1]
|
||||||
|
else:
|
||||||
|
p[0] = None
|
||||||
|
|
||||||
|
def p_keep(self, p):
|
||||||
|
'keep : NUMBER "/"'
|
||||||
|
if p[1] != None:
|
||||||
|
p[0] = p[1]
|
||||||
|
else:
|
||||||
|
p[0] = None
|
||||||
|
|
||||||
|
def p_dice(self, p):
|
||||||
|
'dice : NUMBER "d"'
|
||||||
|
p[0] = p[1]
|
||||||
|
|
||||||
|
def p_dice_one(self, p):
|
||||||
|
'dice : "d"'
|
||||||
|
p[0] = 1
|
||||||
|
|
||||||
|
def p_error(self, p):
|
||||||
|
"""Raise ValueError on unparseable strings."""
|
||||||
|
raise ValueError("Error occurred in parser")
|
||||||
|
|
||||||
|
def get_result(self):
|
||||||
|
global output
|
||||||
|
return output
|
||||||
|
|
||||||
|
def do_roll(self, dicestr):
|
||||||
|
"""
|
||||||
|
Roll some dice and get the result (with broken out rolls).
|
||||||
|
|
||||||
|
Keyword arguments:
|
||||||
|
dicestr - format:
|
||||||
|
N#X/YdS+M label
|
||||||
|
N#: do the following roll N times (optional)
|
||||||
|
X/: take the top X rolls of the Y times rolled (optional)
|
||||||
|
Y : roll the die specified Y times (optional, defaults to 1)
|
||||||
|
dS: roll a S-sided die
|
||||||
|
+M: add M to the result (-M for subtraction) (optional)
|
||||||
|
"""
|
||||||
|
self.build()
|
||||||
|
yacc.parse(dicestr)
|
||||||
|
return self.get_result()
|
|
@ -0,0 +1,8 @@
|
||||||
|
"""URL patterns for the dice views."""
|
||||||
|
from django.urls import path
|
||||||
|
|
||||||
|
from dice.views import rpc_roll_dice
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path('rpc/roll/', rpc_roll_dice, name='dice_rpc_roll_dice'),
|
||||||
|
]
|
|
@ -0,0 +1,36 @@
|
||||||
|
"""Views for rolling dice."""
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from rest_framework.authentication import BasicAuthentication
|
||||||
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
from rest_framework.response import Response
|
||||||
|
from rest_framework.decorators import api_view, authentication_classes, permission_classes
|
||||||
|
|
||||||
|
from dice.roller import DiceRoller
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
roller = DiceRoller()
|
||||||
|
|
||||||
|
|
||||||
|
@api_view(['POST'])
|
||||||
|
@authentication_classes((BasicAuthentication, ))
|
||||||
|
@permission_classes((IsAuthenticated, ))
|
||||||
|
def rpc_roll_dice(request):
|
||||||
|
"""Get a dice string from the client, roll the dice, and give the result."""
|
||||||
|
if request.method != 'POST':
|
||||||
|
return Response({'detail': "Supported method: POST."}, status=405)
|
||||||
|
|
||||||
|
try:
|
||||||
|
roll_data = json.loads(request.body)
|
||||||
|
dice_str = roll_data['dice']
|
||||||
|
except (json.decoder.JSONDecodeError, KeyError):
|
||||||
|
return Response({'detail': "Request must be JSON with a 'dice' parameter."}, status=400)
|
||||||
|
|
||||||
|
try:
|
||||||
|
result_str = roller.do_roll(dice_str)
|
||||||
|
return Response({'dice': dice_str, 'result': result_str})
|
||||||
|
except AssertionError as aex:
|
||||||
|
return Response({'detail': f"Could not roll dice: {aex}", 'dice': dice_str}, status=400)
|
||||||
|
except ValueError:
|
||||||
|
return Response({'detail': f"Could not parse requested dice '{dice_str}'.", 'dice': dice_str}, status=400)
|
|
@ -29,6 +29,6 @@ class Migration(migrations.Migration):
|
||||||
migrations.AddField(
|
migrations.AddField(
|
||||||
model_name='dispatcheraction',
|
model_name='dispatcheraction',
|
||||||
name='dispatcher',
|
name='dispatcher',
|
||||||
field=models.ForeignKey(to='dispatch.Dispatcher'),
|
field=models.ForeignKey(to='dispatch.Dispatcher', on_delete=models.CASCADE),
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
|
@ -17,6 +17,6 @@ class Migration(migrations.Migration):
|
||||||
migrations.AlterField(
|
migrations.AlterField(
|
||||||
model_name='dispatcheraction',
|
model_name='dispatcheraction',
|
||||||
name='dispatcher',
|
name='dispatcher',
|
||||||
field=models.ForeignKey(related_name='actions', to='dispatch.Dispatcher'),
|
field=models.ForeignKey(related_name='actions', to='dispatch.Dispatcher', on_delete=models.CASCADE),
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import unicode_literals
|
|
||||||
|
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
# Generated by Django 3.1.2 on 2021-04-25 14:37
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('dispatch', '0005_auto_20160116_1955'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='dispatcher',
|
||||||
|
name='bot_xmlrpc_host',
|
||||||
|
field=models.CharField(default='localhost', max_length=200),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='dispatcher',
|
||||||
|
name='bot_xmlrpc_port',
|
||||||
|
field=models.PositiveSmallIntegerField(default=13132),
|
||||||
|
),
|
||||||
|
]
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 3.2.18 on 2023-03-01 00:26
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('dispatch', '0006_xmlrpc_settings'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RenameField(
|
||||||
|
model_name='dispatcheraction',
|
||||||
|
old_name='type',
|
||||||
|
new_name='action_type',
|
||||||
|
),
|
||||||
|
]
|
|
@ -1,10 +1,8 @@
|
||||||
"""Track dispatcher configurations."""
|
"""Track dispatcher configurations."""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger('dispatch.models')
|
log = logging.getLogger('dispatch.models')
|
||||||
|
|
||||||
|
|
||||||
|
@ -13,6 +11,9 @@ class Dispatcher(models.Model):
|
||||||
|
|
||||||
key = models.CharField(max_length=16, unique=True)
|
key = models.CharField(max_length=16, unique=True)
|
||||||
|
|
||||||
|
bot_xmlrpc_host = models.CharField(max_length=200, default='localhost')
|
||||||
|
bot_xmlrpc_port = models.PositiveSmallIntegerField(default=13132)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
"""Meta options."""
|
"""Meta options."""
|
||||||
|
|
||||||
|
@ -21,7 +22,7 @@ class Dispatcher(models.Model):
|
||||||
)
|
)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""String representation."""
|
"""Provide string representation."""
|
||||||
return "{0:s}".format(self.key)
|
return "{0:s}".format(self.key)
|
||||||
|
|
||||||
|
|
||||||
|
@ -36,11 +37,11 @@ class DispatcherAction(models.Model):
|
||||||
(FILE_TYPE, "Write to file"),
|
(FILE_TYPE, "Write to file"),
|
||||||
)
|
)
|
||||||
|
|
||||||
dispatcher = models.ForeignKey('Dispatcher', related_name='actions')
|
dispatcher = models.ForeignKey('Dispatcher', related_name='actions', on_delete=models.CASCADE)
|
||||||
type = models.CharField(max_length=16, choices=TYPE_CHOICES)
|
action_type = models.CharField(max_length=16, choices=TYPE_CHOICES)
|
||||||
destination = models.CharField(max_length=200)
|
destination = models.CharField(max_length=200)
|
||||||
include_key = models.BooleanField(default=False)
|
include_key = models.BooleanField(default=False)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""String representation."""
|
"""Provide string representation."""
|
||||||
return "{0:s} -> {1:s} {2:s}".format(self.dispatcher.key, self.type, self.destination)
|
return "{0:s} -> {1:s} {2:s}".format(self.dispatcher.key, self.action_type, self.destination)
|
||||||
|
|
|
@ -12,7 +12,7 @@ class DispatcherActionSerializer(serializers.ModelSerializer):
|
||||||
"""Meta options."""
|
"""Meta options."""
|
||||||
|
|
||||||
model = DispatcherAction
|
model = DispatcherAction
|
||||||
fields = ('id', 'dispatcher', 'type', 'destination')
|
fields = ('id', 'dispatcher', 'action_type', 'destination')
|
||||||
|
|
||||||
|
|
||||||
class DispatcherSerializer(serializers.ModelSerializer):
|
class DispatcherSerializer(serializers.ModelSerializer):
|
||||||
|
|
|
@ -1,20 +1,16 @@
|
||||||
"""URL patterns for the dispatcher API."""
|
"""URL patterns for the dispatcher API."""
|
||||||
|
from django.urls import path
|
||||||
|
|
||||||
from django.conf.urls import url
|
from dispatch.views import (DispatcherActionDetail, DispatcherActionList, DispatcherDetail, DispatcherDetailByKey,
|
||||||
|
DispatcherList, DispatchMessage, DispatchMessageByKey)
|
||||||
from dispatch.views import (DispatchMessage, DispatchMessageByKey, DispatcherList, DispatcherDetail,
|
|
||||||
DispatcherDetailByKey, DispatcherActionList, DispatcherActionDetail)
|
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^api/dispatchers/$', DispatcherList.as_view(), name='dispatch_api_dispatchers'),
|
path('api/dispatchers/', DispatcherList.as_view(), name='dispatch_api_dispatchers'),
|
||||||
url(r'^api/dispatchers/(?P<pk>[0-9]+)/$', DispatcherDetail.as_view(), name='dispatch_api_dispatcher_detail'),
|
path('api/dispatchers/<int:pk>/', DispatcherDetail.as_view(), name='dispatch_api_dispatcher_detail'),
|
||||||
url(r'^api/dispatchers/(?P<pk>[0-9]+)/message$', DispatchMessage.as_view(), name='dispatch_api_dispatch_message'),
|
path('api/dispatchers/<int:pk>/message', DispatchMessage.as_view(), name='dispatch_api_dispatch_message'),
|
||||||
url(r'^api/dispatchers/(?P<key>[A-Za-z-]+)/$', DispatcherDetailByKey.as_view(),
|
path('api/dispatchers/<key>/', DispatcherDetailByKey.as_view(), name='dispatch_api_dispatcher_detail'),
|
||||||
name='dispatch_api_dispatcher_detail'),
|
path('api/dispatchers/<key>/message', DispatchMessageByKey.as_view(), name='dispatch_api_dispatch_message'),
|
||||||
url(r'^api/dispatchers/(?P<key>[A-Za-z-]+)/message$', DispatchMessageByKey.as_view(),
|
|
||||||
name='dispatch_api_dispatch_message'),
|
|
||||||
|
|
||||||
url(r'^api/actions/$', DispatcherActionList.as_view(), name='dispatch_api_actions'),
|
path('api/actions/', DispatcherActionList.as_view(), name='dispatch_api_actions'),
|
||||||
url(r'^api/actions/(?P<pk>[0-9]+)/$', DispatcherActionDetail.as_view(), name='dispatch_api_action_detail'),
|
path('api/actions/<int:pk>/', DispatcherActionDetail.as_view(), name='dispatch_api_action_detail'),
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,18 +1,15 @@
|
||||||
"""Handle dispatcher API requests."""
|
"""Handle dispatcher API requests."""
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import xmlrpc.client
|
import xmlrpc.client
|
||||||
|
|
||||||
from django.conf import settings
|
|
||||||
from rest_framework import generics, status
|
from rest_framework import generics, status
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
|
||||||
from dispatch.models import Dispatcher, DispatcherAction
|
from dispatch.models import Dispatcher, DispatcherAction
|
||||||
from dispatch.serializers import DispatchMessageSerializer, DispatcherSerializer, DispatcherActionSerializer
|
from dispatch.serializers import DispatcherActionSerializer, DispatcherSerializer, DispatchMessageSerializer
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger('dispatch.views')
|
log = logging.getLogger('dispatch.views')
|
||||||
|
|
||||||
|
@ -31,6 +28,8 @@ class HasSendMessagePermission(IsAuthenticated):
|
||||||
class DispatcherList(generics.ListAPIView):
|
class DispatcherList(generics.ListAPIView):
|
||||||
"""List all dispatchers."""
|
"""List all dispatchers."""
|
||||||
|
|
||||||
|
permission_classes = (IsAuthenticated,)
|
||||||
|
|
||||||
queryset = Dispatcher.objects.all()
|
queryset = Dispatcher.objects.all()
|
||||||
serializer_class = DispatcherSerializer
|
serializer_class = DispatcherSerializer
|
||||||
|
|
||||||
|
@ -38,6 +37,8 @@ class DispatcherList(generics.ListAPIView):
|
||||||
class DispatcherDetail(generics.RetrieveAPIView):
|
class DispatcherDetail(generics.RetrieveAPIView):
|
||||||
"""Detail the given dispatcher."""
|
"""Detail the given dispatcher."""
|
||||||
|
|
||||||
|
permission_classes = (IsAuthenticated,)
|
||||||
|
|
||||||
queryset = Dispatcher.objects.all()
|
queryset = Dispatcher.objects.all()
|
||||||
serializer_class = DispatcherSerializer
|
serializer_class = DispatcherSerializer
|
||||||
|
|
||||||
|
@ -74,19 +75,19 @@ class DispatchMessage(generics.GenericAPIView):
|
||||||
else:
|
else:
|
||||||
text = message.data['message']
|
text = message.data['message']
|
||||||
|
|
||||||
if action.type == DispatcherAction.PRIVMSG_TYPE:
|
if action.action_type == DispatcherAction.PRIVMSG_TYPE:
|
||||||
# connect over XML-RPC and send
|
# connect over XML-RPC and send
|
||||||
try:
|
try:
|
||||||
bot_url = 'http://{0:s}:{1:d}/'.format(settings.IRCBOT_XMLRPC_HOST, settings.IRCBOT_XMLRPC_PORT)
|
bot_url = 'http://{0:s}:{1:d}/'.format(dispatcher.bot_xmlrpc_host, dispatcher.bot_xmlrpc_port)
|
||||||
bot = xmlrpc.client.ServerProxy(bot_url, allow_none=True)
|
bot = xmlrpc.client.ServerProxy(bot_url, allow_none=True)
|
||||||
log.debug("sending '%s' to channel %s", text, action.destination)
|
log.debug("sending '%s' to channel %s", text, action.destination)
|
||||||
bot.reply(None, text, False, action.destination)
|
bot.reply(None, text, False, action.destination)
|
||||||
except Exception as e:
|
except xmlrpc.client.Fault as xmlex:
|
||||||
new_data = copy.deepcopy(message.data)
|
new_data = copy.deepcopy(message.data)
|
||||||
new_data['status'] = "FAILED - {0:s}".format(str(e))
|
new_data['status'] = "FAILED - {0:s}".format(str(xmlex))
|
||||||
new_message = self.serializer_class(data=new_data)
|
new_message = self.serializer_class(data=new_data)
|
||||||
return Response(new_message.initial_data)
|
return Response(new_message.initial_data)
|
||||||
elif action.type == DispatcherAction.FILE_TYPE:
|
elif action.action_type == DispatcherAction.FILE_TYPE:
|
||||||
# write to file
|
# write to file
|
||||||
filename = os.path.abspath(action.destination)
|
filename = os.path.abspath(action.destination)
|
||||||
log.debug("sending '%s' to file %s", text, filename)
|
log.debug("sending '%s' to file %s", text, filename)
|
||||||
|
@ -110,6 +111,8 @@ class DispatchMessageByKey(DispatchMessage):
|
||||||
class DispatcherActionList(generics.ListAPIView):
|
class DispatcherActionList(generics.ListAPIView):
|
||||||
"""List all dispatchers."""
|
"""List all dispatchers."""
|
||||||
|
|
||||||
|
permission_classes = (IsAuthenticated,)
|
||||||
|
|
||||||
queryset = DispatcherAction.objects.all()
|
queryset = DispatcherAction.objects.all()
|
||||||
serializer_class = DispatcherActionSerializer
|
serializer_class = DispatcherActionSerializer
|
||||||
|
|
||||||
|
@ -117,5 +120,7 @@ class DispatcherActionList(generics.ListAPIView):
|
||||||
class DispatcherActionDetail(generics.RetrieveAPIView):
|
class DispatcherActionDetail(generics.RetrieveAPIView):
|
||||||
"""Detail the given dispatcher."""
|
"""Detail the given dispatcher."""
|
||||||
|
|
||||||
|
permission_classes = (IsAuthenticated,)
|
||||||
|
|
||||||
queryset = DispatcherAction.objects.all()
|
queryset = DispatcherAction.objects.all()
|
||||||
serializer_class = DispatcherActionSerializer
|
serializer_class = DispatcherActionSerializer
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
"""Set up the version number of the bot."""
|
||||||
|
from ._version import get_versions
|
||||||
|
__version__ = get_versions()['version']
|
||||||
|
del get_versions
|
|
@ -0,0 +1,520 @@
|
||||||
|
|
||||||
|
# This file helps to compute a version number in source trees obtained from
|
||||||
|
# git-archive tarball (such as those provided by githubs download-from-tag
|
||||||
|
# feature). Distribution tarballs (built by setup.py sdist) and build
|
||||||
|
# directories (produced by setup.py build) will contain a much shorter file
|
||||||
|
# that just contains the computed version number.
|
||||||
|
|
||||||
|
# This file is released into the public domain. Generated by
|
||||||
|
# versioneer-0.18 (https://github.com/warner/python-versioneer)
|
||||||
|
|
||||||
|
"""Git implementation of _version.py."""
|
||||||
|
|
||||||
|
import errno
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def get_keywords():
|
||||||
|
"""Get the keywords needed to look up the version information."""
|
||||||
|
# these strings will be replaced by git during git-archive.
|
||||||
|
# setup.py/versioneer.py will grep for the variable names, so they must
|
||||||
|
# each be defined on a line of their own. _version.py will just call
|
||||||
|
# get_keywords().
|
||||||
|
git_refnames = "$Format:%d$"
|
||||||
|
git_full = "$Format:%H$"
|
||||||
|
git_date = "$Format:%ci$"
|
||||||
|
keywords = {"refnames": git_refnames, "full": git_full, "date": git_date}
|
||||||
|
return keywords
|
||||||
|
|
||||||
|
|
||||||
|
class VersioneerConfig:
|
||||||
|
"""Container for Versioneer configuration parameters."""
|
||||||
|
|
||||||
|
|
||||||
|
def get_config():
|
||||||
|
"""Create, populate and return the VersioneerConfig() object."""
|
||||||
|
# these strings are filled in when 'setup.py versioneer' creates
|
||||||
|
# _version.py
|
||||||
|
cfg = VersioneerConfig()
|
||||||
|
cfg.VCS = "git"
|
||||||
|
cfg.style = "pep440"
|
||||||
|
cfg.tag_prefix = "v"
|
||||||
|
cfg.parentdir_prefix = "None"
|
||||||
|
cfg.versionfile_source = "dr_botzo/_version.py"
|
||||||
|
cfg.verbose = False
|
||||||
|
return cfg
|
||||||
|
|
||||||
|
|
||||||
|
class NotThisMethod(Exception):
|
||||||
|
"""Exception raised if a method is not valid for the current scenario."""
|
||||||
|
|
||||||
|
|
||||||
|
LONG_VERSION_PY = {}
|
||||||
|
HANDLERS = {}
|
||||||
|
|
||||||
|
|
||||||
|
def register_vcs_handler(vcs, method): # decorator
|
||||||
|
"""Decorator to mark a method as the handler for a particular VCS."""
|
||||||
|
def decorate(f):
|
||||||
|
"""Store f in HANDLERS[vcs][method]."""
|
||||||
|
if vcs not in HANDLERS:
|
||||||
|
HANDLERS[vcs] = {}
|
||||||
|
HANDLERS[vcs][method] = f
|
||||||
|
return f
|
||||||
|
return decorate
|
||||||
|
|
||||||
|
|
||||||
|
def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False,
|
||||||
|
env=None):
|
||||||
|
"""Call the given command(s)."""
|
||||||
|
assert isinstance(commands, list)
|
||||||
|
p = None
|
||||||
|
for c in commands:
|
||||||
|
try:
|
||||||
|
dispcmd = str([c] + args)
|
||||||
|
# remember shell=False, so use git.cmd on windows, not just git
|
||||||
|
p = subprocess.Popen([c] + args, cwd=cwd, env=env,
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=(subprocess.PIPE if hide_stderr
|
||||||
|
else None))
|
||||||
|
break
|
||||||
|
except EnvironmentError:
|
||||||
|
e = sys.exc_info()[1]
|
||||||
|
if e.errno == errno.ENOENT:
|
||||||
|
continue
|
||||||
|
if verbose:
|
||||||
|
print("unable to run %s" % dispcmd)
|
||||||
|
print(e)
|
||||||
|
return None, None
|
||||||
|
else:
|
||||||
|
if verbose:
|
||||||
|
print("unable to find command, tried %s" % (commands,))
|
||||||
|
return None, None
|
||||||
|
stdout = p.communicate()[0].strip()
|
||||||
|
if sys.version_info[0] >= 3:
|
||||||
|
stdout = stdout.decode()
|
||||||
|
if p.returncode != 0:
|
||||||
|
if verbose:
|
||||||
|
print("unable to run %s (error)" % dispcmd)
|
||||||
|
print("stdout was %s" % stdout)
|
||||||
|
return None, p.returncode
|
||||||
|
return stdout, p.returncode
|
||||||
|
|
||||||
|
|
||||||
|
def versions_from_parentdir(parentdir_prefix, root, verbose):
|
||||||
|
"""Try to determine the version from the parent directory name.
|
||||||
|
|
||||||
|
Source tarballs conventionally unpack into a directory that includes both
|
||||||
|
the project name and a version string. We will also support searching up
|
||||||
|
two directory levels for an appropriately named parent directory
|
||||||
|
"""
|
||||||
|
rootdirs = []
|
||||||
|
|
||||||
|
for i in range(3):
|
||||||
|
dirname = os.path.basename(root)
|
||||||
|
if dirname.startswith(parentdir_prefix):
|
||||||
|
return {"version": dirname[len(parentdir_prefix):],
|
||||||
|
"full-revisionid": None,
|
||||||
|
"dirty": False, "error": None, "date": None}
|
||||||
|
else:
|
||||||
|
rootdirs.append(root)
|
||||||
|
root = os.path.dirname(root) # up a level
|
||||||
|
|
||||||
|
if verbose:
|
||||||
|
print("Tried directories %s but none started with prefix %s" %
|
||||||
|
(str(rootdirs), parentdir_prefix))
|
||||||
|
raise NotThisMethod("rootdir doesn't start with parentdir_prefix")
|
||||||
|
|
||||||
|
|
||||||
|
@register_vcs_handler("git", "get_keywords")
|
||||||
|
def git_get_keywords(versionfile_abs):
|
||||||
|
"""Extract version information from the given file."""
|
||||||
|
# the code embedded in _version.py can just fetch the value of these
|
||||||
|
# keywords. When used from setup.py, we don't want to import _version.py,
|
||||||
|
# so we do it with a regexp instead. This function is not used from
|
||||||
|
# _version.py.
|
||||||
|
keywords = {}
|
||||||
|
try:
|
||||||
|
f = open(versionfile_abs, "r")
|
||||||
|
for line in f.readlines():
|
||||||
|
if line.strip().startswith("git_refnames ="):
|
||||||
|
mo = re.search(r'=\s*"(.*)"', line)
|
||||||
|
if mo:
|
||||||
|
keywords["refnames"] = mo.group(1)
|
||||||
|
if line.strip().startswith("git_full ="):
|
||||||
|
mo = re.search(r'=\s*"(.*)"', line)
|
||||||
|
if mo:
|
||||||
|
keywords["full"] = mo.group(1)
|
||||||
|
if line.strip().startswith("git_date ="):
|
||||||
|
mo = re.search(r'=\s*"(.*)"', line)
|
||||||
|
if mo:
|
||||||
|
keywords["date"] = mo.group(1)
|
||||||
|
f.close()
|
||||||
|
except EnvironmentError:
|
||||||
|
pass
|
||||||
|
return keywords
|
||||||
|
|
||||||
|
|
||||||
|
@register_vcs_handler("git", "keywords")
|
||||||
|
def git_versions_from_keywords(keywords, tag_prefix, verbose):
|
||||||
|
"""Get version information from git keywords."""
|
||||||
|
if not keywords:
|
||||||
|
raise NotThisMethod("no keywords at all, weird")
|
||||||
|
date = keywords.get("date")
|
||||||
|
if date is not None:
|
||||||
|
# git-2.2.0 added "%cI", which expands to an ISO-8601 -compliant
|
||||||
|
# datestamp. However we prefer "%ci" (which expands to an "ISO-8601
|
||||||
|
# -like" string, which we must then edit to make compliant), because
|
||||||
|
# it's been around since git-1.5.3, and it's too difficult to
|
||||||
|
# discover which version we're using, or to work around using an
|
||||||
|
# older one.
|
||||||
|
date = date.strip().replace(" ", "T", 1).replace(" ", "", 1)
|
||||||
|
refnames = keywords["refnames"].strip()
|
||||||
|
if refnames.startswith("$Format"):
|
||||||
|
if verbose:
|
||||||
|
print("keywords are unexpanded, not using")
|
||||||
|
raise NotThisMethod("unexpanded keywords, not a git-archive tarball")
|
||||||
|
refs = set([r.strip() for r in refnames.strip("()").split(",")])
|
||||||
|
# starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of
|
||||||
|
# just "foo-1.0". If we see a "tag: " prefix, prefer those.
|
||||||
|
TAG = "tag: "
|
||||||
|
tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)])
|
||||||
|
if not tags:
|
||||||
|
# Either we're using git < 1.8.3, or there really are no tags. We use
|
||||||
|
# a heuristic: assume all version tags have a digit. The old git %d
|
||||||
|
# expansion behaves like git log --decorate=short and strips out the
|
||||||
|
# refs/heads/ and refs/tags/ prefixes that would let us distinguish
|
||||||
|
# between branches and tags. By ignoring refnames without digits, we
|
||||||
|
# filter out many common branch names like "release" and
|
||||||
|
# "stabilization", as well as "HEAD" and "master".
|
||||||
|
tags = set([r for r in refs if re.search(r'\d', r)])
|
||||||
|
if verbose:
|
||||||
|
print("discarding '%s', no digits" % ",".join(refs - tags))
|
||||||
|
if verbose:
|
||||||
|
print("likely tags: %s" % ",".join(sorted(tags)))
|
||||||
|
for ref in sorted(tags):
|
||||||
|
# sorting will prefer e.g. "2.0" over "2.0rc1"
|
||||||
|
if ref.startswith(tag_prefix):
|
||||||
|
r = ref[len(tag_prefix):]
|
||||||
|
if verbose:
|
||||||
|
print("picking %s" % r)
|
||||||
|
return {"version": r,
|
||||||
|
"full-revisionid": keywords["full"].strip(),
|
||||||
|
"dirty": False, "error": None,
|
||||||
|
"date": date}
|
||||||
|
# no suitable tags, so version is "0+unknown", but full hex is still there
|
||||||
|
if verbose:
|
||||||
|
print("no suitable tags, using unknown + full revision id")
|
||||||
|
return {"version": "0+unknown",
|
||||||
|
"full-revisionid": keywords["full"].strip(),
|
||||||
|
"dirty": False, "error": "no suitable tags", "date": None}
|
||||||
|
|
||||||
|
|
||||||
|
@register_vcs_handler("git", "pieces_from_vcs")
|
||||||
|
def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
|
||||||
|
"""Get version from 'git describe' in the root of the source tree.
|
||||||
|
|
||||||
|
This only gets called if the git-archive 'subst' keywords were *not*
|
||||||
|
expanded, and _version.py hasn't already been rewritten with a short
|
||||||
|
version string, meaning we're inside a checked out source tree.
|
||||||
|
"""
|
||||||
|
GITS = ["git"]
|
||||||
|
if sys.platform == "win32":
|
||||||
|
GITS = ["git.cmd", "git.exe"]
|
||||||
|
|
||||||
|
out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root,
|
||||||
|
hide_stderr=True)
|
||||||
|
if rc != 0:
|
||||||
|
if verbose:
|
||||||
|
print("Directory %s not under git control" % root)
|
||||||
|
raise NotThisMethod("'git rev-parse --git-dir' returned error")
|
||||||
|
|
||||||
|
# if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty]
|
||||||
|
# if there isn't one, this yields HEX[-dirty] (no NUM)
|
||||||
|
describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty",
|
||||||
|
"--always", "--long",
|
||||||
|
"--match", "%s*" % tag_prefix],
|
||||||
|
cwd=root)
|
||||||
|
# --long was added in git-1.5.5
|
||||||
|
if describe_out is None:
|
||||||
|
raise NotThisMethod("'git describe' failed")
|
||||||
|
describe_out = describe_out.strip()
|
||||||
|
full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root)
|
||||||
|
if full_out is None:
|
||||||
|
raise NotThisMethod("'git rev-parse' failed")
|
||||||
|
full_out = full_out.strip()
|
||||||
|
|
||||||
|
pieces = {}
|
||||||
|
pieces["long"] = full_out
|
||||||
|
pieces["short"] = full_out[:7] # maybe improved later
|
||||||
|
pieces["error"] = None
|
||||||
|
|
||||||
|
# parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty]
|
||||||
|
# TAG might have hyphens.
|
||||||
|
git_describe = describe_out
|
||||||
|
|
||||||
|
# look for -dirty suffix
|
||||||
|
dirty = git_describe.endswith("-dirty")
|
||||||
|
pieces["dirty"] = dirty
|
||||||
|
if dirty:
|
||||||
|
git_describe = git_describe[:git_describe.rindex("-dirty")]
|
||||||
|
|
||||||
|
# now we have TAG-NUM-gHEX or HEX
|
||||||
|
|
||||||
|
if "-" in git_describe:
|
||||||
|
# TAG-NUM-gHEX
|
||||||
|
mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe)
|
||||||
|
if not mo:
|
||||||
|
# unparseable. Maybe git-describe is misbehaving?
|
||||||
|
pieces["error"] = ("unable to parse git-describe output: '%s'"
|
||||||
|
% describe_out)
|
||||||
|
return pieces
|
||||||
|
|
||||||
|
# tag
|
||||||
|
full_tag = mo.group(1)
|
||||||
|
if not full_tag.startswith(tag_prefix):
|
||||||
|
if verbose:
|
||||||
|
fmt = "tag '%s' doesn't start with prefix '%s'"
|
||||||
|
print(fmt % (full_tag, tag_prefix))
|
||||||
|
pieces["error"] = ("tag '%s' doesn't start with prefix '%s'"
|
||||||
|
% (full_tag, tag_prefix))
|
||||||
|
return pieces
|
||||||
|
pieces["closest-tag"] = full_tag[len(tag_prefix):]
|
||||||
|
|
||||||
|
# distance: number of commits since tag
|
||||||
|
pieces["distance"] = int(mo.group(2))
|
||||||
|
|
||||||
|
# commit: short hex revision ID
|
||||||
|
pieces["short"] = mo.group(3)
|
||||||
|
|
||||||
|
else:
|
||||||
|
# HEX: no tags
|
||||||
|
pieces["closest-tag"] = None
|
||||||
|
count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"],
|
||||||
|
cwd=root)
|
||||||
|
pieces["distance"] = int(count_out) # total number of commits
|
||||||
|
|
||||||
|
# commit date: see ISO-8601 comment in git_versions_from_keywords()
|
||||||
|
date = run_command(GITS, ["show", "-s", "--format=%ci", "HEAD"],
|
||||||
|
cwd=root)[0].strip()
|
||||||
|
pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1)
|
||||||
|
|
||||||
|
return pieces
|
||||||
|
|
||||||
|
|
||||||
|
def plus_or_dot(pieces):
|
||||||
|
"""Return a + if we don't already have one, else return a ."""
|
||||||
|
if "+" in pieces.get("closest-tag", ""):
|
||||||
|
return "."
|
||||||
|
return "+"
|
||||||
|
|
||||||
|
|
||||||
|
def render_pep440(pieces):
|
||||||
|
"""Build up version string, with post-release "local version identifier".
|
||||||
|
|
||||||
|
Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you
|
||||||
|
get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty
|
||||||
|
|
||||||
|
Exceptions:
|
||||||
|
1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty]
|
||||||
|
"""
|
||||||
|
if pieces["closest-tag"]:
|
||||||
|
rendered = pieces["closest-tag"]
|
||||||
|
if pieces["distance"] or pieces["dirty"]:
|
||||||
|
rendered += plus_or_dot(pieces)
|
||||||
|
rendered += "%d.g%s" % (pieces["distance"], pieces["short"])
|
||||||
|
if pieces["dirty"]:
|
||||||
|
rendered += ".dirty"
|
||||||
|
else:
|
||||||
|
# exception #1
|
||||||
|
rendered = "0+untagged.%d.g%s" % (pieces["distance"],
|
||||||
|
pieces["short"])
|
||||||
|
if pieces["dirty"]:
|
||||||
|
rendered += ".dirty"
|
||||||
|
return rendered
|
||||||
|
|
||||||
|
|
||||||
|
def render_pep440_pre(pieces):
|
||||||
|
"""TAG[.post.devDISTANCE] -- No -dirty.
|
||||||
|
|
||||||
|
Exceptions:
|
||||||
|
1: no tags. 0.post.devDISTANCE
|
||||||
|
"""
|
||||||
|
if pieces["closest-tag"]:
|
||||||
|
rendered = pieces["closest-tag"]
|
||||||
|
if pieces["distance"]:
|
||||||
|
rendered += ".post.dev%d" % pieces["distance"]
|
||||||
|
else:
|
||||||
|
# exception #1
|
||||||
|
rendered = "0.post.dev%d" % pieces["distance"]
|
||||||
|
return rendered
|
||||||
|
|
||||||
|
|
||||||
|
def render_pep440_post(pieces):
|
||||||
|
"""TAG[.postDISTANCE[.dev0]+gHEX] .
|
||||||
|
|
||||||
|
The ".dev0" means dirty. Note that .dev0 sorts backwards
|
||||||
|
(a dirty tree will appear "older" than the corresponding clean one),
|
||||||
|
but you shouldn't be releasing software with -dirty anyways.
|
||||||
|
|
||||||
|
Exceptions:
|
||||||
|
1: no tags. 0.postDISTANCE[.dev0]
|
||||||
|
"""
|
||||||
|
if pieces["closest-tag"]:
|
||||||
|
rendered = pieces["closest-tag"]
|
||||||
|
if pieces["distance"] or pieces["dirty"]:
|
||||||
|
rendered += ".post%d" % pieces["distance"]
|
||||||
|
if pieces["dirty"]:
|
||||||
|
rendered += ".dev0"
|
||||||
|
rendered += plus_or_dot(pieces)
|
||||||
|
rendered += "g%s" % pieces["short"]
|
||||||
|
else:
|
||||||
|
# exception #1
|
||||||
|
rendered = "0.post%d" % pieces["distance"]
|
||||||
|
if pieces["dirty"]:
|
||||||
|
rendered += ".dev0"
|
||||||
|
rendered += "+g%s" % pieces["short"]
|
||||||
|
return rendered
|
||||||
|
|
||||||
|
|
||||||
|
def render_pep440_old(pieces):
|
||||||
|
"""TAG[.postDISTANCE[.dev0]] .
|
||||||
|
|
||||||
|
The ".dev0" means dirty.
|
||||||
|
|
||||||
|
Eexceptions:
|
||||||
|
1: no tags. 0.postDISTANCE[.dev0]
|
||||||
|
"""
|
||||||
|
if pieces["closest-tag"]:
|
||||||
|
rendered = pieces["closest-tag"]
|
||||||
|
if pieces["distance"] or pieces["dirty"]:
|
||||||
|
rendered += ".post%d" % pieces["distance"]
|
||||||
|
if pieces["dirty"]:
|
||||||
|
rendered += ".dev0"
|
||||||
|
else:
|
||||||
|
# exception #1
|
||||||
|
rendered = "0.post%d" % pieces["distance"]
|
||||||
|
if pieces["dirty"]:
|
||||||
|
rendered += ".dev0"
|
||||||
|
return rendered
|
||||||
|
|
||||||
|
|
||||||
|
def render_git_describe(pieces):
|
||||||
|
"""TAG[-DISTANCE-gHEX][-dirty].
|
||||||
|
|
||||||
|
Like 'git describe --tags --dirty --always'.
|
||||||
|
|
||||||
|
Exceptions:
|
||||||
|
1: no tags. HEX[-dirty] (note: no 'g' prefix)
|
||||||
|
"""
|
||||||
|
if pieces["closest-tag"]:
|
||||||
|
rendered = pieces["closest-tag"]
|
||||||
|
if pieces["distance"]:
|
||||||
|
rendered += "-%d-g%s" % (pieces["distance"], pieces["short"])
|
||||||
|
else:
|
||||||
|
# exception #1
|
||||||
|
rendered = pieces["short"]
|
||||||
|
if pieces["dirty"]:
|
||||||
|
rendered += "-dirty"
|
||||||
|
return rendered
|
||||||
|
|
||||||
|
|
||||||
|
def render_git_describe_long(pieces):
|
||||||
|
"""TAG-DISTANCE-gHEX[-dirty].
|
||||||
|
|
||||||
|
Like 'git describe --tags --dirty --always -long'.
|
||||||
|
The distance/hash is unconditional.
|
||||||
|
|
||||||
|
Exceptions:
|
||||||
|
1: no tags. HEX[-dirty] (note: no 'g' prefix)
|
||||||
|
"""
|
||||||
|
if pieces["closest-tag"]:
|
||||||
|
rendered = pieces["closest-tag"]
|
||||||
|
rendered += "-%d-g%s" % (pieces["distance"], pieces["short"])
|
||||||
|
else:
|
||||||
|
# exception #1
|
||||||
|
rendered = pieces["short"]
|
||||||
|
if pieces["dirty"]:
|
||||||
|
rendered += "-dirty"
|
||||||
|
return rendered
|
||||||
|
|
||||||
|
|
||||||
|
def render(pieces, style):
|
||||||
|
"""Render the given version pieces into the requested style."""
|
||||||
|
if pieces["error"]:
|
||||||
|
return {"version": "unknown",
|
||||||
|
"full-revisionid": pieces.get("long"),
|
||||||
|
"dirty": None,
|
||||||
|
"error": pieces["error"],
|
||||||
|
"date": None}
|
||||||
|
|
||||||
|
if not style or style == "default":
|
||||||
|
style = "pep440" # the default
|
||||||
|
|
||||||
|
if style == "pep440":
|
||||||
|
rendered = render_pep440(pieces)
|
||||||
|
elif style == "pep440-pre":
|
||||||
|
rendered = render_pep440_pre(pieces)
|
||||||
|
elif style == "pep440-post":
|
||||||
|
rendered = render_pep440_post(pieces)
|
||||||
|
elif style == "pep440-old":
|
||||||
|
rendered = render_pep440_old(pieces)
|
||||||
|
elif style == "git-describe":
|
||||||
|
rendered = render_git_describe(pieces)
|
||||||
|
elif style == "git-describe-long":
|
||||||
|
rendered = render_git_describe_long(pieces)
|
||||||
|
else:
|
||||||
|
raise ValueError("unknown style '%s'" % style)
|
||||||
|
|
||||||
|
return {"version": rendered, "full-revisionid": pieces["long"],
|
||||||
|
"dirty": pieces["dirty"], "error": None,
|
||||||
|
"date": pieces.get("date")}
|
||||||
|
|
||||||
|
|
||||||
|
def get_versions():
|
||||||
|
"""Get version information or return default if unable to do so."""
|
||||||
|
# I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have
|
||||||
|
# __file__, we can work backwards from there to the root. Some
|
||||||
|
# py2exe/bbfreeze/non-CPython implementations don't do __file__, in which
|
||||||
|
# case we can only use expanded keywords.
|
||||||
|
|
||||||
|
cfg = get_config()
|
||||||
|
verbose = cfg.verbose
|
||||||
|
|
||||||
|
try:
|
||||||
|
return git_versions_from_keywords(get_keywords(), cfg.tag_prefix,
|
||||||
|
verbose)
|
||||||
|
except NotThisMethod:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
root = os.path.realpath(__file__)
|
||||||
|
# versionfile_source is the relative path from the top of the source
|
||||||
|
# tree (where the .git directory might live) to this file. Invert
|
||||||
|
# this to find the root from __file__.
|
||||||
|
for i in cfg.versionfile_source.split('/'):
|
||||||
|
root = os.path.dirname(root)
|
||||||
|
except NameError:
|
||||||
|
return {"version": "0+unknown", "full-revisionid": None,
|
||||||
|
"dirty": None,
|
||||||
|
"error": "unable to find root of source tree",
|
||||||
|
"date": None}
|
||||||
|
|
||||||
|
try:
|
||||||
|
pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose)
|
||||||
|
return render(pieces, cfg.style)
|
||||||
|
except NotThisMethod:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
if cfg.parentdir_prefix:
|
||||||
|
return versions_from_parentdir(cfg.parentdir_prefix, root, verbose)
|
||||||
|
except NotThisMethod:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return {"version": "0+unknown", "full-revisionid": None,
|
||||||
|
"dirty": None,
|
||||||
|
"error": "unable to compute version", "date": None}
|
|
@ -1,5 +1,5 @@
|
||||||
"""Site processors to add additional template tags and whatnot."""
|
"""Site processors to add additional template tags and whatnot."""
|
||||||
|
from django.conf import settings
|
||||||
from django.contrib.sites.shortcuts import get_current_site
|
from django.contrib.sites.shortcuts import get_current_site
|
||||||
from django.utils.functional import SimpleLazyObject
|
from django.utils.functional import SimpleLazyObject
|
||||||
|
|
||||||
|
@ -11,4 +11,5 @@ def site(request):
|
||||||
return {
|
return {
|
||||||
'site': site,
|
'site': site,
|
||||||
'site_root': SimpleLazyObject(lambda: "{0}://{1}".format(protocol, site.domain)),
|
'site_root': SimpleLazyObject(lambda: "{0}://{1}".format(protocol, site.domain)),
|
||||||
|
'WEB_ENABLED_APPS': settings.WEB_ENABLED_APPS,
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,8 +11,6 @@ https://docs.djangoproject.com/en/1.6/ref/settings/
|
||||||
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
|
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from django.core.urlresolvers import reverse_lazy
|
|
||||||
|
|
||||||
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
|
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
|
||||||
|
|
||||||
|
|
||||||
|
@ -41,12 +39,10 @@ INSTALLED_APPS = (
|
||||||
'django_extensions',
|
'django_extensions',
|
||||||
'adminplus',
|
'adminplus',
|
||||||
'bootstrap3',
|
'bootstrap3',
|
||||||
'registration',
|
|
||||||
'rest_framework',
|
'rest_framework',
|
||||||
'countdown',
|
'countdown',
|
||||||
'dispatch',
|
'dispatch',
|
||||||
'facts',
|
'facts',
|
||||||
'gitlab_bot',
|
|
||||||
'ircbot',
|
'ircbot',
|
||||||
'karma',
|
'karma',
|
||||||
'markov',
|
'markov',
|
||||||
|
@ -54,18 +50,16 @@ INSTALLED_APPS = (
|
||||||
'races',
|
'races',
|
||||||
'seen',
|
'seen',
|
||||||
'storycraft',
|
'storycraft',
|
||||||
'twitter',
|
|
||||||
)
|
)
|
||||||
|
|
||||||
MIDDLEWARE_CLASSES = (
|
MIDDLEWARE = (
|
||||||
|
'django.middleware.security.SecurityMiddleware',
|
||||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||||
'django.middleware.common.CommonMiddleware',
|
'django.middleware.common.CommonMiddleware',
|
||||||
'django.middleware.csrf.CsrfViewMiddleware',
|
'django.middleware.csrf.CsrfViewMiddleware',
|
||||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||||
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
|
|
||||||
'django.contrib.messages.middleware.MessageMiddleware',
|
'django.contrib.messages.middleware.MessageMiddleware',
|
||||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||||
'django.middleware.security.SecurityMiddleware',
|
|
||||||
)
|
)
|
||||||
|
|
||||||
ROOT_URLCONF = 'dr_botzo.urls'
|
ROOT_URLCONF = 'dr_botzo.urls'
|
||||||
|
@ -73,7 +67,7 @@ ROOT_URLCONF = 'dr_botzo.urls'
|
||||||
TEMPLATES = [
|
TEMPLATES = [
|
||||||
{
|
{
|
||||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||||
'DIRS': [os.path.join(BASE_DIR, 'templates')],
|
'DIRS': [os.path.join(BASE_DIR, 'dr_botzo', 'templates')],
|
||||||
'APP_DIRS': True,
|
'APP_DIRS': True,
|
||||||
'OPTIONS': {
|
'OPTIONS': {
|
||||||
'context_processors': [
|
'context_processors': [
|
||||||
|
@ -100,6 +94,9 @@ DATABASES = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# inherited default, look at changing to BigAutoField
|
||||||
|
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
|
||||||
|
|
||||||
|
|
||||||
# Internationalization
|
# Internationalization
|
||||||
# https://docs.djangoproject.com/en/1.6/topics/i18n/
|
# https://docs.djangoproject.com/en/1.6/topics/i18n/
|
||||||
|
@ -148,37 +145,23 @@ BOOTSTRAP3 = {
|
||||||
'javascript_in_head': True,
|
'javascript_in_head': True,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
###############
|
||||||
|
# web options #
|
||||||
|
###############
|
||||||
|
|
||||||
# registration
|
# choose which apps to display in the web UI, for those that support this config
|
||||||
LOGIN_REDIRECT_URL = reverse_lazy('index')
|
WEB_ENABLED_APPS = [
|
||||||
ACCOUNT_ACTIVATION_DAYS = 7
|
'karma',
|
||||||
REGISTRATION_AUTO_LOGIN = True
|
'races',
|
||||||
|
|
||||||
|
|
||||||
# IRC bot stuff
|
|
||||||
|
|
||||||
# tuple of hostname, port number, and password (or None)
|
|
||||||
IRCBOT_SERVER_LIST = [
|
|
||||||
('localhost', 6667, None),
|
|
||||||
]
|
]
|
||||||
IRCBOT_NICKNAME = 'dr_botzo'
|
|
||||||
IRCBOT_REALNAME = 'Dr. Botzo'
|
|
||||||
IRCBOT_SSL = False
|
|
||||||
IRCBOT_IPV6 = False
|
|
||||||
|
|
||||||
|
|
||||||
# post-connect, pre-autojoin stuff
|
|
||||||
IRCBOT_SLEEP_BEFORE_AUTOJOIN_SECONDS = 10
|
|
||||||
IRCBOT_POST_CONNECT_COMMANDS = [ ]
|
|
||||||
|
|
||||||
|
|
||||||
# XML-RPC settings
|
|
||||||
IRCBOT_XMLRPC_HOST = 'localhost'
|
|
||||||
IRCBOT_XMLRPC_PORT = 13132
|
|
||||||
|
|
||||||
|
|
||||||
# IRC module stuff
|
# IRC module stuff
|
||||||
|
|
||||||
|
# dice
|
||||||
|
|
||||||
|
DICE_PREFIX_ROLLER = False
|
||||||
|
|
||||||
# karma
|
# karma
|
||||||
|
|
||||||
KARMA_IGNORE_CHATTER_TARGETS = []
|
KARMA_IGNORE_CHATTER_TARGETS = []
|
||||||
|
@ -199,15 +182,6 @@ STORYCRAFT_DEFAULT_GAME_LENGTH = 20
|
||||||
STORYCRAFT_DEFAULT_LINE_LENGTH = 140
|
STORYCRAFT_DEFAULT_LINE_LENGTH = 140
|
||||||
STORYCRAFT_DEFAULT_LINES_PER_TURN = 2
|
STORYCRAFT_DEFAULT_LINES_PER_TURN = 2
|
||||||
|
|
||||||
# twitter
|
|
||||||
|
|
||||||
TWITTER_CONSUMER_KEY = None
|
|
||||||
TWITTER_CONSUMER_SECRET = None
|
|
||||||
|
|
||||||
# weather
|
|
||||||
|
|
||||||
WEATHER_WEATHER_UNDERGROUND_API_KEY = None
|
|
||||||
|
|
||||||
|
|
||||||
# load local settings
|
# load local settings
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="navbar-header">
|
<div class="navbar-header">
|
||||||
{% block navbarbrand %}
|
{% block navbarbrand %}
|
||||||
<a class="navbar-brand" href="{% url 'home' %}">{{ site.domain }}</a>
|
<a class="navbar-brand" href="{% url 'index' %}">{{ site.domain }}</a>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
<!-- .navbar-toggle is used as the toggle for collapsed navbar content -->
|
<!-- .navbar-toggle is used as the toggle for collapsed navbar content -->
|
||||||
|
@ -30,9 +30,9 @@
|
||||||
{% block navbar_menu %}
|
{% block navbar_menu %}
|
||||||
<ul class="nav navbar-nav">
|
<ul class="nav navbar-nav">
|
||||||
<li><a href="{% url 'facts_index' %}">Item Sets</a></li>
|
<li><a href="{% url 'facts_index' %}">Item Sets</a></li>
|
||||||
<li><a href="{% url 'karma_index' %}">Karma</a></li>
|
{% if "karma" in WEB_ENABLED_APPS %}<li><a href="{% url 'karma_index' %}">Karma</a></li>{% endif %}
|
||||||
<li><a href="{% url 'markov_index' %}">Markov</a></li>
|
<li><a href="{% url 'markov_index' %}">Markov</a></li>
|
||||||
<li><a href="{% url 'races_index' %}">Races</a></li>
|
{% if "races" in WEB_ENABLED_APPS %}<li><a href="{% url 'races_index' %}">Races</a></li>{% endif %}
|
||||||
</ul>
|
</ul>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
<div class="navbar-right">
|
<div class="navbar-right">
|
||||||
|
@ -44,18 +44,6 @@
|
||||||
<b class="caret"></b>
|
<b class="caret"></b>
|
||||||
</a>
|
</a>
|
||||||
<ul class="dropdown-menu">
|
<ul class="dropdown-menu">
|
||||||
<li>
|
|
||||||
<a href="{% url 'auth_password_change' %}">
|
|
||||||
<i class="fa fa-pencil-square-o"></i>
|
|
||||||
Change password
|
|
||||||
</a>
|
|
||||||
</li>
|
|
||||||
<li>
|
|
||||||
<a href="{% url 'auth_logout' %}">
|
|
||||||
<i class="fa fa-power-off"></i>
|
|
||||||
Log out
|
|
||||||
</a>
|
|
||||||
</li>
|
|
||||||
{% if user.is_authenticated and user.is_staff %}
|
{% if user.is_authenticated and user.is_staff %}
|
||||||
<li>
|
<li>
|
||||||
<a href="{% url 'admin:index' %}">
|
<a href="{% url 'admin:index' %}">
|
||||||
|
@ -66,13 +54,6 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</ul>
|
</ul>
|
||||||
</li>
|
</li>
|
||||||
{% else %}
|
|
||||||
<li>
|
|
||||||
<a href="{% url 'auth_login' %}?next={{ request.path }}">Log in</a>
|
|
||||||
</li>
|
|
||||||
<li>
|
|
||||||
<a href="{% url 'registration_register' %}">Sign up</a>
|
|
||||||
</li>
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
|
@ -3,7 +3,7 @@
|
||||||
{% block title %}{{ site.domain }}{% endblock %}
|
{% block title %}{{ site.domain }}{% endblock %}
|
||||||
|
|
||||||
{% block navbarbrand %}
|
{% block navbarbrand %}
|
||||||
<a class="navbar-brand navbar-brand-active" href="{% url 'home' %}">{{ site.domain }}</a>
|
<a class="navbar-brand navbar-brand-active" href="{% url 'index' %}">{{ site.domain }}</a>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
|
@ -1,24 +1,26 @@
|
||||||
"""General/baselite/site-wide URLs."""
|
"""General/baselite/site-wide URLs."""
|
||||||
|
|
||||||
from django.conf.urls import include, url
|
|
||||||
from django.contrib import admin
|
|
||||||
from django.views.generic import TemplateView
|
|
||||||
|
|
||||||
from adminplus.sites import AdminSitePlus
|
from adminplus.sites import AdminSitePlus
|
||||||
|
from django.conf.urls import include
|
||||||
|
from django.contrib import admin
|
||||||
|
from django.urls import path
|
||||||
|
from django.views.generic import TemplateView
|
||||||
|
|
||||||
admin.site = AdminSitePlus()
|
admin.site = AdminSitePlus()
|
||||||
admin.sites.site = admin.site
|
admin.sites.site = admin.site
|
||||||
admin.autodiscover()
|
admin.autodiscover()
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^$', TemplateView.as_view(template_name='index.html'), name='home'),
|
path('', TemplateView.as_view(template_name='index.html'), name='index'),
|
||||||
|
|
||||||
url(r'^dispatch/', include('dispatch.urls')),
|
path('countdown/', include('countdown.urls')),
|
||||||
url(r'^itemsets/', include('facts.urls')),
|
path('dice/', include('dice.urls')),
|
||||||
url(r'^karma/', include('karma.urls')),
|
path('dispatch/', include('dispatch.urls')),
|
||||||
url(r'^markov/', include('markov.urls')),
|
path('itemsets/', include('facts.urls')),
|
||||||
url(r'^races/', include('races.urls')),
|
path('karma/', include('karma.urls')),
|
||||||
|
path('markov/', include('markov.urls')),
|
||||||
|
path('pi/', include('pi.urls')),
|
||||||
|
path('races/', include('races.urls')),
|
||||||
|
path('weather/', include('weather.urls')),
|
||||||
|
|
||||||
url(r'^accounts/', include('registration.backends.default.urls')),
|
path('admin/', admin.site.urls),
|
||||||
url(r'^admin/', include(admin.site.urls)),
|
|
||||||
]
|
]
|
||||||
|
|
|
@ -3,8 +3,8 @@ import logging
|
||||||
|
|
||||||
from irc.client import NickMask
|
from irc.client import NickMask
|
||||||
|
|
||||||
from ircbot.lib import Plugin, has_permission
|
|
||||||
from facts.models import Fact, FactCategory
|
from facts.models import Fact, FactCategory
|
||||||
|
from ircbot.lib import Plugin
|
||||||
|
|
||||||
log = logging.getLogger('facts.ircplugin')
|
log = logging.getLogger('facts.ircplugin')
|
||||||
|
|
||||||
|
@ -14,10 +14,14 @@ class Facts(Plugin):
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
"""Set up the handlers."""
|
"""Set up the handlers."""
|
||||||
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!facts\s+add\s+(\S+)\s+(.*)$',
|
self.connection.reactor.add_global_regex_handler(
|
||||||
self.handle_add_fact, -20)
|
['pubmsg', 'privmsg'], r'remember\s+that\s+(?P<which>\S+)\s+(is|means)\s+(?P<what>.*)$',
|
||||||
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!facts\s+(\S+)(\s+(.*)$|$)',
|
self.handle_add_fact, -20
|
||||||
self.handle_facts, -20)
|
)
|
||||||
|
self.connection.reactor.add_global_regex_handler(
|
||||||
|
['pubmsg', 'privmsg'], r'^!f(acts)?\s+(?P<which>\S+)(\s+(?P<what>.*)$|$)',
|
||||||
|
self.handle_facts, -20
|
||||||
|
)
|
||||||
|
|
||||||
super(Facts, self).start()
|
super(Facts, self).start()
|
||||||
|
|
||||||
|
@ -30,10 +34,10 @@ class Facts(Plugin):
|
||||||
|
|
||||||
def handle_facts(self, connection, event, match):
|
def handle_facts(self, connection, event, match):
|
||||||
"""Respond to the facts command with desired fact."""
|
"""Respond to the facts command with desired fact."""
|
||||||
category = match.group(1)
|
category = match.group('which')
|
||||||
regex = None
|
regex = None
|
||||||
if match.group(2) != '':
|
if match.group(2) != '':
|
||||||
regex = match.group(3)
|
regex = match.group('what')
|
||||||
|
|
||||||
fact = Fact.objects.random_fact(category, regex)
|
fact = Fact.objects.random_fact(category, regex)
|
||||||
if fact:
|
if fact:
|
||||||
|
@ -47,15 +51,16 @@ class Facts(Plugin):
|
||||||
|
|
||||||
def handle_add_fact(self, connection, event, match):
|
def handle_add_fact(self, connection, event, match):
|
||||||
"""Add a new fact to the database."""
|
"""Add a new fact to the database."""
|
||||||
category_name = match.group(1)
|
if event.in_privmsg or event.addressed:
|
||||||
fact_text = match.group(2)
|
category_name = match.group('which')
|
||||||
|
fact_text = match.group('what')
|
||||||
|
|
||||||
if has_permission(event.source, 'facts.add_fact'):
|
|
||||||
# create the category
|
# create the category
|
||||||
category, created = FactCategory.objects.get_or_create(name=category_name)
|
category, created = FactCategory.objects.get_or_create(name=category_name)
|
||||||
fact = Fact.objects.create(fact=fact_text, category=category, nickmask=event.source)
|
fact = Fact.objects.create(fact=fact_text, category=category, nickmask=event.source)
|
||||||
if fact:
|
if fact:
|
||||||
return self.bot.reply(event, "fact added to {0:s}".format(category.name))
|
self.bot.reply(event, f"ok, I now know more about {category.name}")
|
||||||
|
return 'NO MORE'
|
||||||
|
|
||||||
|
|
||||||
plugin = Facts
|
plugin = Facts
|
||||||
|
|
|
@ -26,6 +26,6 @@ class Migration(migrations.Migration):
|
||||||
migrations.AddField(
|
migrations.AddField(
|
||||||
model_name='fact',
|
model_name='fact',
|
||||||
name='category',
|
name='category',
|
||||||
field=models.ForeignKey(to='facts.FactCategory'),
|
field=models.ForeignKey(to='facts.FactCategory', on_delete=models.CASCADE),
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# Generated by Django 1.10.5 on 2017-02-11 15:00
|
# Generated by Django 1.10.5 on 2017-02-11 15:00
|
||||||
from __future__ import unicode_literals
|
|
||||||
|
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -52,7 +52,7 @@ class Fact(models.Model):
|
||||||
"""Define facts."""
|
"""Define facts."""
|
||||||
|
|
||||||
fact = models.TextField()
|
fact = models.TextField()
|
||||||
category = models.ForeignKey(FactCategory)
|
category = models.ForeignKey(FactCategory, on_delete=models.CASCADE)
|
||||||
nickmask = models.CharField(max_length=200, default='', blank=True)
|
nickmask = models.CharField(max_length=200, default='', blank=True)
|
||||||
time = models.DateTimeField(auto_now_add=True)
|
time = models.DateTimeField(auto_now_add=True)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
"""Serializers for the fact objects."""
|
||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
from facts.models import Fact
|
||||||
|
|
||||||
|
|
||||||
|
class FactSerializer(serializers.ModelSerializer):
|
||||||
|
"""Serializer for the REST API."""
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
"""Meta options."""
|
||||||
|
|
||||||
|
model = Fact
|
||||||
|
fields = ('id', 'fact', 'category')
|
|
@ -1,9 +1,12 @@
|
||||||
"""URL patterns for the facts web views."""
|
"""URL patterns for the facts web views."""
|
||||||
from django.conf.urls import url
|
from django.urls import path
|
||||||
|
|
||||||
from facts.views import index, factcategory_detail
|
from facts.views import factcategory_detail, index, rpc_get_facts, rpc_get_random_fact
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^$', index, name='facts_index'),
|
path('rpc/<category>/', rpc_get_facts, name='weather_rpc_get_facts'),
|
||||||
url(r'^(?P<factcategory_name>.+)/$', factcategory_detail, name='facts_factcategory_detail'),
|
path('rpc/<category>/random/', rpc_get_random_fact, name='weather_rpc_get_random_fact'),
|
||||||
|
|
||||||
|
path('', index, name='facts_index'),
|
||||||
|
path('<factcategory_name>/', factcategory_detail, name='facts_factcategory_detail'),
|
||||||
]
|
]
|
||||||
|
|
|
@ -2,12 +2,49 @@
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from django.shortcuts import get_object_or_404, render
|
from django.shortcuts import get_object_or_404, render
|
||||||
|
from rest_framework.authentication import BasicAuthentication
|
||||||
|
from rest_framework.decorators import api_view, authentication_classes, permission_classes
|
||||||
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
from rest_framework.response import Response
|
||||||
|
|
||||||
from facts.models import FactCategory
|
from facts.models import FactCategory
|
||||||
|
from facts.serializers import FactSerializer
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@api_view(['GET'])
|
||||||
|
@authentication_classes((BasicAuthentication, ))
|
||||||
|
@permission_classes((IsAuthenticated, ))
|
||||||
|
def rpc_get_facts(request, category):
|
||||||
|
"""Get all the facts in a category."""
|
||||||
|
if request.method != 'GET':
|
||||||
|
return Response({'detail': "Supported method: GET."}, status=405)
|
||||||
|
|
||||||
|
try:
|
||||||
|
fact_category = FactCategory.objects.get(name=category)
|
||||||
|
except FactCategory.DoesNotExist:
|
||||||
|
return Response({'detail': f"Item set category '{category}' not found."}, status=404)
|
||||||
|
|
||||||
|
return Response(FactSerializer(fact_category.fact_set.all(), many=True).data)
|
||||||
|
|
||||||
|
|
||||||
|
@api_view(['GET'])
|
||||||
|
@authentication_classes((BasicAuthentication, ))
|
||||||
|
@permission_classes((IsAuthenticated, ))
|
||||||
|
def rpc_get_random_fact(request, category):
|
||||||
|
"""Get all the facts in a category."""
|
||||||
|
if request.method != 'GET':
|
||||||
|
return Response({'detail': "Supported method: GET."}, status=405)
|
||||||
|
|
||||||
|
try:
|
||||||
|
fact_category = FactCategory.objects.get(name=category)
|
||||||
|
except FactCategory.DoesNotExist:
|
||||||
|
return Response({'detail': f"Item set category '{category}' not found."}, status=404)
|
||||||
|
|
||||||
|
return Response(FactSerializer(fact_category.random_fact()).data)
|
||||||
|
|
||||||
|
|
||||||
def index(request):
|
def index(request):
|
||||||
"""Display a simple list of the fact categories, for the moment."""
|
"""Display a simple list of the fact categories, for the moment."""
|
||||||
factcategories = FactCategory.objects.all()
|
factcategories = FactCategory.objects.all()
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
"""Admin stuff for GitLab bot models."""
|
|
||||||
|
|
||||||
from django.contrib import admin
|
|
||||||
|
|
||||||
from gitlab_bot.models import GitlabConfig, GitlabProjectConfig
|
|
||||||
|
|
||||||
admin.site.register(GitlabConfig)
|
|
||||||
admin.site.register(GitlabProjectConfig)
|
|
|
@ -1,253 +0,0 @@
|
||||||
"""Wrapped client for the configured GitLab bot."""
|
|
||||||
|
|
||||||
import logging
|
|
||||||
import random
|
|
||||||
import re
|
|
||||||
|
|
||||||
import gitlab
|
|
||||||
|
|
||||||
from gitlab_bot.models import GitlabConfig
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class GitlabBot(object):
|
|
||||||
|
|
||||||
"""Bot for doing GitLab stuff. Might be used by the IRC bot, or daemons, or whatever."""
|
|
||||||
|
|
||||||
REVIEWS_START_FORMAT = "Starting review process."
|
|
||||||
REVIEWS_RESET_FORMAT = "Review process reset."
|
|
||||||
REVIEWS_REVIEW_REGISTERED_FORMAT = "Review identified."
|
|
||||||
REVIEWS_PROGRESS_FORMAT = "{0:d} reviews of {1:d} necessary to proceed."
|
|
||||||
REVIEWS_REVIEWS_COMPLETE = "Reviews complete."
|
|
||||||
NEW_REVIEWER_FORMAT = "Assigning to {0:s} to review merge request."
|
|
||||||
NEW_ACCEPTER_FORMAT = "Assigning to {0:s} to accept merge request."
|
|
||||||
|
|
||||||
NOTE_COMMENT = [
|
|
||||||
"The computer is your friend.",
|
|
||||||
"Trust the computer.",
|
|
||||||
"Quality is mandatory.",
|
|
||||||
"Errors show your disloyalty to the computer.",
|
|
||||||
]
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
"""Initialize the actual GitLab client."""
|
|
||||||
config = GitlabConfig.objects.first()
|
|
||||||
self.client = gitlab.Gitlab(config.url, private_token=config.token)
|
|
||||||
self.client.auth()
|
|
||||||
|
|
||||||
def random_reviews_start_message(self):
|
|
||||||
return "{0:s} {1:s} {2:s}".format(self.REVIEWS_START_FORMAT, self.REVIEWS_PROGRESS_FORMAT,
|
|
||||||
random.choice(self.NOTE_COMMENT))
|
|
||||||
|
|
||||||
def random_reviews_reset_message(self):
|
|
||||||
return "{0:s} {1:s} {2:s}".format(self.REVIEWS_RESET_FORMAT, self.REVIEWS_PROGRESS_FORMAT,
|
|
||||||
random.choice(self.NOTE_COMMENT))
|
|
||||||
|
|
||||||
def random_review_progress_message(self):
|
|
||||||
return "{0:s} {1:s} {2:s}".format(self.REVIEWS_REVIEW_REGISTERED_FORMAT, self.REVIEWS_PROGRESS_FORMAT,
|
|
||||||
random.choice(self.NOTE_COMMENT))
|
|
||||||
|
|
||||||
def random_reviews_complete_message(self):
|
|
||||||
return "{0:s} {1:s}".format(self.REVIEWS_REVIEWS_COMPLETE, random.choice(self.NOTE_COMMENT))
|
|
||||||
|
|
||||||
def random_new_reviewer_message(self):
|
|
||||||
return "{0:s} {1:s}".format(self.NEW_REVIEWER_FORMAT, random.choice(self.NOTE_COMMENT))
|
|
||||||
|
|
||||||
def random_reviews_done_message(self):
|
|
||||||
return "{0:s} {1:s}".format(self.NEW_ACCEPTER_FORMAT, random.choice(self.NOTE_COMMENT))
|
|
||||||
|
|
||||||
def scan_project_for_reviews(self, project, merge_request_ids=None):
|
|
||||||
project_obj = self.client.projects.get(project.project_id)
|
|
||||||
if not project_obj:
|
|
||||||
return
|
|
||||||
|
|
||||||
if merge_request_ids:
|
|
||||||
merge_requests = []
|
|
||||||
for merge_request_id in merge_request_ids:
|
|
||||||
merge_requests.append(project_obj.mergerequests.get(id=merge_request_id))
|
|
||||||
else:
|
|
||||||
merge_requests = project_obj.mergerequests.list(state='opened')
|
|
||||||
|
|
||||||
for merge_request in merge_requests:
|
|
||||||
log.debug("scanning merge request '%s'", merge_request.title)
|
|
||||||
|
|
||||||
if merge_request.state in ['merged', 'closed']:
|
|
||||||
log.info("merge request '%s' is already %s, doing nothing", merge_request.title,
|
|
||||||
merge_request.state)
|
|
||||||
continue
|
|
||||||
|
|
||||||
request_state = _MergeRequestScanningState()
|
|
||||||
notes = sorted(self.client.project_mergerequest_notes.list(project_id=merge_request.project_id,
|
|
||||||
merge_request_id=merge_request.id,
|
|
||||||
all=True),
|
|
||||||
key=lambda x: x.id)
|
|
||||||
for note in notes:
|
|
||||||
if not note.system:
|
|
||||||
log.debug("merge request '%s', note '%s' is a normal message", merge_request.title, note.id)
|
|
||||||
# note that we can't ignore note.system = True + merge_request.author, that might have been
|
|
||||||
# a new push or something, so we only ignore normal notes from the author
|
|
||||||
if note.author == merge_request.author:
|
|
||||||
log.debug("skipping note from the merge request author")
|
|
||||||
elif note.author.username == self.client.user.username:
|
|
||||||
log.debug("saw a message from myself, i might have already sent a notice i'm sitting on")
|
|
||||||
if note.body.find(self.REVIEWS_RESET_FORMAT) >= 0 and request_state.unlogged_approval_reset:
|
|
||||||
log.debug("saw a reset message, unsetting the flag")
|
|
||||||
request_state.unlogged_approval_reset = False
|
|
||||||
elif note.body.find(self.REVIEWS_START_FORMAT) >= 0 and request_state.unlogged_review_start:
|
|
||||||
log.debug("saw a start message, unsetting the flag")
|
|
||||||
request_state.unlogged_review_start = False
|
|
||||||
elif note.body.find(self.REVIEWS_REVIEW_REGISTERED_FORMAT) >= 0 and request_state.unlogged_approval:
|
|
||||||
log.debug("saw a review log message, unsetting the flag")
|
|
||||||
request_state.unlogged_approval = False
|
|
||||||
elif note.body.find(self.REVIEWS_REVIEWS_COMPLETE) >= 0 and request_state.unlogged_review_complete:
|
|
||||||
log.debug("saw a review complete message, unsetting the flag")
|
|
||||||
request_state.unlogged_review_complete = False
|
|
||||||
else:
|
|
||||||
log.debug("nothing in particular relevant in '%s'", note.body)
|
|
||||||
else:
|
|
||||||
if note.body.find("LGTM") >= 0:
|
|
||||||
log.debug("merge request '%s', note '%s' has a LGTM", merge_request.title, note.id)
|
|
||||||
request_state.unlogged_approval = True
|
|
||||||
request_state.approver_list.append(note.author.username)
|
|
||||||
log.debug("approvers: %s", request_state.approver_list)
|
|
||||||
if len(request_state.approver_list) < project.code_reviews_necessary:
|
|
||||||
log.debug("not enough code reviews yet, setting needs_reviewer")
|
|
||||||
request_state.needs_reviewer = True
|
|
||||||
request_state.needs_accepter = False
|
|
||||||
else:
|
|
||||||
log.debug("enough code reviews, setting needs_accepter")
|
|
||||||
request_state.needs_accepter = True
|
|
||||||
request_state.needs_reviewer = False
|
|
||||||
request_state.unlogged_review_complete = True
|
|
||||||
else:
|
|
||||||
log.debug("merge request '%s', note '%s' does not have a LGTM", merge_request.title,
|
|
||||||
note.id)
|
|
||||||
else:
|
|
||||||
log.debug("merge request '%s', note '%s' is a system message", merge_request.title, note.id)
|
|
||||||
if re.match(r'Added \d+ commit', note.body):
|
|
||||||
log.debug("resetting approval list, '%s' looks like a push!", note.body)
|
|
||||||
|
|
||||||
# only set the unlogged approval reset flag if there's some kind of progress
|
|
||||||
if len(request_state.approver_list) > 0:
|
|
||||||
request_state.unlogged_approval_reset = True
|
|
||||||
request_state.needs_reviewer = True
|
|
||||||
request_state.approver_list.clear()
|
|
||||||
else:
|
|
||||||
log.debug("leaving the approval list as it is, i don't think '%s' is a push", note.body)
|
|
||||||
|
|
||||||
# do some cleanup
|
|
||||||
excluded_review_candidates = request_state.approver_list + [merge_request.author.username]
|
|
||||||
review_candidates = [x for x in project.code_reviewers.split(',') if x not in excluded_review_candidates]
|
|
||||||
if merge_request.assignee:
|
|
||||||
if request_state.needs_reviewer and merge_request.assignee.username in review_candidates:
|
|
||||||
log.debug("unsetting the needs_reviewer flag, the request is already assigned to one")
|
|
||||||
request_state.needs_reviewer = False
|
|
||||||
elif request_state.needs_reviewer and merge_request.assignee.username == merge_request.author.username:
|
|
||||||
log.info("unsetting the needs_reviewer flag, the request is assigned to the author")
|
|
||||||
log.info("in this case we are assuming that the author has work to do, and will re/unassign")
|
|
||||||
request_state.needs_reviewer = False
|
|
||||||
|
|
||||||
excluded_accept_candidates = [merge_request.author.username]
|
|
||||||
accept_candidates = [x for x in project.code_review_final_merge_assignees.split(',') if x not in excluded_accept_candidates]
|
|
||||||
if merge_request.assignee:
|
|
||||||
if request_state.needs_accepter and merge_request.assignee.username in accept_candidates:
|
|
||||||
log.debug("unsetting the needs_accepter flag, the request is already assigned to one")
|
|
||||||
request_state.needs_accepter = False
|
|
||||||
elif request_state.needs_accepter and merge_request.assignee.username == merge_request.author.username:
|
|
||||||
log.info("unsetting the needs_accepter flag, the request is assigned to the author")
|
|
||||||
log.info("in this case we are assuming that the author has work to do, and will re/unassign")
|
|
||||||
request_state.needs_accepter = False
|
|
||||||
|
|
||||||
log.debug("%s", request_state.__dict__)
|
|
||||||
|
|
||||||
# status message stuff
|
|
||||||
if request_state.unlogged_review_start:
|
|
||||||
log.info("sending message for start of reviews")
|
|
||||||
msg = {'body': self.random_reviews_start_message().format(len(request_state.approver_list),
|
|
||||||
project.code_reviews_necessary)}
|
|
||||||
self.client.project_mergerequest_notes.create(msg, project_id=project_obj.id,
|
|
||||||
merge_request_id=merge_request.id)
|
|
||||||
if request_state.unlogged_approval_reset:
|
|
||||||
log.info("sending message for review reset")
|
|
||||||
msg = {'body': self.random_reviews_reset_message().format(len(request_state.approver_list),
|
|
||||||
project.code_reviews_necessary)}
|
|
||||||
self.client.project_mergerequest_notes.create(msg, project_id=project_obj.id,
|
|
||||||
merge_request_id=merge_request.id)
|
|
||||||
if request_state.unlogged_approval:
|
|
||||||
log.info("sending message for code review progress")
|
|
||||||
msg = {'body': self.random_review_progress_message().format(len(request_state.approver_list),
|
|
||||||
project.code_reviews_necessary)}
|
|
||||||
self.client.project_mergerequest_notes.create(msg, project_id=project_obj.id,
|
|
||||||
merge_request_id=merge_request.id)
|
|
||||||
if request_state.unlogged_review_complete:
|
|
||||||
log.info("sending message for code review complete")
|
|
||||||
msg = {'body': self.random_reviews_complete_message()}
|
|
||||||
self.client.project_mergerequest_notes.create(msg, project_id=project_obj.id,
|
|
||||||
merge_request_id=merge_request.id)
|
|
||||||
|
|
||||||
# if there's a reviewer necessary, assign the merge request
|
|
||||||
if len(request_state.approver_list) < project.code_reviews_necessary and request_state.needs_reviewer:
|
|
||||||
log.debug("%s needs a code review", merge_request.title)
|
|
||||||
if merge_request.assignee is not None:
|
|
||||||
log.debug("%s currently assigned to %s", merge_request.title, merge_request.assignee.username)
|
|
||||||
|
|
||||||
if merge_request.assignee is None or merge_request.assignee.username not in review_candidates:
|
|
||||||
if len(review_candidates) > 0:
|
|
||||||
new_reviewer = review_candidates[merge_request.iid % len(review_candidates)]
|
|
||||||
log.debug("%s is the new reviewer", new_reviewer)
|
|
||||||
|
|
||||||
# get the user object for the new reviewer
|
|
||||||
new_reviewer_obj = self.client.users.get_by_username(new_reviewer)
|
|
||||||
|
|
||||||
# create note for the update
|
|
||||||
msg = {'body': self.random_new_reviewer_message().format(new_reviewer_obj.name)}
|
|
||||||
self.client.project_mergerequest_notes.create(msg, project_id=project_obj.id,
|
|
||||||
merge_request_id=merge_request.id)
|
|
||||||
|
|
||||||
# assign the merge request to the new reviewer
|
|
||||||
self.client.update(merge_request, assignee_id=new_reviewer_obj.id)
|
|
||||||
else:
|
|
||||||
log.warning("no reviewers left to review %s, doing nothing", merge_request.title)
|
|
||||||
else:
|
|
||||||
log.debug("needs_reviewer set but the request is assigned to a reviewer, doing nothing")
|
|
||||||
|
|
||||||
# if there's an accepter necessary, assign the merge request
|
|
||||||
if len(request_state.approver_list) >= project.code_reviews_necessary and request_state.needs_accepter:
|
|
||||||
log.debug("%s needs an accepter", merge_request.title)
|
|
||||||
if merge_request.assignee is not None:
|
|
||||||
log.debug("%s currently assigned to %s", merge_request.title, merge_request.assignee.username)
|
|
||||||
|
|
||||||
if merge_request.assignee is None or merge_request.assignee.username not in accept_candidates:
|
|
||||||
if len(accept_candidates) > 0:
|
|
||||||
new_accepter = accept_candidates[merge_request.iid % len(accept_candidates)]
|
|
||||||
log.debug("%s is the new accepter", new_accepter)
|
|
||||||
|
|
||||||
# get the user object for the new accepter
|
|
||||||
new_accepter_obj = self.client.users.get_by_username(new_accepter)
|
|
||||||
|
|
||||||
# create note for the update
|
|
||||||
msg = {'body': self.random_reviews_done_message().format(new_accepter_obj.name)}
|
|
||||||
self.client.project_mergerequest_notes.create(msg, project_id=project_obj.id,
|
|
||||||
merge_request_id=merge_request.id)
|
|
||||||
|
|
||||||
# assign the merge request to the new reviewer
|
|
||||||
self.client.update(merge_request, assignee_id=new_accepter_obj.id)
|
|
||||||
else:
|
|
||||||
log.warning("no accepters left to accept %s, doing nothing", merge_request.title)
|
|
||||||
|
|
||||||
|
|
||||||
class _MergeRequestScanningState(object):
|
|
||||||
|
|
||||||
"""Track the state of a merge request as it is scanned and appropriate action identified."""
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
"""Set default flags/values."""
|
|
||||||
self.approver_list = []
|
|
||||||
|
|
||||||
self.unlogged_review_start = True
|
|
||||||
self.unlogged_approval_reset = False
|
|
||||||
self.unlogged_approval = False
|
|
||||||
self.unlogged_review_complete = False
|
|
||||||
self.needs_reviewer = True
|
|
||||||
self.needs_accepter = False
|
|
|
@ -1,20 +0,0 @@
|
||||||
"""Find merge requests that need code reviewers."""
|
|
||||||
|
|
||||||
import logging
|
|
||||||
|
|
||||||
from django.core.management import BaseCommand
|
|
||||||
|
|
||||||
from gitlab_bot.lib import GitlabBot
|
|
||||||
from gitlab_bot.models import GitlabProjectConfig
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
|
||||||
help = "Find merge requests needing code reviewers/accepters"
|
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
|
||||||
bot = GitlabBot()
|
|
||||||
projects = GitlabProjectConfig.objects.filter(manage_merge_request_code_reviews=True)
|
|
||||||
for project in projects:
|
|
||||||
bot.scan_project_for_reviews(project)
|
|
|
@ -1,25 +0,0 @@
|
||||||
"""Run the code review process on a specific merge request."""
|
|
||||||
|
|
||||||
import logging
|
|
||||||
|
|
||||||
from django.core.management import BaseCommand
|
|
||||||
|
|
||||||
from gitlab_bot.lib import GitlabBot
|
|
||||||
from gitlab_bot.models import GitlabProjectConfig
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
|
||||||
help = "Assign code reviewers/accepters for a specific merge request"
|
|
||||||
|
|
||||||
def add_arguments(self, parser):
|
|
||||||
parser.add_argument('project_id', type=int)
|
|
||||||
parser.add_argument('merge_request_id', type=int)
|
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
|
||||||
project = GitlabProjectConfig.objects.get(pk=options['project_id'])
|
|
||||||
merge_request_ids = [options['merge_request_id'], ]
|
|
||||||
|
|
||||||
bot = GitlabBot()
|
|
||||||
bot.scan_project_for_reviews(project, merge_request_ids=merge_request_ids)
|
|
|
@ -1,33 +0,0 @@
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
from __future__ import unicode_literals
|
|
||||||
|
|
||||||
from django.db import migrations, models
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
|
||||||
|
|
||||||
dependencies = [
|
|
||||||
]
|
|
||||||
|
|
||||||
operations = [
|
|
||||||
migrations.CreateModel(
|
|
||||||
name='GitlabConfig',
|
|
||||||
fields=[
|
|
||||||
('id', models.AutoField(auto_created=True, verbose_name='ID', primary_key=True, serialize=False)),
|
|
||||||
('url', models.URLField()),
|
|
||||||
('token', models.CharField(max_length=64)),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
migrations.CreateModel(
|
|
||||||
name='GitlabProjectConfig',
|
|
||||||
fields=[
|
|
||||||
('id', models.AutoField(auto_created=True, verbose_name='ID', primary_key=True, serialize=False)),
|
|
||||||
('project_id', models.CharField(max_length=64)),
|
|
||||||
('manage_merge_request_code_reviews', models.BooleanField(default=False)),
|
|
||||||
('code_reviews_necessary', models.PositiveSmallIntegerField(default=0)),
|
|
||||||
('code_reviewers', models.TextField(blank=True, default='')),
|
|
||||||
('code_review_final_merge_assignees', models.TextField(blank=True, default='')),
|
|
||||||
('gitlab_config', models.ForeignKey(to='gitlab_bot.GitlabConfig', null=True)),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
]
|
|
|
@ -1,19 +0,0 @@
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
from __future__ import unicode_literals
|
|
||||||
|
|
||||||
from django.db import migrations, models
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
|
||||||
|
|
||||||
dependencies = [
|
|
||||||
('gitlab_bot', '0001_initial'),
|
|
||||||
]
|
|
||||||
|
|
||||||
operations = [
|
|
||||||
migrations.AlterField(
|
|
||||||
model_name='gitlabprojectconfig',
|
|
||||||
name='project_id',
|
|
||||||
field=models.CharField(unique=True, max_length=64),
|
|
||||||
),
|
|
||||||
]
|
|
|
@ -1,36 +0,0 @@
|
||||||
"""Bot/daemons for doing stuff with GitLab."""
|
|
||||||
|
|
||||||
import logging
|
|
||||||
|
|
||||||
from django.db import models
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class GitlabConfig(models.Model):
|
|
||||||
|
|
||||||
"""Maintain bot-wide settings (URL, auth key, etc.)."""
|
|
||||||
|
|
||||||
url = models.URLField()
|
|
||||||
token = models.CharField(max_length=64)
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
"""String representation."""
|
|
||||||
return "bot @ {0:s}".format(self.url)
|
|
||||||
|
|
||||||
|
|
||||||
class GitlabProjectConfig(models.Model):
|
|
||||||
|
|
||||||
"""Maintain settings for a particular project in GitLab."""
|
|
||||||
|
|
||||||
gitlab_config = models.ForeignKey('GitlabConfig', null=True)
|
|
||||||
project_id = models.CharField(max_length=64, unique=True)
|
|
||||||
|
|
||||||
manage_merge_request_code_reviews = models.BooleanField(default=False)
|
|
||||||
code_reviews_necessary = models.PositiveSmallIntegerField(default=0)
|
|
||||||
code_reviewers = models.TextField(default='', blank=True)
|
|
||||||
code_review_final_merge_assignees = models.TextField(default='', blank=True)
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
"""String representation."""
|
|
||||||
return "configuration for {0:s} @ {1:s}".format(self.project_id, self.gitlab_config.url)
|
|
|
@ -0,0 +1 @@
|
||||||
|
"""Track IRC channel history and report on it for users."""
|
|
@ -0,0 +1,160 @@
|
||||||
|
"""Monitor and playback IRC stuff."""
|
||||||
|
import logging
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
import irc.client
|
||||||
|
|
||||||
|
from ircbot.lib import Plugin, most_specific_message
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class History(Plugin):
|
||||||
|
"""Watch the history of IRC channels and try to track what users may have missed."""
|
||||||
|
|
||||||
|
what_missed_regex = r'(?i)(what did I miss\?|did I miss anything\?)$'
|
||||||
|
|
||||||
|
def __init__(self, bot, connection, event):
|
||||||
|
"""Initialize some tracking stuff."""
|
||||||
|
super(History, self).__init__(bot, connection, event)
|
||||||
|
self.channel_history = {}
|
||||||
|
self.channel_participants = {}
|
||||||
|
self.channel_leave_points = {}
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
"""Set up the handlers."""
|
||||||
|
logger.debug("%s starting up", __name__)
|
||||||
|
self.connection.add_global_handler('pubmsg', self.handle_chatter, 50)
|
||||||
|
self.connection.add_global_handler('join', self.handle_join, 50)
|
||||||
|
self.connection.add_global_handler('part', self.handle_part, 50)
|
||||||
|
self.connection.add_global_handler('quit', self.handle_quit, 50)
|
||||||
|
|
||||||
|
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], self.what_missed_regex,
|
||||||
|
self.handle_what_missed, 60)
|
||||||
|
|
||||||
|
super(History, self).start()
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
"""Tear down handlers."""
|
||||||
|
logger.debug("%s shutting down", __name__)
|
||||||
|
self.connection.remove_global_handler('pubmsg', self.handle_chatter)
|
||||||
|
self.connection.remove_global_handler('join', self.handle_join)
|
||||||
|
self.connection.remove_global_handler('part', self.handle_part)
|
||||||
|
self.connection.remove_global_handler('quit', self.handle_quit)
|
||||||
|
|
||||||
|
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_what_missed)
|
||||||
|
|
||||||
|
super(History, self).stop()
|
||||||
|
|
||||||
|
def handle_chatter(self, connection, event):
|
||||||
|
"""Track IRC chatter."""
|
||||||
|
what = event.arguments[0]
|
||||||
|
where = event.target
|
||||||
|
who = irc.client.NickMask(event.source).nick
|
||||||
|
when = datetime.now()
|
||||||
|
|
||||||
|
logger.debug("tracking message for %s: (%s,%s)", where, who, what)
|
||||||
|
history = self.channel_history.setdefault(where, [])
|
||||||
|
history.append((where, when.isoformat(), who, what))
|
||||||
|
logger.debug("history for %s: %s", where, history)
|
||||||
|
|
||||||
|
# for when we maybe don't see a join, if they talked in the channel, add them to it
|
||||||
|
self._add_channel_participant(where, who)
|
||||||
|
|
||||||
|
def handle_join(self, connection, event):
|
||||||
|
"""Track who is entitled to see channel history."""
|
||||||
|
where = event.target
|
||||||
|
who = irc.client.NickMask(event.source).nick
|
||||||
|
logger.debug("%s joined %s", who, where)
|
||||||
|
|
||||||
|
self._add_channel_participant(where, who)
|
||||||
|
|
||||||
|
def handle_part(self, connection, event):
|
||||||
|
"""Note when people leave IRC channels."""
|
||||||
|
where = event.target
|
||||||
|
who = irc.client.NickMask(event.source).nick
|
||||||
|
logger.debug("%s left %s", who, where)
|
||||||
|
|
||||||
|
# if they parted the channel, they must have been in it, so note their point in history
|
||||||
|
self._add_channel_leave_point(where, who)
|
||||||
|
self._remove_channel_participant(where, who)
|
||||||
|
|
||||||
|
def handle_quit(self, connection, event):
|
||||||
|
"""Note when people leave IRC."""
|
||||||
|
who = irc.client.NickMask(event.source).nick
|
||||||
|
logger.debug("%s disconnected", who)
|
||||||
|
|
||||||
|
# find all channels the quitter was in, save their leave points
|
||||||
|
for channel in self.channel_participants.keys():
|
||||||
|
self._add_channel_leave_point(channel, who)
|
||||||
|
self._remove_channel_participant(channel, who)
|
||||||
|
|
||||||
|
def handle_what_missed(self, connection, event, match):
|
||||||
|
"""Tell the user what they missed."""
|
||||||
|
who = irc.client.NickMask(event.source).nick
|
||||||
|
if event.in_privmsg or event.addressed:
|
||||||
|
logger.debug("<%s> %s is asking for an update", who, most_specific_message(event))
|
||||||
|
if event.in_privmsg:
|
||||||
|
total_history = []
|
||||||
|
channel_count = 0
|
||||||
|
for channel in self.channel_leave_points.keys():
|
||||||
|
logger.debug("checking history slice for %s", channel)
|
||||||
|
total_history += self._missed_slice(channel, who)
|
||||||
|
self._delete_channel_leave_point(channel, who)
|
||||||
|
logger.debug("total history so far: %s", total_history)
|
||||||
|
channel_count += 1
|
||||||
|
logger.debug("final missed history: %s", total_history)
|
||||||
|
self._send_history(who, total_history)
|
||||||
|
self.bot.reply(event, f"{len(total_history)} line(s) over {channel_count} channel(s)")
|
||||||
|
return 'NO MORE'
|
||||||
|
else:
|
||||||
|
where = event.target
|
||||||
|
history = self._missed_slice(where, who)
|
||||||
|
self._delete_channel_leave_point(where, who)
|
||||||
|
self._send_history(who, history)
|
||||||
|
privmsged_str = " (PRIVMSGed)" if history else ""
|
||||||
|
self.bot.reply(event, f"{len(history)} line(s){privmsged_str}")
|
||||||
|
return 'NO MORE'
|
||||||
|
|
||||||
|
def _send_history(self, who, history):
|
||||||
|
"""Reply to who with missed history."""
|
||||||
|
for line in history:
|
||||||
|
self.bot.privmsg(who, f"{line[0]}: [{line[1]}] <{line[2]}> {line[3]}")
|
||||||
|
|
||||||
|
def _add_channel_leave_point(self, where, who):
|
||||||
|
"""Note that the given who left the channel at the current history point."""
|
||||||
|
leave_points = self.channel_leave_points.setdefault(where, {})
|
||||||
|
leave_points[who] = len(self.channel_history.setdefault(where, [])) - 1
|
||||||
|
logger.debug("leave points for %s: %s", where, leave_points)
|
||||||
|
|
||||||
|
def _delete_channel_leave_point(self, where, who):
|
||||||
|
"""Remove tracking for user's history point."""
|
||||||
|
leave_points = self.channel_leave_points.setdefault(where, {})
|
||||||
|
leave_points.pop(who, None)
|
||||||
|
logger.debug("leave points for %s: %s", where, leave_points)
|
||||||
|
|
||||||
|
def _add_channel_participant(self, where, who):
|
||||||
|
"""Add a who to the list of people who are/were in a channel."""
|
||||||
|
participants = self.channel_participants.setdefault(where, set())
|
||||||
|
participants.add(who)
|
||||||
|
logger.debug("participants for %s: %s", where, participants)
|
||||||
|
|
||||||
|
def _missed_slice(self, where, who):
|
||||||
|
"""Get the lines in where since who last left."""
|
||||||
|
leave_points = self.channel_leave_points.setdefault(where, {})
|
||||||
|
if leave_points.get(who) is not None:
|
||||||
|
leave_point = leave_points.get(who) + 1
|
||||||
|
history = self.channel_history.setdefault(where, [])
|
||||||
|
missed_history = history[leave_point:]
|
||||||
|
logger.debug("in %s, %s missed: %s", where, who, missed_history)
|
||||||
|
return missed_history
|
||||||
|
return []
|
||||||
|
|
||||||
|
def _remove_channel_participant(self, where, who):
|
||||||
|
"""Remove the specified who from the where channel's participants list."""
|
||||||
|
participants = self.channel_participants.setdefault(where, set())
|
||||||
|
participants.discard(who)
|
||||||
|
logger.debug("participants for %s: %s", where, participants)
|
||||||
|
|
||||||
|
|
||||||
|
plugin = History
|
|
@ -3,23 +3,15 @@
|
||||||
import logging
|
import logging
|
||||||
import xmlrpc.client
|
import xmlrpc.client
|
||||||
|
|
||||||
from django.conf import settings
|
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
|
|
||||||
from ircbot.forms import PrivmsgForm
|
from ircbot.forms import PrivmsgForm
|
||||||
from ircbot.models import Alias, BotUser, IrcChannel, IrcPlugin
|
from ircbot.models import Alias, BotUser, IrcChannel, IrcPlugin, IrcServer
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger('ircbot.admin')
|
log = logging.getLogger('ircbot.admin')
|
||||||
|
|
||||||
|
|
||||||
admin.site.register(Alias)
|
|
||||||
admin.site.register(BotUser)
|
|
||||||
admin.site.register(IrcChannel)
|
|
||||||
admin.site.register(IrcPlugin)
|
|
||||||
|
|
||||||
|
|
||||||
def send_privmsg(request):
|
def send_privmsg(request):
|
||||||
"""Send a privmsg over XML-RPC to the IRC bot."""
|
"""Send a privmsg over XML-RPC to the IRC bot."""
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
|
@ -28,7 +20,8 @@ def send_privmsg(request):
|
||||||
target = form.cleaned_data['target']
|
target = form.cleaned_data['target']
|
||||||
message = form.cleaned_data['message']
|
message = form.cleaned_data['message']
|
||||||
|
|
||||||
bot_url = 'http://{0:s}:{1:d}/'.format(settings.IRCBOT_XMLRPC_HOST, settings.IRCBOT_XMLRPC_PORT)
|
bot_url = 'http://{0:s}:{1:d}/'.format(form.cleaned_data['xmlrpc_host'],
|
||||||
|
form.cleaned_data['xmlrpc_port'])
|
||||||
bot = xmlrpc.client.ServerProxy(bot_url, allow_none=True)
|
bot = xmlrpc.client.ServerProxy(bot_url, allow_none=True)
|
||||||
bot.reply(None, message, False, target)
|
bot.reply(None, message, False, target)
|
||||||
form = PrivmsgForm()
|
form = PrivmsgForm()
|
||||||
|
@ -37,4 +30,11 @@ def send_privmsg(request):
|
||||||
|
|
||||||
return render(request, 'privmsg.html', {'form': form})
|
return render(request, 'privmsg.html', {'form': form})
|
||||||
|
|
||||||
|
|
||||||
|
admin.site.register(Alias)
|
||||||
|
admin.site.register(BotUser)
|
||||||
|
admin.site.register(IrcChannel)
|
||||||
|
admin.site.register(IrcPlugin)
|
||||||
|
admin.site.register(IrcServer)
|
||||||
|
|
||||||
admin.site.register_view('ircbot/privmsg/', "Ircbot - privmsg", view=send_privmsg, urlname='ircbot_privmsg')
|
admin.site.register_view('ircbot/privmsg/', "Ircbot - privmsg", view=send_privmsg, urlname='ircbot_privmsg')
|
||||||
|
|
288
ircbot/bot.py
288
ircbot/bot.py
|
@ -1,46 +1,36 @@
|
||||||
"""Provide the base IRC client bot which other code can latch onto."""
|
"""Provide the base IRC client bot which other code can latch onto."""
|
||||||
|
|
||||||
import bisect
|
import bisect
|
||||||
import collections
|
import collections
|
||||||
import copy
|
import copy
|
||||||
import importlib
|
import importlib
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
from xmlrpc.server import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
|
|
||||||
import socket
|
import socket
|
||||||
import ssl
|
import ssl
|
||||||
import sys
|
import sys
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
from xmlrpc.server import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer
|
||||||
|
|
||||||
from django.conf import settings
|
|
||||||
|
|
||||||
import irc.buffer
|
|
||||||
import irc.client
|
import irc.client
|
||||||
|
import irc.modes
|
||||||
|
from irc.bot import Channel
|
||||||
from irc.connection import Factory
|
from irc.connection import Factory
|
||||||
from irc.dict import IRCDict
|
from irc.dict import IRCDict
|
||||||
import irc.modes
|
from jaraco.stream import buffer
|
||||||
|
|
||||||
import ircbot.lib as ircbotlib
|
import ircbot.lib as ircbotlib
|
||||||
from ircbot.models import Alias, IrcChannel, IrcPlugin
|
from dr_botzo import __version__
|
||||||
|
from ircbot.models import Alias, IrcChannel, IrcPlugin, IrcServer
|
||||||
|
|
||||||
log = logging.getLogger('ircbot.bot')
|
log = logging.getLogger('ircbot.bot')
|
||||||
|
|
||||||
|
|
||||||
class IrcBotXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
|
|
||||||
"""Override the basic request handler to change the logging."""
|
|
||||||
|
|
||||||
def log_message(self, format, *args):
|
|
||||||
"""Use a logger rather than stderr."""
|
|
||||||
log.debug("XML-RPC - %s - %s", self.client_address[0], format % args)
|
|
||||||
|
|
||||||
|
|
||||||
class PrioritizedRegexHandler(collections.namedtuple('Base', ('priority', 'regex', 'callback'))):
|
class PrioritizedRegexHandler(collections.namedtuple('Base', ('priority', 'regex', 'callback'))):
|
||||||
"""Regex handler that still uses the normal handler priority stuff."""
|
"""Regex handler that still uses the normal handler priority stuff."""
|
||||||
|
|
||||||
def __lt__(self, other):
|
def __lt__(self, other):
|
||||||
"""When sorting prioritized handlers, only use the priority"""
|
"""When sorting prioritized handlers, only use the priority."""
|
||||||
return self.priority < other.priority
|
return self.priority < other.priority
|
||||||
|
|
||||||
|
|
||||||
|
@ -52,7 +42,9 @@ class LenientServerConnection(irc.client.ServerConnection):
|
||||||
method on a Reactor object.
|
method on a Reactor object.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
buffer_class = irc.buffer.LenientDecodingLineBuffer
|
buffer_class = buffer.LenientDecodingLineBuffer
|
||||||
|
|
||||||
|
server_config = None
|
||||||
|
|
||||||
def _prep_message(self, string):
|
def _prep_message(self, string):
|
||||||
"""Override SimpleIRCClient._prep_message to add some logging."""
|
"""Override SimpleIRCClient._prep_message to add some logging."""
|
||||||
|
@ -63,6 +55,9 @@ class LenientServerConnection(irc.client.ServerConnection):
|
||||||
class DrReactor(irc.client.Reactor):
|
class DrReactor(irc.client.Reactor):
|
||||||
"""Customize the basic IRC library's Reactor with more features."""
|
"""Customize the basic IRC library's Reactor with more features."""
|
||||||
|
|
||||||
|
# used by Reactor.server() to initialize
|
||||||
|
connection_class = LenientServerConnection
|
||||||
|
|
||||||
def __do_nothing(*args, **kwargs):
|
def __do_nothing(*args, **kwargs):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -71,18 +66,10 @@ class DrReactor(irc.client.Reactor):
|
||||||
super(DrReactor, self).__init__(on_connect=on_connect, on_disconnect=on_disconnect)
|
super(DrReactor, self).__init__(on_connect=on_connect, on_disconnect=on_disconnect)
|
||||||
self.regex_handlers = {}
|
self.regex_handlers = {}
|
||||||
|
|
||||||
def server(self):
|
|
||||||
"""Creates and returns a ServerConnection object."""
|
|
||||||
c = LenientServerConnection(self)
|
|
||||||
with self.mutex:
|
|
||||||
self.connections.append(c)
|
|
||||||
return c
|
|
||||||
|
|
||||||
def add_global_regex_handler(self, events, regex, handler, priority=0):
|
def add_global_regex_handler(self, events, regex, handler, priority=0):
|
||||||
"""Adds a global handler function for a specific event type and regex.
|
"""Add a global handler function for a specific event type and regex.
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
|
|
||||||
events --- Event type(s) (a list of strings).
|
events --- Event type(s) (a list of strings).
|
||||||
|
|
||||||
handler -- Callback function taking connection and event
|
handler -- Callback function taking connection and event
|
||||||
|
@ -112,10 +99,9 @@ class DrReactor(irc.client.Reactor):
|
||||||
bisect.insort(event_regex_handlers, handler)
|
bisect.insort(event_regex_handlers, handler)
|
||||||
|
|
||||||
def remove_global_regex_handler(self, events, handler):
|
def remove_global_regex_handler(self, events, handler):
|
||||||
"""Removes a global regex handler function.
|
"""Remove a global regex handler function.
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
|
|
||||||
events -- Event type(s) (a list of strings).
|
events -- Event type(s) (a list of strings).
|
||||||
handler -- Callback function.
|
handler -- Callback function.
|
||||||
|
|
||||||
|
@ -163,13 +149,31 @@ class DrReactor(irc.client.Reactor):
|
||||||
event.original_msg = what
|
event.original_msg = what
|
||||||
|
|
||||||
# check if we were addressed or not
|
# check if we were addressed or not
|
||||||
my_nick = connection.get_nickname()
|
if connection.server_config.additional_addressed_nicks:
|
||||||
addressed_pattern = r'^{0:s}[:,]\s+(?P<addressed_msg>.*)'.format(my_nick)
|
all_nicks = '|'.join(connection.server_config.additional_addressed_nicks.split('\n') +
|
||||||
|
[connection.get_nickname()])
|
||||||
|
else:
|
||||||
|
all_nicks = connection.get_nickname()
|
||||||
|
addressed_pattern = r'^(({nicks})[:,]|@({nicks})[:,]?)\s+(?P<addressed_msg>.*)'.format(nicks=all_nicks)
|
||||||
|
|
||||||
|
# ignore the first word, a nick, if the speaker is the bridge
|
||||||
|
try:
|
||||||
|
channel = IrcChannel.objects.get(name=sent_location)
|
||||||
|
if sender_nick == channel.discord_bridge:
|
||||||
|
short_what = ' '.join(what.split(' ')[1:])
|
||||||
|
match = re.match(addressed_pattern, short_what, re.IGNORECASE)
|
||||||
|
event.arguments[0] = short_what
|
||||||
|
else:
|
||||||
match = re.match(addressed_pattern, what, re.IGNORECASE)
|
match = re.match(addressed_pattern, what, re.IGNORECASE)
|
||||||
|
except IrcChannel.DoesNotExist:
|
||||||
|
match = re.match(addressed_pattern, what, re.IGNORECASE)
|
||||||
|
|
||||||
if match:
|
if match:
|
||||||
event.addressed = True
|
event.addressed = True
|
||||||
event.addressed_msg = match.group('addressed_msg')
|
event.addressed_msg = match.group('addressed_msg')
|
||||||
|
|
||||||
|
log.debug("all_nicks: %s, addressed: %s", all_nicks, event.addressed)
|
||||||
|
|
||||||
# only do aliasing for pubmsg/privmsg
|
# only do aliasing for pubmsg/privmsg
|
||||||
log.debug("checking for alias for %s", what)
|
log.debug("checking for alias for %s", what)
|
||||||
|
|
||||||
|
@ -209,8 +213,7 @@ class DrReactor(irc.client.Reactor):
|
||||||
if result == "NO MORE":
|
if result == "NO MORE":
|
||||||
return
|
return
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
log.error("caught exception!")
|
log.exception("caught exception!")
|
||||||
log.exception(ex)
|
|
||||||
connection.privmsg(event.target, str(ex))
|
connection.privmsg(event.target, str(ex))
|
||||||
|
|
||||||
def try_recursion(self, connection, event):
|
def try_recursion(self, connection, event):
|
||||||
|
@ -351,15 +354,16 @@ class IRCBot(irc.client.SimpleIRCClient):
|
||||||
reactor_class = DrReactor
|
reactor_class = DrReactor
|
||||||
splitter = "..."
|
splitter = "..."
|
||||||
|
|
||||||
def __init__(self, reconnection_interval=60):
|
def __init__(self, server_name, reconnection_interval=60):
|
||||||
"""Initialize bot."""
|
"""Initialize bot."""
|
||||||
super(IRCBot, self).__init__()
|
super(IRCBot, self).__init__()
|
||||||
|
|
||||||
self.channels = IRCDict()
|
self.channels = IRCDict()
|
||||||
self.plugins = []
|
self.plugins = []
|
||||||
|
|
||||||
# set up the server list
|
self.server_config = IrcServer.objects.get(name=server_name)
|
||||||
self.server_list = settings.IRCBOT_SERVER_LIST
|
# the reactor made the connection, save the server reference in it since we pass that around
|
||||||
|
self.connection.server_config = self.server_config
|
||||||
|
|
||||||
# set reconnection interval
|
# set reconnection interval
|
||||||
if not reconnection_interval or reconnection_interval < 0:
|
if not reconnection_interval or reconnection_interval < 0:
|
||||||
|
@ -367,8 +371,8 @@ class IRCBot(irc.client.SimpleIRCClient):
|
||||||
self.reconnection_interval = reconnection_interval
|
self.reconnection_interval = reconnection_interval
|
||||||
|
|
||||||
# set basic stuff
|
# set basic stuff
|
||||||
self._nickname = settings.IRCBOT_NICKNAME
|
self._nickname = self.server_config.nickname
|
||||||
self._realname = settings.IRCBOT_REALNAME
|
self._realname = self.server_config.realname
|
||||||
|
|
||||||
# guess at nickmask. hopefully _on_welcome() will set this, but this should be
|
# guess at nickmask. hopefully _on_welcome() will set this, but this should be
|
||||||
# a pretty good guess if not
|
# a pretty good guess if not
|
||||||
|
@ -395,8 +399,8 @@ class IRCBot(irc.client.SimpleIRCClient):
|
||||||
getattr(self, 'handle_reload'), -20)
|
getattr(self, 'handle_reload'), -20)
|
||||||
|
|
||||||
# load XML-RPC server
|
# load XML-RPC server
|
||||||
self.xmlrpc = SimpleXMLRPCServer((settings.IRCBOT_XMLRPC_HOST, settings.IRCBOT_XMLRPC_PORT),
|
self.xmlrpc = SimpleXMLRPCServer((self.server_config.xmlrpc_host, self.server_config.xmlrpc_port),
|
||||||
requestHandler=IrcBotXMLRPCRequestHandler, allow_none=True)
|
requestHandler=SimpleXMLRPCRequestHandler, allow_none=True)
|
||||||
self.xmlrpc.register_introspection_functions()
|
self.xmlrpc.register_introspection_functions()
|
||||||
|
|
||||||
t = threading.Thread(target=self._xmlrpc_listen, args=())
|
t = threading.Thread(target=self._xmlrpc_listen, args=())
|
||||||
|
@ -409,28 +413,25 @@ class IRCBot(irc.client.SimpleIRCClient):
|
||||||
|
|
||||||
def _connected_checker(self):
|
def _connected_checker(self):
|
||||||
if not self.connection.is_connected():
|
if not self.connection.is_connected():
|
||||||
self.connection.execute_delayed(self.reconnection_interval,
|
self.reactor.scheduler.execute_after(self.reconnection_interval, self._connected_checker)
|
||||||
self._connected_checker)
|
|
||||||
self.jump_server()
|
self.jump_server()
|
||||||
|
|
||||||
def _connect(self):
|
def _connect(self):
|
||||||
server = self.server_list[0]
|
|
||||||
try:
|
try:
|
||||||
# build the connection factory as determined by IPV6/SSL settings
|
# build the connection factory as determined by IPV6/SSL settings
|
||||||
if settings.IRCBOT_SSL:
|
if self.server_config.use_ssl:
|
||||||
connect_factory = Factory(wrapper=ssl.wrap_socket, ipv6=settings.IRCBOT_IPV6)
|
connect_factory = Factory(wrapper=ssl.wrap_socket, ipv6=self.server_config.use_ipv6)
|
||||||
else:
|
else:
|
||||||
connect_factory = Factory(ipv6=settings.IRCBOT_IPV6)
|
connect_factory = Factory(ipv6=self.server_config.use_ipv6)
|
||||||
|
|
||||||
self.connect(server[0], server[1], self._nickname, server[2], ircname=self._realname,
|
self.connect(self.server_config.hostname, self.server_config.port, self._nickname,
|
||||||
connect_factory=connect_factory)
|
self.server_config.password, ircname=self._realname, connect_factory=connect_factory)
|
||||||
except irc.client.ServerConnectionError:
|
except irc.client.ServerConnectionError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def _on_disconnect(self, c, e):
|
def _on_disconnect(self, c, e):
|
||||||
self.channels = IRCDict()
|
self.channels = IRCDict()
|
||||||
self.connection.execute_delayed(self.reconnection_interval,
|
self.reactor.scheduler.execute_after(self.reconnection_interval, self._connected_checker)
|
||||||
self._connected_checker)
|
|
||||||
|
|
||||||
def _on_join(self, c, e):
|
def _on_join(self, c, e):
|
||||||
ch = e.target
|
ch = e.target
|
||||||
|
@ -528,17 +529,18 @@ class IRCBot(irc.client.SimpleIRCClient):
|
||||||
log.debug("welcome: %s", what)
|
log.debug("welcome: %s", what)
|
||||||
|
|
||||||
# run automsg commands
|
# run automsg commands
|
||||||
for cmd in settings.IRCBOT_POST_CONNECT_COMMANDS:
|
if self.server_config.post_connect:
|
||||||
|
for cmd in self.server_config.post_connect.split('\n'):
|
||||||
# TODO NOTE: if the bot is sending something that changes the vhost
|
# TODO NOTE: if the bot is sending something that changes the vhost
|
||||||
# (like 'hostserv on') we don't pick it up
|
# (like 'hostserv on') we don't pick it up
|
||||||
self.connection.privmsg(cmd.split(' ')[0], ' '.join(cmd.split(' ')[1:]))
|
self.connection.privmsg(cmd.split(' ')[0], ' '.join(cmd.split(' ')[1:]))
|
||||||
|
|
||||||
# sleep before doing autojoins
|
# sleep before doing autojoins
|
||||||
time.sleep(settings.IRCBOT_SLEEP_BEFORE_AUTOJOIN_SECONDS)
|
time.sleep(self.server_config.delay_before_joins)
|
||||||
|
|
||||||
for chan in IrcChannel.objects.filter(autojoin=True):
|
for chan in IrcChannel.objects.filter(autojoin=True, server=connection.server_config):
|
||||||
log.info("autojoining %s", chan.name)
|
log.info("autojoining %s", chan.name)
|
||||||
self.connection.join(chan)
|
self.connection.join(chan.name)
|
||||||
|
|
||||||
for plugin in IrcPlugin.objects.filter(autoload=True):
|
for plugin in IrcPlugin.objects.filter(autoload=True):
|
||||||
log.info("autoloading %s", plugin.path)
|
log.info("autoloading %s", plugin.path)
|
||||||
|
@ -572,23 +574,21 @@ class IRCBot(irc.client.SimpleIRCClient):
|
||||||
self.connection.disconnect(msg)
|
self.connection.disconnect(msg)
|
||||||
|
|
||||||
def get_version(self):
|
def get_version(self):
|
||||||
"""Returns the bot version.
|
"""Return the bot version.
|
||||||
|
|
||||||
Used when answering a CTCP VERSION request.
|
Used when answering a CTCP VERSION request.
|
||||||
"""
|
"""
|
||||||
return "Python irc.bot ({version})".format(
|
return f"dr.botzo {__version__}"
|
||||||
version=irc.client.VERSION_STRING)
|
|
||||||
|
|
||||||
def jump_server(self, msg="Changing servers"):
|
def jump_server(self, msg="Changing servers"):
|
||||||
"""Connect to a new server, potentially disconnecting from the current one."""
|
"""Connect to a new server, potentially disconnecting from the current one."""
|
||||||
if self.connection.is_connected():
|
if self.connection.is_connected():
|
||||||
self.connection.disconnect(msg)
|
self.connection.disconnect(msg)
|
||||||
|
|
||||||
self.server_list.append(self.server_list.pop(0))
|
|
||||||
self._connect()
|
self._connect()
|
||||||
|
|
||||||
def on_ctcp(self, c, e):
|
def on_ctcp(self, c, e):
|
||||||
"""Default handler for ctcp events.
|
"""Handle for ctcp events.
|
||||||
|
|
||||||
Replies to VERSION and PING requests and relays DCC requests
|
Replies to VERSION and PING requests and relays DCC requests
|
||||||
to the on_dccchat method.
|
to the on_dccchat method.
|
||||||
|
@ -889,6 +889,14 @@ class IRCBot(irc.client.SimpleIRCClient):
|
||||||
log.warning("reply() called with no event and no explicit target, aborting")
|
log.warning("reply() called with no event and no explicit target, aborting")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# convert characters that don't make sense for Discord (like ^C^B)
|
||||||
|
if self.connection.server_config.replace_irc_control_with_markdown:
|
||||||
|
log.debug("old replystr: %s", replystr)
|
||||||
|
replystr = replystr.replace('\x02', '**')
|
||||||
|
replystr = replystr.replace('\x0F', '')
|
||||||
|
replystr = re.sub('\x03..', '', replystr)
|
||||||
|
log.debug("new replystr: %s", replystr)
|
||||||
|
|
||||||
log.debug("replypath: %s", replypath)
|
log.debug("replypath: %s", replypath)
|
||||||
|
|
||||||
if replystr is not None:
|
if replystr is not None:
|
||||||
|
@ -970,165 +978,3 @@ class IRCBot(irc.client.SimpleIRCClient):
|
||||||
del sys.modules[path]
|
del sys.modules[path]
|
||||||
|
|
||||||
self.die(msg="Shutting down...")
|
self.die(msg="Shutting down...")
|
||||||
|
|
||||||
|
|
||||||
class Channel(object):
|
|
||||||
"""A class for keeping information about an IRC channel."""
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
"""Initialize channel object."""
|
|
||||||
self.userdict = IRCDict()
|
|
||||||
self.operdict = IRCDict()
|
|
||||||
self.voiceddict = IRCDict()
|
|
||||||
self.ownerdict = IRCDict()
|
|
||||||
self.halfopdict = IRCDict()
|
|
||||||
self.modes = {}
|
|
||||||
|
|
||||||
def users(self):
|
|
||||||
"""Returns an unsorted list of the channel's users."""
|
|
||||||
return list(self.userdict.keys())
|
|
||||||
|
|
||||||
def opers(self):
|
|
||||||
"""Returns an unsorted list of the channel's operators."""
|
|
||||||
return list(self.operdict.keys())
|
|
||||||
|
|
||||||
def voiced(self):
|
|
||||||
"""Returns an unsorted list of the persons that have voice mode set in the channel."""
|
|
||||||
return list(self.voiceddict.keys())
|
|
||||||
|
|
||||||
def owners(self):
|
|
||||||
"""Returns an unsorted list of the channel's owners."""
|
|
||||||
return list(self.ownerdict.keys())
|
|
||||||
|
|
||||||
def halfops(self):
|
|
||||||
"""Returns an unsorted list of the channel's half-operators."""
|
|
||||||
return list(self.halfopdict.keys())
|
|
||||||
|
|
||||||
def has_user(self, nick):
|
|
||||||
"""Check whether the channel has a user."""
|
|
||||||
return nick in self.userdict
|
|
||||||
|
|
||||||
def is_oper(self, nick):
|
|
||||||
"""Check whether a user has operator status in the channel."""
|
|
||||||
return nick in self.operdict
|
|
||||||
|
|
||||||
def is_voiced(self, nick):
|
|
||||||
"""Check whether a user has voice mode set in the channel."""
|
|
||||||
return nick in self.voiceddict
|
|
||||||
|
|
||||||
def is_owner(self, nick):
|
|
||||||
"""Check whether a user has owner status in the channel."""
|
|
||||||
return nick in self.ownerdict
|
|
||||||
|
|
||||||
def is_halfop(self, nick):
|
|
||||||
"""Check whether a user has half-operator status in the channel."""
|
|
||||||
return nick in self.halfopdict
|
|
||||||
|
|
||||||
def add_user(self, nick):
|
|
||||||
"""Add user."""
|
|
||||||
self.userdict[nick] = 1
|
|
||||||
|
|
||||||
def remove_user(self, nick):
|
|
||||||
"""Remove user."""
|
|
||||||
for d in self.userdict, self.operdict, self.voiceddict:
|
|
||||||
if nick in d:
|
|
||||||
del d[nick]
|
|
||||||
|
|
||||||
def change_nick(self, before, after):
|
|
||||||
"""Handle a nick change."""
|
|
||||||
self.userdict[after] = self.userdict.pop(before)
|
|
||||||
if before in self.operdict:
|
|
||||||
self.operdict[after] = self.operdict.pop(before)
|
|
||||||
if before in self.voiceddict:
|
|
||||||
self.voiceddict[after] = self.voiceddict.pop(before)
|
|
||||||
|
|
||||||
def set_userdetails(self, nick, details):
|
|
||||||
"""Set user details."""
|
|
||||||
if nick in self.userdict:
|
|
||||||
self.userdict[nick] = details
|
|
||||||
|
|
||||||
def set_mode(self, mode, value=None):
|
|
||||||
"""Set mode on the channel.
|
|
||||||
|
|
||||||
Arguments:
|
|
||||||
|
|
||||||
mode -- The mode (a single-character string).
|
|
||||||
|
|
||||||
value -- Value
|
|
||||||
"""
|
|
||||||
if mode == "o":
|
|
||||||
self.operdict[value] = 1
|
|
||||||
elif mode == "v":
|
|
||||||
self.voiceddict[value] = 1
|
|
||||||
elif mode == "q":
|
|
||||||
self.ownerdict[value] = 1
|
|
||||||
elif mode == "h":
|
|
||||||
self.halfopdict[value] = 1
|
|
||||||
else:
|
|
||||||
self.modes[mode] = value
|
|
||||||
|
|
||||||
def clear_mode(self, mode, value=None):
|
|
||||||
"""Clear mode on the channel.
|
|
||||||
|
|
||||||
Arguments:
|
|
||||||
|
|
||||||
mode -- The mode (a single-character string).
|
|
||||||
|
|
||||||
value -- Value
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
if mode == "o":
|
|
||||||
del self.operdict[value]
|
|
||||||
elif mode == "v":
|
|
||||||
del self.voiceddict[value]
|
|
||||||
elif mode == "q":
|
|
||||||
del self.ownerdict[value]
|
|
||||||
elif mode == "h":
|
|
||||||
del self.halfopdict[value]
|
|
||||||
else:
|
|
||||||
del self.modes[mode]
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def has_mode(self, mode):
|
|
||||||
"""Return if mode is in channel modes."""
|
|
||||||
return mode in self.modes
|
|
||||||
|
|
||||||
def is_moderated(self):
|
|
||||||
"""Return if the channel is +m."""
|
|
||||||
return self.has_mode("m")
|
|
||||||
|
|
||||||
def is_secret(self):
|
|
||||||
"""Return if the channel is +s."""
|
|
||||||
return self.has_mode("s")
|
|
||||||
|
|
||||||
def is_protected(self):
|
|
||||||
"""Return if the channel is +p."""
|
|
||||||
return self.has_mode("p")
|
|
||||||
|
|
||||||
def has_topic_lock(self):
|
|
||||||
"""Return if the channel is +t."""
|
|
||||||
return self.has_mode("t")
|
|
||||||
|
|
||||||
def is_invite_only(self):
|
|
||||||
"""Return if the channel is +i."""
|
|
||||||
return self.has_mode("i")
|
|
||||||
|
|
||||||
def has_allow_external_messages(self):
|
|
||||||
"""Return if the channel is +n."""
|
|
||||||
return self.has_mode("n")
|
|
||||||
|
|
||||||
def has_limit(self):
|
|
||||||
"""Return if the channel is +l."""
|
|
||||||
return self.has_mode("l")
|
|
||||||
|
|
||||||
def limit(self):
|
|
||||||
"""Return the channel limit count."""
|
|
||||||
if self.has_limit():
|
|
||||||
return self.modes["l"]
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
def has_key(self):
|
|
||||||
"""Return if the channel is +k."""
|
|
||||||
return self.has_mode("k")
|
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
"""Forms for doing ircbot stuff."""
|
"""Forms for doing ircbot stuff."""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from django.forms import Form, CharField, Textarea
|
from django.forms import CharField, Form, IntegerField, Textarea
|
||||||
|
|
||||||
log = logging.getLogger('markov.forms')
|
log = logging.getLogger('ircbot.forms')
|
||||||
|
|
||||||
|
|
||||||
class PrivmsgForm(Form):
|
class PrivmsgForm(Form):
|
||||||
|
@ -12,3 +11,5 @@ class PrivmsgForm(Form):
|
||||||
|
|
||||||
target = CharField()
|
target = CharField()
|
||||||
message = CharField(widget=Textarea)
|
message = CharField(widget=Textarea)
|
||||||
|
xmlrpc_host = CharField()
|
||||||
|
xmlrpc_port = IntegerField()
|
||||||
|
|
|
@ -1,19 +1,17 @@
|
||||||
|
"""Provide some commands for basic IRC functionality."""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from ircbot.lib import Plugin, has_permission
|
from ircbot.lib import Plugin, has_permission
|
||||||
from ircbot.models import IrcChannel
|
from ircbot.models import IrcChannel
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger('ircbot.ircplugins.ircmgmt')
|
log = logging.getLogger('ircbot.ircplugins.ircmgmt')
|
||||||
|
|
||||||
|
|
||||||
class ChannelManagement(Plugin):
|
class ChannelManagement(Plugin):
|
||||||
|
|
||||||
"""Have IRC commands to do IRC things (join channels, quit, etc.)."""
|
"""Have IRC commands to do IRC things (join channels, quit, etc.)."""
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
"""Set up the handlers."""
|
"""Set up the handlers."""
|
||||||
|
|
||||||
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!join\s+([\S]+)',
|
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!join\s+([\S]+)',
|
||||||
self.handle_join, -20)
|
self.handle_join, -20)
|
||||||
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!part\s+([\S]+)',
|
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!part\s+([\S]+)',
|
||||||
|
@ -25,7 +23,6 @@ class ChannelManagement(Plugin):
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""Tear down handlers."""
|
"""Tear down handlers."""
|
||||||
|
|
||||||
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_join)
|
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_join)
|
||||||
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_part)
|
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_part)
|
||||||
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_quit)
|
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_quit)
|
||||||
|
@ -34,11 +31,10 @@ class ChannelManagement(Plugin):
|
||||||
|
|
||||||
def handle_join(self, connection, event, match):
|
def handle_join(self, connection, event, match):
|
||||||
"""Handle the join command."""
|
"""Handle the join command."""
|
||||||
|
|
||||||
if has_permission(event.source, 'ircbot.manage_current_channels'):
|
if has_permission(event.source, 'ircbot.manage_current_channels'):
|
||||||
channel = match.group(1)
|
channel = match.group(1)
|
||||||
# put it in the database if it isn't already
|
# put it in the database if it isn't already
|
||||||
chan_mod, c = IrcChannel.objects.get_or_create(name=channel)
|
chan_mod, c = IrcChannel.objects.get_or_create(name=channel, server=connection.server_config)
|
||||||
log.debug("joining channel %s", channel)
|
log.debug("joining channel %s", channel)
|
||||||
self.connection.join(channel)
|
self.connection.join(channel)
|
||||||
|
|
||||||
|
@ -46,11 +42,10 @@ class ChannelManagement(Plugin):
|
||||||
|
|
||||||
def handle_part(self, connection, event, match):
|
def handle_part(self, connection, event, match):
|
||||||
"""Handle the join command."""
|
"""Handle the join command."""
|
||||||
|
|
||||||
if has_permission(event.source, 'ircbot.manage_current_channels'):
|
if has_permission(event.source, 'ircbot.manage_current_channels'):
|
||||||
channel = match.group(1)
|
channel = match.group(1)
|
||||||
# put it in the database if it isn't already
|
# put it in the database if it isn't already
|
||||||
chan_mod, c = IrcChannel.objects.get_or_create(name=channel)
|
chan_mod, c = IrcChannel.objects.get_or_create(name=channel, server=connection.server_config)
|
||||||
log.debug("parting channel %s", channel)
|
log.debug("parting channel %s", channel)
|
||||||
self.connection.part(channel)
|
self.connection.part(channel)
|
||||||
|
|
||||||
|
@ -58,7 +53,6 @@ class ChannelManagement(Plugin):
|
||||||
|
|
||||||
def handle_quit(self, connection, event, match):
|
def handle_quit(self, connection, event, match):
|
||||||
"""Handle the join command."""
|
"""Handle the join command."""
|
||||||
|
|
||||||
if has_permission(event.source, 'ircbot.quit_bot'):
|
if has_permission(event.source, 'ircbot.quit_bot'):
|
||||||
self.bot.die(msg=match.group(1))
|
self.bot.die(msg=match.group(1))
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
"""Watch channel topics for changes and note them."""
|
"""Watch channel topics for changes and note them."""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
@ -7,24 +6,20 @@ from django.utils import timezone
|
||||||
from ircbot.lib import Plugin
|
from ircbot.lib import Plugin
|
||||||
from ircbot.models import IrcChannel
|
from ircbot.models import IrcChannel
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger('ircbot.ircplugins.topicmonitor')
|
log = logging.getLogger('ircbot.ircplugins.topicmonitor')
|
||||||
|
|
||||||
|
|
||||||
class TopicMonitor(Plugin):
|
class TopicMonitor(Plugin):
|
||||||
|
|
||||||
"""Have IRC commands to do IRC things (join channels, quit, etc.)."""
|
"""Have IRC commands to do IRC things (join channels, quit, etc.)."""
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
"""Set up the handlers."""
|
"""Set up the handlers."""
|
||||||
|
|
||||||
self.connection.reactor.add_global_handler('topic', handle_topic, -20)
|
self.connection.reactor.add_global_handler('topic', handle_topic, -20)
|
||||||
|
|
||||||
super(TopicMonitor, self).start()
|
super(TopicMonitor, self).start()
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""Tear down handlers."""
|
"""Tear down handlers."""
|
||||||
|
|
||||||
self.connection.reactor.remove_global_handler('topic', handle_topic)
|
self.connection.reactor.remove_global_handler('topic', handle_topic)
|
||||||
|
|
||||||
super(TopicMonitor, self).stop()
|
super(TopicMonitor, self).stop()
|
||||||
|
@ -32,13 +27,12 @@ class TopicMonitor(Plugin):
|
||||||
|
|
||||||
def handle_topic(connection, event):
|
def handle_topic(connection, event):
|
||||||
"""Store topic changes in the channel model."""
|
"""Store topic changes in the channel model."""
|
||||||
|
|
||||||
channel = event.target
|
channel = event.target
|
||||||
topic = event.arguments[0]
|
topic = event.arguments[0]
|
||||||
setter = event.source
|
setter = event.source
|
||||||
log.debug("topic change '%s' by %s in %s", topic, setter, channel)
|
log.debug("topic change '%s' by %s in %s", topic, setter, channel)
|
||||||
|
|
||||||
channel, c = IrcChannel.objects.get_or_create(name=channel)
|
channel, c = IrcChannel.objects.get_or_create(name=channel, server=connection.server_config)
|
||||||
channel.topic_msg = topic
|
channel.topic_msg = topic
|
||||||
channel.topic_time = timezone.now()
|
channel.topic_time = timezone.now()
|
||||||
channel.topic_by = setter
|
channel.topic_by = setter
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
"""Start the IRC bot via Django management command."""
|
"""Start the IRC bot via Django management command."""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import signal
|
import signal
|
||||||
|
|
||||||
|
@ -7,7 +6,6 @@ from django.core.management import BaseCommand
|
||||||
|
|
||||||
from ircbot.bot import IRCBot
|
from ircbot.bot import IRCBot
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger('ircbot')
|
log = logging.getLogger('ircbot')
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,9 +17,13 @@ class Command(BaseCommand):
|
||||||
|
|
||||||
help = "Start the IRC bot"
|
help = "Start the IRC bot"
|
||||||
|
|
||||||
|
def add_arguments(self, parser):
|
||||||
|
"""Add arguments to the bot startup."""
|
||||||
|
parser.add_argument('server_name')
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
"""Start the IRC bot and spin forever."""
|
"""Start the IRC bot and spin forever."""
|
||||||
|
self.stdout.write(self.style.NOTICE(f"Starting up {options['server_name']} bot"))
|
||||||
irc = IRCBot()
|
irc = IRCBot(options['server_name'])
|
||||||
signal.signal(signal.SIGINT, irc.sigint_handler)
|
signal.signal(signal.SIGINT, irc.sigint_handler)
|
||||||
irc.start()
|
irc.start()
|
||||||
|
|
|
@ -15,7 +15,7 @@ class Migration(migrations.Migration):
|
||||||
migrations.AddField(
|
migrations.AddField(
|
||||||
model_name='botadmin',
|
model_name='botadmin',
|
||||||
name='user',
|
name='user',
|
||||||
field=models.ForeignKey(default=1, to=settings.AUTH_USER_MODEL),
|
field=models.ForeignKey(default=1, to=settings.AUTH_USER_MODEL, on_delete=models.CASCADE),
|
||||||
preserve_default=False,
|
preserve_default=False,
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import unicode_literals
|
|
||||||
|
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
# Generated by Django 3.1.2 on 2021-04-25 14:05
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('ircbot', '0014_auto_20160116_1955'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='IrcServer',
|
||||||
|
fields=[
|
||||||
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('name', models.CharField(max_length=200, unique=True)),
|
||||||
|
('hostname', models.CharField(max_length=200)),
|
||||||
|
('port', models.PositiveSmallIntegerField(default=6667)),
|
||||||
|
('password', models.CharField(blank=True, default=None, max_length=200, null=True)),
|
||||||
|
('nickname', models.CharField(max_length=32)),
|
||||||
|
('realname', models.CharField(blank=True, default='', max_length=32)),
|
||||||
|
('additional_addressed_nicks', models.TextField(blank=True, default='', help_text='For e.g. BitlBee alternative nicks')),
|
||||||
|
('use_ssl', models.BooleanField(default=False)),
|
||||||
|
('use_ipv6', models.BooleanField(default=False)),
|
||||||
|
('post_connect', models.TextField(blank=True, default='')),
|
||||||
|
('delay_before_joins', models.PositiveSmallIntegerField(default=0)),
|
||||||
|
('xmlrpc_host', models.CharField(default='localhost', max_length=200)),
|
||||||
|
('xmlrpc_port', models.PositiveSmallIntegerField(default=13132)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
|
@ -0,0 +1,26 @@
|
||||||
|
# Generated by Django 3.1.2 on 2021-04-25 04:11
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
def create_placeholder_server(apps, schema_editor):
|
||||||
|
"""Create the first server entry, to be configured by the admin."""
|
||||||
|
IrcServer = apps.get_model('ircbot', 'IrcServer')
|
||||||
|
IrcServer.objects.create(name='default', hostname='irc.example.org', port=6667)
|
||||||
|
|
||||||
|
|
||||||
|
def delete_placeholder_server(apps, schema_editor):
|
||||||
|
"""Remove the default server."""
|
||||||
|
IrcServer = apps.get_model('ircbot', 'IrcServer')
|
||||||
|
IrcServer.objects.filter(name='default').delete()
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('ircbot', '0015_ircserver'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RunPython(create_placeholder_server, delete_placeholder_server),
|
||||||
|
]
|
|
@ -0,0 +1,30 @@
|
||||||
|
# Generated by Django 3.1.2 on 2021-04-25 16:11
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
import ircbot.models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('ircbot', '0016_placeholder_ircserver'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='ircchannel',
|
||||||
|
name='server',
|
||||||
|
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='ircbot.ircserver'),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='ircchannel',
|
||||||
|
name='name',
|
||||||
|
field=ircbot.models.LowerCaseCharField(max_length=200),
|
||||||
|
),
|
||||||
|
migrations.AddConstraint(
|
||||||
|
model_name='ircchannel',
|
||||||
|
constraint=models.UniqueConstraint(fields=('name', 'server'), name='unique_server_channel'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 3.1.2 on 2021-04-25 17:05
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('ircbot', '0017_ircchannel_server'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='ircserver',
|
||||||
|
name='replace_irc_control_with_markdown',
|
||||||
|
field=models.BooleanField(default=False),
|
||||||
|
),
|
||||||
|
]
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 3.2.18 on 2023-02-16 22:38
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('ircbot', '0018_ircserver_replace_irc_control_with_markdown'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='ircchannel',
|
||||||
|
name='discord_bridge',
|
||||||
|
field=models.CharField(blank=True, default='', max_length=32),
|
||||||
|
),
|
||||||
|
]
|
|
@ -1,5 +1,4 @@
|
||||||
"""Track basic IRC settings and similar."""
|
"""Track basic IRC settings and similar."""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
@ -7,12 +6,14 @@ from django.conf import settings
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger('ircbot.models')
|
log = logging.getLogger('ircbot.models')
|
||||||
|
|
||||||
|
|
||||||
class LowerCaseCharField(models.CharField):
|
class LowerCaseCharField(models.CharField):
|
||||||
|
"""Provide a case-insensitive, forced-lower CharField."""
|
||||||
|
|
||||||
def get_prep_value(self, value):
|
def get_prep_value(self, value):
|
||||||
|
"""Manipulate the field value to make it lowercase."""
|
||||||
value = super(LowerCaseCharField, self).get_prep_value(value)
|
value = super(LowerCaseCharField, self).get_prep_value(value)
|
||||||
if value is not None:
|
if value is not None:
|
||||||
value = value.lower()
|
value = value.lower()
|
||||||
|
@ -20,21 +21,22 @@ class LowerCaseCharField(models.CharField):
|
||||||
|
|
||||||
|
|
||||||
class Alias(models.Model):
|
class Alias(models.Model):
|
||||||
|
|
||||||
"""Allow for aliasing of arbitrary regexes to normal supported commands."""
|
"""Allow for aliasing of arbitrary regexes to normal supported commands."""
|
||||||
|
|
||||||
pattern = models.CharField(max_length=200, unique=True)
|
pattern = models.CharField(max_length=200, unique=True)
|
||||||
replacement = models.CharField(max_length=200)
|
replacement = models.CharField(max_length=200)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
"""Settings for the model."""
|
||||||
|
|
||||||
verbose_name_plural = "aliases"
|
verbose_name_plural = "aliases"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""String representation."""
|
"""Provide string representation."""
|
||||||
|
|
||||||
return "{0:s} -> {1:s}".format(self.pattern, self.replacement)
|
return "{0:s} -> {1:s}".format(self.pattern, self.replacement)
|
||||||
|
|
||||||
def replace(self, what):
|
def replace(self, what):
|
||||||
|
"""Match the regex and replace with the command."""
|
||||||
command = None
|
command = None
|
||||||
if re.search(self.pattern, what, flags=re.IGNORECASE):
|
if re.search(self.pattern, what, flags=re.IGNORECASE):
|
||||||
command = re.sub(self.pattern, self.replacement, what, flags=re.IGNORECASE)
|
command = re.sub(self.pattern, self.replacement, what, flags=re.IGNORECASE)
|
||||||
|
@ -43,28 +45,57 @@ class Alias(models.Model):
|
||||||
|
|
||||||
|
|
||||||
class BotUser(models.Model):
|
class BotUser(models.Model):
|
||||||
|
|
||||||
"""Configure bot users, which can do things through the bot and standard Django auth."""
|
"""Configure bot users, which can do things through the bot and standard Django auth."""
|
||||||
|
|
||||||
nickmask = models.CharField(max_length=200, unique=True)
|
nickmask = models.CharField(max_length=200, unique=True)
|
||||||
user = models.ForeignKey(settings.AUTH_USER_MODEL)
|
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
"""Settings for the model."""
|
||||||
|
|
||||||
permissions = (
|
permissions = (
|
||||||
('quit_bot', "Can tell the bot to quit via IRC"),
|
('quit_bot', "Can tell the bot to quit via IRC"),
|
||||||
)
|
)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""String representation."""
|
"""Provide string representation."""
|
||||||
|
|
||||||
return "{0:s} (Django user {1:s})".format(self.nickmask, self.user.username)
|
return "{0:s} (Django user {1:s})".format(self.nickmask, self.user.username)
|
||||||
|
|
||||||
|
|
||||||
class IrcChannel(models.Model):
|
class IrcServer(models.Model):
|
||||||
|
"""Contain server information in an object, and help contextualize channels."""
|
||||||
|
|
||||||
|
name = models.CharField(max_length=200, unique=True)
|
||||||
|
hostname = models.CharField(max_length=200)
|
||||||
|
port = models.PositiveSmallIntegerField(default=6667)
|
||||||
|
password = models.CharField(max_length=200, default=None, null=True, blank=True)
|
||||||
|
|
||||||
|
nickname = models.CharField(max_length=32)
|
||||||
|
realname = models.CharField(max_length=32, default='', blank=True)
|
||||||
|
additional_addressed_nicks = models.TextField(default='', blank=True,
|
||||||
|
help_text="For e.g. BitlBee alternative nicks")
|
||||||
|
|
||||||
|
use_ssl = models.BooleanField(default=False)
|
||||||
|
use_ipv6 = models.BooleanField(default=False)
|
||||||
|
|
||||||
|
post_connect = models.TextField(default='', blank=True)
|
||||||
|
delay_before_joins = models.PositiveSmallIntegerField(default=0)
|
||||||
|
|
||||||
|
xmlrpc_host = models.CharField(max_length=200, default='localhost')
|
||||||
|
xmlrpc_port = models.PositiveSmallIntegerField(default=13132)
|
||||||
|
|
||||||
|
replace_irc_control_with_markdown = models.BooleanField(default=False)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
"""Provide string summary of the server."""
|
||||||
|
return f"{self.name} ({self.hostname}/{self.port})"
|
||||||
|
|
||||||
|
|
||||||
|
class IrcChannel(models.Model):
|
||||||
"""Track channel settings."""
|
"""Track channel settings."""
|
||||||
|
|
||||||
name = LowerCaseCharField(max_length=200, unique=True)
|
name = LowerCaseCharField(max_length=200)
|
||||||
|
server = models.ForeignKey('IrcServer', on_delete=models.CASCADE)
|
||||||
autojoin = models.BooleanField(default=False)
|
autojoin = models.BooleanField(default=False)
|
||||||
|
|
||||||
topic_msg = models.TextField(default='', blank=True)
|
topic_msg = models.TextField(default='', blank=True)
|
||||||
|
@ -73,31 +104,37 @@ class IrcChannel(models.Model):
|
||||||
|
|
||||||
markov_learn_from_channel = models.BooleanField(default=True)
|
markov_learn_from_channel = models.BooleanField(default=True)
|
||||||
|
|
||||||
|
discord_bridge = models.CharField(default='', max_length=32, blank=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
"""Settings for the model."""
|
||||||
|
|
||||||
|
constraints = (
|
||||||
|
models.UniqueConstraint(fields=['name', 'server'], name='unique_server_channel'),
|
||||||
|
)
|
||||||
permissions = (
|
permissions = (
|
||||||
('manage_current_channels', "Can join/part channels via IRC"),
|
('manage_current_channels', "Can join/part channels via IRC"),
|
||||||
)
|
)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""String representation."""
|
"""Provide string representation."""
|
||||||
|
return "{0:s} on {1:s}".format(self.name, self.server.name)
|
||||||
return "{0:s}".format(self.name)
|
|
||||||
|
|
||||||
|
|
||||||
class IrcPlugin(models.Model):
|
class IrcPlugin(models.Model):
|
||||||
|
|
||||||
"""Represent an IRC plugin and its loading settings."""
|
"""Represent an IRC plugin and its loading settings."""
|
||||||
|
|
||||||
path = models.CharField(max_length=200, unique=True)
|
path = models.CharField(max_length=200, unique=True)
|
||||||
autoload = models.BooleanField(default=False)
|
autoload = models.BooleanField(default=False)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
"""Settings for the model."""
|
||||||
|
|
||||||
ordering = ['path']
|
ordering = ['path']
|
||||||
permissions = (
|
permissions = (
|
||||||
('manage_loaded_plugins', "Can load/unload plugins via IRC"),
|
('manage_loaded_plugins', "Can load/unload plugins via IRC"),
|
||||||
)
|
)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""String representation."""
|
"""Provide string representation."""
|
||||||
|
|
||||||
return "{0:s}".format(self.path)
|
return "{0:s}".format(self.path)
|
||||||
|
|
|
@ -6,6 +6,7 @@ import re
|
||||||
import irc.client
|
import irc.client
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.db.models import Count, Sum
|
||||||
|
|
||||||
from ircbot.lib import Plugin
|
from ircbot.lib import Plugin
|
||||||
from karma.models import KarmaKey, KarmaLogEntry
|
from karma.models import KarmaKey, KarmaLogEntry
|
||||||
|
@ -23,6 +24,9 @@ class Karma(Plugin):
|
||||||
self.connection.add_global_handler('pubmsg', self.handle_chatter, -20)
|
self.connection.add_global_handler('pubmsg', self.handle_chatter, -20)
|
||||||
self.connection.add_global_handler('privmsg', self.handle_chatter, -20)
|
self.connection.add_global_handler('privmsg', self.handle_chatter, -20)
|
||||||
|
|
||||||
|
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'],
|
||||||
|
(r'^!karma\s+keyreport\s+(.*)'),
|
||||||
|
self.handle_keyreport, -20)
|
||||||
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'],
|
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'],
|
||||||
r'^!karma\s+rank\s+(.*)$',
|
r'^!karma\s+rank\s+(.*)$',
|
||||||
self.handle_rank, -20)
|
self.handle_rank, -20)
|
||||||
|
@ -42,6 +46,7 @@ class Karma(Plugin):
|
||||||
self.connection.remove_global_handler('pubmsg', self.handle_chatter)
|
self.connection.remove_global_handler('pubmsg', self.handle_chatter)
|
||||||
self.connection.remove_global_handler('privmsg', self.handle_chatter)
|
self.connection.remove_global_handler('privmsg', self.handle_chatter)
|
||||||
|
|
||||||
|
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_keyreport)
|
||||||
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_rank)
|
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_rank)
|
||||||
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_report)
|
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_report)
|
||||||
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_stats)
|
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_stats)
|
||||||
|
@ -102,6 +107,19 @@ class Karma(Plugin):
|
||||||
except KarmaKey.DoesNotExist:
|
except KarmaKey.DoesNotExist:
|
||||||
return self.bot.reply(event, "i have not seen any karma for {0:s}".format(match.group(1)))
|
return self.bot.reply(event, "i have not seen any karma for {0:s}".format(match.group(1)))
|
||||||
|
|
||||||
|
def handle_keyreport(self, connection, event, match):
|
||||||
|
"""Provide report on a karma key."""
|
||||||
|
key = match.group(1).lower().rstrip()
|
||||||
|
try:
|
||||||
|
karma_key = KarmaKey.objects.get(key=key)
|
||||||
|
karmaers = KarmaLogEntry.objects.filter(key=karma_key)
|
||||||
|
karmaers = karmaers.values('nickmask').annotate(Sum('delta')).annotate(Count('delta')).order_by('-delta__count')
|
||||||
|
karmaers_list = [f"{irc.client.NickMask(x['nickmask']).nick} ({x['delta__count']}, {'+' if x['delta__sum'] >= 0 else ''}{x['delta__sum']})" for x in karmaers]
|
||||||
|
karmaers_list_str = ", ".join(karmaers_list[:10])
|
||||||
|
return self.bot.reply(event, f"most opinionated on {key}: {karmaers_list_str}")
|
||||||
|
except KarmaKey.DoesNotExist:
|
||||||
|
return self.bot.reply(event, "i have not seen any karma for {0:s}".format(match.group(1)))
|
||||||
|
|
||||||
def handle_report(self, connection, event, match):
|
def handle_report(self, connection, event, match):
|
||||||
"""Provide some karma reports."""
|
"""Provide some karma reports."""
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ class Migration(migrations.Migration):
|
||||||
('delta', models.SmallIntegerField()),
|
('delta', models.SmallIntegerField()),
|
||||||
('nickmask', models.CharField(default='', max_length=200, blank=True)),
|
('nickmask', models.CharField(default='', max_length=200, blank=True)),
|
||||||
('created', models.DateTimeField(auto_now_add=True)),
|
('created', models.DateTimeField(auto_now_add=True)),
|
||||||
('key', models.ForeignKey(to='karma.KarmaKey')),
|
('key', models.ForeignKey(to='karma.KarmaKey', on_delete=models.CASCADE)),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
|
@ -115,7 +115,7 @@ class KarmaLogEntryManager(models.Manager):
|
||||||
class KarmaLogEntry(models.Model):
|
class KarmaLogEntry(models.Model):
|
||||||
"""Track each karma increment/decrement."""
|
"""Track each karma increment/decrement."""
|
||||||
|
|
||||||
key = models.ForeignKey('KarmaKey')
|
key = models.ForeignKey('KarmaKey', on_delete=models.CASCADE)
|
||||||
delta = models.SmallIntegerField()
|
delta = models.SmallIntegerField()
|
||||||
nickmask = models.CharField(max_length=200, default='', blank=True)
|
nickmask = models.CharField(max_length=200, default='', blank=True)
|
||||||
created = models.DateTimeField(auto_now_add=True)
|
created = models.DateTimeField(auto_now_add=True)
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
"""URL patterns for the karma views."""
|
"""URL patterns for the karma views."""
|
||||||
|
from django.conf.urls import include
|
||||||
from django.conf.urls import url, include
|
from django.urls import path, re_path
|
||||||
from rest_framework.routers import DefaultRouter
|
from rest_framework.routers import DefaultRouter
|
||||||
|
|
||||||
from karma.views import key_detail, index, KarmaKeyViewSet
|
from karma.views import KarmaKeyViewSet, index, key_detail
|
||||||
|
|
||||||
router = DefaultRouter()
|
router = DefaultRouter()
|
||||||
router.register(r'keys', KarmaKeyViewSet)
|
router.register(r'keys', KarmaKeyViewSet)
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^$', index, name='karma_index'),
|
path('', index, name='karma_index'),
|
||||||
url(r'^key/(?P<karma_key>.+)/', key_detail, name='karma_key_detail'),
|
re_path(r'^key/(?P<karma_key>.+)/', key_detail, name='karma_key_detail'),
|
||||||
|
|
||||||
url(r'^api/', include(router.urls)),
|
path('api/', include(router.urls)),
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,27 +1,32 @@
|
||||||
"""Present karma data."""
|
"""Present karma data."""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
from django.core.exceptions import PermissionDenied
|
||||||
from django.shortcuts import get_object_or_404, render
|
from django.shortcuts import get_object_or_404, render
|
||||||
from rest_framework import viewsets
|
from rest_framework import viewsets
|
||||||
|
|
||||||
from karma.models import KarmaKey
|
from karma.models import KarmaKey
|
||||||
from karma.serializers import KarmaKeySerializer
|
from karma.serializers import KarmaKeySerializer
|
||||||
|
|
||||||
log = logging.getLogger('karma.views')
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def index(request):
|
def index(request):
|
||||||
"""Display all karma keys."""
|
"""Display all karma keys."""
|
||||||
entries = KarmaKey.objects.all().order_by('key')
|
if 'karma' not in settings.WEB_ENABLED_APPS:
|
||||||
|
raise PermissionDenied()
|
||||||
|
|
||||||
|
entries = KarmaKey.objects.all().order_by('key')
|
||||||
return render(request, 'karma/index.html', {'entries': entries})
|
return render(request, 'karma/index.html', {'entries': entries})
|
||||||
|
|
||||||
|
|
||||||
def key_detail(request, karma_key):
|
def key_detail(request, karma_key):
|
||||||
"""Display the requested karma key."""
|
"""Display the requested karma key."""
|
||||||
entry = get_object_or_404(KarmaKey, key=karma_key.lower())
|
if 'karma' not in settings.WEB_ENABLED_APPS:
|
||||||
|
raise PermissionDenied()
|
||||||
|
|
||||||
|
entry = get_object_or_404(KarmaKey, key=karma_key.lower())
|
||||||
return render(request, 'karma/karma_key.html', {'entry': entry, 'entry_history': entry.history(mode='date')})
|
return render(request, 'karma/karma_key.html', {'entry': entry, 'entry_history': entry.history(mode='date')})
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,22 @@
|
||||||
|
"""IRC support for Markov chain learning and text generation."""
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
|
|
||||||
import irc.client
|
import irc.client
|
||||||
|
|
||||||
|
import markov.lib as markovlib
|
||||||
from ircbot.lib import Plugin, reply_destination_for_event
|
from ircbot.lib import Plugin, reply_destination_for_event
|
||||||
from ircbot.models import IrcChannel
|
from ircbot.models import IrcChannel
|
||||||
import markov.lib as markovlib
|
from markov.models import MarkovContext, MarkovTarget
|
||||||
|
|
||||||
log = logging.getLogger('markov.ircplugin')
|
log = logging.getLogger('markov.ircplugin')
|
||||||
|
|
||||||
|
|
||||||
class Markov(Plugin):
|
class Markov(Plugin):
|
||||||
|
|
||||||
"""Build Markov chains and reply with them."""
|
"""Build Markov chains and reply with them."""
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
"""Set up the handlers."""
|
"""Set up the handlers."""
|
||||||
|
|
||||||
self.connection.add_global_handler('pubmsg', self.handle_chatter, -20)
|
self.connection.add_global_handler('pubmsg', self.handle_chatter, -20)
|
||||||
self.connection.add_global_handler('privmsg', self.handle_chatter, -20)
|
self.connection.add_global_handler('privmsg', self.handle_chatter, -20)
|
||||||
|
|
||||||
|
@ -28,7 +28,6 @@ class Markov(Plugin):
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""Tear down handlers."""
|
"""Tear down handlers."""
|
||||||
|
|
||||||
self.connection.remove_global_handler('pubmsg', self.handle_chatter)
|
self.connection.remove_global_handler('pubmsg', self.handle_chatter)
|
||||||
self.connection.remove_global_handler('privmsg', self.handle_chatter)
|
self.connection.remove_global_handler('privmsg', self.handle_chatter)
|
||||||
|
|
||||||
|
@ -38,12 +37,11 @@ class Markov(Plugin):
|
||||||
|
|
||||||
def handle_reply(self, connection, event, match):
|
def handle_reply(self, connection, event, match):
|
||||||
"""Generate a reply to one line, without learning it."""
|
"""Generate a reply to one line, without learning it."""
|
||||||
|
|
||||||
target = reply_destination_for_event(event)
|
target = reply_destination_for_event(event)
|
||||||
|
|
||||||
min_size = 15
|
min_size = 15
|
||||||
max_size = 30
|
max_size = 30
|
||||||
context = markovlib.get_or_create_target_context(target)
|
context = self.get_or_create_target_context(target)
|
||||||
|
|
||||||
if match.group(2):
|
if match.group(2):
|
||||||
min_size = int(match.group(2))
|
min_size = int(match.group(2))
|
||||||
|
@ -62,41 +60,53 @@ class Markov(Plugin):
|
||||||
|
|
||||||
def handle_chatter(self, connection, event):
|
def handle_chatter(self, connection, event):
|
||||||
"""Learn from IRC chatter."""
|
"""Learn from IRC chatter."""
|
||||||
|
|
||||||
what = event.arguments[0]
|
what = event.arguments[0]
|
||||||
my_nick = connection.get_nickname()
|
who = irc.client.NickMask(event.source).nick
|
||||||
trimmed_what = re.sub(r'^{0:s}[:,]\s+'.format(my_nick), '', what)
|
|
||||||
nick = irc.client.NickMask(event.source).nick
|
|
||||||
target = reply_destination_for_event(event)
|
target = reply_destination_for_event(event)
|
||||||
|
|
||||||
|
log.debug("what: '%s', who: '%s', target: '%s'", what, who, target)
|
||||||
# check to see whether or not we should learn from this channel
|
# check to see whether or not we should learn from this channel
|
||||||
channel = None
|
channel = None
|
||||||
if irc.client.is_channel(target):
|
if irc.client.is_channel(target):
|
||||||
channel, c = IrcChannel.objects.get_or_create(name=target)
|
channel, c = IrcChannel.objects.get_or_create(name=target, server=connection.server_config)
|
||||||
|
|
||||||
if channel and not channel.markov_learn_from_channel:
|
if channel and not channel.markov_learn_from_channel:
|
||||||
log.debug("not learning from %s as i've been told to ignore it", channel)
|
log.debug("not learning from %s as i've been told to ignore it", channel)
|
||||||
else:
|
else:
|
||||||
# learn the line
|
# learn the line
|
||||||
|
learning_what = what
|
||||||
|
|
||||||
|
# don't learn the speaker's nick if this came over a bridge
|
||||||
|
if channel and who == channel.discord_bridge:
|
||||||
|
learning_what = ' '.join(learning_what.split(' ')[1:])
|
||||||
|
|
||||||
|
# remove our own nick and aliases from what we learn
|
||||||
|
if connection.server_config.additional_addressed_nicks:
|
||||||
|
all_nicks = '|'.join(connection.server_config.additional_addressed_nicks.split('\n') +
|
||||||
|
[connection.get_nickname()])
|
||||||
|
else:
|
||||||
|
all_nicks = connection.get_nickname()
|
||||||
|
learning_what = re.sub(r'^(({nicks})[:,]|@({nicks})[:,]?)\s+'.format(nicks=all_nicks), '', learning_what)
|
||||||
|
|
||||||
recursing = getattr(event, 'recursing', False)
|
recursing = getattr(event, 'recursing', False)
|
||||||
if not recursing:
|
if not recursing:
|
||||||
log.debug("learning %s", trimmed_what)
|
log.debug("learning %s", learning_what)
|
||||||
context = markovlib.get_or_create_target_context(target)
|
context = self.get_or_create_target_context(target)
|
||||||
markovlib.learn_line(trimmed_what, context)
|
markovlib.learn_line(learning_what, context)
|
||||||
|
|
||||||
log.debug("searching '%s' for '%s'", what, my_nick)
|
log.debug("searching '%s' for '%s'", what, all_nicks)
|
||||||
if re.search(my_nick, what, re.IGNORECASE) is not None:
|
if re.search(all_nicks, what, re.IGNORECASE) is not None:
|
||||||
context = markovlib.get_or_create_target_context(target)
|
context = self.get_or_create_target_context(target)
|
||||||
|
|
||||||
addressed_pattern = r'^{0:s}[:,]\s+(.*)'.format(my_nick)
|
addressed_pattern = r'^(({nicks})[:,]|@({nicks})[:,]?)\s+(?P<addressed_msg>.*)'.format(nicks=all_nicks)
|
||||||
addressed_re = re.compile(addressed_pattern)
|
match = re.match(addressed_pattern, what, re.IGNORECASE)
|
||||||
if addressed_re.match(what):
|
if match:
|
||||||
# i was addressed directly, so respond, addressing
|
# i was addressed directly, so respond, addressing
|
||||||
# the speaker
|
# the speaker
|
||||||
topics = [x for x in addressed_re.match(what).group(1).split(' ') if len(x) >= 3]
|
topics = [x for x in match.group('addressed_msg').split(' ') if len(x) >= 3]
|
||||||
|
|
||||||
return self.bot.reply(event, "{0:s}: {1:s}"
|
return self.bot.reply(event, "{0:s}: {1:s}"
|
||||||
"".format(nick, " ".join(markovlib.generate_line(context, topics=topics))))
|
"".format(who, " ".join(markovlib.generate_line(context, topics=topics))))
|
||||||
else:
|
else:
|
||||||
# i wasn't addressed directly, so just respond
|
# i wasn't addressed directly, so just respond
|
||||||
topics = [x for x in what.split(' ') if len(x) >= 3]
|
topics = [x for x in what.split(' ') if len(x) >= 3]
|
||||||
|
@ -104,5 +114,31 @@ class Markov(Plugin):
|
||||||
return self.bot.reply(event, "{0:s}"
|
return self.bot.reply(event, "{0:s}"
|
||||||
"".format(" ".join(markovlib.generate_line(context, topics=topics))))
|
"".format(" ".join(markovlib.generate_line(context, topics=topics))))
|
||||||
|
|
||||||
|
def get_or_create_target_context(self, target_name):
|
||||||
|
"""Return the context for a provided nick/channel, creating missing ones."""
|
||||||
|
target_name = target_name.lower()
|
||||||
|
|
||||||
|
# find the stuff, or create it
|
||||||
|
channel, c = IrcChannel.objects.get_or_create(name=target_name, server=self.connection.server_config)
|
||||||
|
try:
|
||||||
|
target = MarkovTarget.objects.get(channel=channel)
|
||||||
|
except MarkovTarget.DoesNotExist:
|
||||||
|
# we need to create a context and a target, and we have to make the context first
|
||||||
|
# make a context --- lacking a good idea, just create one with this target name until configured otherwise
|
||||||
|
context, c = MarkovContext.objects.get_or_create(name=target_name)
|
||||||
|
target, c = MarkovTarget.objects.get_or_create(name=target_name, context=context, channel=channel)
|
||||||
|
|
||||||
|
return target.context
|
||||||
|
|
||||||
|
try:
|
||||||
|
return target.context
|
||||||
|
except MarkovContext.DoesNotExist:
|
||||||
|
# make a context --- lacking a good idea, just create one with this target name until configured otherwise
|
||||||
|
context, c = MarkovContext.objects.get_or_create(name=target_name)
|
||||||
|
target.context = context
|
||||||
|
target.save()
|
||||||
|
|
||||||
|
return target.context
|
||||||
|
|
||||||
|
|
||||||
plugin = Markov
|
plugin = Markov
|
||||||
|
|
|
@ -1,17 +1,16 @@
|
||||||
|
"""Provide methods for manipulating markov chain processing."""
|
||||||
import logging
|
import logging
|
||||||
import random
|
import random
|
||||||
|
|
||||||
from django.db.models import Sum
|
from django.db.models import Sum
|
||||||
|
|
||||||
from markov.models import MarkovContext, MarkovState, MarkovTarget
|
from markov.models import MarkovState
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
log = logging.getLogger('markov.lib')
|
|
||||||
|
|
||||||
|
|
||||||
def generate_line(context, topics=None, min_words=15, max_words=30, sentence_bias=2, max_tries=5):
|
def generate_line(context, topics=None, min_words=15, max_words=30, sentence_bias=2, max_tries=5):
|
||||||
"""String multiple sentences together into a coherent sentence."""
|
"""Combine multiple sentences together into a coherent sentence."""
|
||||||
|
|
||||||
tries = 0
|
tries = 0
|
||||||
line = []
|
line = []
|
||||||
min_words_per_sentence = min_words / sentence_bias
|
min_words_per_sentence = min_words / sentence_bias
|
||||||
|
@ -23,7 +22,7 @@ def generate_line(context, topics=None, min_words=15, max_words=30, sentence_bia
|
||||||
else:
|
else:
|
||||||
if len(line) > 0:
|
if len(line) > 0:
|
||||||
if line[-1][-1] not in [',', '.', '!', '?', ':']:
|
if line[-1][-1] not in [',', '.', '!', '?', ':']:
|
||||||
line[-1] += random.choice(['?', '.', '!'])
|
line[-1] += random.SystemRandom().choice(['?', '.', '!'])
|
||||||
|
|
||||||
tries += 1
|
tries += 1
|
||||||
|
|
||||||
|
@ -33,7 +32,6 @@ def generate_line(context, topics=None, min_words=15, max_words=30, sentence_bia
|
||||||
|
|
||||||
def generate_longish_sentence(context, topics=None, min_words=15, max_words=30, max_tries=100):
|
def generate_longish_sentence(context, topics=None, min_words=15, max_words=30, max_tries=100):
|
||||||
"""Generate a Markov chain, but throw away the short ones unless we get desperate."""
|
"""Generate a Markov chain, but throw away the short ones unless we get desperate."""
|
||||||
|
|
||||||
sent = ""
|
sent = ""
|
||||||
tries = 0
|
tries = 0
|
||||||
while tries < max_tries:
|
while tries < max_tries:
|
||||||
|
@ -52,20 +50,19 @@ def generate_longish_sentence(context, topics=None, min_words=15, max_words=30,
|
||||||
|
|
||||||
def generate_sentence(context, topics=None, min_words=15, max_words=30):
|
def generate_sentence(context, topics=None, min_words=15, max_words=30):
|
||||||
"""Generate a Markov chain."""
|
"""Generate a Markov chain."""
|
||||||
|
|
||||||
words = []
|
words = []
|
||||||
# if we have topics, try to work from it and work backwards
|
# if we have topics, try to work from it and work backwards
|
||||||
if topics:
|
if topics:
|
||||||
topic_word = random.choice(topics)
|
topic_word = random.SystemRandom().choice(topics)
|
||||||
topics.remove(topic_word)
|
topics.remove(topic_word)
|
||||||
log.debug("looking for topic '{0:s}'".format(topic_word))
|
log.debug("looking for topic '%s'", topic_word)
|
||||||
new_states = MarkovState.objects.filter(context=context, v=topic_word)
|
new_states = MarkovState.objects.filter(context=context, v=topic_word)
|
||||||
|
|
||||||
if len(new_states) > 0:
|
if len(new_states) > 0:
|
||||||
log.debug("found '{0:s}', starting backwards".format(topic_word))
|
log.debug("found '%s', starting backwards", topic_word)
|
||||||
words.insert(0, topic_word)
|
words.insert(0, topic_word)
|
||||||
while len(words) <= max_words and words[0] != MarkovState._start2:
|
while len(words) <= max_words and words[0] != MarkovState._start2:
|
||||||
log.debug("looking backwards for '{0:s}'".format(words[0]))
|
log.debug("looking backwards for '%s'", words[0])
|
||||||
new_states = MarkovState.objects.filter(context=context, v=words[0])
|
new_states = MarkovState.objects.filter(context=context, v=words[0])
|
||||||
# if we find a start, use it
|
# if we find a start, use it
|
||||||
if MarkovState._start2 in new_states:
|
if MarkovState._start2 in new_states:
|
||||||
|
@ -87,7 +84,7 @@ def generate_sentence(context, topics=None, min_words=15, max_words=30):
|
||||||
|
|
||||||
i = len(words)
|
i = len(words)
|
||||||
while words[-1] != MarkovState._stop:
|
while words[-1] != MarkovState._stop:
|
||||||
log.debug("looking for '{0:s}','{1:s}'".format(words[i-2], words[i-1]))
|
log.debug("looking for '%s','%s'", words[i-2], words[i-1])
|
||||||
new_states = MarkovState.objects.filter(context=context, k1=words[i-2], k2=words[i-1])
|
new_states = MarkovState.objects.filter(context=context, k1=words[i-2], k2=words[i-1])
|
||||||
log.debug("states retrieved")
|
log.debug("states retrieved")
|
||||||
|
|
||||||
|
@ -103,7 +100,7 @@ def generate_sentence(context, topics=None, min_words=15, max_words=30):
|
||||||
words.append(MarkovState._stop)
|
words.append(MarkovState._stop)
|
||||||
elif len(target_hits) > 0:
|
elif len(target_hits) > 0:
|
||||||
# if there's a target word in the states, pick it
|
# if there's a target word in the states, pick it
|
||||||
target_hit = random.choice(target_hits)
|
target_hit = random.SystemRandom().choice(target_hits)
|
||||||
log.debug("found a topic hit %s, using it", target_hit)
|
log.debug("found a topic hit %s, using it", target_hit)
|
||||||
topics.remove(target_hit)
|
topics.remove(target_hit)
|
||||||
words.append(target_hit)
|
words.append(target_hit)
|
||||||
|
@ -127,36 +124,8 @@ def generate_sentence(context, topics=None, min_words=15, max_words=30):
|
||||||
return words
|
return words
|
||||||
|
|
||||||
|
|
||||||
def get_or_create_target_context(target_name):
|
|
||||||
"""Return the context for a provided nick/channel, creating missing ones."""
|
|
||||||
|
|
||||||
target_name = target_name.lower()
|
|
||||||
|
|
||||||
# find the stuff, or create it
|
|
||||||
try:
|
|
||||||
target = MarkovTarget.objects.get(name=target_name)
|
|
||||||
except MarkovTarget.DoesNotExist:
|
|
||||||
# we need to create a context and a target, and we have to make the context first
|
|
||||||
# make a context --- lacking a good idea, just create one with this target name until configured otherwise
|
|
||||||
context, c = MarkovContext.objects.get_or_create(name=target_name)
|
|
||||||
target, c = MarkovTarget.objects.get_or_create(name=target_name, context=context)
|
|
||||||
|
|
||||||
return target.context
|
|
||||||
|
|
||||||
try:
|
|
||||||
return target.context
|
|
||||||
except MarkovContext.DoesNotExist:
|
|
||||||
# make a context --- lacking a good idea, just create one with this target name until configured otherwise
|
|
||||||
context, c = MarkovContext.objects.get_or_create(name=target_name)
|
|
||||||
target.context = context
|
|
||||||
target.save()
|
|
||||||
|
|
||||||
return target.context
|
|
||||||
|
|
||||||
|
|
||||||
def get_word_out_of_states(states, backwards=False):
|
def get_word_out_of_states(states, backwards=False):
|
||||||
"""Pick one random word out of the given states."""
|
"""Pick one random word out of the given states."""
|
||||||
|
|
||||||
# work around possible broken data, where a k1,k2 should have a value but doesn't
|
# work around possible broken data, where a k1,k2 should have a value but doesn't
|
||||||
if len(states) == 0:
|
if len(states) == 0:
|
||||||
states = MarkovState.objects.filter(v=MarkovState._stop)
|
states = MarkovState.objects.filter(v=MarkovState._stop)
|
||||||
|
@ -164,9 +133,13 @@ def get_word_out_of_states(states, backwards=False):
|
||||||
new_word = ''
|
new_word = ''
|
||||||
running = 0
|
running = 0
|
||||||
count_sum = states.aggregate(Sum('count'))['count__sum']
|
count_sum = states.aggregate(Sum('count'))['count__sum']
|
||||||
hit = random.randint(0, count_sum)
|
if not count_sum:
|
||||||
|
# this being None probably means there's no data for this context
|
||||||
|
raise ValueError("no markov states to generate from")
|
||||||
|
|
||||||
log.debug("sum: {0:d} hit: {1:d}".format(count_sum, hit))
|
hit = random.SystemRandom().randint(0, count_sum)
|
||||||
|
|
||||||
|
log.debug("sum: %s hit: %s", count_sum, hit)
|
||||||
|
|
||||||
states_itr = states.iterator()
|
states_itr = states.iterator()
|
||||||
for state in states_itr:
|
for state in states_itr:
|
||||||
|
@ -179,13 +152,12 @@ def get_word_out_of_states(states, backwards=False):
|
||||||
|
|
||||||
break
|
break
|
||||||
|
|
||||||
log.debug("found '{0:s}'".format(new_word))
|
log.debug("found '%s'", new_word)
|
||||||
return new_word
|
return new_word
|
||||||
|
|
||||||
|
|
||||||
def learn_line(line, context):
|
def learn_line(line, context):
|
||||||
"""Create a bunch of MarkovStates for a given line of text."""
|
"""Create a bunch of MarkovStates for a given line of text."""
|
||||||
|
|
||||||
log.debug("learning %s...", line[:40])
|
log.debug("learning %s...", line[:40])
|
||||||
|
|
||||||
words = line.split()
|
words = line.split()
|
||||||
|
@ -196,7 +168,7 @@ def learn_line(line, context):
|
||||||
return
|
return
|
||||||
|
|
||||||
for i, word in enumerate(words):
|
for i, word in enumerate(words):
|
||||||
log.debug("'{0:s}','{1:s}' -> '{2:s}'".format(words[i], words[i+1], words[i+2]))
|
log.debug("'%s','%s' -> '%s'", words[i], words[i+1], words[i+2])
|
||||||
state, created = MarkovState.objects.get_or_create(context=context,
|
state, created = MarkovState.objects.get_or_create(context=context,
|
||||||
k1=words[i],
|
k1=words[i],
|
||||||
k2=words[i+1],
|
k2=words[i+1],
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
"""Management operations for the markov plugin and models."""
|
|
@ -0,0 +1 @@
|
||||||
|
"""Management commands for the markov plugin and models."""
|
|
@ -0,0 +1,90 @@
|
||||||
|
"""Clean up learned chains with speaker nicks (from the bridge) or self (because the bridge broke the regex)."""
|
||||||
|
from django.core.management import BaseCommand
|
||||||
|
|
||||||
|
from ircbot.models import IrcChannel
|
||||||
|
from markov.models import MarkovContext, MarkovState
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
"""Find markov chains that erroneously have speaker/self nicks and remove them."""
|
||||||
|
|
||||||
|
def handle(self, *args, **kwargs):
|
||||||
|
"""Scan the DB, looking for bad chains, and repair them."""
|
||||||
|
candidate_channels = IrcChannel.objects.exclude(discord_bridge='')
|
||||||
|
markov_contexts = MarkovContext.objects.filter(markovtarget__name__in=list(candidate_channels))
|
||||||
|
for context in markov_contexts:
|
||||||
|
self.stdout.write(self.style.NOTICE(f"scanning context {context}..."))
|
||||||
|
# get starting states that look like they came over the bridge
|
||||||
|
bridge_states = context.states.filter(k1=MarkovState._start1, k2=MarkovState._start2,
|
||||||
|
v__regex=r'<[A-Za-z0-9_]+>', context=context)
|
||||||
|
self._chain_remover(context, bridge_states)
|
||||||
|
# get states that look like mentions
|
||||||
|
for target in context.markovtarget_set.all():
|
||||||
|
if target.channel.server.additional_addressed_nicks:
|
||||||
|
all_nicks = '|'.join(target.channel.server.additional_addressed_nicks.split('\n') +
|
||||||
|
[target.channel.server.nickname])
|
||||||
|
else:
|
||||||
|
all_nicks = target.channel.server.nickname
|
||||||
|
mention_regex = r'^(({nicks})[:,]|@({nicks}))$'.format(nicks=all_nicks)
|
||||||
|
mention_states = context.states.filter(k1=MarkovState._start1, k2=MarkovState._start2,
|
||||||
|
v__regex=mention_regex, context=context)
|
||||||
|
self._chain_remover(context, mention_states)
|
||||||
|
|
||||||
|
def _chain_remover(self, context, start_states):
|
||||||
|
"""Remove a given k from markov states, deleting the found states after rebuilding subsequent states.
|
||||||
|
|
||||||
|
As in, if trying to remove A,B -> X, then B,X -> C and X,C -> D must be rebuilt (A,B -> C / B,C -> D)
|
||||||
|
then the three states with X deleted.
|
||||||
|
"""
|
||||||
|
for start_state in start_states:
|
||||||
|
self.stdout.write(self.style.NOTICE(f" diving into {start_state}..."))
|
||||||
|
# find the states that build off of the start
|
||||||
|
second_states = context.states.filter(k1=start_state.k2, k2=start_state.v)
|
||||||
|
|
||||||
|
for second_state in second_states:
|
||||||
|
self.stdout.write(self.style.NOTICE(f" diving into {second_state}..."))
|
||||||
|
# find the third states
|
||||||
|
leaf_states = context.states.filter(k1=second_state.k2, k2=second_state.v)
|
||||||
|
|
||||||
|
for leaf_state in leaf_states:
|
||||||
|
self.stdout.write(self.style.NOTICE(f" upserting state based on {leaf_state}"))
|
||||||
|
# get/update state without the nick from the bridge
|
||||||
|
try:
|
||||||
|
updated_leaf = MarkovState.objects.get(k1=second_state.k1, k2=leaf_state.k2, v=leaf_state.v,
|
||||||
|
context=context)
|
||||||
|
updated_leaf.count += leaf_state.count
|
||||||
|
updated_leaf.save()
|
||||||
|
self.stdout.write(self.style.SUCCESS(f" updated count for {updated_leaf}"))
|
||||||
|
except MarkovState.DoesNotExist:
|
||||||
|
new_leaf = MarkovState.objects.create(k1=second_state.k1, k2=leaf_state.k2, v=leaf_state.v,
|
||||||
|
context=context)
|
||||||
|
new_leaf.count = leaf_state.count
|
||||||
|
new_leaf.save()
|
||||||
|
self.stdout.write(self.style.SUCCESS(f" created {new_leaf}"))
|
||||||
|
|
||||||
|
# remove the migrated leaf state
|
||||||
|
self.stdout.write(self.style.SUCCESS(f" deleting {leaf_state}"))
|
||||||
|
leaf_state.delete()
|
||||||
|
|
||||||
|
# take care of the new middle state
|
||||||
|
self.stdout.write(self.style.NOTICE(f" upserting state based on {second_state}"))
|
||||||
|
try:
|
||||||
|
updated_second = MarkovState.objects.get(k1=start_state.k1, k2=start_state.k2, v=second_state.v,
|
||||||
|
context=context)
|
||||||
|
updated_second.count += second_state.count
|
||||||
|
updated_second.save()
|
||||||
|
self.stdout.write(self.style.SUCCESS(f" updated count for {updated_second}"))
|
||||||
|
except MarkovState.DoesNotExist:
|
||||||
|
new_second = MarkovState.objects.create(k1=start_state.k1, k2=start_state.k2, v=second_state.v,
|
||||||
|
context=context)
|
||||||
|
new_second.count = second_state.count
|
||||||
|
new_second.save()
|
||||||
|
self.stdout.write(self.style.SUCCESS(f" created {new_second}"))
|
||||||
|
|
||||||
|
# remove the migrated second state
|
||||||
|
self.stdout.write(self.style.SUCCESS(f" deleting {second_state}"))
|
||||||
|
second_state.delete()
|
||||||
|
|
||||||
|
# remove the dead end original start
|
||||||
|
self.stdout.write(self.style.SUCCESS(f" deleting {start_state}"))
|
||||||
|
start_state.delete()
|
|
@ -27,7 +27,7 @@ class Migration(migrations.Migration):
|
||||||
('k2', models.CharField(max_length=128)),
|
('k2', models.CharField(max_length=128)),
|
||||||
('v', models.CharField(max_length=128)),
|
('v', models.CharField(max_length=128)),
|
||||||
('count', models.IntegerField(default=0)),
|
('count', models.IntegerField(default=0)),
|
||||||
('context', models.ForeignKey(to='markov.MarkovContext')),
|
('context', models.ForeignKey(to='markov.MarkovContext', on_delete=models.CASCADE)),
|
||||||
],
|
],
|
||||||
options={
|
options={
|
||||||
'permissions': set([('teach_line', 'Can teach lines'), ('import_log_file', 'Can import states from a log file')]),
|
'permissions': set([('teach_line', 'Can teach lines'), ('import_log_file', 'Can import states from a log file')]),
|
||||||
|
@ -40,7 +40,7 @@ class Migration(migrations.Migration):
|
||||||
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
|
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
|
||||||
('name', models.CharField(unique=True, max_length=64)),
|
('name', models.CharField(unique=True, max_length=64)),
|
||||||
('chatter_chance', models.IntegerField(default=0)),
|
('chatter_chance', models.IntegerField(default=0)),
|
||||||
('context', models.ForeignKey(to='markov.MarkovContext')),
|
('context', models.ForeignKey(to='markov.MarkovContext', on_delete=models.CASCADE)),
|
||||||
],
|
],
|
||||||
options={
|
options={
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import unicode_literals
|
|
||||||
|
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
# Generated by Django 3.2.18 on 2023-02-19 19:00
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('markov', '0003_auto_20161112_2348'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='markovstate',
|
||||||
|
name='context',
|
||||||
|
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='states', to='markov.markovcontext'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -0,0 +1,20 @@
|
||||||
|
# Generated by Django 3.2.18 on 2023-02-20 00:09
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('ircbot', '0019_ircchannel_discord_bridge'),
|
||||||
|
('markov', '0004_alter_markovstate_context'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='markovtarget',
|
||||||
|
name='channel',
|
||||||
|
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='ircbot.ircchannel'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -0,0 +1,24 @@
|
||||||
|
"""Generated by Django 3.2.18 on 2023-02-19 23:15."""
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
def link_markovcontext_to_ircchannel(apps, schema_editor):
|
||||||
|
"""Link the markov targets to a hopefully matching channel, by name."""
|
||||||
|
IrcChannel = apps.get_model('ircbot', 'IrcChannel')
|
||||||
|
MarkovTarget = apps.get_model('markov', 'MarkovTarget')
|
||||||
|
for target in MarkovTarget.objects.all():
|
||||||
|
channel = IrcChannel.objects.get(name=target.name)
|
||||||
|
target.channel = channel
|
||||||
|
target.save()
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
"""Populate the markov target to IRC channel link."""
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('markov', '0005_markovtarget_channel'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RunPython(link_markovcontext_to_ircchannel)
|
||||||
|
]
|
|
@ -0,0 +1,20 @@
|
||||||
|
# Generated by Django 3.2.18 on 2023-02-20 00:11
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('ircbot', '0019_ircchannel_discord_bridge'),
|
||||||
|
('markov', '0006_link_markovtarget_to_ircchannel'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='markovtarget',
|
||||||
|
name='channel',
|
||||||
|
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ircbot.ircchannel'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 3.2.18 on 2023-05-04 22:17
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('markov', '0007_alter_markovtarget_channel'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='markovtarget',
|
||||||
|
name='name',
|
||||||
|
field=models.CharField(max_length=200),
|
||||||
|
),
|
||||||
|
]
|
|
@ -1,45 +1,38 @@
|
||||||
"""
|
"""Save brain pieces as markov chains for chaining."""
|
||||||
markov/models.py --- save brain pieces for chaining
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
from ircbot.models import IrcChannel
|
||||||
|
|
||||||
log = logging.getLogger('markov.models')
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class MarkovContext(models.Model):
|
class MarkovContext(models.Model):
|
||||||
|
|
||||||
"""Define contexts for Markov chains."""
|
"""Define contexts for Markov chains."""
|
||||||
|
|
||||||
name = models.CharField(max_length=200, unique=True)
|
name = models.CharField(max_length=200, unique=True)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""String representation."""
|
"""Provide string representation."""
|
||||||
|
|
||||||
return "{0:s}".format(self.name)
|
return "{0:s}".format(self.name)
|
||||||
|
|
||||||
|
|
||||||
class MarkovTarget(models.Model):
|
class MarkovTarget(models.Model):
|
||||||
|
|
||||||
"""Define IRC targets that relate to a context, and can occasionally be talked to."""
|
"""Define IRC targets that relate to a context, and can occasionally be talked to."""
|
||||||
|
|
||||||
name = models.CharField(max_length=200, unique=True)
|
name = models.CharField(max_length=200)
|
||||||
context = models.ForeignKey(MarkovContext)
|
context = models.ForeignKey(MarkovContext, on_delete=models.CASCADE)
|
||||||
|
channel = models.ForeignKey(IrcChannel, on_delete=models.CASCADE)
|
||||||
|
|
||||||
chatter_chance = models.IntegerField(default=0)
|
chatter_chance = models.IntegerField(default=0)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""String representation."""
|
"""Provide string representation."""
|
||||||
|
return "{0:s} -> {1:s}".format(str(self.channel), self.context.name)
|
||||||
return "{0:s} -> {1:s}".format(self.name, self.context.name)
|
|
||||||
|
|
||||||
|
|
||||||
class MarkovState(models.Model):
|
class MarkovState(models.Model):
|
||||||
|
|
||||||
"""One element in a Markov chain, some text or something."""
|
"""One element in a Markov chain, some text or something."""
|
||||||
|
|
||||||
_start1 = '__start1'
|
_start1 = '__start1'
|
||||||
|
@ -51,9 +44,11 @@ class MarkovState(models.Model):
|
||||||
v = models.CharField(max_length=128)
|
v = models.CharField(max_length=128)
|
||||||
|
|
||||||
count = models.IntegerField(default=0)
|
count = models.IntegerField(default=0)
|
||||||
context = models.ForeignKey(MarkovContext)
|
context = models.ForeignKey(MarkovContext, on_delete=models.CASCADE, related_name='states')
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
"""Options for the model itself."""
|
||||||
|
|
||||||
index_together = [
|
index_together = [
|
||||||
['context', 'k1', 'k2'],
|
['context', 'k1', 'k2'],
|
||||||
['context', 'v'],
|
['context', 'v'],
|
||||||
|
@ -65,6 +60,5 @@ class MarkovState(models.Model):
|
||||||
unique_together = ('context', 'k1', 'k2', 'v')
|
unique_together = ('context', 'k1', 'k2', 'v')
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""String representation."""
|
"""Provide string representation."""
|
||||||
|
|
||||||
return "{0:s},{1:s} -> {2:s} (count: {3:d})".format(self.k1, self.k2, self.v, self.count)
|
return "{0:s},{1:s} -> {2:s} (count: {3:d})".format(self.k1, self.k2, self.v, self.count)
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
"""URL patterns for markov stuff."""
|
"""URL patterns for markov stuff."""
|
||||||
|
from django.urls import path
|
||||||
from django.conf.urls import url
|
|
||||||
from django.views.generic import TemplateView
|
from django.views.generic import TemplateView
|
||||||
|
|
||||||
from markov.views import context_index
|
from markov.views import context_index, rpc_generate_line_for_context, rpc_learn_line_for_context
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^$', TemplateView.as_view(template_name='index.html'), name='markov_index'),
|
path('', TemplateView.as_view(template_name='index.html'), name='markov_index'),
|
||||||
url(r'^context/(?P<context_id>\d+)/$', context_index, name='markov_context_index'),
|
path('context/<int:context_id>/', context_index, name='markov_context_index'),
|
||||||
|
|
||||||
|
path('rpc/context/<context>/generate/', rpc_generate_line_for_context, name='markov_rpc_generate_line'),
|
||||||
|
path('rpc/context/<context>/learn/', rpc_learn_line_for_context, name='markov_rpc_learn_line'),
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,16 +1,19 @@
|
||||||
"""Manipulate Markov data via the Django site."""
|
"""Manipulate Markov data via the Django site."""
|
||||||
|
import json
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.shortcuts import get_object_or_404, render
|
from django.shortcuts import get_object_or_404, render
|
||||||
|
from rest_framework.authentication import BasicAuthentication
|
||||||
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
from rest_framework.response import Response
|
||||||
|
from rest_framework.decorators import api_view, authentication_classes, permission_classes
|
||||||
|
|
||||||
import markov.lib as markovlib
|
import markov.lib as markovlib
|
||||||
from markov.models import MarkovContext
|
from markov.models import MarkovContext
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
log = logging.getLogger('markov.views')
|
|
||||||
|
|
||||||
|
|
||||||
def index(request):
|
def index(request):
|
||||||
|
@ -28,3 +31,52 @@ def context_index(request, context_id):
|
||||||
end_t = time.time()
|
end_t = time.time()
|
||||||
|
|
||||||
return render(request, 'markov/context.html', {'chain': chain, 'context': context, 'elapsed': end_t - start_t})
|
return render(request, 'markov/context.html', {'chain': chain, 'context': context, 'elapsed': end_t - start_t})
|
||||||
|
|
||||||
|
|
||||||
|
@api_view(['POST'])
|
||||||
|
@authentication_classes((BasicAuthentication, ))
|
||||||
|
@permission_classes((IsAuthenticated, ))
|
||||||
|
def rpc_generate_line_for_context(request, context):
|
||||||
|
"""Generate a line from a given context, with optional topics included."""
|
||||||
|
if request.method != 'POST':
|
||||||
|
return Response({'detail': "Supported method: POST."}, status=405)
|
||||||
|
|
||||||
|
topics = None
|
||||||
|
try:
|
||||||
|
if request.body:
|
||||||
|
markov_data = json.loads(request.body)
|
||||||
|
topics = markov_data.get('topics', [])
|
||||||
|
except (json.decoder.JSONDecodeError, KeyError):
|
||||||
|
return Response({'detail': "Request body, if provided, must be JSON with an optional 'topics' parameter."},
|
||||||
|
status=400)
|
||||||
|
|
||||||
|
context_id = markovlib.get_or_create_target_context(context)
|
||||||
|
try:
|
||||||
|
generated_words = markovlib.generate_line(context_id, topics)
|
||||||
|
except ValueError as vex:
|
||||||
|
return Response({'detail': f"Could not generate line: {vex}", 'context': context, 'topics': topics},
|
||||||
|
status=400)
|
||||||
|
else:
|
||||||
|
return Response({
|
||||||
|
'context': context, 'topics': topics,
|
||||||
|
'generated_line': ' '.join(generated_words), 'generated_words': generated_words
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
@api_view(['POST'])
|
||||||
|
@authentication_classes((BasicAuthentication, ))
|
||||||
|
@permission_classes((IsAuthenticated, ))
|
||||||
|
def rpc_learn_line_for_context(request, context):
|
||||||
|
"""Learn a line for a given context."""
|
||||||
|
if request.method != 'POST':
|
||||||
|
return Response({'detail': "Supported method: POST."}, status=405)
|
||||||
|
|
||||||
|
try:
|
||||||
|
markov_data = json.loads(request.body)
|
||||||
|
line = markov_data.get('line', [])
|
||||||
|
except (json.decoder.JSONDecodeError, KeyError):
|
||||||
|
return Response({'detail': "Request body must be JSON with a 'line' parameter."}, status=400)
|
||||||
|
|
||||||
|
context_id = markovlib.get_or_create_target_context(context)
|
||||||
|
markovlib.learn_line(line, context_id)
|
||||||
|
return Response({'status': "OK", 'context': context, 'line': line})
|
||||||
|
|
|
@ -1,21 +1,14 @@
|
||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
|
"""Provide pi simulation results to IRC."""
|
||||||
import logging
|
|
||||||
|
|
||||||
from ircbot.lib import Plugin
|
from ircbot.lib import Plugin
|
||||||
from pi.models import PiLog
|
from pi.models import PiLog
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger('pi.ircplugin')
|
|
||||||
|
|
||||||
|
|
||||||
class Pi(Plugin):
|
class Pi(Plugin):
|
||||||
|
|
||||||
"""Use the Monte Carlo method to simulate pi."""
|
"""Use the Monte Carlo method to simulate pi."""
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
"""Set up the handlers."""
|
"""Set up the handlers."""
|
||||||
|
|
||||||
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!pi$',
|
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!pi$',
|
||||||
self.handle_pi, -20)
|
self.handle_pi, -20)
|
||||||
|
|
||||||
|
@ -23,17 +16,16 @@ class Pi(Plugin):
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""Tear down handlers."""
|
"""Tear down handlers."""
|
||||||
|
|
||||||
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_pi)
|
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_pi)
|
||||||
|
|
||||||
super(Pi, self).stop()
|
super(Pi, self).stop()
|
||||||
|
|
||||||
def handle_pi(self, connection, event, match):
|
def handle_pi(self, connection, event, match):
|
||||||
"""Handle the pi command by generating another value and presenting it."""
|
"""Handle the pi command by generating another value and presenting it."""
|
||||||
|
newest, x, y = PiLog.objects.simulate()
|
||||||
newest, x, y, hit = PiLog.objects.simulate()
|
|
||||||
msg = ("({0:.10f}, {1:.10f}) is {2}within the unit circle. π is {5:.10f}. (i:{3:d} p:{4:d})"
|
msg = ("({0:.10f}, {1:.10f}) is {2}within the unit circle. π is {5:.10f}. (i:{3:d} p:{4:d})"
|
||||||
"".format(x, y, "" if hit else "not ", newest.count_inside, newest.count_total, newest.value()))
|
"".format(x, y, "" if newest.hit else "not ", newest.total_count_inside,
|
||||||
|
newest.total_count, newest.value))
|
||||||
|
|
||||||
return self.bot.reply(event, msg)
|
return self.bot.reply(event, msg)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
# Generated by Django 3.1.2 on 2020-10-24 16:27
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('pi', '0002_auto_20150521_2204'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RenameField(
|
||||||
|
model_name='pilog',
|
||||||
|
old_name='count_total',
|
||||||
|
new_name='total_count',
|
||||||
|
),
|
||||||
|
migrations.RenameField(
|
||||||
|
model_name='pilog',
|
||||||
|
old_name='count_inside',
|
||||||
|
new_name='total_count_inside',
|
||||||
|
),
|
||||||
|
]
|
|
@ -0,0 +1,25 @@
|
||||||
|
# Generated by Django 3.1.2 on 2020-10-24 16:31
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('pi', '0003_rename_count_fields'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='pilog',
|
||||||
|
name='simulation_x',
|
||||||
|
field=models.DecimalField(decimal_places=10, default=-1.0, max_digits=11),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='pilog',
|
||||||
|
name='simulation_y',
|
||||||
|
field=models.DecimalField(decimal_places=10, default=-1.0, max_digits=11),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
]
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue