get user's highest permission in discord.py - discord

#destiny.command(aliases=["ui, whois"])
async def userinfo(ctx, user: discord.Member=None):
if user == None:
user = ctx.author
boosted = time.strftime("%B %d, %Y %I-:%M %p", user.premium_since.utctimetuple())
created = time.strftime("%B %d, %Y %I-:%M %p", user.created_at.utctimetuple())
joined = time.strftime("%B %d, %Y %I-:%M %p", user.joined_at.utctimetuple())
members = sorted(ctx.guild.members, key=lambda m: m.joined_at)
joinpos = str(members.index(user)+1)
i want to get the highest permission of {user} (highestperm =) but dont know how

' '.join([str(p[0]).title() for p in user.guild_permissions]).lower().split()[0]

Related

How do I manipulate the values to a pointer to a struct

It's a function that makes events based off the time values the user enters, it then converts the value to Unix Epoch using mktime and stores them to a file. I've tested and found that everything else works well until we reach the section where the user enters the values seems like I'm not using the pointer event to the struct tm well, so I need help in doing it right.
int EventCreator(){
FILE *fptr = fopen("Events_Calendar.txt", "a+");
struct tm *event;
printf("Enter the values of the hour minute day month year in digits and this specific format meaning using spaces to seperate them");
printf("\n hr min day mon yr\n>");
scanf("%d %d %d %d %d", event->tm_hour, event->tm_min, event->tm_mday, event->tm_mon, event->tm_year);
time_t NineteenEvent = mktime(event);
printf("%ld", NineteenEvent);
fprintf(fptr, "%ld\n", NineteenEvent);
fclose(fptr);
}
You have declared a pointer to a struct tm but you haven't allocated memory for a struct tm. I suggest that you make event an automatic variable instead.
Example:
void EventCreator() { // make the function `void` since you do not return anything
FILE *fptr = fopen("Events_Calendar.txt", "a+");
if (fptr) { // check that opening the file succeeds
// `event` is now not a pointer and set `tm_isdst` to a negative value
// to make `mktime` guess if DST is in effect:
struct tm event = {.tm_isdst = -1};
printf(
"Enter the values of the hour minute day month year in digits and "
"this specific format meaning using spaces to seperate them\n"
"hr min day mon yr\n>");
// %d requires an `int*` but you've supplied it with `int`.
// (also, check that scanf succeeds)
if (scanf("%d %d %d %d %d", &event.tm_hour, &event.tm_min,
&event.tm_mday, &event.tm_mon, &event.tm_year) == 5) {
event.tm_year -= 1900; // years since 1900
--event.tm_mon; // 0-11
time_t NineteenEvent = mktime(&event); // take its address here
printf("%ld", NineteenEvent);
fprintf(fptr, "%ld\n", NineteenEvent);
fclose(fptr);
}
}
}

Python C-API… how to write python code in C

