1+ namespace Deployf . Botf ;
2+
3+ public class ConnectionString
4+ {
5+ /// <summary>
6+ /// token?key=value&key=value...
7+ /// </summary>
8+ /// <param name="value"></param>
9+ /// <returns></returns>
10+ /// <exception cref="ArgumentNullException"></exception>
11+ /// <exception cref="Exception"></exception>
12+ public static BotfOptions Parse ( string value )
13+ {
14+ if ( string . IsNullOrEmpty ( value ) )
15+ {
16+ throw new ArgumentNullException ( "value" ) ;
17+ }
18+
19+ var options = new BotfOptions ( ) ;
20+
21+ var main = value . Split ( '?' , StringSplitOptions . RemoveEmptyEntries | StringSplitOptions . TrimEntries ) ;
22+
23+ if ( main . Length > 0 )
24+ {
25+ options . Token = main [ 0 ] ;
26+ }
27+ else
28+ {
29+ throw new ArgumentException ( ) ;
30+ }
31+
32+ if ( main . Length == 1 )
33+ {
34+ return options ;
35+ }
36+
37+ var values = main [ 1 ] . Split ( '&' , StringSplitOptions . RemoveEmptyEntries | StringSplitOptions . TrimEntries ) ;
38+ foreach ( var kv in values )
39+ {
40+ var cortage = kv . Split ( '=' , StringSplitOptions . RemoveEmptyEntries | StringSplitOptions . TrimEntries ) ;
41+ if ( cortage == null || cortage . Length != 2 )
42+ {
43+ throw new Exception ( "Botf connection string is wrong" ) ;
44+ }
45+
46+ switch ( cortage [ 0 ] )
47+ {
48+ case "botname" :
49+ options . Username = cortage [ 1 ] ;
50+ break ;
51+ case "autosend" :
52+ if ( bool . TryParse ( cortage [ 1 ] , out var autosend ) )
53+ {
54+ options . AutoSend = autosend ;
55+ }
56+ break ;
57+ case "group_mode" :
58+ if ( bool . TryParse ( cortage [ 1 ] , out var groupMode ) )
59+ {
60+ options . HandleOnlyMentionedInGroups = groupMode ;
61+ }
62+ break ;
63+ case "webhook" :
64+ options . WebhookUrl = cortage [ 1 ] ;
65+ break ;
66+ case "api" :
67+ options . ApiBaseUrl = cortage [ 1 ] ;
68+ break ;
69+ }
70+ }
71+
72+ return options ;
73+ }
74+ }
0 commit comments