-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathautoreply.py
179 lines (134 loc) · 5.65 KB
/
autoreply.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from ts3plugin import ts3plugin
import ts3lib, pytson, ts3client
from ts3defines import ERROR_ok, ClientPropertiesRare, TextMessageTargetMode
from PythonQt.QtGui import *
from PythonQt.QtCore import Qt
import os
from configparser import ConfigParser
import traceback
ct = ts3lib.printMessageToCurrentTab
CONF_DEFAULTS = {'onlyfriends': 'True', 'sendaway': 'False', 'custommsg': '', 'onlyonce': 'True'}
class AutoReplyConfig(QDialog):
def __init__(self, cfg, parent=None):
super(QDialog, self).__init__(parent)
self.setAttribute(Qt.WA_DeleteOnClose)
self.lay = QFormLayout(self)
self.friendscb = QCheckBox("Only to friends", self)
self.lay.addRow(self.friendscb)
self.msggrp = QGroupBox("Message", self)
self.lay.addRow(self.msggrp)
self.grplayout = QFormLayout(self.msggrp)
self.awaymsgradio = QRadioButton("Send away msg", self)
self.grplayout.addRow(self.awaymsgradio)
self.custommsgradio = QRadioButton("Send custom msg", self)
self.grplayout.addRow(self.custommsgradio)
self.custommsgedit = QLineEdit(self)
self.grplayout.addRow(self.custommsgedit)
self.onlyoncecb = QCheckBox("Only once per conversation", self)
self.lay.addRow(self.onlyoncecb)
self.buttonbox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self)
self.lay.addRow(self.buttonbox)
self.connect("finished(int)", self.onClose)
self.awaymsgradio.connect("toggled(bool)", self.onRadioChanged)
self.custommsgradio.connect("toggled(bool)", self.onRadioChanged)
self.buttonbox.connect("accepted()", self.accept)
self.buttonbox.connect("rejected()", self.reject)
try:
self.cfg = cfg['general']
self.initValues()
except Exception as e:
self.delete()
raise e
def initValues(self):
self.friendscb.setChecked(self.cfg['onlyfriends'] == "True")
if self.cfg['sendaway'] == "True":
self.awaymsgradio.setChecked(True)
else:
self.custommsgradio.setChecked(True)
self.custommsgedit.setText(self.cfg['custommsg'])
self.onlyoncecb.setChecked(self.cfg['onlyonce'] == "True")
def onClose(self, r):
if r == QDialog.Accepted:
self.cfg['onlyfriends'] = "True" if self.friendscb.isChecked() else "False"
self.cfg['sendaway'] = "True" if self.awaymsgradio.isChecked() else "False"
self.cfg['custommsg'] = self.custommsgedit.text
self.cfg['onlyonce'] = "True" if self.onlyoncecb.isChecked() else "False"
def onRadioChanged(self):
self.custommsgedit.setEnabled(self.custommsgradio.isChecked())
def isFriend(uid):
db = ts3client.Config()
q = db.query("SELECT * FROM contacts WHERE value LIKE '%%IDS=%s%%'" % uid)
ret = 2
if q.next():
val = q.value("value")
for l in val.split('\n'):
if l.startswith('Friend='):
ret = int(l[-1])
del db
return ret == 0
class autoreply(ts3plugin):
name = "AutoReplyAway"
requestAutoload = False
version = "1.0.1"
apiVersion = 21
author = "Thomas \"PLuS\" Pathmann"
description = "This plugin autoreplies if you are away. It also shows one way to create ui-configurable scripts."
offersConfigure = True
commandKeyword = ""
infoTitle = None
menuItems = []
hotkeys = []
def __init__(self):
try:
self.dlg = None
self.handled = []
self.cfg = ConfigParser(CONF_DEFAULTS)
self.cfg.add_section("general")
p = pytson.getConfigPath("autoreply.ini")
if os.path.isfile(p):
self.cfg.read(p)
except:
ct(traceback.format_exc())
def stop(self):
if self.dlg:
self.dlg.close()
self.dlg = None
with open(pytson.getConfigPath("autoreply.ini"), "w") as f:
self.cfg.write(f)
def configure(self, qParentWidget):
try:
if not self.dlg:
self.dlg = AutoReplyConfig(self.cfg, qParentWidget)
self.dlg.show()
self.dlg.raise_()
except:
ct(traceback.format_exc())
def onTextMessageEvent(self, schid, targetMode, toID, fromID, fromName, fromUID, message, ffIgnored):
(err, myid) = ts3lib.getClientID(schid)
if err != ERROR_ok or fromID == myid:
return False
#only private msgs
if targetMode != TextMessageTargetMode.TextMessageTarget_CLIENT:
return False
#only, if I'm away
(err, away) = ts3lib.getClientSelfVariableAsInt(schid, ClientPropertiesRare.CLIENT_AWAY)
if err != ERROR_ok or not away:
return False
#only to friends? is from friend?
if self.cfg['general']['onlyfriends'] == "True" and not isFriend(fromUID):
return False
#only once per conversation? did we already sent him?
if self.cfg['general']['onlyonce'] == "True" and (schid, fromUID) in self.handled:
return False
if self.cfg['general']['sendaway'] == "True":
(err, msg) = ts3lib.getClientSelfVariableAsString(schid, ClientPropertiesRare.CLIENT_AWAY_MESSAGE)
else:
msg = self.cfg['general']['custommsg']
if msg == "":
return False
err = ts3lib.requestSendPrivateTextMsg(schid, msg, fromID)
if err == ERROR_ok:
self.handled.append((schid, fromUID))
def onClientChatClosedEvent(self, schid, clientID, clientUID):
if (schid, clientUID) in self.handled:
self.handled.remove((schid, clientUID))