Skip to content

Commit

Permalink
Merge branch 'develop' into pythonmultithread
Browse files Browse the repository at this point in the history
  • Loading branch information
thommey committed Jun 29, 2024
2 parents af20c86 + 3302c4a commit 9068a67
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 117 deletions.
1 change: 0 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ EGG_TLS_DETECT
EGG_TDNS_ENABLE

# Check for Python
EGG_PYTHON_ENABLE
EGG_PYTHON_WITHCONFIG


Expand Down
107 changes: 5 additions & 102 deletions doc/sphinx_source/modules/mod/python.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Python Module

This module adds a Python interpreter to Eggdrop, allowing you to run Python scripts.

-------------------
System Requirements
-------------------
This module requires Python version 3.8 or higher in order to run. Similar to Tcl requirements, Eggdrop requires both python and python development libraries to be installed on the host machine. On Debian/Ubuntu machines, this means the packages ``python``, ``python-dev`` AND ``python-is-python3`` to be installed. The python-is-python3 updates symlinks on the host system that allow Eggdrop to find it.

--------------
Loading Python
--------------
Expand Down Expand Up @@ -50,105 +55,3 @@ pysource <path/to/file>
^^^^^^^^^^^^^^^^^^^^^^^

The ``pysource`` command is analogous to the Tcl ``source`` command, except that it loads a Python script into Eggdrop instead of a Tcl script.

-----------------------
Eggdrop Python Commands
-----------------------

The Python module is built to use the existing core Tcl commands integrated into Eggdrop via the ``eggdrop.tcl`` module. To call an existing Tcl command from Python, you can either load the entire catalog by running ``import eggdrop.tcl``, or be more specific by ``from eggdrop.tcl import putserv, putlog, chanlist``, etc.

Arguments to the Tcl functions are automatically converted as follows:

* ``None`` is converted to an empty Tcl object (the empty string, ``""``)
* ``List`` and ``Tuple`` is converted to a ``Tcl list``
* ``Dict`` is converted to a ``Tcl dictionary``
* Everything else is converted to a string using the str() method

Return values from Tcl functions must be manually converted:

* ``""`` the empty string is automatically converted to None
* everything else is returned as string
* ``Tcl list`` as string can be converted to a Python ``List`` using ``parse_tcl_list``
* ``Tcl dictionary`` as string can be converted to a Python ``Dict`` using ``parse_tcl_list``

Additionally, a few extra python commands have been created for use without these conversions:

^^^^^^^^^^^^^^^^
bind <arguments>
^^^^^^^^^^^^^^^^

The python version of the bind command is used to create a bind that triggers a python function. The python bind takes the same arguments as the Tcl binds, but here each argument is passed individually. For example, a bind that would look like ``bind pub * !foo myproc`` in Tcl is written as ``bind("pub", "*", "!foo", myproc)``. For more information on Eggsrop bind argument syntax please see :ref:`bind_types`. The eggdrop.tcl.bind command should not be used as it will attempt to call a Tcl proc.

^^^^^^^^^^^^^^^^^^^^^^^
parse_tcl_list <string>
^^^^^^^^^^^^^^^^^^^^^^^

When a python script calls a Tcl command that returns a list via the eggdrop.tcl module, the return value will be a Tcl-formatted list- also simply known as a string. The ``parse_tcl_list`` command will convert the Tcl-formatted list into a Python list, which can then freely be used within the Python script.

^^^^^^^^^^^^^^^^^^^^^^^
parse_tcl_dict <string>
^^^^^^^^^^^^^^^^^^^^^^^

When a python script calls a Tcl command that returns a dict via the eggdrop.tcl module, the return value will be a Tcl-formatted dict- also simply known as a string. The ``parse_tcl_dict`` command will c
onvert the Tcl-formatted dict into a Python list, which can then freely be used within the Python script.

----------------
Config variables
----------------

There are also some variables you can set in your config file:

set allow-resync 0
When two bots get disconnected, this setting allows them to create a
resync buffer which saves all changes done to the userfile during
the disconnect. When they reconnect, they will not have to transfer
the complete user file, but, instead, just send the resync buffer.

--------------------------------
Writing an Eggdrop Python script
--------------------------------

This is how to write a python script for Eggdrop.

