Initial commit

This commit is contained in:
Alec Murphy
2017-05-28 15:54:53 -04:00
parent 6cb900e139
commit 30625ba3c1
4 changed files with 278 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
StrCpy(CROC_TOKEN,"");
StrCpy(CROC_CHAN,"");
+131
View File
@@ -0,0 +1,131 @@
// Crocodile: Discord client for TempleOS
#define CROC_GETAVATAR 0x21
#define CROC_GETCHAN 0x22
#define CROC_GETMSGS 0x23
#define CROC_SENDMSG 0x24
class CDiscordMsg {
U8 msg_id[64];
U8 timestamp[64];
U8 author[64];
U8 author_id[64];
U8 avatar[64];
U8 content[1024];
};
CDoc *discord_msgsbuf=DocNew;
CTask *discord_msgs=User;
CTask *discord_msgloop;
U8 CROC_CHAN[256];
U8 CROC_LMID[256];
U8 CROC_TOKEN[256];
U8 CROC_MSG[1024];
U0 CrocAvatar(CDoc *doc, U8 *author_id, U8 *avatar)
{
U8 *av_buffer=CAlloc(4096);
if (StrCmp(avatar,"")==0) { return; };
U8 av_trunc[64];
U8 av_file[64];
StrCpy(av_trunc,avatar);
av_trunc[12]=0;
StrPrint(av_file,"C:/Tmp/%s.bmp",av_trunc);
I64 size;
if (FileFind(av_file)==0)
{
ZeroParamBuf;
StrCpy(HGBD_PARAM_BUF,author_id);
StrCpy(HGBD_PARAM_BUF+64,avatar);
WriteParamBuf;
HGExec(CROC_GETAVATAR);
ReadParamBuf;
size=Str2I64(HGBD_PARAM_BUF);
BlkRead(HGBD,av_buffer,1,(size/BLK_SIZE)+1);
FileWrite(av_file,av_buffer,size);
};
DocBMP(doc,av_file);
Free(av_buffer);
}
U0 CrocUpdMsgs()
{
CDiscordMsg *msg;
I64 msg_cnt=0;
I64 msg_ctr=0;
ZeroParamBuf;
StrCpy(HGBD_PARAM_BUF,CROC_TOKEN);
StrCpy(HGBD_PARAM_BUF+256,CROC_CHAN);
WriteParamBuf;
HGExec(CROC_GETMSGS);
ReadParamBuf;
msg_cnt=Str2I64(HGBD_PARAM_BUF);
BlkRead(HGBD,HGFS_BUF,1,4*msg_cnt);
msg_ctr=0;
DocClear(discord_msgsbuf);
while (msg_ctr<msg_cnt)
{
msg=HGFS_BUF+(msg_ctr*sizeof(CDiscordMsg)/8);
CrocAvatar(discord_msgsbuf,msg->author_id,msg->avatar);
DocPrint(discord_msgsbuf,"\dID,5\d\dCYAN\d%s \dDKGRAY\d%s\n\dBLACK\d%s\n\dFG\d\dID,-5\d\n\n\n",
msg->author,msg->timestamp,msg->content);
msg_ctr++;
};
DocClear(discord_msgs->put_doc);
DocInsDoc(discord_msgs->put_doc,discord_msgsbuf);
DocBottom(discord_msgs->put_doc);
}
U0 CrocSendMsg()
{
ZeroParamBuf;
StrCpy(HGBD_PARAM_BUF,CROC_TOKEN);
StrCpy(HGBD_PARAM_BUF+256,CROC_CHAN);
WriteParamBuf;
BlkWrite(HGBD,CROC_MSG,1,(StrLen(CROC_MSG)/BLK_SIZE)+1);
HGExec(CROC_SENDMSG);
StrCpy(CROC_MSG,"");
}
U0 CrocUpdChan()
{
U8 buf[256];
ZeroParamBuf;
StrCpy(HGBD_PARAM_BUF,CROC_TOKEN);
StrCpy(HGBD_PARAM_BUF+256,CROC_CHAN);
WriteParamBuf;
HGExec(CROC_GETCHAN);
ReadParamBuf;
StrPrint(buf, "#%s: %s\n", HGBD_PARAM_BUF, HGBD_PARAM_BUF+256);
StrCpy(discord_msgs->task_title,buf);
if (StrCmp(CROC_MSG,"")!=0)
{
CrocSendMsg;
};
if (StrCmp(CROC_LMID,HGBD_PARAM_BUF+128)!=0)
{
StrCpy(CROC_LMID,HGBD_PARAM_BUF+128);
CrocUpdMsgs;
};
}
U0 CrocUpdLoop()
{
while (TRUE)
{
CrocUpdChan;
Sleep(3000);
};
}
#include "::/Home/Private/CrocSettings"
StrCpy(CROC_LMID,"");
StrCpy(CROC_MSG,"");
WinTileHorz;
WinFocus(Fs);
XTalk(discord_msgs,"StrCpy(Fs->task_title,\"\");\n");
Sleep(100);
DocClear(discord_msgs->put_doc);
discord_msgloop = Spawn(&CrocUpdLoop);
+2
View File
@@ -1,2 +1,4 @@
# crocodile
Discord client for TempleOS
Placeholder
+143
View File
@@ -0,0 +1,143 @@
CROC_GETAVATAR = 0x21
CROC_GETCHAN = 0x22
CROC_GETMSGS = 0x23
CROC_SENDMSG = 0x24
class Crocodile:
getchan_cmd = ''
getchan_cmd += 'curl -v '
getchan_cmd += '-H "authorization: DISCORD_TOKEN" '
getchan_cmd += '-H "User-Agent: Crocodile (https://shekihs.space, v0.1)" '
getchan_cmd += '-H "Content-Type: application/json" '
getchan_cmd += '-X GET '
getchan_cmd += 'https://discordapp.com/api/channels/DISCORD_CHANNEL 2>/dev/null'
sendmsg_cmd = ''
sendmsg_cmd += 'curl -v '
sendmsg_cmd += '-H "authorization: DISCORD_TOKEN" '
sendmsg_cmd += '-H "User-Agent: Crocodile (https://shekihs.space, v0.1)" '
sendmsg_cmd += '-H "Content-Type: application/json" '
sendmsg_cmd += '-X POST '
sendmsg_cmd += '-d \'{"content":"DISCORD_MSG"}\' '
sendmsg_cmd += 'https://discordapp.com/api/channels/DISCORD_CHANNEL/messages 2>/dev/null'
pass
def crocodile(data):
if data == CROC_GETAVATAR:
CrocGetAvatar()
if data == CROC_GETCHAN:
CrocGetChan()
if data == CROC_GETMSGS:
CrocGetMsgs()
if data == CROC_SENDMSG:
CrocSendMsg()
def CrocGetAvatar():
global Crocodile
os.lseek(HGBD,0,os.SEEK_SET)
HGBD_PARAM_BUF = os.read(HGBD,BLK_SIZE)
r_author_id = HGBD_PARAM_BUF[:HGBD_PARAM_BUF.find('\x00')]
r_avatar = HGBD_PARAM_BUF[64:HGBD_PARAM_BUF.find('\x00',64)]
tmp_bmp_file = '/tmp/' + r_avatar + '.bmp'
resp = subprocess.Popen('wget -q -O - https://cdn.discordapp.com/avatars/' + r_author_id + '/' + r_avatar + '.png\?size=32 | gm convert - ' + tmp_bmp_file,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE).communicate()[0]
filedata = open(tmp_bmp_file,"rb").read()
filesize = len(filedata)
ZeroParamBuf()
os.lseek(HGBD,0,os.SEEK_SET)
os.write(HGBD,str(filesize))
os.lseek(HGBD,BLK_SIZE,os.SEEK_SET)
os.write(HGBD,filedata)
os.remove(tmp_bmp_file)
conn.send(chr(CROC_GETAVATAR))
def CrocGetChan():
global Crocodile
os.lseek(HGBD,0,os.SEEK_SET)
HGBD_PARAM_BUF = os.read(HGBD,BLK_SIZE)
r_token = HGBD_PARAM_BUF[:HGBD_PARAM_BUF.find('\x00')]
r_chan_id = HGBD_PARAM_BUF[256:HGBD_PARAM_BUF.find('\x00',256)]
chan_lmid = ''
chan_name = ''
chan_topic = ''
try:
croc_cmd = str(Crocodile.getchan_cmd)
croc_cmd = croc_cmd.replace('DISCORD_TOKEN',r_token)
croc_cmd = croc_cmd.replace('DISCORD_CHANNEL',r_chan_id)
chan_json = json.loads(subprocess.Popen(croc_cmd,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE).communicate()[0])
chan_lmid = str(chan_json['last_message_id'])
chan_name = str(chan_json['name'])
chan_topic = str(chan_json['topic'])
except:
pass
ZeroParamBuf()
os.lseek(HGBD,0,os.SEEK_SET)
os.write(HGBD,str(chan_name))
os.lseek(HGBD,128,os.SEEK_SET)
os.write(HGBD,str(chan_lmid))
os.lseek(HGBD,256,os.SEEK_SET)
os.write(HGBD,str(chan_topic))
conn.send(chr(CROC_GETCHAN))
def CrocGetMsgs():
global Crocodile
os.lseek(HGBD,0,os.SEEK_SET)
HGBD_PARAM_BUF = os.read(HGBD,BLK_SIZE)
r_token = HGBD_PARAM_BUF[:HGBD_PARAM_BUF.find('\x00')]
r_chan_id = HGBD_PARAM_BUF[256:HGBD_PARAM_BUF.find('\x00',256)]
msgs_cnt = 0
try:
croc_cmd = str(Crocodile.getchan_cmd)
croc_cmd = croc_cmd.replace('DISCORD_TOKEN',r_token)
croc_cmd = croc_cmd.replace('DISCORD_CHANNEL',r_chan_id + '/messages')
msgs_json = json.loads(subprocess.Popen(croc_cmd,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE).communicate()[0])
msgs_cnt = len(msgs_json)
except:
pass
ZeroParamBuf()
os.lseek(HGBD,0,os.SEEK_SET)
os.write(HGBD,str(msgs_cnt))
msg_ofs = BLK_SIZE
msgs_json.reverse()
for msg in msgs_json:
os.lseek(HGBD,msg_ofs,os.SEEK_SET)
os.write(HGBD, '\x00'*2048)
os.lseek(HGBD,msg_ofs,os.SEEK_SET)
os.write(HGBD,msg['id']+'\x00')
msg_ofs += 64
os.lseek(HGBD,msg_ofs,os.SEEK_SET)
os.write(HGBD,msg['timestamp'][:19].replace('T',' ')+'\x00')
msg_ofs += 64
os.lseek(HGBD,msg_ofs,os.SEEK_SET)
os.write(HGBD,msg['author']['username']+'\x00')
msg_ofs += 64
os.lseek(HGBD,msg_ofs,os.SEEK_SET)
os.write(HGBD,msg['author']['id']+'\x00')
msg_ofs += 64
os.lseek(HGBD,msg_ofs,os.SEEK_SET)
os.write(HGBD,msg['author']['avatar']+'\x00')
msg_ofs += 64
os.lseek(HGBD,msg_ofs,os.SEEK_SET)
os.write(HGBD,msg['content'].encode('utf8')+'\x00')
msg_ofs += 1024
conn.send(chr(CROC_GETMSGS))
def CrocSendMsg():
global Crocodile
os.lseek(HGBD,0,os.SEEK_SET)
HGBD_PARAM_BUF = os.read(HGBD,BLK_SIZE)
r_token = HGBD_PARAM_BUF[:HGBD_PARAM_BUF.find('\x00')]
r_chan_id = HGBD_PARAM_BUF[256:HGBD_PARAM_BUF.find('\x00',256)]
os.lseek(HGBD,BLK_SIZE,os.SEEK_SET)
HGBD_MSG_BUF = os.read(HGBD,BLK_SIZE*2)
r_msg = HGBD_MSG_BUF[:HGBD_MSG_BUF.find('\x00')]
r_msg = r_msg.replace('"','\"')
r_msg = r_msg.replace('\'','\\\'')
try:
croc_cmd = str(Crocodile.sendmsg_cmd)
croc_cmd = croc_cmd.replace('DISCORD_TOKEN',r_token)
croc_cmd = croc_cmd.replace('DISCORD_CHANNEL',r_chan_id)
croc_cmd = croc_cmd.replace('DISCORD_MSG',r_msg)
resp = subprocess.Popen(croc_cmd,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE).communicate()[0]
except:
pass
ZeroParamBuf()
conn.send(chr(CROC_SENDMSG))