I have the following python code… and want to write this Code in… Python-C-API
from enum import Enum
class MqWaitOnEventE(Enum):
NO = 0
ONCE = 1
FOREVER = 2
Thanks for help.
FIRST approach
// enum definition
PyObject *enumModO=NULL, *enumDictO=NULL, *intEnumO=NULL;
LngErrorCheckN(enumModO = PyImport_ImportModule("enum"));
LngErrorCheckN(enumDictO = PyModule_GetDict(enumModO));
LngErrorCheckN(intEnumO = PyDict_GetItemString(enumDictO,"IntEnum"));
// get 'MqWaitOnEventE '
PyObject *dictMqWaitOnEventE = NULL, *result=NULL, *base=NULL;
LngErrorCheckN(dictMqWaitOnEventE = PyDict_New());
LngErrorCheck(PyDict_SetItemString (dict, "__module__", PyUnicode_FromString("pymsgque")));
LngErrorCheck(PyDict_SetItemString (dict, "NO", OT_NEW_INT_OBJ(MQ_WAIT_NO)));
LngErrorCheck(PyDict_SetItemString (dict, "ONCE", OT_NEW_INT_OBJ(MQ_WAIT_ONCE)));
LngErrorCheck(PyDict_SetItemString (dict, "FOREVER", OT_NEW_INT_OBJ(MQ_WAIT_FOREVER)));
LngErrorCheckN(base = PyTuple_Pack(1, intEnumO));
LngErrorCheckN(result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO","MqWaitOnEventE", base, dictMqWaitOnEventE));
end with following error…
Traceback (most recent call last):
File "…/MyServer.py", line 14, in <module>
import pymsgque
File "…/python3.7/enum.py", line 151, in __new__
enum_members = {k: classdict[k] for k in classdict._member_names}
AttributeError: 'dict' object has no attribute '_member_names'
what is this? … does python require som internals from me ?
This is the type-safe solution for a enum in C-Module initialization file…
MqBuffer… is my string buffer class
NS(…) is a macro to create a unique namespace… I use this for all c-extern objects
.
the example will create the new MqSlaveE class in the module namespace.
PyObject* m = PyModule_Create(&ModuleDef);
...
MQ_BUF buf = MqBufferCreate(1000);
MqBufferAppendC(buf,"from enum import IntEnum\n");
MqBufferAppendC(buf,"class MqSlaveE(IntEnum):\n");
MqBufferAppendV(buf," LOOPBACK = %i\n", MQ_SLAVE_LOOPBACK);
MqBufferAppendV(buf," OTHER = %i\n", MQ_SLAVE_OTHER);
MqBufferAppendV(buf," FILTER = %i\n", MQ_SLAVE_FILTER);
MqBufferAppendV(buf," MASTER = %i\n", MQ_SLAVE_MASTER);
MqBufferAppendV(buf," USER = %i\n", MQ_SLAVE_USER);
PyObject *g, *dl, *dg, *v;
MQ_CST script = MqBufferGetC_e(buf);
g = PyImport_AddModule("__main__");
dg = PyModule_GetDict(g);
dl = PyModule_GetDict(m);
LngErrorCheckN(v = PyRun_String(script, Py_file_input, dg, dl));
Py_DECREF(v);
NS(MqSlaveE) = PyDict_GetItemString(dl,"MqSlaveE");
MqBufferDelete(&buf);
return m;
to check the object and extract the value I use a proc…
enum MqErrorE NS(Get_Enum_FromObj) (PyObject *enumT, PyObject *enumE, MQ_INT *ret) {
if (!PyObject_IsInstance(enumE, enumT)) {
PyErr_Format(PyExc_TypeError,
"input_enum_type = '%s' is not the required_enum_type = '%s'",
Py_TYPE(enumE)->tp_name, ((PyTypeObject*)enumT)->tp_name
);
return MQ_ERROR;
}
return PyObj_AsINT(enumE,ret);
}

Can anyone explain this C password hashing algorithm?

I am trying to replicate the below code in php to have a web based account creation script but I do not really understand what is happening before the md5 hash.
This is the code:
reg_seconds = (unsigned) regtime / 3600L;
ch = strlen (&password[0]);
_itoa (reg_seconds, &config_data[0], 10 );
//Throw some salt in the game ;)
sprintf (&password[ch], "_%s_salt", &config_data[0] );
//printf ("New password = %s\n", password );
MDString (&password[0], &MDBuffer[0] );
for (ch=0;ch<16;ch++)
sprintf (&md5password[ch*2], "%02x", (unsigned char) MDBuffer[ch]);
md5password[32] = 0;
When using the password "password" and a regtime of "399969" I get a hashed password of "9a5c041c5b37febc90ad3dc66ec62c83"
Could anyone explain what exactly is happening and what the final string is that gets hashed?
Ok let's look line by line, assuming password = "password" and regtime = 399969
reg_seconds = (unsigned) regtime / 3600L;
=> reg_seconds = 111 NB incoherent with the name, isn't is regtime % 3600L
ch = strlen (&password[0]);
=> ch = 8
_itoa (reg_seconds, &config_data[0], 10 );
=> config_data = "111"
//Throw some salt in the game ;)
sprintf (&password[ch], "_%s_salt", &config_data[0] );
=> password = "password_111_salt"
//printf ("New password = %s\n", password ); Why did not you uncomment this ?
MDString (&password[0], &MDBuffer[0] );
MDBuffer receives the binary hash
for (ch=0;ch<16;ch++)
sprintf (&md5password[ch*2], "%02x", (unsigned char) MDBuffer[ch]);
md5password[32] = 0;
md5password receives the hex encoded hash : "033f7d591eda915e708571edd255b511"
Oups it is not the expected hash !
Because 399969 is not regtime but is reg_seconds in above code... So
password = "password_399969_salt"
md5password = "9a5c041c5b37febc90ad3dc66ec62c83"

C Struct values not being assigned

I'm trying to use the Video4Linux2 API, but something peculiar is happening with the structs that I'm supposed to use to change the various controls on a given camera. For some reason, some of the members aren't reporting back as changed on assignment. I wrote the following code to simplify the problem:
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <linux/videodev2.h>
#include <stdlib.h>
#include <errno.h>
#include <stdbool.h>
int setExtendedControl(int id, int value);
int fd = 0;
int main()
{
setExtendedControl(20, 30);
return 0;
}
int setExtendedControl(int id, int value)
{
struct v4l2_ext_control extControl;
struct v4l2_ext_controls extControls;
extControl.id = id;
extControl.value = value;
extControl.value64 = value;
extControl.reserved2[0] = 0;
extControl.reserved2[1] = 0;
extControl.reserved2[2] = 0;
//Put the individual control structure into the
//multi-control container structure and initialize the container
//as well
extControls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
extControls.count = 1;
extControls.controls = &extControl;
printf("extControls.controls = %i, extControl = %i\n", (extControls).controls, &extControl);
extControls.reserved[0] = 0;
extControls.reserved[1] = 0;
extControls.reserved[2] = 0;
extControls.error_idx = 0;
printf("Verifying settings:\n");
printf("extControl.id = %i, id = %i\n", extControl.id, id);
printf("extControl.value = %i, value = %i\n", extControl.value, value);
printf("extControl.value64 = %i, value = %i\n", extControl.value64, value);
printf("extControl.reserved2[0] = %i, set to 0\n", extControl.reserved2[0]);
printf("extControl.reserved2[1] = %i, set to 0\n", extControl.reserved2[1]);
printf("extControl.reserved2[2] = %i, set to 0\n\n", extControl.reserved2[2]);
printf("extControls.ctrl_class = %i, V4L2_CTRL_CLASS_MPEG = %i\n", extControls.ctrl_class,
V4L2_CTRL_CLASS_MPEG);
printf("extControls.count = %i, set to 1\n", extControls.count);
printf("extControls.reserved[0] = %i, set to 0\n", extControls.reserved[0]);
printf("extControls.reserved[1] = %i, set to 0\n", extControls.reserved[1]);
printf("extControls.reserved[2] = %i, set to 0\n", extControls.reserved[2]);
printf("extControls.error_idx = %i, set to 0\n", extControls.error_idx);
printf ("\nRunning secondary check..\n\n");
int rval;
//Set up the individual control structure
//Try to change the control and return the
//value reporting the outcome.
rval = ioctl(fd, VIDIOC_S_EXT_CTRLS, &extControls);
if (extControls.controls != &extControl)
{
printf("\n\nLost the pointer on initial set!\n");
}
//printf("error_idx after initial set %i\n", extControls.error_idx);
//printf("extControl = %i, extControls = %i\n");
//freeStructs(extControl, extControls);
return rval;
}
When I run this, the "value" and "value64" members aren't set. The following is the printf outputs:
extControls.controls = -1954893344, extControl = -1954893344
Verifying settings:
extControl.id = 20, id = 20
extControl.value = 0, value = 30
extControl.value64 = 0, value = 30
extControl.reserved2[0] = 0, set to 0
extControl.reserved2[1] = 0, set to 0
extControl.reserved2[2] = 0, set to 0
extControls.ctrl_class = 10027008, V4L2_CTRL_CLASS_MPEG = 10027008
extControls.count = 1, set to 1
extControls.reserved[0] = 0, set to 0
extControls.reserved[1] = 0, set to 0
extControls.reserved[2] = 0, set to 0
extControls.error_idx = 0, set to 0
And here is a small snippet from gdb:
(gdb) step
printf (__fmt=0x400880 "extControls.controls = %i, extControl = %i\n") at /usr/include/x86_64-linux-gnu/bits/stdio2.h:105
105 return __printf_chk (__USE_FORTIFY_LEVEL - 1, __fmt, __va_arg_pack ());
(gdb) step
setExtendedControl (id=20, value=30) at ioctlTest.c:30
30 extControl.reserved2[0] = 0;
(gdb) step
Hardware watchpoint 2: extControl
Old value =
{id = 4294960502, size = 32767, reserved2 = {4196309}, {value = 0, value64 = 67750802696962048, string = 0xf0b2ff00000000 <Address 0xf0b2ff00000000 out of bounds>}}
New value =
{id = 20, size = 32767, reserved2 = {0}, {value = 0, value64 = 67750802696962048, string = 0xf0b2ff00000000 <Address 0xf0b2ff00000000 out of bounds>}}
It's not happening here, but there have been other instances in a beefier version of this code where the pointer assignment for extControls.controls and int assignment of member extControl.id both fail. What would cause a struct member assignment failure of this nature?
Thanks in advance!
Can't check this myself, but looking at this spec of struct v4l2_ext_control (Table 1), it says:
Table 1. struct v4l2_ext_control
__u32 id Identifies the control, set by the application.
__u32 reserved2[2] Reserved for future extensions. Drivers and applications must set the array to zero.
union (anonymous)
__s32 value New value or current value.
__s64 value64 New value or current value.
void * reserved Reserved for future pointer-type controls. Currently unused.
Note three things here:
value and value64 are part of a union, they occupy the same location in memory
reserved2 has size two, so only, reserved2[0], reserved2[1], no reserved2[2] !
according to this spec, reserved memory address precedes the union within the struct
This means that when you set
extControl.reserved2[2] = 0;
you are actually writing outside the memory assigned to that array, overwriting the data of the union, thus altering "both" value and value64 members at the same time.
See kernel docs for struct information
The reserved[] array for the v4l2_ext_control struct is only of length 2...so you're modifying memory that you shouldn't be with the line:
extControl.reserved2[2] = 0;

How to check if pid belongs to current user session?

I can get the list of running process from the this source code on mac.
Now, I want to filter these processes for different users or at least for current user session.
You can just extend your code like this..
kinfo_proc *mylist;
size_t mycount = 0;
mylist = (kinfo_proc *)malloc(sizeof(kinfo_proc));
GetBSDProcessList(&mylist, &mycount);
char *user = getenv("USER");
for (int i = 0; i < mycount; i++)
{
uid_t uid = mylist[i].kp_eproc.e_pcred.p_ruid;
struct passwd * pwd = getpwuid(uid);
char * username = pwd->pw_name;
if(strcmp(username, user) == 0)
{
printf(" %d - %s \n", mylist[i].kp_proc.p_pid, mylist[i].kp_proc.p_comm);
}
}
To be more precise you can get username buy this technique
SCDynamicStoreRef store;
store = SCDynamicStoreCreate(NULL, CFSTR("com.apple.dts.ConsoleUser"), NULL, NULL);
CFStringRef currentConsoleUser = CopyCurrentConsoleUsername(store);
const int kBufferSize = 256;
char logedinusername[kBufferSize];
CFStringGetCString(currentConsoleUser,logedinusername,kBufferSize,kCFStringEncodingMacRoman);
as getenv("USER"); may not work if you are running as root user and want logged in user.

Resources