You can view examples of Python scripts in the exampleScripts folder included with this module.

.. glossary::

bestfriend.py
This example script demonstrates how to use the parse_tcl_list() python command to convert a list returned by a Tcl command into a list that is usable by Python.

greet.py
This is a very basic script that demonstrates how a Python script with binds can be run by Eggdrop.

imdb.py
This script shows how to use an existing third-party module to extend a Python script, in this case retrieving information from imdb.com.

listtls.py
This script demonstrates how to use parse-tcl_list() and parse_tcl_dict() to convert a list of dicts provided by Tcl into something that is usable by Python.

urltitle.py
This script shows how to use an existing third-party module to extend a Python script, in this case using an http parser to collect title information from a provided web page.


^^^^^^^^^^^^^^
Header section
^^^^^^^^^^^^^^

An Eggdrop python script requires you to import X Y and Z, in this format.

^^^^^^^^^^^^
Code Section
^^^^^^^^^^^^

Normal python code works here. To run a command from the Eggdrop Tcl library, use this format.

Use this format all over.

-------------------------------------
Writing a module for use with Eggdrop
-------------------------------------

This is how you import a module for use with an egg python script.


Copyright (C) 2000 - 2024 Eggheads Development Team
113 changes: 113 additions & 0 deletions doc/sphinx_source/using/python.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
=======================
Using the Python Module
=======================

In Eggdrop 1.10.0, Eggdrop was shipped with a Python module that, similar to the existing core Tcl capability, allows Eggdrop to run python scripts.

-------------------
System Requirements
-------------------
Similar to Tcl requirements, Eggdrop requires both python and python development libraries to be installed on the host machine. On Debian/Ubuntu machines, this requires the packages python-dev AND python-is-python3 to be installed. The python-is-python3 updates symlinks on the host system that allow Eggdrop to find it.

--------------
Loading Python
--------------

Put this line into your Eggdrop configuration file to load the python module::

loadmodule python

To load a python script from your config file, place the .py file in the scripts/ folder and add the following line to your config::

pysource scripts/myscript.py

-----------------------
Eggdrop Python Commands
-----------------------

The Python module is built to use the existing core Tcl commands integrated into Eggdrop via the ``eggdrop.tcl`` module. To call an existing Tcl command from Python, you can either load the entire catalog by running ``import eggdrop.tcl``, or be more specific by ``from eggdrop.tcl import putserv, putlog, chanlist``, etc.

Arguments to the Tcl functions are automatically converted as follows:

* ``None`` is converted to an empty Tcl object (the empty string, ``""``)
* ``List`` and ``Tuple`` is converted to a ``Tcl list``
* ``Dict`` is converted to a ``Tcl dictionary``
* Everything else is converted to a string using the str() method

Return values from Tcl functions must be manually converted:

* ``""`` the empty string is automatically converted to None
* everything else is returned as string
* ``Tcl list`` as string can be converted to a Python ``List`` using ``parse_tcl_list``
* ``Tcl dictionary`` as string can be converted to a Python ``Dict`` using ``parse_tcl_list``

^^^^^^^^^^^^^^^^
bind <arguments>
^^^^^^^^^^^^^^^^

An important difference to note is that Eggdrop Python has its own ``bind`` command implemented. You will generally want to create binds using the Python ``bind`` command and not import bind from eggdrop.tcl because a Python bind will call a Python function, whereas using the Tcl bind will call a Tcl function (not one from the script you are writing).

The python version of the bind command is used to create a bind that triggers a python function. The python bind takes the same arguments as the Tcl binds, but here each argument is passed individually. For example, a bind that would look like ``bind pub * !foo myproc`` in Tcl is written as ``bind("pub", "*", "!foo", myproc)``. For more information on Eggsrop bind argument syntax please see :ref:`bind_types`. The eggdrop.tcl.bind command should not be used as it will attempt to call a Tcl proc.

^^^^^^^^^^^^^^^^^^^^^^^
parse_tcl_list <string>
^^^^^^^^^^^^^^^^^^^^^^^

When a python script calls a Tcl command that returns a list via the eggdrop.tcl module, the return value will be a Tcl-formatted list- also simply known as a string. The ``parse_tcl_list`` command will convert the Tcl-formatted list into a Python list, which can then freely be used within the Python script.

