@@ -57,6 +57,112 @@ function wget(url, callback) {
5757 } ) ;
5858}
5959
60+ function parseTeamLinkPokePast ( link ) {
61+ let linkRegex = / ^ h t t p s : \/ \/ p o k e p a s t \. e s \/ ( [ a - f 0 - 9 ] + ) / ;
62+ let linkRes = linkRegex . exec ( link ) ;
63+
64+ if ( ! linkRes ) {
65+ return null ;
66+ }
67+
68+ return {
69+ service : "pokepast" ,
70+ id : linkRes [ 1 ] ,
71+ } ;
72+ }
73+
74+ function parseTeamLinkPokemonShowdown ( link ) {
75+ let linkRegex1 = / ^ h t t p s : \/ \/ p s i m \. u s \/ t \/ ( [ 0 - 9 ] + ) / ;
76+
77+ let linkRes = linkRegex1 . exec ( link ) ;
78+
79+ if ( ! linkRes ) {
80+ let linkRegex2 = / ^ h t t p s : \/ \/ t e a m s \. p o k e m o n s h o w d o w n \. c o m \/ v i e w \/ ( [ 0 - 9 ] + ) / ;
81+
82+ linkRes = linkRegex2 . exec ( link ) ;
83+
84+ if ( ! linkRes ) {
85+ return null ;
86+ }
87+ }
88+
89+ return {
90+ service : "psim" ,
91+ id : linkRes [ 1 ] ,
92+ } ;
93+ }
94+
95+ function parseTeamLink ( link ) {
96+ const parseFuncs = [
97+ parseTeamLinkPokePast ,
98+ parseTeamLinkPokemonShowdown ,
99+ ] ;
100+
101+ for ( const fn of parseFuncs ) {
102+ const parsed = fn ( link ) ;
103+ if ( parsed ) {
104+ return parsed ;
105+ }
106+ }
107+
108+ return null ;
109+ }
110+
111+ const INVALID_TEAM_FORMAT_ERROR_MESSAGE = "Invalid team format" ;
112+
113+ function fetchTeamPokePast ( Teams , teamId , callback ) {
114+ wget ( "https://pokepast.es/" + encodeURIComponent ( teamId ) + "/raw" , function ( data , err ) {
115+ if ( err ) {
116+ return callback ( null , err ) ;
117+ }
118+
119+ let packed ;
120+
121+ try {
122+ let team = Teams . teamToJSON ( data ) ;
123+ packed = Teams . packTeam ( team ) ;
124+ } catch ( ex ) {
125+ return callback ( null , new Error ( INVALID_TEAM_FORMAT_ERROR_MESSAGE ) ) ;
126+ }
127+
128+ return callback ( packed ) ;
129+ } ) ;
130+ }
131+
132+ function fetchTeamPokemonShowdown ( Teams , teamId , callback ) {
133+ wget ( "https://teams.pokemonshowdown.com/api/getteam?teamid=" + encodeURIComponent ( teamId ) + "&full=1" , function ( data , err ) {
134+ if ( err ) {
135+ return callback ( null , err ) ;
136+ }
137+
138+ let packed ;
139+
140+ try {
141+ let team = JSON . parse ( data . substring ( 1 ) . trim ( ) ) . team ;
142+ if ( ! team ) {
143+ return callback ( null , new Error ( "Team not found" ) ) ;
144+ }
145+ team = Teams . fastUnpackTeam ( team + "" ) ;
146+ packed = Teams . packTeam ( team ) ;
147+ } catch ( ex ) {
148+ return callback ( null , new Error ( INVALID_TEAM_FORMAT_ERROR_MESSAGE ) ) ;
149+ }
150+
151+ return callback ( packed ) ;
152+ } ) ;
153+ }
154+
155+ function fetchTeam ( Teams , parsedLink , callback ) {
156+ switch ( parsedLink . service ) {
157+ case "pokepast" :
158+ return fetchTeamPokePast ( Teams , parsedLink . id , callback ) ;
159+ case "psim" :
160+ return fetchTeamPokemonShowdown ( Teams , parsedLink . id , callback ) ;
161+ default :
162+ return callback ( null , new Error ( "Unsupported service" ) ) ;
163+ }
164+ }
165+
60166module . exports = {
61167 addteam : function ( App ) {
62168 this . setLangFile ( Lang_File ) ;
@@ -93,24 +199,23 @@ module.exports = {
93199 }
94200
95201 let link = ( this . args [ 2 ] + "" ) . toLowerCase ( ) . trim ( ) ;
96- let linkRegex = / ^ h t t p s : \/ \/ p o k e p a s t \. e s \/ ( [ a - f 0 - 9 ] + ) / ;
97- let linkRes = linkRegex . exec ( link ) ;
98202
99- if ( ! linkRes ) {
203+ let parsedLink = parseTeamLink ( link ) ;
204+
205+ if ( ! parsedLink ) {
100206 return this . errorReply ( this . mlt ( 6 ) ) ;
101207 }
102208
103- wget ( "https://pokepast.es/" + linkRes [ 1 ] + "/raw" , function ( data , err ) {
209+ fetchTeam ( Teams , parsedLink , ( packed , err ) => {
104210 if ( err ) {
105- return this . errorReply ( this . mlt ( 7 ) + " " + err . message ) ;
211+ if ( err . message === INVALID_TEAM_FORMAT_ERROR_MESSAGE ) {
212+ return this . errorReply ( this . mlt ( 8 ) ) ;
213+ } else {
214+ return this . errorReply ( this . mlt ( 7 ) + " " + err . message ) ;
215+ }
106216 }
107217
108- let packed ;
109-
110- try {
111- let team = Teams . teamToJSON ( data ) ;
112- packed = Teams . packTeam ( team ) ;
113- } catch ( ex ) {
218+ if ( ! packed ) {
114219 return this . errorReply ( this . mlt ( 8 ) ) ;
115220 }
116221
@@ -125,7 +230,14 @@ module.exports = {
125230
126231 this . reply ( this . mlt ( 9 ) + " " + Chat . italics ( teamName ) + " " + this . mlt ( 10 ) + " " + Chat . italics ( formatName ) ) ;
127232 this . addToSecurityLog ( ) ;
128- } . bind ( this ) ) ;
233+
234+ // Display the added team
235+
236+ this . cmd = 'getteam' ;
237+ this . arg = teamId ;
238+ this . args = [ teamId ] ;
239+ this . parser . exec ( this ) ;
240+ } ) ;
129241 } ,
130242
131243 deleteteam : function ( App ) {
0 commit comments