+ if(!PyArg_ParseTuple(args, "s", &name))
+ return NULL;
+
+ if(!(channel = GetChannel(name))) {
+ PyErr_SetString(PyExc_Exception, "unknown channel");
+ return NULL;
+ }
+
+ return pyobj_from_channode(channel);
+}
+
+PyDoc_STRVAR(emb_get_account__doc__,
+ "get_account(account) -> dict with account information\n\n"
+ "Changes made to the returned dictionary will not be reflected in the\n"
+ "account's information.");
+
+static PyObject*
+emb_get_account(UNUSED_ARG(PyObject *self), PyObject *args)
+{
+ /* Returns a python dict object with all sorts of info about an account.
+ usage: _svc.get_account(<account name>)
+ */
+ char *name;
+ struct handle_info *hi;
+
+
+ if(!PyArg_ParseTuple(args, "s", &name))
+ return NULL;
+
+ hi = get_handle_info(name);
+
+ if(!hi) {
+ PyErr_SetString(PyExc_Exception, "unknown account name");
+ return NULL;
+ }
+
+ return Py_BuildValue("{s:s,s:i,s:s,s:s,s:s"
+ ",s:s,s:s}",
+
+ "account", hi->handle,
+ "registered", hi->registered,
+ "last_seen", hi->lastseen,
+ "infoline", hi->infoline ? hi->infoline : "",
+ "email", hi->email_addr ? hi->email_addr : "",
+
+ "fakehost", hi->fakehost ? hi->fakehost : "",
+ "last_quit_host", hi->last_quit_host
+
+ /* TODO: */
+ /* users online authed to this account */
+ /* cookies */
+ /* nicks (nickserv nets only?) */
+ /* masks */
+ /* ignores */
+ /* channels */
+ );
+}
+
+PyDoc_STRVAR(emb_get_info__doc__,
+ "get_info() -> dict with general service setup information\n\n"
+ "The dictionary contains the nicks of the different services.");
+
+static PyObject*
+emb_get_info(UNUSED_ARG(PyObject *self), UNUSED_ARG(PyObject *args))
+{
+ /* return some info about the general setup
+ * of X3, such as what the chanserv's nickname
+ * is.
+ */
+
+
+ return Py_BuildValue("{s:s,s:s,s:s,s:s,s:s}",
+ "chanserv", chanserv? chanserv->nick : "ChanServ",
+ "nickserv", nickserv?nickserv->nick : "NickServ",
+ "opserv", opserv?opserv->nick : "OpServ",
+ "global", global?global->nick : "Global",
+ "spamserv", spamserv?spamserv->nick : "SpamServ");
+}
+
+PyDoc_STRVAR(emb_log_module__doc__,
+ "log_module(level, message)\n\n"
+ "Logs a message in the PY_LOG subsystem given a severity level and a message.");
+
+static PyObject*
+emb_log_module(UNUSED_ARG(PyObject *self), PyObject *args)
+{
+ /* a gateway to standard X3 logging subsystem.
+ * level is a value 0 to 9 as defined by the log_severity enum in log.h.
+ *
+ * for now, all logs go to the PY_LOG log. In the future this will change.
+ */
+ char *message;
+ int level;
+
+ if(!PyArg_ParseTuple(args, "is", &level, &message))
+ return NULL;
+
+ log_module(PY_LOG, level, "%s", message);
+
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+PyDoc_STRVAR(emb_kill__doc__,
+ "kill(servicenick, target, message)\n\n"
+ "Kills a given user.");
+
+static PyObject*
+emb_kill(UNUSED_ARG(PyObject* self), PyObject* args) {
+ char const* from_nick, *target_nick, *message;
+ struct userNode *target;
+ struct service *service;
+
+ if (!PyArg_ParseTuple(args, "sss", &from_nick, &target_nick, &message))
+ return NULL;
+
+ if(!(service = service_find(from_nick))) {
+ PyErr_SetString(PyExc_Exception, "unknown service user specified as from user");
+ return NULL;
+ }
+
+ if ((target = GetUserH(target_nick)) == NULL) {
+ PyErr_SetString(PyExc_Exception, "unknown target user");
+ return NULL;
+ }
+
+ irc_kill(service->bot, target, message);
+
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+struct py_timeq_extra {
+ PyObject* func;
+ PyObject* arg;
+};
+
+static
+void py_timeq_callback(void* data) {
+ struct py_timeq_extra* extra = (struct py_timeq_extra*)data;
+
+ PyObject* retval = PyObject_Call(extra->func, extra->arg, NULL);
+ Py_XDECREF(retval);
+
+ Py_DECREF(extra->func);
+ Py_DECREF(extra->arg);
+}
+
+PyDoc_STRVAR(emb_timeq_add__doc__,
+ "timeq_add(when, function, args)\n\n"
+ "Adds a callback to the service timer system.\n\n"
+ "The specific function must be callable, and the specified arguments must be\n"
+ "a tuple with the arguments that the function expects.");
+
+static PyObject*
+emb_timeq_add(UNUSED_ARG(PyObject* self), PyObject* args) {
+ time_t when;
+ PyObject* func, *arg;
+ struct py_timeq_extra* extra;
+
+ if (!PyArg_ParseTuple(args, "lOO", &when, &func, &arg))
+ return NULL;
+
+ if (!PyFunction_Check(func)) {
+ PyErr_SetString(PyExc_Exception, "first argument must be a function");
+ return NULL;
+ }
+
+ if (!PyTuple_Check(arg)) {
+ PyErr_SetString(PyExc_Exception, "second argument must be a tuple");
+ return NULL;
+ }
+
+ extra = malloc(sizeof(struct py_timeq_extra));
+ if (extra == NULL) {
+ PyErr_SetString(PyExc_Exception, "out of memory");
+ return NULL;
+ }
+
+ Py_INCREF(func);
+ Py_INCREF(arg);
+
+ extra->func = func;
+ extra->arg = arg;
+
+ timeq_add(when, py_timeq_callback, (void*)extra);
+
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+PyDoc_STRVAR(emb_timeq_del__doc__,
+ "timeq_del(when)\n\n"
+ "This function deletes all python-added callbacks registered to run at the\n"
+ "given time, regardless of their data. This is due to the unnecessary extra\n"
+ "burden it would require to get the same data for multiple runs.");
+
+static PyObject*
+emb_timeq_del(UNUSED_ARG(PyObject* self), PyObject* args) {
+ time_t when;
+
+ if (!PyArg_ParseTuple(args, "l", &when))
+ return NULL;
+
+ timeq_del(when, py_timeq_callback, NULL, TIMEQ_IGNORE_DATA);
+
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+static int pyobj_config_make_dict(char const* key, void* data_, void* extra) {
+ struct record_data* data = (struct record_data*)data_;
+ PyObject* dict = (PyObject*)extra;
+ PyObject* value = NULL, *tmp;
+ size_t n, idx;
+ int success;
+
+ switch (data->type) {
+ case RECDB_QSTRING:
+ value = PyString_FromString(data->d.qstring);
+ break;
+
+ case RECDB_STRING_LIST:
+ value = PyList_New(data->d.slist->used);
+ if (value == NULL)
+ break;
+
+ success = 1;
+ for (n = 0; n < data->d.slist->used; ++n) {
+ tmp = PyString_FromString(data->d.slist->list[n]);
+ if (tmp == NULL) {
+ success = 0;
+ break;
+ }
+
+ if (PyList_SetItem(value, n, tmp)) {
+ Py_DECREF(tmp);
+ success = 0;
+ break;
+ }
+ }
+ if (!success) {
+ for (idx = 0; idx < n; ++idx) {
+ tmp = PyList_GET_ITEM(value, idx);
+ Py_DECREF(tmp);
+ PyList_SET_ITEM(value, idx, NULL);
+ }
+ Py_DECREF(value);
+ value = NULL;
+ }
+ break;
+
+ case RECDB_OBJECT:
+ value = PyDict_New();
+ if (value == NULL)
+ break;
+
+ if (dict_foreach(data->d.object, pyobj_config_make_dict, (void*)value) != NULL) {
+ PyDict_Clear(value);
+ value = NULL;
+ break;
+ }
+
+ break;
+
+ default:
+ Py_INCREF(Py_None);
+ value = Py_None;
+ }
+
+ if (value == NULL)
+ return 1;
+
+ if (PyDict_SetItemString(dict, key, value))
+ return 1;
+
+ return 0;
+}
+
+PyDoc_STRVAR(emb_get_config__doc__,
+ "get_config() -> dict with config elements and values\n\n"
+ "Updates to the returned dictionary will not reflect in the service's\n"
+ "configuration.");
+
+static PyObject*
+emb_get_config(UNUSED_ARG(PyObject* self), PyObject* args) {
+ PyObject* dict;
+
+ if (!PyArg_ParseTuple(args, ""))
+ return NULL;
+
+ dict = PyDict_New();
+ if (dict == NULL)
+ return NULL;
+
+ if (conf_enum_root(pyobj_config_make_dict, (void*)dict) != NULL) {
+ PyDict_Clear(dict);
+ PyErr_SetString(PyExc_Exception, "unable to iterate config");
+ return NULL;
+ }
+
+ return dict;
+}
+
+PyDoc_STRVAR(emb_kick__doc__,
+ "kick(who, target, message)\n\n"
+ "Kicks a given target as if the who user kicked them using the given message.");
+
+static PyObject* emb_kick(UNUSED_ARG(PyObject* self), PyObject* args) {
+ struct userNode* who, *target;
+ struct chanNode* channel;
+ char const* msg;
+
+ char const* who_s, *target_s, *channel_s;
+
+ if (!PyArg_ParseTuple(args, "ssss", &who_s, &target_s, &channel_s, &msg))
+ return NULL;
+
+ if ((who = GetUserH(who_s)) == NULL) {
+ PyErr_SetString(PyExc_Exception, "no such user");
+ return NULL;
+ }
+
+ if ((target = GetUserH(target_s)) == NULL) {
+ PyErr_SetString(PyExc_Exception, "no such target");
+ return NULL;
+ }
+
+ if ((channel = GetChannel(channel_s)) == NULL) {
+ PyErr_SetString(PyExc_Exception, "no such channel");
+ return NULL;
+ }
+
+ irc_kick(who, target, channel, msg);
+
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+PyDoc_STRVAR(emb_channel_mode__doc__,
+ "channel_mode(who, channel, modes)\n\n"
+ "Lets a current server's user set a specified channel's modes as specified.");
+
+static PyObject* emb_channel_mode(UNUSED_ARG(PyObject* self_), PyObject* args) {
+ struct userNode* who;
+ struct chanNode* channel;
+ char const* modes;
+
+ char const* who_s, *channel_s;
+
+ if (!PyArg_ParseTuple(args, "sss", &who_s, &channel_s, &modes))
+ return NULL;
+
+ if ((who = GetUserH(who_s)) == NULL) {
+ PyErr_SetString(PyExc_Exception, "unknown user");
+ return NULL;
+ }
+
+ if (who->uplink != self) {
+ PyErr_SetString(PyExc_Exception, "user not on current server");
+ return NULL;
+ }
+
+ if ((channel = GetChannel(channel_s)) == NULL) {
+ PyErr_SetString(PyExc_Exception, "unknown channel");
+ return NULL;
+ }
+
+ irc_mode(who, channel, modes);
+
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+PyDoc_STRVAR(emb_user_mode__doc__,
+ "user_mode(target, modes)\n\n"
+ "Sets target's modes as specified. The modes are in normal +f-n syntax.");
+
+static PyObject* emb_user_mode(UNUSED_ARG(PyObject* self), PyObject* args) {
+ struct userNode* target;
+ char const* modes;
+
+ char const* target_s;
+
+ if (!PyArg_ParseTuple(args, "ss", &target_s, &modes))
+ return NULL;
+
+ if ((target = GetUserH(target_s)) == NULL) {
+ PyErr_SetString(PyExc_Exception, "unknown user");
+ return NULL;
+ }
+
+ irc_umode(target, modes);
+
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+PyDoc_STRVAR(emb_fakehost__doc__,
+ "fakehost(target, host)\n\n"
+ "Sets the fakehost of a given user to the specified host.");
+
+static PyObject* emb_fakehost(UNUSED_ARG(PyObject* self), PyObject* args) {
+ struct userNode* target;
+ char const* host;
+
+ char const* target_s;
+
+ if (!PyArg_ParseTuple(args, "ss", &target_s, &host))
+ return NULL;
+
+ if ((target = GetUserH(target_s)) == NULL) {
+ PyErr_SetString(PyExc_Exception, "unknown user");
+ return NULL;
+ }
+
+ irc_fakehost(target, host);
+
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+PyDoc_STRVAR(emb_svsnick__doc__,
+ "svsnick(from, target, newnick)\n\n"
+ "The from nick must be on the service server.");
+
+static PyObject*
+emb_svsnick(UNUSED_ARG(PyObject* self_), PyObject* args) {
+ struct userNode* from, *target;
+ const char* newnick;
+
+ const char* from_s, *target_s;
+
+ if (!PyArg_ParseTuple(args, "sss", &from_s, &target_s, &newnick))
+ return NULL;
+
+ if ((from = GetUserH(from_s)) == NULL) {
+ PyErr_SetString(PyExc_Exception, "unknown from user");
+ return NULL;
+ }
+
+ if ((target = GetUserH(target_s)) == NULL) {
+ PyErr_SetString(PyExc_Exception, "unknown target user");
+ return NULL;
+ }
+
+ if (from->uplink != self) {
+ PyErr_SetString(PyExc_Exception, "from user is not on service server");
+ return NULL;
+ }
+
+ irc_svsnick(from, target, newnick);
+
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+PyDoc_STRVAR(emb_svsquit__doc__,
+ "svsquit(from, target, reason)\n\n"
+ "The from user must be on the service server.");
+
+static PyObject*
+emb_svsquit(UNUSED_ARG(PyObject* self_), PyObject* args) {
+ struct userNode* from, *target;
+ char const* reason;
+
+ char const* from_s, *target_s;
+
+ if (!PyArg_ParseTuple(args, "sss", &from_s, &target_s, &reason))
+ return NULL;
+
+ if ((from = GetUserH(from_s)) == NULL) {
+ PyErr_SetString(PyExc_Exception, "unknown from user");
+ return NULL;
+ }
+
+ if (from->uplink != self) {
+ PyErr_SetString(PyExc_Exception, "from user is not on service server");
+ return NULL;
+ }
+
+ if ((target = GetUserH(target_s)) == NULL) {
+ PyErr_SetString(PyExc_Exception, "unknown target user");
+ return NULL;
+ }
+
+ irc_svsquit(from, target, reason);
+
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+PyDoc_STRVAR(emb_svsjoin__doc__,
+ "svsjoin(from, target, to)\n\n"
+ "From user from must a user on the service server.\n"
+ "To must be an existing channel name.");
+
+static PyObject*
+emb_svsjoin(UNUSED_ARG(PyObject* self_), PyObject* args) {
+ struct userNode* from, *target;
+ struct chanNode* to;
+
+ const char* from_s, *target_s, *to_s;
+
+ if (!PyArg_ParseTuple(args, "sss", &from_s, &target_s, &to_s))
+ return NULL;
+
+ if ((from = GetUserH(from_s)) == NULL) {
+ PyErr_SetString(PyExc_Exception, "unknown from user");
+ return NULL;
+ }
+
+ if (from->uplink != self) {
+ PyErr_SetString(PyExc_Exception, "from user is not on service server");
+ return NULL;
+ }
+
+ if ((target = GetUserH(target_s)) == NULL) {
+ PyErr_SetString(PyExc_Exception, "unknown target user");
+ return NULL;
+ }
+
+ if ((to = GetChannel(to_s)) == NULL)
+ to = AddChannel(to_s, now, NULL, NULL, NULL);
+
+ irc_svsjoin(from, target, to);
+
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+PyDoc_STRVAR(emb_adduser__doc__,
+ "adduser(nick, ident, hostname, description, modes) -> dict with user information\n\n"
+ "Adds a new local user with the given information.");
+
+static PyObject*
+emb_adduser(UNUSED_ARG(PyObject* self_), PyObject* args) {
+ char const* nick, *ident, *hostname, *desc, *modes;
+ struct userNode* user;
+ PyObject* retval;
+
+ if (!PyArg_ParseTuple(args, "sssss", &nick, &ident, &hostname, &desc, &modes))
+ return NULL;
+
+ user = AddLocalUser(nick, ident, hostname, desc, modes);
+
+ retval = pyobj_from_usernode(user);
+
+ return retval;
+}
+
+/* TODO: Add the rest of the service members to the dict */
+static PyObject*
+pyobj_from_service(struct service* serv) {
+ PyObject* bot, *retval;
+
+ bot = pyobj_from_usernode(serv->bot);
+ if (bot == NULL)
+ goto cleanup;
+
+ retval = Py_BuildValue("{s:O,s:c,s:I}",
+ "bot", bot,
+ "trigger", serv->trigger,
+ "privileged", serv->privileged);
+ if (retval == NULL)
+ goto cleanup;
+
+ return retval;
+
+cleanup:
+ Py_XDECREF(bot);
+ return NULL;
+}
+
+PyDoc_STRVAR(emb_service_register__doc__,
+ "service_register(nick)\n\n"
+ "Registers nick as a service. The specified nick must be on the local server.");
+
+static PyObject*
+emb_service_register(UNUSED_ARG(PyObject* self_), PyObject* args) {
+ struct userNode* user;
+ char const* user_s;
+
+ if (!PyArg_ParseTuple(args, "s", &user_s))