^^^^^^^^^^^^^^^^^^^^^^^
parse_tcl_dict <string>
^^^^^^^^^^^^^^^^^^^^^^^

When a python script calls a Tcl command that returns a dict via the eggdrop.tcl module, the return value will be a Tcl-formatted dict- also simply known as a string. The ``parse_tcl_dict`` command will convert the Tcl-formatted dict into a Python list, which can then freely be used within the Python script.

--------------------------------
Writing an Eggdrop Python script
--------------------------------

Some example scripts, complete with documentation, are included with the Python module that ships with Eggdrop (src/mod/python.mod/scripts). These scripts are included to help demonstrate script formatting and usage. The scripts are:


.. glossary::

bestfriend.py
This example script demonstrates how to use the parse_tcl_list() python command to convert a list returned by a Tcl command into a list that is usable by Python.

greet.py
This is a very basic script that demonstrates how a Python script with binds can be run by Eggdrop.

imdb.py
This script shows how to use an existing third-party module to extend a Python script, in this case retrieving information from imdb.com.

listtls.py
This script demonstrates how to use parse-tcl_list() and parse_tcl_dict() to convert a list of dicts provided by Tcl into something that is usable by Python.

urltitle.py
This script shows how to use an existing third-party module to extend a Python script, in this case using an http parser to collect title information from a provided web page.


^^^^^^^^^^^^^^
Header section
^^^^^^^^^^^^^^

Python is able to call any Tcl command by importing the ``eggdrop`` module. For example, to use the ``putlog`` command in a python script, you would import it as::

from eggdrop.tcl import putlog

and then call it using::

putlog("This is a logged message")


An important difference to note is that Eggdrop Python has its own ``bind`` command implemented. You will generally want to create binds using the Python ``bind`` command and not import bind from eggdrop.tcl because a Python bind will call a Python function, whereas using the Tcl bind will call a Tcl function (not one from the script you are writing).

Where does python print go?

-------------------------------------
Writing a module for use with Eggdrop
-------------------------------------

This is how you import a module for use with an egg python script.

Copyright (C) 2000 - 2024 Eggheads Development Team

23 changes: 9 additions & 14 deletions eggdrop.conf
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ loadmodule pbkdf2
# This setting is planned to be enabled by default in Eggdrop 2.0.
#set remove-pass 0


#### BLOWFISH MODULE ####
#
# This module is planned to be removed in Eggdrop 2.0
Expand All @@ -676,21 +677,14 @@ loadmodule blowfish
set blowfish-use-mode cbc


#### DNS MODULE (Deprecated) ####

## This module provided asynchronous dns support, but as of v1.9.2, this
## functionality was moved into the core code. If you are having issues with the
## new DNS functionality, or just want to continue using this module, compile
## Eggdrop with the --disable-tdns flag (./configure --disdable-tdns).
#
## You really probably don't want to uncomment this!!!!
#### PYTHON MODULE #####
#
#loadmodule dns
#set dns-servers "8.8.8.8 1.1.1.1 185.222.222.222"
#set dns-cache 86400
#set dns-negcache 600
#set dns-maxsends 4
#set dns-retrydelay 3
# This module gives Eggdrop the ability to run python scripts. if loaded,
# Python scripts can be loaded at the end of the config file using the pysouce
# command to tell Eggdrop where the file is loaded. The module requires Python
# version 3.8 or higher to run. To load the python module, uncomment it below.
#loadmodule python


#### CHANNELS MODULE ####

Expand Down Expand Up @@ -1448,6 +1442,7 @@ set xfer-timeout 30
# be v1.9.0 or higher).
#set sharefail-unlink 1


#### SHARE MODULE ####

# This module provides userfile sharing support between two directly
Expand Down
1 change: 1 addition & 0 deletions src/mod/python.mod/scripts/urlTitle.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def pubGetTitle(nick, host, handle, channel, text, **kwargs):
try:
reqs = requests.get(text)
soup = BeautifulSoup(reqs.text, 'html.parser')
putlog("Found title for "+text)
putmsg(channel, "The title of the webpage is: "+soup.find_all('title')[0].get_text())
except Exception as e:
putmsg(channel, "Error: " + str(e))
Expand Down

0 comments on commit 9068a67

Please sign in to comment.