~amsn-daily/amsn/amsn-packaging

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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/tclsh
###
### Remote controller program
###

set initialize_amsn 1

source amsncore.tcl

proc readsocket { } {
	global sock

	set complete 0
	set response ""

	while { [info exists sock] && $complete == 0 } {
		if { [gets $sock tmp] == -1 } {
			puts "Amsn server closed\n"
			connect "Amsn server closed"
		} elseif { $tmp != "" } {
			append response $tmp
			append response "\n"
			set complete [info complete $response]
			
			if { $complete } {
				set data [lindex $response 0]
				set colour [lindex $response 1]
				puts "\033\[[colorToCode $colour]m"
				puts $data
				puts "\033\[0m"
			} 
		}
	}
    
}

proc colorToCode { colour } {
	switch -- $colour {
	  error {
	       return "0;31"
	  }
	  msgsent {
	  	return "0;34"
	  }
	  msgrcv {
	  	return "0;33"
	  }
	  command {
	  	return "1;30"
	  }
	  event {
	  	return "0;35"
	  }
	  normal {
	  	return 0
	  }
	  default {
	  	return 0
	  }

	}
}

proc writesocket { command } {
    global sock
    set command [string map { \; \\\; \" \\\" \[ \\\[ \] \\\] \: \\\: \\ \\\\ \* \\\* \? \\\?} $command]
    if { $command != "quit" } {
	puts ${sock} $command
	flush $sock
    } 

}

proc authenticate { password } {
    global sock

    puts ${sock} "auth"
    flush $sock
    gets $sock response
    set response [lindex $response 0]

     if { [lindex $response 0] != "auth" } {
	    if { [lindex $response 0] == "wait" } {
		    return "wait"
	    } else {
		    return "fail"
	    }
    } else {
	set hash [::md5::hmac "[lindex $response 1]" "$password"]
	puts $sock "auth2 $hash"
	flush $sock
	gets $sock response	
        set response [lindex $response 0]
	if { "$response" ==  "Authentication successfull" } {
	    return "OK"
	} elseif { "$response" == "Authentication failed" } {
	    return "fail"
	} else {
	    return "disabled"
	}
       
    }

}

proc connect { status } {
    global sockDS stat ip
    
    if { $sockDS == 0 } {
	set stat [catch {socket $ip 63251} sockDS]
	set status "Connected to $ip"
    }

    if { $stat != 0 } {
	puts "Error connecting to server\n$sockDS"
	set sockDS 0
    }  else {
	puts $status
	puts "Account: "
	set account [gets stdin]
	puts "Password for remote access : "
	catch {eval exec stty -echo 0}
	set password [gets stdin]
	catch {eval exec stty echo}
	retreive_port $ip $account $password
    }

}

proc retreive_port { ip account password} {
    global sockDS sock
    puts $sockDS "$account"

    if { [catch { flush $sockDS}] } {
	set sockDS 0
	connect "Disconnected from the DS server" 
	return 0
    }

    set port [gets $sockDS]

    if { "$port" == "invalid" } {
	connect "This Amsn Account is invalid"
	return 0
    }
    if { "$port" == "noexist" } {
	connect "The Amsn profile file doesn't exist or you don't have permissions to read from it"
	return 0
    }
    if { "$port" == "versionerror" } {
	connect "The Amsn profile file has an invalid version" 
	return 0
    }

    if { [catch {socket $ip $port} sock] != 0 } {
	connect "This Amsn Account isn't connected"
	return 0
    } else {
	fconfigure $sock -buffering line
	if { [set auth_response [authenticate "$password"]] == "fail" } {
	    connect "Password incorrect or not set"
	    return 0
	} elseif { $auth_response == "disabled" } {
	    connect "This account doesn't have remote controlling enabled"
	    return 0
	} elseif { $auth_response == "wait" } {
	    connect "Please wait 3 seconds before retrying."
	    return 0
	} else {
	    puts "Connected to port : $port"
	    fileevent $sock readable "readsocket"
	    control
	    return 1
	}	
    }
}
proc readstdin { } {
	global commando
	set commando [gets stdin]
	writesocket $commando
}

proc control { } {
    global sock
    global commando
	fconfigure stdin -blocking false
	fileevent stdin readable "readstdin"
	set goout 1
	while {$goout != "quit"} {
		vwait commando
		
		set goout $commando 
	}
    }

proc main { } {
    global ip
    puts "Enter the IP address of the host : "
    gets stdin ip
    connect "Connected to the server"
}


############################################################
### Look if we are launched from a link
### and set the correct working directory
############################################################

if {[catch {file readlink [info script]} res]!=0} {

   #Error in readlink, so it's not a symbolic link
   set program_dir [file dirname [info script]]

} else {

   #Recursively update $resdir until it's not a link
   set program_dir [file dirname $res]

   while {[catch {file readlink $res} res2]==0} {
      set res $res2

      #Update $program_dir, depending on absolute or relative path
      if { [string range $res2 0 0]=="/" } {
         set program_dir [file dirname $res2]
      } else {
         set program_dir [file join $resdir [file dirname $res2]]
      }

   }
}

source [file join $program_dir migmd5.tcl]

set sockDS 0

main