Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def make_incoming_message(self, text, addr="Alice "):
msg = Message.new_empty(self.account, "text")
msg.set_text(text)
name, routeable_addr = parseaddr(addr)
contact = self.account.create_contact(email=routeable_addr, name=name)
chat = self.account.create_chat_by_contact(contact)
msg_in = chat.prepare_message(msg)
return msg_in
@account_hookimpl
def ac_incoming_message(self, message):
if message.chat == self.bot_chat:
self._replies.put(message)
#
# This file is execfile()d with the current directory set to its containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.
import sys, os
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
from deltachat import __version__ as release
version = ".".join(release.split(".")[:2])
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(0, os.path.abspath('.'))
# -- General configuration -----------------------------------------------------
# If your documentation needs a minimal Sphinx version, state it here.
#needs_sphinx = '1.0'
# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.autosummary',
:param db_path: a path to the account database. The database
will be created if it doesn't exist.
:param logid: an optional logging prefix that should be used with
the default internal logging.
"""
self._dc_context = ffi.gc(
lib.dc_context_new(lib.py_dc_callback, ffi.NULL, ffi.NULL),
_destroy_dc_context,
)
if hasattr(db_path, "encode"):
db_path = db_path.encode("utf8")
if not lib.dc_open(self._dc_context, db_path, ffi.NULL):
raise ValueError("Could not dc_open: {}".format(db_path))
self._evhandler = EventHandler(self._dc_context)
self._evlogger = EventLogger(self._dc_context, logid)
deltachat.set_context_callback(self._dc_context, self._process_event)
self._threads = IOThreads(self._dc_context)
self._configkeys = self.get_config("sys.config_keys").split()
def bot_main(ctx, basedir, stdout_loglevel):
"""delta.chat bot management command line interface."""
basedir = os.path.abspath(os.path.expanduser(basedir))
if not os.path.exists(basedir):
os.makedirs(basedir)
db_path = os.path.join(basedir, "account.db")
account = deltachat.Account(db_path, "deltabot/{}".format(sys.platform))
loglevel = getattr(logging, stdout_loglevel.upper())
logger = make_logger(basedir, loglevel)
ctx.bot = DeltaBot(account, logger)
def get_account(basedir, remove=False):
dbpath = os.path.join(basedir, "account.db")
if remove and os.path.exists(dbpath):
os.remove(dbpath)
acc = deltachat.Account(dbpath)
acc.db_path = dbpath
return acc
def make_bot_from_args(args, plugin_manager, account=None):
basedir = os.path.abspath(os.path.expanduser(args.basedir))
if not os.path.exists(basedir):
os.makedirs(basedir)
if account is None:
db_path = os.path.join(basedir, "account.db")
account = Account(db_path, "deltabot/{}".format(sys.platform))
logger = plugin_manager.hook.deltabot_get_logger(args=args)
return DeltaBot(account, logger, plugin_manager=plugin_manager)
evt_name = get_dc_event_name(evt)
event_sig_types = capi.lib.dc_get_event_signature_types(evt)
if data1 and event_sig_types & 1:
data1 = ffi.string(ffi.cast('char*', data1)).decode("utf8")
if data2 and event_sig_types & 2:
try:
data2 = ffi.string(ffi.cast('char*', data2)).decode("utf8")
except UnicodeDecodeError:
# XXX ignoring this error is not quite correct but for now
# i don't want to hunt down encoding problems in the c lib
data2 = ffi.string(ffi.cast('char*', data2))
try:
ret = callback(ctx, evt_name, data1, data2)
if event_sig_types & 4:
return ffi.cast('uintptr_t', ret)
elif event_sig_types & 8:
return ffi.cast('int', ret)
except: # noqa
raise
ret = 0
return ret
CFFI only allows us to set one global event handler, so this one
looks up the correct event handler for the given context.
"""
try:
callback = _DC_CALLBACK_MAP.get(ctx, lambda *a: 0)
except AttributeError:
# we are in a deep in GC-free/interpreter shutdown land
# nothing much better to do here than:
return 0
# the following code relates to the deltachat/_build.py's helper
# function which provides us signature info of an event call
evt_name = get_dc_event_name(evt)
event_sig_types = capi.lib.dc_get_event_signature_types(evt)
if data1 and event_sig_types & 1:
data1 = ffi.string(ffi.cast('char*', data1)).decode("utf8")
if data2 and event_sig_types & 2:
try:
data2 = ffi.string(ffi.cast('char*', data2)).decode("utf8")
except UnicodeDecodeError:
# XXX ignoring this error is not quite correct but for now
# i don't want to hunt down encoding problems in the c lib
data2 = ffi.string(ffi.cast('char*', data2))
try:
ret = callback(ctx, evt_name, data1, data2)
if event_sig_types & 4:
return ffi.cast('uintptr_t', ret)
elif event_sig_types & 8:
return ffi.cast('int', ret)
except: # noqa
raise
try:
callback = _DC_CALLBACK_MAP.get(ctx, lambda *a: 0)
except AttributeError:
# we are in a deep in GC-free/interpreter shutdown land
# nothing much better to do here than:
return 0
# the following code relates to the deltachat/_build.py's helper
# function which provides us signature info of an event call
evt_name = get_dc_event_name(evt)
event_sig_types = capi.lib.dc_get_event_signature_types(evt)
if data1 and event_sig_types & 1:
data1 = ffi.string(ffi.cast('char*', data1)).decode("utf8")
if data2 and event_sig_types & 2:
try:
data2 = ffi.string(ffi.cast('char*', data2)).decode("utf8")
except UnicodeDecodeError:
# XXX ignoring this error is not quite correct but for now
# i don't want to hunt down encoding problems in the c lib
data2 = ffi.string(ffi.cast('char*', data2))
try:
ret = callback(ctx, evt_name, data1, data2)
if event_sig_types & 4:
return ffi.cast('uintptr_t', ret)
elif event_sig_types & 8:
return ffi.cast('int', ret)
except: # noqa
raise
ret = 0
return ret