-
Notifications
You must be signed in to change notification settings - Fork 9
/
yayad.py
129 lines (117 loc) · 4.18 KB
/
yayad.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# -*- coding: utf-8 -*-
#
# yayad.py - a (Real) YAYA loader for ninix
# Copyright (C) 2004 by linjian
# Copyright (C) 2004-2011 by Shyouzou Sugitani <shy@users.sourceforge.jp>
# Copyright (C) 2011 by henryhu
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License (version 2) as
# published by the Free Software Foundation. It is distributed in the
# hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
import os
import sys
import traceback
from ctypes import *
import _ctypes
try:
yaya = cdll.LoadLibrary("libaya5.so")
except:
print "yaya load fail!"
traceback.print_exc()
yaya = None
class Shiori:
def __init__(self, dll_name):
self.dll_name = dll_name
self.pathdic = []
self.reqdic = []
def use_saori(self, saori):
self.saori = saori
def find(self, topdir, dll_name): ## FIXME
result = 0
if yaya:
if os.path.isfile(os.path.join(topdir, 'yaya.txt')):
result = 205
elif dll_name is not None and \
os.path.isfile(os.path.join(topdir,
''.join((dll_name[:-3], 'txt')))):
result = 105
return result
def show_description(self):
sys.stdout.write(
'Shiori: Real Direct YAYA loader for ninix\n'
' Copyright (C) 2004 by linjian\n'
' Copyright (C) 2004-2011 by Shyouzou Sugitani\n'
' Copyright (C) 2011 by henryhu\n')
def get_charset(self, topdir, dll_name): ## FIXME
charset = 'Shift_JIS'
f = open(os.path.join(topdir, ''.join((dll_name[:-3], 'txt'))))
for line in f:
if 'charset' in line:
charset = line.split(',')[1]
charset = charset.strip()
break
if charset == 'default':
charset = 'Shift_JIS'
return charset
def load(self, topdir):
self.dir = topdir
self.charset = self.get_charset(self.dir, self.dll_name)
if yaya:
if self.dir.endswith(os.sep):
topdir = self.dir
else:
topdir = ''.join((self.dir, os.sep))
path = create_string_buffer(topdir)
self.pathdic += [path] # so python would not free it
# print self.pathdic
# since yaya would free it (...)
# we must not allow python to free it
ret = yaya.load(path, len(topdir))
# print "load result: %d" % ret
return ret
else:
return 0
def unload(self):
global yaya
if yaya:
yaya.unload()
# force Python to unload the library
# else the next load would not function properly
try:
_ctypes.FreeLibrary(yaya._handle)
except:
pass
try:
_ctypes.dlclose(yaya._handle)
except:
pass
yaya = cdll.LoadLibrary("libaya5.so")
__req_charset = 'GET SHIORI/3.0\r\n' \
'ID: charset\r\n' \
'Sender: ninix-aya\r\n' \
'SecurityLevel: local\r\n\r\n'
def request(self, req_string):
if yaya:
if req_string == self.__req_charset:
return 'SHIORI/3.0 200 OK\r\n' \
'Sender: YAYA\r\n' \
'Value: %s\r\n\r\n' % self.charset
reqf = yaya.request
reqf.restype = c_char_p
request = create_string_buffer(req_string)
# since yaya may free it (...)
# we must not allow python to free it
self.reqdic += [request] # so python would not free it
rlen = c_long(len(request))
# print "request:"
# print req_string
ret = reqf(request, byref(rlen))
# print "result len: %d" % rlen.value
# print ret
return ret
else:
return '' # FIXME