11from ts3plugin import ts3plugin , PluginHost
2- from datetime import datetime
3- import ts3lib , ts3defines
2+ from ts3defines import *
3+ import ts3lib
44from pytson import getCurrentApiVersion
55from PythonQt .QtCore import QTimer
6- from PythonQt .QtGui import QFileDialog , QDialog
7- from bluscream import timestamp
6+ from PythonQt .QtGui import QFileDialog , QDialog , QMessageBox
7+ from bluscream import timestamp , msgBox , escapeStr
88from ts3Ext import ts3SessionHost as ts3host
99
1010
@@ -21,11 +21,14 @@ class exporter(ts3plugin):
2121 offersConfigure = False
2222 commandKeyword = "ex"
2323 infoTitle = ""
24- menuItems = [(ts3defines . PluginMenuType .PLUGIN_MENU_TYPE_GLOBAL , 0 , "Export Channel Tree" , "" ),
25- (ts3defines . PluginMenuType .PLUGIN_MENU_TYPE_GLOBAL , 1 , "Import Channel Tree" , "" )]
24+ menuItems = [(PluginMenuType .PLUGIN_MENU_TYPE_GLOBAL , 0 , "Export Channel Tree" , "" ),
25+ (PluginMenuType .PLUGIN_MENU_TYPE_GLOBAL , 1 , "Import Channel Tree" , "" )]
2626 hotkeys = []
2727 ts3host = ts3host
2828 timer = QTimer
29+ # retcodes = []
30+ result = {"success" : 0 , "failed" : 0 , "total" : 0 , "msg" : "" , "stop" : False }
31+ channels = []
2932
3033 def __init__ (self ):
3134 if "aaa_ts3Ext" in PluginHost .active :
@@ -39,7 +42,7 @@ def __init__(self):
3942
4043
4144 def onMenuItemEvent (self , schid , atype , menuItemID , selectedItemID ):
42- if atype == ts3defines . PluginMenuType .PLUGIN_MENU_TYPE_GLOBAL :
45+ if atype == PluginMenuType .PLUGIN_MENU_TYPE_GLOBAL :
4346 if menuItemID == 1 : self .importChannels (schid )
4447 def processCommand (self , schid , keyword ):
4548 args = keyword .split (" " )
@@ -54,24 +57,75 @@ def processCommand(self, schid, keyword):
5457 else : return False
5558 return True
5659
57- def importChannels (self , schid , file = "" ):
60+ def readChannels (self , file ):
5861 if not file : file = QFileDialog .getOpenFileName (QDialog (), "" , "" , "All Files (*);;YaTQA Exported Channels (*.ts3_chans)" )
5962 channels = []
6063 with open (file ) as fp :
6164 lines = fp .readlines ()
62- i = 0
65+ # i = 0
6366 for line in lines :
64- line = line .rstrip ().split ('|' , 1 )
65- channel = line .pop (0 ).split (" " )
66- for pair in channel :
67+ if not line .strip (): continue # Todo: Maybe trycatch instead?
68+ line = line .rstrip ().split ("|" , 1 )
69+ _channel = line .pop (0 ).split (" " )
70+ channel = {"flags" : {}}
71+ for pair in _channel :
6772 pair = pair .split ("=" )
6873 key = pair .pop (0 )
6974 (err , flag ) = ts3lib .channelPropertyStringToFlag (key )
70- value = pair .pop (0 ) if len (pair ) else ""
75+ if err != ERROR_ok or not len (pair ): continue
76+ channel ["flags" ][flag ] = escapeStr (pair .pop (0 ))
7177 # ts3lib.printMessageToCurrentTab("{}: {} = {}".format(i, flag, value))
7278 if len (line ):
7379 perms = line .pop (0 ).split ("|" )
80+ channel ["permissions" ] = []
7481 for perm in perms :
82+ permission = {}
7583 perm = perm .split (" " )
76- for perm in perm
77- i += 1
84+ for prop in perm :
85+ prop = prop .split ("=" )
86+ if prop [0 ] == "cid" : continue
87+ permission [prop [0 ]] = prop [1 ]
88+ # print("{}: {} = {}".format(i, prop[0], prop[1]))
89+ channel ["permissions" ].append (permission )
90+ channels .append (channel )
91+ # i += 1
92+ return channels
93+ def importChannels (self , schid , file = "" ):
94+ self .result = {"success" : 0 , "failed" : 0 , "total" : 0 , "msg" : "" , "stop" : False }
95+ channels = self .readChannels (file )
96+ self .result ["total" ] = len (channels )
97+ for channel in channels :
98+ if self .result ["stop" ]:
99+ self .result ["stop" ] = False
100+ return
101+ for k , v in channel ["flags" ].items ():
102+ ts3lib .setChannelVariableAsString (schid , 0 , k , v )
103+ print ("Setting var" ,k ,"with value" ,v )
104+ returnCode = ts3lib .createReturnCode ()
105+ # result = {}
106+ self .channels .append ({returnCode : (channel ["flags" ][ChannelProperties .CHANNEL_NAME ], channel ["permissions" ])})
107+ self .channels .append (channel )
108+ ts3lib .flushChannelCreation (schid , 0 , returnCode )
109+
110+ def onNewChannelCreatedEvent (self , serverConnectionHandlerID , channelID , channelParentID , invokerID , invokerName , invokerUniqueIdentifier ):
111+ pass
112+
113+ def onUpdateChannelEvent (self , serverConnectionHandlerID , channelID ):
114+ pass
115+
116+
117+ def onServerErrorEvent (self , schid , errorMessage , error , returnCode , extraMessage ):
118+ if not returnCode in self .channels : return False
119+ reason = "finished" ; self .result ["processed" ] += 1
120+ if error == ERROR_ok :
121+ self .result ["success" ] += 1 # Todo Permissions
122+ elif error == ERROR_client_is_flooding :
123+ self .result ["failed" ] += 1 ; self .result ["stop" ] = True ;reason = "stopped because of flooding"
124+ elif error in [ERROR_permissions_client_insufficient ,ERROR_permissions_insufficient_group_power ,ERROR_permissions_insufficient_permission_power ]:
125+ self .result ["failed" ] += 1
126+ self .result ["msg" ] += "\n \" {}\" : {}" .format (self .channels [returnCode ], reason )
127+ del self .channels [returnCode ]
128+ if not len (self .channels ):
129+ icon = QMessageBox .Error if self .result ["failed" ] else QMessageBox .Confirmation
130+ msgBox (self .result ["msg" ], icon , self .name )
131+ return True
0 commit comments