00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef lint
00021 static const char rcsid[] = "$Id: myegguser.c,v 1.1 2006-01-05 20:42:42 sven Exp $";
00022 #endif
00023
00024 #include <Python.h>
00025 #include <eggdrop/eggdrop.h>
00026
00027 #include "pythonscript.h"
00028
00029 #define Return_None do {\
00030 Py_INCREF(Py_None);\
00031 return Py_None;\
00032 } while (0)
00033
00034 #define CheckSelf user_t *user;\
00035 EgguserObject *O = (EgguserObject *) self;\
00036 user = O->user;\
00037 if (!user || (user->flags & USER_DELETED)) {\
00038 PyErr_SetString(PyExc_RuntimeError, "This user has been deleted.");\
00039 return 0;\
00040 }
00041
00042 static PyObject *Egguser_New(PyTypeObject *Type, PyObject *args, PyObject *kwds) {
00043 user_t *user = 0;
00044 PyObject *Para;
00045 EgguserObject *Egguser;
00046
00047 if (args) {
00048 if (!PyArg_ParseTuple(args, "O", &Para)) return 0;
00049 if (Egguser_Check(Para)) {
00050 Egguser = (EgguserObject *) Para;
00051 user = Egguser->user;
00052 } else if (PyInt_Check(Para)) {
00053 user = user_lookup_by_uid(PyInt_AsLong(Para));
00054 } else if (PyString_Check(Para)) {
00055 user = user_lookup_by_handle(PyString_AsString(Para));
00056 } else {
00057 PyErr_Format(PyExc_TypeError, "Expected eggdrop.egguser or int or str got %s.", Para->ob_type->tp_name);
00058 return 0;
00059 }
00060 if (!user) {
00061 PyErr_SetString(PyExc_RuntimeError, "No such user.");
00062 return 0;
00063 }
00064 }
00065 if (!(Egguser = (EgguserObject *) Type->tp_alloc(Type, 0))) return 0;
00066 Egguser->user = user;
00067 return (PyObject *) Egguser;
00068 }
00069
00070 static PyObject *Repr(PyObject *self) {
00071 EgguserObject *Obj = (EgguserObject *) self;
00072 user_t *user = Obj->user;
00073
00074
00075
00076 return PyString_FromFormat("eggdrop.egguser(%d)", user->uid);
00077 }
00078
00079 static int Compare(PyObject *self, PyObject *other) {
00080 user_t *u2;
00081 EgguserObject *O2 = (EgguserObject *) other;
00082 CheckSelf
00083
00084 if (!Egguser_Check(other)) return -1;
00085 u2 = O2->user;
00086 if (!u2 || (u2->flags & USER_DELETED)) {
00087 PyErr_SetString(PyExc_RuntimeError, "This user has been deleted.");
00088 return -1;
00089 }
00090 return user->uid < u2->uid ? -1 : user->uid != u2->uid;
00091 }
00092
00093 static long Hash(PyObject *self) {
00094 CheckSelf
00095
00096 return user->uid * 15485863;
00097 }
00098
00099 static PyObject *GetUid(PyObject *self, void *ignored) {
00100 CheckSelf
00101
00102 return PyInt_FromLong(user->uid);
00103 }
00104
00105 static PyObject *GetHandle(PyObject *self, void *ignored) {
00106 CheckSelf
00107
00108 return PyString_FromString(user->handle);
00109 }
00110
00111 static PyObject *GetIrcMasks(PyObject *self, void *ignored) {
00112 script_var_t retval;
00113 CheckSelf
00114
00115 retval.type = SCRIPT_ARRAY | SCRIPT_STRING;
00116 retval.len = user->nircmasks;
00117 retval.value = user->ircmasks;
00118 return c_to_python_var(&retval);
00119 }
00120
00121 static PyObject *Delete(PyObject *self, PyObject *na) {
00122 CheckSelf
00123
00124 user_delete(user);
00125 Return_None;
00126 }
00127
00128 static PyObject *AddMask(PyObject *self, PyObject *args) {
00129 const char *mask;
00130 CheckSelf
00131
00132 if (!PyArg_ParseTuple(args, "s", &mask)) return 0;
00133 user_add_ircmask(user, mask);
00134 Return_None;
00135 }
00136
00137 static PyObject *DelMask(PyObject *self, PyObject *args) {
00138 const char *mask;
00139 CheckSelf
00140
00141 if (!PyArg_ParseTuple(args, "s", &mask)) return 0;
00142 if (user_del_ircmask(user, mask)) {
00143 PyErr_Format(PyExc_RuntimeError, "Could not delete IRC mask %s from user %s: No such mask.", mask, user->handle);
00144 return 0;
00145 }
00146 Return_None;
00147 }
00148
00149 static PyObject *Get(PyObject *self, PyObject *args) {
00150 const char *setting, *chan = 0;
00151 script_var_t retval;
00152 CheckSelf
00153
00154 if (!PyArg_ParseTuple(args, "s|s", &setting, &chan)) return 0;
00155 if (user_get_setting(user, chan, setting, (char **) &retval.value)) {
00156 PyErr_Format(PyExc_RuntimeError, "Could not get setting %s from user %s.", setting, user->handle);
00157 return 0;
00158 }
00159 retval.type = SCRIPT_STRING;
00160 retval.len = -1;
00161 return c_to_python_var(&retval);
00162 }
00163
00164 static PyObject *Set(PyObject *self, PyObject *args) {
00165 const char *setting, *value, *chan = 0;
00166 CheckSelf
00167
00168 if (!PyArg_ParseTuple(args, "ss|s", &setting, &value, &chan)) return 0;
00169 user_set_setting(user, chan, setting, value);
00170 Return_None;
00171 }
00172
00173 static PyObject *GetFlags(PyObject *self, PyObject *args) {
00174 const char *chan = 0;
00175 flags_t flags;
00176 char flagstr[64];
00177 script_var_t retval;
00178 CheckSelf
00179
00180 if (!PyArg_ParseTuple(args, "|s", &chan)) return 0;
00181
00182 if (user_get_flags(user, chan, &flags)) {
00183 PyErr_Format(PyExc_RuntimeError, "Could not get flags for user %s.", user->handle);
00184 return 0;
00185 }
00186 flag_to_str(&flags, flagstr);
00187 retval.type = SCRIPT_STRING;
00188 retval.value = flagstr;
00189 retval.len = -1;
00190 return c_to_python_var(&retval);
00191 }
00192
00193 static PyObject *SetFlags(PyObject *self, PyObject *args) {
00194 const char *chan = 0, *flags;
00195 CheckSelf
00196
00197 if (!PyArg_ParseTuple(args, "s|s", &flags, &chan)) return 0;
00198
00199 user_set_flags_str(user, chan, flags);
00200 Return_None;
00201 }
00202
00203 static PyObject *MatchFlags(PyObject *self, PyObject *args) {
00204 const char *chan = 0, *flags;
00205 flags_t flags_left, flags_right;
00206 int r = 0;
00207 CheckSelf
00208
00209 if (!PyArg_ParseTuple(args, "s|s", &flags, &chan)) return 0;
00210 if (user_get_flags(user, chan, &flags_right)) {
00211 PyErr_Format(PyExc_RuntimeError, "Could not get flags for user %s.", user->handle);
00212 return 0;
00213 }
00214 flag_from_str(&flags_left, flags);
00215 r = flag_match_subset(&flags_left, &flags_right);
00216 return PyInt_FromLong(r);
00217 }
00218
00219 static PyObject *MatchFlagsOr(PyObject *self, PyObject *args) {
00220 const char *chan = 0, *flags;
00221 flags_t flags_left, flags_right;
00222 int r = 0;
00223 CheckSelf
00224
00225 if (!PyArg_ParseTuple(args, "s|s", &flags, &chan)) return 0;
00226 if (user_get_flags(user, chan, &flags_right)) {
00227 PyErr_Format(PyExc_RuntimeError, "Could not get flags for user %s.", user->handle);
00228 return 0;
00229 }
00230 flag_from_str(&flags_left, flags);
00231 r = flag_match_partial(&flags_left, &flags_right);
00232 return PyInt_FromLong(r);
00233 }
00234
00235 static PyObject *HasPass(PyObject *self, PyObject *args) {
00236 CheckSelf
00237
00238 return PyInt_FromLong(user_has_pass(user));
00239 }
00240
00241 static PyObject *CheckPass(PyObject *self, PyObject *args) {
00242 const char *pass;
00243 CheckSelf
00244
00245 if (!PyArg_ParseTuple(args, "s", &pass)) return 0;
00246 return PyInt_FromLong(user_check_pass(user, pass));
00247 }
00248
00249 static PyObject *SetPass(PyObject *self, PyObject *args) {
00250 const char *pass = 0;
00251 CheckSelf
00252
00253 if (!PyArg_ParseTuple(args, "|s", &pass)) return 0;
00254 user_set_pass(user, pass);
00255 Return_None;
00256 }
00257
00258 static PyMethodDef Methods[] = {
00259 {"delete", Delete, METH_NOARGS, "This method will delete this user and all his data. "
00260 "If he is currently on the partyline he will be booted. "
00261 "Useing this object or any other object referring to the same user "
00262 "after calling this method will raise an exception."},
00263 {"addmask", AddMask, METH_VARARGS, "Adds an IRC hostmask to this user."},
00264 {"delmask", DelMask, METH_VARARGS, "Deletes an IRC hostmask from this user."},
00265 {"get", Get, METH_VARARGS, ""},
00266 {"set", Set, METH_VARARGS, ""},
00267 {"getflags", GetFlags, METH_VARARGS, ""},
00268 {"setflags", SetFlags, METH_VARARGS, ""},
00269 {"matchflags", MatchFlags, METH_VARARGS, ""},
00270 {"matchflags_or", MatchFlagsOr, METH_VARARGS, ""},
00271 {"haspass", HasPass, METH_NOARGS, ""},
00272 {"checkpass", CheckPass, METH_VARARGS, ""},
00273 {"setpass", SetPass, METH_VARARGS, ""},
00274 {0}
00275 };
00276
00277 static PyGetSetDef GetSeter[] = {
00278 {"uid", GetUid, 0, 0, 0},
00279 {"handle", GetHandle, 0, 0, 0},
00280 {"ircmasks", GetIrcMasks, 0, 0, 0},
00281 {0}
00282 };
00283
00284 PyTypeObject Egguser_Type = {
00285 PyObject_HEAD_INIT(0)
00286 0,
00287 "eggdrop.egguser",
00288 sizeof(EgguserObject),
00289 0,
00290 0,
00291 0,
00292 0,
00293 0,
00294 Compare,
00295 Repr,
00296 0,
00297 0,
00298 0,
00299 Hash,
00300 0,
00301 0,
00302 0,
00303 0,
00304 0,
00305 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
00306 "an eggdrop user",
00307 0,
00308 0,
00309 0,
00310 0,
00311 0,
00312 0,
00313 Methods,
00314 0,
00315 GetSeter,
00316 0,
00317 0,
00318 0,
00319 0,
00320 0,
00321 0,
00322 0,
00323 Egguser_New
00324 };