Advertisement
Guest User

Untitled

a guest
May 8th, 2019
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 301.24 KB | None | 0 0
  1. #!/bin/bash
  2. MYVERSION="0.2.0e"
  3.  
  4. # Transform debug to DEBUG, in order to declare debug method.
  5. if [ -n "${debug}" ]; then
  6. echo "Warning: debug variable is obsolete. Use DEBUG instead." >> /dev/stderr
  7. export DEBUG="${debug}"
  8. unset debug
  9. fi
  10.  
  11. # Display formatted debug output if DEBUG variable is set.
  12. # This method should have as less dependencies as possible.
  13. debug() {
  14. if [ "a$1" != "arun_command" -a "a$1" != "ashow_file" -a -z "${DEBUG}" ]; then
  15. return 0
  16. elif [ "a$1" = "arun_command" -a -z "${DEBUG}" ]; then
  17. shift
  18. sh -c "$*" > /dev/null 2> /dev/null
  19. return $?
  20. elif [ "a$1" = "arun_command" -a -n "${DEBUG}" ]; then
  21. shift
  22. printf "/-------------------------------------------------------------------------------\n" >> "/dev/stderr"
  23. printf "[%.5d] [%s] Will now run command: \'%s\'\n" "$$" "`date "+%H:%M:%S"`" "${*}" >> "/dev/stderr"
  24. printf "/-------------------------------------------------------------------------------\n" >> "/dev/stderr"
  25. sh -c "$*" >> "/dev/stderr" 2>&1
  26. rdebug=$?
  27. printf "\-------------------------------------------------------------------------------\n" >> "/dev/stderr"
  28. printf "[%.5d] [%s] Command returned %d.\n" "$$" "`date "+%H:%M:%S"`" "${rdebug}" >> "/dev/stderr"
  29. printf "\-------------------------------------------------------------------------------\n" >> "/dev/stderr"
  30. return ${rdebug}
  31. elif [ -z "${DEBUG}" ]; then
  32. return 0
  33. elif [ "a$1" = "ashow_file" ]; then
  34. if [ -f "$2" ]; then
  35. printf "/-------------------------------------------------------------------------------\n" >> "/dev/stderr"
  36. printf "[%.5d] [%s] Will now display contents of: \'%s\'\n" "$$" "`date "+%H:%M:%S"`" "$2" >> "/dev/stderr"
  37. printf "/-------------------------------------------------------------------------------\n" >> "/dev/stderr"
  38. cat "$2" >> "/dev/stderr"
  39. printf "\-------------------------------------------------------------------------------\n" >> "/dev/stderr"
  40. ls -ld "$2" >> "/dev/stderr"
  41. printf "\-------------------------------------------------------------------------------\n" >> "/dev/stderr"
  42. else
  43. printf "/-------------------------------------------------------------------------------\n" >> "/dev/stderr"
  44. printf "[%.5d] [%s] File does not exist: \'%s\'\n" "$$" "`date "+%H:%M:%S"`" "$2" >> "/dev/stderr"
  45. printf "\-------------------------------------------------------------------------------\n" >> "/dev/stderr"
  46. fi
  47. return 0
  48. else
  49. rfmt="$1"
  50. shift
  51. if [ "a${YESFUNCNAME}" = "a" ]; then
  52. if [ "$#" -ge "1" ]; then
  53. printf "[%.5d] [%s] ${rfmt}" "$$" "`date "+%H:%M:%S"`" "${@}" >> "/dev/stderr"
  54. else
  55. printf "[%.5d] [%s] ${rfmt}" "$$" "`date "+%H:%M:%S"`" >> "/dev/stderr"
  56. fi
  57. else
  58. if [ "$#" -ge "1" ]; then
  59. printf "[%.5d] [%s] [%-20s] ${rfmt}" "$$" "`date "+%H:%M:%S"`" "${FUNCNAME[1]}" "${@}" >> "/dev/stderr"
  60. else
  61. printf "[%.5d] [%s] [%-20s] ${rfmt}" "$$" "`date "+%H:%M:%S"`" "${FUNCNAME[1]}" >> "/dev/stderr"
  62. fi
  63. fi
  64. unset rfmt
  65. fi
  66. return 0
  67. }
  68.  
  69. # Print debug header
  70. debug_header() {
  71. if [ -n "${DEBUG}" ]; then
  72. echo >> "/dev/stderr"
  73. echo "-------------------------------------------" >> "/dev/stderr"
  74. echo "Sakis3G ${MYVERSION} running on DEBUG mode." >> "/dev/stderr"
  75. echo "-------------------------------------------" >> "/dev/stderr"
  76. date >> "/dev/stderr"
  77. echo "-------------------------------------------" >> "/dev/stderr"
  78. if [ "a${allargs}" = "a" ]; then
  79. echo "Command line was: $0 $@" >> "/dev/stderr"
  80. else
  81. echo "Command line was: $0 ${allargs}" >> "/dev/stderr"
  82. fi
  83. echo "Running with PID: $$" >> "/dev/stderr"
  84. echo "-------------------------------------------" >> "/dev/stderr"
  85. echo "Environment is:" >> "/dev/stderr"
  86. set >> "/dev/stderr"
  87. echo "-------------------------------------------" >> "/dev/stderr"
  88. echo "Will now proceed with Sakis3G execution." >> "/dev/stderr"
  89. echo "-------------------------------------------" >> "/dev/stderr"
  90. fi
  91. }
  92.  
  93. need_arg() {
  94. [ "a`eval echo \\\$$1 2> /dev/null`" != "a" ] && return 0
  95. case "$1" in
  96. foldwrapping)
  97. export foldwrapping=60
  98. ;;
  99. pppint)
  100. export pppint="ppp0"
  101. ;;
  102. CHAT_ABORT_STRINGS)
  103. # Abort strings that chat program may encounter
  104. export CHAT_ABORT_STRINGS="ABORT BUSY ABORT ERROR ABORT BLOCKED ABORT NOCARRIER"
  105. ;;
  106. BAUD)
  107. export BAUD="115200"
  108. ;;
  109. PPPD_OPTIONS)
  110. # Options passed to pppd when called directly
  111. export PPPD_OPTIONS="modem /dev/ttyS0 115200 debug -detach defaultroute dump noipdefault usepeerdns usehostname ktune logfd 2 noauth name sakis3g lock maxfail 3"
  112. ;;
  113. PPPD_PEERS)
  114. # Directory where pppd keeps its peers
  115. export PPPD_PEERS=/etc/ppp/peers
  116. ;;
  117. SERIALDRIVERS)
  118. export SERIALDRIVERS="ftdi_sio ipaq safe_serial usbserial visor"
  119. ;;
  120. LOGPOSITION)
  121. export LOGPOSITION="/var/log/sakis3g.log"
  122. ;;
  123. XOSDFONT)
  124. export XOSDFONT='-*-freesans-bold-r-*-*-36-*-*-*-*-*-*-*'
  125. ;;
  126. AOSDFONT)
  127. export AOSDFONT='DejaVuSans 36'
  128. ;;
  129. MENUFONT)
  130. export MENUFONT="-monotype-arial-medium-r-normal-*-18-*-*-*-*-*-*-*"
  131. ;;
  132. *)
  133. debug "No default value for %s was found.\n" "$1"
  134. ;;
  135. esac
  136. debug "Loaded default value for %s: %s\n" "$1" "`eval echo \\\$$1 2> /dev/null`"
  137. }
  138.  
  139. sanitize() {
  140. echo "$@" 2> /dev/null | ${trbin} "\\" " " 2> /dev/null | ${trbin} "\"" " " | ${trbin} "'" " " | ${trbin} "\`" " "
  141. }
  142.  
  143. release_X_cookie() {
  144. debug "Disposing stolen X session cookie for %s.\n" "${XCOOKIE}"
  145. if [ -n "${XCOOKIE}" ] && find_binary "xauth"; then
  146. # If having an authority file that does not belong to us, unset it.
  147. if [ "a${XAUTHORITY}" = "a${runhome}/.Xauthority" -a "a$HOME" != "a${runhome}" ]; then
  148. unset XAUTHORITY
  149. fi
  150. found=`${xauthbin} nlist ${XCOOKIE} | ${wcbin} -l`
  151. found=`echo ${found}`
  152. if [ "a${found}" = "a0" ]; then
  153. debug "Cookie already disposed.\n"
  154. else
  155. ${xauthbin} remove ${XCOOKIE}
  156. found=`xauth nlist ${XCOOKIE} | ${wcbin} -l`
  157. found=`echo ${found}`
  158. if [ "a${found}" = "a0" ]; then
  159. debug "Cookie disposed as it should.\n"
  160. unset XCOOKIE
  161. else
  162. debug "Failed to dispose cookie.\n"
  163. fi
  164. fi
  165. unset found
  166. fi
  167. }
  168.  
  169. # Callback function called on EXIT to execute traps.
  170. exittrap() {
  171. if [ -n "${TRAPS}" ]; then
  172. debug "Now executing traps.\n"
  173. for tr in ${TRAPS}
  174. do
  175. debug "Executing trap \"%s\".\n" "${tr}"
  176. ${tr}
  177. done
  178. else
  179. debug "No exit traps defined.\n"
  180. fi
  181. if ppp_fast_status; then
  182. debug "\n>>>>>>>>> If program is paused, you may freely press Ctrl+C. <<<<<<<<<\n>>> This happens due to DEBUG being set. Connection will NOT drop. <<<\n"
  183. fi
  184. }
  185.  
  186. # Adds trap to be executed on EXIT
  187. addexittrap() {
  188. [ "a$1" = "a" ] && return 0;
  189. if [ "a${TRAPS}" = "a" ]; then
  190. trap exittrap EXIT
  191. debug "Established trap handler.\n"
  192. fi
  193. if ! strinstr "$1" "${TRAPS}" " "; then
  194. TRAPS="$1 ${TRAPS}"
  195. debug "Traps are now: %s\n" "${TRAPS}"
  196. else
  197. debug "Trap %s already registered.\n" "${1}"
  198. fi
  199. }
  200.  
  201. translate_load() {
  202. unset translatebase
  203. ! find_binary "printf" && return 1
  204. ! find_binary "grep" && return 1
  205. ! find_binary "head" && return 1
  206. ! find_binary "tail" && return 1
  207. ! find_binary "sed" && return 1
  208. ! find_binary "cat" && return 1
  209. ! find_binary "cut" && return 1
  210. ! find_binary "tr" && return 1
  211. if [ "a${notranslate}" != "a" ]; then
  212. export notranslate=yes; unset foldwrapping; need_arg "foldwrapping"
  213. return 1
  214. fi
  215. if [ "a${TRANSLATION}" != "a" ] && [ -f "${TRANSLATION}" ]; then
  216. debug "Requested to use file %s for translations.\n" "${TRANSLATION}"
  217. translatebase=`${catbin} "${TRANSLATION}" 2> /dev/null`
  218. elif [ "a${PROVIDER}" != "a" ] && [ -x "${PROVIDER}" ]; then
  219. translatelocale="${LC_MESSAGES}"
  220. [ "a${translatelocale}" = "a" ] && translatelocale="${LC_ALL}"
  221. [ "a${translatelocale}" = "a" ] && translatelocale="${LOCALE}"
  222. [ "a${translatelocale}" = "a" ] && translatelocale="${LANG}"
  223. if [ "a${translatelocale}" = "a" ]; then
  224. debug "No locale reported by system. Will not be using translations.\n"
  225. export notranslate=yes; unset foldwrapping; need_arg "foldwrapping"
  226. return 1
  227. fi
  228. debug "Locale %s found in environment.\n" "${translatelocale}"
  229. translatelocale=`echo ${translatelocale} | ${grepbin} -i "UTF\(.*\)8"`
  230. if [ "a${translatelocale}" = "a" ]; then
  231. debug "Reported locale is not UTF-8. Will not be using translations.\n"
  232. export notranslate=yes; unset foldwrapping; need_arg "foldwrapping"
  233. return 1
  234. fi
  235. translatelocale=`echo ${translatelocale} | ${cutbin} -d. -f1 | ${sedbin} -e "s/$/.UTF-8/g"`
  236. debug "Will attempt to get translation file from package: %s.\n" "messages/${translatelocale}"
  237. translatebase=`${PROVIDER} getfile "messages/${translatelocale}" 2> /dev/null`
  238. else
  239. debug "Unable to retrieve any translation file. Will not be using translations.\n"
  240. export notranslate=yes; unset foldwrapping; need_arg "foldwrapping"
  241. return 1
  242. fi
  243. helperfactor=`echo "${translatebase}" 2> /dev/null | ${headbin} -1 | ${grepbin} "^##\([0-9]\)$" | ${sedbin} -e "s/^##//g"`
  244. [ "a${helperfactor}" = "a" ] && helperfactor=1
  245. unset foldwrapping; need_arg "foldwrapping";
  246. foldwrapping=`expr ${foldwrapping} \* ${helperfactor} 2> /dev/null`; foldwrapping=`echo ${foldwrapping}`
  247. [ "a${foldwrapping}" = "a" ] && unset foldwrapping
  248. need_arg "foldwrapping"; unset helperfactor
  249. translatebase=`${printfbin} "%s" "${translatebase}" | ${grepbin} -v "^#"`
  250. if [ "a${translatebase}" = "a" ]; then
  251. debug "No translations retrieved from file. Will not be using translations.\n"
  252. export notranslate=yes; unset foldwrapping; need_arg "foldwrapping"
  253. return 1
  254. fi
  255. export foldwrapping
  256. debug "Translations loaded. Wrapping at %d bytes.\n" "${foldwrapping}"
  257. return 0
  258. }
  259.  
  260. # Translates argument to locale
  261. translate_text() {
  262. if [ "a${notranslate}" != "a" ]; then
  263. ${printfbin} "%s" "$1"
  264. return 0
  265. fi
  266. [ "a${translatebase}" = "a" ] && translate_load
  267. if [ "a${notranslate}" != "a" -o "a${translatebase}" = "a" ]; then
  268. ${printfbin} "%s" "$1"
  269. return 0
  270. fi
  271. # Safer, still slower way to translate
  272. #texttomatch=`${printfbin} "%s" "$1" | ${trbin} "\\n" ".." | ${trbin} "\n" "." | ${trbin} "\t" "." | ${trbin} "\\\\\\\" "." | ${sedbin} -e "s/\\\\\\/\./g" | ${sedbin} -e "s/\"/\./g"`
  273. texttomatch=`${printfbin} "%s" "$1" | ${trbin} "\\\\\\\" "." | ${trbin} "\\\\\"" "." | ${trbin} "\n" "."`
  274. # Comment below is to help gedit in colouring text
  275. # `"`
  276. if [ -n "${showtext}" ]; then
  277. debug "Text to check: %s\n" "${1}"
  278. debug "Text to match: %s\n" "${texttomatch}"
  279. fi
  280. texttranslated=`${printfbin} "%s" "${translatebase}" | ${grepbin} -A 1 -G "^.${texttomatch}.$" | ${headbin} -2 | ${tailbin} -1 | ${sedbin} -e "s/^\"\(.*\)\"$/\1/g"`
  281. unset texttomatch
  282. if [ "a${texttranslated}" != "a" -a "a${texttranslated}" != "a\"" ]; then
  283. [ -n "${showtext}" ] && debug "Text returned: %s\n" "${texttranslated}"
  284. ${printfbin} "%s" "${texttranslated}"
  285. else
  286. [ -n "${showtext}" ] && debug "No translation found, returning original text: %s\n" "$1"
  287. ${printfbin} "%s" "$1"
  288. fi
  289. unset texttranslated
  290. return 0
  291. }
  292.  
  293. # Safety helper
  294. safe_invoke() {
  295. if [ "a${NOFUNCNAME}" != "a" ]; then
  296. debug "FIXME: Called %s without FUNCNAME variables available.\n" "$0"
  297. return 99
  298. fi
  299. if [ "a${FUNCNAME[1]}" = "a" ]; then
  300. debug "FIXME: %s called without FUNCNAME[1] being available.\n"
  301. return 99
  302. fi
  303. debug "Function %s requested to call it in a safe manner.\n" "${FUNCNAME[1]}"
  304. callertimeout="$1"; callertimeout=`expr ${callertimeout} + 1 - 1 2> /dev/null`
  305. if [ "a${callertimeout}" = "a$1" ]; then
  306. debug "Timeout set by %s to %d seconds.\n" "${FUNCNAME[1]}" "${callertimeout}"
  307. shift
  308. else
  309. callertimeout=5
  310. debug "Caller did not provide timeout. Using default (%d seconds).\n" "${callertimeout}"
  311. fi
  312. if [ "a${safetimeout}" != "a" ]; then
  313. unset callertimeout
  314. debug "FIXME: However, we are already within a safe session.\n"
  315. ${FUNCNAME[1]} "safe" "$@"
  316. ret=$?
  317. debug "FIXME: Function %s returned from within a nested safe session.\n" "${FUNCNAME[1]}"
  318. return ${ret}
  319. elif [ "a${nosafety}" != "a" ]; then
  320. unset callertimeout
  321. debug "However, config instructs not to use safety-checks.\n"
  322. ${FUNCNAME[1]} "safe" "$@"
  323. ret=$?
  324. unset TIMEOUTOCCURED
  325. debug "Unsafe function %s returned.\n" "${FUNCNAME[1]}"
  326. return ${ret}
  327. fi
  328. safetimeout="${callertimeout}"
  329. callermethod="${FUNCNAME[1]}"
  330. unset TIMEOUTOCCURED; unset callertimeout
  331. if find_binary "mktemp"; then
  332. debug "Using %s to store standard output and error of %s.\n" "${mktempbin}" "${callermethod}"
  333. outputholder=`${mktempbin} -p /tmp -q sakis3g.safe.output.$$.XXXXXXXX`
  334. errorholder=`${mktempbin} -p /tmp -q sakis3g.safe.error.$$.XXXXXXXX`
  335. fi
  336. if [ "a${outputholder}" = "a" ]; then
  337. debug "Using backup method for creating temporary output holder.\n"
  338. outputholder="/tmp/sakis3g.unsafe.output.$$"
  339. fi
  340. if [ "a${errorholder}" = "a" ]; then
  341. debug "Using backup method for creating temporary output holder.\n"
  342. errorholder="/tmp/sakis3g.unsafe.error.$$"
  343. fi
  344. debug "Will store output at: %s\n" "${outputholder}"
  345. debug "Will store error at: %s\n" "${errorholder}"
  346. debug "Initiating safe session for %s (%d seconds timeout).\n" "${callermethod}" "${safetimeout}"
  347. "${callermethod}" "safe" "$@" > "${outputholder}" 2> "${errorholder}" &
  348. safecallerpid=$!; eval "disown -a" > /dev/null 2> /dev/null
  349. debug "Method spawned with PID %d.\n" "${safecallerpid}"
  350. while ! notrunning "${safecallerpid}"
  351. do
  352. debug "%s still runs. %d seconds before killing.\n" "${callermethod}" "${safetimeout}"
  353. if [ "a${safetimeout}" = "a0" ]; then
  354. debug "Time expired with no results.\n"
  355. if find_binary "kill"; then
  356. debug "Will attempt to kill process with PID %d.\n" "${safecallerpid}"
  357. ! notrunning "${safecallerpid}" && debug run_command "${killbin} -1 ${safecallerpid}"
  358. ! notrunning "${safecallerpid}" && debug run_command "${killbin} -9 ${safecallerpid}"
  359. ! notrunning "${safecallerpid}" && debug "Failed to kill it.\n"
  360. fi
  361. if find_binary "rm"; then
  362. debug "Will attempt to unlink temporary holders.\n"
  363. debug run_command "${rmbin} -f \"${outputholder}\" \"${errorholder}\""
  364. fi
  365. export TIMEOUTOCCURED="${callermethod}"
  366. unset callermethod; unset safecallerpid; unset outputholder; unset errorholder
  367. debug "Method %s failed to respond within timeout.\n"
  368. unset safetimeout
  369. return 99
  370. fi
  371. safetimeout=`expr ${safetimeout} - 1`; safetimeout=`echo ${safetimeout}`
  372. sleep 1
  373. done
  374. debug "Method returned while %d seconds were remaining.\n" "${safetimeout}"
  375. debug show_file "${outputholder}"
  376. debug show_file "${errorholder}"
  377. if find_binary "cat"; then
  378. debug "Will now route output and error to mine.\n"
  379. [ -f "${outputholder}" ] && ${catbin} "${outputholder}" 2> /dev/null
  380. [ -f "${errorholder}" ] && ${catbin} "${errorholder}" >> /dev/stderr 2> /dev/null
  381. fi
  382. if find_binary "rm"; then
  383. debug "Will attempt to unlink temporary holders.\n"
  384. debug run_command "${rmbin} -f \"${outputholder}\" \"${errorholder}\""
  385. fi
  386. debug "Method %s responded within timeout specified.\n" "${FUNCNAME[1]}"
  387. unset callermethod; unset safecallerpid; unset outputholder; unset errorholder
  388. unset safetimeout
  389. debug "Left safe mode.\n"
  390. return 0
  391. }
  392.  
  393. safe_lsusb() {
  394. if [ "a$1" != "asafe" -a "a${NOFUNCNAME}" = "a" ]; then
  395. safe_invoke "5" "$@"
  396. return $?
  397. elif [ "a${NOFUNCNAME}" = "a" ]; then
  398. shift
  399. fi
  400. [ "a${lsusbbin}" != "a" ] && ${lsusbbin} "$@"
  401. }
  402.  
  403. safe_chat() {
  404. if [ "a$1" != "asafe" -a "a${NOFUNCNAME}" = "a" ]; then
  405. safe_invoke "10" "$@"
  406. return $?
  407. elif [ "a${NOFUNCNAME}" = "a" ]; then
  408. shift
  409. fi
  410. [ "a${chatbin}" != "a" ] && ${chatbin} "$@"
  411. }
  412.  
  413. safe_cat() {
  414. if [ "a$1" != "asafe" -a "a${NOFUNCNAME}" = "a" ]; then
  415. safe_invoke "5" "$@"
  416. return $?
  417. elif [ "a${NOFUNCNAME}" = "a" ]; then
  418. shift
  419. fi
  420. [ "a${catbin}" != "a" ] && ${catbin} "$@"
  421. }
  422.  
  423. # Formats text
  424. format_text() {
  425. if find_binary "printf"; then
  426. rfmt="$1"
  427. [ "$#" -gt "0" ] && shift
  428. rfmt=`translate_text "${rfmt}"`
  429. if [ "$#" -ge "1" ]; then
  430. ${printfbin} "${rfmt}" "${@}"
  431. else
  432. ${printfbin} "${rfmt}"
  433. fi
  434. unset rfmt
  435. else
  436. echo "${@}"
  437. fi
  438. }
  439.  
  440. # Clears terminal line if required
  441. term_clearline() {
  442. [ "a${COLUMNS}" != "a" ] && find_binary "printf" && [ -n "${interactive}" ] && ${printfbin} "\r%.-${COLUMNS}s\r" "" && return 0
  443. [ "a${COLUMNS}" = "a" ] && find_binary "printf" && [ -n "${interactive}" ] && ${printfbin} "\r%s\r" " " && return 0
  444. }
  445.  
  446. # Prints argument to terminal
  447. term_print() {
  448. term_clearline
  449. echo "${@}"
  450. }
  451.  
  452. # Sends error to terminal, colored if possible.
  453. term_error() {
  454. if find_binary "grep" && [ -n "${interactive}" ]; then
  455. term_print "$@" | ${grepbin} --color "."
  456. else
  457. term_print "$@"
  458. fi
  459. }
  460.  
  461. term_verbose() {
  462. if [ -n "${interactive}" ]; then
  463. if find_binary "grep" && find_binary "tr"; then
  464. currentlyverbosing=yes
  465. term_print "$@" | GREP_COLOR=2 ${grepbin} --color=always "." | ${trbin} "\n" " "
  466. unset currentlyverbosing; lastlineverbose=yes
  467. else
  468. term_print "$@"
  469. fi
  470. fi
  471. }
  472.  
  473. term_notify() {
  474. if find_binary "grep" && [ -n "${interactive}" ]; then
  475. term_print "$@" | GREP_COLOR=1 ${grepbin} --color "."
  476. else
  477. term_print "$@"
  478. fi
  479. }
  480.  
  481. select_example() {
  482. examplevariable=`selection_argument variable "$@"`
  483. exampleoptions=`selection_argument options "$@"`
  484. [ "a${exampleoptions}" != "a" ] && format_text "\nAvailable options are:\n" && format_text "%s\n" "${exampleoptions}"
  485. [ "a${exampleoptions}" = "a" ] && exampleoptions="foo"
  486. format_text "\nExample:\n"
  487. format_text "\t$ %s [...] %s=\"%s\"\n\r\n" "${ME}" "${examplevariable}" "`echo ${exampleoptions} | ${cutbin} -d\ -f1`"
  488. unset exampletitle; unset examplevariable; unset exampleoptions
  489. }
  490.  
  491. term_select_example() {
  492. examplevariable=`selection_argument variable "$@"`
  493. exampleoptions=`selection_argument options "$@"`
  494. [ "a${exampleoptions}" != "a" ] && notify "\nAvailable options are:\n" && notify "%s\n" "${exampleoptions}"
  495. [ "a${exampleoptions}" = "a" ] && exampleoptions="foo"
  496. notify "\nExample:\n"
  497. notify "\t$ %s %s=\"%s\"\n\r\n" "${ME}" "${examplevariable}" "`echo ${exampleoptions} | ${cutbin} -d\ -f1`"
  498. unset exampletitle; unset examplevariable; unset exampleoptions
  499. }
  500.  
  501. term_select() {
  502. options=`selection_argument options "$@" | ${wcbin} -l`; options=`echo ${options}`
  503. variable=`selection_argument variable "$@"`
  504. if [ "a${options}" != "a1" ]; then
  505. exampletitle=`selection_argument title "$@"`
  506. notify "%s by using %s variable, or by enabling interactive mode.\n" "${exampletitle}" "${variable}"
  507. notify "\t$ %s --interactive %s\n" "${ME}" "${allargs}"
  508. unset exampletitle
  509. term_select_example "$@"
  510. else
  511. eval export ${variable}="`selection_argument option 1 "$@"`"
  512. debug "Returning sole available option \"%s\" on behalf of user.\n" "`eval echo \\\${${variable}}`"
  513. unset variable
  514. return 1
  515. fi
  516. unset variable; unset options
  517. stop_with 98
  518. return 98
  519. }
  520.  
  521. term_confirm() {
  522. options=`selection_argument options "$@" | ${wcbin} -l`; options=`echo ${options}`
  523. variable=`selection_argument variable "$@"`
  524. exampletitle=`selection_argument title "$@"`
  525. notify "%s by using --%syes or --%sno switches, or by enabling interactive mode.\n" "${exampletitle}" "${variable}" "${variable}"
  526. notify "\t$ %s --interactive %s\n" "${ME}" "${allargs}"
  527. unset exampletitle
  528. stop_with 98
  529. return 98
  530. }
  531.  
  532. term_prompt() {
  533. variable=`selection_argument variable "$@"`
  534. exampletitle=`selection_argument title "$@"`
  535. notify "%s by using %s variable, or by enabling interactive mode.\n" "${exampletitle}" "${variable}"
  536. notify "\t$ %s --interactive %s\n" "${ME}" "${allargs}"
  537. unset exampletitle
  538. term_select_example "$@"
  539. return 98
  540. }
  541.  
  542. interactive_term_select() {
  543. variable=`selection_argument variable "$@"`
  544. title=`selection_argument title "$@"`
  545. text=`selection_argument text "$@"`
  546. options=`selection_argument options "$@" | ${wcbin} -l` ; options=`echo ${options}`
  547. debug "Prompting user to select variable %s.\n" "${variable}"
  548. [ "a${title}" != "a" ] && notify "%s\n\r\n" "${title}"
  549. [ "a${text}" != "a" ] && ${printfbin} "%s\n\r\n" "${text}"
  550. notify "Available options are:\n"
  551. selection_argument options "$@" | ${grepbin} -n "." | ${sedbin} -e "s/^\([0-9]*\):\(.*\) \(.*\)/\1. \3/g" | GREP_COLOR=1 ${grepbin} --color=AUTO "\([0-9]*\)\."
  552. selection_argument button2 "$@" | ${grepbin} -n "." | ${sedbin} -e "s/^\([0-9]*\):\(.*\)/0. \2/g" | GREP_COLOR=1 ${grepbin} --color=AUTO "\([0-9]*\)\."
  553. format_text "You can automate this selection by setting %s variable.\nEnter \"*\" to discover how.\n" "${variable}" | GREP_COLOR=2 ${grepbin} --color=ALWAYS "."
  554. notify "\r\n"
  555. echo -n "`format_text "Please use numbers %d-%d to perform your selection: " "0" "${options}"`"
  556. read termselection
  557. debug "User typed: \"%s\"\n" "${termselection}"
  558. if [ "a${termselection}" = "a*" ]; then
  559. debug "Showing select example through variable %s.\n" "${variable}"
  560. term_select_example "$@"
  561. interactive_term_select "$@"
  562. selection=$?
  563. else
  564. selection=`${printfbin} "%d\n" "${termselection}" 2> /dev/null`
  565. [ "${selection}" -gt "${options}" ] && selection=0
  566. [ "${selection}" -eq "0" ] && selection=98
  567. [ "${selection}" -gt "0" -a "${selection}" -le "${options}" ] && eval export ${variable}="`selection_argument option ${selection} "$@"`"
  568. fi
  569. debug "Considering selection: %d\n" "${selection}"
  570. unset variable; unset title; unset text; unset options; unset termselection
  571. [ "a${DEBUG}" = "a" ] && find_binary "clear" && ${clearbin}
  572. return "${selection}"
  573. }
  574.  
  575. interactive_term_confirm() {
  576. variable=`selection_argument variable "$@"`
  577. title=`selection_argument title "$@"`
  578. text=`selection_argument text "$@"`
  579. debug "Prompting user to select variable %s.\n" "${variable}"
  580. [ "a${title}" != "a" ] && notify "%s\n\r\n" "${title}"
  581. [ "a${text}" != "a" ] && ${printfbin} "%s\n\r\n" "${text}"
  582. notify "Available options are:\n"
  583. selection_argument button1 "$@" | ${sedbin} -e "s/^\(.*\)$/1. \1/g" | GREP_COLOR=1 ${grepbin} --color=AUTO "^\([0-9]*\)\."
  584. selection_argument button2 "$@" | ${sedbin} -e "s/^\(.*\)$/2. \1/g" | GREP_COLOR=1 ${grepbin} --color=AUTO "^\([0-9]*\)\."
  585. [ "a$6" != "areset" ] && format_text "You can automate this selection by setting --%syes or --%sno switches.\n" "${variable}" "${variable}" | GREP_COLOR=2 ${grepbin} --color=ALWAYS "."
  586. notify "\r\n"
  587. echo -n "`format_text "Please use numbers %d-%d to perform your selection: " "1" "2"`"
  588. read termselection
  589. debug "User typed: \"%s\"\n" "${termselection}"
  590. if [ "a${termselection}" = "a1" ]; then
  591. debug "User selected \"yes\".\n"
  592. selection=0
  593. elif [ "a${termselection}" = "a2" ]; then
  594. debug "User selected \"no\".\n"
  595. selection=1
  596. else
  597. interactive_term_confirm "$@"
  598. selection=$?
  599. return ${selection}
  600. fi
  601. unset variable; unset title; unset text; unset options; unset termselection
  602. [ "a${DEBUG}" = "a" ] && find_binary "clear" && ${clearbin}
  603. return "${selection}"
  604. }
  605.  
  606. interactive_term_prompt() {
  607. variable=`selection_argument variable "$@"`
  608. title=`selection_argument title "$@"`
  609. text=`selection_argument text "$@"`
  610. debug "Prompting user to type variable %s.\n" "${variable}"
  611. [ "a${title}" != "a" ] && notify "%s\n" "${title}"
  612. format_text "You can automate this selection by setting %s variable.\nEnter \"*\" to discover how.\n" "${variable}" | GREP_COLOR=2 ${grepbin} --color=ALWAYS "."
  613. notify "\r\n"
  614. [ "a${text}" != "a" ] && ${printfbin} "%s: " "${text}"
  615. read termselection
  616. termselection=`sanitize "${termselection}"`
  617. termselection=`echo "${termselection}" | ${sedbin} -e "s/^ *//g" | ${sedbin} -e "s/ *$//g"`
  618. debug "User typed: \"%s\"\n" "${termselection}"
  619. if [ "a${termselection}" = "a*" ]; then
  620. debug "Showing select example through variable %s.\n" "${variable}"
  621. term_select_example "$@"
  622. interactive_term_prompt "$@"
  623. selection=$?
  624. elif [ "a${termselection}" = "a" ]; then
  625. debug "User pressed enter which indicates to abort.\n"
  626. selection=98
  627. else
  628. eval "export ${variable}=\"${termselection}\""
  629. selection=0
  630. fi
  631. debug "Considering selection: %d\n" "${selection}"
  632. unset variable; unset title; unset text; unset termselection
  633. [ "a${DEBUG}" = "a" ] && find_binary "clear" && ${clearbin}
  634. return "${selection}"
  635. }
  636.  
  637. nine_verbose() {
  638. term_verbose "$@"
  639. }
  640.  
  641. nine_fixcolors() {
  642. status_connected
  643. [ "a${NINEISP}" != "a${ISPID}" -o "a${ISPID}" = "a" ] && unset NINEBGCOLOR && unset NINEFGCOLOR
  644. [ "a${NINEFGCOLOR}" = "a" ] && NINEFGCOLOR=`echo ${ISP_FGCOLOR} | ${sedbin} -e "s/^\(..\)\(..\)\(..\)$/rgb:\1\/\2\/\3/g"`
  645. [ "a${NINEBGCOLOR}" = "a" ] && NINEBGCOLOR=`echo ${ISP_BGCOLOR} | ${sedbin} -e "s/^\(..\)\(..\)\(..\)$/rgb:\1\/\2\/\3/g"`
  646. [ "a${ISPID}" = "a" ] && unset NINEBGCOLOR && unset NINEFGCOLOR
  647. [ "a${NINEFGCOLOR}" = "a" ] && NINEFGCOLOR="rgb:00/00/00"
  648. [ "a${NINEBGCOLOR}" = "a" ] && NINEBGCOLOR="rgb:ec/ec/ec"
  649. [ "a${NINEISP}" = "a" ] && NINEISP="${ISPID}"
  650. export NINEISP; export NINEFGCOLOR; export NINEBGCOLOR
  651. }
  652.  
  653. nine_notify() {
  654. unset nineerrormode
  655. ! find_binary "9menu" && return 1
  656. if [ "a${1}" = "aerror" ]; then
  657. nineerrormode=1
  658. shift
  659. debug "Going into error mode.\n"
  660. else
  661. debug "Normal notification.\n"
  662. fi
  663. ninetext=`echo "$@" | ${sedbin} -e "s/:/;/g"`
  664. need_arg "MENUFONT"
  665. need_arg "foldwrapping"
  666. if find_binary "fold"; then
  667. ninetext=`echo "${ninetext}" | ${foldbin} -s -w ${foldwrapping}`
  668. else
  669. ninetext=`echo "${ninetext}" | ${sedbin} -e "s/^\(............................................................\)\(.*\)$/\1\\n\2/g"`
  670. fi
  671. ninetext=`echo "${ninetext}" | ${sedbin} -e "s/^\(.*\)$/\"\1:\"/g" | ${trbin} " " " " | ${trbin} "\n" " "`
  672. debug "Displaying text to user through 9menu:%s\n" " $@ "
  673. debug "Displaying text to user through 9menu:%s\n" " ${ninetext} "
  674. if [ "a${nineerrormode}" = "a1" ]; then
  675. result=`eval "${ninemenubin} -font \"${MENUFONT}\" -bg \"rgb:ff/80/80\" -fg white -display \"${DISPLAY}\" -label \"\`translate_text "Error"\`\" ${ninetext} \":\" \"OK:exec echo OK-\"" 2> /dev/null | ${grepbin} "^OK-" | ${cutbin} -d- -f1`
  676. else
  677. nine_fixcolors
  678. result=`eval "${ninemenubin} -font \"${MENUFONT}\" -bg ${NINEBGCOLOR} -fg ${NINEFGCOLOR} -display \"${DISPLAY}\" -label \"\`translate_text "Notification"\`\" ${ninetext} \":\" \"OK:exec echo OK-\"" 2> /dev/null | ${grepbin} "^OK-" | ${cutbin} -d- -f1`
  679. fi
  680. if [ "a${result}" = "aOK" ]; then
  681. debug "User saw message.\n"
  682. else
  683. debug "9menu died or user closed it.\n"
  684. fi
  685. unset ninetext; unset result; unset nineerrormode
  686. }
  687.  
  688. nine_error() {
  689. nine_notify "error" "$@"
  690. }
  691.  
  692. nine_select() {
  693. ! find_binary "9menu" && return 1
  694. nine_fixcolors
  695. need_arg "MENUFONT"
  696. need_arg "foldwrapping"
  697. debug "Prompting user to select variable %s with 9menu.\n" "${variable}"
  698. variable=`selection_argument variable "$@"`
  699. title=`selection_argument title "$@"`
  700. text=`selection_argument text "$@"`
  701. options=`selection_argument options "$@" | ${wcbin} -l` ; options=`echo ${options}`
  702. helptext=`format_text "You can automate this selection by setting %s variable on command line, click here to discover how.\n" "${variable}" | ${sedbin} -e "s/:/;/g"`
  703. localbutton2=`selection_argument button2 "$@"`; [ "a${localbutton2}" = "a" ] && localbutton2="Cancel"
  704. ninetext=`echo "${text}" | ${sedbin} -e "s/:/;/g"`
  705. if find_binary "fold"; then
  706. ninetext=`echo "${ninetext}" | ${foldbin} -s -w ${foldwrapping}`
  707. helptext=`echo "${helptext}" | ${foldbin} -s -w ${foldwrapping}`
  708. else
  709. ninetext=`echo "${ninetext}" | ${sedbin} -e "s/^\(............................................................\)\(.*\)$/\1\\n\2/g"`
  710. helptext=`echo "${helptext}" | ${sedbin} -e "s/^\(............................................................\)\(.*\)$/\1\\n\2/g"`
  711. fi
  712. ninetext=`echo "${ninetext}" | ${sedbin} -e "s/^\(.*\)$/\"\1:\"/g" | ${trbin} " " " " | ${trbin} "\n" " "`
  713. helptext=`echo "${helptext}" | ${sedbin} -e "s/^\(.*\)$/\"\1:exec echo OPTION-AUTOMATE\"/g" | ${trbin} " " " " | ${trbin} "\n" " "`
  714. localarguments=`selection_argument options "$@" | ${sedbin} -e "s/:/;/g" | ${grepbin} -n "." | ${sedbin} -e "s/^\([0-9]*\):\(.*\) \(.*\)$/\"\1. \3:exec echo OPTION-\1\"/g" | ${trbin} "\n" " "`
  715. termselection=`eval "${ninemenubin} -font \"${MENUFONT}\" -bg ${NINEBGCOLOR} -fg ${NINEFGCOLOR} -display \"${DISPLAY}\" -label \"${title}\" ${ninetext} \":\" ${localarguments} \":\" ${helptext} \":\" \"${localbutton2}:exec echo OPTION-9menuexit\"" 2> /dev/null | ${grepbin} "^OPTION-" | ${cutbin} -d- -f2-`
  716. debug "User selected: \"%s\"\n" "${termselection}"
  717. if [ "a${termselection}" = "aAUTOMATE" ]; then
  718. localtext=`format_text "You can automate this selection by setting %s variable on command line.\n" "${variable}"`
  719. notify "${localtext}\n`select_example "$@"`"
  720. nine_select "$@"
  721. selection=$?
  722. return ${selection}
  723. elif [ "a${termselection}" = "a9menuexit" -o "a${termselection}" = "a" ]; then
  724. debug "User selected button: %s\n" "${localbutton2}"
  725. selection=98
  726. else
  727. termselection=`echo "${termselection}" | ${trbin} -t "." ""`
  728. selection=`${printfbin} "%d\n" "${termselection}" 2> /dev/null`
  729. [ "${selection}" -gt "${options}" ] && selection=0
  730. [ "${selection}" -eq "0" ] && selection=98
  731. [ "${selection}" -gt "0" -a "${selection}" -le "${options}" ] && eval export ${variable}="`selection_argument option ${selection} "$@"`"
  732. fi
  733. debug "Considering selection: %d\n" "${selection}"
  734. unset variable; unset title; unset text; unset ninetext; unset helptext; unset options; unset termselection; unset buttonselection; unset localtext; unset localarguments; unset localbutton2
  735. return "${selection}"
  736. }
  737.  
  738. nine_confirm() {
  739. ! find_binary "9menu" && return 1
  740. nine_fixcolors
  741. need_arg "MENUFONT"
  742. need_arg "foldwrapping"
  743. variable=`selection_argument variable "$@"`
  744. title=`selection_argument title "$@"`
  745. ninetext=`selection_argument text "$@" | ${sedbin} -e "s/:/;/g"`
  746. localbutton1=`selection_argument button1 "$@"`; [ "a${localbutton1}" = "a" ] && localbutton1="OK"
  747. localbutton2=`selection_argument button2 "$@"`; [ "a${localbutton2}" = "a" ] && localbutton2="Cancel"
  748. debug "Prompting user to confirm variable %s with 9menu.\n" "${variable}"
  749. helptext=""
  750. [ "a$6" != "areset" ] && helptext=`format_text "You can automate this selection by setting --%syes or --%sno switches.\n" "${variable}" "${variable}" | ${sedbin} -e "s/:/;/g"`
  751. if find_binary "fold"; then
  752. ninetext=`echo "${ninetext}" | ${foldbin} -s -w ${foldwrapping}`
  753. helptext=`echo "${helptext}" | ${foldbin} -s -w ${foldwrapping}`
  754. else
  755. ninetext=`echo "${ninetext}" | ${sedbin} -e "s/^\(............................................................\)\(.*\)$/\1\\n\2/g"`
  756. helptext=`echo "${helptext}" | ${sedbin} -e "s/^\(............................................................\)\(.*\)$/\1\\n\2/g"`
  757. fi
  758. ninetext=`echo "${ninetext}" | ${sedbin} -e "s/^\(.*\)$/\"\1:\"/g" | ${trbin} " " " " | ${trbin} "\n" " "`
  759. helptext=`echo "${helptext}" | ${sedbin} -e "s/^\(.*\)$/\"\1:\"/g" | ${trbin} " " " " | ${trbin} "\n" " "`
  760. localarguments="\"${localbutton1}:exec echo Answer-Yes\" \"${localbutton2}:exec echo Answer-No\""
  761. termselection=`eval "${ninemenubin} -font \"${MENUFONT}\" -bg ${NINEBGCOLOR} -fg ${NINEFGCOLOR} -display \"${DISPLAY}\" -label \"${title}\" ${ninetext} \":\" ${localarguments} \":\" ${helptext} \":\"" 2> /dev/null | ${grepbin} "^Answer-" | ${cutbin} -d- -f2-`
  762. debug "User selected: \"%s\"\n" "${termselection}"
  763. if [ "a${termselection}" = "aYes" ]; then
  764. selection=0
  765. else
  766. selection=1
  767. fi
  768. debug "Considering selection: %d\n" "${selection}"
  769. unset variable; unset title; unset text; unset ninetext; unset helptext; unset options; unset termselection; unset buttonselection; unset localtext; unset localarguments; unset localbutton2; unset localbutton1
  770. return "${selection}"
  771. }
  772.  
  773. nine_prompt() {
  774. ! find_binary "xterm" && return 1
  775. variable=`selection_argument variable "$@"`
  776. title=`selection_argument title "$@"`
  777. text=`selection_argument text "$@"`
  778. debug "Prompting user to type variable %s through xterm.\n" "${variable}"
  779. localtext=`format_text "You can automate this selection by setting %s variable.\n" "${variable}"`
  780. [ "a${text}" != "a" ] && localtext=`${printfbin} "%s\n\n%s: " "${localtext}" "${text}"`
  781. locallines=`echo "${localtext}" | ${wcbin} -l`; locallines=`echo ${locallines}`; locallines=`expr ${locallines} + 10`; locallines=`echo ${locallines}`;
  782. ${printfbin} "%s\n" "${localtext}" > "/tmp/sakis3g.xterm.pipe.$$"
  783. ${xtermbin} -T "${title}" +cm +dc -e sh -c "${catbin} /tmp/sakis3g.xterm.pipe.$$; read XTERMANSWER; set >> /tmp/sakis3g.xterm.pipe.$$;" 2> /dev/null
  784. debug show_file "/tmp/sakis3g.xterm.pipe.$$"
  785. termselection=`${grepbin} "^XTERMANSWER=" "/tmp/sakis3g.xterm.pipe.$$" 2> /dev/null | ${cutbin} -d= -f2- | ${sedbin} -e "s/^\"\(.*\)\"$/\1/g" | ${sedbin} -e "s/^\'\(.*\)\'$/\1/g"`; ${rmbin} -f "/tmp/sakis3g.xterm.pipe.$$"
  786. termselection=`sanitize "${termselection}"`
  787. debug "User typed: \"%s\"\n" "${termselection}"
  788. # TODO: Implement example
  789. if [ "a${termselection}" != "a" ]; then
  790. debug "User typed: %s\n" "${termselection}"
  791. eval "export ${variable}=\"${termselection}\""
  792. selection=0
  793. elif [ "a${termselection}" = "a" ]; then
  794. debug "User requested to cancel.\n"
  795. selection=98
  796. else
  797. debug "Unknown error.\n"
  798. selection=99
  799. fi
  800. debug "Considering selection: %d\n" "${selection}"
  801. unset variable; unset title; unset text; unset termselection; unset buttonselection; unset localtext
  802. return "${selection}"
  803. }
  804.  
  805. dialog_error() {
  806. dialog_cleanscreen
  807. if [ "a${SGUI}" = "adialog" ]; then
  808. ! find_binary "dialog" && return 1
  809. ${dialogbin} --backtitle "Sakis3G ${MYVERSION}" --colors --title "\Z1\Zb`translate_text "Error occured"`" --ok-label `translate_text "OK"` --clear --colors --aspect 30 --msgbox "\Z1\ZB$@" 0 0
  810. elif [ "a${SGUI}" = "aXdialog" ]; then
  811. ! find_binary "Xdialog" && return 1
  812. ${Xdialogbin} --backtitle `translate_text "Error occured"` --title "Sakis3G ${MYVERSION}" --ok-label `translate_text "OK"` --msgbox "$@ " 0 0
  813. elif [ "a${SGUI}" = "azenity" ]; then
  814. ! find_binary "zenity" && return 1
  815. ${zenitybin} --title "`translate_text "Error occured"`" --error --text "$@ "
  816. elif [ "a${SGUI}" = "akdialog" ]; then
  817. ! find_binary "kdialog" && return 1
  818. ${kdialogbin} --title "`translate_text "Error occured"`" --error "$@ " > /dev/null 2> /dev/null
  819. elif [ "a${SGUI}" = "awhiptail" ]; then
  820. ! find_binary "whiptail" && return 1
  821. ${whiptailbin} --backtitle "Sakis3G ${MYVERSION}" --title "`translate_text "Error occured"`" --clear --msgbox "$@ " 0 0
  822. fi
  823. }
  824.  
  825. dialog_notify() {
  826. dialog_cleanscreen
  827. if [ "a${SGUI}" = "adialog" ]; then
  828. ! find_binary "dialog" && return 1
  829. ${dialogbin} --backtitle "Sakis3G ${MYVERSION}" --title `translate_text "Notification"` --ok-label `translate_text "OK"` --clear --aspect 30 --msgbox "$@" 0 0
  830. elif [ "a${SGUI}" = "aXdialog" ]; then
  831. ! find_binary "Xdialog" && return 1
  832. ${Xdialogbin} --title "Sakis3G ${MYVERSION}" --ok-label `translate_text "OK"` --msgbox "$@" 0 0
  833. elif [ "a${SGUI}" = "azenity" ]; then
  834. ! find_binary "zenity" && return 1
  835. ${zenitybin} --title "Sakis3G ${MYVERSION}" --info --text "$@ "
  836. elif [ "a${SGUI}" = "akdialog" ]; then
  837. ! find_binary "kdialog" && return 1
  838. ${kdialogbin} --title "Sakis3G ${MYVERSION}" --msgbox "$@ " > /dev/null 2> /dev/null
  839. elif [ "a${SGUI}" = "awhiptail" ]; then
  840. ! find_binary "whiptail" && return 1
  841. ${whiptailbin} --backtitle "Sakis3G ${MYVERSION}" --title `translate_text "Notification"` --clear --msgbox "$@" 0 0
  842. fi
  843. }
  844.  
  845. dialog_cleanscreen() {
  846. if [ "a${SGUI}" = "aXdialog" ]; then
  847. if [ "a${XdialogVerbose}" != "a" -a "a${XdialogVerbose}" != "a0" ]; then
  848. ! notrunning "${XdialogVerbose}" && find_binary "kill" && ${killbin} -1 "${XdialogVerbose}" 2> /dev/null
  849. unset XdialogVerbose
  850. fi
  851. unset currentlyverbosing
  852. elif [ "a${SGUI}" = "azenity" ]; then
  853. [ -f "/tmp/sakis3g.zenity.pipe" ] && ${printfbin} "\n100\n" >> "/tmp/sakis3g.zenity.pipe"
  854. [ -f "/tmp/sakis3g.zenity.pipe" ] && debug run_command "${rmbin} -f /tmp/sakis3g.zenity.pipe"
  855. [ "a${zenityverbosepid}" != "a" ] && ! notrunning "${zenityverbosepid}" && ${killbin} -1 "${zenityverbosepid}" 2> /dev/null
  856. [ "a${zenityverbosepid}" != "a" ] && ! notrunning "${zenityverbosepid}" && ${killbin} -9 "${zenityverbosepid}" 2> /dev/null
  857. unset zenityverbosepid
  858. elif [ "a${SGUI}" = "akdialog" ]; then
  859. ! find_binary "kdialog" && return 1
  860. if [ "a${kdialogaddress}" != "a" ]; then
  861. dcopbased=`echo ${kdialogaddress} | ${grepbin} -i "dcop"`
  862. if [ "a${dcopbased}" != "a" ]; then
  863. if find_binary "dcop"; then
  864. debug run_command "${dcopbin} \"${kdialogaddress}\" close"
  865. fi
  866. else
  867. ! find_binary "dbus-send" && find_binary "qdbus"
  868. if [ "a${dbus_sendbin}" != "a" ]; then
  869. debug run_command "${dbus_sendbin} --print-reply --dest=${kdialogaddress} \"org.kde.kdialog.ProgressDialog.close\""
  870. elif [ "a${qdbusbin}" != "a" ]; then
  871. debug run_command "${qdbusbin} ${kdialogaddress} \"org.kde.kdialog.ProgressDialog.close\""
  872. fi
  873. fi
  874. else
  875. term_clearline
  876. fi
  877. unset kdialogaddress
  878. fi
  879. }
  880.  
  881. dialog_verbose() {
  882. if [ "a${SGUI}" = "adialog" ]; then
  883. ! find_binary "dialog" && return 1
  884. verbwidth=`echo "${COLUMNS}"`; verbwidth=`expr ${verbwidth} + 1 - 1`; verbwidth=`echo ${verbwidth}`
  885. verbheight=0; [ "${verbwidth}" -gt "40" ] && verbheight=3
  886. currentlyverbosing=yes
  887. ${dialogbin} --backtitle "Sakis3G ${MYVERSION}" --title "Working" --aspect 40 --infobox "$@..." ${verbheight} ${verbwidth}
  888. unset currentlyverbosing; lastlineverbose=yes; unset verbwidth; unset verbheight
  889. elif [ "a${SGUI}" = "aXdialog" ]; then
  890. # ! find_binary "Xdialog" && return 1
  891. # ${Xdialogbin} --title "Sakis3G is working" --infobox "$@..." 0 0 30000 &
  892. # XdialogVerbose=$!; currentlyverbosing=yes
  893. term_verbose "$@"
  894. elif [ "a${SGUI}" = "akdialog" ]; then
  895. ! find_binary "kdialog" && return 1
  896. if ! find_binary "dbus-send" && ! find_binary "qdbus"; then
  897. term_verbose "$@"; return 0
  898. fi
  899. if [ "a${kdialogaddress}" = "a" ]; then
  900. temptext=`translate_text Working...`
  901. kdialogaddress=`${kdialogbin} --title "Sakis3G ${MYVERSION}" --progressbar "${temptext}" 100`
  902. # kdialogaddress=`${kdialogbin} --title "Sakis3G ${MYVERSION}" --progressbar "${temptext}" 100 2> /dev/null`
  903. debug "Returned 1\n"
  904. unset temptext
  905. [ "a${kdialogaddress}" != "a" ] && export kdialogaddress
  906. fi
  907. debug "Returned 2\n"
  908. if [ "a${kdialogaddress}" != "a" ]; then
  909. dcopbased=`echo ${kdialogaddress} | ${grepbin} -i "dcop"`
  910. if [ "a${dcopbased}" != "a" ]; then
  911. if find_binary "dcop"; then
  912. debug run_command "${dcopbin} --user \"${runner}\" --all-sessions \"${kdialogaddress}\" setProgress ${verbosecurrentcount}"
  913. debug run_command "${dcopbin} --user \"${runner}\" --all-sessions \"${kdialogaddress}\" setLabel \"$@\""
  914. fi
  915. else
  916. if [ "a${kdialogaddress}" != "a" ] && [ "a${dbus_sendbin}" != "a" ]; then
  917. debug run_command "${dbus_sendbin} --print-reply --dest=${kdialogaddress} \"org.freedesktop.DBus.Properties.Set\" string:'org.kde.kdialog.ProgressDialog' string:'value' variant:int32:${verbosecurrentcount}"
  918. debug run_command "${dbus_sendbin} --print-reply --dest=${kdialogaddress} \"org.kde.kdialog.ProgressDialog.setLabelText\" \"string:$@\""
  919. elif [ "a${kdialogaddress}" != "a" ] && [ "a${qdbusbin}" != "a" ]; then
  920. debug run_command "qdbus ${kdialogaddress} \"org.kde.kdialog.ProgressDialog.setLabelText\" \"$@\""
  921. debug run_command "qdbus ${kdialogaddress} \"org.freedesktop.DBus.Properties.Set\" \"\" \"value\" \"${verbosecurrentcount}\""
  922. fi
  923. fi
  924. else
  925. term_verbose "$@"
  926. fi
  927. elif [ "a${SGUI}" = "azenity" ]; then
  928. ! find_binary "zenity" && return 1
  929. [ "a${zenityverbosepid}" != "a" ] && notrunning "${zenityverbosepid}" && unset zenityverbosepid
  930. [ ! -f "/tmp/sakis3g.zenity.pipe" ] && unset zenityverbosepid
  931. if [ "a${zenityverbosepid}" = "a" ] && find_binary "rm" && find_binary "tail" && find_binary "kill" && find_binary "printf" && find_binary "touch" && find_binary "chmod" && find_binary "setsid"; then
  932. debug "Establishing zenity verbose helper.\n"
  933. [ -f "/tmp/sakis3g.zenity.pipe" ] && ${printfbin} "\n100\n" >> "/tmp/sakis3g.zenity.pipe"
  934. [ -f "/tmp/sakis3g.zenity.pipe" ] && debug run_command "${rmbin} -f /tmp/sakis3g.zenity.pipe"
  935. debug run_command "${touchbin} /tmp/sakis3g.zenity.pipe"
  936. debug run_command "${chmodbin} 666 /tmp/sakis3g.zenity.pipe"
  937. if [ -f "/tmp/sakis3g.zenity.pipe" ]; then
  938. eval ${tailbin} -f "/tmp/sakis3g.zenity.pipe" "2> /dev/null" | ${zenitybin} --title="Sakis3G ${MYVERSION}" --text="`translate_text Working...`" --progress --percentage=0 --auto-close --auto-kill "> /dev/null 2> /dev/null" &
  939. zenityverbosepid=$!
  940. eval "disown -a" > /dev/null 2> /dev/null
  941. export zenityverbosepid
  942. fi
  943. fi
  944. [ "a${zenityverbosepid}" != "a" ] && notrunning "${zenityverbosepid}" && unset zenityverbosepid
  945. [ "a${zenityverbosepid}" = "a" ] && term_verbose "$@" && return 1
  946. ${printfbin} "\n%d\n# $@...\n\n" "${verbosecurrentcount}" >> "/tmp/sakis3g.zenity.pipe"
  947. elif [ "a${SGUI}" = "awhiptail" ]; then
  948. term_verbose "$@"
  949. fi
  950. }
  951.  
  952. dialog_confirm() {
  953. dialog_cleanscreen
  954. if [ "a${SGUI}" = "adialog" ]; then
  955. ! find_binary "dialog" && return 1
  956. elif [ "a${SGUI}" = "aXdialog" ]; then
  957. ! find_binary "Xdialog" && return 1
  958. elif [ "a${SGUI}" = "azenity" ]; then
  959. ! find_binary "zenity" && return 1
  960. elif [ "a${SGUI}" = "akdialog" ]; then
  961. ! find_binary "kdialog" && return 1
  962. elif [ "a${SGUI}" = "awhiptail" ]; then
  963. ! find_binary "whiptail" && return 1
  964. fi
  965. variable=`selection_argument variable "$@"`
  966. title=`selection_argument title "$@"`
  967. text=`selection_argument text "$@"`
  968. debug "Prompting user to select yes%s or no%s.\n" "${variable}" "${variable}"
  969. localtext=""
  970. [ "a$6" != "areset" ] && localtext=`format_text "You can automate this selection by setting --%syes or --%sno switches.\n" "${variable}" "${variable}"`
  971. if [ "a${SGUI}" = "adialog" ]; then
  972. [ "a${text}" != "a" ] && localtext=`${printfbin} "%s\\\\\\n\\\\\\n%s\n" "${text}" "${localtext}"`
  973. ${dialogbin} --backtitle "Sakis3G ${MYVERSION}" --title " ${title} " --clear --cr-wrap --yesno "${localtext}" 0 0 2> /dev/null
  974. buttonselection=$?
  975. elif [ "a${SGUI}" = "aXdialog" ]; then
  976. [ "a${text}" != "a" ] && localtext=`${printfbin} "%s\\\\\\n\\\\\\n%s\n" "${text}" "${localtext}"`
  977. ${Xdialogbin} --backtitle " ${title} " --title "Sakis3G ${MYVERSION}" --wrap --fill --yesno "${localtext}" 0 0 2> /dev/null
  978. buttonselection=$?
  979. elif [ "a${SGUI}" = "akdialog" ]; then
  980. [ "a${text}" != "a" ] && localtext=`${printfbin} "%s\\\\\\n\\\\\\n%s\n" "${text}" "${localtext}"`
  981. ${kdialogbin} --title " ${title} " --yesno "${text}" 2> /dev/null
  982. buttonselection=$?
  983. elif [ "a${SGUI}" = "azenity" ]; then
  984. need_arg "foldwrapping"
  985. text=`echo "${text}" | ${sedbin} -e "s/_/__/g"`
  986. localtext=`echo "${localtext}" | ${sedbin} -e "s/_/__/g"`
  987. # if find_binary "fold"; then
  988. # localtext=`echo "${localtext}" | ${foldbin} -s -w ${foldwrapping}`
  989. # text=`echo "${text}" | ${foldbin} -s -w ${foldwrapping}`
  990. # fi
  991. [ "a${text}" != "a" ] && localtext=`${printfbin} "%s\\\\\\n\\\\\\n%s\n" "${text}" "${localtext}"`
  992. ${zenitybin} --title " ${title} " --question --text "${localtext}" 2> /dev/null
  993. buttonselection=$?
  994. elif [ "a${SGUI}" = "awhiptail" ]; then
  995. [ "a${text}" != "a" ] && localtext=`${printfbin} "%s\\\\\\n\\\\\\n%s\n" "${text}" "${localtext}"`
  996. ${whiptailbin} --backtitle "Sakis3G ${MYVERSION}" --title " ${title} " --clear --yesno "${localtext}" 0 0 2> /dev/null
  997. buttonselection=$?;
  998. fi
  999. if [ "a${buttonselection}" = "a0" ]; then
  1000. debug "User selected \"yes\" (%d).\n" "${buttonselection}"
  1001. return 0
  1002. else
  1003. debug "User selected \"no\" (%d).\n" "${buttonselection}"
  1004. return 1
  1005. fi
  1006. }
  1007.  
  1008. dialog_select() {
  1009. dialog_cleanscreen
  1010. if [ "a${SGUI}" = "adialog" ]; then
  1011. ! find_binary "dialog" && return 1
  1012. elif [ "a${SGUI}" = "aXdialog" ]; then
  1013. ! find_binary "Xdialog" && return 1
  1014. elif [ "a${SGUI}" = "azenity" ]; then
  1015. ! find_binary "zenity" && return 1
  1016. elif [ "a${SGUI}" = "akdialog" ]; then
  1017. ! find_binary "kdialog" && return 1
  1018. elif [ "a${SGUI}" = "awhiptail" ]; then
  1019. ! find_binary "whiptail" && return 1
  1020. fi
  1021. variable=`selection_argument variable "$@"`
  1022. title=`selection_argument title "$@"`
  1023. text=`selection_argument text "$@"`
  1024. options=`selection_argument options "$@" | ${wcbin} -l` ; options=`echo ${options}`
  1025. debug "Prompting user to select variable %s.\n" "${variable}"
  1026. localtext=`format_text "You can automate this selection by setting %s variable on command line.\n" "${variable}"`
  1027. [ "a${text}" != "a" ] && localtext=`${printfbin} "%s\\\\\\n\\\\\\n%s\n" "${localtext}" "${text}"`
  1028. localbutton2=`selection_argument button2 "$@"`; [ "a${localbutton2}" = "a" ] && localbutton2="Cancel"
  1029. if [ "a${SGUI}" = "adialog" ]; then
  1030. localarguments=`selection_argument options "$@" | ${grepbin} -n "." | ${sedbin} -e "s/^\([0-9]*\):\(.*\) \(.*\)$/\"\1.\" \"\3\" \"${variable}=\\\\\"\2\\\\\"\"/g"`
  1031. eval ${dialogbin} --backtitle \"Sakis3G ${MYVERSION}\" --title \" ${title} \" --ok-label \"`translate_text "OK"`\" --cancel-label \"${localbutton2}\" --item-help --clear --cr-wrap --menu \"${localtext}\" 0 0 0 ${localarguments} 2> "/tmp/sakis3g.dialog.$$"
  1032. buttonselection=$?
  1033. elif [ "a${SGUI}" = "aXdialog" ]; then
  1034. localarguments=`selection_argument options "$@" | ${grepbin} -n "." | ${sedbin} -e "s/^\([0-9]*\):\(.*\) \(.*\)$/\"\1.\" \"\3\" \"${variable}=\\\\\"\2\\\\\"\"/g"`
  1035. eval ${Xdialogbin} --backtitle \" ${title} \" --title \"Sakis3G ${MYVERSION}\" --ok-label \"`translate_text "OK"`\" --cancel-label \"${localbutton2}\" --item-help --wrap --fill --menubox \""${localtext}"\" 0 0 0 ${localarguments} 2> "/tmp/sakis3g.dialog.$$"
  1036. buttonselection=$?
  1037. elif [ "a${SGUI}" = "akdialog" ]; then
  1038. localarguments=`selection_argument options "$@" | ${grepbin} -n "." | ${sedbin} -e "s/^\([0-9]*\):\(.*\) \(.*\)$/\"\1.\" \"\3\"/g"`
  1039. eval ${kdialogbin} --title \" ${title} \" --menu \""${text}"\" ${localarguments} \"AUTOMATE\" \"`translate_text "Help with this question"`\" > "/tmp/sakis3g.dialog.$$" 2> /dev/null
  1040. buttonselection=$?
  1041. elif [ "a${SGUI}" = "azenity" ]; then
  1042. need_arg "foldwrapping"
  1043. localarguments=`selection_argument options "$@" | ${grepbin} -n "." | ${sedbin} -e "s/^\([0-9]*\):\(.*\) \(.*\)$/\"\1.\" \"\3\"/g"`
  1044. localtext=`echo "${text}" | ${sedbin} -e "s/_/__/g"`
  1045. find_binary "fold" && localtext=`echo "${localtext}" | ${foldbin} -s -w ${foldwrapping}`
  1046. eval ${zenitybin} --title \" ${title} \" --list --text \""${localtext}"\" --hide-column 1 --column Index --column `translate_text "Option"` ${localarguments} \"AUTOMATE\" \"`translate_text "Help with this question"`\" > "/tmp/sakis3g.dialog.$$"
  1047. buttonselection=$?
  1048. elif [ "a${SGUI}" = "awhiptail" ]; then
  1049. localarguments=`selection_argument options "$@" | ${grepbin} -n "." | ${sedbin} -e "s/^\([0-9]*\):\(.*\) \(.*\)$/\"\1. \3\" \"\2\"/g"`
  1050. eval ${whiptailbin} --backtitle \"Sakis3G ${MYVERSION}\" --title \" ${title} \" --noitem --clear --menu \"${localtext}\" 0 0 0 ${localarguments} 2> "/tmp/sakis3g.dialog.$$"
  1051. buttonselection=$?;
  1052. fi
  1053. termselection=`${catbin} "/tmp/sakis3g.dialog.$$" 2> /dev/null`; ${rmbin} -f "/tmp/sakis3g.dialog.$$"
  1054. debug "User selected: \"%s\"\n" "${termselection}"
  1055. if [ "a${termselection}" = "aAUTOMATE" ]; then
  1056. variable=`selection_argument variable "$@"`
  1057. localtext=`format_text "You can automate this selection by setting %s variable on command line.\n" "${variable}"`
  1058. notify "${localtext}\n`select_example "$@" | ${sedbin} -e "s/ /\\\n /g"`"
  1059. dialog_select "$@"
  1060. selection=$?
  1061. return ${selection}
  1062. elif [ "a${buttonselection}" != "a0" ]; then
  1063. debug "User selected button: %d\n" "${buttonselection}"
  1064. selection=98
  1065. else
  1066. termselection=`echo "${termselection}" | ${trbin} -t "." ""`
  1067. selection=`${printfbin} "%d\n" "${termselection}" 2> /dev/null`
  1068. [ "${selection}" -gt "${options}" ] && selection=0
  1069. [ "${selection}" -eq "0" ] && selection=98
  1070. [ "${selection}" -gt "0" -a "${selection}" -le "${options}" ] && eval export ${variable}="`selection_argument option ${selection} "$@"`"
  1071. fi
  1072. debug "Considering selection: %d\n" "${selection}"
  1073. unset variable; unset title; unset text; unset options; unset termselection; unset buttonselection; unset localtext; unset localarguments; unset localbutton2
  1074. return "${selection}"
  1075. }
  1076.  
  1077. dialog_prompt() {
  1078. dialog_cleanscreen
  1079. if [ "a${SGUI}" = "adialog" ]; then
  1080. ! find_binary "dialog" && return 1
  1081. elif [ "a${SGUI}" = "aXdialog" ]; then
  1082. ! find_binary "Xdialog" && return 1
  1083. elif [ "a${SGUI}" = "azenity" ]; then
  1084. ! find_binary "zenity" && return 1
  1085. elif [ "a${SGUI}" = "akdialog" ]; then
  1086. ! find_binary "kdialog" && return 1
  1087. elif [ "a${SGUI}" = "awhiptail" ]; then
  1088. ! find_binary "whiptail" && return 1
  1089. fi
  1090. variable=`selection_argument variable "$@"`
  1091. title=`selection_argument title "$@"`
  1092. text=`selection_argument text "$@"`
  1093. debug "Prompting user to type variable %s.\n" "${variable}"
  1094. localtext=`format_text "You can automate this selection by setting %s variable.\n" "${variable}"`
  1095. [ "a${text}" != "a" ] && localtext=`${printfbin} "%s\n\n%s: " "${localtext}" "${text}"`
  1096. locallines=`echo "${localtext}" | ${wcbin} -l`; locallines=`echo ${locallines}`; locallines=`expr ${locallines} + 10`; locallines=`echo ${locallines}`;
  1097. if [ "a${SGUI}" = "adialog" ]; then
  1098. ${dialogbin} --backtitle "Sakis3G ${MYVERSION}" --title " ${title} " --ok-label `translate_text "OK"` --cancel-label `translate_text "Cancel"` --clear --inputbox "${localtext}" ${locallines} 0 2> "/tmp/sakis3g.dialog.$$"
  1099. buttonselection=$?
  1100. elif [ "a${SGUI}" = "aXdialog" ]; then
  1101. ${Xdialogbin} --backtitle " ${title} " --title "Sakis3G ${MYVERSION}" --ok-label `translate_text "OK"` --cancel-label `translate_text "Cancel"` --wrap --fill --clear --inputbox "${localtext}" ${locallines} 0 2> "/tmp/sakis3g.dialog.$$"
  1102. buttonselection=$?
  1103. elif [ "a${SGUI}" = "akdialog" ]; then
  1104. ${kdialogbin} --title " ${title} " --inputbox "${localtext}" "" > "/tmp/sakis3g.dialog.$$" 2> /dev/null
  1105. buttonselection=$?
  1106. elif [ "a${SGUI}" = "azenity" ]; then
  1107. need_arg "foldwrapping"
  1108. localtext=`echo "${localtext}" | ${sedbin} -e "s/_/__/g"`
  1109. find_binary "fold" && localtext=`echo "${localtext}" | ${foldbin} -s -w ${foldwrapping}`
  1110. ${zenitybin} --title " ${title} " --entry --text "${localtext}" > "/tmp/sakis3g.dialog.$$"
  1111. buttonselection=$?
  1112. elif [ "a${SGUI}" = "awhiptail" ]; then
  1113. ${whiptailbin} --backtitle "Sakis3G ${MYVERSION}" --title " ${title} " --clear --inputbox "${localtext}" 0 0 2> "/tmp/sakis3g.dialog.$$"
  1114. buttonselection=$?
  1115. fi
  1116. termselection=`${catbin} "/tmp/sakis3g.dialog.$$" 2> /dev/null`; ${rmbin} -f "/tmp/sakis3g.dialog.$$"
  1117. termselection=`sanitize "${termselection}"`
  1118. debug "User pressed button %d while typed: \"%s\"\n" "${buttonselection}" "${termselection}"
  1119. # TODO: Implement example
  1120. if [ "a${buttonselection}" = "a0" ]; then
  1121. debug "User pressed OK.\n"
  1122. if [ "a${termselection}" != "a" ]; then
  1123. debug "User typed: %s\n" "${termselection}"
  1124. eval "export ${variable}=\"${termselection}\""
  1125. selection=0
  1126. else
  1127. debug "User entered no text.\n" "${variable}"
  1128. selection=98
  1129. fi
  1130. elif [ "a${termselection}" = "a" -o "a${buttonselection}" != "a0" ]; then
  1131. debug "User requested to cancel.\n"
  1132. selection=98
  1133. else
  1134. debug "Unknown error.\n"
  1135. selection=99
  1136. fi
  1137. debug "Considering selection: %d\n" "${selection}"
  1138. unset variable; unset title; unset text; unset termselection; unset buttonselection; unset localtext
  1139. return "${selection}"
  1140. }
  1141.  
  1142. dialog_clearscreen() {
  1143. [ "a${DEBUG}" = "a" ] && find_binary "clear" && ${clearbin}
  1144. return 0
  1145. }
  1146.  
  1147. selection_argument() {
  1148. selectarg="$1"
  1149. [ "$#" -gt "0" ] && shift
  1150. case "${selectarg}" in
  1151. variable)
  1152. echo "$1"
  1153. ;;
  1154. title)
  1155. echo "$2"
  1156. ;;
  1157. text)
  1158. echo "$3"
  1159. ;;
  1160. button1)
  1161. echo "$4"
  1162. ;;
  1163. button2)
  1164. echo "$5"
  1165. ;;
  1166. options)
  1167. for selectind in `${seqbin} 6 2 $#`
  1168. do
  1169. eval selcode=\${${selectind}}
  1170. selectinc=`expr ${selectind} + 1`; selectinc=`echo ${selectinc}`
  1171. eval seloption=\${${selectinc}}
  1172. echo "${selcode} ${seloption}"
  1173. unset selcode; unset seloption; unset selectinc
  1174. done
  1175. unset selectind
  1176. ;;
  1177. option)
  1178. selectind=$1
  1179. [ "$#" -gt "0" ] && shift
  1180. selectind=`expr ${selectind} \* 2`; selectind=`echo ${selectind}`
  1181. selectind=`expr ${selectind} + 4`; selectind=`echo ${selectind}`
  1182. eval selcode=\${${selectind}}
  1183. echo "${selcode}"
  1184. unset selcode; unset selectind
  1185. ;;
  1186. invalid)
  1187. for selectind in `${seqbin} 7 2 $#`
  1188. do
  1189. eval selcode=\${${selectind}}
  1190. if [ "a${selcode}" = "a$1" ]; then
  1191. debug "%s==%s.\n" "$1" "${selcode}"
  1192. unset selcode; unset selectarg
  1193. selectind=`expr ${selectind} - 5`; selectind=`echo ${selectind}`
  1194. selectind=`expr ${selectind} / 2`; selectind=`echo ${selectind}`
  1195. return ${selectind}
  1196. fi
  1197. debug "%s!=%s\n" "${selcode}" "${1}"
  1198. unset selcode
  1199. done
  1200. debug "Not a valid option \"%s\".\n" "$1"
  1201. unset selectind
  1202. return 0
  1203. ;;
  1204. *)
  1205. debug "Unknown selection id %s.\n" "${selectarg}"
  1206. ;;
  1207. esac
  1208. unset selectarg
  1209. }
  1210.  
  1211. # Returns true if $1 is found within $2.
  1212. strinstr() {
  1213. [ "a$1" = "a" -o "a$2" = "a" ] && return 1
  1214. if [ "a$3" = "a" ]; then
  1215. strinstrsep=":"
  1216. else
  1217. strinstrsep="$3"
  1218. fi
  1219. strinstrfound=`echo "${strinstrsep}${2}${strinstrsep}" | grep "${strinstrsep}${1}${strinstrsep}"`
  1220. strinstrfound=`echo "${strinstrfound}"`
  1221. unset strinstrsep
  1222. if [ "a${strinstrfound}" != "a" ]; then
  1223. unset strinstrfound
  1224. return 0
  1225. fi
  1226. unset strinstrfound
  1227. return 1
  1228. }
  1229.  
  1230. # Checks if running with root priviledges
  1231. we_are_root_already() {
  1232. if [ "a${UID}" = "a0" -o "a${USER}" = "aroot" -o "a${EUID}" = "a0" ]; then
  1233. return 0
  1234. fi
  1235. debug "Not currently running with root privileges.\n"
  1236. return 1
  1237. }
  1238.  
  1239. # Makes sure we are root
  1240. we_are_root() {
  1241. if we_are_root_already; then
  1242. debug "We are root already. Proceeding.\n"
  1243. return 0
  1244. fi
  1245. state_variables
  1246. verbose "Acquiring root privileges"
  1247. [ -z "${SUMETHOD}" ] && check_su_software
  1248. [ -z "${ME}" ] && have_me
  1249. cleanscreen
  1250. debug "This instance will call %s and abort.\n" "${SUMETHOD}"
  1251. case "${SUMETHOD}" in
  1252. "sudo")
  1253. [ "a${DEBUG}" = "a" ] && find_binary "clear" && ${clearbin}
  1254. eval ${sudobin} ${ME} ${allargs} ${statevariables}
  1255. ret=$?
  1256. if [ "$ret" -eq "1" ]; then
  1257. echo
  1258. echo If it didn\'t worked, you need to add yourself
  1259. echo to sudo file: as root, type visudo and add the
  1260. echo following line at the end of file:
  1261. echo ${runner} ALL=\(root\) NOPASSWD: ${ME}
  1262. stop_fmt_error 1 "Failed to become root."
  1263. fi
  1264. ;;
  1265. "gksu")
  1266. # Nusty workaround because gksu does not properly handle arguments containing whitespaces
  1267. ${touchbin} "/tmp/sakis3g.gksu.wrapper.$$"
  1268. ${chmodbin} +x "/tmp/sakis3g.gksu.wrapper.$$"
  1269. ${printfbin} "#!/bin/bash\nexport PROVIDER=\"%s\"\n%s %s %s\nexit \$?\n\n" "${PROVIDER}" "${ME}" "${allargs}" "${statevariables}" > "/tmp/sakis3g.gksu.wrapper.$$"
  1270. [ ! -f "/tmp/sakis3g.gksu.wrapper.$$" ] && stop_fmt_error 3 "No method for acquiring root privileges found."
  1271. debug show_file "/tmp/sakis3g.gksu.wrapper.$$"
  1272. ${gksubin} /tmp/sakis3g.gksu.wrapper.$$
  1273. ret=$?
  1274. ${rmbin} -f "/tmp/sakis3g.gksu.wrapper.$$"
  1275. ;;
  1276. "kdesu")
  1277. ${kdesubin} "${ME} ${allargs} ${statevariables}"
  1278. ret=$?
  1279. ;;
  1280. "su")
  1281. if [ -n "${stick_to_console}" -a -z "${interactive}" ]; then
  1282. notify "\nUtility \"%s\" found but cannot be used since we are not interactive.\n" "su"
  1283. notify "Either enable interactive mode:\n"
  1284. notify "\t%s --interactive %s\n" "${ME}" "${allargs}"
  1285. notify "or, force using %s instead:\n" "sudo"
  1286. notify "\t%s --sudo %s\n\r\n" "${ME}" "${allargs}"
  1287. stop_fmt_error 3 "No method for acquiring root privileges found."
  1288. fi
  1289. [ "a${DEBUG}" = "a" ] && find_binary "clear" && ${clearbin}
  1290. term_print "Please supply root password, or press enter to abort."
  1291. ${subin} -c "${ME} ${allargs} ${statevariables}"
  1292. ret=$?
  1293. ;;
  1294. esac
  1295. stop_with ${ret}
  1296. }
  1297.  
  1298. # Shows error
  1299. show_fmt_error() {
  1300. errortext=`format_text "$@"`
  1301. debug "Error: %s\n" "${errortext}"
  1302. if [ "a${noerror}" != "a" ]; then
  1303. debug "Skipping error notification due to configuration.\n"
  1304. unset errortext
  1305. return 0
  1306. fi
  1307. guruplug_error
  1308. case "${SGUI}" in
  1309. whiptail|dialog|Xdialog|zenity|kdialog)
  1310. dialog_error "${errortext}"
  1311. ;;
  1312. "9menu")
  1313. nine_error "${errortext}"
  1314. ;;
  1315. "interactive terminal")
  1316. term_error "${errortext}"
  1317. ;;
  1318. "terminal")
  1319. term_error "${errortext}"
  1320. ;;
  1321. *)
  1322. term_error "${errortext}"
  1323. ;;
  1324. esac
  1325. unset errortext
  1326. }
  1327.  
  1328. cleanscreen() {
  1329. osd_cleanscreen
  1330. case "${SGUI}" in
  1331. whiptail|dialog|Xdialog|zenity|kdialog)
  1332. dialog_cleanscreen
  1333. term_clearline
  1334. ;;
  1335. "9menu")
  1336. term_clearline
  1337. ;;
  1338. "interactive terminal")
  1339. term_clearline
  1340. ;;
  1341. "terminal")
  1342. term_clearline
  1343. ;;
  1344. *)
  1345. term_clearline
  1346. ;;
  1347. esac
  1348. }
  1349.  
  1350. osd_cleanscreen() {
  1351. if [ "a${OSDPID}" != "a" ]; then
  1352. if find_binary "kill"; then
  1353. ! notrunning "${OSDPID}" && ${killbin} -1 "${OSDPID}" 2> /dev/null
  1354. ! notrunning "${OSDPID}" && ${killbin} -9 "${OSDPID}" 2> /dev/null
  1355. fi
  1356. unset OSDPID
  1357. fi
  1358. return 0
  1359. }
  1360.  
  1361. osd_verbose() {
  1362. [ "a${OSDOUTPUT}" = "a" ] && return 1
  1363. osd_cleanscreen
  1364. case "${OSDOUTPUT}" in
  1365. osd_cat)
  1366. need_arg "XOSDFONT"
  1367. if [ "a${OSDISP}" != "a${ISPID}" ]; then
  1368. unset OSDFGCOLOR; unset OSDBGCOLOR;
  1369. fi
  1370. [ "a${OSDFGCOLOR}" = "a" ] && OSDFGCOLOR=`echo ${ISP_FG_COLOR} | ${sedbin} -e "s/^\(..\)\(..\)\(..\)$/rgb:\1\/\2\/\3/g"`
  1371. [ "a${OSDBGCOLOR}" = "a" ] && OSDBGCOLOR=`echo ${ISP_BG_COLOR} | ${sedbin} -e "s/^\(..\)\(..\)\(..\)$/rgb:\1\/\2\/\3/g"`
  1372. [ "a${OSDFGCOLOR}" = "a" ] && OSDFGCOLOR="rgb:80/ff/80"
  1373. [ "a${OSDBGCOLOR}" = "a" ] && OSDBGCOLOR="rgb:00/40/00"
  1374. OSDISP="${ISPID}"
  1375. export OSDISP; export OSDFGCOLOR; export OSDBGCOLOR;
  1376. ${OSDOUTPUT} -T "Sakis3G: $1" -P ${verbosecurrentcount} -b percentage -O 2 -c ${OSDFGCOLOR} -u ${OSDBGCOLOR} -f "${XOSDFONT}" -p bottom -a 0 -d 10 -A center &
  1377. OSDPID=$!
  1378. eval "disown -a" > /dev/null 2> /dev/null
  1379. export OSDPID
  1380. [ "a${verbosecurrentcount}" = "a100" ] && sleep 3
  1381. ;;
  1382. aosd_cat)
  1383. need_arg "AOSDFONT"
  1384. ${OSDOUTPUT} -n "${AOSDFONT}" -f 100 -t 2 --input=- -u 10000 -o 3000 <<endl &
  1385. $1
  1386. endl
  1387. OSDPID=$!
  1388. eval "disown -a" > /dev/null 2> /dev/null
  1389. export OSDPID
  1390. [ "a${verbosecurrentcount}" = "a100" ] && sleep 3
  1391. ;;
  1392. esac
  1393. }
  1394.  
  1395. verbose() {
  1396. need_binary "printf"
  1397. if [ "a$1" = "a100" ]; then
  1398. verbosecurrentcount=93
  1399. shift
  1400. fi
  1401. verbosetext=`format_text "$@"`
  1402. [ "${verbosecurrentcount}" = "a" ] && verbosecurrentcount=0
  1403. if [ "a${verbosetext}" = "a${lastverbosetext}" ]; then
  1404. verbosecurrentcount=`expr ${verbosecurrentcount} + 1`;
  1405. guruplug_verbose_same
  1406. else
  1407. verbosecurrentcount=`expr ${verbosecurrentcount} + 7`;
  1408. guruplug_verbose
  1409. fi
  1410. lastverbosetext="${verbosetext}"
  1411. verbosecurrentcount=`echo ${verbosecurrentcount}`
  1412. export lastverbosetext
  1413. export verbosecurrentcount
  1414. debug "Verbosing: %d%% %s\n" "${verbosecurrentcount}" "${verbosetext}"
  1415. if [ "a${noverbose}" != "a" ]; then
  1416. debug "Skipping verbose due to configuration.\n"
  1417. unset verbosetext
  1418. return 0
  1419. fi
  1420. if [ "a${OSDOUTPUT}" != "a" ]; then
  1421. osdtext=`${printfbin} "$@"`
  1422. osd_verbose "${osdtext}"
  1423. unset osdtext
  1424. else
  1425. case "${SGUI}" in
  1426. whiptail|dialog|Xdialog|zenity|kdialog)
  1427. dialog_verbose "${verbosetext}"
  1428. ;;
  1429. "9menu")
  1430. nine_verbose "${verbosetext}"
  1431. ;;
  1432. "interactive terminal")
  1433. term_verbose "${verbosetext}"
  1434. ;;
  1435. "terminal")
  1436. term_verbose "${verbosetext}"
  1437. ;;
  1438. *)
  1439. term_verbose "${verbosetext}"
  1440. ;;
  1441. esac
  1442. fi
  1443. unset verbosetext
  1444. }
  1445.  
  1446. notify() {
  1447. notificationtext=`format_text "$@"`
  1448. debug "Notify: %s\n" "${notificationtext}"
  1449. if [ "a${nonotify}" != "a" ]; then
  1450. debug "Skipping notification due to configuration.\n"
  1451. unset notificationtext
  1452. return 0
  1453. fi
  1454. case "${SGUI}" in
  1455. whiptail|dialog|Xdialog|zenity|kdialog)
  1456. dialog_notify "${notificationtext}"
  1457. ;;
  1458. "9menu")
  1459. nine_notify "${notificationtext}"
  1460. ;;
  1461. "interactive terminal")
  1462. term_notify "${notificationtext}"
  1463. ;;
  1464. "terminal")
  1465. term_notify "${notificationtext}"
  1466. ;;
  1467. *)
  1468. term_notify "${notificationtext}"
  1469. ;;
  1470. esac
  1471. unset notificationtext
  1472. }
  1473.  
  1474. balloon_notify() {
  1475. notificationtext=`format_text "$@"`
  1476. if [ "a${nonotify}" != "a" ]; then
  1477. debug "Skipping balloon notification due to configuration: %s\n" "${notificationtext}"
  1478. unset notificationtext
  1479. return 0
  1480. fi
  1481. # File is deleted during release_X_cookie
  1482. [ "a${PROVIDER}" != "a" ] && [ -x "${PROVIDER}" ] && ${PROVIDER} getfile "files/sakis3g.png" 2> /dev/null > "/tmp/sakis3g.notification.icon.$$.png"
  1483. debug "Will display final message using libnotify.\n"
  1484. if [ ! -f "/tmp/sakis3g.notification.icon.$$.png" ]; then
  1485. debug "Notifying user without icon.\n"
  1486. debug run_command "${notify_sendbin} \"Sakis3G\" \"${notificationtext}\""
  1487. else
  1488. debug "Notifying user with icon.\n"
  1489. debug run_command "${notify_sendbin} --icon=\"/tmp/sakis3g.notification.icon.$$.png\" \"Sakis3G\" \"${notificationtext}\""
  1490. debug run_command "${rmbin} -f \"/tmp/sakis3g.notification.icon.$$.png\""
  1491. fi
  1492. unset notificationtext
  1493. }
  1494.  
  1495. finalnotify() {
  1496. guruplug_notify
  1497. if [ "a${prefer_osd}" != "a" -a "a${OSDOUTPUT}" != "a" ]; then
  1498. verbose "100" "$@"
  1499. elif [ "a${prefer_osd}" = "a" -a "a${balloons}" != "a" -a "a${stick_to_console}" = "a" ] && find_binary "notify-send" && [ "a${DBUS_SESSION_BUS_ADDRESS}" != "a" -a "a${SESSION_MANAGER}" != "a" ]; then
  1500. balloon_notify "$@"
  1501. else
  1502. notify "$@"
  1503. fi
  1504. }
  1505.  
  1506. translate_selection() {
  1507. unset translatedarguments
  1508. localcounter=0
  1509. for localargument in "$@"
  1510. do
  1511. localcounter=`expr ${localcounter} + 1`; localcounter=`echo ${localcounter}`
  1512. if [ "${localcounter}" -eq "1" ]; then
  1513. translatedarguments="\"${localargument}\""
  1514. elif [ "${localcounter}" -le "5" ]; then
  1515. translatedtext=`translate_text "${localargument}"`
  1516. translatedarguments=`${printfbin} "%s \"%s\"" "${translatedarguments}" "${translatedtext}"`
  1517. unset translatedtext
  1518. elif [ "${localcounter}" -gt "5" ]; then
  1519. verification=`expr \( ${localcounter} / 2 \) \* 2`; verification=`echo ${verification}`
  1520. if [ "a${verification}" = "a${localcounter}" ]; then
  1521. translatedarguments=`${printfbin} "%s \"%s\"" "${translatedarguments}" "${localargument}"`
  1522. else
  1523. translatedtext=`translate_text "${localargument}"`
  1524. translatedarguments=`${printfbin} "%s \"%s\"" "${translatedarguments}" "${translatedtext}"`
  1525. unset translatedtext
  1526. fi
  1527. unset verification
  1528. fi
  1529. done
  1530. unset localcounter; unset
  1531. eval "user_select \"translated\" ${translatedarguments}"
  1532. translatedselection=$?
  1533. unset translatedarguments
  1534. return ${translatedselection}
  1535. }
  1536.  
  1537. translate_confirm() {
  1538. unset translatedarguments
  1539. localcounter=0
  1540. for localargument in "$@"
  1541. do
  1542. localcounter=`expr ${localcounter} + 1`; localcounter=`echo ${localcounter}`
  1543. if [ "${localcounter}" -eq "1" ]; then
  1544. translatedarguments="\"${localargument}\""
  1545. elif [ "${localcounter}" -le "5" ]; then
  1546. translatedtext=`translate_text "${localargument}"`
  1547. translatedarguments=`${printfbin} "%s \"%s\"" "${translatedarguments}" "${translatedtext}"`
  1548. unset translatedtext
  1549. elif [ "${localcounter}" -gt "5" ]; then
  1550. translatedarguments=`${printfbin} "%s \"%s\"" "${translatedarguments}" "${localargument}"`
  1551. fi
  1552. done
  1553. unset localcounter; unset
  1554. eval "user_confirm \"translated\" ${translatedarguments}"
  1555. translatedselection=$?
  1556. unset translatedarguments
  1557. return ${translatedselection}
  1558. }
  1559.  
  1560. translate_prompt() {
  1561. unset translatedarguments
  1562. localcounter=0
  1563. for localargument in "$@"
  1564. do
  1565. localcounter=`expr ${localcounter} + 1`; localcounter=`echo ${localcounter}`
  1566. if [ "${localcounter}" -eq "1" ]; then
  1567. translatedarguments="\"${localargument}\""
  1568. elif [ "${localcounter}" -le "5" ]; then
  1569. translatedtext=`translate_text "${localargument}"`
  1570. translatedarguments=`${printfbin} "%s \"%s\"" "${translatedarguments}" "${translatedtext}"`
  1571. unset translatedtext
  1572. elif [ "${localcounter}" -gt "5" ]; then
  1573. verification=`expr \( ${localcounter} / 2 \) \* 2`; verification=`echo ${verification}`
  1574. if [ "a${verification}" = "a${localcounter}" ]; then
  1575. translatedarguments=`${printfbin} "%s \"%s\"" "${translatedarguments}" "${localargument}"`
  1576. else
  1577. translatedtext=`translate_text "${localargument}"`
  1578. translatedarguments=`${printfbin} "%s \"%s\"" "${translatedarguments}" "${translatedtext}"`
  1579. unset translatedtext
  1580. fi
  1581. unset verification
  1582. fi
  1583. done
  1584. unset localcounter; unset
  1585. eval "user_prompt \"translated\" ${translatedarguments}"
  1586. translatedselection=$?
  1587. unset translatedarguments
  1588. return ${translatedselection}
  1589. }
  1590.  
  1591. user_confirm() {
  1592. unset selection
  1593. if [ "a$1" != "atranslated" ]; then
  1594. debug "Asking user to confirm: %s\n" "`echo "$@"`"
  1595. translate_confirm "$@"
  1596. return $?
  1597. else
  1598. shift
  1599. debug "Asking user to confirm: %s\n" "`echo "$@"`"
  1600. fi
  1601. if [ "$#" -lt 3 ]; then
  1602. debug "FIXME: Wrong number of arguments (%d). Returning 99.\n" "$#"
  1603. return 99
  1604. fi
  1605. variable=`selection_argument variable "$@"`
  1606. if [ "a$6" = "areset" -a "a${interactive}" != "a" ]; then
  1607. eval unset ${variable}yes
  1608. eval unset ${variable}no
  1609. else
  1610. eval alreadyset=\${${variable}yes}
  1611. if [ "a${alreadyset}" != "a" ]; then
  1612. debug "Switch %syes is already set.\n" "${variable}"
  1613. return 0
  1614. fi
  1615. eval alreadyset=\${${variable}no}
  1616. if [ "a${alreadyset}" != "a" ]; then
  1617. debug "Switch %sno is already set.\n" "${variable}"
  1618. return 1
  1619. fi
  1620. fi
  1621. if voodoo_mode; then
  1622. debug "Voodoo-mode is enabled. Will consider it a yes.\n"
  1623. eval unset ${variable}no
  1624. eval ${variable}yes=1
  1625. eval export ${variable}yes
  1626. selection=0
  1627. return 0
  1628. fi
  1629. case "${SGUI}" in
  1630. whiptail|dialog|Xdialog|zenity|kdialog)
  1631. dialog_confirm "$@"
  1632. selection=$?
  1633. ;;
  1634. "9menu")
  1635. nine_confirm "$@"
  1636. selection=$?
  1637. ;;
  1638. "interactive terminal")
  1639. interactive_term_confirm "$@"
  1640. selection=$?
  1641. ;;
  1642. "terminal")
  1643. term_confirm "$@"
  1644. selection=$?
  1645. ;;
  1646. *)
  1647. term_confirm "$@"
  1648. selection=$?
  1649. ;;
  1650. esac
  1651. variable=`selection_argument variable "$@"`
  1652. if [ "a${selection}" = "a0" ]; then
  1653. debug "User answered \"yes\".\n"
  1654. eval unset ${variable}no
  1655. eval ${variable}yes=1
  1656. eval export ${variable}yes
  1657. selection=0
  1658. else
  1659. debug "User answered \"no\".\n"
  1660. eval unset ${variable}yes
  1661. eval ${variable}no=1
  1662. eval export ${variable}no
  1663. selection=1
  1664. fi
  1665. unset variable
  1666. return ${selection}
  1667. }
  1668.  
  1669. user_select() {
  1670. unset selection
  1671. if [ "a$1" != "atranslated" ]; then
  1672. debug "Asking user to select: %s\n" "`echo "$@"`"
  1673. translate_selection "$@"
  1674. return $?
  1675. else
  1676. shift
  1677. debug "Asking user to select: %s\n" "`echo "$@"`"
  1678. fi
  1679. if [ "$#" -lt 3 ]; then
  1680. debug "FIXME: Wrong number of arguments (%d). Returning 99.\n" "$#"
  1681. return 99
  1682. fi
  1683. variable=`selection_argument variable "$@"`
  1684. eval alreadyset=\${${variable}}
  1685. if [ "a${alreadyset}" != "a" ]; then
  1686. debug "%s=%s\n" "${variable}" "${alreadyset}"
  1687. # Check tokens first
  1688. for selectioniterator in ${alreadyset}
  1689. do
  1690. selection_argument invalid "${selectioniterator}" "$@"
  1691. selection=$?
  1692. if [ "a${selection}" != "a0" -a "a${selection}" != "a" ]; then
  1693. debug "User has already selected %s=\"%s\". Returning %d.\n" "${variable}" "${selectioniterator}" "${selection}"
  1694. eval export ${variable}="${selectioniterator}"
  1695. unset variable; unset alreadyset; unset selectioniterator
  1696. return ${selection}
  1697. fi
  1698. unset selection;
  1699. done
  1700. unset selectioniterator
  1701. # Check whole value then
  1702. selection_argument invalid "${alreadyset}" "$@"
  1703. selection=$?
  1704. if [ "a${selection}" != "a0" -a "a${selection}" != "a" ]; then
  1705. debug "User has already selected %s=\"%s\". Returning %d.\n" "${variable}" "${alreadyset}" "${selection}"
  1706. eval export ${variable}="${alreadyset}"
  1707. unset variable; unset alreadyset
  1708. return ${selection}
  1709. fi
  1710. # Else warn
  1711. show_fmt_error "Already selected value %s=\"%s\". Is not valid.\n" "${variable}" "${alreadyset}"
  1712. eval unset ${variable}
  1713. unset variable; unset alreadyset;
  1714. else
  1715. debug "Variable %s is not set already.\n" "${variable}"
  1716. unset alreadyset; unset variable
  1717. fi
  1718. if voodoo_mode; then
  1719. variable=`selection_argument variable "$@"`
  1720. selectioncount=`selection_argument options "$@" | ${wcbin} -l`; selectioncount=`echo ${selectioncount}`
  1721. if [ "a${selectioncount}" = "a1" ]; then
  1722. selection=1
  1723. eval export ${variable}="`selection_argument option 1 "$@"`"
  1724. debug "Employed voodoo-mode in selecting %s from unique option: %s\n" "${variable}" "`eval echo \\${${variable}}`"
  1725. unset selectioncount; unset variable
  1726. return ${selection}
  1727. elif [ "a${selectioncount}" = "a2" ]; then
  1728. for selection in 1 2
  1729. do
  1730. voodoovalue=`selection_argument option ${selection} "$@" | ${grepbin} -i "custom" | ${wcbin} -l`; voodoovalue=`echo ${voodoovalue}`
  1731. [ "a${voodoovalue}" = "a0" ] && voodoovalue=`selection_argument option ${selection} "$@" | ${grepbin} -i "other" | ${wcbin} -l`; voodoovalue=`echo ${voodoovalue}`
  1732. if [ "a${voodoovalue}" = "a1" ]; then
  1733. debug "Voodoo determined that selection %d leads to custom/other.\n" "${selection}"
  1734. if [ "${selection}" -eq "1" ]; then
  1735. selection=2
  1736. else
  1737. selection=1
  1738. fi
  1739. eval export ${variable}="`selection_argument option 1 "$@"`"
  1740. debug "Employed voodoo-mode in selecting %s from option %d: %s\n" "${variable}" "${selection}" "`eval echo \\${${variable}}`"
  1741. unset selectioncount; unset voodoovalue; unset variable
  1742. return ${selection}
  1743. fi
  1744. unset voodoovalue
  1745. done
  1746. unset selection
  1747. fi
  1748. unset selectioncount; unset variable
  1749. fi
  1750. case "${SGUI}" in
  1751. whiptail|dialog|Xdialog|zenity|kdialog)
  1752. dialog_select "$@"
  1753. selection=$?
  1754. ;;
  1755. "9menu")
  1756. nine_select "$@"
  1757. selection=$?
  1758. ;;
  1759. "interactive terminal")
  1760. interactive_term_select "$@"
  1761. selection=$?
  1762. ;;
  1763. "terminal")
  1764. term_select "$@"
  1765. selection=$?
  1766. ;;
  1767. *)
  1768. term_select "$@"
  1769. selection=$?
  1770. ;;
  1771. esac
  1772. debug "User selection was %d.\n" "${selection}"
  1773. return ${selection}
  1774. }
  1775.  
  1776. # Prompts user to enter text
  1777. user_prompt() {
  1778. if [ "a$1" != "atranslated" ]; then
  1779. debug "Asking user to enter: %s\n" "`echo "$@"`"
  1780. translate_prompt "$@"
  1781. return $?
  1782. else
  1783. shift
  1784. debug "Asking user to enter: %s\n" "`echo "$@"`"
  1785. fi
  1786. if [ "$#" -lt 3 ]; then
  1787. debug "FIXME: Wrong number of arguments (%d). Returning 99.\n" "$#"
  1788. return 99
  1789. fi
  1790. promptvariable=`selection_argument variable "$@"`
  1791. eval alreadyset=\${${promptvariable}}
  1792. if [ "a${alreadyset}" != "a" ]; then
  1793. selection=0
  1794. debug "%s=%s\n" "${promptvariable}" "${alreadyset}"
  1795. debug "User has already entered %s=\"%s\". Returning %d.\n" "${promptvariable}" "${alreadyset}" "${selection}"
  1796. unset alreadyset; unset promptvariable
  1797. return ${selection}
  1798. else
  1799. debug "Variable %s is not set already.\n" "${promptvariable}"
  1800. unset alreadyset; unset promptvariable
  1801. fi
  1802. case "${SGUI}" in
  1803. whiptail|dialog|Xdialog|zenity|kdialog)
  1804. dialog_prompt "$@"
  1805. selection=$?
  1806. ;;
  1807. "9menu")
  1808. nine_prompt "$@"
  1809. selection=$?
  1810. ;;
  1811. "interactive terminal")
  1812. interactive_term_prompt "$@"
  1813. selection=$?
  1814. ;;
  1815. "terminal")
  1816. term_prompt "$@"
  1817. selection=$?
  1818. ;;
  1819. *)
  1820. term_prompt "$@"
  1821. selection=$?
  1822. ;;
  1823. esac
  1824. promptvariable=`selection_argument variable "$@"`
  1825. eval "alreadyset=\"\${${promptvariable}}\""
  1826. debug "New value is %s=\"%s\". Returning %d.\n" "${promptvariable}" "${alreadyset}" "${selection}"
  1827. unset alreadyset; unset promptvariable
  1828. return ${selection}
  1829. }
  1830.  
  1831. # Stops execution with return status $1
  1832. stop_with() {
  1833. verbose "Cleaning"
  1834. if [ "a$1" = "a" ]; then
  1835. debug "Stopping operation with return status: %d\n" "99"
  1836. exit 99
  1837. else
  1838. debug "Stopping operation with return status: %d\n" "$1"
  1839. exit $1
  1840. fi
  1841. }
  1842.  
  1843. # Shows error and aborts execution
  1844. stop_fmt_error() {
  1845. errorcode="$1"
  1846. [ "$#" -gt "0" ] && shift
  1847. show_fmt_error "$@"
  1848. stop_with ${errorcode}
  1849. }
  1850.  
  1851.  
  1852. # Make sure usual binary containing directories exist in PATH, even
  1853. # if not running as root. Some distributions do not include those
  1854. # folders in PATH (although they contain some useful user oriented
  1855. # utilities) if not running as root, or if not running from an
  1856. # interactive shell.
  1857. fix_path() {
  1858. [ -n "${PATHFIXXED}" ] && return 0
  1859. for fixpathi in "/bin" "/usr/bin" "/sbin" "/usr/sbin"
  1860. do
  1861. if strinstr "${fixpathi}" "${PATH}"; then
  1862. debug "Dir \"%s\" exists in PATH.\n" "${fixpathi}"
  1863. else
  1864. export PATH="${fixpathi}:${PATH}"
  1865. debug "Added \"%s\" to PATH.\n" "${fixpathi}"
  1866. fi
  1867. done
  1868. unset fixpathi
  1869. PATHFIXXED=YES
  1870. debug "Done setting up PATH.\n"
  1871. }
  1872.  
  1873.  
  1874. # Locates full path of binary specified in argument
  1875. find_binary() {
  1876. [ "a$1" = "a" ] && return 1
  1877. binaryvariable=`echo "$1" | sed -e "s/-/_/g"`
  1878. binaryvariable=`echo "${binaryvariable}" | sed -e "s/9/nine/g"`
  1879. [ "a${binaryvariable}" = "a" ] && binaryvariable="$1"
  1880. findbinaryfound=`eval echo \\\${${binaryvariable}bin}`
  1881. if [ "a${findbinaryfound}" = "a" ]; then
  1882. if [ "a${whichbin}" = "a" ]; then
  1883. findbinaryfound=`which which 2> /dev/null`
  1884. elif [ ! -x "${whichbin}" ]; then
  1885. findbinaryfound=`which which 2> /dev/null`
  1886. else
  1887. findbinaryfound="${whichbin}"
  1888. fi
  1889. if [ "a${findbinaryfound}" = "a" ]; then
  1890. debug "Unable to locate \"%s\" within PATH, because \"%s\" utility is not available.\n" "$1" "which"
  1891. unset findbinaryfound
  1892. return 1
  1893. else
  1894. if [ -z "${whichbin}" ]; then
  1895. export whichbin="${findbinaryfound}"
  1896. debug "Can search through PATH, \"%s\" binary found.\n" "which"
  1897. fi
  1898. findbinaryfound=`${findbinaryfound} "${1}" 2> /dev/null`
  1899. if [ "a${findbinaryfound}" = "a" ]; then
  1900. debug "Unable to locate \"%s\" within PATH.\n" "$1"
  1901. unset findbinaryfound
  1902. return 1
  1903. elif [ -x "${findbinaryfound}" ]; then
  1904. debug "Located \"%s\" within PATH (%s).\n" "$1" "${findbinaryfound}"
  1905. eval export ${binaryvariable}bin="${findbinaryfound}"
  1906. fi
  1907. fi
  1908. fi
  1909. if [ -x "${findbinaryfound}" ]; then
  1910. unset findbinaryfound
  1911. return 0
  1912. else
  1913. debug "Unable to execute \"%s\" from \"%s\".\n" "$1" "${findbinaryfound}"
  1914. unset findbinaryfound
  1915. eval unset ${binaryvariable}bin
  1916. return 1
  1917. fi
  1918. }
  1919.  
  1920. # Aborts execution if "$1" binary is not found.
  1921. need_binary() {
  1922. [ "a$1" = "a" ] && term_error "FIXME: Executed need_binary with no argument."
  1923. ! find_binary "$1" && stop_fmt_error 4 "Unable to locate dependency \"%s\". Script will now abort." "$1"
  1924. }
  1925.  
  1926. # Check dependencies. Only really required staff should be placed here.
  1927. # We cannot yet search in PATH.
  1928. check_level_one_deps() {
  1929. for checkdependency in which echo grep
  1930. do
  1931. need_binary "${checkdependency}"
  1932. done
  1933. unset checkdependency
  1934. debug "Level 1 dependencies met.\n"
  1935. }
  1936.  
  1937. # Checks if communication software is available and aborts if not
  1938. check_com_software() {
  1939. ! find_binary "wvdial" && direct_pppd=yes
  1940. if [ -n "${direct_pppd}" ]; then
  1941. need_binary "pppd"
  1942. need_arg "PPPD_PEERS"
  1943. if [ ! -d "${PPPD_PEERS}" ]; then
  1944. debug "Unable to locate pppd peers directory (%s)." "${pppdpeerdir}"
  1945. fi
  1946. fi
  1947. for checkdependency in chat
  1948. do
  1949. need_binary "${checkdependency}"
  1950. done
  1951. unset checkdependency
  1952. return 0
  1953. }
  1954.  
  1955. # Checks if method for becoming root, exists.
  1956. check_su_software() {
  1957. unset SUMETHOD
  1958. if [ -z "${alwayssudo}" ]; then
  1959. if [ -z "${stick_to_console}" ]; then
  1960. if find_binary "gksu"; then
  1961. debug "Running on a GNOME system.\n"
  1962. SUMETHOD="gksu"
  1963. elif find_binary "kdesu"; then
  1964. debug "Running on a KDE system.\n"
  1965. SUMETHOD="kdesu"
  1966. elif [ -x "/usr/lib/kde4/libexec/kdesu" ]; then
  1967. export kdesubin="/usr/lib/kde4/libexec/kdesu"
  1968. debug "Running on a KDE4 system.\n"
  1969. SUMETHOD="kdesu"
  1970. fi
  1971. fi
  1972. if [ -z "${SUMETHOD}" ]; then
  1973. if find_binary "su"; then
  1974. debug "Using plain text mode su.\n"
  1975. SUMETHOD="su"
  1976. else
  1977. debug "No su method found. Will check if sudo available.\n"
  1978. export alwayssudo=yes
  1979. fi
  1980. fi
  1981. fi
  1982. if [ -n "${alwayssudo}" ]; then
  1983. ! find_binary "sudo" && stop_fmt_error 3 "No method for acquiring root privileges found."
  1984. debug "Running with sudo configuration.\n"
  1985. SUMETHOD="sudo"
  1986. fi
  1987. return 0
  1988. }
  1989.  
  1990. # Forces use of text mode utilities for getting root privileges
  1991. console_su_software() {
  1992. if [ "${SUMETHOD}" = "kdesu" -o "${SUMETHOD}" = "gksu" ]; then
  1993. if find_binary "su"; then
  1994. debug "Reverting to plain text mode su.\n"
  1995. SUMETHOD="su"
  1996. else
  1997. debug "No text mode su found. Will check if sudo available.\n"
  1998. export alwayssudo=yes
  1999. check_su_software
  2000. fi
  2001. fi
  2002. }
  2003.  
  2004.  
  2005. # Check dependencies. Only really required staff should be placed here.
  2006. # We can now search within path.
  2007. check_level_two_deps() {
  2008. for checkdependency in printf wc cut cat tail head sort uniq ls sed setsid getent ps chmod chown touch expr tr seq cp rm who mv expr basename dirname
  2009. do
  2010. need_binary "${checkdependency}"
  2011. done
  2012. unset checkdependency
  2013. debug "Level 2 dependencies met.\n"
  2014. if [ "a${PROVIDER}" != "a" ] && [ ! -x "${PROVIDER}" ]; then
  2015. debug run_command "${chmodbin} +x \"${PROVIDER}\""
  2016. if [ ! -x "${PROVIDER}" ]; then
  2017. stop_fmt_error 5 "Unable to determine my path.\n"
  2018. fi
  2019. debug "Successfully turned myself an executable.\n"
  2020. fi
  2021. }
  2022.  
  2023. # If running as root, checks dependencies that should be met
  2024. # once running as root. If not running as root, checks if
  2025. # software for becoming root, is available.
  2026. check_root_deps() {
  2027. if ! we_are_root_already; then
  2028. check_su_software
  2029. else
  2030. for checkdependency in ifconfig
  2031. do
  2032. need_binary "${checkdependency}"
  2033. done
  2034. unset checkdependency
  2035. fi
  2036. debug "Root level dependencies met.\n"
  2037. }
  2038.  
  2039. # Locate required utilities within PATH
  2040. resolv_binaries() {
  2041. check_level_one_deps
  2042. fix_path
  2043. get_me ${1}
  2044. check_level_two_deps
  2045. debug "Basic binaries are resolved.\n"
  2046. }
  2047.  
  2048. # Provides a writable log position
  2049. need_log() {
  2050. need_arg "LOGPOSITION"
  2051. for logcandidate in "${LOGPOSITION}" "/var/log/sakis3g.log" "sakis3g.log.$$" "/tmp/sakis3g.log.$$"
  2052. do
  2053. if [ "a${logcandidate}" != "a" ]; then
  2054. ${touchbin} "${logcandidate}" 2> /dev/null
  2055. if [ -w "${logcandidate}" ]; then
  2056. export LOGPOSITION="${logcandidate}"
  2057. debug "Log file is set to \"%s\".\n" "${LOGPOSITION}"
  2058. return 0
  2059. else
  2060. unset logposition
  2061. fi
  2062. fi
  2063. done
  2064. debug "Failed to find a place to store log.\n"
  2065. export LOGPOSITION="/dev/null"
  2066. debug "Log file is set to \"%s\".\n" "${logposition}"
  2067. return 1
  2068. }
  2069.  
  2070. # Returns true(0) if process with PID given as
  2071. # argument, is not running any more.
  2072. notrunning() {
  2073. if [ -n "$1" ]; then
  2074. pidrunning=`${psbin} -p $1 -o pid= 2> /dev/null | wc -l`
  2075. pidrunning=`echo ${pidrunning}`
  2076. else
  2077. pidrunning=1
  2078. fi
  2079. if [ "${pidrunning}" -ne "0" ]; then
  2080. debug "PID %s is still running.\n" "$1"
  2081. else
  2082. debug "PID %s is not running any more.\n" "$1"
  2083. fi
  2084. return ${pidrunning}
  2085. }
  2086.  
  2087. # Sets PPROCESS variable
  2088. get_parent_process() {
  2089. pidtocheck=$1
  2090. [ "a${pidtocheck}" = "a" ] && pidtocheck="${PPID}"
  2091. PPROCESS=`${psbin} -p ${pidtocheck} -o comm= 2> /dev/null`
  2092. export PPROCESS
  2093. if [ "a${PPROCESS}" = "a" ]; then
  2094. debug "Unable to get parent process name.\n"
  2095. unset PPROCESS; unset pidtocheck
  2096. return 1
  2097. fi
  2098. unset pidtocheck
  2099. return 0
  2100. }
  2101.  
  2102. # Check if called by udevd. If yes, spawn in background
  2103. # and exit this instance.
  2104. check_udevd() {
  2105. if [ "a${PROVIDER}" = "a" ]; then
  2106. get_parent_process
  2107. else
  2108. pppid=`${psbin} -p ${PPID} -o ppid= 2> /dev/null`
  2109. pppid=`echo ${pppid}`
  2110. get_parent_process "${pppid}"
  2111. unset pppid
  2112. fi
  2113. debug "Parent process is: %s\n" "${PPROCESS}"
  2114. if [ "a${PPROCESS}" = "audevd" ]; then
  2115. debug "Running by a udevd event.\n"
  2116. need_log
  2117. if have_me; then
  2118. debug "Unconditionally setting DISPLAY to :0.\n"
  2119. export DISPLAY=:0
  2120. debug "Will now spawn child process in background.\n"
  2121. ${ME} "$@" > "${LOGPOSITION}" 2>&1 &
  2122. stop_with 0
  2123. fi
  2124. else
  2125. debug "Running by user request.\n"
  2126. fi
  2127. }
  2128.  
  2129. # Get my location if possible
  2130. get_me() {
  2131. unset metarget
  2132. [ -x "${PROVIDER}" ] && metarget="${PROVIDER}"
  2133. [ -z "${metarget}" ] && metarget="$1"
  2134. find_binary "readlink" && ME=`${readlinkbin} -e ${metarget} 2> /dev/null`
  2135. [ -z "${ME}" ] && find_binary "which" && ME="`${whichbin} ${metarget} 2> /dev/null`"
  2136. unset metarget
  2137. [ -n "${ME}" ] && [ ! -x "${ME}" ] && unset ME
  2138. if [ -z "${ME}" ]; then
  2139. unset ME
  2140. debug "Unable to determine my own location.\n"
  2141. else
  2142. debug "My location is \"%s\".\n" "${ME}"
  2143. fi
  2144.  
  2145. }
  2146.  
  2147. # Makes sure we know our own executable
  2148. have_me() {
  2149. [ -n "${ME}" ] && [ -x "${ME}" ] && return 0;
  2150. stop_fmt_error 5 "Unable to determine my path.\n"
  2151. }
  2152.  
  2153. # If we are root, attempts to determine real user
  2154. # behind it, by looking back the process tree. Goes
  2155. # back 15 processes before giving up.
  2156. resolv_root() {
  2157. [ -z "$1" ] && pid=$$
  2158. [ -z "${pid}" ] && pid=$1
  2159. puser=`${psbin} -p ${pid} -o user= 2> /dev/null`
  2160. if [ -z "${puser}" ]; then
  2161. unset pid; unset puser
  2162. return
  2163. elif [ "${puser}" != "root" ]; then
  2164. runner="${puser}"
  2165. unset pid; unset puser
  2166. return
  2167. fi
  2168. unset puser
  2169. ppid=`${psbin} -p ${pid} -o ppid= 2> /dev/null`; unset pid
  2170. if [ -z "${ppid}" ]; then
  2171. unset ppid
  2172. return
  2173. elif [ "a${ppid}" = "a1" ]; then
  2174. unset ppid
  2175. return
  2176. fi
  2177. [ -z "$2" ] && count=0
  2178. count=`expr $2 + 1`
  2179. count=`echo ${count}`
  2180. [ "$count" -lt 15 ] && resolv_root ${ppid} ${count}
  2181. unset ppid; unset count
  2182. }
  2183.  
  2184. # Locates really running user. If root, attempts to resolv
  2185. # it to real user.
  2186. find_user() {
  2187. unset runner; unset runhome
  2188. [ -n "${USER}" ] && runner="${USER}"
  2189. [ -n "${LOGNAME}" ] && runner="${LOGNAME}"
  2190. [ -n "${USERNAME}" ] && runner="${USERNAME}"
  2191. [ -z "${runner}" ] && find_binary "who" && runner=`${whobin} -m | ${cutbin} -d\ -f1`
  2192. [ -z "${runner}" ] && we_are_root_already && runner="root"
  2193. [ -n "${runner}" ] && [ "${runner}" = "root" ] && resolv_root $$ 0
  2194. if [ -n "${runner}" ]; then
  2195. runhome=`${getentbin} passwd ${runner} 2> /dev/null | ${cutbin} -d: -f6`
  2196. [ -n "${runhome}" ] && [ ! -d "${runhome}" ] && unset runhome
  2197. [ -z "${runhome}" ] && unset runner
  2198. [ -n "${runner}" ] && debug "Person behind screen is %s.\n" "${runner}"
  2199. fi
  2200. [ -z "${runner}" ] && debug "Unable to find our username.\n"
  2201. }
  2202.  
  2203. # Determines if display $1 is unique local display.
  2204. unique_local_display() {
  2205. debug "Will check if display %s belongs to unique user logged in.\n" "$1"
  2206. [ "a$1" = "a" ] && return 1
  2207. ! find_binary "who" && return 1
  2208. loggedusers=`${whobin} | ${grepbin} -v root | ${cutbin} -d\ -f1 | ${sortbin} | ${uniqbin} | ${wcbin} -l`
  2209. loggedusers=`echo ${loggedusers}`
  2210. if [ "a${loggedusers}" != "a1" ]; then
  2211. debug "Found %d logged users. Cannot make sure who will really see message.\n" "${loggedusers}"
  2212. unset loggedusers
  2213. return 1
  2214. fi
  2215. unset loggedusers
  2216. loggeduser=`${whobin} | ${grepbin} -v root | ${cutbin} -d\ -f1 | ${sortbin} | ${uniqbin}`
  2217. confirm=`${whobin} | ${grepbin} ${1} | ${cutbin} -d\ -f1 | ${sortbin} | ${uniqbin} | ${grepbin} "^${loggeduser}$"`
  2218. if [ "a${loggeduser}" = "a${confirm}" ]; then
  2219. debug "Unique display %s belongs to user %s.\n" "$1" "${loggeduser}"
  2220. unset confirm; unset loggeduser;
  2221. return 0
  2222. fi
  2223. debug "Display %s does not belongs to user %s.\n" "$1" "${loggeduser}"
  2224. unset confirm; unset loggeduser;
  2225. return 1
  2226. }
  2227.  
  2228. # Attemtps to get access to display $1
  2229. access_display() {
  2230. debug "Will attempt to get access to display %s.\n" "$1"
  2231. [ "a$1" = "a" ] && return 1
  2232. ! find_binary "xauth" && return 1
  2233. [ "a${XAUTHORITY}" = "a" ] && unset XAUTHORITY
  2234. gotaccess=`${xauthbin} nlist $1 2> /dev/null | ${wcbin} -l`
  2235. gotaccess=`echo ${gotaccess}`
  2236. if [ "a${gotaccess}" = "a0" ]; then
  2237. xuser=`${whobin} -u | ${grepbin} -v "^root" | ${grepbin} ${1} | ${cutbin} -d\ -f1 | ${sortbin} | ${uniqbin}`
  2238. xhome=$(${getentbin} passwd ${xuser} | ${cutbin} -d: -f6)
  2239. debug "Not currently trusted to display %s belonging to %s.\n" "${1}" "${xuser}"
  2240. if [ "a${LOCALAUTHORITY}" != "a" ] && [ -r "${LOCALAUTHORITY}" ]; then
  2241. xholder="${LOCALAUTHORITY}"
  2242. elif [ -d "${xhome}" ] && [ -r "${xhome}/.Xauthority" ]; then
  2243. xholder="${xhome}/.Xauthority"
  2244. else
  2245. unset xholder
  2246. fi
  2247. if [ "a${xholder}" = "a" ]; then
  2248. debug "Unable to locate Xauthority file of %s.\n" "${xuser}"
  2249. else
  2250. debug "Will attempt to steal cookie from %s.\n" "${xholder}"
  2251. if [ "a${HOME}" = "a" ]; then
  2252. rruser="${runner}"
  2253. we_are_root_already && rruser="root"
  2254. rrhome=$(${getentbin} passwd ${rruser} | ${cutbin} -d: -f6)
  2255. if [ -d "${rrhome}" -a "a${xhome}" != "a${rrhome}" ]; then
  2256. debug "HOME variable not set already. Setting HOME for %s: %s\n" "${rruser}" "${rrhome}"
  2257. export HOME="${rrhome}"
  2258. fi
  2259. unset rruser; unset rrhome;
  2260. else
  2261. debug "HOME directory set already (%s).\n" "${HOME}"
  2262. fi
  2263. ${xauthbin} -f "${xholder}" nextract - $1 2> /dev/null | ${xauthbin} nmerge - 2> /dev/null
  2264. gotaccess=`${xauthbin} nlist ${1} 2> /dev/null | ${wcbin} -l`
  2265. gotaccess=`echo ${gotaccess}`
  2266. if [ "a${gotaccess}" = "a0" ]; then
  2267. debug "Failed to steal cookie.\n"
  2268. else
  2269. debug "Cookie stolen from %s(%s) to %s.\n" "${xuser}" "${1}" "${HOME}/.Xauthority"
  2270. export XCOOKIE="$1"
  2271. addexittrap "release_X_cookie" EXIT
  2272. unset xuser; unset xhome; unset gotaccess; unset xholder
  2273. return 0
  2274. fi
  2275. fi
  2276. unset xuser; unset xhome; unset gotaccess; unset xholder
  2277. else
  2278. debug "Already granted access to display %s.\n" "${1}"
  2279. [ "a${XAUTHORITY}" != "a" ] && export LOCALAUTHORITY="${XAUTHORITY}"
  2280. unset gotaccess
  2281. return 0
  2282. fi
  2283. return 1
  2284. }
  2285.  
  2286. # Find DISPLAY
  2287. find_display() {
  2288. if [ -n "${stick_to_console}" ]; then
  2289. unset DISPLAY; unset display;
  2290. debug "Not using an X display due to stick-to-console variable.\n"
  2291. else
  2292. display="${DISPLAY}"
  2293. # If display is not set, or is remote, attempt to find a local display of runner user.
  2294. [ -z "${DISPLAY}" -o "`echo ${DISPLAY} | ${cutbin} -b1`" != ":" ] \
  2295. && display="" \
  2296. && [ -n "${runner}" -a "a${runner}" != "aroot" ] \
  2297. && find_binary "who" \
  2298. && display=`${whobin} -m | ${grepbin} "^${runner}" | ${grepbin} "(\:.*)" | ${cutbin} -d\( -f2 | ${cutbin} -d\) -f1 | ${sortbin} | ${headbin} -1`
  2299. # If DISPLAY is set to a local display, but we are run
  2300. # from root himself, and that X display is the unique
  2301. # local X display available, use it no matter what.
  2302. [ -n "${display}" -a -n "${DISPLAY}" -a "a${runner}" = "aroot" ] \
  2303. && ! unique_local_display "${DISPLAY}" && unset display
  2304. # Get number of display (if any)
  2305. displaynum=`echo ${display} | ${cutbin} -b2- | ${cutbin} -d. -f1`
  2306. display=":${displaynum}"
  2307. # Validate that determined xsession exists.
  2308. [ ! -S "/tmp/.X11-unix/X${displaynum}" ] && unset display
  2309. unset displaynum
  2310. [ -n "${display}" ] && ! access_display "${display}" && unset display
  2311. if [ -z "${display}" ]; then
  2312. debug "Unable to find a local X display, will stick to terminal.\n"
  2313. unset DISPLAY; unset display;
  2314. export stick_to_console=yes
  2315. find_gui
  2316. else
  2317. export display
  2318. export DISPLAY="${display}"
  2319. fi
  2320. fi
  2321. if [ -z "${display}" ]; then
  2322. unset DISPLAY; unset display;
  2323. export stick_to_console=yes
  2324. console_su_software
  2325. else
  2326. debug "Will be using display %s.\n" "${display}"
  2327. fi
  2328. }
  2329.  
  2330. # Selects GUI to be used if not already set by SGUI variable.
  2331. find_gui() {
  2332. debug "Selecting GUI.\n"
  2333. if [ -n "${stick_to_console}" -a -n "$SGUI" ]; then
  2334. debug "Validating pre-selected GUI: %s\n" "${SGUI}"
  2335. [ "a$SGUI" = "azenity" ] && unset SGUI
  2336. [ "a$SGUI" = "agdialog" ] && unset SGUI
  2337. [ "a$SGUI" = "akdialog" ] && unset SGUI
  2338. [ "a$SGUI" = "aXdialog" ] && unset SGUI
  2339. [ "a$SGUI" = "a9menu" ] && unset SGUI
  2340. [ "a$SGUI" = "agnome-terminal" ] && unset SGUI
  2341. [ "a$SGUI" = "akonsole" ] && unset SGUI
  2342. [ "a$SGUI" = "axterm" ] && unset SGUI
  2343. [ -z "${SGUI}" ] && debug "Could not be used from console. Will seek an alternative.\n"
  2344. fi
  2345. if [ "a${SGUI}" != "a" -a "a${SGUI}" != "ainteractive terminal" -a "a${SGUI}" != "aterminal" ] && ! find_binary "${SGUI}"; then
  2346. debug "%s not found. Will search for another GUI provider.\n" "${SGUI}"
  2347. unset SGUI
  2348. fi
  2349. if [ "a${stick_to_console}" = "a" ]; then
  2350. [ "a${SGUI}" = "a9menu" ] && ! find_binary "9menu" && unset SGUI
  2351. [ "a${SGUI}" = "a9menu" ] && ! find_binary "xterm" && unset SGUI
  2352. [ -z "${SGUI}" ] && find_binary "kdialog" && SGUI="kdialog"
  2353. [ -z "${SGUI}" ] && find_binary "zenity" && SGUI="zenity"
  2354. [ -z "${SGUI}" ] && find_binary "Xdialog" && SGUI="Xdialog"
  2355. [ -z "${SGUI}" ] && find_binary "gnome-terminal" && SGUI="gnome-terminal"
  2356. [ -z "${SGUI}" ] && find_binary "konsole" && SGUI="konsole"
  2357. [ -z "${SGUI}" ] && find_binary "xterm" && SGUI="xterm"
  2358. if [ -z "${SGUI}" ]; then
  2359. debug "No graphical GUI found. Forced to stick to terminal.\n"
  2360. export stick_to_console=yes
  2361. fi
  2362. fi
  2363. if [ "a${interactive}" != "a" ]; then
  2364. [ -z "${SGUI}" ] && find_binary "dialog" && SGUI="dialog"
  2365. [ -z "${SGUI}" ] && find_binary "whiptail" && SGUI="whiptail"
  2366. [ -z "${SGUI}" ] && SGUI="interactive terminal"
  2367. fi
  2368. [ -z "${SGUI}" ] && SGUI="terminal"
  2369. # Custom setup per GUI
  2370. [ "a${SGUI}" = "adialog" ] && addexittrap "dialog_clearscreen"
  2371. [ "a${SGUI}" = "aXdialog" -o "a${SGUI}" = "a9menu" ] && export notranslate=yes && unset foldwrapping && need_arg "foldwrapping"
  2372. [ "a${SGUI}" = "adialog" -o "a${SGUI}" = "awhiptail" -o "a${SGUI}" = "aterminal" -o "a${SGUI}" = "ainteractive terminal" ] && export stick_to_console=yes
  2373. [ "a${SGUI}" = "adialog" -o "a${SGUI}" = "awhiptail" -o "a${SGUI}" = "ainteractive terminal" -o "a${SGUI}" = "aXdialog" -o "a${SGUI}" = "azenity" -o "a${SGUI}" = "akdialog" -o "a${SGUI}" = "a9menu" ] && export interactive=yes
  2374. debug "%s selected as GUI.\n" "${SGUI}"
  2375. # Forward to terminal if have to do so
  2376. case "${SGUI}" in
  2377. xterm)
  2378. debug "Will spawn xterm window.\n"
  2379. unset SGUI; export stick_to_console=yes; export interactive=yes;
  2380. state_variables
  2381. eval ${xtermbin} -T "Sakis3G" +cm +dc -e ${ME} ${allargs} ${statevariables}
  2382. stop_with $?
  2383. ;;
  2384. gnome-terminal)
  2385. debug "Will spawn gnome-terminal window.\n"
  2386. unset SGUI; export stick_to_console=yes; export interactive=yes;
  2387. state_variables
  2388. eval ${gnome_terminalbin} -t "Sakis3G" -x ${ME} ${allargs} ${statevariables}
  2389. stop_with $?
  2390. ;;
  2391. konsole)
  2392. debug "Will spawn konsole window.\n"
  2393. unset SGUI; export stick_to_console=yes; export interactive=yes;
  2394. state_variables
  2395. eval ${konsolebin} --title "Sakis3G" -e "${ME} ${allargs} ${statevariables}"
  2396. stop_with $?
  2397. ;;
  2398. esac
  2399. # Check for OSD
  2400. unset OSDOUTPUT
  2401. if [ "a${stick_to_console}" = "a" -a "a${prefer_osd}" != "a" ]; then
  2402. if find_binary "osd_cat"; then
  2403. export OSDOUTPUT="osd_cat"
  2404. elif find_binary "aosd_cat"; then
  2405. export OSDOUTPUT="aosd_cat"
  2406. fi
  2407. [ "a${OSDOUTPUT}" != "a" ] && debug "Will be using \"%s\" for displaying OSD messages.\n" "${OSDOUTPUT}"
  2408. fi
  2409. [ "a${prefer_osd}" != "a" -a "a${OSDOUTPUT}" = "a" ] && unset prefer_osd && debug "OSD will not appear.\n"
  2410. return 0
  2411. }
  2412.  
  2413. # Checks if module $1 is loaded
  2414. module_loaded() {
  2415. [ "a$1" = "a" ] && return 99
  2416. unset moduleloaded
  2417. debug "Checking if module \"%s\" is currently loaded.\n" "$1"
  2418. if [ -r "/proc/modules" ]; then
  2419. moduleloaded=`${grepbin} "^$1 " "/proc/modules" 2> /dev/null | ${sedbin} -e "s/^$1 \([0-9][0-9]*\) \([0-9][0-9]*\) \(.*\)$/\1 \3/g"`
  2420. if [ "a${moduleloaded}" = "a" ]; then
  2421. debug "Module \"%s\" is not currently loaded.\n" "$1"
  2422. unset moduleloaded
  2423. return 1
  2424. elif [ "a${moduleloaded}" = "a$1 0" ]; then
  2425. debug "Module \"%s\" is currently loaded having no users.\n" "$1"
  2426. unset moduleloaded
  2427. return 0
  2428. else
  2429. debug "Module \"%s\" is currently loaded and occupied.\n" "$1"
  2430. unset moduleloaded
  2431. return 0
  2432. fi
  2433. elif find_binary "lsmod"; then
  2434. moduleloaded=`${lsmodbin} | ${grepbin} "^$1 " | ${wcbin} -l`; moduleloaded=`echo ${moduleloaded}`
  2435. if [ "a${moduleloaded}" = "a" -o "a${moduleloaded}" = "a0" ]; then
  2436. debug "Module \"%s\" is not currently loaded.\n" "$1"
  2437. unset moduleloaded
  2438. return 1
  2439. else
  2440. debug "Module \"%s\" is currently loaded.\n" "$1"
  2441. unset moduleloaded
  2442. return 0
  2443. fi
  2444. else
  2445. show_fmt_error "Unable to check if module is currently loaded or if it has users.\n"
  2446. return 1
  2447. fi
  2448. }
  2449.  
  2450. # Unloads module from system if possible
  2451. module_unload() {
  2452. [ "a$1" = "a" ] && return 99
  2453. ! module_loaded "$1" && return 0
  2454. ! find_binary "modprobe" && return 1
  2455. debug "Attempting to unload module \"%s\".\n" "$1"
  2456. debug run_command "${modprobebin} -r -v $1"
  2457. debug "Waiting for module to vanish.\n"
  2458. if module_loaded "$1"; then
  2459. show_fmt_error "Failed to unload module \"%s\".\n" "$1"
  2460. return 1
  2461. else
  2462. debug "Module \"%s\" succesfully unloaded.\n" "$1"
  2463. return 0
  2464. fi
  2465. }
  2466.  
  2467. # Loads module $1 if possible, providing arguments for device $2 if applicable
  2468. module_load() {
  2469. [ "a$1" = "a" ] && return 99
  2470. module_loaded "$1" && return 0
  2471. ! find_binary "modprobe" && return 1
  2472. debug "Attempting to load module \"%s\".\n" "$1"
  2473. need_arg "SERIALDRIVERS"
  2474. needsarg=`echo " ${SERIALDRIVERS} " | ${grepbin} " $1 " | ${wcbin} -l`; needsarg=`echo ${needsarg}`
  2475. if [ "a${needsarg}" = "a1" -a "a$2" != "a" ]; then
  2476. if usb_device_connected "$2"; then
  2477. modulevend=`echo "$2" | ${cutbin} -d: -f1`
  2478. moduleprod=`echo "$2" | ${cutbin} -d: -f2`
  2479. modargs="vendor=0x${modulevend} product=0x${moduleprod}"
  2480. unset modulevend; unset moduleprod
  2481. debug "Will provide arguments to \"%s\": %s\n" "$1" "${modargs}"
  2482. else
  2483. debug "Warning: Module \"%s\" needs arguments, but \"%s\" does not refer to a connected device.\n" "$1" "$2"
  2484. unset modargs
  2485. fi
  2486. elif [ "a${needsarg}" = "a1" ]; then
  2487. debug "Warning: Module \"%s\" needs arguments but device was not specified.\n" "$1"
  2488. unset modargs
  2489. fi
  2490. debug run_command "${modprobebin} $1 ${modargs}"
  2491. debug "Waiting for module to get loaded.\n"
  2492. unset modargs; unset needsarg
  2493. counter=0
  2494. while [ "${counter}" -lt "6" ]
  2495. do
  2496. if ! module_loaded "$1"; then
  2497. debug "Module \"%s\" still not live. Waiting a second.\n" "$1"
  2498. sleep 1
  2499. fi
  2500. if module_loaded "$1"; then
  2501. debug "Module \"%s\" succesfully loaded.\n" "$1"
  2502. unset counter
  2503. return 0
  2504. fi
  2505. counter=`expr ${counter} + 1`; counter=`echo ${counter}`
  2506. done
  2507. unset counter
  2508. show_fmt_error "Failed to load module \"%s\".\n" "$1"
  2509. return 1
  2510. }
  2511.  
  2512. # Attempts to detach driver $1 from device $2 interface $3
  2513. # If failed to detach, attempts to completely unload $1 before failing.
  2514. module_unbind() {
  2515. [ "a$1" = "a" ] && return 99
  2516. verbose "Unloading driver %s" "$1"
  2517. if [ "a$2" = "a" -a "a$3" = "a" ]; then
  2518. debug "Requested to unbind module \"%s\" without device specified. Will try to unload it.\n" "$1"
  2519. module_unload "$1"
  2520. return $?
  2521. fi
  2522. moduleattached=`usb_loaded_driver "$2" "$3" | ${tailbin} -1 | ${grepbin} "$1" | ${wcbin} -l`
  2523. if [ "a${TIMEOUTOCCURED}" != "a" ]; then
  2524. show_fmt_error "Seems like an electrical/USB bus error occured. You may need to replug your modem for it to properly work.\n"
  2525. unset TIMEOUTOCCURED
  2526. fi
  2527. moduleattached=`echo ${moduleattached}`
  2528. if [ "a${moduleattached}" = "a" -o "a${moduleattached}" = "a0" ]; then
  2529. debug "Module \"%s\" does not appear to be attached to device \"%s\".\n" "$1" "$2"
  2530. unset moduleattached
  2531. return 0
  2532. fi
  2533. debug "Module \"%s\" is currently attached to \"%s\".\n" "$1" "$2"
  2534. [ "a$3" != "a" ] && usb_sysfsdir_int "$2" "$3" && unbindvalue=`${basenamebin} ${sysfsintloc}`
  2535. [ "a$3" = "a" ] && usb_sysfsdir && unbindvalue=`cd "${sysfsloc}"; ${lsbin} -1d \`${basenamebin} ${sysfsloc}\`*`
  2536. [ "a${unbindvalue}" = "a" ] && unbindvalue=`echo "$2" | ${sedbin} -e "s/:/ /g"`
  2537. sent=0
  2538. [ "a$1" = "ausb_storage" ] && localdrivername="usb-storage"
  2539. for endpoint in /sys/bus/usb/drivers/${localdrivername}/unbind /sys/bus/usb/drivers/$1/unbind
  2540. do
  2541. for destination in ${unbindvalue}
  2542. do
  2543. if [ -w "${endpoint}" ]; then
  2544. echo -n "${destination}" > "${endpoint}" 2> /dev/null
  2545. debug "Sent \"%s\" to \"%s\".\n" "${destination}" "${endpoint}"
  2546. sent=1
  2547. fi
  2548. done
  2549. unset destination
  2550. done
  2551. unset unbindvalue; unset endpoint; unset localdrivername
  2552. if [ "${sent}" -eq "1" ]; then
  2553. debug "Expecting module to detach from device.\n"
  2554. while [ "${sent}" -lt "6" ]
  2555. do
  2556. unset moduleattached
  2557. moduleattached=`usb_loaded_driver "$2" "$3" | ${tailbin} -1 | ${grepbin} "$1" | ${wcbin} -l`
  2558. if [ "a${TIMEOUTOCCURED}" != "a" ]; then
  2559. show_fmt_error "Seems like an electrical/USB bus error occured. You may need to replug your modem for it to properly work.\n"
  2560. unset TIMEOUTOCCURED
  2561. break
  2562. fi
  2563. moduleattached=`echo ${moduleattached}`
  2564. if [ "a${moduleattached}" = "a" -o "a${moduleattached}" = "a0" ]; then
  2565. debug "Module \"%s\" has detached from device \"%s\".\n" "$1" "$2"
  2566. unset moduleattached; unset sent
  2567. if find_binary "modprobe"; then
  2568. if [ "a$1" != "ausb_storage" -a "a$1" != "ausb-storage" ]; then
  2569. debug "Will attempt to also remove it.\n"
  2570. debug run_command "${modprobebin} -r -v $1"
  2571. module_loaded "$1"
  2572. fi
  2573. fi
  2574. return 0
  2575. fi
  2576. debug "Module still attached. Waiting for %d second(s).\n" "${sent}"
  2577. sent=`expr ${sent} + 1`; sent=`echo ${sent}`
  2578. sleep 1
  2579. done
  2580. debug "Module \"%s\" did not detach from device \"%s\". Will try to unload it.\n" "$1" "$2"
  2581. fi
  2582. module_unload "$1"
  2583. return $?
  2584. }
  2585.  
  2586. # Attempts to bind module $1 to device $2, interface $3 (optional)
  2587. module_bind() {
  2588. [ "a$1" = "a" -o "a$2" = "a" ] && return 99
  2589. verbose "Loading driver %s" "$1"
  2590. module_load "$1" "$2"
  2591. ret=$?; [ "${ret}" -ne "0" ] && return ${ret}
  2592. unset moduleattached
  2593. modulename="$1"
  2594. [ "a${modulename}" = "ausb-storage" ] && modulename="usb_storage"
  2595. [ "a${modulename}" = "acdc-acm" ] && modulename="cdc_acm"
  2596. moduleattached=`usb_loaded_driver "$2" "$3" | ${tailbin} -1 | ${grepbin} "${modulename}" | ${wcbin} -l`
  2597. if [ "a${TIMEOUTOCCURED}" != "a" ]; then
  2598. show_fmt_error "Seems like an electrical/USB bus error occured. You may need to replug your modem for it to properly work.\n"
  2599. unset TIMEOUTOCCURED
  2600. fi
  2601. moduleattached=`echo ${moduleattached}`
  2602. if [ "a${moduleattached}" != "a" -a "a${moduleattached}" != "a0" ]; then
  2603. debug "Module \"%s\" attached to device \"%s\".\n" "$1" "$2"
  2604. unset moduleattached; unset sent; unset modulename
  2605. return 0
  2606. fi
  2607. unset moduleattached
  2608. debug "Module \"%s\" not yet attached to \"%s\".\n" "$1" "$2"
  2609. bindvalue=`echo "$2" | ${sedbin} -e "s/:/ /g"`
  2610. sent=0
  2611. for endpoint in /sys/module/${modulename}/drivers/usb-serial:*/new_id
  2612. do
  2613. if [ -w "${endpoint}" ]; then
  2614. echo -n "${bindvalue}" > "${endpoint}" 2> /dev/null
  2615. if [ "$?" -eq "0" ]; then
  2616. debug "Sent \"%s\" to \"%s\".\n" "${bindvalue}" "${endpoint}"
  2617. sent=1
  2618. fi
  2619. fi
  2620. done
  2621. if [ "a${sent}" != "a1" ]; then
  2622. for endpoint in /sys/module/${modulename}/drivers/usb:*/new_id
  2623. do
  2624. echo -n "${bindvalue}" > "${endpoint}" 2> /dev/null
  2625. if [ "$?" -eq "0" ]; then
  2626. debug "Sent \"%s\" to \"%s\".\n" "${bindvalue}" "${endpoint}"
  2627. sent=1
  2628. fi
  2629. done
  2630. fi
  2631. unset bindvalue; unset endpoint
  2632. if [ "a${sent}" = "a1" ]; then
  2633. counter=0
  2634. while [ "${counter}" -lt "10" ]
  2635. do
  2636. moduleattached=`usb_loaded_driver "$2" "$3" | ${tailbin} -1 | ${grepbin} "${modulename}" | ${wcbin} -l`
  2637. if [ "a${TIMEOUTOCCURED}" != "a" ]; then
  2638. show_fmt_error "Seems like an electrical/USB bus error occured. You may need to replug your modem for it to properly work.\n"
  2639. unset TIMEOUTOCCURED
  2640. break;
  2641. fi
  2642. moduleattached=`echo ${moduleattached}`
  2643. if [ "a${moduleattached}" != "a1" ]; then
  2644. debug "Waiting one second.\n"
  2645. sleep 1
  2646. fi
  2647. moduleattached=`usb_loaded_driver "$2" "$3" | ${tailbin} -1 | ${grepbin} "${modulename}" | ${wcbin} -l`
  2648. if [ "a${TIMEOUTOCCURED}" != "a" ]; then
  2649. show_fmt_error "Seems like an electrical/USB bus error occured. You may need to replug your modem for it to properly work.\n"
  2650. unset TIMEOUTOCCURED
  2651. break;
  2652. fi
  2653. moduleattached=`echo ${moduleattached}`
  2654. if [ "a${moduleattached}" != "a" -a "a${moduleattached}" != "a0" ]; then
  2655. debug "Module \"%s\" attached to device \"%s\".\n" "$1" "$2"
  2656. unset moduleattached; unset sent; unset counter; unset modulename
  2657. return 0
  2658. fi
  2659. unset moduleattached
  2660. counter=`expr ${counter} + 1`; counter=`echo ${counter}`
  2661. done
  2662. unset counter
  2663. fi
  2664. debug "Module \"%s\" did not bind to device \"%s\".\n" "$1" "$2"
  2665. if [ "a$4" != "anoreload" ]; then
  2666. debug "Will attempt to do a reload cycle.\n"
  2667. if module_unload "$1" "$2" "$3"; then
  2668. if module_bind "$1" "$2" "$3" "noreload"; then
  2669. unset counter; unset sent; unset moduleattached; unset modulename
  2670. debug "Reload cycle did the trick.\n"
  2671. debug "Module \"%s\" attached to device \"%s\".\n" "$1" "$2"
  2672. return 0
  2673. else
  2674. return $?
  2675. fi
  2676. fi
  2677. fi
  2678. unset moduleattached; unset modulename
  2679. if [ "${sent}" -eq "0" ]; then
  2680. show_fmt_error "Module \"%s\" loaded but did not bind to device \"%s\".\n" "$1" "$2"
  2681. else
  2682. show_fmt_error "Module \"%s\" loaded but refused to bind to device \"%s\".\n" "$1" "$2"
  2683. fi
  2684. unset sent; unset counter
  2685. return 1
  2686. return 99
  2687. }
  2688.  
  2689. # Unlocks a previously acquired lock to hal
  2690. hal_unlock() {
  2691. [ "a${nohal}" != "a" ] && return 0
  2692. [ "a${HAL_LOCK}" = "a" ] && return 0
  2693. debug "Releasing HAL lock %d.\n" "${HAL_LOCK}"
  2694. if notrunning "${HAL_LOCK}"; then
  2695. debug "Already unlocked.\n"
  2696. else
  2697. if [ -f "/tmp/sakis3g.hal.lock.$$" ]; then
  2698. ${rmbin} -f "/tmp/sakis3g.hal.lock.$$"
  2699. sleep 2
  2700. fi
  2701. if ! notrunning "${HAL_LOCK}"; then
  2702. debug "Failed to unlock by unlinking mutex file. Will try to kill.\n"
  2703. term_clearline
  2704. ${killbin} -1 ${HAL_LOCK} 2> /dev/null
  2705. if ! notrunning "${HAL_LOCK}"; then
  2706. debug "Failed to kill PID %d.\n" "${HAL_LOCK}"
  2707. return 1
  2708. fi
  2709. fi
  2710. debug "Unlocked.\n"
  2711. fi
  2712. unset HAL_LOCK
  2713. return 0
  2714. }
  2715.  
  2716. # Attempts to establish lock of interface $1 (i.e. org.freedesktop.Hal.Device.Storage)
  2717. hal_acquire_lock() {
  2718. [ "a${nohal}" != "a" ] && return 0
  2719. [ "a${HAL_LOCK}" != "a" ] && ! hal_unlock && return 1
  2720. ! find_binary "hal-lock" && return 1
  2721. ! find_binary "kill" && return 1
  2722. ! find_binary "touch" && return 1
  2723. ! we_are_root && return 1
  2724. debug "Acquiring lock in HAL for \"%s\".\n" "$1"
  2725. ${touchbin} "/tmp/sakis3g.hal.lock.$$"
  2726. hal-lock --interface "$1" --exclusive --run "${ME} holdlock /tmp/sakis3g.hal.lock.$$" &
  2727. halpid=$!; unset informed
  2728. while ! notrunning ${halpid}
  2729. do
  2730. hallocksuccess=`${catbin} "/tmp/sakis3g.hal.lock.$$" 2> /dev/null`; hallocksuccess=`echo ${hallocksuccess} | ${sedbin} -e "s/ //g"`
  2731. if [ "a${hallocksuccess}" = "a" ]; then
  2732. unset hallocksuccess
  2733. [ "a${informed}" = "a" ] && verbose "Acquiring exclusive lock to HAL"
  2734. informed=`expr ${informed} + 1`; informed=`echo ${informed}`
  2735. if [ "${informed}" -le "20" ]; then
  2736. debug "Waiting for spawned process to acquire lock (%d secs will have passed).\n" "${informed}"
  2737. sleep 1
  2738. else
  2739. debug "Giving up waiting for lock to occur. Will try to terminate current lock attempt.\n"
  2740. export HAL_LOCK=${halpid}
  2741. hal_unlock; unset HAL_LOCK
  2742. break
  2743. fi
  2744. else
  2745. debug "Exclusive lock granted to PID %d.\n" "${hallocksuccess}"
  2746. break;
  2747. fi
  2748. done
  2749. if [ "a${hallocksuccess}" != "a" ]; then
  2750. export HAL_LOCK=${halpid}
  2751. addexittrap hal_unlock
  2752. debug "Succesfully locked HAL interface \"%s\" (hal-lock itself running with PID %d).\n" "$1" "${HAL_LOCK}"
  2753. unset halpid; unset informed; unset hallocksuccess
  2754. return 0
  2755. fi
  2756. unset halpid; unset informed; unset hallocksuccess
  2757. debug "Failed to acquire exclusive lock to %s.\n" "$1"
  2758. return 1
  2759. }
  2760.  
  2761. # Makes sure that only one instance of $3 is contained within
  2762. # list $2 of hal udi $1.
  2763. hal_add_to_list() {
  2764. ! find_binary "hal-get-property" && return 1
  2765. ! find_binary "hal-set-property" && return 1
  2766. [ "$#" -ne "3" ] && return 1
  2767. halfound=0
  2768. while [ "${halfound}" -ne "1" ];
  2769. do
  2770. contents=`hal-get-property --udi "$1" --key "$2" 2> /dev/null`
  2771. halfound=0
  2772. for op in ${contents}
  2773. do
  2774. if [ "a${op}" = "a$3" ]; then
  2775. halfound=`expr ${halfound} + 1`
  2776. halfound=`echo ${halfound}`
  2777. fi
  2778. done
  2779. if [ "${halfound}" -eq "0" ]; then
  2780. debug "Will attempt to add \"%s\" inside \"%s\".\n" "$3" "$2"
  2781. hal-set-property --udi "$1" --key "$2" --strlist-post "$3" 2> /dev/null
  2782. elif [ "${halfound}" -gt "1" ]; then
  2783. debug "Will attempt to remove multiple instance of \"%s\" inside \"%s\".\n" "$3" "$2"
  2784. hal-set-property --udi "$1" --key "$2" --strlist-rem "$3" 2> /dev/null
  2785. fi
  2786. hala=$?
  2787. if [ "a${hala}" != "a0" ]; then
  2788. debug "Unable to properly modify HAL.\n"
  2789. break
  2790. fi
  2791. done
  2792. [ "${halfound}" -eq "1" ] && debug "HAL is updated.\n"
  2793. unset contents; unset op; unset hala
  2794. if [ "a${halfound}" = "a1" ]; then
  2795. unset halfound
  2796. return 0
  2797. fi
  2798. unset halfound
  2799. return 1
  2800. }
  2801.  
  2802. # Makes sure modem $1 capabilities are loaded on hal
  2803. hal_tty_update() {
  2804. [ "a${nohal}" != "a" ] && return 0
  2805. [ "a${nohalinform}" != "a" ] && return 0
  2806. ! find_binary "hal-get-property" && return 1
  2807. ! find_binary "hal-set-property" && return 1
  2808. ! find_binary "hal-find-by-property" && return 1
  2809. unset halcapabilities
  2810. case "a${TTY_CAPABILITIES}" in
  2811. aGSM)
  2812. halcapabilities="GSM-07.07 GSM-07.05"
  2813. ;;
  2814. a)
  2815. debug "No capabilities found. Don't know what to tell to HAL.\n"
  2816. ;;
  2817. *)
  2818. debug "FIX ME: Unknown capabilities %s. Don't know what to tell to HAL.\n" "${TTY_CAPABILITIES}"
  2819. ;;
  2820. esac
  2821. [ "a${halcapabilities}" = "a" ] && unset halcapabilities && return 1
  2822. debug "Capabilities of %s are: %s\n" "$1" "${halcapabilities}"
  2823. haludi=`hal-find-by-property --key "linux.device_file" --string "$1" 2> /dev/null | ${tailbin} -1`
  2824. if [ "a${haludi}" = "a" ]; then
  2825. unset haludi; unset halcapabilities
  2826. debug "No HAL device referring to %s was found.\n" "$1"
  2827. return 1
  2828. fi
  2829. debug "Found device in HAL: %s\n" "${haludi}"
  2830. verbose "Updating HAL"
  2831. hal_add_to_list "${haludi}" info.capabilities "modem"
  2832. for cap in ${halcapabilities}
  2833. do
  2834. hal_add_to_list "${haludi}" modem.command_sets "${cap}"
  2835. done
  2836. debug "HAL was updated that %s is a modem.\n" "$1"
  2837. find_binary "hal-device" && debug run_command "hal-device \"${haludi}\""
  2838. unset cap; unset halcapabilities; unset haludi
  2839. return 0
  2840. }
  2841.  
  2842. # If $1 node does not exist, creates it with major $2 and minor $3
  2843. dev_create_node() {
  2844. [ "a$1" = "a" -o "a$2" = "a" -o "a$3" = "a" ] && return 99
  2845. if [ ! -c "$1" ]; then
  2846. debug "Device node \"%s\" does not exist.\n" "$1"
  2847. if ! find_binary "mknod"; then
  2848. show_fmt_error "Unable to create device node \"%s\"." "$1"
  2849. return 1
  2850. fi
  2851. debug run_command "${mknodbin} \"$1\" c $2 $3"
  2852. if [ ! -c "$1" ]; then
  2853. show_fmt_error "Failed to create device node \"%s\"." "$1"
  2854. return 1
  2855. fi
  2856. debug "Made node \"%s (%d,%d)\" ourselves.\n" "$1" "$2" "$3"
  2857. return 0
  2858. else
  2859. debug "Device node \"%s\" already exists.\n" "$1"
  2860. return 0
  2861. fi
  2862. }
  2863.  
  2864. pin_valid() {
  2865. [ "a$1" = "a" ] && return 1
  2866. need_binary "expr"
  2867. givenpin=`echo $1`
  2868. mathpin=`${exprbin} 1${givenpin} + 1 - 1 2> /dev/null`; mathpin=`echo ${mathpin}`
  2869. if [ "a${mathpin}" = "a1${givenpin}" ]; then
  2870. if [ "1${givenpin}" -ge "10000" -a "1${givenpin}" -le "19999" ]; then
  2871. debug "Valid PIN %s.\n" "$1"
  2872. return 0
  2873. fi
  2874. fi
  2875. debug "Invalid PIN %s.\n" "$1"
  2876. return 1
  2877. }
  2878.  
  2879. pin_prompt() {
  2880. user_prompt "SIM_PIN" "Modem needs PIN" "Please enter PIN number, or leave empty to abort" "OK" "Cancel"
  2881. case "a${SIM_PIN}" in
  2882. a)
  2883. debug "User did not provide PIN.\n"
  2884. unset SIM_PIN
  2885. return 98
  2886. ;;
  2887. *)
  2888. if ! pin_valid "${SIM_PIN}"; then
  2889. debug "PIN supplied by user was not a valid PIN number (%s).\n" "${SIM_PIN}"
  2890. unset SIM_PIN
  2891. pin_prompt
  2892. ret=$?
  2893. fi
  2894. export SIM_PIN
  2895. return 0
  2896. ;;
  2897. esac
  2898. }
  2899.  
  2900.  
  2901. # Makes sure tty $1 is not busy
  2902. tty_not_busy() {
  2903. [ "a$1" = "a" ] && return 1
  2904. [ ! -c "$1" ] && return 1
  2905. if we_are_root; then
  2906. ttyusers=`${lsbin} -l /proc/*/fd/* 2> /dev/null | ${grepbin} "${1}$" | ${sedbin} -e "s/\(.*\)\/proc\/\([0-9][0-9]*\)\/fd\/\(.*\)/\2/g" | ${sortbin} | ${uniqbin} | ${wcbin} -l` ; ttyusers=`echo ${ttyusers}`
  2907. if [ "a${ttyusers}" = "a0" ]; then
  2908. unset ttyusers
  2909. debug "Device %s is not busy.\n" "$1"
  2910. [ "a$2" != "a" ] && verbose "Resuming"
  2911. return 0
  2912. fi
  2913. debug "Device %s is currently occupied by %d process(es).\n" "$1" "${users}"
  2914. ttyusers=`${lsbin} -l /proc/*/fd/* 2> /dev/null | ${grepbin} "${1}$" | ${sedbin} -e "s/\(.*\)\/proc\/\([0-9][0-9]*\)\/fd\/\(.*\)/\2/g" | ${sortbin} | ${uniqbin}` ; ttyusers=`echo ${ttyusers}`
  2915. debug "PID(s) are: %s\n" "${ttyusers}"
  2916. for ttyp in ${ttyusers}
  2917. do
  2918. debug run_command "${psbin} -p ${ttyp} -o pid,comm= | ${tailbin} -1"
  2919. ttyusers="${ttyusers} `${psbin} -p ${ttyp} -o comm= 2> /dev/null`"
  2920. done
  2921. unset ttyp
  2922. if [ "a$2" = "a" ]; then
  2923. debug "Will wait for 10 seconds in case port is freed.\n"
  2924. verbose "Waiting %s to be released by PID %s." "$1" "${ttyusers}"
  2925. ttycount=11
  2926. else
  2927. ttycount=$2
  2928. fi
  2929. ttycount=`expr ${ttycount} - 1`
  2930. if [ "a${ttycount}" = "a0" ]; then
  2931. show_fmt_error "Port %s is currently occupied by %s." "$1" "${ttyusers}"
  2932. unset ttyusers; unset ttycount;
  2933. return 1
  2934. else
  2935. debug "Wait for another %d seconds in case port %s is freed.\n" "${ttycount}" "$1"
  2936. sleep 1
  2937. if tty_not_busy $1 $ttycount; then
  2938. unset ttyusers; unset ttycount;
  2939. return 0
  2940. else
  2941. unset ttyusers; unset ttycount;
  2942. return 1
  2943. fi
  2944. fi
  2945. unset ttyusers; unset ttycount;
  2946. else
  2947. show_fmt_error "Unable to check if %s is occupied. Not root." "$1"
  2948. fi
  2949. return 1
  2950. }
  2951.  
  2952. at_default_commands() {
  2953. unset ttycommands
  2954. [ "a$1" = "a" ] && return 1
  2955. case "a$1" in
  2956. aPROBEGSM)
  2957. ttycommands="ATI OK 'AT+GCAP' OK 'AT+CGSN' OK"
  2958. ;;
  2959. aIDENTIFY)
  2960. ttycommands="AT+CGMM OK"
  2961. ;;
  2962. aSTAGE0)
  2963. if [ "a${INIT_STAGE0}" != "a" ]; then
  2964. ttycommands="${INIT_STAGE0}"
  2965. fi
  2966. ;;
  2967. aSTAGE1)
  2968. if [ "a${INIT_STAGE1}" != "a" ]; then
  2969. ttycommands="${INIT_STAGE1}"
  2970. fi
  2971. ;;
  2972. aPINCHECK)
  2973. if [ "a${INIT_STAGE2}" != "a" ]; then
  2974. ttycommands="${INIT_STAGE2}"
  2975. debug "Using instructed PINCHECK.\n"
  2976. else
  2977. ttycommands="'AT+CPIN?' OK"
  2978. debug "Using default PINCHECK.\n"
  2979. fi
  2980. ;;
  2981. aPINSUPPLY)
  2982. if pin_valid "${2}"; then
  2983. if [ "a${INIT_STAGE3}" != "a" ]; then
  2984. ttycommands=`${printfbin} "${INIT_STAGE3}\n" "${2}"`
  2985. debug "Using instructed PINSUPPLY.\n"
  2986. else
  2987. ttycommands=`${printfbin} "'AT+CPIN=\"%s\"' OK\n" "${2}"`
  2988. debug "Using default PINSUPPLY.\n"
  2989. fi
  2990. fi
  2991. ;;
  2992. aSTAGE4)
  2993. if [ "a${INIT_STAGE4}" != "a" ]; then
  2994. ttycommands="${INIT_STAGE4}"
  2995. fi
  2996. ;;
  2997. aOPERATOR)
  2998. ttycommands="AT+COPS=3,2 OK 'AT+COPS?' OK"
  2999. ;;
  3000. aOPERATORS)
  3001. ttycommands="AT+COPS=3,2 OK 'AT+COPS=?' TIMEOUT 120 OK"
  3002. ;;
  3003. aOPERATORNAME)
  3004. ttycommands="AT+COPS=3,0 OK 'AT+COPS?' OK"
  3005. ;;
  3006. aSIMOPERATORNAME)
  3007. ttycommands="'AT+CRSM=176,28486,0,0,17' OK"
  3008. ;;
  3009. aSETOPERATOR)
  3010. ttycommands=`${printfbin} "'AT+COPS=1,2,\"%s\"' OK\n" "${2}"`
  3011. ;;
  3012. aSTAGE5)
  3013. if [ "a${INIT_STAGE5}" != "a" ]; then
  3014. ttycommands="${INIT_STAGE5}"
  3015. fi
  3016. ;;
  3017. aDETECTAPN)
  3018. ttycommands="AT+CGDCONT? OK"
  3019. ;;
  3020. aINITIALIZE)
  3021. if [ "a${APN}" != "a" ]; then
  3022. apnpart=`echo "${APN}" | ${cutbin} -d: -f1`
  3023. [ "a${apnpart}" = "aCUSTOM_APN" ] && apnpart="${CUSTOM_APN}"
  3024. if [ "a${INIT_STAGE6}" != "a" ]; then
  3025. ttycommands=`${printfbin} "${INIT_STAGE6}\n" "${apnpart}"`
  3026. debug "Using instructed INITIALIZE.\n"
  3027. else
  3028. ttycommands=`${printfbin} "ATZ OK 'AT&F' OK 'ATQ0 V1 E1' OK 'AT&D2 &C1' OK AT+FCLASS=0 OK ATS0=0 OK 'AT+CGDCONT=1,\"IP\",\"%s\"' OK\n" "${apnpart}"`
  3029. debug "Using default INITIALIZE.\n"
  3030. fi
  3031. unset apnpart
  3032. fi
  3033. ;;
  3034. aSTAGE7)
  3035. if [ "a${INIT_STAGE7}" != "a" ]; then
  3036. ttycommands="${INIT_STAGE7}"
  3037. fi
  3038. ;;
  3039. aSTAGE8)
  3040. if [ "a${INIT_STAGE8}" != "a" ]; then
  3041. ttycommands="${INIT_STAGE8}"
  3042. fi
  3043. ;;
  3044. aDIAL)
  3045. dialphone="${ISP_DIAL}"
  3046. [ "a${dialphone}" = "a" ] && dialphone="*99#"
  3047. ttycommands=`${printfbin} "ATD%s\n" "${dialphone}"`
  3048. unset dialphone
  3049. ;;
  3050. esac
  3051. [ "a${ttycommands}" != "a" ] && debug "Command \"%s\" refers to AT commands: %s\n" "$1" "${ttycommands}" && return 0
  3052. debug "Unknown command \"%s\".\n" "$1"
  3053. return 1
  3054. }
  3055.  
  3056. # Send command $2 to tty $1
  3057. tty_send_command() {
  3058. unset LASTTTYLOG
  3059. [ "a$1" = "a" -o "a$2" = "a" ] && return 99
  3060. if [ ! -c "$1" ]; then
  3061. debug "Device node %s did not exist while trying \"%s\" command.\n" "$1" "$2"
  3062. if [ "a${informedspurious}" != "a1" ] && [ "a${usbdevice}" != "a" ] && usb_device_connected "${usbdevice}" && usb_has_storage "$1" && [ "a${killstorage}" = "a" ]; then
  3063. show_fmt_error "Spurious changes in tty %s. Consider using \"killstorage\" switch in case it improves stability." "$1"
  3064. export informedspurious=1
  3065. fi
  3066. if [ "a${informedspurious}" != "a1" ]; then
  3067. show_fmt_error "Spurious changes in tty %s. Problem may be solved by upgrading your kernel or by choosing another driver." "$1"
  3068. export informedspurious=1
  3069. fi
  3070. [ ! -c "$1" ] && debug "Waiting one second in case it appears.\n" && sleep 1
  3071. [ ! -c "$1" ] && debug "Device still missing. Aborting.\n" && return 1
  3072. debug "Device %s appeared. Will proceed sending %s commands.\n" "$1" "$2"
  3073. fi
  3074. ! find_binary "chat" && return 4
  3075. need_arg "CHAT_ABORT_STRINGS"
  3076. ! at_default_commands "$2" "$3" && return 1
  3077. ttycommands="\"\" '\pAT' OK ${ttycommands} '\pAT' OK"
  3078. debug "Will send %s commands to tty %s: %s\n" "$2" "$1" "${ttycommands}"
  3079. ! tty_not_busy "$1" && return 1
  3080. timestamp_before=`${lsbin} --full-time -G1 $1 2> /dev/null`; timestamp_before=`echo ${timestamp_before}`
  3081. sh -c "${chatbin} -t 2 -e ${CHAT_ABORT_STRINGS} ${ttycommands} >> ${1} < ${1} 2> /tmp/sakis3g.chat.$$.log"
  3082. timestamp_after=`${lsbin} --full-time -G1 $1 2> /dev/null`; timestamp_before=`echo ${timestamp_after}`
  3083. if [ "a${timestamp_before}" != "a${timestamp_after}" ]; then
  3084. debug "Spurious changes in tty %s.\n" "$1"
  3085. if [ "a${usbdevice}" != "a" ] && usb_device_connected "${usbdevice}" && usb_has_storage "$1" && [ "a${killstorage}" = "a" ] && [ "a${informedspurious}" != "a1" ]; then
  3086. show_fmt_error "Spurious changes in tty %s. Consider using \"killstorage\" switch in case it improves stability." "$1"
  3087. export informedspurious=1
  3088. fi
  3089. if [ "a${informedspurious}" != "a1" ]; then
  3090. show_fmt_error "Spurious changes in tty %s. Problem may be solved by upgrading your kernel or by choosing another driver." "$1"
  3091. export informedspurious=1
  3092. fi
  3093. fi
  3094. unset ttycommands
  3095. if [ -f "/tmp/sakis3g.chat.$$.log" ]; then
  3096. LASTTTYLOG=`${catbin} "/tmp/sakis3g.chat.$$.log"`
  3097. debug "Got response from tty:\n%s\n" "${LASTTTYLOG}"
  3098. rm -f "/tmp/sakis3g.chat.$$.log"
  3099. return 0
  3100. else
  3101. debug "No response from tty %s.\n" "$1"
  3102. return 1
  3103. fi
  3104. }
  3105.  
  3106. # Finds capabilities of tty $1 and sets TTY_CAPABILITIES variable
  3107. tty_get_caps() {
  3108. unset TTY_CAPABILITIES
  3109. [ "a$1" = "a" ] && return 1
  3110. ! tty_send_command "$1" "PROBEGSM" && return 1
  3111. gsmfound=`echo "${LASTTTYLOG}" | ${grepbin} "+CGSM" | wc -l`; gsmfound=`echo ${gsmfound}`
  3112. if [ "a${gsmfound}" = "a0" ]; then
  3113. debug "No GSM capabilities were advertised.\n"
  3114. gsmfound=`echo "${LASTTTYLOG}" | ${grepbin} "^\([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\)$" | wc -l` ; gsmfound=`echo ${gsmfound}`
  3115. if [ "a${gsmfound}" = "a1" ]; then
  3116. debug "However, IMEI information was provided. Will consider it GSM capable.\n"
  3117. fi
  3118. fi
  3119. [ "a${gsmfound}" = "a0" -o "a${gsmfound}" = "a" ] && [ "a${NOPROBEGSM}" != "a" ] && debug "Forced by user to consider tty %s GSM capable.\n" "$1" && gsmfound=1
  3120. if [ "a${gsmfound}" != "a0" ]; then
  3121. export TTY_CAPABILITIES="GSM"
  3122. debug "Found GSM capabilities on tty %s.\n" "$1"
  3123. return 0
  3124. fi
  3125. # TODO: Implement support for non GSM devices.
  3126. debug "Device did not report GSM capabilities.\n"
  3127. if [ "a$2" != "anoretry" ]; then
  3128. debug "Will check one more time.\n"
  3129. tty_get_caps "$1" "noretry"
  3130. return $?
  3131. fi
  3132. show_fmt_error "Device did not report GSM capabilities. You can skip this by adding --noprobe command line switch.\n"
  3133. return 1
  3134. }
  3135.  
  3136. # Sets MODEM_VARIANT, if possible, for tty $1
  3137. tty_identify() {
  3138. unset MODEM_VARIANT
  3139. [ "a$1" = "a" ] && return 1
  3140. ! tty_send_command "$1" "IDENTIFY" && return 1
  3141. MODEM_VARIANT=`echo "${LASTTTYLOG}" | ${grepbin} -v "^AT" | ${sedbin} -e "s/AT//g" | ${sedbin} -e "s/OK//g" | ${sedbin} -e "s/ERROR//g" | ${trbin} "\n" " " | ${trbin} "\t" " " | ${sedbin} -e "s/ */ /g" | ${sedbin} -e "s/^ *//g" | ${sedbin} -e "s/ *$//g"`
  3142. MODEM_VARIANT=`echo ${MODEM_VARIANT}`
  3143. if [ "a${MODEM_VARIANT}" = "a" ]; then
  3144. debug "Modem did not report a name.\n"
  3145. unset MODEM_VARIANT
  3146. else
  3147. debug "Modem on tty identified itself as: %s\n" "${MODEM_VARIANT}"
  3148. base_modem "${usbdevice}" "${MODEM_VARIANT}"
  3149. fi
  3150. return 0
  3151. }
  3152.  
  3153. # Checks if tty $1 needs PIN.
  3154. tty_needspin() {
  3155. unset pinrequirement
  3156. [ "a$1" = "a" ] && return 1
  3157. ! tty_send_command "$1" "PINCHECK" && return 1
  3158. pinrequirement=`echo "${LASTTTYLOG}" | ${grepbin} "ERROR"`
  3159. [ "a${pinrequirement}" != "a" ] && pinrequirement="ERROR"
  3160. [ "a${pinrequirement}" = "a" ] && pinrequirement=`echo "${LASTTTYLOG}" | ${grepbin} "^.CPIN: " | ${cutbin} -d\ -f2-`
  3161. [ "a${pinrequirement}" = "a" ] && pinrequirement="ERROR"
  3162. [ "a${pinrequirement}" = "aREADY" ] && debug "Modem on %s does not need PIN.\n" "$1" && return 1
  3163. [ "a${pinrequirement}" = "aSIM PIN" ] && debug "Modem on %s needs PIN.\n" "$1" && return 0
  3164. debug "Got \"%s\" while checking modem on %s for PIN requirement.\n" "${pinrequirement}" "$1"
  3165. return 0
  3166. }
  3167.  
  3168. # Unlocks tty $1 from PIN.
  3169. tty_pin_unlock() {
  3170. [ "a$1" = "a" ] && return 1
  3171. ! tty_needspin "$1" && unset pinrequirement && return 0
  3172. if [ "a${pinrequirement}" = "a" ]; then
  3173. show_fmt_error "Failed to get valid response from modem while checking for PIN."
  3174. unset pinrequirement
  3175. return 1
  3176. elif [ "a${pinrequirement}" != "aSIM PIN" ]; then
  3177. show_fmt_error "Modem responded \"%s\" while checking for PIN." "${pinrequirement}"
  3178. unset pinrequirement
  3179. return 1
  3180. fi
  3181. unset pinrequirement
  3182. if ! pin_valid "${SIM_PIN}"; then
  3183. unset SIM_PIN
  3184. ! pin_prompt && return 98
  3185. ! pin_valid "${SIM_PIN}" && return 1
  3186. fi
  3187. verbose "Sending PIN"
  3188. ! tty_send_command "$1" "PINSUPPLY" "${SIM_PIN}" && return 1
  3189. debug "PIN sent to %s. Waiting 3 seconds before checking success.\n" "$1"
  3190. sleep 3
  3191. if ! tty_needspin "$1"; then
  3192. unset pinrequirement
  3193. debug "SIM is now READY.\n"
  3194. return 0
  3195. fi
  3196. unset SIM_PIN
  3197. stop_fmt_error 12 "WRONG PIN. Aborting to prevent you from locking your SIM card."
  3198. return 1
  3199. }
  3200.  
  3201. tty_detect_apn() {
  3202. unset MODEM_APN
  3203. [ "a${MODEM_TTY}" = "a" ] && return 1
  3204. ! tty_send_command "${MODEM_TTY}" "DETECTAPN" && return 1
  3205. MODEM_APN=`echo "${LASTTTYLOG}" | ${grepbin} "^+CGDCONT" | ${sedbin} -e "s/^\(.*\)IP\",\"\(.*\)\",\"\(.*\)$/\2/g" | ${grepbin} -v "^+CGDCONT"`
  3206. export MODEM_APN
  3207. debug "APN stored in modem was: %s\n" "${MODEM_APN}"
  3208. [ "a${MODEM_APN}" = "a" ] && unset MODEM_APN && return 1
  3209. return 0
  3210. }
  3211.  
  3212. tty_sim_provider_name() {
  3213. unset ISPSIMNAME
  3214. [ "a$1" = "a" ] && return 1
  3215. if tty_send_command "$1" "SIMOPERATORNAME"; then
  3216. ISPSIMNAME=`echo "${LASTTTYLOG}" | ${grepbin} "^+CRSM:\( *\)144," | ${sedbin} -e "s/^.CRSM:\( *\)144,\(.*\),\"\(.*\)\"\(.*\)$/ISPSIMNAME=\3/g" | ${grepbin} "^ISPSIMNAME=" | ${cutbin} -d= -f2-`
  3217. [ "a${ISPSIMNAME}" = "a" ] && unset ISPSIMNAME && return 1
  3218. ISPSIMNAME=`${printfbin} "\`echo "${ISPSIMNAME}" | ${sedbin} -e "s/\([0-9A-Fa-f][0-9A-Fa-f]\)/\\\\\\\\\\x\1/g" | ${sedbin} -e "s/\\\\\\\\\\xFF//g" | ${sedbin} -e "s/\\\\\\\\\\x01//g" | ${sedbin} -e "s/\\\\\\\\\\x02//g"\`\n"`
  3219. ISPSIMNAME=`echo ${ISPSIMNAME}`
  3220. [ "a${ISPSIMNAME}" = "a" ] && unset ISPSIMNAME && return 1
  3221. debug "SIM contains \"service provider\" name: %s\n" "${ISPSIMNAME}"
  3222. export ISPSIMNAME
  3223. return 0
  3224. fi
  3225. return 1
  3226. }
  3227.  
  3228. tty_select_network() {
  3229. unset FORCE_ISP
  3230. verbose "Scanning networks"
  3231. ! tty_send_command "$1" "OPERATORS" && return 1
  3232. networks=`echo "${LASTTTYLOG}" | ${grepbin} "^+COPS:" | ${cutbin} -d: -f2- -s | ${sedbin} -e "s/(/\\\n(/g" | ${sedbin} -e "s/)/)\\\n/g" | ${grepbin} "^([1-3],\"\(.*\)\",\"\(.*\)\",\"\([0-9][0-9]*\)\",0)$" | ${sedbin} -e "s/^(1,/(Allowed,/g" | ${sedbin} -e "s/^(2,/(Current,/g" | ${sedbin} -e "s/^(3,/(Forbidden,/g" | ${sedbin} -e "s/^(\(.*\),\"\(.*\)\",\"\(.*\)\",\"\([0-9][0-9]*\)\",0)$/\"\4\" \"\2 (\1\)\" /g"`
  3233. eval user_select \"FORCE_ISP\" \"Please select a network\" \"Select network for modem to register.\" \"Select\" \"Cancel\" ${networks}
  3234. # in
  3235. case "$?" in
  3236. 0)
  3237. unset FORCE_ISP; unset networks
  3238. return 98
  3239. ;;
  3240. 98)
  3241. unset FORCE_ISP; unset networks
  3242. return 98
  3243. ;;
  3244. 99)
  3245. unset FORCE_ISP; unset networks
  3246. return 99
  3247. ;;
  3248. *)
  3249. case "a${FORCE_ISP}" in
  3250. a)
  3251. unset FORCE_ISP; unset networks
  3252. return 98
  3253. ;;
  3254. *)
  3255. debug "User selected %s.\n" "${FORCE_ISP}"
  3256. return 0
  3257. ;;
  3258. esac
  3259. ;;
  3260. esac
  3261. return 1
  3262. }
  3263.  
  3264. tty_registered_network() {
  3265. unset ISPID; unset ISPNAME; unset ISPTEXT
  3266. [ "a$1" = "a" ] && return 1
  3267. ! tty_send_command "$1" "OPERATOR" && return 1
  3268. ISPID=`echo "${LASTTTYLOG}" | ${grepbin} "^+COPS:" | ${cutbin} -d\" -f2`; ISPID=`echo ${ISPID}`
  3269. if [ "a${ISPID}" = "a+COPS: 0" -o "a${ISPID}" = "a" ]; then
  3270. debug "Modem not registered to a network yet.\n"
  3271. unset ISPID
  3272. return 1
  3273. fi
  3274. debug "Modem registered to network ID \"%s\".\n" "${ISPID}"
  3275. if [ -n "${FORCE_ISP}" ]; then
  3276. if [ "a${ISPID}" != "a${FORCE_ISP}" ]; then
  3277. debug "FORCE_ISP variable instructs to use \"%s\" instead.\n" "${FORCE_ISP}"
  3278. fi
  3279. debug "Will attempt to manually set %s and prevent roaming.\n" "${FORCE_ISP}"
  3280. ! tty_send_command "$1" "SETOPERATOR" "${FORCE_ISP}"
  3281. if [ "a$2" != "aFORCE_ISP" ]; then
  3282. tty_registered_network "$1" "FORCE_ISP"
  3283. else
  3284. if [ "a${ISPID}" != "a" ]; then
  3285. show_fmt_error "Modem refused to register operator \"%s\" (currently registered to \"%s\").\n" "${FORCE_ISP}" "${ISPID}"
  3286. else
  3287. show_fmt_error "Modem refused to register operator \"%s\".\n" "${FORCE_ISP}"
  3288. fi
  3289. unset ISPID
  3290. return 1
  3291. fi
  3292. unset ISPID
  3293. return $?
  3294. fi
  3295. export ISPID
  3296. if tty_send_command "$1" "OPERATORNAME"; then
  3297. ISPNAME=`echo "${LASTTTYLOG}" | ${grepbin} "^+COPS:" | ${sedbin} -e "s/^.COPS: 0,0,\"\(.*\)\"\(.*\)$/ISPNAME=\1/g" | ${grepbin} "^ISPNAME=" | ${cutbin} -d= -f2-`
  3298. if tty_sim_provider_name "$1" && [ "a${ISPSIMNAME}" != "a" -a "a${ISPSIMNAME}" != "a${ISPNAME}" ]; then
  3299. debug "SIM card instructs to use \"%s\" as service provider name, instead of \"%s\".\n" "${ISPSIMNAME}" "${ISPNAME}"
  3300. ISPNAME="${ISPSIMNAME}"
  3301. fi
  3302. # Some modems return numeric ID, even if instructed to display name.
  3303. [ "a${ISPNAME}" = "a${ISPID}" ] && unset ISPNAME
  3304. # Some SIMs incorrectly confuse modem to display FFs as service provider name.
  3305. [ "a`echo ${ISPNAME} | ${cutbin} -b1-8`" = "aFFFFFFFF" ] && unset ISPNAME
  3306. # Huawei E160 does not correctly report operator name.
  3307. # This is not true. Was a SIM issue. This is not true.
  3308. #[ "a${MODEM_VARIANT}" = "aE160" ] && unset ISPNAME
  3309. export ISPNAME
  3310. [ "a${ISPNAME}" = "a" ] && net_info "${ISPID}"
  3311. [ "a${ISPNAME}" = "a" ] && unset ISPNAME
  3312. fi
  3313. export ISPTEXT="${ISPID}"
  3314. [ "a${ISPNAME}" != "a" ] && export ISPTEXT="${ISPNAME}"
  3315. return 0
  3316. }
  3317.  
  3318. tty_register_network() {
  3319. [ "a$1" = "a" ] && return 1
  3320. wat=0
  3321. if [ -n "${FORCE_ISP}" ]; then
  3322. debug "FORCE_ISP variable instructs to enter network \"%s\".\n" "${FORCE_ISP}"
  3323. debug "Will attempt to manually set %s and prevent roaming.\n" "${FORCE_ISP}"
  3324. ! tty_send_command "$1" "SETOPERATOR" "${FORCE_ISP}"
  3325. fi
  3326. while ! tty_registered_network $1;
  3327. do
  3328. if [ "${wat}" -gt "20" ]; then
  3329. debug "Giving up after %d seconds have passed.\n" "${wat}"
  3330. break
  3331. else
  3332. wat=`expr ${wat} + 4`
  3333. wat=`echo ${wat}`
  3334. fi
  3335. debug "Waiting modem to register network (%d seconds).\n" "${wat}"
  3336. verbose "Registering network"
  3337. sleep 4
  3338. done
  3339. unset wat
  3340. if [ "a${ISPID}" = "a" ]; then
  3341. show_fmt_error "Modem unable to register a network."
  3342. unset ISPID
  3343. if user_confirm "scan" "Scan for network" "Modem was unable to register a network. Would you like to manually select a network?" "Yes" "No" "reset"; then
  3344. if tty_select_network "$@"; then
  3345. tty_register_network "$@"
  3346. return $?
  3347. fi
  3348. fi
  3349. return 13
  3350. else
  3351. debug "Modem on %s has registered %s.\n" "$1" "${ISPID}"
  3352. return 0
  3353. fi
  3354. }
  3355.  
  3356. # Makes sure tty $1 is ready for connection to be established.
  3357. tty_prepare() {
  3358. check_com_software
  3359. ret=$?; [ "${ret}" -ne "0" ] && return ${ret}
  3360. [ "a$1" = "a" ] && return 1
  3361. verbose "Preparing modem"
  3362. tty_send_command "$1" "STAGE0"
  3363. ! tty_get_caps "$@" && return 1
  3364. ! tty_identify "$@" && return 1
  3365. tty_send_command "$1" "STAGE1"
  3366. ! tty_pin_unlock "$@" && return 1
  3367. tty_send_command "$1" "STAGE4"
  3368. tty_register_network "$@"; ret=$?; [ "${ret}" -ne "0" ] && return ${ret}
  3369. tty_send_command "$1" "STAGE5"
  3370. hal_tty_update "$@"
  3371. debug "Modem on device node %s is now ready for connection.\n" "$1"
  3372. return 0
  3373. }
  3374.  
  3375. # Attempts to get connected USB devices, exiting if it fails.
  3376. usb_connected_devices() {
  3377. unset usb_devices
  3378. need_binary "sort"; need_binary "uniq"; need_binary "sed"
  3379. if [ -r "/proc/bus/usb/devices" ]; then
  3380. debug "Fetching connected USB devices by using \"%s\".\n" "/proc/bus/usb/devices"
  3381. usb_devices=`safe_cat "/proc/bus/usb/devices" | ${grepbin} -A 4 "^P:" | ${sedbin} -e "s/^\(.*\)Ven\(.*\)=\(....\) Pro\(.*\)=\(....\) \(.*\)$/\3:\5:/g" | ${sedbin} -e "s/^S:\(.*\)Pro\(.*\)=\(.*\)$/\3/g" | ${sedbin} -e "s/^.:\(.*\)$//g" | ${grepbin} -v "^$"`
  3382. usb_devices=`echo ${usb_devices} | ${sedbin} -e "s/: */:/g" | ${sedbin} -e "s/ -- /\\\n/g" | ${sortbin} | ${uniqbin} | ${grepbin} -v "HCI Host Controller"`
  3383. elif [ -d "/sys/bus/usb/devices" ]; then
  3384. debug "Fetching connected USB devices by using \"%s\".\n" "/sys/bus/usb/devices"
  3385. usb_devices=`cd /sys/bus/usb/devices/; ${grepbin} . */idProduct */idVendor */product */uevent | ${sortbin} | ${sedbin} -e "s/idProduct:\(.*\)$/idProduct:\1:/g" | ${sedbin} -e "s/idVendor:\(.*\)$/idVendor:\1:/g" | ${grepbin} -A 2 "idProduct" | ${sedbin} -e "s/^\(.*\):\(..*\):*$/\2/g"`
  3386. usb_devices=`echo ${usb_devices} | ${sedbin} -e "s/: */:/g" | ${sedbin} -e "s/ -- /\\\n/g" | ${sortbin} | ${uniqbin} | ${sedbin} -e "s/^\(....\):\(....\):/\2:\1:/g" | ${sortbin} | ${uniqbin} | ${grepbin} -v "HCI Host Controller"`
  3387. elif find_binary "lsusb"; then
  3388. debug "Fetching connected USB devices by using \"%s\".\n" "${lsusbbin}"
  3389. usb_devices=`safe_lsusb | ${sedbin} -e "s/\(.*\)ID \(....\):\(....\) \(.*\)$/\2:\3:\4/g" | ${sortbin} | ${uniqbin} | ${grepbin} -v "root hub"`
  3390. else
  3391. stop_fmt_error 7 "Unable to discover connected USB devices. Script will now abort."
  3392. fi
  3393. if [ "a${TIMEOUTOCCURED}" != "a" ]; then
  3394. show_fmt_error "Seems like an electrical/USB bus error occured. You may need to replug your modem for it to properly work.\n"
  3395. unset TIMEOUTOCCURED
  3396. fi
  3397. debug "Connected USB devices are:\n%s\n" "${usb_devices}"
  3398. return 0
  3399. }
  3400.  
  3401. # Helper method. Prints equivalent of usb_devices only for those devices in $1
  3402. usb_device_list() {
  3403. for condevice in $@
  3404. do
  3405. echo "${usb_devices}" | ${grepbin} "^${condevice}:"
  3406. done
  3407. }
  3408.  
  3409. # Does scoring things to determine if which usb device looks like a sure candidate for being a modem.
  3410. # On success, returns 0 and sets usb_modems.
  3411. # On failure, returns 1
  3412. voodoo_connected_modems() {
  3413. unset usb_modems
  3414. need_binary "uname"
  3415. debug "Voodoo mode employed to discover a possible modem.\n"
  3416. debug "Attempting to enumerate interfaces.\n"
  3417. uevents=`cd /sys/bus/usb/devices; ${grepbin} . [0-9]*/uevent 2> /dev/null`
  3418. interfaces=`echo "${uevents}" | ${cutbin} -d/ -f1 -s | ${grepbin} ":" | ${sortbin} | ${uniqbin}` ; interfaces=`echo ${interfaces}`
  3419. debug "uevent contents are:\n%s\n" "${uevents}"
  3420. debug "Interfaces are:\n%s\n" "${interfaces}"
  3421. unset orphaninterfaces; unset storageinterfaces; unset communicationdevices; unset suspectdevices; unset serialinterfaces; unset interruptinterfaces
  3422. for intstr in ${interfaces}
  3423. do
  3424. intclass=`echo "${uevents}" | ${grepbin} "^${intstr}\/" | ${grepbin} "uevent:INTERFACE=" | ${cutbin} -d= -f2 -s | ${cutbin} -d/ -f1`
  3425. [ "a${intclass}" = "a9" ] && continue
  3426. intdriver=`echo "${uevents}" | ${grepbin} "^${intstr}\/" | ${grepbin} "uevent:DRIVER=" | ${cutbin} -d= -f2 -s | ${sedbin} -e "s/-/_/g"`
  3427. intserial=0; [ "a${intdriver}" != "a" ] && intserial=`usb_serial_drivers ${intdriver} 2> /dev/null | ${wcbin} -w`; intserial=`echo ${intserial}`
  3428. intpoint=`cd /sys/bus/usb/devices; ${grepbin} . ${intstr}/ep_*/type 2> /dev/null | ${grepbin} -i "interrupt$" | ${wcbin} -l`; intpoint=`echo ${intpoint}`
  3429. intdevice=`echo "${intstr}" | ${cutbin} -d: -f1 -s`
  3430. intvendor=`${catbin} /sys/bus/usb/devices/${intdevice}/idVendor 2> /dev/null`
  3431. intmodemmaker=`${grepbin} -i "usb:v${intvendor}" /lib/modules/\`${unamebin} -r\`/modules.alias 2> /dev/null | ${cutbin} -d\ -f3 -s | ${sortbin} | ${uniqbin}`
  3432. intmodemmaker=`usb_serial_drivers ${intmodemmaker} 2> /dev/null`
  3433. intbrothers=`echo "${interfaces}" | ${trbin} " " "\n" | ${sedbin} -e "s/^\( *\)//g" | ${grepbin} "^${intdevice}:" | ${wcbin} -l`; intbrothers=`echo ${intbrothers}`
  3434. if [ "a${intdriver}" = "a" ]; then
  3435. orphaninterfaces="${orphaninterfaces} ${intstr} ${intstr} ${intstr}"
  3436. debug "Added interface %s to orphan interfaces.\n" "${intstr}"
  3437. fi
  3438. if [ "a${intpoint}" = "a1" ]; then
  3439. interruptinterfaces="${interruptinterfaces} ${intstr} ${intstr} ${intstr}"
  3440. debug "Added interface %s to interrupt interfaces.\n" "${intstr}"
  3441. fi
  3442. if [ "a${intdriver}" = "ausb-storage" -o "a${intdriver}" = "ausb_storage" -o "a${intclass}" = "a8" ]; then
  3443. storageinterfaces="${storageinterfaces} ${intstr} ${intstr} ${intstr} ${intstr}"
  3444. debug "Added interface %s to storage interfaces.\n" "${intstr}"
  3445. if [ "a${intbrothers}" = "a1" ]; then
  3446. storageinterfaces="${storageinterfaces} ${intstr} ${intstr}"
  3447. debug "+2 for being unique interface of device %s.\n" "${intdevice}"
  3448. fi
  3449. if [ "a${intbrothers}" = "a1" ] && [ "a${intmodemmaker}" != "a" ]; then
  3450. storageinterfaces="${storageinterfaces} ${intstr} ${intstr} ${intstr}"
  3451. debug "+3 for vendor %s being a modem maker.\n" "${intvendor}"
  3452. fi
  3453. fi
  3454. if [ "a${intclass}" = "a2" ]; then
  3455. communicationdevices="${communicationdevices} ${intstr} ${intstr}"
  3456. debug "Added interface %s to communication interfaces.\n" "${intstr}"
  3457. fi
  3458. if [ "a${intclass}" = "a255" -o "a${intclass}" = "a224" ]; then
  3459. suspectdevices="${suspectdevices} ${intstr}"
  3460. debug "Added interface %s to suspect interfaces.\n" "${intstr}"
  3461. fi
  3462. if [ "a${intserial}" = "a1" ]; then
  3463. serialinterfaces="${serialinterfaces} ${intstr} ${intstr} ${intstr} ${intstr} ${intstr}"
  3464. debug "Added interface %s to serial interfaces.\n" "${intstr}"
  3465. fi
  3466. done
  3467. unset interfaces; unset uevents
  3468. unset intstr; unset intclass; unset intdriver; unset intserial; unset intpoint; unset intdevice; unset intvendor; unset intmodemmaker; unset intbrothers
  3469. devicescore=`echo ${serialinterfaces} ${suspectdevices} ${communicationdevices} ${storageinterfaces} ${orphaninterfaces} ${interruptinterfaces} | ${trbin} " " "\n" | ${sedbin} -e "s/ //g" | ${grepbin} -v "^$" | ${cutbin} -d: -f1 -s | ${sortbin} | ${uniqbin} -c | ${sedbin} -e "s/^\( *\)//g" | ${sedbin} -e "s/\( *\)/ /g" | ${sedbin} -e "s/^\([0-9]\) /0\1 /g" | ${sedbin} -e "s/^\([0-9][0-9]\) /0\1 /g" | ${sedbin} -e "s/^\([0-9][0-9][0-9]\) /0\1 /g" | ${sortbin} -r`
  3470. interfacescore=`echo ${serialinterfaces} ${suspectdevices} ${communicationdevices} ${storageinterfaces} ${orphaninterfaces} ${interruptinterfaces} | ${trbin} " " "\n" | ${sedbin} -e "s/ //g" | ${grepbin} -v "^$" | ${sortbin} | ${uniqbin} -c | ${sedbin} -e "s/^\( *\)//g" | ${sedbin} -e "s/\( *\)/ /g" | ${sedbin} -e "s/^\([0-9]\) /0\1 /g" | ${sedbin} -e "s/^\([0-9][0-9]\) /0\1 /g" | ${sedbin} -e "s/^\([0-9][0-9][0-9]\) /0\1 /g" | ${sortbin} -r`
  3471. unset serialinterfaces; unset suspectdevices; unset communicationdevices; unset storageinterfaces; unset orphaninterfaces; unset interruptinterfaces;
  3472. debug "Device score is:\n%s\n" "${devicescore}"
  3473. debug "Interface score is:\n%s\n" "${interfacescore}"
  3474. besttwo=`echo "${interfacescore}" | ${headbin} -2 | ${cutbin} -d\ -f2 -s | ${cutbin} -d: -f1 -s | ${uniqbin}`
  3475. besttwocount=`echo "${besttwo}" | ${wcbin} -w`; besttwocount=`echo ${besttwocount}`
  3476. if [ "a${besttwocount}" != "a" ] && [ "${besttwocount}" -eq "1" ]; then
  3477. intvendor=`${catbin} /sys/bus/usb/devices/${besttwo}/idVendor 2> /dev/null`
  3478. intproduct=`${catbin} /sys/bus/usb/devices/${besttwo}/idProduct 2> /dev/null`
  3479. if usb_device_connected "${intvendor}:${intproduct}"; then
  3480. export usb_modems="${intvendor}:${intproduct}"
  3481. unset intvendor; unset intproduct
  3482. debug "Voodoo operations determined device %s (%s) is a modem.\n" "${usb_modems}" "${besttwo}"
  3483. unset besttwocount; unset besttwo; unset interfacescore; unset devicescore
  3484. return 0
  3485. fi
  3486. unset intvendor; unset intproduct
  3487. fi
  3488. debug "No device is the absolute winner. Will check for dominating score.\n"
  3489. firstscore=`echo "${interfacescore}" | ${headbin} -1 | ${cutbin} -d\ -f1 -s | ${sedbin} -e "s/^\(0*\)//g" | ${sedbin} -e "s/^$/0/g"`
  3490. firstscore=`${printfbin} "%d\n" "${firstscore}" 2> /dev/null`; [ "a${firstscore}" = "a" ] && firstscore=0
  3491. secondscore=`echo "${interfacescore}" | ${headbin} -2 | ${tailbin} -1 | ${cutbin} -d\ -f1 -s | ${sedbin} -e "s/^\(0*\)//g" | ${sedbin} -e "s/^$/0/g"`
  3492. secondscore=`${printfbin} "%d\n" "${secondscore}" 2> /dev/null`; [ "a${secondscore}" = "a" ] && secondscore=0
  3493. if [ "a${firstscore}" != "a" -a "a${secondscore}" != "a" ] && [ "a${firstscore}" != "a${secondscore}" ] && [ "${firstscore}" -gt "0" -a "${firstscore}" -gt "${secondscore}" ]; then
  3494. secondscore=`expr ${secondscore} + ${secondscore}`; secondscore=`echo ${secondscore}`
  3495. if [ "${firstscore}" -gt "${secondscore}" ]; then
  3496. besttwo=`echo "${interfacescore}" | ${headbin} -1 | ${cutbin} -d\ -f2 -s | ${cutbin} -d: -f1 -s | ${uniqbin}`
  3497. intvendor=`${catbin} /sys/bus/usb/devices/${besttwo}/idVendor 2> /dev/null`
  3498. intproduct=`${catbin} /sys/bus/usb/devices/${besttwo}/idProduct 2> /dev/null`
  3499. if usb_device_connected "${intvendor}:${intproduct}"; then
  3500. export usb_modems="${intvendor}:${intproduct}"
  3501. unset intvendor; unset intproduct
  3502. debug "Device %s (%s) out-numbered second candidate and will be considered a modem.\n" "${usb_modems}" "${besttwo}"
  3503. unset firstscore; unset secondscore
  3504. unset besttwocount; unset besttwo; unset interfacescore; unset devicescore
  3505. return 0
  3506. fi
  3507. fi
  3508. fi
  3509. unset firstscore; unset secondscore
  3510. debug "No device dominates the other in score. Cannot decide.\n"
  3511. unset besttwo; unset besttwocount; unset interfacescore; unset devicescore
  3512. return 1
  3513. }
  3514.  
  3515. # Attempts to get connected modems, and sets usb_modems and usb_modem_devices variables
  3516. usb_connected_modems() {
  3517. unset usb_modems; unset usb_modem_devices
  3518. [ "a${KNOWN_devices}" = "a" ] && ! modeswitch_load && return 1
  3519. ! usb_connected_devices && return 1
  3520. connectedevices=`echo "${usb_devices}" | ${cutbin} -d: -f1,2`
  3521. for condevice in ${connectedevices}
  3522. do
  3523. devfound=`${printfbin} "%s\n%s\n%s\n" "${KNOWN_devices}" "${MODEM}" "${USBMODEM}" | ${grepbin} "${condevice}"`
  3524. [ "a${devfound}" != "a" ] && usb_modems="${usb_modems} ${condevice}"
  3525. unset devfound
  3526. done
  3527. unset condevice; unset connectedevices
  3528. usb_modems=`echo ${usb_modems}`
  3529. export usb_modems
  3530. if [ "a${usb_modems}" = "a" ] && unset usb_modems; then
  3531. debug "No plugged modems found.\n"
  3532. ! voodoo_mode && return 1
  3533. ! voodoo_connected_modems && return 1
  3534. fi
  3535. usb_modem_devices=`usb_device_list ${usb_modems}`
  3536. export usb_modem_devices
  3537. debug "Connected USB modems are:\n%s\n" "${usb_modem_devices}"
  3538. return 0
  3539. }
  3540.  
  3541. # Returns true if device $1 is currently connected
  3542. usb_device_connected() {
  3543. [ "a$1" = "a" ] && return 99
  3544. usb_connected_devices
  3545. usbconnected=`echo "${usb_devices}" | ${cutbin} -d: -f1,2 | ${grepbin} "^$1"`
  3546. [ "a${usbconnected}" = "a$1" ] && unset usbconnected && return 0
  3547. unset usbconnected
  3548. debug "Device \"%s\" is not connected.\n" "$1"
  3549. return 1
  3550. }
  3551.  
  3552. # Parses /proc/bus/usb/devices and only returns block referring to $1.
  3553. usb_device_block() {
  3554. unset usbdeviceblock
  3555. [ "a$1" = "a" ] && return 1
  3556. if [ -r "/proc/bus/usb/devices" ]; then
  3557. usbvend=`echo "$1" | ${cutbin} -d: -f1`
  3558. usbprod=`echo "$1" | ${cutbin} -d: -f2`
  3559. usbdevcat=`safe_cat /proc/bus/usb/devices`
  3560. usblines=`${printfbin} "%s\n\n" "${usbdevcat}" | ${grepbin} -A 60 "=${usbvend} \(.*\)=${usbprod}" | ${grepbin} -n "^$" | ${headbin} -1 | ${sedbin} -e "s/:$//g"`; usblines=`echo ${usblines}`
  3561. [ "a${usblines}" = "a" ] && usblines=0
  3562. [ "${usblines}" -gt "0" ] && usbdeviceblock=`echo "${usbdevcat}" | ${grepbin} -A 60 "Vendor=${usbvend} ProdID=${usbprod}" | ${headbin} -${usblines}`
  3563. unset usblines; unset usbvend; unset usbprod; unset usbdevcat
  3564. echo "${usbdeviceblock}" | ${grepbin} -v "^$"
  3565. return 0
  3566. fi
  3567. return 1
  3568. }
  3569.  
  3570. # Filters arguments and returns back only those that usb serial drivers
  3571. usb_serial_drivers() {
  3572. [ "a${kernel}" = "a" ] && find_binary "uname" && kernel=`${unamebin} -r`
  3573. [ "a${kernel}" = "a" ] && return 1
  3574. export kernel
  3575. for driver in $@
  3576. do
  3577. [ -f "/lib/modules/${kernel}/kernel/drivers/usb/serial/${driver}.ko" ] && echo "${driver}" && continue
  3578. [ "a${driver}" = "acdc_acm" ] && [ -f "/lib/modules/${kernel}/kernel/drivers/usb/class/cdc-acm.ko" ] && echo "${driver}"
  3579. done
  3580. }
  3581.  
  3582. # Attempts to find appropriate driver for USB device $1, and sets USBDRIVER
  3583. usb_serial_detect_driver() {
  3584. unset USBDRIVER
  3585. [ "a$1" = "a" ] && return 99
  3586. serialvend=`echo "$1" | ${cutbin} -d: -f1`
  3587. serialprod=`echo "$1" | ${cutbin} -d: -f2`
  3588. if ! find_binary "uname"; then
  3589. debug "Failed to locate running kernel's modules. Uname missing.\n"
  3590. else
  3591. kernel=`${unamebin} -r`
  3592. if [ -f "/lib/modules/${kernel}/modules.alias" ]; then
  3593. drivercandidates=`${grepbin} -i "usb:v${serialvend}p${serialprod}" "/lib/modules/${kernel}/modules.alias" | ${cutbin} -d\ -f3 | ${sortbin} | ${uniqbin}`;
  3594. drivercandidates=`usb_serial_drivers ${drivercandidates}`
  3595. if [ "a${drivercandidates}" = "a" ]; then
  3596. debug "No perfect match found for device \"%s\".\n" "$1"
  3597. debug "Will seek if exists a driver for other devices from vendor \"%s\".\n" "${serialvend}"
  3598. drivercandidates=`${grepbin} -i "usb:v${serialvend}p" "/lib/modules/${kernel}/modules.alias" | ${cutbin} -d\ -f3 | ${sortbin} | ${uniqbin}`;
  3599. drivercandidates=`usb_serial_drivers ${drivercandidates}`
  3600. if [ "a${drivercandidates}" = "a" ]; then
  3601. debug "No candidates were found.\n"
  3602. else
  3603. debug "Found driver(s) for other devices of same vendor: %s\n" "${drivercandidates}"
  3604. fi
  3605. drivercandidates=`echo "${drivercandidates}" | ${trbin} " " "\n" | ${sedbin} -e "s/ //g" | ${sortbin} | ${uniqbin}`
  3606. else
  3607. debug "Found driver(s) available for \"%s\": %s\n" "$1" "${drivercandidates}"
  3608. fi
  3609. fi
  3610. unset kernel
  3611. fi
  3612. if base_drivers "${serialvend}" "${serialprod}"; then
  3613. drivercandidates=`echo ${drivercandidates} ${basedrivercandidates}`
  3614. unset basedrivercandidates
  3615. fi
  3616. drivercandidates=`echo ${drivercandidates} | ${trbin} " " "\n" | ${sedbin} -e "s/ //g" | ${sortbin} | ${uniqbin}`
  3617. candidatenum=`echo ${drivercandidates} | ${wcbin} -w`; candidatenum=`echo ${candidatenum}`
  3618. if [ "a${candidatenum}" = "a1" ]; then
  3619. USBDRIVER=`echo ${drivercandidates}`
  3620. export USBDRIVER
  3621. debug "Only one candidate, will use it unconditionally: %s\n" "${USBDRIVER}"
  3622. unset candidatenum; unset drivercandidates; unset serialvend; unset serialprod
  3623. return 0
  3624. elif [ "a${candidatenum}" = "a0" ]; then
  3625. drivercandidates="option"
  3626. debug "No driver found, will propose \"%s\" driver.\n" "${drivercandidates}"
  3627. if usb_usable_interfaces "$1" && [ "a${usableinterfacecount}" != "a" ] && [ "${usableinterfacecount}" -gt "1" ]; then
  3628. debug "Device has %d usable interfaces. Will propose cdc_acm also.\n"
  3629. drivercandidates="cdc_acm option"
  3630. fi
  3631. fi
  3632. debug "Driver candidates are: %s\n" "${drivercandidates}"
  3633. if voodoo_mode; then
  3634. for safetoprobe in cdc_acm
  3635. do
  3636. debug "Will try safe to probe driver %s.\n" "${safetoprobe}"
  3637. if module_bind "${safetoprobe}" "$1"; then
  3638. debug "Appropriate driver %s determined.\n" "${safetoprobe}"
  3639. export USBDRIVER="${safetoprobe}"
  3640. unset candidatenum; unset drivercandidates; unset serialvend; unset serialprod
  3641. return 0
  3642. else
  3643. debug "Determined %s is not a valid candidate. Will make sure is not within list of candidates either.\n" "${safetoprobe}"
  3644. drivercandidates=`echo ${drivercandidates} | ${sedbin} -e "s/\( *\)${safetoprobe}\( *\)/ /g" | ${sedbin} -e "s/ / /g"`
  3645. drivercandidates=`echo ${drivercandidates}`
  3646. debug "Driver candidates are: %s\n" "${drivercandidates}"
  3647. fi
  3648. done
  3649. fi
  3650. drivers=`echo ${drivercandidates} | ${trbin} " " "\n" | ${sedbin} -e "s/^\( *\)\(.*\)\( *\)$/\2/g" | ${sortbin} | ${uniqbin} | ${sedbin} -e "s/^\(.*\)$/\"\1\" \"\1 kernel module\"/g"`
  3651. eval user_select \"USBDRIVER\" \"Please select appropriate driver\" \"Select kernel module that should be used.\" \"Select\" \"Cancel\" ${drivers} \"OTHER\" \"Other driver...\"
  3652. case "$?" in
  3653. 0)
  3654. unset drivers
  3655. unset candidatenum; unset drivercandidates; unset serialvend; unset serialprod
  3656. return 98
  3657. ;;
  3658. 98)
  3659. unset drivers
  3660. unset candidatenum; unset drivercandidates; unset serialvend; unset serialprod
  3661. return 98
  3662. ;;
  3663. 99)
  3664. unset drivers
  3665. unset candidatenum; unset drivercandidates; unset serialvend; unset serialprod
  3666. return 99
  3667. ;;
  3668. *)
  3669. case "a${USBDRIVER}" in
  3670. aOTHER)
  3671. unset USBDRIVER
  3672. user_prompt "USBDRIVER" "Please enter name of driver" "Enter name of appropriate kernel module that should be used, or leave empty to abort" "OK" "Cancel"
  3673. if [ "a${USBDRIVER}" != "a" ]; then
  3674. unset drivers
  3675. unset candidatenum; unset drivercandidates; unset serialvend; unset serialprod
  3676. return 0
  3677. fi
  3678. usb_serial_detect_driver "$@"
  3679. return $?
  3680. ;;
  3681. *)
  3682. unset drivers
  3683. unset candidatenum; unset drivercandidates; unset serialvend; unset serialprod
  3684. return 0
  3685. ;;
  3686. esac
  3687. ;;
  3688. esac
  3689. return 99
  3690. }
  3691.  
  3692. # Returns driver attached to interface $2 of device $1
  3693. usb_loaded_driver() {
  3694. [ "a$1" = "a" ] && return 99
  3695. if [ -r "/proc/bus/usb/devices" ]; then
  3696. usbblock=`usb_device_block "$1"`; [ "a${usbblock}" = "a" ] && return 7
  3697. if [ "a$2" != "a" ]; then
  3698. usbdriver=`echo "${usbblock}" | ${grepbin} "^I:\(.*\)If.=\( *\)$2 " | ${sedbin} -e "s/^I:\(.*\)If.=\( *\)$2 \(.*\) Driver=\(.*\)/\4/g"`
  3699. else
  3700. usbdriver=`echo "${usbblock}" | ${grepbin} "^I:\(.*\)If.=" | ${sedbin} -e "s/^I:\(.*\)If.=\( *\) \(.*\) Driver=\(.*\)/\4/g"`
  3701. fi
  3702. case "a${usbdriver}" in
  3703. a)
  3704. usbdriver="FAIL"
  3705. echo ${usbdriver}
  3706. unset usbdriver
  3707. return 1
  3708. ;;
  3709. "a(none)")
  3710. usbdriver="NONE"
  3711. echo ${usbdriver}
  3712. unset usbdriver
  3713. return 0
  3714. ;;
  3715. *)
  3716. usbdriver=`echo ${usbdriver} | ${trbin} "-" "_"`
  3717. usbdriver=`echo ${usbdriver} | ${sedbin} -e "s/usbserial_generic/usbserial/g"`
  3718. echo ${usbdriver}
  3719. unset usbdriver
  3720. return 1
  3721. esac
  3722. elif usb_sysfsdir "$1"; then
  3723. if [ "a$2" != "a" ]; then
  3724. usbdriver=`${grepbin} . ${sysfsloc}/*-*\:*.$2/uevent | ${grepbin} -i DRIVER= | ${cutbin} -d= -f2 -s`
  3725. else
  3726. usbdriver=`${grepbin} . ${sysfsloc}/*-*\:*.*/uevent | ${grepbin} -i DRIVER= | ${cutbin} -d= -f2 -s`; usbdriver=`echo ${usbdriver}`
  3727. fi
  3728. usbdriver=`echo ${usbdriver} | ${trbin} "-" "_"`
  3729. usbdriver=`echo ${usbdriver} | ${sedbin} -e "s/usbserial_generic/usbserial/g"`
  3730. [ "a${usbdriver}" = "a" ] && usbdriver="NONE"
  3731. echo ${usbdriver}
  3732. unset usbdriver
  3733. return 0
  3734. else
  3735. debug "Unable to determine driver attached to interface %s of device %s.\n" "$2" "$1"
  3736. usbdriver="FAIL"
  3737. echo ${usbdriver}
  3738. unset usbdriver
  3739. return 1
  3740. fi
  3741. }
  3742.  
  3743. usb_usable_interfaces() {
  3744. unset usableinterfaces; usableinterfacecount=0
  3745. [ "a$1" = "a" ] && return 99
  3746. if [ -r "/proc/bus/usb/devices" ]; then
  3747. debug "Using information of %s to determine usable interfaces.\n" "/proc/bus/usb/devices"
  3748. usbblock=`usb_device_block "$1"`
  3749. usbinter=`echo "${usbblock}" | ${grepbin} "^I:" | ${wcbin} -l`; usbinter=`echo ${usbinter}`
  3750. debug "USB Device %s provides %d interface(s).\n" "$1" "${usbinter}"
  3751. usbinter=`echo "${usbblock}" | ${grepbin} "(I)\(.*\)Atr=...Int\.. " | ${wcbin} -l`; usbinter=`echo ${usbinter}`
  3752. debug "USB Device %s provides %d interruptable endpoint(s).\n" "$1" "${usbinter}"
  3753. if [ "a${usbinter}" = "a0" -o "a${usbinter}" = "a" ]; then
  3754. debug "USB device block output was:\n%s\n" "${usbblock}"
  3755. debug "No modem lines on USB device \"%s\". Device may need switching.\n" "$1"
  3756. else
  3757. usbinter=`echo "${usbblock}" | ${grepbin} "(I)\(.*\)Atr=...Int\.. " | ${sedbin} -e "s/^E:\(.*\)Ad=\(.*\)(I)\(.*\)Atr=...Int\..\(.*\)$/\2/g"`; usbinter=`echo ${usbinter}`
  3758. debug "Interruptable endpoint(s) are: %s\n" "${usbinter}"
  3759. unset USB_INTERFACE
  3760. for endpoint in ${usbinter}
  3761. do
  3762. localinter=`echo "${usbblock}" | ${grepbin} -B 10 "^E\(.*\)Ad=${endpoint}(I)" | ${grepbin} "^I" | ${tailbin} -1 | ${sedbin} -e "s/^\(.*\)If.=\( *\)\([0-9][0-9]*\) \(.*\)$/\3/g"`
  3763. if [ "a${localinter}" != "a" ]; then
  3764. localinter=`${printfbin} "%d\n" ${localinter} 2> /dev/null`
  3765. if [ "a${localinter}" != "a" ]; then
  3766. debug "Added interface %s, containing endpoint %s.\n" "${localinter}" "${endpoint}"
  3767. usableinterfaces="${usableinterfaces} ${localinter}"
  3768. fi
  3769. fi
  3770. unset localinter
  3771. done
  3772. unset localinter; unset endpoint
  3773. fi
  3774. unset usbinter; unset usbblock
  3775. usableinterfaces=`echo ${usableinterfaces}`
  3776. export usableinterfaces
  3777. debug "Interfaces collected from %s are: %s\n" "/proc/bus/usb/devices" "${usableinterfaces}"
  3778. fi
  3779. if find_binary "lsusb"; then
  3780. debug "Using information of %s to determine usable interfaces.\n" "lsusb"
  3781. usbvend=`echo "$1" | ${cutbin} -d: -f1`; usbprod=`echo "$1" | ${cutbin} -d: -f2`
  3782. usbblock=`safe_lsusb -v -d ${usbvend}:${usbprod}`
  3783. usbinter=`echo "${usbblock}" | ${sedbin} -e "s/ */ /g" | ${grepbin} -i "transfer type interrupt" | ${wcbin} -l`; usbinter=`echo ${usbinter}`
  3784. debug "USB Device %s provides %d interruptable endpoint(s).\n" "$1" "${usbinter}"
  3785. if [ "a${usbinter}" = "a0" -o "a${usbinter}" = "a" ]; then
  3786. debug "lsusb output was:\n%s\n" "${usbblock}"
  3787. debug "No modem lines on USB device \"%s\". Device may need switching.\n" "$1"
  3788. unset usbinter
  3789. else
  3790. # Enter character after "interrupt" word on following line IS NOT a mistake
  3791. usbinter=`echo "${usbblock}" | ${sedbin} -e "s/ */ /g" | ${grepbin} -F -i "transfer type interrupt
  3792. binterfacenumber" | ${grepbin} -B 1 -i interrupt | ${grepbin} -i "binterfacenumber" | ${cutbin} -d\ -f3 -s`; usbinter=`echo ${usbinter}`
  3793. fi
  3794. debug "Interfaces collected from %s are: %s\n" "lsusb" "${usbinter}"
  3795. [ "a${usbinter}" != "a" ] && usableinterfaces=`echo "${usableinterfaces} ${usbinter}"`
  3796. export usableinterfaces
  3797. unset usbvend; unset usbprod; unset usbblock; unset usbinter;
  3798. elif [ ! -r "/proc/bus/usb/devices" ]; then
  3799. debug "Failed to read USB information.\n"
  3800. show_fmt_error "Both \"%s\" and \"%s\" are missing." "lsusb" "/proc/bus/usb/devices"
  3801. return 7
  3802. fi
  3803. if [ "a${TIMEOUTOCCURED}" != "a" ]; then
  3804. show_fmt_error "Seems like an electrical/USB bus error occured. You may need to replug your modem for it to properly work.\n"
  3805. unset TIMEOUTOCCURED
  3806. fi
  3807. usableinterfaces=`echo ${usableinterfaces} | ${trbin} " " "\n" | ${sedbin} -e "s/ //g" | ${sortbin} | ${uniqbin}`
  3808. usableinterfaces=`echo ${usableinterfaces}`
  3809. usableinterfacecount=`echo ${usableinterfaces} | ${wcbin} -w`; usableinterfacecount=`echo ${usableinterfacecount}`
  3810. return 0
  3811. }
  3812.  
  3813. usb_get_interface() {
  3814. unset USB_INTERFACE
  3815. [ "a$1" = "a" ] && return 99
  3816. usb_usable_interfaces "$1"
  3817. ret=$?; [ "a${ret}" != "a0" ] && return ${ret}
  3818. USB_INTERFACE=`echo ${usableinterfaces}`
  3819. export USB_INTERFACE
  3820. if [ "a${USB_INTERFACE}" = "a" -o "a${usableinterfacecount}" = "a0" ]; then
  3821. debug "Failed to locate any usable interface for device %s.\n" "$1"
  3822. unset USB_INTERFACE
  3823. return 8
  3824. fi
  3825. debug "Collected %d interface(s): %s\n" "${usableinterfacecount}" "${USB_INTERFACE}"
  3826. if [ "a${usableinterfacecount}" = "a1" ]; then
  3827. debug "Only one interface available. Will use that one unconditionally.\n"
  3828. return 0
  3829. fi
  3830. usbvend=`echo "$1" | ${cutbin} -d: -f1 -s`
  3831. usbprod=`echo "$1" | ${cutbin} -d: -f2 -s`
  3832. if base_drivers "${usbvend}" "${usbprod}"; then
  3833. if [ "a${baseintercandidates}" != "a" ]; then
  3834. ifvalid=`echo ${USB_INTERFACE} | ${trbin} " " "\n" | ${grepbin} "^${baseintercandidates}$"`
  3835. if [ "a${ifvalid}" = "a${baseintercandidates}" ]; then
  3836. if [ "a${USBINTERFACE}" = "a" ]; then
  3837. export USB_INTERFACE="${baseintercandidates}"
  3838. debug "Will use interface #%d derived by config.\n" "${USB_INTERFACE}"
  3839. unset ifvalid; unset usbvend; unset usbprod; unset baseintercandidates
  3840. return 0
  3841. else
  3842. debug "However, will not use it in favor of already selected USBINTERFACE: %s\n" "${USBINTERFACE}"
  3843. fi
  3844. else
  3845. debug "However, this interface is not available from device.\n"
  3846. fi
  3847. unset ifvalid
  3848. else
  3849. debug "Config does not specify interface for device %s.\n" "$1"
  3850. fi
  3851. unset baseintercandidates
  3852. else
  3853. debug "No configuration for device %s.\n" "$1"
  3854. fi
  3855. unset usbvend; unset usbprod
  3856. if flow_select_interface "$1" "${USB_INTERFACE}"; then
  3857. [ "a${USBINTERFACE}" = "a" ] && return 98
  3858. export USB_INTERFACE="${USBINTERFACE}"
  3859. debug "Will use interface #%d of USB device \"%s\".\n" "${USB_INTERFACE}" "$1"
  3860. return 0
  3861. else
  3862. unset USB_INTERFACE
  3863. debug "User did not provide an interface.\n"
  3864. return 98
  3865. fi
  3866. return 99
  3867. }
  3868.  
  3869. usb_sysfsdir() {
  3870. unset sysfsloc
  3871. [ "a$1" = "a" ] && return 1
  3872. ! find_binary "dirname" && return 1
  3873. usbven=`echo "$1" | ${cutbin} -d: -f1`; usbpro=`echo "$1" | ${cutbin} -d: -f2`
  3874. venentry=`${grepbin} -H . /sys/bus/usb/devices/*/*/idVendor 2> /dev/null | ${grepbin} ":${usbven}$"`
  3875. if [ "a${venentry}" != "a" ]; then
  3876. for ff in ${venentry}
  3877. do
  3878. unset dirloc
  3879. dirloc=`${dirnamebin} \`echo ${ff} | ${sedbin} -e "s/:${usbven}$//g"\` 2> /dev/null`
  3880. if [ "a${dirloc}" != "a" ]; then
  3881. if [ -d "${dirloc}" ]; then
  3882. dirpro=`${catbin} ${dirloc}/idProduct`
  3883. if [ "a${dirpro}" = "a${usbpro}" ]; then
  3884. export sysfsloc="${dirloc}"
  3885. debug "Device %s sysfs dir found: %s\n" "$1" "${sysfsloc}"
  3886. unset dirpro; unset dirloc; unset venentry; unset usbven; unset usbpro
  3887. return 0
  3888. fi
  3889. unset dirpro
  3890. fi
  3891. fi
  3892. unset dirloc
  3893. done
  3894. fi
  3895. debug "No device %s located in sysfs.\n" "$1"
  3896. unset venentry; unset usbven; unset usbpro
  3897. return 1
  3898. }
  3899.  
  3900. usb_sysfsdir_int() {
  3901. unset sysfsintloc
  3902. [ "a$1" = "a" -o "a$2" = "a" ] && return 99
  3903. ! usb_sysfsdir "$1" && return 1
  3904. intnum="$2"; [ "${intnum}" -lt "10" ] && intnum="0$2"
  3905. intdir=`${grepbin} "." ${sysfsloc}/*/bInterfaceNumber | ${grepbin} "bInterfaceNumber:${intnum}$" | ${sedbin} -e "s/bInterfaceNumber:${intnum}$/bInterfaceNumber/g"`; intdir=`${dirnamebin} "${intdir}" 2> /dev/null`; unset intnum
  3906. if [ "a${intdir}" = "a" ]; then
  3907. unset intdir; debug "Failed to retrieve sysfs directory of interface #%d.\n" "$2"
  3908. return 1
  3909. elif [ ! -d "${intdir}" ]; then
  3910. unset intdir; debug "Unable to retrieve sysfs directory of interface #%d.\n" "$2"
  3911. return 1
  3912. fi
  3913. debug "Interface #%d sysfs dir is: %s\n" "$2" "${intdir}"
  3914. export sysfsintloc="${intdir}"
  3915. unset intdir
  3916. return 0
  3917. }
  3918.  
  3919. # Finds tty which resides on interface $2 of usb device $1 and sets USBTTY
  3920. usb_sysfs_locate_tty() {
  3921. unset USBTTY
  3922. [ "a$1" = "a" -o "a$2" = "a" ] && return 99
  3923. ! usb_sysfsdir_int "$1" "$2" && return 1
  3924. intstr=`cd ${sysfsintloc} ; ${grepbin} -H "." tty*/dev tty*/tty*/dev tty*/tty*/tty*/dev 2> /dev/null | ${sortbin} | ${uniqbin}`; unset intdir
  3925. if [ "a${intstr}" = "a" ]; then
  3926. unset intstr; debug "Failed to retrieve sysfs tty for interface #%d.\n" "$2"
  3927. return 1
  3928. fi
  3929. debug "Interface #%d tty string is: %s\n" "$2" "${intstr}"
  3930. unset ttynode
  3931. [ "a${ttynode}" = "a" ] && ttynode=`echo "${intstr}" | ${sedbin} -e "s/^\(.*\)ttyUSB\([0-9][0-9]*\)\(.*\)$/ttyUSB\2/g" | ${grepbin} "^ttyUSB"`
  3932. [ "a${ttynode}" = "a" ] && ttynode=`echo "${intstr}" | ${sedbin} -e "s/^\(.*\)ttyACM\([0-9][0-9]*\)\(.*\)$/ttyACM\2/g" | ${grepbin} "^ttyACM"`
  3933. [ "a${ttynode}" = "a" ] && ttynode=`echo "${intstr}" | ${sedbin} -e "s/^\(.*\)ttyHS\([0-9][0-9]*\)\(.*\)$/ttyHS\2/g" | ${grepbin} "^ttyHS"`
  3934. devicenode=`echo "${intstr}" | ${sedbin} -e "s/^\(.*\)dev:\([0-9][0-9]*\):\([0-9][0-9]*\)$/\2:\3/g"`
  3935. ttymajor=`echo "${devicenode}" | ${cutbin} -d: -f1`
  3936. ttyminor=`echo "${devicenode}" | ${cutbin} -d: -f2`
  3937. unset intstr; unset devicenode
  3938. debug "Will check if \"%s\" exists, or will create it with major %d and minor %d.\n" "/dev/${ttynode}" "${ttymajor}" "${ttyminor}"
  3939. if dev_create_node "/dev/${ttynode}" "${ttymajor}" "${ttyminor}"; then
  3940. export USBTTY="/dev/${ttynode}"
  3941. unset ttynode; unset ttymajor; unset ttyminor
  3942. debug "Found tty device node of interface #%d from device \"%s\": %s\n" "$2" "$1" "${USBTTY}"
  3943. return 0
  3944. else
  3945. debug "Failed to create tty device of interface #%d from device \"%s\".\n" "$2" "$1"
  3946. unset ttynode; unset ttymajor; unset ttyminor
  3947. return 8
  3948. fi
  3949. unset ttynode; unset ttymajor; unset ttyminor
  3950. return 99
  3951. }
  3952.  
  3953. usb_sysfsattr() {
  3954. unset SYSFS_USB_Manufacturer
  3955. unset SYSFS_USB_Product
  3956. unset SYSFS_USB_Serial
  3957. [ "a$1" = "a" ] && return 1
  3958. ! usb_sysfsdir "$1" && return 1
  3959. [ "a${sysfsloc}" = "a" ] && return 1
  3960. [ ! -d "${sysfsloc}" ] && return 1
  3961. SYSFS_USB_Manufacturer=`${sedbin} -e "s/ /_/g" "${sysfsloc}/manufacturer" 2> /dev/null`
  3962. SYSFS_USB_Product=`${sedbin} -e "s/ /_/g" "${sysfsloc}/product" 2> /dev/null`
  3963. SYSFS_USB_Serial=`${sedbin} -e "s/ /_/g" "${sysfsloc}/serial" 2> /dev/null`
  3964. debug "Information from sysfs:\nUSB Manufacturer: %s\nUSB Product: %s\nUSB Serial: %s\n" "${SYSFS_USB_Manufacturer}" "${SYSFS_USB_Product}" "${SYSFS_USB_Serial}"
  3965. return 0
  3966. }
  3967.  
  3968. usb_scsisysfsattr() {
  3969. unset scsidir
  3970. unset SYSFS_SCSI_Vendor
  3971. unset SYSFS_SCSI_Model
  3972. unset SYSFS_SCSI_Revision
  3973. [ "a$1" = "a" ] && return 1
  3974. ! usb_sysfsattr "$1" && return 1
  3975. ! find_binary "dirname" && return 1
  3976. # How many seconds to wait for SCSI device to settle
  3977. counter=30
  3978. while [ "a${scsidir}" = "a" ];
  3979. do
  3980. if [ "a$2" = "a" ]; then
  3981. debug "Checking interface #%d of device %s.\n" "0" "$1"
  3982. target=`${lsbin} -1d ${sysfsloc}/*.0/host*/target*/*/vendor 2> /dev/null`
  3983. else
  3984. debug "Checking interface #%d of device %s.\n" "$2" "$1"
  3985. target=`${lsbin} -1d ${sysfsloc}/*.$2/host*/target*/*/vendor 2> /dev/null | ${tailbin} -1`
  3986. fi
  3987. [ "a${target}" != "a" ] && scsidir=`${dirnamebin} "${target}" 2> /dev/null`
  3988. [ "a${scsidir}" = "a" ] && unset scsidir
  3989. [ "a${scsidir}" != "a" ] && [ ! -d "${scsidir}" ] && unset scsidir
  3990. unset target
  3991. if [ "a${scsidir}" = "a" ]; then
  3992. if [ "${counter}" -gt "0" ]; then
  3993. debug "Device not yet settled. Will be waiting for another %d second(s).\n" "${counter}"
  3994. [ "${counter}" -eq "30" ] && verbose "Waiting device to settle"
  3995. sleep 1
  3996. else
  3997. debug "Giving up. SCSI device did not settle, or is switched already.\n"
  3998. break
  3999. fi
  4000. counter=`expr ${counter} - 1`; counter=`echo ${counter}`
  4001. fi
  4002. done
  4003. unset counter
  4004. [ "a${scsidir}" = "a" ] && unset scsidir && return 1
  4005. SYSFS_SCSI_Vendor=`${sedbin} -e "s/ /_/g" "${scsidir}/vendor" 2> /dev/null`
  4006. SYSFS_SCSI_Model=`${sedbin} -e "s/ /_/g" "${scsidir}/model" 2> /dev/null`
  4007. SYSFS_SCSI_Revision=`${sedbin} -e "s/ /_/g" "${scsidir}/rev" 2> /dev/null`
  4008. debug "SCSI device is settled:\nSCSI Vendor: %s\nSCSI Model: %s\nSCSI Revision: %s\n" "${SYSFS_SCSI_Vendor}" "${SYSFS_SCSI_Model}" "${SYSFS_SCSI_Revision}"
  4009. return 0
  4010. }
  4011.  
  4012. # Returns true if device $1 has a storage interface
  4013. usb_has_storage() {
  4014. unset STORAGEIF
  4015. [ "a$1" = "a" ] && return 99
  4016. if [ -r "/proc/bus/usb/devices" ]; then
  4017. usbblock=`usb_device_block "$1"`
  4018. if [ "a${usbblock}" = "a" ]; then
  4019. if [ "a${TIMEOUTOCCURED}" != "a" ]; then
  4020. show_fmt_error "Seems like an electrical/USB bus error occured. You may need to replug your modem for it to properly work.\n"
  4021. unset TIMEOUTOCCURED
  4022. fi
  4023. debug "No information for device was available. Is device really connected?\n"
  4024. debug "Will consider storage part does not exist.\n"
  4025. debug "However, this may lead into problems later.\n"
  4026. unset usbblock
  4027. return 1
  4028. fi
  4029. storageclass=`echo "${usbblock}" | ${grepbin} "Cls=08.stor."`
  4030. [ "a${storageclass}" = "a" ] && storageclass=`echo "${usbblock}" | ${grepbin} "river=usb_storage"`
  4031. [ "a${storageclass}" = "a" ] && storageclass=`echo "${usbblock}" | ${grepbin} "river=usb-storage"`
  4032. if [ "a${storageclass}" = "a" ]; then
  4033. debug "Device \"%s\" has no storage part.\n" "$1"
  4034. unset usbblock; unset storageclass
  4035. return 1
  4036. fi
  4037. storageclass=`echo "${storageclass}" | ${sedbin} -e "s/^I:\(.*\)If.=\( *\)\([0-9][0-9]*\) \(.*\)$/\3/g"`
  4038. debug "Device has storage part on interface #%s.\n" "${storageclass}"
  4039. STORAGEIF=`echo "${storageclass}" | ${headbin} -1 | ${cutbin} "-d " -f1`
  4040. export STORAGEIF
  4041. unset usbblock; unset storageclass
  4042. return 0
  4043. elif find_binary "lsusb"; then
  4044. storageclass=`safe_lsusb -d "$1" -v | ${grepbin} -B 10 -A 10 -i storage | ${grepbin} -i "bInterfaceNumber" | ${tailbin} -1 | ${sedbin} -e "s/ */ /g" | ${sedbin} -e "s/^ //g" | ${cutbin} -d\ -f2`
  4045. if [ "a${storageclass}" = "a" ]; then
  4046. if [ "a${TIMEOUTOCCURED}" != "a" ]; then
  4047. show_fmt_error "Seems like an electrical/USB bus error occured. You may need to replug your modem for it to properly work.\n"
  4048. unset TIMEOUTOCCURED
  4049. fi
  4050. debug "Device \"%s\" has no storage part.\n" "$1"
  4051. unset storageclass
  4052. return 1
  4053. fi
  4054. debug "Device has storage part on interface #%s.\n" "${storageclass}"
  4055. # TODO: What happens when more storage interfaces exist?
  4056. STORAGEIF=`echo "${storageclass}" | ${headbin} -1 | ${cutbin} -d\ -f1`
  4057. export STORAGEIF
  4058. unset storageclass
  4059. return 0
  4060. else
  4061. debug "Both %s and %s are not available.\n" "/proc/bus/usb/devices" "lsusb"
  4062. debug "Failed to determine if device %s has a storage interface.\n" "$1"
  4063. debug "Will consider storage part does not exist.\n"
  4064. debug "However, this may lead into problems later.\n"
  4065. return 1
  4066. fi
  4067. }
  4068.  
  4069. # Newer kernels provide this feature
  4070. usb_stabilize() {
  4071. [ "a$1" = "a" ] && return 1
  4072. ! usb_sysfsdir "$1" && return 1
  4073. [ "a${sysfsloc}" = "a" ] && return 1
  4074. [ ! -d "${sysfsloc}" ] && return 1
  4075. [ ! -w "${sysfsloc}/avoid_reset_quirk" ] && return 1
  4076. debug "Kernel provides AVOID_RESET_QUIRK for device %s.\n" "$1"
  4077. echo "1" > "${sysfsloc}/avoid_reset_quirk" 2> /dev/null
  4078. ret=$?
  4079. [ "a${ret}" = "a0" ] && debug "Succesfully enabled AVOID_RESET_QUIRK for device %s.\n" "$1"
  4080. [ "a${ret}" = "a0" ] && debug "Failed to enable AVOID_RESET_QUIRK for device %s.\n" "$1"
  4081. return ${ret}
  4082. }
  4083.  
  4084. # Makes sure that if device $1 has a storage part, this part
  4085. # is settled.
  4086. usb_storage_settled() {
  4087. if ! usb_has_storage "$1"; then
  4088. hal_unlock
  4089. return 0
  4090. fi
  4091. debug "Device %s, provides storage interface #%s.\n" "$1" "${STORAGEIF}"
  4092. storageloaded=`usb_loaded_driver "$1" "${STORAGEIF}" | ${tailbin} -1`
  4093. if [ "a${TIMEOUTOCCURED}" != "a" ]; then
  4094. show_fmt_error "Seems like an electrical/USB bus error occured. You may need to replug your modem for it to properly work.\n"
  4095. unset TIMEOUTOCCURED
  4096. fi
  4097. if [ "a${storageloaded}" != "ausb-storage" -a "a${storageloaded}" != "ausb_storage" ]; then
  4098. debug "Interface %s of device %s is bound to driver: %s\n" "${STORAGEIF}" "$1" "${storageloaded}"
  4099. debug "No need to wait for storage interface to settle.\n"
  4100. unset storageloaded
  4101. hal_unlock
  4102. return 0
  4103. fi
  4104. debug "Storage interface #%s is binded by %s driver.\n" "${STORAGEIF}" "${storageloaded}"
  4105. if [ "a${killstorage}" != "a" ]; then
  4106. debug "User has set \"%s\" switch. Will try to unbind %s driver from interface #%d.\n" "killstorage" "${storageloaded}" "${STORAGEIF}"
  4107. if module_unbind "${storageloaded}" "$1" "${STORAGEIF}"; then
  4108. debug "Storage interface has been freed as instructed.\n"
  4109. debug "No need to wait for storage interface to settle.\n"
  4110. unset storageloaded
  4111. hal_unlock
  4112. return 0
  4113. else
  4114. debug "Failed to unbind it. Too bad.\n"
  4115. show_fmt_error "Failed to unbind driver %s. I am sorry." "${storageloaded}"
  4116. fi
  4117. fi
  4118. unset storageloaded
  4119. if ! usb_scsisysfsattr "$1" "${STORAGEIF}"; then
  4120. debug "Will consider storage part is settled.\n"
  4121. debug "However, this may lead into problems later.\n"
  4122. fi
  4123. storagedevices=`${grepbin} . ${sysfsloc}/*.${STORAGEIF}/host*/target*/*/block*/size 2> /dev/null | ${grepbin} -v ":0$" | ${sedbin} -e "s/^\(.*\)\/block:\(.*\)\/size:\(.*\)$/\2/g" | ${trbin} "\n" " "`; storagedevices=`echo ${storagedevices}`
  4124. if [ "a${storagedevices}" = "a" ]; then
  4125. debug "No storage device(s) with inserted media exist. Safe to unlock HAL.\n"
  4126. hal_unlock
  4127. unset storagedevices; unset STORAGEIF
  4128. return 0
  4129. fi
  4130. debug "Media found on following device(s) provided by interface #%s of device %s: %s\n" "${STORAGEIF}" "$1" "${storagedevices}"
  4131. if [ "a${HAL_LOCK}" != "a" ]; then
  4132. hal_unlock
  4133. debug "HAL is unlocked. Checking if HAL will kick in.\n"
  4134. pausecounter=21
  4135. while [ "${pausecounter}" -gt "0" ]
  4136. do
  4137. blocking=""
  4138. for blockdevice in ${storagedevices}
  4139. do
  4140. poller=`${psbin} -Af | ${grepbin} "hald" | ${grepbin} "storage" | ${grepbin} "polling \(.*\)${blockdevice}" | ${grepbin} -v "${grepbin}"`
  4141. if [ "a${poller}" = "a" ]; then
  4142. debug "No polling process exists for %s.\n" "${blockdevice}"
  4143. continue
  4144. fi
  4145. debug "Polling process exists for %s: %s\n" "${blockdevice}" "${poller}"
  4146. poller=`${grepbin} "\/${blockdevice} " "/proc/mounts" 2> /dev/null`
  4147. if [ "a${poller}" != "a" ]; then
  4148. debug "Device %s mounted: %s\n" "${blockdevice}" "${poller}"
  4149. else
  4150. debug "Device %s still not mounted.\n" "${blockdevice}"
  4151. blocking="${blocking} ${blockdevice}"
  4152. fi
  4153. done
  4154. unset blockdevice; unset poller; blocking=`echo ${blocking}`
  4155. if [ "a${blocking}" = "a" ]; then
  4156. debug "HAL has finished mounting media on device(s): %s\n" "${storagedevices}"
  4157. break
  4158. fi
  4159. pausecounter=`expr ${pausecounter} - 1`; pausecounter=`echo ${pausecounter}`
  4160. if [ "${pausecounter}" -eq "0" ]; then
  4161. debug "Giving up waiting for HAL to mount devices.\n"
  4162. debug "This may lead into problems later on.\n"
  4163. else
  4164. [ "${pausecounter}" -eq "20" ] && verbose "Waiting HAL to mount device(s)"
  4165. debug "Waiting for HAL to mount all devices. %d second(s) to abort.\n" "${pausecounter}"
  4166. sleep 1
  4167. fi
  4168. done
  4169. unset poller; unset blockdevice
  4170. unset pausecounter; unset blocking
  4171. else
  4172. debug "HAL was not locked. Will skip waiting for device mount.\n"
  4173. fi
  4174. unset storagedevices; unset STORAGEIF
  4175. return 0
  4176. }
  4177.  
  4178.  
  4179. # Scans for bluetooth devices
  4180. bluetooth_scan_devices() {
  4181. ! find_binary "hcitool" && return 1
  4182. verbose "Scanning bluetooth devices"
  4183. bluetooth_devices=`${hcitoolbin} scan 2> /dev/null | ${grepbin} "^ " | ${sedbin} -e "s/^ \(.*\) \(.*\)/\1 \2/g"`
  4184. debug "Found bluetooth devices:\n%s\n" "${bluetooth_devices}"
  4185. return 0
  4186. }
  4187.  
  4188. # Returns rfcomm channel for service $2 for device $1
  4189. bluetooth_rfcomm_channel() {
  4190. ! find_binary "sdptool" && return 0
  4191. rfchannel=`${sdptoolbin} search --raw --bdaddr "$1" "$2" 2> /dev/null | ${grepbin} -A 1 --binary-files=text "RFCOMM" | ${grepbin} "UINT8" | ${sedbin} -e "s/^\(.*\)UINT8 \(.*\)/\2/g"`
  4192. rfchannel=`echo ${rfchannel}`
  4193. debug "Found RFCOMM channel \"%s\" of service \"%s\" on device \"%s\".\n" "${rfchannel}" "$2" "$1"
  4194. [ "a${rfchannel}" != "a" ] && rfchannel=`${printfbin} "%d\n" ${rfchannel} 2> /dev/null`
  4195. [ "a${rfchannel}" = "a" ] && rfchannel="0"
  4196. debug "Returning RFCOMM channels \"%s\".\n" "`echo ${rfchannel}`"
  4197. return `echo ${rfchannel} | ${cutbin} -d\ -f1`
  4198. }
  4199.  
  4200. # Returns true if device $1, channel $2 is on tty $3
  4201. # NOTE: Does not depend on bluetooth_rfcomm_dev because
  4202. # bluetooth_rfcomm_dev only returns 1st valid
  4203. # channel
  4204. bluetooth_rfcomm_match() {
  4205. ! find_binary "rfcomm" && return 1
  4206. [ "a${3}" != "a" ] && [ ! -c "${3}" ] && return 1
  4207. bluematch=`${rfcommbin} -a --raw | ${grepbin} --binary-files=text " $1 " | ${grepbin} --binary-files=text " channel $2 " | ${grepbin} "^\`${basenamebin} $3\`:" | ${wcbin} -l`; bluematch=`echo ${bluematch}`
  4208. if [ "a${bluematch}" = "a1" ]; then
  4209. debug "%s refers to RFCOMM channel %d of device %s.\n" "$3" "$2" "$1"
  4210. unset bluematch
  4211. return 0
  4212. fi
  4213. debug "%s does not refer to RFCOMM channel %d of device %s.\n" "$3" "$2" "$1"
  4214. unset bluematch
  4215. return 1
  4216. }
  4217.  
  4218. # Returns tty where device $1, channel $2 resides (if any)
  4219. bluetooth_rfcomm_dev() {
  4220. ! find_binary "rfcomm" > /dev/null && return 1
  4221. bluematch=`${rfcommbin} -a --raw 2> /dev/null | ${grepbin} --binary-files=text " $1 " | ${grepbin} --binary-files=text " channel $2 " | ${headbin} -1 | ${cutbin} -d: -f1`
  4222. if [ "a${bluematch}" = "a" ]; then
  4223. unset bluematch
  4224. return 1
  4225. fi
  4226. echo "${bluematch}"
  4227. unset bluematch
  4228. return 0
  4229. }
  4230.  
  4231. # Creates rfcomm device for device $1, channel $2 and sets RFCOMM_TTY
  4232. bluetooth_create_dev() {
  4233. ! we_are_root_already && debug "Need root privileges to create rfcomm nodes.\n"
  4234. ! we_are_root && return 1
  4235. need_binary "rfcomm"
  4236. for devindex in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  4237. do
  4238. unset occupied
  4239. occupied=`${rfcommbin} show ${devindex} 2> /dev/null`
  4240. [ "a${occupied}" = "a" ] && break
  4241. done
  4242. if [ "a${occupied}" != "a" ]; then
  4243. unset occupied
  4244. unset devindex
  4245. show_fmt_error "Unable to find available rfcomm device index. Minors occupied."
  4246. return 1
  4247. fi
  4248. unset occupied
  4249. debug "Found available rfcomm device node %d.\n" "${devindex}"
  4250. debug run_command "${rfcommbin} bind ${devindex} \"$1\" \"$2\""
  4251. if ! bluetooth_rfcomm_match "$1" "$2" "/dev/rfcomm${devindex}"; then
  4252. show_fmt_error "Unable to create rfcomm node %s." "rfcomm${devindex}"
  4253. unset devindex
  4254. return 1
  4255. fi
  4256. if [ ! -c "/dev/rfcomm${devindex}" ]; then
  4257. debug "Device node \"%s\" was not automatically created.\n" "/dev/rfcomm${devindex}"
  4258. if ! dev_create_node "/dev/rfcomm${devindex}" "216" "${devindex}"; then
  4259. show_fmt_error "Unable to create rfcomm device node \"%s\"." "/dev/rfcomm${devindex}"
  4260. unset devindex
  4261. return 1
  4262. fi
  4263. debug "Made node \"%s\" ourselves.\n" "/dev/rfcomm${devindex}"
  4264. fi
  4265. export RFCOMM_TTY="/dev/rfcomm${devindex}"
  4266. unset devindex
  4267. }
  4268.  
  4269. # Setups rfcomm device for device $1, channel $2 and sets RFCOMM_TTY
  4270. bluetooth_setup_rfcomm() {
  4271. if [ "a${RFCOMM_TTY}" != "a" ]; then
  4272. if bluetooth_rfcomm_match "$1" "$2" "${RFCOMM_TTY}"; then
  4273. debug "Will use already set RFCOMM_TTY (%s).\n" "${RFCOMM_TTY}"
  4274. return 0
  4275. else
  4276. debug "Already set RFCOMM_TTY does not refer to a tty of \"%s\": \"%s\"\n" "$1" "${RFCOMM_TTY}"
  4277. unset RFCOMM_TTY
  4278. fi
  4279. fi
  4280. RFCOMM_TTY=`bluetooth_rfcomm_dev "$1" "$2"`
  4281. if [ "a${RFCOMM_TTY}" != "a" ]; then
  4282. export RFCOMM_TTY="/dev/${RFCOMM_TTY}"
  4283. if [ -c "${RFCOMM_TTY}" ]; then
  4284. debug "Found already binded rfcomm tty %s.\n" "${RFCOMM_TTY}"
  4285. return 0
  4286. fi
  4287. unset RFCOMM_TTY
  4288. fi
  4289. unset RFCOMM_TTY
  4290. if bluetooth_create_dev "$1" "$2"; then
  4291. debug "Succesfully binded to rfcomm tty %s.\n" "${RFCOMM_TTY}"
  4292. return 0
  4293. fi
  4294. debug "Failed to setup rfcomm tty for device \"%s\".\n" "$1"
  4295. return 1
  4296. }
  4297.  
  4298. # Method invoked when "status" action was requested.
  4299. ppp_fast_status() {
  4300. need_binary "grep"
  4301. need_arg "pppint"
  4302. unset status
  4303. [ -f "/proc/net/dev" ] && [ -r "/proc/net/dev" ] && status=`${grepbin} "${pppint}:" /proc/net/dev`
  4304. [ -z "${status}" ] && find_binary "ifconfig" && status=`${ifconfigbin} ${pppint} 2> /dev/null | ${grepbin} " UP "`
  4305. if [ "a${status}" = "a" ]; then
  4306. unset status
  4307. return 1
  4308. else
  4309. unset status
  4310. return 0
  4311. fi
  4312. }
  4313.  
  4314. # Called by ispconnect to determine if connection is established
  4315. ppp_slow_status() {
  4316. need_arg "pppint"
  4317. need_binary "netstat"
  4318. [ "a${pppint}" = "a" ] && pppint="ppp0"
  4319. pppcon=`${catbin} /proc/net/dev | ${grepbin} "^\( *\)${pppint}:" | ${wcbin} -l`; pppcon=`echo ${pppcon}`
  4320. [ "a${pppcon}" = "a0" ] && unset pppcon && return 1
  4321. debug "Interface %s is up.\n" "${pppint}"
  4322. pppcon=`${netstatbin} -rn | ${grepbin} " ${pppint}$" | ${grepbin} -v "^0.0.0.0" | ${wcbin} -l`; pppcon=`echo ${pppcon}`
  4323. [ "a${pppcon}" = "a0" ] && unset pppcon && return 1
  4324. debug "Default route through %s is established.\n" "${pppint}"
  4325. debug "Really connected.\n"
  4326. return 0
  4327. }
  4328.  
  4329. # Shows version
  4330. show_version() {
  4331. if find_binary "printf"; then
  4332. format_text "Sakis 3G All-in-one script - Version %s\n" "${MYVERSION}"
  4333. format_text "(c) Sakis Dimopoulos 2009, 2010 under GNU GPL v2\n\n"
  4334. else
  4335. need_binary "echo"
  4336. echo "Sakis 3G All-in-one script - Version ${MYVERSION}"
  4337. echo "(c) Sakis Dimopoulos 2009, 2010 under GNU GPL v2"
  4338. echo
  4339. fi
  4340. }
  4341.  
  4342. # Shows usage help
  4343. show_help() {
  4344. helptext=`raw_help`
  4345. notify "%s\n" "${helptext}"
  4346. return 0
  4347. }
  4348.  
  4349. raw_help() {
  4350. [ "a${PROVIDER}" = "a" ] && return 1
  4351. [ ! -x "${PROVIDER}" ] && return 1
  4352. show_version
  4353. echo
  4354. "${PROVIDER}" getfile "files/help.txt" 2> /dev/null
  4355. }
  4356.  
  4357. modeswitch_load_realtime() {
  4358. debug "Loading system supplied device database.\n"
  4359. for dep in sed rm grep tr sort uniq cut printf basename
  4360. do
  4361. ! find_binary "${dep}" && return 1
  4362. done
  4363. unset dep; unset realtime_switchable; unset realtime_switched;
  4364. unset CheckSuccess; unset Configuration;
  4365. unset GCTMode; unset HuaweiMode; unset SierraMode; unset SonyMode;
  4366. unset Interface; unset MessageContent; unset ResponseNeeded
  4367. unset DefaultProduct; unset DefaultVendor;
  4368. unset TargetClass; unset TargetProduct; unset TargetProductList; unset TargetVendor
  4369. for filename in /etc/usb_modeswitch.d/* /etc/usb_modeswitch.d/*
  4370. do
  4371. current_device=`${basenamebin} ${filename} | ${cutbin} -d: -f1,2 -s`
  4372. [ "a${current_device}" = "a" ] && unset current_device && continue
  4373. realtime_switchable="${realtime_switchable} ${current_device}"
  4374. ${sedbin} -e "s/ //g" "${filename}" > "/tmp/$$.tmp" 2> /dev/null
  4375. . "/tmp/$$.tmp" > /dev/null 2> /dev/null
  4376. ${rmbin} -f "/tmp/$$.tmp" > /dev/null 2> /dev/null
  4377. for ven in ${TargetVendor}
  4378. do
  4379. if [ -n "${TargetProductList}" ]; then
  4380. list=`echo ${TargetProductList} | ${sedbin} -e "s/,/ /g"`
  4381. for pro in $list
  4382. do
  4383. realtime_switched="${realtime_switched} ${ven}:${pro}"
  4384. done
  4385. unset list
  4386. elif [ -n "${TargetProduct}" ]; then
  4387. realtime_switched="${realtime_switched} ${ven}:${TargetProduct}"
  4388. fi
  4389. done
  4390. unset current_device
  4391. unset CheckSuccess; unset Configuration;
  4392. unset GCTMode; unset HuaweiMode; unset SierraMode; unset SonyMode;
  4393. unset Interface; unset MessageContent; unset ResponseNeeded
  4394. unset DefaultProduct; unset DefaultVendor;
  4395. unset TargetClass; unset TargetProduct; unset TargetProductList; unset TargetVendor
  4396. done
  4397. realtime_switched=`echo ${realtime_switched} | ${sedbin} -e "s/0x//g" | ${trbin} " " "\n" | ${sortbin} | ${uniqbin}`
  4398. realtime_switched=`echo ${realtime_switched}`
  4399. realtime_switchable=`echo ${realtime_switchable} | ${sedbin} -e "s/0x//g" | ${trbin} " " "\n" | ${sortbin} | ${uniqbin}`
  4400. realtime_switchable=`echo ${realtime_switchable}`
  4401. debug "Switchable devices within system device database:\n%s\n" "${realtime_switchable}"
  4402. debug "Switched devices within system device database:\n%s\n" "${realtime_switched}"
  4403. ${printfbin} "%s\n%s\n" "${realtime_switchable}" "${realtime_switched}"
  4404. unset realtime_switched; unset realtime_switchable
  4405. return 0
  4406. }
  4407.  
  4408. # Loads Usb-ModeSwitch device database, both embedded and system supplied
  4409. modeswitch_load() {
  4410. unset SWITCHABLE_devices; unset SWITCHED_devices; unset KNOWN_devices
  4411. debug "Loading Usb-ModeSwitch device database.\n"
  4412. # Check if possible to include embedded database
  4413. if [ "a${PROVIDER}" = "a" ]; then
  4414. debug "Unable to locate package provider.\n"
  4415. elif [ ! -x "${PROVIDER}" ]; then
  4416. debug "Unable to execute package provider.\n"
  4417. else
  4418. "${PROVIDER}" getfile "build/switchconfig" > "/tmp/sakis3g.$$.sw"
  4419. if [ ! -f "/tmp/sakis3g.$$.sw" ]; then
  4420. debug "Unable to find device database.\n"
  4421. elif [ ! -s "/tmp/sakis3g.$$.sw" ]; then
  4422. rm -f "/tmp/sakis3g.$$.sw"
  4423. debug "Embedded device database contains 0 entries.\n"
  4424. else
  4425. . "/tmp/sakis3g.$$.sw"
  4426. rm -f "/tmp/sakis3g.$$.sw"
  4427. getswitchabledevices 2> /dev/null
  4428. # TODO: Comment following line to render embedded device database unusable.
  4429. #unset usbswitchabledevices && unset usbswitcheddevices && debug "DELETED DATABASE TO EMULATE VOODOO MODE.\n"
  4430. ret=$?;
  4431. if [ "${ret}" -ne "0" ]; then
  4432. debug "Failed to load embedded Usb-ModeSwitch device database.\n"
  4433. else
  4434. debug "Embedded device database loaded.\n"
  4435. export SWITCHABLE_devices="${usbswitchabledevices}"
  4436. export SWITCHED_devices="${usbswitcheddevices}"
  4437. debug "Switchable devices within embedded device database:\n%s\n" "${SWITCHABLE_devices}"
  4438. debug "Switched devices within embedded device database:\n%s\n" "${SWITCHED_devices}"
  4439. KNOWN_devices=`echo "${SWITCHABLE_devices} ${SWITCHED_devices}" | ${trbin} " " "\n" | ${sortbin} | ${uniqbin}`
  4440. KNOWN_devices=`echo "${KNOWN_devices}"`
  4441. export KNOWN_devices
  4442. devicecount=`echo "${KNOWN_devices}" | ${wcbin} -w`; devicecount=`echo ${devicecount}`; [ "a${devicecount}" = "a" ] && devicecount=0
  4443. debug "Embedded Usb-ModeSwitch device database contains %d entries.\n" "${devicecount}"
  4444. unset devicecount
  4445. fi
  4446. fi
  4447. fi
  4448. # Check if system supplied Usb-ModeSwitch database exists
  4449. if [ -d "/etc/usb_modeswitch.d" ]; then
  4450. debug "Folder \"%s\" exists. Will check if it contains configuration files.\n" "/etc/usb_modeswitch.d"
  4451. realtime_devices=`modeswitch_load_realtime`
  4452. if [ "a${realtime_devices}" != "a" ]; then
  4453. usbswitchabledevices=`echo "${realtime_devices}" | ${tailbin} -2 | ${headbin} -1`
  4454. usbswitcheddevices=`echo "${realtime_devices}" | ${tailbin} -1`
  4455. SWITCHABLE_devices="${SWITCHABLE_devices} ${usbswitchabledevices}"
  4456. SWITCHED_devices="${SWITCHED_devices} ${usbswitcheddevices}"
  4457. SWITCHABLE_devices=`echo "${SWITCHABLE_devices}" | ${trbin} " " "\n" | ${sortbin} | ${uniqbin}`
  4458. SWITCHED_devices=`echo "${SWITCHED_devices}" | ${trbin} " " "\n" | ${sortbin} | ${uniqbin}`
  4459. SWITCHABLE_devices=`echo ${SWITCHABLE_devices}`
  4460. SWITCHED_devices=`echo ${SWITCHED_devices}`
  4461. KNOWN_devices=`echo "${SWITCHABLE_devices} ${SWITCHED_devices}" | ${trbin} " " "\n" | ${sortbin} | ${uniqbin}`
  4462. KNOWN_devices=`echo "${KNOWN_devices}"`
  4463. export SWITCHABLE_devices
  4464. export SWITCHED_devices
  4465. export KNOWN_devices
  4466. devicecount=`echo "${KNOWN_devices}" | ${wcbin} -w`; devicecount=`echo ${devicecount}`; [ "a${devicecount}" = "a" ] && devicecount=0
  4467. debug "Device database now contains %d entries.\n" "${devicecount}"
  4468. unset devicecount
  4469. fi
  4470. unset realtime_devices
  4471. fi
  4472. unset usbswitchabledevices; unset usbswitcheddevices
  4473. return 0
  4474. }
  4475.  
  4476. # Returns 0 if device $1 is switchable
  4477. modeswitch_is_switchable() {
  4478. [ "a${KNOWN_devices}" = "a" ] && ! modeswitch_load && return 1
  4479. isswitchable=`echo "${SWITCHABLE_devices}" | ${grepbin} "$1" | ${wcbin} -l`; isswitchable=`echo ${isswitchable}`
  4480. [ "a${isswitchable}" = "a" ] && isswitchable="0"
  4481. if [ "a${isswitchable}" = "a0" ]; then
  4482. if [ -f "/etc/usb_modeswitch.d/$1" ]; then
  4483. debug "Since file \"%s\" exists, will consider device \"%s\" switchable.\n" "/etc/usb_modeswitch.d/$1" "$1"
  4484. unset isswitchable
  4485. return 0
  4486. fi
  4487. debug "Device \"%s\" is not switchable.\n" "$1"
  4488. unset isswitchable
  4489. return 1
  4490. else
  4491. unset isswitchable
  4492. return 0
  4493. fi
  4494. }
  4495.  
  4496. modeswitch_switchable_devices() {
  4497. ! usb_connected_modems && return 1
  4498. for condevice in ${usb_modems}
  4499. do
  4500. if modeswitch_is_switchable "${condevice}"; then
  4501. debug "There is at least one (%s) switchable device plugged.\n" "${condevice}"
  4502. unset condevice
  4503. return 0
  4504. fi
  4505. done
  4506. unset condevice
  4507. debug "No switchable device plugged.\n" "${condevice}"
  4508. return 1
  4509. }
  4510.  
  4511. modeswitch_conf_realtime() {
  4512. unset modeswitch_config
  4513. debug "Seeking a system supplied configuration file for switching device %s.\n" "$1"
  4514. [ ! -d "/etc/usb_modeswitch.d" -a ! -d "/etc/usb-modeswitch.d" ] && return 1
  4515. # Check if exists a candidate which needs SCSI information
  4516. needscsi=`${lsbin} -1r /etc/usb_modeswitch.d/$1:s* /etc/usb-modeswitch.d/$1:s* 2> /dev/null | ${wcbin} -l`
  4517. needscsi=`echo ${needscsi}`
  4518. if [ "a${needscsi}" != "a0" -a "a${needscsi}" != "a" ]; then
  4519. debug "Will need SCSI information for finding appropriate file.\n"
  4520. usb_scsisysfsattr "$1"
  4521. fi
  4522. unset needscsi
  4523. # Iterate through files
  4524. files=`${lsbin} -1r /etc/usb_modeswitch.d/$1* /etc/usb-modeswitch.d/$1* 2> /dev/null`
  4525. for filename in ${files}
  4526. do
  4527. debug "Checking if file \"%s\" matches.\n" "${filename}"
  4528. bn=`${basenamebin} "${filename}"`
  4529. crit=`echo "${bn}" | ${cutbin} -d: -f3-`; crit=`echo ${crit}`
  4530. if [ "a${crit}" != "a" ]; then
  4531. field=`echo "${crit}" | ${cutbin} -d= -f1 -s`
  4532. value=`echo "${crit}" | ${cutbin} -d= -f2 -s`
  4533. if [ "a${field}" = "a" ]; then
  4534. debug "File %s does not specify criteria to match. Skipping it." "${bn}"
  4535. unset bn; unset crit; unset field; unset value
  4536. continue
  4537. fi
  4538. if [ "a${value}" = "a" ]; then
  4539. debug "File %s does not specify value for attribute %s. Skipping it." "${bn}" "${field}"
  4540. unset bn; unset crit; unset field; unset value
  4541. continue
  4542. fi
  4543. len=`echo "${value}" | ${wcbin} -c`; len=`echo ${len}`; len=`expr ${len} - 1 2> /dev/null`; len=`echo ${len}`
  4544. if [ "a${len}" = "a" ] || [ "${len}" -eq "0" ]; then
  4545. debug "Value \"%s\" to be checked is not valid. Skipping file %s." "${value}" "${bn}"
  4546. unset bn; unset crit; unset field; unset value
  4547. continue
  4548. fi
  4549. unset attribute
  4550. case "${field}" in
  4551. sVe)
  4552. attribute=`echo ${SYSFS_SCSI_Vendor}`
  4553. ;;
  4554. sMo)
  4555. attribute=`echo ${SYSFS_SCSI_Model}`
  4556. ;;
  4557. sRe)
  4558. attribute=`echo ${SYSFS_SCSI_Revision}`
  4559. ;;
  4560. uMa)
  4561. attribute=`echo ${SYSFS_USB_Manufacturer}`
  4562. ;;
  4563. uPr)
  4564. attribute=`echo ${SYSFS_USB_Product}`
  4565. ;;
  4566. uSe)
  4567. attribute=`echo ${SYSFS_USB_Serial}`
  4568. ;;
  4569. *)
  4570. debug "FIXME: Unknown field \"%s\". Try downloading a newer Sakis3G version.\n" "${field}"
  4571. ;;
  4572. esac
  4573. if [ "a${attribute}" = "a" ]; then
  4574. debug "No value for field %s exists for device %s.\n" "${field}" "$1"
  4575. unset attribute; unset len; unset field; unset value; unset bn; unset crit
  4576. continue
  4577. fi
  4578. debug "Found %s.%s=\"%s\".\n" "$1" "${field}" "${attribute}"
  4579. attribute=`echo ${attribute} | ${cutbin} -b1-${len} 2> /dev/null`
  4580. if [ "a${attribute}" != "a" -a "a${attribute}" = "a${value}" ]; then
  4581. debug "File %s matches information derived from device.\n" "${bn}"
  4582. modeswitch_config="${filename}"
  4583. unset attribute; unset len; unset field; unset value; unset bn; unset crit
  4584. break;
  4585. fi
  4586. debug "File %s does not match.\n" "${bn}"
  4587. unset attribute; unset len; unset field; unset value;
  4588. else
  4589. debug "Found an exact match file: %s\n" "${filename}"
  4590. modeswitch_config="${filename}"
  4591. fi
  4592. unset crit; unset bn
  4593. done
  4594. filename="${modeswitch_config}"
  4595. unset modeswitch_config
  4596. if [ "a${filename}" != "a" ] && [ -f "${filename}" ]; then
  4597. ${catbin} "${filename}" > "/tmp/sakis3g.sw.dev.$$" 2> /dev/null
  4598. if [ -s "/tmp/sakis3g.sw.dev.$$" ]; then
  4599. export modeswitch_config="/tmp/sakis3g.sw.dev.$$"
  4600. unset filename
  4601. return 0
  4602. else
  4603. ${rmbin} -f "/tmp/sakis3g.sw.dev.$$"
  4604. fi
  4605. fi
  4606. unset filename
  4607. debug "No system supplied configuration file found for switching device \"%s\".\n" "$1"
  4608. return 1
  4609. }
  4610.  
  4611. # Returns location of extracted configuration file that should be used to switch device $1 within modeswitch_config variable
  4612. modeswitch_conf_embedded() {
  4613. unset modeswitch_config
  4614. debug "Seeking embedded configuration file for switching device %s.\n" "$1"
  4615. switchfunc="usbswitchconf_`echo "$1" | ${sedbin} -e "s/:/_/g"`"
  4616. unset modeswitchconf
  4617. eval ${switchfunc} 2> /dev/null
  4618. ret=$?; unset switchfunc
  4619. if [ "${ret}" -ne "0" ]; then
  4620. debug "Failed to retrieve configuration for switching device \"%s\".\n" "$1"
  4621. return 1
  4622. fi
  4623. [ "a${modeswitchconf}" = "a" ] && debug "No configuration file found for switching device \"%s\".\n" "$1" && return 1
  4624. [ "a${PROVIDER}" = "a" ] && debug "Unable to locate package provider.\n" && unset modeswitchconf && return 1
  4625. [ ! -x "${PROVIDER}" ] && debug "Unable to execute package provider.\n" && unset modeswitchconf && return 1
  4626. eval "${PROVIDER}" getfile "${modeswitchconf}" > "/tmp/sakis3g.sw.dev.$$"
  4627. [ ! -f "/tmp/sakis3g.sw.dev.$$" ] && debug "Configuration file \"%s\" for switching device \"%s\" not found within package.\n" "${modeswitchconf}" "$1" && unset modeswitchconf && return 1
  4628. if [ ! -s "/tmp/sakis3g.sw.dev.$$" ]; then
  4629. ${rmbin} -f "/tmp/sakis3g.sw.dev.$$"
  4630. debug "Configuration file \"%s\" for switching device \"%s\" not found within package.\n" "${modeswitchconf}" "$1"
  4631. unset modeswitchconf
  4632. return 1
  4633. fi
  4634. unset modeswitchconf
  4635. export modeswitch_config="/tmp/sakis3g.sw.dev.$$"
  4636. return 0
  4637. }
  4638.  
  4639. modeswitch_conf() {
  4640. unset modeswitch_config
  4641. debug "Seeking configuration file for switching device %s.\n" "$1"
  4642. [ "a${KNOWN_devices}" = "a" ] && ! modeswitch_load && return 1
  4643. ! modeswitch_conf_embedded "$1" && modeswitch_conf_realtime "$1"
  4644. if [ "a${modeswitch_config}" = "a" ] || [ ! -f "${modeswitch_config}" ] || [ ! -s "${modeswitch_config}" ]; then
  4645. unset modeswitch_config
  4646. show_fmt_error "Unable to locate a configuration file for switching device %s.\n" "$1"
  4647. return 1
  4648. fi
  4649. debug run_command "${catbin} \"${modeswitch_config}\""
  4650. debug "Configuration file for switching device \"%s\" at: %s\n" "$1" "${modeswitch_config}"
  4651. return 0
  4652. }
  4653.  
  4654. # Executed when binary Usb-ModeSwitch is binary incompatible on running platform.
  4655. modeswitch_emergency() {
  4656. ! usb_has_storage "$1" && return 1
  4657. debug "Will attempt to switch device by detaching usb-storage driver.\n"
  4658. storageloaded=`usb_loaded_driver "$1" "${STORAGEIF}" | ${tailbin} -1`
  4659. if [ "a${TIMEOUTOCCURED}" != "a" ]; then
  4660. show_fmt_error "Seems like an electrical/USB bus error occured. You may need to replug your modem for it to properly work.\n"
  4661. unset TIMEOUTOCCURED
  4662. fi
  4663. [ "a${storageloaded}" = "aNONE" ] && ! module_bind "usb_storage" "$1" "${STORAGEIF}"
  4664. ! module_unbind "usb_storage" "$1" "${STORAGEIF}"
  4665. unset STORAGEIF
  4666. return 0
  4667. }
  4668.  
  4669. # Switches device $1. Caller should check NEWIDS variable, because device most likely will change its ID.
  4670. modeswitch_switch() {
  4671. [ "a$1" = "a" ] && return 1
  4672. ! modeswitch_is_switchable "$1" && return 1
  4673. ! modeswitch_conf "$1" && return 1
  4674. [ ! -f "${modeswitch_config}" ] && debug "Configuration file \"%s\" disappeared.\n" "${modeswitch_config}"
  4675. # Block hal from messing with us
  4676. hal_acquire_lock "org.freedesktop.Hal.Device.Storage"
  4677. verbose "Switching modem"
  4678. debug "Connected devices before switching.\n"
  4679. usb_connected_devices
  4680. previously=`echo "${usb_devices}" | ${grepbin} -v "^$1:" | ${cutbin} -d: -f1,2`
  4681. if [ "a${binaryfree}" = "a" ]; then
  4682. debug run_command "${PROVIDER} usb_modeswitch -W -I -c \"${modeswitch_config}\""
  4683. ret=$?
  4684. else
  4685. ret=95
  4686. fi
  4687. if [ "${ret}" -eq "95" ]; then
  4688. [ "a${binaryfree}" = "a" ] && debug "Binary incompatibility of package provided usb-modeswitch.\n"
  4689. if find_binary "usb_modeswitch"; then
  4690. debug "Will try system provided binary, hoping it is a recent one: %s\n" "${usb_modeswitchbin}"
  4691. verbose "Switching modem (using %s)" "${usb_modeswitchbin}"
  4692. debug run_command "${usb_modeswitchbin} --version"
  4693. debug run_command "${usb_modeswitchbin} -W -I -c \"${modeswitch_config}\""
  4694. ret=$?
  4695. elif [ "a${binaryfree}" = "a" ]; then
  4696. show_fmt_error "Embedded Usb-ModeSwitch binary is not valid for your architect. You need to recompile for devices to switch properly. Start by issueing: %s recompile" "${PROVIDER}"
  4697. debug "Will try emergency mode.\n"
  4698. verbose "Trying emergency switch"
  4699. modeswitch_emergency "$1"
  4700. ret=$?
  4701. else
  4702. show_fmt_error "%s is not installed on your system.\n" "Usb-ModeSwitch"
  4703. return 4
  4704. fi
  4705. fi
  4706. rm -f "${modeswitch_config}"; unset modeswitch_config
  4707. counter=0
  4708. while [ "${counter}" -lt "20" ]
  4709. do
  4710. usb_connected_devices
  4711. after=`echo "${usb_devices}" | ${grepbin} "^$1:"`
  4712. if [ "a${after}" != "a" -a "${counter}" -eq "0" ]; then
  4713. debug "Device \"%s\" still there. Lets hope it has switched.\n" "$1"
  4714. unset after; unset previously
  4715. export NEWIDS="$1"
  4716. return 0
  4717. fi
  4718. after="`echo "${usb_devices}" | ${cutbin} -d: -f1,2`"
  4719. for device in ${after}
  4720. do
  4721. newdevice=`echo "${previously}" | ${grepbin} "^${device}"`
  4722. if [ "a${newdevice}" = "a" ]; then
  4723. export NEWIDS="${device}"
  4724. debug "New device \"%s\" appeared.\n" "${NEWIDS}"
  4725. unset device; unset after; unset previously
  4726. return 0
  4727. fi
  4728. done
  4729. unset device
  4730. counter=`expr ${counter} + 1`; counter=`echo ${counter}`
  4731. [ "${counter}" -lt "20" ] && debug "Waiting a second for device to appear.\n" && sleep 1
  4732. done
  4733. debug "Giving up waiting for new device to appear after %d seconds.\n" "${counter}"
  4734. unset after; unset previously; unset counter
  4735. return 1
  4736. }
  4737.  
  4738. # Detects usable interface of device $1 and sets USB_INTERFACE variable. Device will get switched if required.
  4739. # Callers should check NEWIDS variable
  4740. modem_usb_detect_int() {
  4741. [ "a$1" = "a" ] && return 99
  4742. usb_get_interface "$1"; ret=$?
  4743. case "${ret}" in
  4744. 7)
  4745. debug "Failed to get USB interface.\n"
  4746. return 7
  4747. ;;
  4748. 98)
  4749. debug "Failed to get USB interface, by user request.\n"
  4750. return 98
  4751. ;;
  4752. 8)
  4753. debug "Failed to get an interface for device.\n"
  4754. if modeswitch_is_switchable "$1"; then
  4755. if [ "a$2" != "aNOSWITCH" ]; then
  4756. debug "This is normal, device \"%s\" is switchable.\n" "$1"
  4757. modeswitch_switch "$1"
  4758. ret=$?
  4759. if [ "${ret}" -eq "0" -a "a${NEWIDS}" != "a" -a "a$2" != "aNOSWITCH" ]; then
  4760. debug "Device \"%s\" is now switched to \"%s\". Will attempt to setup this one instead.\n" "$1" "${NEWIDS}"
  4761. modem_usb_detect_int "${NEWIDS}" "NOSWITCH"
  4762. ret=$?
  4763. return ${ret}
  4764. else
  4765. debug "Failed to switch device \"%s\".\n" "$1"
  4766. return ${ret}
  4767. fi
  4768. else
  4769. debug "However, device \"%s\" is already switched.\n" "$1"
  4770. return 8
  4771. fi
  4772. fi
  4773. debug "Failed setting up USB modem \"%s\".\n" "$1"
  4774. return 8
  4775. ;;
  4776. 0)
  4777. debug "Got valid USB interface %d of USB modem \"%s\".\n" "${USB_INTERFACE}" "$1"
  4778. return 0
  4779. ;;
  4780. *)
  4781. debug "Unknown error %d while retrieving interface number of USB Device.\n" "${ret}"
  4782. return ${ret}
  4783. ;;
  4784. esac
  4785. }
  4786.  
  4787. # Sets USBTTY one way or another
  4788. modem_usb_locate_tty() {
  4789. verbose "Locating tty"
  4790. lcounter=0
  4791. while ! usb_sysfs_locate_tty "$1" "$2" "$3"
  4792. do
  4793. lcounter=`expr ${lcounter} + 1`; lcounter=`echo ${lcounter}`
  4794. if [ "${lcounter}" -le "20" ]; then
  4795. if [ "a$3" != "a" ]; then
  4796. debug "Waiting for driver %s to create tty of device %s (interface #%d). [%d second(s) will have pass]\n" "$3" "$1" "$2" "${lcounter}"
  4797. else
  4798. debug "Waiting for tty of device %s (interface #%d). [%d second(s) will have pass]\n" "$1" "$2" "${lcounter}"
  4799. fi
  4800. sleep 1
  4801. else
  4802. if [ "a$3" != "a" ]; then
  4803. debug "Driver %s failed to create a tty node for interface #%d of device %s.\n" "$3" "$2" "$1"
  4804. else
  4805. debug "No tty node created for interface #%d of device %s.\n" "$2" "$1"
  4806. fi
  4807. debug "Failed to locate tty device of interface #%d from device \"%s\".\n" "$2" "$1"
  4808. unset lcounter
  4809. return 8
  4810. fi
  4811. done
  4812. return 0
  4813. }
  4814.  
  4815. modem_usb_fix_driver() {
  4816. [ "a$1" = "a" -o "a$2" = "a" ] && return 99
  4817. usb_stabilize "$1"
  4818. # Make sure storage part (if exists), is settled before
  4819. # we start playing with drivers. This should eliminate
  4820. # cases that appeared on 2.6.31+ kernels.
  4821. usb_storage_settled "$1"
  4822. # Retrieve loaded driver
  4823. usbdriver=`usb_loaded_driver "$1" "$2" | ${tailbin} -1`
  4824. if [ "a${TIMEOUTOCCURED}" != "a" ]; then
  4825. show_fmt_error "Seems like an electrical/USB bus error occured. You may need to replug your modem for it to properly work.\n"
  4826. unset TIMEOUTOCCURED
  4827. fi
  4828. # Check value
  4829. if [ "a${usbdriver}" = "a" -o "a${usbdriver}" = "aFAIL" ]; then
  4830. show_fmt_error "Failed to detect driver of interface %d.\n" "$2"
  4831. unset usbdriver
  4832. return 7
  4833. elif [ "a${usbdriver}" = "aNONE" ]; then
  4834. debug "No driver attached to interface #%d of \"%s\".\n" "$2" "$1"
  4835. else
  4836. debug "Found already loaded driver \"%s\".\n" "${usbdriver}"
  4837. fi
  4838. # If driver mismatch, unload wrong driver
  4839. if [ "a${USBDRIVER}" != "a" -a "a${usbdriver}" != "a" -a "a${usbdriver}" != "a${USBDRIVER}" -a "a${usbdriver}" != "aNONE" ]; then
  4840. debug "USBDRIVER variable instructs to use \"%s\" instead.\n" "${USBDRIVER}"
  4841. if ! module_unbind "${usbdriver}" "$1" "$2"; then
  4842. show_fmt_error "Failed to detach wrong driver \"%s\". Please do it yourself and retry.\n" "${usbdriver}"
  4843. unset usbdriver
  4844. return 9
  4845. fi
  4846. usbdriver="NONE"
  4847. fi
  4848. # If no driver, attempt to detect one
  4849. [ "a${USBDRIVER}" = "a" -a "a${usbdriver}" != "aNONE" ] && USBDRIVER="${usbdriver}"
  4850. if [ "a${USBDRIVER}" = "a" -a "a${usbdriver}" = "aNONE" ]; then
  4851. if [ "a${USB_INTERFACE}" != "a" ]; then
  4852. usb_serial_detect_driver "$1" "$2"
  4853. if [ "a${USB_INTERFACE}" = "a" ]; then
  4854. # "usb_serial_detect_driver" call above, called "usb_usable_interfaces" which reset USB_INTERFACE
  4855. USB_INTERFACE="$2" ; export USB_INTERFACE
  4856. debug "Interface of device \"%s\" is still #%d.\n" "$1" "${USB_INTERFACE}"
  4857. fi
  4858. else
  4859. usb_serial_detect_driver "$1" "$2"
  4860. fi
  4861. fi
  4862. # We should know a driver now
  4863. if [ "a${USBDRIVER}" = "a" ]; then
  4864. unset usbdriver
  4865. show_fmt_error "Unable to locate driver to use for device \"%s\"" "$1"
  4866. return 10
  4867. fi
  4868. # Load driver (if required)
  4869. if [ "${USBDRIVER}" = "${usbdriver}" ]; then
  4870. debug "Driver \"%s\" already attached to interface #%d of device \"%s\".\n" "${USBDRIVER}" "$2" "$1"
  4871. unset usbdriver
  4872. return 0
  4873. elif module_bind "${USBDRIVER}" "$1" "$2"; then
  4874. debug "Driver \"%s\" loaded for interface #%d of device \"%s\".\n" "${USBDRIVER}" "$2" "$1"
  4875. unset usbdriver
  4876. return 0
  4877. else
  4878. show_fmt_error "Unable to load driver \"%s\" for device \"%s\"" "${USBDRIVER}" "$1"
  4879. unset usbdriver
  4880. return 11
  4881. fi
  4882. }
  4883.  
  4884. modem_usb() {
  4885. unset MODEM_TTY
  4886. [ "a$1" = "a" ] && return 99
  4887. ! usb_device_connected "$1" && debug "Device \"%s\" not found.\n" "$1" && return 7
  4888. export usbdevice="$1"
  4889.  
  4890. # Find appropriate interface, switching if required
  4891. modem_usb_detect_int "${usbdevice}"
  4892. ret=$?; [ "${ret}" -ne "0" ] && unset usbdevice && return ${ret}
  4893. # Check if device is switched
  4894. [ "a${NEWIDS}" != "a" ] && export usbdevice="${NEWIDS}"
  4895. # Load driver
  4896. modem_usb_fix_driver "${usbdevice}" "${USB_INTERFACE}"
  4897. ret=$?; [ "${ret}" -ne "0" ] && unset usbdevice && return ${ret}
  4898. # Locate tty
  4899. modem_usb_locate_tty "${usbdevice}" "${USB_INTERFACE}" "${USBDRIVER}"
  4900. ret=$?; [ "${ret}" -ne "0" ] && unset usbdevice && return ${ret}
  4901. if [ "a${USBTTY}" != "a" ]; then
  4902. if [ -c "${USBTTY}" ]; then
  4903. debug "Nothing more to do for setting it up device \"%s\".\n" "$1"
  4904. export MODEM_TTY="${USBTTY}"
  4905. return 0
  4906. fi
  4907. fi
  4908. unset USB_INTERFACE
  4909. unset USBTTY
  4910. unset MODEM_TTY
  4911. unset usbdevice
  4912. return 99
  4913. }
  4914.  
  4915. modem_bluetooth() {
  4916. unset MODEM_TTY
  4917. [ "a$1" = "a" -o "a$2" = "a" ] && return 99
  4918. debug "Attempting to setup bluetooth modem \"%s\" on channel \"%s\".\n" "$1" "$2"
  4919. bluetooth_setup_rfcomm "$1" "$2"
  4920. ret=$?
  4921. if [ "a${ret}" = "a0" ]; then
  4922. if [ "a${RFCOMM_TTY}" = "a" ]; then
  4923. debug "No RFCOMM tty returned.\n"
  4924. elif [ ! -c "${RFCOMM_TTY}" ]; then
  4925. debug "RFCOMM tty returned (%s), does exist.\n" "${RFCOMM_TTY}"
  4926. else
  4927. debug "Nothing more to do for setting it up %s(channel #%d).\n" "$1" "$2"
  4928. export MODEM_TTY="${RFCOMM_TTY}"
  4929. return 0
  4930. fi
  4931. fi
  4932. return 8
  4933. }
  4934.  
  4935. modem_custom() {
  4936. [ "a$1" = "a" ] && return 99
  4937. debug "Attempting to setup custom tty modem on \"%s\".\n" "$1"
  4938. if [ -c "$1" ]; then
  4939. debug "Custom tty \"%s\" exists. Nothing more to do for setting it up.\n" "$1"
  4940. export MODEM_TTY="$1"
  4941. return 0
  4942. else
  4943. debug "Custom tty \"%s\" does not exist. Unable to proceed.\n" "$1"
  4944. show_fmt_error "Device node \"%s\" does not exist. Setup failed." "$1"
  4945. unset MODEM_TTY
  4946. return 8
  4947. fi
  4948. }
  4949.  
  4950. modem_setup() {
  4951. unset MODEM_TTY
  4952. debug "Setting up modem.\n"
  4953. ! we_are_root && return 3
  4954. [ "a${MODEM}" = "a" ] && ! flow_select_modem "$@" && return 98
  4955. if [ "a${MODEM}" = "aOTHER" ]; then
  4956. [ "a${OTHER}" = "a" ] && ! flow_other_modem "$@" && return 98
  4957. if [ "a${OTHER}" = "aUSBMODEM" ]; then
  4958. [ "a${USBMODEM}" = "a" ] && ! flow_usb_modem "$@" && return 98
  4959. if [ "a${USBMODEM}" != "a" ]; then
  4960. debug "Setting up USB modem %s.\n" "${USBMODEM}"
  4961. modem_usb "${USBMODEM}" "$@"
  4962. ret=$?
  4963. [ "a${usbdevice}" != "a" -a "a${usbdevice}" = "a${NEWIDS}" ] && export USBMODEM="${USBMODEM} ${usbdevice}"
  4964. return ${ret}
  4965. fi
  4966. elif [ "a${OTHER}" = "aBLUETOOTH" ]; then
  4967. [ "a${BLUETOOTH}" = "a" ] && ! flow_blue_modem "$@" && return 98
  4968. if [ "a${BLUETOOTH}" = "aUNDISCOVERABLE" ]; then
  4969. [ "a${UNDISCOVERABLE}" = "a" ] && ! flow_undiscoverable "$@" && return 98
  4970. if [ "a${UNDISCOVERABLE}" != "a" ]; then
  4971. [ "a${RFSERVICE}" = "a" ] && ! flow_rfchannel "$@" && return 98
  4972. if [ "a${RFSERVICE}" = "aRFCHANNEL" ]; then
  4973. [ "a${RFCHANNEL}" = "a" ] && ! flow_manual_rfcomm "$@" && return 98
  4974. if [ "a${RFCHANNEL}" != "a" ]; then
  4975. debug "Setting up Bluetooth modem %s on channel %s.\n" "${UNDISCOVERABLE}" "${RFCHANNEL}"
  4976. modem_bluetooth "${UNDISCOVERABLE}" "${RFCHANNEL}" "$@"
  4977. return $?
  4978. fi
  4979. elif [ "a${RFSERVICE}" != "a" ]; then
  4980. debug "Setting up Bluetooth modem %s on channel %s.\n" "${UNDISCOVERABLE}" "${RFSERVICE}"
  4981. modem_bluetooth "${UNDISCOVERABLE}" "${RFSERVICE}" "$@"
  4982. return $?
  4983. fi
  4984. fi
  4985. elif [ "a${BLUETOOTH}" != "a" ]; then
  4986. [ "a${RFSERVICE}" = "a" ] && ! flow_rfchannel "$@" && return 98
  4987. if [ "a${RFSERVICE}" = "aRFCHANNEL" ]; then
  4988. [ "a${RFCHANNEL}" = "a" ] && ! flow_manual_rfcomm "$@" && return 98
  4989. if [ "a${RFCHANNEL}" != "a" ]; then
  4990. debug "Setting up Bluetooth modem %s on channel %s.\n" "${BLUETOOTH}" "${RFCHANNEL}"
  4991. modem_bluetooth "${BLUETOOTH}" "${RFCHANNEL}" "$@"
  4992. return $?
  4993. fi
  4994. elif [ "a${RFSERVICE}" != "a" ]; then
  4995. debug "Setting up Bluetooth modem %s on channel %s.\n" "${BLUETOOTH}" "${RFSERVICE}"
  4996. modem_bluetooth "${BLUETOOTH}" "${RFSERVICE}" "$@"
  4997. return $?
  4998. fi
  4999. fi
  5000. elif [ "a${OTHER}" = "aCUSTOM_TTY" ]; then
  5001. [ "a${CUSTOM_TTY}" = "a" ] && ! flow_custom_tty "$@" && return 98
  5002. if [ "a${CUSTOM_TTY}" != "a" ]; then
  5003. debug "Setting up custom modem on %s.\n" "${CUSTOM_TTY}"
  5004. modem_custom "${CUSTOM_TTY}" "$@"
  5005. return $?
  5006. fi
  5007. fi
  5008. elif [ "a${MODEM}" != "a" ]; then
  5009. debug "Setting up USB modem %s.\n" "${MODEM}"
  5010. modem_usb "${MODEM}" "$@"
  5011. ret=$?
  5012. [ "a${usbdevice}" != "a" -a "a${usbdevice}" = "a${NEWIDS}" ] && export MODEM="${MODEM} ${usbdevice}"
  5013. return ${ret}
  5014. fi
  5015. debug "Failed to setup any modem.\n"
  5016. return 99
  5017. }
  5018.  
  5019. # Gets entries from file $1 that start with $2:$3
  5020. base_fetch() {
  5021. unset BASERESULT
  5022. [ "a$1" = "a" ] && return 1
  5023. [ "a${PROVIDER}" = "a" ] && debug "Unable to locate package provider.\n" && return 1
  5024. [ ! -x "${PROVIDER}" ] && debug "Unable to execute package provider.\n" && return 1
  5025. baseentries=`"${PROVIDER}" getfile "$1" | ${grepbin} -v "^#" | ${grepbin} -i "^$2:"`
  5026. if [ "a${baseentries}" = "a" ]; then
  5027. unset baseentries
  5028. debug "No information found for %s within %s database.\n" "$2" "$1"
  5029. return 1
  5030. fi
  5031. [ "a$3" != "a" ] && BASERESULT=`echo "${baseentries}" | ${grepbin} -i "^$2:$3:"`
  5032. [ "a${BASERESULT}" = "a" ] && BASERESULT=`echo "${baseentries}" | ${grepbin} -i "^$2::"`
  5033. export BASERESULT
  5034. unset baseentries
  5035. if [ "a${BASERESULT}" = "a" ]; then
  5036. unset BASERESULT
  5037. [ "a$3" != "a" ] && debug "No information found for \"%s:%s:\" within %s.\n" "$2" "$3" "$1"
  5038. [ "a$3" = "a" ] && debug "No default entry \"%s::\" found within %s.\n" "$2" "$1"
  5039. return 1
  5040. fi
  5041. debug "Loaded entries from %s:\n%s\n" "$1" "${BASERESULT}"
  5042. return 0
  5043. }
  5044.  
  5045. base_drivers() {
  5046. unset basedrivercandidates; unset baseintercandidates
  5047. [ "a$1" = "a" ] && return 1
  5048. ! base_fetch "files/usb_devices.db" "$1" "$2" && return 1
  5049. [ "a${BASERESULT}" = "a" ] && return 1
  5050. BASERESULT=`echo "${BASERESULT}" | ${headbin} -1`
  5051. basedrivercandidates=`echo "${BASERESULT}" | ${cutbin} -f2 -s`
  5052. baseintercandidates=`echo "${BASERESULT}" | ${cutbin} -f3 -s`
  5053. [ "a${basedrivercandidates}" != "a" ] && debug "Device database indicates to use \"%s\" driver(s) for device %s.\n" "${basedrivercandidates}" "$1"
  5054. [ "a${baseintercandidates}" != "a" ] && debug "Device database indicates to use interface #%d for device %s.\n" "${baseintercandidates}" "$1"
  5055. unset BASERESULT
  5056. return 0
  5057. }
  5058.  
  5059. # Sets INIT_STAGES of modem $1 with optional name $2.
  5060. # Parses files/modem_init.db.
  5061. base_modem() {
  5062. unset INIT_STAGE0; unset INIT_STAGE1; unset INIT_STAGE2; unset INIT_STAGE3; unset INIT_STAGE4; unset INIT_STAGE5; unset INIT_STAGE6; unset INIT_STAGE7; unset INIT_STAGE8;
  5063. [ "a$1" = "a" -a "a$2" = "a" ] && return 1
  5064. ! base_fetch "files/modem_init.db" "$1" "$2" && return 1
  5065. [ "a${BASERESULT}" = "a" ] && return 1
  5066. BASERESULT=`echo "${BASERESULT}" | ${headbin} -1`
  5067. for item in 0 1 2 3 4 5 6 7 8
  5068. do
  5069. plusone=`expr ${item} + 1`; plusone=`echo ${plusone}`
  5070. if [ "${item}" -le "7" -a "${item}" -ge "1" ]; then
  5071. contents=`${printfbin} "%s\n" "${BASERESULT}" | ${cutbin} -f${plusone}`
  5072. elif [ "${item}" -eq "8" ]; then
  5073. contents=`${printfbin} "%s\n" "${MODEM_INIT}"`
  5074. elif [ "${item}" -eq "0" ]; then
  5075. contents=`${printfbin} "%s\n" "${MODEM_PREPARE}"`
  5076. fi
  5077. if [ "a${contents}" != "a" ]; then
  5078. replacement=`${printfbin} "%s\n" "${contents}" | ${sedbin} -e "s/AT/\\nAT/g" | ${sedbin} -e "s/ *$//g" | ${grepbin} -v "^$" | ${grepbin} "^AT" | ${sedbin} -e "s/^\(.*\)$/\\\'\1\\\' OK/g" | ${trbin} "\n" " "`
  5079. eval "INIT_STAGE${item}=\${replacement}"
  5080. eval export INIT_STAGE${item}
  5081. unset replacement
  5082. if [ "a$1" != "a" ]; then
  5083. if [ "a$2" != "a" ]; then
  5084. debug "Loaded INIT Stage #%d for %s(%s): %s\n" "${item}" "$1" "$2" "`eval echo \\"\\${INIT_STAGE${item}}\\"`"
  5085. else
  5086. debug "Loaded INIT Stage #%d for %s: %s\n" "${item}" "$1" "`eval echo \\"\\${INIT_STAGE${item}}\\"`"
  5087. fi
  5088. else
  5089. debug "Loaded INIT Stage #%d for %s: %s\n" "${item}" "$2" "`eval echo \\"\\${INIT_STAGE${item}}\\"`"
  5090. fi
  5091. else
  5092. eval unset INIT_STAGE${item}
  5093. fi
  5094. unset contents; unset plusone
  5095. done
  5096. debug run_command "set | ${grepbin} \"^INIT_STAGE\""
  5097. unset item
  5098. return 0
  5099. }
  5100.  
  5101. # Sets NETINFO of ISP $1 with optional name $2. If $1==$ISPID sets ISP_ fields also
  5102. # Parses files/operators.db.
  5103. base_net() {
  5104. unset NETINFO
  5105. [ "a$1" = "a" ] && return 1
  5106. ! base_fetch "files/operators.db" "$1" "$2" && return 1
  5107. [ "a${BASERESULT}" = "a" ] && return 1
  5108. BASERESULT=`echo "${BASERESULT}" | ${headbin} -1`
  5109. ISP_NAME=`echo "${BASERESULT}" | ${cutbin} -f2`
  5110. ISP_PRODUCT=`echo "${BASERESULT}" | ${cutbin} -f3`
  5111. ISP_FGCOLOR=`echo "${BASERESULT}" | ${cutbin} -f4`
  5112. ISP_BGCOLOR=`echo "${BASERESULT}" | ${cutbin} -f5`
  5113. ISP_DIAL=`echo "${BASERESULT}" | ${cutbin} -f6`
  5114. ISP_APNS=`echo "${BASERESULT}" | ${cutbin} -f7 | ${trbin} " " "\n"`
  5115. ISP_ICON=`echo "${BASERESULT}" | ${cutbin} -f8`
  5116. export ISP_NAME
  5117. export ISP_PRODUCT
  5118. export ISP_FGCOLOR
  5119. export ISP_BGCOLOR
  5120. export ISP_DIAL
  5121. export ISP_APNS
  5122. export ISP_ICON
  5123. return 0
  5124. }
  5125.  
  5126. # Gathers information about ISP $1 with optional name $2
  5127. net_info() {
  5128. [ "a$1" != "a" ] && base_net "$@"
  5129. [ "a$1" != "a${ISPID}" -a "a$1" != "a" ] && return 0
  5130. [ "a${ISP_NAME}" = "a" ] && export ISP_NAME="${ISPNAME}"
  5131. [ "a${ISP_NAME}" = "a" ] && export ISP_NAME="Unknown operator ${ISPID}"
  5132. [ "a${ISPNAME}" = "a${ISPID}" -o "a${ISPNAME}" = "a" ] && export ISPNAME="${ISP_NAME}"
  5133. [ "a${ISPNAME}" = "a${ISPID}" -o "a${ISPNAME}" = "a" ] && export ISPNAME="${ISP_NAME}"
  5134. export ISPTEXT="${ISPNAME}"
  5135. [ "a${ISP_PRODUCT}" = "a" ] && export ISP_PRODUCT="${ISP_NAME} Internet"
  5136. [ "a${ISP_FGCOLOR}" = "a" ] && export ISP_FGCOLOR="ffffff"
  5137. [ "a${ISP_BGCOLOR}" = "a" ] && export ISP_BGCOLOR="000000"
  5138. [ "a${ISP_DIAL}" = "a" ] && export ISP_DIAL="*99#"
  5139. [ "a${ISP_ICON}" = "a" ] && export ISP_ICON="files/sakis3g.png"
  5140. if [ "a${CUSTOM_DIAL}" != "a" -a "a${CUSTOM_DIAL}" != "a${ISP_DIAL}" ]; then
  5141. debug "Will dial \"%s\" specified by user instead of \"%s\" found in database.\n" "${CUSTOM_DIAL}" "${ISP_DIAL}"
  5142. export ISP_DIAL="${CUSTOM_DIAL}"
  5143. fi
  5144. debug "ISPID: %s / ISPNAME: %s / ISPTEXT: %s\n" "${ISPID}" "${ISPNAME}" "${ISPTEXT}"
  5145. return 0
  5146. }
  5147.  
  5148.  
  5149. # Select a device interface
  5150. flow_select_interface() {
  5151. usbinterfaces=`echo "$2" | ${trbin} " " "\n" | ${sedbin} -e "s/^\(.*\)$/\"\1\" \"Interface #\1\"/g" | ${trbin} "\n" " "`
  5152. eval user_select \"USBINTERFACE\" \"Please appropriate interface\" \"Select modem interface of USB device that provides modem capabilities.\" \"Select\" \"Cancel\" ${usbinterfaces}
  5153. case "$?" in
  5154. 0)
  5155. unset usbinterfaces
  5156. return 98
  5157. ;;
  5158. 98)
  5159. unset usbinterfaces
  5160. return 98
  5161. ;;
  5162. 99)
  5163. unset usbinterfaces
  5164. return 99
  5165. ;;
  5166. *)
  5167. unset usbinterfaces
  5168. return 0
  5169. ;;
  5170. esac
  5171. return 99
  5172. }
  5173.  
  5174.  
  5175. # Makes sure one USB modem exists
  5176. flow_usb_modem() {
  5177. usb_connected_devices
  5178. usbmodems=`echo "${usb_devices}" | ${sedbin} -e "s/^\(....\):\(....\):\(.*\)$/\"\1:\2\" \"\3\"/g"`
  5179. eval user_select \"USBMODEM\" \"Please select USB modem\" \"Select USB device that provides modem capabilities.\" \"Select\" \"Cancel\" ${usbmodems}
  5180. case "$?" in
  5181. 0)
  5182. unset usbmodems
  5183. return 98
  5184. ;;
  5185. 98)
  5186. unset usbmodems
  5187. return 98
  5188. ;;
  5189. 99)
  5190. unset usbmodems
  5191. return 99
  5192. ;;
  5193. *)
  5194. unset usbmodems
  5195. usb_connected_devices
  5196. usbmodems=`echo "${usb_devices}" | ${sedbin} -e "s/^\(....\):\(....\):\(.*\)$/ \1:\2 /g" | ${grepbin} " ${USBMODEM} "`; usbmodems=`echo ${usbmodems}`
  5197. if [ "a${usbmodems}" = "a${USBMODEM}" ]; then
  5198. debug "User selected USB modem \"%s\".\n" "${USBMODEM}"
  5199. unset usbmodems
  5200. return 0
  5201. else
  5202. debug "USB modem \"%s\" is not currently plugged.\n" "${USBMODEM}"
  5203. unset usbmodems; unset USBMODEM
  5204. flow_usb_modem "$@"
  5205. return "$?"
  5206. fi
  5207. ;;
  5208. esac
  5209. return 99
  5210. }
  5211.  
  5212. flow_manual_rfcomm() {
  5213. user_prompt "RFCHANNEL" "Please enter RFCOMM channel" "Enter RFCOMM channel that should be used, or leave empty to abort [1-255]" "OK" "Cancel"
  5214. case "a${RFCHANNEL}" in
  5215. a)
  5216. return 98
  5217. ;;
  5218. *)
  5219. RFCHANNEL=`${printfbin} "%d\n" "${RFCHANNEL}" 2> /dev/null`
  5220. [ "a$RFCHANNEL" = "a" ] && RFCHANNEL=0
  5221. [ "$RFCHANNEL" -gt "255" ] && RFCHANNEL=0
  5222. export RFCHANNEL
  5223. if [ "$RFCHANNEL" -eq "0" ]; then
  5224. unset RFCHANNEL
  5225. return 98
  5226. fi
  5227. return 0
  5228. ;;
  5229. esac
  5230. }
  5231.  
  5232. flow_custom_tty() {
  5233. user_prompt "CUSTOM_TTY" "Please enter tty" "Enter tty node where your 3G modem resides, or leave empty to abort" "OK" "Cancel"
  5234. case "a${CUSTOM_TTY}" in
  5235. a)
  5236. unset CUSTOM_TTY
  5237. return 98
  5238. ;;
  5239. *)
  5240. [ ! -c "${CUSTOM_TTY}" ] && [ -c "/dev/${CUSTOM_TTY}" ] && CUSTOM_TTY="/dev/${CUSTOM_TTY}"
  5241. [ ! -c "${CUSTOM_TTY}" ] && [ -c "/dev${CUSTOM_TTY}" ] && CUSTOM_TTY="/dev${CUSTOM_TTY}"
  5242. if [ ! -c "${CUSTOM_TTY}" ]; then
  5243. show_fmt_error "Device node \"%s\" does not exist.\n" "${CUSTOM_TTY}"
  5244. unset CUSTOM_TTY
  5245. flow_custom_tty "$@"
  5246. return $?
  5247. else
  5248. debug "Using device node \"%s\".\n" "${CUSTOM_TTY}"
  5249. export CUSTOM_TTY
  5250. return 0
  5251. fi
  5252. ;;
  5253. esac
  5254. }
  5255.  
  5256. flow_rfchannel() {
  5257. [ "a${BLUETOOTH}" = "a" ] && ! flow_blue_modem "$@" && return $?
  5258. if [ "a${RFSERVICE}" = "aRFCHANNEL" ]; then
  5259. if ! flow_manual_rfcomm; then
  5260. unset RFSERVICE
  5261. flow_rfchannel "$@"
  5262. return "$?"
  5263. fi
  5264. return 0
  5265. fi
  5266. verbose "Seeking %s" "${BLUETOOTH}"
  5267. if bluetooth_rfcomm_channel "${BLUETOOTH}" "DUN"; then
  5268. debug "No DUN rfcomm channels returned from \"%s\".\n" "${BLUETOOTH}"
  5269. bluetooth_rfcomm_channel "${BLUETOOTH}" "SP"
  5270. rfchannels=`echo "${rfchannel}" | ${sedbin} -e "s/^\([0-9][0-9]*\)$/\"\1\" \"Serial Port from RFCOMM channel #\1\"/g"`
  5271. else
  5272. rfchannels=`echo "${rfchannel}" | ${sedbin} -e "s/^\([0-9][0-9]*\)$/\"\1\" \"Dialup Networking from RFCOMM channel #\1\"/g"`
  5273. bluetooth_rfcomm_channel "${BLUETOOTH}" "SP"
  5274. rfchannels="${rfchannels} `echo \"${rfchannel}\" | ${sedbin} -e \"s/^\\\([0-9][0-9]*\\\)$/\\\"\\\1\\\" \\\"Serial Port from RFCOMM channel #\\\1\\\"/g\"`"
  5275. fi
  5276. eval user_select \"RFSERVICE\" \"Please select RFCOMM service\" \"Select RFCOMM service of Bluetooth device that provides 3G modem capabilities.\" \"Select\" \"Cancel\" \"RESCAN\" \"Scan device again\" ${rfchannels} \"RFCHANNEL\" \"Manually enter non-discovered channel...\"
  5277. case "$?" in
  5278. 0)
  5279. unset rfchannels
  5280. return 98
  5281. ;;
  5282. 98)
  5283. unset rfchannels
  5284. return 98
  5285. ;;
  5286. 99)
  5287. unset rfchannels
  5288. return 99
  5289. ;;
  5290. *)
  5291. unset rfchannels
  5292. case "a${RFSERVICE}" in
  5293. aRESCAN)
  5294. unset RFSERVICE
  5295. flow_rfchannel "$@"
  5296. return "$?"
  5297. ;;
  5298. aRFCHANNEL)
  5299. if ! flow_manual_rfcomm; then
  5300. unset RFSERVICE
  5301. flow_rfchannel "$@"
  5302. return "$?"
  5303. fi
  5304. return 0
  5305. ;;
  5306. *)
  5307. export RFCHANNEL="${RFSERVICE}"
  5308. return 0
  5309. ;;
  5310. esac
  5311. ;;
  5312. esac
  5313. return 99
  5314.  
  5315.  
  5316. }
  5317.  
  5318. # Makes sure one UNDISCOVERABLE BLUETOOTH modem exists
  5319. flow_undiscoverable() {
  5320. user_prompt "UNDISCOVERABLE" "Enter Bluetooth address" "Enter Bluetooth address of undiscoverable device, or leave empty to abort" "OK" "Cancel"
  5321. case "a${UNDISCOVERABLE}" in
  5322. a)
  5323. return 98
  5324. ;;
  5325. *)
  5326. export BLUETOOTH="${UNDISCOVERABLE}"
  5327. if ! flow_rfchannel; then
  5328. unset UNDISCOVERABLE; unset BLUETOOTH
  5329. return 98
  5330. fi
  5331. export BLUETOOTH="UNDISCOVERABLE"
  5332. return 0
  5333. ;;
  5334. esac
  5335. }
  5336.  
  5337.  
  5338. # Makes sure one BLUETOOTH modem exists
  5339. flow_blue_modem() {
  5340. if [ "a${BLUETOOTH}" = "aUNDISCOVERABLE" ]; then
  5341. if ! flow_undiscoverable; then
  5342. unset BLUETOOTH
  5343. flow_blue_modem "$@"
  5344. return "$?"
  5345. fi
  5346. return 0
  5347. fi
  5348. bluetooth_scan_devices
  5349. bluemodems=`echo "${bluetooth_devices}" | ${sedbin} -e "s/^\(.*\) \(.*\)$/\"\1\" \"\2 (\1)\"/g"`
  5350. eval user_select \"BLUETOOTH\" \"Please select Bluetooth device\" \"Select Bluetooth device that provides 3G modem capabilities.\" \"Select\" \"Cancel\" \"RESCAN\" \"Scan devices again\" ${bluemodems} \"UNDISCOVERABLE\" \"Manually enter undiscoverable device...\"
  5351. case "$?" in
  5352. 0)
  5353. unset bluemodems
  5354. return 98
  5355. ;;
  5356. 98)
  5357. unset bluemodems
  5358. return 98
  5359. ;;
  5360. 99)
  5361. unset bluemodems
  5362. return 99
  5363. ;;
  5364. *)
  5365. unset bluemodems
  5366. case "a${BLUETOOTH}" in
  5367. aRESCAN)
  5368. unset BLUETOOTH
  5369. flow_blue_modem "$@"
  5370. return "$?"
  5371. ;;
  5372. aUNDISCOVERABLE)
  5373. if ! flow_undiscoverable; then
  5374. unset BLUETOOTH
  5375. flow_blue_modem "$@"
  5376. return "$?"
  5377. fi
  5378. return 0
  5379. ;;
  5380. *)
  5381. if ! flow_rfchannel; then
  5382. unset BLUETOOTH
  5383. flow_blue_modem "$@"
  5384. return "$?"
  5385. fi
  5386. return 0
  5387. ;;
  5388. esac
  5389. ;;
  5390. esac
  5391. return 99
  5392. }
  5393.  
  5394. # Makes sure one other modem is selected
  5395. flow_other_modem() {
  5396. user_select "OTHER" "Please select modem type" "Select modem category that best fits your 3G modem." "Select" "Cancel" "USBMODEM" "USB device" "BLUETOOTH" "Bluetooth modem" "CUSTOM_TTY" "Custom tty..."
  5397. case "$?" in
  5398. 0)
  5399. return 98
  5400. ;;
  5401. 98)
  5402. return 98
  5403. ;;
  5404. 99)
  5405. return 99
  5406. ;;
  5407. *)
  5408. case "a${OTHER}" in
  5409. aUSBMODEM)
  5410. if ! flow_usb_modem; then
  5411. unset OTHER
  5412. flow_other_modem "$@"
  5413. return "$?"
  5414. fi
  5415. return 0
  5416. ;;
  5417. aBLUETOOTH)
  5418. if ! flow_blue_modem; then
  5419. unset OTHER
  5420. flow_other_modem "$@"
  5421. return "$?"
  5422. fi
  5423. return 0
  5424. ;;
  5425. aCUSTOM_TTY)
  5426. if ! flow_custom_tty; then
  5427. unset OTHER
  5428. flow_other_modem "$@"
  5429. return "$?"
  5430. fi
  5431. return 0
  5432. ;;
  5433. *)
  5434. return 99
  5435. ;;
  5436. esac
  5437. ;;
  5438. esac
  5439. return 99
  5440. }
  5441.  
  5442.  
  5443. # Makes sure one selected modem exists
  5444. flow_select_modem() {
  5445. verbose "Locating device"
  5446. usb_connected_modems
  5447. modems=`echo "${usb_modem_devices}" | ${sedbin} -e "s/^\(....\):\(....\):\(.*\)$/\"\1:\2\" \"\3\"/g"`
  5448. if [ "a${modems}" != "a" ]; then
  5449. modemscount=`echo "${usb_modem_devices}" | ${wcbin} -l`; modemscount=`echo ${modemscount}`
  5450. if [ "a${modemscount}" = "a1" -a "a${MODEM}" = "a" ]; then
  5451. MODEM=`echo "${usb_modem_devices}" | ${headbin} -1 | ${sedbin} -e "s/^\(....\):\(....\):\(.*\)$/\1:\2/g"`
  5452. export MODEM
  5453. debug "Autoselecting unique USB modem plugged: %s\n" "${MODEM}"
  5454. ret=1
  5455. else
  5456. eval user_select \"MODEM\" \"Please select modem\" \"Select modem that will be used for establishing 3G connection.\" \"Select\" \"Cancel\" ${modems} \"OTHER\" \"Other...\"
  5457. # in
  5458. ret=$?
  5459. fi
  5460. unset modemscount
  5461. else
  5462. export MODEM="OTHER"
  5463. ret=97
  5464. fi
  5465. case "${ret}" in
  5466. 0)
  5467. return 98
  5468. ;;
  5469. 98)
  5470. return 98
  5471. ;;
  5472. 99)
  5473. return 99
  5474. ;;
  5475. *)
  5476. case "a${MODEM}" in
  5477. aOTHER)
  5478. if [ "${ret}" -eq "97" ]; then
  5479. flow_other_modem "$@"
  5480. ret=$?
  5481. [ "${ret}" -ne "0" ] && unset MODEM
  5482. return ${ret}
  5483. else
  5484. if ! flow_other_modem "$@"; then
  5485. unset MODEM
  5486. flow_select_modem "$@"
  5487. return "$?"
  5488. fi
  5489. fi
  5490. return 0
  5491. ;;
  5492. a)
  5493. return 98
  5494. ;;
  5495. *)
  5496. return 0
  5497. ;;
  5498. esac
  5499. return 99
  5500. ;;
  5501. esac
  5502. return 0
  5503. }
  5504.  
  5505. flow_custom_apn() {
  5506. user_prompt "CUSTOM_APN" "Please enter APN" "Enter correct APN, or leave empty to abort. Contact your operator if unsure" "OK" "Cancel"
  5507. case "a${CUSTOM_APN}" in
  5508. a)
  5509. unset CUSTOM_APN
  5510. return 98
  5511. ;;
  5512. *)
  5513. export CUSTOM_APN
  5514. return 0
  5515. ;;
  5516. esac
  5517. }
  5518.  
  5519.  
  5520. # Makes selected APN exists
  5521. flow_select_apn() {
  5522. net_info "${ISPID}" "${ISPNAME}"
  5523. [ "a${FORCE_APN}" != "a" ] && export ISP_APNS="${FORCE_APN}"
  5524. [ "a${ISP_APNS}" = "a" ] && tty_detect_apn && export ISP_APNS="${MODEM_APN}"
  5525. if [ "a${ISP_APNS}" != "a" -a "a${FORCE_APN}" = "a" ]; then
  5526. apnoptions=""
  5527. for apnoption in ${ISP_APNS}
  5528. do
  5529. apnname=`echo "${apnoption}" | ${cutbin} -d: -f1 -s`
  5530. if [ "a${apnname}" = "a" ]; then
  5531. apnname="${apnoption}"
  5532. apndesc="${apnoption}"
  5533. apnuser="" ; apnpass=""
  5534. else
  5535. apndesc=`echo "${apnoption}" | ${cutbin} -d: -f2 | ${sedbin} -e "s/_/ /g"`
  5536. apndesc="${apndesc} (${apnname})"
  5537. apnuser=`echo "${apnoption}" | ${cutbin} -d: -f3`
  5538. apnpass=`echo "${apnoption}" | ${cutbin} -d: -f4`
  5539. fi
  5540. if [ "a${apnname}" = "a${MODEM_APN}" -a "a${MODEM_APN}" != "a" ]; then
  5541. apndesc=`format_text "Reported by your modem (%s)" "${apnname}"`
  5542. fi
  5543. apnoptions="${apnoptions} \"${apnname}\" \"${apndesc}\""
  5544. unset apnname; unset apndesc; unset apnuser; unset apnpass
  5545. done
  5546. unset apnoption
  5547. eval user_select \"APN\" \"Please select APN\" \"Select APN that best describes your connection. Contact your operator if unsure. This information, along with APN username and password, is usually easily retrieved through a fast call to customer support\" \"Select\" \"Cancel\" ${apnoptions} \"CUSTOM_APN\" \"Custom APN...\"
  5548. ret=$?
  5549. unset apnoptions
  5550. elif [ "a${FORCE_APN}" != "a" ]; then
  5551. debug "Selected APN by FORCE_APN: %s\n" "${FORCE_APN}"
  5552. APN=`echo "${FORCE_APN}" | ${cutbin} -d: -f1`
  5553. export APN
  5554. ret=1
  5555. else
  5556. export APN="CUSTOM_APN"
  5557. ret=97
  5558. fi
  5559. case "${ret}" in
  5560. 0)
  5561. return 98
  5562. ;;
  5563. 98)
  5564. return 98
  5565. ;;
  5566. 99)
  5567. return 99
  5568. ;;
  5569. *)
  5570. case "a${APN}" in
  5571. aCUSTOM_APN)
  5572. if [ "${ret}" -eq "97" ]; then
  5573. if ! flow_custom_apn "$@"; then
  5574. unset APN
  5575. return 98
  5576. fi
  5577. else
  5578. if ! flow_custom_apn "$@"; then
  5579. unset APN
  5580. flow_select_apn "$@"
  5581. return "$?"
  5582. fi
  5583. fi
  5584. return 0
  5585. ;;
  5586. a)
  5587. return 98
  5588. ;;
  5589. *)
  5590. APN=`echo "${ISP_APNS}" | ${grepbin} "^${APN}\(:*\)" | ${cutbin} -d: -f1`
  5591. export APN
  5592. if [ "a${APN}" = "a" ]; then
  5593. unset APN
  5594. flow_select_apn "$@"
  5595. return "$?"
  5596. else
  5597. apnuser=`echo "${ISP_APNS}" | ${grepbin} "^${APN}\(:*\)" | ${cutbin} -d: -f3 -s`
  5598. [ "a${apnuser}" != "a" -a "a${APN_USER}" = "a" ] && export APN_USER="${apnuser}"
  5599. apnpass=`echo "${ISP_APNS}" | ${grepbin} "^${APN}\(:*\)" | ${cutbin} -d: -f4 -s`
  5600. [ "a${apnuser}" != "a" -a "a${APN_PASS}" = "a" ] && export APN_PASS="${apnpass}"
  5601. unset apnuser; unset apnpass
  5602. fi
  5603. return 0
  5604. ;;
  5605. esac
  5606. return 99
  5607. ;;
  5608. esac
  5609. return 0
  5610. }
  5611.  
  5612. flow_apn_user() {
  5613. [ "a${APN}" = "a" ] && return 98
  5614. if [ "a${APN_USER}" = "a" ]; then
  5615. if [ "a${APN}" != "a" ]; then
  5616. if [ "a${APN}" = "aCUSTOM_APN" -a "a${CUSTOM_APN}" != "a" ]; then
  5617. APN_USER=`echo "${CUSTOM_APN}" | ${cutbin} -d: -s -f3`
  5618. else
  5619. APN_USER=`echo "${APN}" | ${cutbin} -d: -s -f3`
  5620. fi
  5621. fi
  5622. fi
  5623. [ "a${APN_USER}" = "a" ] && unset APN_USER
  5624. if [ "a${APN}" = "aCUSTOM_APN" -a "a${CUSTOM_APN}" != "a" ]; then
  5625. apntitle=`echo "${CUSTOM_APN}" | ${cutbin} -d: -f1`
  5626. elif [ "a${APN}" != "a" ]; then
  5627. apntitle=`echo "${APN}" | ${cutbin} -d: -f1`
  5628. fi
  5629. user_prompt "APN_USER" "APN: ${apntitle}" "Enter username required by APN, or leave empty to abort. Contact your operator if unsure. This information, along with APN password, is usually easily retrieved through a fast call to customer support" "OK" "Cancel"
  5630. case "a${APN_USER}" in
  5631. a)
  5632. unset APN_USER
  5633. return 98
  5634. ;;
  5635. *)
  5636. export APN_USER
  5637. return 0
  5638. ;;
  5639. esac
  5640. }
  5641.  
  5642. flow_apn_pass() {
  5643. [ "a${APN}" = "a" ] && return 98
  5644. if [ "a${APN_PASS}" = "a" ]; then
  5645. if [ "a${APN}" != "a" ]; then
  5646. if [ "a${APN}" = "aCUSTOM_APN" -a "a${CUSTOM_APN}" != "a" ]; then
  5647. APN_PASS=`echo "${CUSTOM_APN}" | ${cutbin} -d: -s -f4`
  5648. else
  5649. APN_PASS=`echo "${APN}" | ${cutbin} -d: -s -f4`
  5650. fi
  5651. fi
  5652. fi
  5653. [ "a${APN_PASS}" = "a" ] && unset APN_PASS
  5654. if [ "a${APN}" = "aCUSTOM_APN" -a "a${CUSTOM_APN}" != "a" ]; then
  5655. apntitle=`echo "${CUSTOM_APN}" | ${cutbin} -d: -f1`
  5656. elif [ "a${APN}" != "a" ]; then
  5657. apntitle=`echo "${APN}" | ${cutbin} -d: -f1`
  5658. fi
  5659. user_prompt "APN_PASS" "APN: ${apntitle}" "Enter password required by APN, or leave empty to abort. Contact your operator if unsure. This information is usually easily retrieved through a fast call to customer support" "OK" "Cancel"
  5660. case "a${APN_PASS}" in
  5661. a)
  5662. unset APN_PASS
  5663. return 98
  5664. ;;
  5665. *)
  5666. export APN_PASS
  5667. return 0
  5668. ;;
  5669. esac
  5670. }
  5671.  
  5672. flow_switch() {
  5673. flow_select_modem "$@"
  5674. ret=$?; [ "${ret}" -ne "0" ] && return ${ret}
  5675. unset NEWIDS
  5676. if modeswitch_is_switchable "${MODEM}" && usb_device_connected "${MODEM}"; then
  5677. modeswitch_switch "${MODEM}"
  5678. ret=$?
  5679. if [ "a${NEWIDS}" != "a" -a "a${NEWIDS}" != "a${MODEM}" ]; then
  5680. debug "Device changed ID. From \"%s\" to \"%s\".\n" "${MODEM}" "${NEWIDS}"
  5681. export MODEM="${NEWIDS}"
  5682. fi
  5683. localdev="${MODEM}"
  5684. elif modeswitch_is_switchable "${USBMODEM}" && usb_device_connected "${USBMODEM}"; then
  5685. modeswitch_switch "${USBMODEM}"
  5686. ret=$?
  5687. if [ "a${NEWIDS}" != "a" -a "a${NEWIDS}" != "a${USBMODEM}" ]; then
  5688. debug "Device changed ID. From \"%s\" to \"%s\".\n" "${USBMODEM}" "${NEWIDS}"
  5689. export USBMODEM="${NEWIDS}"
  5690. fi
  5691. localdev="${USBMODEM}"
  5692. else
  5693. debug "Currently selected modem cannot be switched. No need to switch anything.\n"
  5694. return 0
  5695. fi
  5696. # Should normally unlock HAL here. But we don't.
  5697. # If we will stay within program, it will get unlocked during driver setup.
  5698. # If stand-alone action, exit trap will unlock it.
  5699. # So, line below should be commented out.
  5700. #hal_unlock
  5701. return ${ret}
  5702. }
  5703.  
  5704. flow_setup() {
  5705. flow_select_modem "$@"
  5706. ret=$?; [ "${ret}" -ne "0" ] && return ${ret}
  5707. if modem_setup "$@"; then
  5708. debug "Modem is now setup and resides on %s.\n" "${MODEM_TTY}"
  5709. return 0
  5710. else
  5711. return $?
  5712. fi
  5713. }
  5714.  
  5715. flow_prepare() {
  5716. flow_setup "$@"
  5717. ret=$?; [ "${ret}" -ne "0" ] && return ${ret}
  5718. if tty_prepare "${MODEM_TTY}" "$@"; then
  5719. debug "Device %s is now prepared and registered to %s.\n" "${MODEM_TTY}" "${ISPTEXT}"
  5720. else
  5721. return $?
  5722. fi
  5723. }
  5724.  
  5725. pppd_config() {
  5726. need_binary "pppd"
  5727. need_arg "PPPD_OPTIONS"; need_arg "BAUD"
  5728. unset CONNECTION_CONF; unset CONNECTION_COMMAND
  5729. ${touchbin} "/tmp/pppd.tmp.$$"
  5730. if [ ! -w "/tmp/pppd.tmp.$$" ]; then
  5731. show_fmt_error "Unable to create temporary pppd config file within %s directory.\n" "/tmp"
  5732. return 99
  5733. fi
  5734. CONNECTION_CONF="/tmp/pppd.tmp.$$"
  5735. find_binary "chmod" && ${chmodbin} 600 "${CONNECTION_CONF}"
  5736. ${catbin} > "${CONNECTION_CONF}" <<endl
  5737. ABORT "NO CARRIER"
  5738. ABORT "NO DIALTONE"
  5739. ABORT "BUSY"
  5740. ABORT "ERROR"
  5741. ABORT "NO ANSWER"
  5742. "" ATZ
  5743. endl
  5744. #initstrings=`echo "${INIT_COMMANDS}" "ATDT${ISP_DIAL}" | ${sedbin} -e "s/AT/\\nAT/g" | ${grepbin} "^AT" | ${sedbin} -e "s/ *$//g" | ${grepbin} -v "^$" | ${sedbin} -e "s/^\(.*\)$/OK '\1'/g"`
  5745. initstrings=`echo "${DIAL_COMMANDS}" | ${sedbin} -e "s/AT/\\nAT/g" | ${grepbin} "^AT" | ${sedbin} -e "s/ *$//g" | ${grepbin} -v "^$" | ${sedbin} -e "s/^\(.*\)$/OK '\1'/g"`
  5746. debug "Init strings are:\n%s\n" "${initstrings}"
  5747. echo "${initstrings}" >> "${CONNECTION_CONF}"
  5748. unset initstrings
  5749. if [ ! -s "${CONNECTION_CONF}" ]; then
  5750. ${rmbin} -f "${CONNECTION_CONF}"
  5751. show_fmt_error "Failure to write on %s.\n" "${CONNECTION_CONF}"
  5752. unset CONNECTION_CONF
  5753. return 99
  5754. fi
  5755. debug "Config file that will be used is: \"%s\".\n" "${CONNECTION_CONF}"
  5756. debug show_file "${CONNECTION_CONF}"
  5757. pppdoptions="${PPPD_OPTIONS}"
  5758. [ "a${pppdoptions}" = "a" ] && pppdoptions="modem crtscts -detach defaultroute dump noipdefault usepeerdns usehostname ktune logfd 2 noauth name sakis3g lock maxfail 3"
  5759. if [ "a${PPPD_PEERS}" != "a" ]; then
  5760. if [ -d "${PPPD_PEERS}" ]; then
  5761. if [ -f "${PPPD_PEERS}/sakis3g" ]; then
  5762. debug "Found peers file %s.\n" "${PPPD_PEERS}/sakis3g"
  5763. pppdoptions="-detach dump logfd 2 name sakis3gpeer maxfail 3 call sakis3g"
  5764. fi
  5765. fi
  5766. fi
  5767. export CONNECTION_COMMAND="${setsidbin} ${pppdbin} ${MODEM_TTY} ${BAUD} ${pppdoptions} connect \"${chatbin} -v -f ${CONNECTION_CONF}\" user \"${APN_USER}\" password \"${APN_PASS}\""
  5768. unset pppdoptions
  5769. debug "Connection command that will be used is: %s\n" "${CONNECTION_COMMAND}"
  5770. return 0
  5771. }
  5772.  
  5773. wvdial_config() {
  5774. need_binary "wvdial"
  5775. need_arg "BAUD"
  5776. unset CONNECTION_CONF; unset CONNECTION_COMMAND
  5777. ${touchbin} "/tmp/wvdial.tmp.$$"
  5778. if [ ! -w "/tmp/wvdial.tmp.$$" ]; then
  5779. show_fmt_error "Unable to create temporary wvdial config file within %s directory.\n" "/tmp"
  5780. return 99
  5781. fi
  5782. CONNECTION_CONF="/tmp/wvdial.tmp.$$"
  5783. find_binary "chmod" && ${chmodbin} 600 "${CONNECTION_CONF}"
  5784. ${catbin} > "${CONNECTION_CONF}" <<endl
  5785. [Dialer Defaults]
  5786. Modem = ${MODEM_TTY}
  5787. Modem Type = USB Modem
  5788. ISDN = 0
  5789. Baud = ${BAUD}
  5790. Dial Attempts = 3
  5791. Username = ${APN_USER}
  5792. Password = ${APN_PASS}
  5793. Phone = ${ISP_DIAL}
  5794. Auto Reconnect = off
  5795. Stupid Mode = 1
  5796. endl
  5797. echo "Init1 = ATZ" >> "${CONNECTION_CONF}"
  5798. if [ ! -s "${CONNECTION_CONF}" ]; then
  5799. ${rmbin} -f "${CONNECTION_CONF}"
  5800. show_fmt_error "Failure to write on %s.\n" "${CONNECTION_CONF}"
  5801. unset CONNECTION_CONF
  5802. return 99
  5803. fi
  5804. debug "Config file that will be used is: \"%s\".\n" "${CONNECTION_CONF}"
  5805. debug show_file "${CONNECTION_CONF}"
  5806. export CONNECTION_COMMAND="${setsidbin} ${wvdialbin} --config \"${CONNECTION_CONF}\""
  5807. debug "Connection command that will be used is: %s\n" "${CONNECTION_COMMAND}"
  5808. return 0
  5809. }
  5810.  
  5811. ispconnect() {
  5812. [ "a${CONNECTION_COMMAND}" = "a" ] && return 95
  5813. if ppp_fast_status; then
  5814. debug "Already connected.\n"
  5815. return 0
  5816. fi
  5817. ! we_are_root && return 1
  5818. debug run_command "${rmbin} -f \"/tmp/sakis3g.3gnet\""
  5819. ! tty_not_busy "${MODEM_TTY}" && return 1
  5820. if [ "a$1" = "anoretry" ]; then
  5821. verbose "Connecting (second attempt)"
  5822. else
  5823. verbose "Connecting"
  5824. fi
  5825. logpid=0
  5826. if [ -n "${DEBUG}" ]; then
  5827. eval "${CONNECTION_COMMAND} &"
  5828. logpid=$!
  5829. else
  5830. eval "${CONNECTION_COMMAND} <&- >&- 2>&- &"
  5831. logpid=$!
  5832. fi
  5833. passed=0
  5834. while ! notrunning "${logpid}"
  5835. do
  5836. ppp_slow_status && break
  5837. debug "Waiting for interface to go up (%d seconds passed).\n" "${passed}"
  5838. passed=`expr ${passed} + 1`; passed=`echo ${passed}`
  5839. if [ "a${passed}" = "a21" ]; then
  5840. debug "Giving up waiting for connection to occur.\n"
  5841. if find_binary "kill"; then
  5842. ${killbin} -1 ${logpid} 2> /dev/null
  5843. ! notrunning "${logpid}" && sleep 1
  5844. ! notrunning "${logpid}" && sleep 1
  5845. fi
  5846. break
  5847. fi
  5848. sleep 1
  5849. done
  5850. if notrunning "${logpid}" && [ "a$1" != "anoretry" ]; then
  5851. unset logpid
  5852. ispconnect "noretry"
  5853. return $?
  5854. fi
  5855. unset passed
  5856. if ! ppp_slow_status; then
  5857. debug "Failed to connect.\n"
  5858. unset logpid
  5859. return 95
  5860. fi
  5861. debug "Connection is established.\n"
  5862. ${printfbin} "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n" "${ISPID}" "${ISPTEXT}" "${MODEM_VARIANT}" "${USBDRIVER}" "${MODEM_TTY}" "${MODEM}" "${OTHER}" "${APN}" "${CUSTOM_APN}" "${APN_USER}" "${APN_PASS}" "${logpid}" > "/tmp/sakis3g.3gnet"
  5863. [ -f "/tmp/sakis3g.3gnet" ] && ${chmodbin} 644 "/tmp/sakis3g.3gnet"
  5864. unset logpid
  5865. return 0
  5866. }
  5867.  
  5868. # Deletes all currently existing default gateways.
  5869. routing_delete_gateways() {
  5870. ! we_are_root && return 1
  5871. ! find_binary "netstat" && return 1
  5872. ! find_binary "route" && return 1
  5873. while [ "1" = "1" ];
  5874. do
  5875. gatehost=`${netstatbin} -rn | ${grepbin} "^0.0.0.0 " | ${tailbin} -1 | ${sedbin} -e "s/ */ /g" | ${cutbin} -d\ -f2`
  5876. if [ "a${gatehost}" != "a" ]; then
  5877. debug "Deleting default gateway %s.\n" "${gatehost}"
  5878. debug run_command "${routebin} del default gw ${gatehost}"
  5879. else
  5880. debug "Deleted all default gateways.\n"
  5881. break
  5882. fi
  5883. done
  5884. unset gatehost
  5885. return 0
  5886. }
  5887.  
  5888. # Makes sure only our pppint(ppp0) peer is used as default gateway.
  5889. routing_fix() {
  5890. ! find_binary "netstat" && return 1
  5891. ! find_binary "route" && return 1
  5892. need_arg "pppint"
  5893. hostpeer=`${netstatbin} -rn | ${grepbin} " ${pppint}$" | ${grepbin} -v "^0.0.0.0 " | ${cutbin} -d\ -f1`
  5894. if [ "a${hostpeer}" != "a" ]; then
  5895. routing_delete_gateways
  5896. debug run_command "${routebin} add default gw ${hostpeer}"
  5897. debug "Added correct default gateway: %s.\n" "${hostpeer}"
  5898. debug run_command "${netstatbin} -rn"
  5899. fi
  5900. unset hostpeer
  5901. return 0
  5902. }
  5903.  
  5904. dns_fix() {
  5905. debug "Checking if required to fix DNS settings.\n"
  5906. unset needdnsfix
  5907. if [ ! -f "/etc/resolv.conf" ]; then
  5908. needdnsfix=1
  5909. debug "No %s exists. Will make it ourselves.\n" "/etc/resolv.conf"
  5910. else
  5911. debug show_file "/etc/resolv.conf"
  5912. baddns=`${grepbin} "10.11.12.13" "/etc/resolv.conf" | ${wcbin} -l`; baddns=`echo ${baddns}`
  5913. [ "a${baddns}" = "a0" ] && baddns=`${grepbin} "10.11.12.14" "/etc/resolv.conf" | ${wcbin} -l`
  5914. baddns=`echo ${baddns}`
  5915. if [ "a${baddns}" != "a0" ]; then
  5916. needdnsfix=1
  5917. debug "File %s contains bad DNS servers. Will fix it.\n" "/etc/resolv.conf"
  5918. fi
  5919. unset baddns
  5920. fi
  5921. if [ "a${DNS}" != "a" ]; then
  5922. needdnsfix=1
  5923. debug "Will fix DNS due to DNS servers being set: %s\n" "${DNS}"
  5924. fi
  5925. if [ "a${needdnsfix}" != "a1" ]; then
  5926. debug "No need to fix DNS servers. Skipping it.\n"
  5927. unset needdnsfix
  5928. return 0
  5929. fi
  5930. if [ "a${DNS}" = "a" ]; then
  5931. debug "No DNS servers where set. Will use Google DNS.\n"
  5932. export DNS="8.8.8.8 8.8.4.4"
  5933. fi
  5934. ${rmbin} -f "/etc/resolv.conf"
  5935. ${touchbin} "/etc/resolv.conf"
  5936. ${chmodbin} 644 "/etc/resolv.conf"
  5937. for dnsserver in ${DNS}
  5938. do
  5939. ${printfbin} "nameserver %s\n" "${dnsserver}" >> "/etc/resolv.conf"
  5940. debug "Added name server %s to %s.\n" "${dnsserver}" "/etc/resolv.conf"
  5941. done
  5942. unset dnsserver
  5943. ${printfbin} "\n# This file was autogenerated by %s.\n\n" "Sakis3G script" >> "/etc/resolv.conf"
  5944. debug "Done setting up custom DNS server(s).\n"
  5945. return 0
  5946. }
  5947.  
  5948.  
  5949. flow_post_connection() {
  5950. if [ "a${NOSMART}" != "a" ]; then
  5951. debug "Requested not to smart-fix connection.\n"
  5952. return 0
  5953. fi
  5954. verbose "Fixing connection"
  5955. debug "Running post-connection setup.\n"
  5956. routing_fix
  5957. dns_fix
  5958. }
  5959.  
  5960. flow_connect() {
  5961. ppp_fast_status && return 0
  5962. flow_prepare "$@"
  5963. ret=$?; [ "${ret}" -ne "0" ] && return ${ret}
  5964. verbose "Resolving connection details"
  5965. flow_select_apn "$@"
  5966. ret=$?; [ "${ret}" -ne "0" ] && return ${ret}
  5967. flow_apn_user "$@"
  5968. ret=$?; [ "${ret}" -ne "0" ] && return ${ret}
  5969. flow_apn_pass "$@"
  5970. ret=$?; [ "${ret}" -ne "0" ] && return ${ret}
  5971. [ "a${MODEM_TTY}" = "a" ] && return 99
  5972. [ ! -c "${MODEM_TTY}" ] && return 99
  5973. [ "a${APN}" = "a" -o "a${APN_USER}" = "a" -o "a${APN_PASS}" = "a" -o "a${ISP_DIAL}" = "a" ] && return 99
  5974. ! at_default_commands "DIAL" && return 99
  5975. [ "a${ttycommands}" = "a" ] && return 99
  5976. export DIAL_COMMANDS="${ttycommands}"
  5977. if [ "a${direct_pppd}" != "a" ]; then
  5978. pppd_config "$@"
  5979. ret=$?
  5980. else
  5981. wvdial_config "$@"
  5982. ret=$?
  5983. fi
  5984. unset DIAL_COMMANDS
  5985. [ "${ret}" -ne "0" ] && return ${ret}
  5986. verbose "Initializing modem"
  5987. tty_send_command "${MODEM_TTY}" "INITIALIZE"
  5988. ret=$?; [ "${ret}" -ne "0" ] && return ${ret}
  5989. tty_send_command "${MODEM_TTY}" "STAGE7"
  5990. tty_send_command "${MODEM_TTY}" "STAGE8"
  5991. ispconnect "$@"
  5992. ret=$?
  5993. [ "a${CONNECTION_CONF}" != "a" ] && [ -f "${CONNECTION_CONF}" ] && ${rmbin} -f "${CONNECTION_CONF}"
  5994. unset CONNECTION_CONF; unset CONNECTION_COMMAND
  5995. [ "${ret}" -ne "0" ] && return ${ret}
  5996. flow_post_connection "$@"
  5997. if [ "a${CONNECTION_HOOK}" != "a" ]; then
  5998. debug "Will now execute CONNECTION_HOOK: %s.\n" "${CONNECTION_HOOK}"
  5999. term_clearline
  6000. debug run_command "${CONNECTION_HOOK}"
  6001. debug "Connection hook returned.\n"
  6002. fi
  6003. ! ppp_slow_status && return 99
  6004. return ${ret}
  6005. }
  6006.  
  6007. flow_disconnect() {
  6008. ! ppp_fast_status && return 0
  6009. ! we_are_root && return 1
  6010. verbose "Disconnecting"
  6011. if find_binary "killall"; then
  6012. ${killallbin} -q -1 pppd
  6013. sleep 1
  6014. ppp_fast_status && ${killallbin} -q -9 pppd
  6015. elif find_binary "ps" && find_binary "kill"; then
  6016. ppppid=`${psbin} -C "pppd" -o pid= 2> /dev/null | ${grepbin} -i "PID"`; ppppid=`echo ${ppppid}`
  6017. for propid in ${ppppid}
  6018. do
  6019. debug run_command "kill -1 ${propid}"
  6020. done
  6021. ppppid=`${psbin} -C "pppd" -o pid= 2> /dev/null | ${grepbin} -i "PID"`; ppppid=`echo ${ppppid}`
  6022. for propid in ${ppppid}
  6023. do
  6024. debug run_command "kill -9 ${propid}"
  6025. done
  6026. unset ppppid; unset propid
  6027. else
  6028. show_fmt_error "Unable to stop running pppd session. Both %s and % were not found.\n" "kill" "killall"
  6029. return 1
  6030. fi
  6031. sleep 1
  6032. if ppp_fast_status; then
  6033. debug "Failed to disconnect.\n"
  6034. return 1
  6035. fi
  6036. debug "Disconnected.\n"
  6037. return 0
  6038. }
  6039.  
  6040. compile_check_local_requirements() {
  6041. if [ "a${PROVIDER}" = "a" ]; then
  6042. show_fmt_error "Not running from within package.\n"
  6043. return 1
  6044. fi
  6045. if [ ! -x "${PROVIDER}" ]; then
  6046. show_fmt_error "Reported package location is not executable: %s\n" "${PROVIDER}"
  6047. return 1
  6048. fi
  6049. if [ ! -w "${PROVIDER}" ] && we_are_root; then
  6050. show_fmt_error "Unable to overwrite existing package %s. Check if on read-only filesystem.\n" "${PROVIDER}"
  6051. return 1
  6052. fi
  6053. return 0
  6054. }
  6055.  
  6056. compile_check_dependencies() {
  6057. unset missing
  6058. for localdependency in bzip2 bunzip2 pwd basename dirname gcc tar dd stat cp chmod rm mkdir mv find rmdir printf grep expr ls sort uniq cut sed tr cat touch
  6059. do
  6060. ! find_binary "${localdependency}" && missing="${missing} ${localdependency}"
  6061. done
  6062. missing=`echo ${missing}`
  6063. if [ "a${missing}" != "a" ]; then
  6064. show_fmt_error "Following dependencies not found within path: %s\n" "${missing}"
  6065. unset missing
  6066. return 1
  6067. fi
  6068. unset missing
  6069. return 0
  6070. }
  6071.  
  6072. compile_compilation_dependencies() {
  6073. [ "a${PROVIDER}" = "a" ] && return 1
  6074. [ ! -x "${PROVIDER}" ] && return 1
  6075. ${rmbin} -f "/tmp/sakis3g.source.$$.combined.c" > /dev/null 2> /dev/null
  6076. ${touchbin} "/tmp/sakis3g.source.$$.combined.c" > /dev/null 2> /dev/null
  6077. if [ ! -f "/tmp/sakis3g.source.$$.combined.c" ]; then
  6078. show_fmt_error "Unable to create file %s.\n" "/tmp/sakis3g.source.$$.combined.c"
  6079. return 1
  6080. fi
  6081. ${PROVIDER} getfile usb_modeswitch.c 2> /dev/null > "/tmp/sakis3g.source.$$.c"
  6082. ${PROVIDER} getfile usb_modeswitch.h 2> /dev/null > "/tmp/sakis3g.source.$$.h"
  6083. ${catbin} "/tmp/sakis3g.source.$$.c" "/tmp/sakis3g.source.$$.h" > "/tmp/sakis3g.source.$$.combined.c"
  6084. if [ ! -s "/tmp/sakis3g.source.$$.combined.c" ]; then
  6085. ${rmbin} -f "/tmp/sakis3g.source.$$.combined.c" "/tmp/sakis3g.source.$$.c" "/tmp/sakis3g.source.$$.h" > /dev/null 2> /dev/null
  6086. show_fmt_error "Unable to extract %s source from package.\n" "Usb-ModeSwitch"
  6087. return 1
  6088. fi
  6089. headerfiles=`${grepbin} "^#include <" "/tmp/sakis3g.source.$$.combined.c" | ${sedbin} -e "s/\( *\)/ /g" | ${cutbin} -d\ -f2 -s | ${sedbin} -e "s/^<\(.*\)>$/INCLUDE:\1/g" | ${grepbin} "^INCLUDE:" | ${cutbin} -d: -f2- -s`
  6090. if [ "a${headerfiles}" = "a" ]; then
  6091. unset headerfiles
  6092. ${rmbin} -f "/tmp/sakis3g.source.$$.combined.c" "/tmp/sakis3g.source.$$.c" "/tmp/sakis3g.source.$$.h" > /dev/null 2> /dev/null
  6093. show_fmt_error "Problem while parsing %s sources.\n" "Usb-ModeSwitch"
  6094. return 1
  6095. fi
  6096. debug "Header files are:\n%s\n" "${headerfiles}"
  6097. accumulative=""
  6098. for header in ${headerfiles}
  6099. do
  6100. debug "Probing: %s\n" "${header}"
  6101. accumulative=`${printfbin} "%s\n%s\n" "${accumulative}" "${header}" | ${grepbin} -v "^$"`
  6102. echo "${accumulative}" | ${sedbin} -e "s/^\(.*\)$/#include <\1\>/g" > "/tmp/sakis3g.source.$$.combined.c"
  6103. ${printfbin} "int main(int argc, char **argv) {\nreturn 22;\n}\n\n" >> "/tmp/sakis3g.source.$$.combined.c"
  6104. ${rmbin} -f "/tmp/sakis3g.object.$$" > /dev/null 2> /dev/null
  6105. if [ -f "/tmp/sakis3g.object.$$" ]; then
  6106. unset headerfiles; unset header; unset accumulative;
  6107. ${rmbin} -f "/tmp/sakis3g.source.$$.combined.c" "/tmp/sakis3g.source.$$.c" "/tmp/sakis3g.source.$$.h" > /dev/null 2> /dev/null
  6108. show_fmt_error "Unable to delete \"%s\".\n" "/tmp/sakis3g.object.$$"
  6109. return 1
  6110. fi
  6111. debug run_command "${gccbin} -Wall -l usb-1.0 -o \"/tmp/sakis3g.object.$$\" \"/tmp/sakis3g.source.$$.combined.c\" -lusb-1.0 "
  6112. if [ ! -x "/tmp/sakis3g.object.$$" ]; then
  6113. if [ "a${header}" != "ausb.h" ]; then
  6114. show_fmt_error "Header file %s missing from your system.\n" "${header}"
  6115. else
  6116. show_fmt_error "Header file %s missing from your system. This usually indicates libusb (or libusb-compat) development kit missing.\n" "${header}"
  6117. fi
  6118. unset headerfiles; unset header; unset accumulative;
  6119. ${rmbin} -f "/tmp/sakis3g.source.$$.combined.c" "/tmp/sakis3g.source.$$.c" "/tmp/sakis3g.source.$$.h" > /dev/null 2> /dev/null
  6120. ${rmbin} -f "/tmp/sakis3g.object.$$" > /dev/null 2> /dev/null
  6121. return 1
  6122. fi
  6123. debug run_command "/tmp/sakis3g.object.$$"
  6124. ret=$?
  6125. if [ "${ret}" -ne "22" ]; then
  6126. show_fmt_error "Unusable binary after including \"%s\".\n" "${header}"
  6127. unset headerfiles; unset header; unset accumulative;
  6128. ${rmbin} -f "/tmp/sakis3g.source.$$.combined.c" "/tmp/sakis3g.source.$$.c" "/tmp/sakis3g.source.$$.h" > /dev/null 2> /dev/null
  6129. ${rmbin} -f "/tmp/sakis3g.object.$$" > /dev/null 2> /dev/null
  6130. return 1
  6131. fi
  6132. debug "No problem encountered when including \"%s\".\n" "${header}"
  6133. ${rmbin} -f "/tmp/sakis3g.object.$$" > /dev/null 2> /dev/null
  6134. done
  6135. ${rmbin} -f "/tmp/sakis3g.source.$$.combined.c" > /dev/null 2> /dev/null
  6136. unset header; unset headerfiles;
  6137. debug "Will now attempt linking.\n"
  6138. echo "${accumulative}" | ${sedbin} -e "s/^\(.*\)$/#include <\1\>/g" > "/tmp/sakis3g.source.$$.combined.c"
  6139. ${catbin} "/tmp/sakis3g.source.$$.h" "/tmp/sakis3g.source.$$.c" | ${grepbin} -v "^#include" >> "/tmp/sakis3g.source.$$.combined.c"
  6140. debug show_file "/tmp/sakis3g.source.$$.combined.c"
  6141. debug run_command "${gccbin} -Wall -l usb-1.0 -o \"/tmp/sakis3g.object.$$\" \"/tmp/sakis3g.source.$$.combined.c\" -lusb-1.0"
  6142. unset accumulative;
  6143. ${rmbin} -f "/tmp/sakis3g.source.$$.combined.c" "/tmp/sakis3g.source.$$.c" "/tmp/sakis3g.source.$$.h" > /dev/null 2> /dev/null
  6144. if [ ! -x "/tmp/sakis3g.object.$$" ]; then
  6145. ${rmbin} -f "/tmp/sakis3g.object.$$" > /dev/null 2> /dev/null
  6146. show_fmt_error "Failed to link %s. This usually indicates libusb (or libusb-compat) development kit missing.\n" "Usb-ModeSwitch"
  6147. return 1
  6148. fi
  6149. ${rmbin} -f "/tmp/sakis3g.object.$$" > /dev/null 2> /dev/null
  6150. debug "Linking test was successful.\n"
  6151. return 0
  6152. }
  6153.  
  6154. compile_perform() {
  6155. [ "a${PROVIDER}" = "a" ] && return 1
  6156. [ ! -x "${PROVIDER}" ] && return 1
  6157. tempdestination="/tmp/sakis3g.recompile.$$"
  6158. debug run_command "${rmbin} -rf \"${tempdestination}\""
  6159. debug run_command "${mkdirbin} \"${tempdestination}\""
  6160. if [ ! -d "${tempdestination}" ]; then
  6161. show_fmt_error "Unable to create temporary folder \"%s\".\n" "${tempdestination}"
  6162. unset tempdestination
  6163. return 1
  6164. fi
  6165. debug run_command "${touchbin} \"${tempdestination}/test.$$\""
  6166. if [ ! -w "${tempdestination}/test.$$" ]; then
  6167. debug run_command "${rmbin} -rf \"${tempdestination}\""
  6168. show_fmt_error "No write access to temporary folder \"%s\".\n" "${tempdestination}"
  6169. unset tempdestination
  6170. return 1
  6171. fi
  6172. debug run_command "${rmbin} -rf \"${tempdestination}/\"*"
  6173. if [ ! -d "${tempdestination}" ]; then
  6174. show_fmt_error "Oops. Weird \"%s\" version. Deleted temporary folder \"%s\".\n" "${rmbin}" "${tempdestination}"
  6175. unset tempdestination
  6176. return 1
  6177. fi
  6178. debug run_command "${PROVIDER} disassemble \"${tempdestination}/.\""
  6179. if [ ! -x "${tempdestination}/sakis3g-${MYVERSION}/compile" ]; then
  6180. debug run_command "${rmbin} -rf \"${tempdestination}\""
  6181. show_fmt_error "Failed to disassemble Sakis3G package.\n" "${tempdestination}"
  6182. unset tempdestination
  6183. return 1
  6184. fi
  6185. debug run_command "${rmbin} -f \"${tempdestination}/sakis3g-${MYVERSION}/build/sakis3gz\""
  6186. compilation_error=`cd "${tempdestination}/sakis3g-${MYVERSION}"; ./compile 2>&1`
  6187. debug "Compilation output:\n%s\n" "${compilation_error}"
  6188. if [ ! -x "${tempdestination}/sakis3g-${MYVERSION}/build/sakis3gz" -o ! -s "${tempdestination}/sakis3g-${MYVERSION}/build/sakis3gz" ]; then
  6189. debug run_command "${rmbin} -rf \"${tempdestination}\""
  6190. show_fmt_error "Failed to compile. Compilation output:\n%s\n" "${compilation_error}"
  6191. unset compilation_error; unset tempdestination
  6192. return 1
  6193. fi
  6194. unset compilation_error
  6195. verification=`"${tempdestination}/sakis3g-${MYVERSION}/build/sakis3gz" usb_modeswitch --version 2> /dev/null | ${grepbin} -i "josua" | ${wcbin} -l`; verification=`echo ${verification}`
  6196. if [ "a${verification}" = "a" -o "a${verification}" = "a0" ]; then
  6197. unset verification
  6198. debug run_command "${rmbin} -rf \"${tempdestination}\""
  6199. show_fmt_error "Failed to verify result of compilation.\n"
  6200. unset tempdestination
  6201. return 1
  6202. fi
  6203. unset verification
  6204. debug run_command "${rmbin} -f \"${PROVIDER}.backup\""
  6205. if [ -f "${PROVIDER}.backup" ]; then
  6206. debug run_command "${rmbin} -rf \"${tempdestination}\""
  6207. show_fmt_error "Unable to delete previously existing \"%s\".\n" "${PROVIDER}.backup"
  6208. unset tempdestination
  6209. return 1
  6210. fi
  6211. debug run_command "${cpbin} \"${PROVIDER}\" \"${PROVIDER}.backup\""
  6212. if [ ! -f "${PROVIDER}.backup" ]; then
  6213. debug run_command "${rmbin} -rf \"${tempdestination}\""
  6214. show_fmt_error "Unable to create a safe backup to \"%s\".\n" "${PROVIDER}.backup"
  6215. unset tempdestination
  6216. return 1
  6217. fi
  6218. debug run_command "${lsbin} -l \"${PROVIDER}\""
  6219. # No rm, or we loose owner and rights information
  6220. #debug run_command "${rmbin} -f \"${PROVIDER}\""
  6221. #debug run_command "${lsbin} -l \"${PROVIDER}\""
  6222. debug run_command "${cpbin} \"${tempdestination}/sakis3g-${MYVERSION}/build/sakis3gz\" \"${PROVIDER}\""
  6223. debug run_command "${lsbin} -l \"${PROVIDER}\""
  6224. debug run_command "${rmbin} -rf \"${tempdestination}\""
  6225. unset tempdestination
  6226. verification=`"${PROVIDER}" usb_modeswitch --version 2> /dev/null | ${grepbin} -i "josua" | ${wcbin} -l`; verification=`echo ${verification}`
  6227. if [ "a${verification}" = "a" -o "a${verification}" = "a0" ]; then
  6228. debug "Verification failed. Restoring previous version.\n"
  6229. debug run_command "${cpbin} \"${PROVIDER}.backup\" \"${PROVIDER}\""
  6230. unset verification
  6231. if [ -x "${PROVIDER}" ]; then
  6232. show_fmt_error "Failed to succesfully place new version. You are still using old version.\n"
  6233. elif [ -x "${PROVIDER}.backup" ]; then
  6234. show_fmt_error "Failed to succesfully create new version. Old version is still available at \"%s\".\n" "${PROVIDER}.backup"
  6235. else
  6236. show_fmt_error "Something really bad happened. I am really sorry. You may need to re-download Sakis3G.\n"
  6237. fi
  6238. return 1
  6239. fi
  6240. unset verification
  6241. debug run_command "${rmbin} -f \"${PROVIDER}.backup\""
  6242. debug run_command "${lsbin} -l \"${PROVIDER}.backup\""
  6243. debug "Succesfully recompiled.\n"
  6244. return 0
  6245. }
  6246.  
  6247. flow_compile() {
  6248. if [ "a${binaryfree}" != "a" ]; then
  6249. show_fmt_error "You are running a binary free version of Sakis3G. Unable to recompile %s.\n" "Usb-ModeSwitch"
  6250. return 99
  6251. fi
  6252. verbose "Checking tools availability"
  6253. compile_check_local_requirements
  6254. ret=$?; [ "${ret}" -ne "0" ] && return ${ret}
  6255. verbose "Checking dependencies"
  6256. compile_check_dependencies
  6257. ret=$?; [ "${ret}" -ne "0" ] && return ${ret}
  6258. verbose "Testing compiler"
  6259. compile_compilation_dependencies
  6260. ret=$?; [ "${ret}" -ne "0" ] && return ${ret}
  6261. verbose "Compiling"
  6262. compile_perform
  6263. ret=$?; [ "${ret}" -ne "0" ] && return ${ret}
  6264. return 0
  6265. }
  6266.  
  6267. flow_report() {
  6268. if ! status_connected; then
  6269. show_fmt_error "You need to be connected for generating report.\n"
  6270. return 6
  6271. fi
  6272. unset report_text
  6273. old_translation=${notranslate}
  6274. export notranslate=1
  6275. report_text="Sakis3G version: ${MYVERSION}"
  6276. if [ "a${PROVIDER}" = "a" ]; then
  6277. report_text=`${printfbin} "%s\nNot running from within package.\n" "${report_text}"`
  6278. elif [ ! -x "${PROVIDER}" ]; then
  6279. report_text=`${printfbin} "%s\nPackage is not executable.\n" "${report_text}"`
  6280. else
  6281. if [ "a${binaryfree}" != "a" ]; then
  6282. if [ "a${stripped}" != "a" ]; then
  6283. report_text=`${printfbin} "%s\nRunning a binary free version.\n" "${report_text}"`
  6284. else
  6285. report_text=`${printfbin} "%s\nRunning a stripped version.\n" "${report_text}"`
  6286. fi
  6287. switchversion="95"
  6288. else
  6289. switchversion=`${PROVIDER} usb_modeswitch --version 2> /dev/null || echo $?`
  6290. fi
  6291. if [ "a${switchversion}" = "a95" ]; then
  6292. if find_binary "usb_modeswitch"; then
  6293. switchversion=`${usb_modeswitchbin} --version 2> /dev/null`
  6294. report_text=`${printfbin} "%s\nSystem provided Usb-ModeSwitch:\n%s\n" "${report_text}" "${switchversion}"`
  6295. else
  6296. report_text=`${printfbin} "%s\nUsb-ModeSwitch is not available.\n" "${report_text}"`
  6297. fi
  6298. else
  6299. switchversion=`${printfbin} "%s\n" "${switchversion}" | ${grepbin} -i version`
  6300. report_text=`${printfbin} "%s\nUsing embedded Usb-ModeSwitch version:\n%s\n" "${report_text}" "${switchversion}"`
  6301. fi
  6302. fi
  6303. if find_binary "uname"; then
  6304. report_text=`${printfbin} "%s\nKernel version: %s\nArchitect: %s\n" "${report_text}" "\`${unamebin} -r\`" "\`${unamebin} -m\`"`
  6305. else
  6306. report_text=`${printfbin} "%s\nUtility uname is not available.\n" "${report_text}"`
  6307. fi
  6308. report_text=`${printfbin} "%s\nSelected UI is: %s\n" "${report_text}" "${SGUI}"`
  6309. info_text=`action_info report | ${grepbin} -v "^$" | ${grepbin} -v "^ $"`
  6310. report_text=`${printfbin} "%s\n%s\n" "${report_text}" "${info_text}"`
  6311. state_variables "APN_USER APN_PASS SIM_PIN SGUI UNDISCOVERABLE BLUETOOTH MENU MOREMENU BALOONIZER"
  6312. report_text=`${printfbin} "%s\nVariables: %s\n" "${report_text}" "${statevariables}"`
  6313. unset info_text
  6314. export notranslate="${old_translation}"
  6315. [ "a${notranslate}" = "a" ] && unset notranslate
  6316. unset old_translation
  6317. trtext=`format_text "Please report following text:\n"`
  6318. notify "%s\n\n%s\n" "${trtext}" "${report_text}"
  6319. unset trtext; unset switchversion; unset report_text
  6320. return 0
  6321. }
  6322.  
  6323.  
  6324. # Returns 0 if connected, 1 if not connected.
  6325. # Sets as much of the following information:
  6326. # - statusnet containing numeric ID of ISP
  6327. # - statusname containing text of ISP or nothing
  6328. # - statusicon if statusnet exists within operator database and contains icon.
  6329. # Sets ISPID only if not set already.
  6330. # It is possible that it returns 0 and not set variables. This happens
  6331. # if ppp interface was initiated by another application.
  6332. status_connected() {
  6333. unset statusnet; unset statusname; unset statusicon; unset localpid
  6334. ! ppp_fast_status && return 1
  6335. if [ -r "/tmp/sakis3g.3gnet" ]; then
  6336. statusnet=`${headbin} -1 "/tmp/sakis3g.3gnet"`
  6337. statusname=`${headbin} -2 "/tmp/sakis3g.3gnet" | ${tailbin} -1`
  6338. debug "Retrieved from %s: \"%s\" - \"%s\"\n" "/tmp/sakis3g.3gnet" "${statusnet}" "${statusname}"
  6339. statusnet=`echo "${statusnet}" | ${grepbin} "^[0-9][0-9][0-9][0-9][0-9]"`
  6340. if [ "a${statusnet}" != "a" ]; then
  6341. [ "a${statusname}" = "a${statusnet}" ] && unset statusname
  6342. debug "Retrieved ISP ID from %s: %s\n" "/tmp/sakis3g.3gnet" "${statusnet}"
  6343. [ "a${statusname}" != "a" ] && debug "Also retrieved ISP name: %s\n" "${statusname}"
  6344. else
  6345. unset statusname; unset statusnet
  6346. debug "Invalid or not existing %s within %s.\n" "ISPID" "/tmp/sakis3g.3gnet"
  6347. fi
  6348. if [ "a${statusnet}" != "a" ] && [ "a${ISPID}" = "a" -o "a${ISPID}" = "a${statusnet}" ]; then
  6349. if [ "a${MODEM_VARIANT}" = "a" ]; then
  6350. MODEM_VARIANT=`${headbin} -3 "/tmp/sakis3g.3gnet" | ${tailbin} -1`
  6351. [ "a${MODEM_VARIANT}" != "a" ] && export MODEM_VARIANT && debug "Also read: %s\n" "${MODEM_VARIANT}"
  6352. fi
  6353. if [ "a${USBDRIVER}" = "a" ]; then
  6354. USBDRIVER=`${headbin} -4 "/tmp/sakis3g.3gnet" | ${tailbin} -1`
  6355. [ "a${USBDRIVER}" != "a" ] && export USBDRIVER && debug "Also read: %s\n" "${USBDRIVER}"
  6356. fi
  6357. if [ "a${MODEM_TTY}" = "a" ]; then
  6358. MODEM_TTY=`${headbin} -5 "/tmp/sakis3g.3gnet" | ${tailbin} -1`
  6359. [ "a${MODEM_TTY}" != "a" ] && export MODEM_TTY && debug "Also read: %s\n" "${MODEM_TTY}"
  6360. fi
  6361. if [ "a${MODEM}" = "a" ]; then
  6362. MODEM=`${headbin} -6 "/tmp/sakis3g.3gnet" | ${tailbin} -1`
  6363. [ "a${MODEM}" != "a" ] && export MODEM && debug "Also read: %s\n" "${MODEM}"
  6364. fi
  6365. if [ "a${OTHER}" = "a" ]; then
  6366. OTHER=`${headbin} -7 "/tmp/sakis3g.3gnet" | ${tailbin} -1`
  6367. [ "a${OTHER}" != "a" ] && export OTHER && debug "Also read: %s\n" "${OTHER}"
  6368. fi
  6369. if [ "a${APN}" = "a" ]; then
  6370. APN=`${headbin} -8 "/tmp/sakis3g.3gnet" | ${tailbin} -1`
  6371. [ "a${APN}" != "a" ] && export APN && debug "Also read: %s\n" "${APN}"
  6372. fi
  6373. if [ "a${CUSTOM_APN}" = "a" -a "a${APN}" = "aCUSTOM_APN" ]; then
  6374. CUSTOM_APN=`${headbin} -9 "/tmp/sakis3g.3gnet" | ${tailbin} -1`
  6375. [ "a${CUSTOM_APN}" != "a" ] && export CUSTOM_APN && debug "Also read: %s\n" "${CUSTOM_APN}"
  6376. fi
  6377. if [ "a${APN_USER}" = "a" ]; then
  6378. APN_USER=`${headbin} -10 "/tmp/sakis3g.3gnet" | ${tailbin} -1`
  6379. [ "a${APN_USER}" != "a" ] && export APN_USER && debug "Also read: %s\n" "${APN_USER}"
  6380. fi
  6381. if [ "a${APN_PASS}" = "a" ]; then
  6382. APN_PASS=`${headbin} -11 "/tmp/sakis3g.3gnet" | ${tailbin} -1`
  6383. [ "a${APN_PASS}" != "a" ] && export APN_PASS && debug "Also read: %s\n" "${APN_PASS}"
  6384. fi
  6385. localpid=`${headbin} -12 "/tmp/sakis3g.3gnet" | ${tailbin} -1`
  6386. if [ "a${localpid}" != "a" ]; then
  6387. localprocess=`${psbin} -p ${localpid} -o comm= 2> /dev/null`
  6388. if [ "a${localprocess}" != "a" ]; then
  6389. debug "Process ID %s is: %s\n" "${localpid}" "${localprocess}"
  6390. [ "${localprocess}" != "pppd" -a "${localprocess}" != "wvdial" ] && unset localprocess
  6391. elif notrunning "${localpid}"; then
  6392. debug "Process ID %s is not running.\n" "${localpid}"
  6393. elif [ "a${localprocess}" = "a" ]; then
  6394. debug "Process ID %s is not valid.\n" "${localpid}"
  6395. fi
  6396. if [ "a${localprocess}" = "a" -a "a${ISPID}" = "a" ]; then
  6397. debug "Status file %s is stalled. Will discard information read from it.\n" "/tmp/sakis3g.3gnet"
  6398. unset statusnet; unset statusname
  6399. fi
  6400. fi
  6401. unset localpid; unset localprocess
  6402. fi
  6403. else
  6404. debug "Invalid or not existing %s.\n" "/tmp/sakis3g.3gnet"
  6405. fi
  6406. if [ "a${ISPID}" != "a" ]; then
  6407. debug "%s is set to: %s\n" "ISPID" "${ISPID}"
  6408. if [ "a${statusnet}" = "a" ]; then
  6409. statusnet="${ISPID}"
  6410. debug "Was adapted since file not available.\n" "statusnet"
  6411. if [ "a${ISPTEXT}" != "a" -a "a${ISPTEXT}" != "a${ISPID}" ]; then
  6412. statusname="${ISPTEXT}"
  6413. debug "Also adapted %s: %s\n" "ISPTEXT" "${ISPTEXT}"
  6414. fi
  6415. elif [ "a${statusnet}" = "a${ISPID}" ]; then
  6416. debug "It matches value retrieved from file.\n"
  6417. if [ "a${ISPTEXT}" != "a" -a "a${ISPTEXT}" != "a${ISPID}" -a "a${statusname}" = "a" ]; then
  6418. statusname="${ISPTEXT}"
  6419. debug "Also adapted %s since networks match: %s\n" "ISPTEXT" "${ISPTEXT}"
  6420. fi
  6421. else
  6422. debug "Does not match value retrieved from file.\n"
  6423. debug "Will trust file.\n"
  6424. fi
  6425. else
  6426. debug "%s is not set.\n" "ISPID"
  6427. if [ "a${statusnet}" = "a" ]; then
  6428. debug "Unable to determine on which network we are connected.\n"
  6429. else
  6430. export ISPID="${statusnet}"
  6431. debug "All information derived from file.\n"
  6432. debug "Also set %s from file contents.\n" "ISPID"
  6433. fi
  6434. fi
  6435. if net_info "${statusnet}" "${statusname}" && [ "a${BASERESULT}" != "a" ]; then
  6436. statusproduct=`echo "${BASERESULT}" | ${cutbin} -f3 -s`
  6437. [ "a${statusproduct}" != "a" ] && export statusproduct
  6438. statusicon=`echo "${BASERESULT}" | ${cutbin} -f8 -s`
  6439. [ "a${statusicon}" != "a" ] && export statusicon
  6440. if [ "a${statusname}" = "a" ]; then
  6441. statusname=`echo "${BASERESULT}" | ${cutbin} -d: -f2 -s`
  6442. [ "a${statusname}" != "a" ] && debug "Retrieved %s name from database: %s\n" "${statusnet}" "${statusname}"
  6443. fi
  6444. fi
  6445. if [ "a${statusnet}" = "a" ]; then
  6446. unset statusnet; unset statusname
  6447. else
  6448. export statusnet
  6449. if [ "a${statusname}" = "a" ]; then
  6450. unset statusname
  6451. else
  6452. export statusname
  6453. fi
  6454. fi
  6455. return 0
  6456. }
  6457.  
  6458. # Method invoked when "status" action was requested.
  6459. action_status() {
  6460. unset verbosecurrentcount
  6461. if ! ppp_fast_status; then
  6462. show_fmt_error "Not connected.\n"
  6463. return 6
  6464. fi
  6465. status_connected
  6466. if [ "a${statusnet}" != "a" ]; then
  6467. if [ "a${statusnet}" != "a${statusname}" -a "a${statusname}" != "a" ]; then
  6468. if [ "a${MODEM_VARIANT}" != "a" ]; then
  6469. finalnotify "%s connected to %s (%s)." "${MODEM_VARIANT}" "${statusname}" "${statusnet}"
  6470. else
  6471. finalnotify "Connected to %s (%s)." "${statusname}" "${statusnet}"
  6472. fi
  6473. else
  6474. if [ "a${MODEM_VARIANT}" != "a" ]; then
  6475. finalnotify "%s connected to %s." "${MODEM_VARIANT}" "${statusnet}"
  6476. else
  6477. finalnotify "Connected to %s." "${statusnet}"
  6478. fi
  6479. fi
  6480. else
  6481. finalnotify "Connected."
  6482. fi
  6483. return 0
  6484. }
  6485.  
  6486. action_info() {
  6487. unset verbosecurrentcount
  6488. if ! ppp_fast_status; then
  6489. notify "Not connected.\n"
  6490. return 0
  6491. fi
  6492. status_connected
  6493. if [ "a${statusnet}" = "a" ]; then
  6494. notify "Unable to gather connection information.\n"
  6495. return 0
  6496. fi
  6497. if [ "a$1" != "areport" ]; then
  6498. infotext=`translate_text "Connection Information"`
  6499. infotext=`${printfbin} "%s\n \n" "${infotext}"`
  6500. fi
  6501. [ "a${pppint}" != "a" ] && localtext=`format_text "Interface:\t\t\tP-t-P (%s)\n" "${pppint}"`
  6502. [ "a${localtext}" != "a" ] && infotext=`${printfbin} "%s\n%s\n" "${infotext}" "${localtext}"`; unset localtext
  6503. infotext=`${printfbin} "%s\n \n" "${infotext}"`
  6504. if [ "a$1" != "areport" ]; then
  6505. if find_binary "stat"; then
  6506. upsince=`${statbin} --printf="%z" "/tmp/sakis3g.3gnet" | ${cutbin} -d: -f1,2 -s`
  6507. if [ "a${upsince}" != "a" ]; then
  6508. localtext=`format_text "Connected since:\t%s\n" "${upsince}"`
  6509. infotext=`${printfbin} "%s\n%s\n" "${infotext}" "${localtext}"`; unset localtext
  6510. fi
  6511. unset upsince
  6512. fi
  6513. if [ -r "/proc/net/dev" -a "a${pppint}" != "a" ]; then
  6514. traffic=`${sedbin} -e "s/ */ /g" "/proc/net/dev" | ${grepbin} "^\( *\)${pppint}:" | ${headbin} -1 | ${cutbin} -d: -f2- | ${sedbin} -e "s/^ *//g" | ${cutbin} -d\ -f1,9`
  6515. debug "Traffic details are: %s\n" "${traffic}"
  6516. bytesreceived=`echo ${traffic} | ${cutbin} -d\ -f1 -s`
  6517. [ "a${bytesreceived}" != "a" ] && bytesreceived=`expr \( ${bytesreceived} + 512 \) / 1024 2> /dev/null`
  6518. [ "a${bytesreceived}" != "a" ] && localtext=`format_text "Kilobytes received:\t%d\n" ${bytesreceived}`
  6519. [ "a${localtext}" != "a" ] && infotext=`${printfbin} "%s\n%s\n" "${infotext}" "${localtext}"`; unset localtext
  6520. bytessent=`echo ${traffic} | ${cutbin} -d\ -f2 -s`
  6521. [ "a${bytessent}" != "a" ] && bytessent=`expr \( ${bytessent} + 512 \) / 1024 2> /dev/null`
  6522. [ "a${bytessent}" != "a" ] && localtext=`format_text "Kilobytes sent:\t%d\n" ${bytessent}`
  6523. [ "a${localtext}" != "a" ] && infotext=`${printfbin} "%s\n%s\n" "${infotext}" "${localtext}"`; unset localtext
  6524. unset traffic; unset bytesreceived; unset bytessent
  6525. fi
  6526. fi
  6527. localtext=`format_text "Network ID:\t\t%s" "${statusnet}"`
  6528. [ "a${localtext}" != "a" ] && infotext=`${printfbin} "%s\n\n%s\n" "${infotext}" "${localtext}"`; unset localtext
  6529. [ "a${statusname}" != "a" ] && localtext=`format_text "Operator name:\t%s" "${statusname}"`
  6530. [ "a${localtext}" != "a" ] && infotext=`${printfbin} "%s\n%s\n" "${infotext}" "${localtext}"`; unset localtext
  6531. if [ "a${APN}" != "a" ]; then
  6532. if [ "a${APN}" = "aCUSTOM_APN" -a "a${CUSTOM_APN}" != "a" ]; then
  6533. localtext=`format_text "APN:\t\t\t%s" "${CUSTOM_APN}"`
  6534. elif [ "a${APN}" != "aCUSTOM_APN" ]; then
  6535. localtext=`format_text "APN:\t\t\t%s" "${APN}"`
  6536. fi
  6537. fi
  6538. [ "a${localtext}" != "a" ] && infotext=`${printfbin} "%s\n%s\n" "${infotext}" "${localtext}"`; unset localtext
  6539. infotext=`${printfbin} "%s\n \n" "${infotext}"`
  6540. [ "a${MODEM_VARIANT}" != "a" ] && localtext=`format_text "Modem:\t\t\t%s\n" "${MODEM_VARIANT}"`
  6541. [ "a${localtext}" != "a" ] && infotext=`${printfbin} "%s\n%s\n" "${infotext}" "${localtext}"`; unset localtext
  6542. if [ "a${MODEM}" != "a" ]; then
  6543. if [ "a${MODEM}" != "a" -a "a${MODEM}" != "aOTHER" ]; then
  6544. localtext=`format_text "Modem type:\t\tUSB\n"`
  6545. elif [ "a${MODEM}" = "aOTHER" ]; then
  6546. if [ "a${OTHER}" = "aUSBMODEM" ]; then
  6547. localtext=`format_text "Modem type:\t\tUSB\n"`
  6548. elif [ "a${OTHER}" = "aBLUETOOTH" ]; then
  6549. localtext=`format_text "Modem type:\t\tBluetooth\n"`
  6550. else
  6551. localtext=`format_text "Modem type:\t\tCustom\n"`
  6552. fi
  6553. else
  6554. localtext=`format_text "Modem type:\t\tUnknown\n"`
  6555. fi
  6556. else
  6557. localtext=`format_text "Modem type:\t\tUnspecified\n"`
  6558. fi
  6559. [ "a${localtext}" != "a" ] && infotext=`${printfbin} "%s\n%s\n" "${infotext}" "${localtext}"`; unset localtext
  6560. [ "a${USBDRIVER}" != "a" ] && localtext=`format_text "Kernel driver:\t\t%s\n" "${USBDRIVER}"`
  6561. [ "a${localtext}" != "a" ] && infotext=`${printfbin} "%s\n%s\n" "${infotext}" "${localtext}"`; unset localtext
  6562. [ "a${MODEM_TTY}" != "a" ] && localtext=`format_text "Device:\t\t\t%s\n" "${MODEM_TTY}"`
  6563. [ "a${localtext}" != "a" ] && infotext=`${printfbin} "%s\n%s\n" "${infotext}" "${localtext}"`; unset localtext
  6564. infotext=`${printfbin} "%s\n \n" "${infotext}"`
  6565. if [ "a$1" != "areport" ]; then
  6566. if find_binary "ifconfig" && [ "a${pppint}" != "a" ]; then
  6567. ipaddress=`${ifconfigbin} ${pppint} | ${sedbin} -e "s/^.*:\([0-9][0-9]*\)\.\([0-9][0-9]*\)\.\([0-9][0-9]*\)\.\([0-9][0-9]*\) .*:\([0-9][0-9]*\)\.\([0-9][0-9]*\)\.\([0-9][0-9]*\)\.\([0-9][0-9]*\) \(.*\):\([0-9][0-9]*\)\.\([0-9][0-9]*\)\.\([0-9][0-9]*\)\.\([0-9][0-9]*\)$/IPADDRESSES: \1.\2.\3.\4 \5.\6.\7.\8/g" | ${grepbin} "^IPADDRESSES:" | ${cutbin} -d: -f2- -s`
  6568. if [ "a${ipaddress}" != "a" ]; then
  6569. localip=`echo ${ipaddress} | ${cutbin} -d\ -f1 -s`
  6570. localpeer=`echo ${ipaddress} | ${cutbin} -d\ -f2 -s`
  6571. localmask="255.255.255.255"
  6572. [ "a${localip}" != "a" ] && localtext=`format_text "IP Address:\t\t%s\n" "${localip}"`
  6573. [ "a${localtext}" != "a" ] && infotext=`${printfbin} "%s\n%s\n" "${infotext}" "${localtext}"`; unset localtext
  6574. [ "a${localip}" != "a" ] && localtext=`format_text "Subnet Mask:\t\t%s\n" "${localmask}"`
  6575. [ "a${localtext}" != "a" ] && infotext=`${printfbin} "%s\n%s\n" "${infotext}" "${localtext}"`; unset localtext
  6576. [ "a${localpeer}" != "a" ] && localtext=`format_text "Peer IP Address:\t%s\n" "${localpeer}"`
  6577. [ "a${localtext}" != "a" ] && infotext=`${printfbin} "%s\n%s\n" "${infotext}" "${localtext}"`; unset localtext
  6578. unset localip; unset localpeer; unset localmask
  6579. fi
  6580. unset ipaddress
  6581. fi
  6582. if find_binary "netstat" && [ "a${pppint}" != "a" ]; then
  6583. localpeer=`${netstatbin} -rn | ${grepbin} "^0.0.0.0 " | ${sedbin} -e "s/ */ /g" | ${cutbin} -d\ -f2 -s`; localpeer=`echo ${localpeer}`
  6584. [ "a${localpeer}" != "a" ] && localtext=`format_text "Default route(s):\t%s\n" "${localpeer}"`
  6585. [ "a${localtext}" != "a" ] && infotext=`${printfbin} "%s\n%s\n" "${infotext}" "${localtext}"`; unset localtext
  6586. unset localpeer
  6587. fi
  6588. [ "a${SGUI}" != "azenity" ] && infotext=`echo "${infotext}" | ${sedbin} -e "s/\( *\)/ /g"`
  6589. notify "%s\n" "${infotext}"
  6590. else
  6591. infotext=`echo "${infotext}" | ${sedbin} -e "s/\( *\)/ /g"`
  6592. ${printfbin} "%s\n" "${infotext}"
  6593. fi
  6594. unset localtext; unset infotext
  6595. return 0
  6596. }
  6597.  
  6598. action_desktop() {
  6599. # Retrieve icon information if possible
  6600. [ "a${runner}" = "a" ] && return 1
  6601. [ "a${runhome}" = "a" ] && return 1
  6602. [ ! -d "${runhome}" ] && return 1
  6603. need_binary "touch"; need_binary "mkdir"; need_binary "chown"; need_binary "chmod"; need_binary "cat"
  6604. unset desktopdestination
  6605. if [ "a${DESKTOP}" = "a" ] && find_binary "xdg-user-dir"; then
  6606. DESKTOP=`${xdg_user_dirbin} DESKTOP`
  6607. [ "a${DESKTOP}" = "a" ] && DESKTOP=`${xdg_user_dirbin} desktop`
  6608. if [ "a${DESKTOP}" != "a" ]; then
  6609. DESKTOP=`echo "${DESKTOP}" | ${sedbin} -e "s/\/$//g"`
  6610. DESKTOP=`${basenamebin} "${DESKTOP}"`
  6611. fi
  6612. fi
  6613. if [ "a${DESKTOP}" != "a" ] && [ -d "${DESKTOP}" ]; then
  6614. desktopdestination="${DESKTOP}"
  6615. elif [ -d "${runhome}/Desktop" ]; then
  6616. desktopdestination="${runhome}/Desktop"
  6617. elif [ -d "${runhome}/desktop" ]; then
  6618. desktopdestination="${runhome}/desktop"
  6619. elif [ "a${HOME}" != "a" -a "a${DESKTOP}" != "a" ] && [ -d "${HOME}/${DESKTOP}" ]; then
  6620. desktopdestination="${HOME}/${DESKTOP}"
  6621. elif find_binary "pwd"; then
  6622. desktopdestination="`${pwdbin} 2> /dev/null`"
  6623. else
  6624. desktopdestination="."
  6625. fi
  6626. [ "a${desktopdestination}" = "a" ] && desktopdestination="."
  6627. debug "Destination of shortcut is: %s\n" "${desktopdestination}"
  6628. if status_connected && [ "a${statusicon}" != "a" -a "a${statusname}" != "a" ] && find_binary "wget" && find_binary "basename"; then
  6629. user_select "DESKTOPICON" "Desktop icon" "Select icon to use for shortcut" "OK" "Cancel" "SAKIS3G" "Sakis3G tux icon" "OPERATOR" "${statusname} icon"
  6630. selection=$?
  6631. case "${selection}" in
  6632. 1)
  6633. DESKTOPICON="SAKIS3G"
  6634. ;;
  6635. 2)
  6636. DESKTOPICON="OPERATOR"
  6637.  
  6638.  
  6639. ;;
  6640. 98)
  6641. return 98
  6642. ;;
  6643. *)
  6644. return 99
  6645. ;;
  6646. esac
  6647. else
  6648. debug "${statusicon} ${statusname}\n"
  6649. debug "Automatically selecting %s icon.\n" "Sakis3G"
  6650. DESKTOPICON="SAKIS3G"
  6651. fi
  6652. if [ "a${DESKTOPICON}" = "aOPERATOR" ]; then
  6653. extension=`${basenamebin} "${statusicon}" | ${cutbin} -d. -f2-`
  6654. verbose "Retrieving operator icon"
  6655. debug run_command "${rmbin} -f \"/tmp/sakis3g.$$.${ISPID}_logo.${extension}\""
  6656. debug run_command "${wgetbin} -O \"/tmp/sakis3g.$$.${ISPID}_logo.${extension}\" \"${statusicon}\""
  6657. if [ ! -s "/tmp/sakis3g.$$.${ISPID}_logo.${extension}" ]; then
  6658. debug run_command "${rmbin} -f \"/tmp/sakis3g.$$.${ISPID}_logo.${extension}\""
  6659. show_fmt_error "Unable to get %s icon, unknown reason." "${statusname}"
  6660. DESKTOPICON="SAKIS3G"
  6661. else
  6662. debug "Successfully retrieved %s icon.\n" "operator"
  6663. fi
  6664. fi
  6665. if [ "a${DESKTOPICON}" = "aSAKIS3G" ] && [ "a${PROVIDER}" != "a" ] && [ -x "${PROVIDER}" ]; then
  6666. extension="png"
  6667. "${PROVIDER}" getfile "files/sakis3g.png" > "/tmp/sakis3g.$$.${ISPID}_logo.${extension}" 2> /dev/null
  6668. if [ ! -s "/tmp/sakis3g.$$.${ISPID}_logo.${extension}" ]; then
  6669. debug run_command "${rmbin} -f \"/tmp/sakis3g.$$.${ISPID}_logo.${extension}\""
  6670. show_fmt_error "Unable to get %s icon, unknown reason." "Sakis3G"
  6671. DESKTOPICON=""
  6672. else
  6673. debug "Successfully retrieved %s icon.\n" "Sakis3G"
  6674. fi
  6675. fi
  6676. if [ -s "/tmp/sakis3g.$$.${ISPID}_logo.${extension}" ]; then
  6677. debug run_command "${mkdirbin} -p \"${runhome}/.local\""
  6678. debug run_command "${chownbin} ${runner} \"${runhome}/.local\""
  6679. debug run_command "${mkdirbin} -p \"${runhome}/.local/share\""
  6680. debug run_command "${chownbin} ${runner} \"${runhome}/.local/share\""
  6681. debug run_command "${mkdirbin} -p \"${runhome}/.local/share/icons\""
  6682. debug run_command "${chownbin} ${runner} \"${runhome}/.local/share/icons\""
  6683. if [ "a${extension}" = "agif" -o "a${extension}" = "aGIF" -o "a${extension}" = "aGif" ]; then
  6684. debug run_command "${mvbin} \"/tmp/sakis3g.$$.${ISPID}_logo.${extension}\" \"${runhome}/.local/share/icons/sakis3g.png\""
  6685. debug run_command "${rmbin} -f \"/tmp/sakis3g.$$.${ISPID}_logo.${extension}\""
  6686. extension="png"
  6687. else
  6688. debug run_command "${mvbin} \"/tmp/sakis3g.$$.${ISPID}_logo.${extension}\" \"${runhome}/.local/share/icons/sakis3g.${extension}\""
  6689. debug run_command "${rmbin} -f \"/tmp/sakis3g.$$.${ISPID}_logo.${extension}\""
  6690. fi
  6691. if [ -s "${runhome}/.local/share/icons/sakis3g.${extension}" ]; then
  6692. debug run_command "${chownbin} ${runner} \"${runhome}/.local/share/icons/sakis3g.${extension}\""
  6693. debug run_command "${chmodbin} 644 \"${runhome}/.local/share/icons/sakis3g.${extension}\""
  6694. icon="${runhome}/.local/share/icons/sakis3g.${extension}"
  6695. debug "Successfully installed icon to \"%s\".\n" "${runhome}/.local/share/icons/sakis3g.${extension}"
  6696. fi
  6697. else
  6698. debug "File %s does not exist or is empty.\n" "/tmp/sakis3g.$$.${ISPID}_logo.${extension}"
  6699. [ -f "/tmp/sakis3g.$$.${ISPID}_logo.${extension}" ] && debug run_command "${rmbin} -f \"/tmp/sakis3g.$$.${ISPID}_logo.${extension}\""
  6700. if [ -f "/usr/share/icons/gnome/scalable/status/nm-device-wireless.svg" ]; then
  6701. debug "Selecting default GNOME icon.\n"
  6702. icon="/usr/share/icons/gnome/scalable/status/nm-device-wireless.svg"
  6703. else
  6704. debug "Unable to locate any icon.\n"
  6705. icon=""
  6706. fi
  6707. fi
  6708. debug "Icon selected is: %s\n" "${icon}"
  6709. shortcuttitle="Sakis3G"
  6710. [ "a${DESKTOPICON}" = "aOPERATOR" ] && shortcuttitle="${statusproduct}" && [ "a${shortcuttitle}" = "a" ] && shortcuttitle="${statusname} Internet"
  6711. [ "a${shortcuttitle}" = "a" ] && shortcuttitle="Sakis3G"
  6712. commenttext=`format_text "Manages 3G internet connection and 3G USB modems"`
  6713. ${catbin} > "${desktopdestination}/sakis3g.desktop" <<endl
  6714. [Desktop Entry]
  6715. Version=1.0
  6716. Encoding=UTF-8
  6717. Type=Application
  6718. Terminal=false
  6719. Name=${shortcuttitle}
  6720. Name[en_US]=${shortcuttitle}
  6721. Exec=${ME} clicked
  6722. Comment=${commenttext}
  6723. Comment[en_US]=${commenttext}
  6724. Icon=${icon}
  6725. Icon[en_US]=${icon}
  6726. GenericName=
  6727. GenericName[en_US]=
  6728. endl
  6729. debug run_command "${catbin} \"${desktopdestination}/sakis3g.desktop\""
  6730. unset commenttext; unset shortcuttitle
  6731. if [ -s "${desktopdestination}/sakis3g.desktop" ]; then
  6732. debug run_command "${chownbin} ${runner} \"${desktopdestination}/sakis3g.desktop\""
  6733. debug run_command "${chmodbin} 750 \"${desktopdestination}/sakis3g.desktop\""
  6734. debug run_command "${lsbin} \"${desktopdestination}/sakis3g.desktop\""
  6735. finalnotify "Desktop shortcut created at %s." "${desktopdestination}/sakis3g.desktop"
  6736. unset desktopdestination
  6737. return 0
  6738. else
  6739. debug run_command "${rmbin} -f \"${desktopdestination}/sakis3g.desktop\""
  6740. unset desktopdestination
  6741. return 1
  6742. fi
  6743. }
  6744.  
  6745. action_modem() {
  6746. unset verbosecurrentcount
  6747. flow_select_modem "$@"
  6748. ret=$?
  6749. if [ "a${ret}" = "a0" ]; then
  6750. finalnotify "Modem selected.\n"
  6751. return 0
  6752. else
  6753. show_fmt_error "No modem selected.\n"
  6754. return ${ret}
  6755. fi
  6756. }
  6757.  
  6758. action_switch() {
  6759. unset verbosecurrentcount
  6760. flow_switch "$@"
  6761. ret=$?
  6762. if [ "a${ret}" = "a0" ]; then
  6763. if [ "a${NEWIDS}" = "a" ]; then
  6764. finalnotify "Modem switched.\n"
  6765. else
  6766. finalnotify "Modem switched to %s.\n" "${NEWIDS}"
  6767. fi
  6768. return 0
  6769. else
  6770. show_fmt_error "Failed to switch.\n"
  6771. return ${ret}
  6772. fi
  6773. }
  6774.  
  6775. action_setup() {
  6776. unset verbosecurrentcount
  6777. flow_setup "$@"
  6778. ret=$?
  6779. if [ "a${ret}" = "a0" ]; then
  6780. finalnotify "Modem setup residing on %s.\n" "${MODEM_TTY}"
  6781. return 0
  6782. else
  6783. show_fmt_error "Failed to setup modem.\n"
  6784. return ${ret}
  6785. fi
  6786. }
  6787.  
  6788. action_prepare() {
  6789. unset verbosecurrentcount
  6790. flow_prepare "$@"
  6791. ret=$?
  6792. if [ "a${ret}" = "a0" ]; then
  6793. finalnotify "Modem on %s is registered to %s.\n" "${MODEM_TTY}" "${ISPTEXT}"
  6794. return 0
  6795. else
  6796. show_fmt_error "Failed to prepare modem.\n"
  6797. return ${ret}
  6798. fi
  6799. }
  6800.  
  6801. action_connect() {
  6802. unset verbosecurrentcount
  6803. flow_connect "$@"
  6804. ret=$?
  6805. if [ "a${ret}" = "a0" ]; then
  6806. action_status "$@"
  6807. return 0
  6808. else
  6809. show_fmt_error "Failed to connect.\n"
  6810. return ${ret}
  6811. fi
  6812. }
  6813.  
  6814. action_compile() {
  6815. unset verbosecurrentcount
  6816. flow_compile "$@"
  6817. ret=$?
  6818. if [ "a${ret}" = "a0" ]; then
  6819. finalnotify "Succesfully recompiled.\n"
  6820. return 0
  6821. else
  6822. return ${ret}
  6823. fi
  6824. }
  6825.  
  6826. action_disconnect() {
  6827. unset verbosecurrentcount
  6828. if ! ppp_fast_status; then
  6829. finalnotify "Not connected.\n"
  6830. return 0
  6831. fi
  6832. flow_disconnect "$@"
  6833. ret=$?
  6834. if [ "a${ret}" = "a0" ]; then
  6835. finalnotify "Disconnected.\n"
  6836. return 0
  6837. else
  6838. show_fmt_error "Failed to disconnect.\n"
  6839. return ${ret}
  6840. fi
  6841. }
  6842.  
  6843. action_reconnect() {
  6844. unset verbosecurrentcount
  6845. unset disconnectedbefore
  6846. if ppp_fast_status; then
  6847. status_connected
  6848. flow_disconnect "$@"
  6849. ret=$?
  6850. if [ "${ret}" -ne "0" ]; then
  6851. show_fmt_error "Failed to disconnect.\n"
  6852. return ${ret}
  6853. fi
  6854. disconnectedbefore=1
  6855. fi
  6856. flow_connect "$@"
  6857. ret=$?
  6858. if [ "a${ret}" = "a0" -a "a${disconnectedbefore}" = "a1" ]; then
  6859. unset disconnectedbefore
  6860. finalnotify "Reconnected to %s.\n" "${ISPTEXT}"
  6861. return 0
  6862. elif [ "a${ret}" = "a0" ]; then
  6863. action_status "$@"
  6864. return 0
  6865. else
  6866. unset disconnectedbefore
  6867. show_fmt_error "Failed to connect.\n"
  6868. return ${ret}
  6869. fi
  6870. }
  6871.  
  6872. action_toggle() {
  6873. unset verbosecurrentcount
  6874. unset disconnectedbefore
  6875. if ppp_fast_status; then
  6876. action_disconnect "$@"
  6877. ret=$?
  6878. else
  6879. action_connect "$@"
  6880. ret=$?
  6881. fi
  6882. return ${ret}
  6883. }
  6884.  
  6885. action_clicked() {
  6886. unset verbosecurrentcount
  6887. unset ret; unset tester
  6888. tester="${SGUI}"; [ "a${tester}" = "a" ] && tester="terminal"
  6889. case "${SGUI}" in
  6890. zenity|dialog|Xdialog|whiptail|kdialog)
  6891. action_menu "$@"
  6892. ret=$?
  6893. ;;
  6894. "9menu")
  6895. action_menu "$@"
  6896. ret=$?
  6897. ;;
  6898. terminal|"interactive terminal")
  6899. if [ "a${interactive}" = "a" ]; then
  6900. action_toggle "$@"
  6901. ret=$?
  6902. else
  6903. unset interactive
  6904. action_toggle "$@"
  6905. ret=$?
  6906. export interactive=yes
  6907. fi
  6908. ;;
  6909. esac
  6910. return ${ret}
  6911. }
  6912.  
  6913. # Method invoked when "more" action was requested.
  6914. action_more() {
  6915. unset verbosecurrentcount
  6916. unset usboptions; [ "a${binaryfree}" = "a" ] && usboptions="\"COMPILE\" \"Compile embedded Usb-ModeSwitch\""
  6917. alwaysoptions="\"DESKTOP\" \"Create desktop shortcut\" \"ABOUT\" \"About Sakis3G\" \"HELP\" \"Show help\" \"EXIT\" \"Exit\""
  6918. if ppp_fast_status; then
  6919. stateoptions="\"DISCONNECT\" \"Disconnect\" \"RECONNECT\" \"Disconnect and then connect again\" \"INFO\" \"Connection information\" \"REPORT\" \"Generate success report\""
  6920. else
  6921. stateoptions="\"CONNECT\" \"Connect with 3G\" \"PREPARE\" \"Only prepare modem (Setup + PIN unlock + Register Network + Update HAL)\" \"SETUP\" \"Only setup modem (Switch + Load module + Setup tty)\" \"SWITCH\" \"Only switch modem (if applicable)\""
  6922. fi
  6923. eval user_select \"MOREMENU\" \"Please select an action\" \"Choose action for Sakis3G script to follow.\" \"OK\" \"Cancel\" ${stateoptions} ${usboptions} ${alwaysoptions}
  6924. # in
  6925. ret=$?
  6926. unset usboptions; unset alwaysoptions; unset stateoptions
  6927. case "${ret}" in
  6928. 99)
  6929. return 99
  6930. ;;
  6931. 98)
  6932. debug "No action selected.\n"
  6933. # Instead of returning 98, returns 0 to allow chaining of actors
  6934. # EXIT menu item is used from breaking chain
  6935. unset MOREMENU
  6936. return 0
  6937. ;;
  6938. *)
  6939. case "a${MOREMENU}" in
  6940. aCONNECT)
  6941. action_connect "$@"
  6942. ;;
  6943. aDISCONNECT)
  6944. action_disconnect "$@"
  6945. ;;
  6946. aRECONNECT)
  6947. action_reconnect "$@"
  6948. ;;
  6949. aPREPARE)
  6950. action_prepare "$@"
  6951. ;;
  6952. aSETUP)
  6953. action_setup "$@"
  6954. ;;
  6955. aSWITCH)
  6956. action_switch "$@"
  6957. ;;
  6958. aCOMPILE)
  6959. action_compile "$@"
  6960. ;;
  6961. aDESKTOP)
  6962. action_desktop "$@"
  6963. ;;
  6964. aINFO)
  6965. action_info "$@"
  6966. ;;
  6967. aREPORT)
  6968. action_report "$@"
  6969. ;;
  6970. aHELP)
  6971. show_help
  6972. ;;
  6973. aABOUT)
  6974. action_about "$@"
  6975. ;;
  6976. aEXIT)
  6977. unset MOREMENU
  6978. return 98
  6979. ;;
  6980. a)
  6981. unset MOREMENU
  6982. return 98
  6983. ;;
  6984. *)
  6985. show_fmt_error "Option %s not implemented.\n" "${MOREMENU}"
  6986. ;;
  6987. esac
  6988. ;;
  6989. esac
  6990. unset MOREMENU
  6991. action_more "$@"
  6992. return $?
  6993. }
  6994.  
  6995. # Method invoked when "menu" action was requested.
  6996. action_menu() {
  6997. unset verbosecurrentcount
  6998. if ppp_fast_status; then
  6999. menuoptions="\"DISCONNECT\" \"Disconnect\" \"INFO\" \"Connection information\" \"DESKTOP\" \"Create shortcut\""
  7000. else
  7001. menuoptions="\"CONNECT\" \"Connect with 3G\""
  7002. fi
  7003. menuoptions="${menuoptions} \"MOREMENU\" \"More options...\" \"ABOUT\" \"About Sakis3G\""
  7004. menuoptions="${menuoptions} \"EXIT\" \"Exit\""
  7005. eval user_select \"MENU\" \"Please select an action\" \"Choose action for Sakis3G script to follow.\" \"OK\" \"Cancel\" ${menuoptions}
  7006. # in
  7007. ret=$?; unset menuoptions
  7008. case "${ret}" in
  7009. 99)
  7010. return 99
  7011. ;;
  7012. 98)
  7013. debug "No action selected.\n"
  7014. # Instead of returning 98, returns 0 to allow chaining of actors
  7015. # EXIT menu item is used from breaking chain
  7016. return 0
  7017. ;;
  7018. *)
  7019. case "a${MENU}" in
  7020. aCONNECT)
  7021. action_connect "$@"
  7022. ;;
  7023. aDISCONNECT)
  7024. action_disconnect "$@"
  7025. ;;
  7026. aMOREMENU)
  7027. action_more "$@"
  7028. ret=$?; [ "a${ret}" = "a98" ] && unset MENU && return 98
  7029. ;;
  7030. aINFO)
  7031. action_info "$@"
  7032. ;;
  7033. aDESKTOP)
  7034. action_desktop "$@"
  7035. ;;
  7036. aHELP)
  7037. show_help
  7038. ;;
  7039. aABOUT)
  7040. action_about "$@"
  7041. ;;
  7042. aEXIT)
  7043. debug "Breaking chain.\n"
  7044. unset MENU
  7045. return 98
  7046. ;;
  7047. a)
  7048. unset MENU
  7049. return 98
  7050. ;;
  7051. *)
  7052. show_fmt_error "Option %s not implemented.\n" "${MENU}"
  7053. ;;
  7054. esac
  7055. ;;
  7056. esac
  7057. unset MENU
  7058. action_menu "$@"
  7059. return $?
  7060. }
  7061.  
  7062. helper_cleaner() {
  7063. [ -f "/tmp/sakis3g.helper.icon.green.$$.png" ] && ${rmbin} -f "/tmp/sakis3g.helper.icon.green.$$.png"
  7064. [ -f "/tmp/sakis3g.helper.icon.red.$$.png" ] && ${rmbin} -f "/tmp/sakis3g.helper.icon.red.$$.png"
  7065. [ -f "/tmp/sakis3g.helper.icon.yellow.$$.png" ] && ${rmbin} -f "/tmp/sakis3g.helper.icon.yellow.$$.png"
  7066. [ "a${helperpid}" != "a" ] && ! notrunning "${helperpid}" && ${killbin} -1 ${helperpid} 2> /dev/null > /dev/null
  7067. [ "a${helperpid}" != "a" ] && ! notrunning "${helperpid}" && ${killbin} -9 ${helperpid} 2> /dev/null > /dev/null
  7068. if [ "a${balloons}" != "a" ] && [ "a${BALOONIZERPID}" != "a" -o "a${BALOONIZER}" != "a" ]; then
  7069. [ "a${BALOONIZER}" != "a" ] && [ -f "${BALOONIZER}" ] && debug run_command "${rmbin} -f \"${BALOONIZER}\""
  7070. [ "a${BALOONIZERPID}" != "a" ] && ! notrunning "${BALOONIZERPID}" && debug run_command "${killbin} -1 ${BALOONIZERPID}"
  7071. [ "a${BALOONIZERPID}" != "a" ] && ! notrunning "${BALOONIZERPID}" && debug run_command "${killbin} -9 ${BALOONIZERPID}"
  7072. find_binary "ps" && tailpid=`${psbin} -A -o pid,command | ${grepbin} "${tailbin} -f ${BALOONIZER}" | ${grepbin} -v "${grepbin}" | ${cutbin} -d\ -f1`
  7073. [ "a${tailpid}" != "a" ] && ! notrunning "${tailpid}" && debug run_command "${killbin} -1 ${tailpid}"
  7074. [ "a${tailpid}" != "a" ] && ! notrunning "${tailpid}" && debug run_command "${killbin} -9 ${tailpid}"
  7075. unset BALOONIZERPID; unset BALOONIZER
  7076. fi
  7077. }
  7078.  
  7079. action_helper() {
  7080. if ! find_binary "zenity"; then
  7081. show_fmt_error "Unable to install tray icon to system notification area. You need to install %s.\n" "zenity"
  7082. return 0
  7083. fi
  7084. need_binary "kill"
  7085. [ "a${PROVIDER}" = "a" ] && return 1
  7086. [ ! -x "${PROVIDER}" ] && return 1
  7087. if we_are_root_already && [ "a${MENU}" != "a" ]; then
  7088. debug "Calling menu since state variable MENU is set.\n"
  7089. action_menu "$@"
  7090. unset MENU
  7091. debug "MENU actor returned. Will now run as helper.\n"
  7092. fi
  7093. debug run_command "${rmbin} -f /tmp/sakis3g.helper.icon.red.$$.png /tmp/sakis3g.helper.icon.green.$$.png /tmp/sakis3g.helper.icon.yellow.$$.png"
  7094. addexittrap helper_cleaner
  7095. ${PROVIDER} getfile files/sakis3g.red.png > "/tmp/sakis3g.helper.icon.red.$$.png" 2> /dev/null
  7096. ${PROVIDER} getfile files/sakis3g.green.png > "/tmp/sakis3g.helper.icon.green.$$.png" 2> /dev/null
  7097. ${PROVIDER} getfile files/sakis3g.yellow.png > "/tmp/sakis3g.helper.icon.yellow.$$.png" 2> /dev/null
  7098. if [ ! -s "/tmp/sakis3g.helper.icon.green.$$.png" ]; then
  7099. debug "Failed to retrieve green/connected state icon.\n"
  7100. return 1
  7101. elif [ ! -s "/tmp/sakis3g.helper.icon.red.$$.png" ]; then
  7102. debug "Failed to retrieve red/no hardware state icon.\n"
  7103. return 1
  7104. elif [ ! -s "/tmp/sakis3g.helper.icon.yellow.$$.png" ]; then
  7105. debug "Failed to retrieve yellow/not connected state icon.\n"
  7106. return 1
  7107. fi
  7108. debug run_command "${chmodbin} 644 \"/tmp/sakis3g.helper.icon.red.$$.png\""
  7109. debug run_command "${chmodbin} 644 \"/tmp/sakis3g.helper.icon.green.$$.png\""
  7110. debug run_command "${chmodbin} 644 \"/tmp/sakis3g.helper.icon.yellow.$$.png\""
  7111. if [ "a${balloons}" != "a" ]; then
  7112. # Establish balloon helper now that will be accessible from root session later
  7113. if [ "a${BALOONIZER}" = "a" ]; then
  7114. BALOONIZER="/tmp/sakis3g.baloon.pipe.$$"
  7115. debug run_command "${rmbin} -f \"${BALOONIZER}\""
  7116. debug run_command "${touchbin} -f \"${BALOONIZER}\""
  7117. if [ -w "${BALOONIZER}" ]; then
  7118. ${printfbin} "\n\n\n\n\nvisible:false\n" >> "${BALOONIZER}"
  7119. export BALOONIZER
  7120. eval ${tailbin} -f "${BALOONIZER}" "2> /dev/null" | ${zenitybin} --notification --listen > /dev/null 2> /dev/null &
  7121. BALOONIZERPID=$!; eval "disown -a" > /dev/null 2> /dev/null
  7122. export BALOONIZERPID
  7123. ${printfbin} "visible:false\n" >> "${BALOONIZER}"
  7124. ${printfbin} "visible:false\nvisible:false\n" >> "${BALOONIZER}"
  7125. debug "Balloon pipe created. Location is \"%s\" and runs with PID %d.\n" "${BALOONIZER}" "${BALOONIZERPID}"
  7126. else
  7127. debug "Error while creating pipe for balloons.\n"
  7128. debug run_command "${rmbin} -f \"${BALOONIZER}\""
  7129. unset BALOONIZER
  7130.  
  7131. fi
  7132. elif [ -w "${BALOONIZER}" ]; then
  7133. debug "Will be using already existing balloon pipe: %s\n" "${BALOONIZER}"
  7134. elif [ ! -w "${BALOONIZER}" ]; then
  7135. debug "Balloons pipe \"%s\" exists but no write access granted.\n" "${BALOONIZER}"
  7136. fi
  7137. [ "a${BALOONIZER}" = "a" ] && debug "No balloons will be appearing.\n"
  7138. fi
  7139. debug "All set. Going into helper mode.\n"
  7140. while [ "1" = "1" ]
  7141. do
  7142. if ppp_slow_status && status_connected; then
  7143. helpertext="`translate_text "Connected"`"
  7144. [ "a${statusname}" != "a" ] && helpertext="${statusname}"
  7145. helperconnected=2
  7146. else
  7147. usb_connected_modems
  7148. if [ "a${usb_modem_devices}" != "a" ]; then
  7149. helpertext="`translate_text "Not connected"`"
  7150. helperconnected=1
  7151. helpermodems=`echo "${usb_modem_devices}" | ${cutbin} -d: -f3- | ${trbin} "\n" " "`
  7152. else
  7153. helpertext="`translate_text "No USB modem"`"
  7154. helperconnected=0
  7155. fi
  7156. fi
  7157. # Check if something was changed not by us. Then balloon user.
  7158. if [ "a${balloons}" != "a" ]; then
  7159. if [ "a${BALOONIZER}" != "a" ] && [ "a${previousstatus}" != "a" -a "a${previousstatus}" != "a${helperconnected}" ]; then
  7160. debug "Status changed. Not by us.\n"
  7161. notificationtext=`translate_text "Notification"`
  7162. if [ "a${helperconnected}" = "a2" ]; then
  7163. iconpath="/tmp/sakis3g.helper.icon.green.$$.png"
  7164. elif [ "a${helperconnected}" = "a1" ]; then
  7165. iconpath="/tmp/sakis3g.helper.icon.yellow.$$.png"
  7166. elif [ "a${helperconnected}" = "a0" ]; then
  7167. iconpath="/tmp/sakis3g.helper.icon.red.$$.png"
  7168. else
  7169. iconpath="/tmp/sakis3g.helper.icon.yellow.$$.png"
  7170. fi
  7171. unset largetext; unset smalltext
  7172. if [ "a${previousstatus}" = "a0" -a "a${helperconnected}" = "a1" ]; then
  7173. largetext=`translate_text "USB Modem plugged"`
  7174. smalltext=`format_text "USB Modem %s is now plugged on computer." "${helpermodems}"`
  7175. elif [ "a${helperconnected}" = "a2" ]; then
  7176. largetext=`translate_text "Connected"`
  7177. if [ "a${MODEM_VARIANT}" != "a" -a "a${statusname}" != "a" ]; then
  7178. smalltext=`format_text "Modem %s is now connected to %s." "${MODEM_VARIANT}" "${statusname}"`
  7179. elif [ "a${MODEM_VARIANT}" != "a" ]; then
  7180. smalltext=`format_text "Modem %s is now connected." "${MODEM_VARIANT}"`
  7181. elif [ "a${statusname}" != "a" ]; then
  7182. smalltext=`format_text "You are now connected to %s." "${statusname}"`
  7183. else
  7184. smalltext=`translate_text "Computer is now connected.\n"`
  7185. fi
  7186. elif [ "a${previousstatus}" = "a1" -a "a${helperconnected}" = "a0" ]; then
  7187. largetext=`translate_text "USB Modem Unplugged"`
  7188. smalltext=`translate_text "No USB modem is anymore plugged on computer."`
  7189. elif [ "a${previousstatus}" = "a2" ]; then
  7190. largetext=`translate_text "Disconnected"`
  7191. if [ "a${helperconnected}" = "a0" ]; then
  7192. smalltext=`translate_text "You were disconnected due to modem being unplugged."`
  7193. elif [ "a${helperconnected}" = "a1" ]; then
  7194. smalltext=`translate_text "You are now disconnected from operator."`
  7195. fi
  7196. fi
  7197. debug "Will display baloon:\nIcon: %s\nTitle: %s\nMessage: %s\n" "${iconpath}" "${largetext}" "${smalltext}"
  7198. if [ "a${smalltext}" != "a" -a "a${largetext}" != "a" ]; then
  7199. ${printfbin} "icon:%s\n" "${iconpath}" >> "${BALOONIZER}" 2> /dev/null
  7200. ${printfbin} "tooltip:%s\n" "${notificationtext}" >> "${BALOONIZER}" 2> /dev/null
  7201. ${printfbin} "visible:true\n" >> "${BALOONIZER}" 2> /dev/null
  7202. ${printfbin} "message:%s\\\n<small>%s</small>\n" "${largetext}" "${smalltext}" >> "${BALOONIZER}" 2> /dev/null
  7203. ${printfbin} "visible:false\n" >> "${BALOONIZER}" 2> /dev/null
  7204. fi
  7205. unset iconpath; unset smalltext; unset largetext; unset notificationtext
  7206. fi
  7207. previousstatus="${helperconnected}"
  7208. fi
  7209. if [ "a${helpertext}" != "a${announcedtext}" -a "a${helperpid}" != "a" ] && ! notrunning "${helperpid}"; then
  7210. [ "a${helperpid}" != "a" ] && ! notrunning "${helperpid}" && ${killbin} -1 ${helperpid} 2> /dev/null > /dev/null
  7211. [ "a${helperpid}" != "a" ] && ! notrunning "${helperpid}" && ${killbin} -9 ${helperpid} 2> /dev/null > /dev/null
  7212. notrunning "${helperpid}" && unset helperpid
  7213. fi
  7214. if [ "a${helperpid}" = "a" ]; then
  7215. if [ "a${helperconnected}" = "a2" ]; then
  7216. ${zenitybin} --notification --window-icon=/tmp/sakis3g.helper.icon.green.$$.png --text="${helpertext}" > /dev/null 2> /dev/null &
  7217. elif [ "a${helperconnected}" = "a1" ]; then
  7218. ${zenitybin} --notification --window-icon=/tmp/sakis3g.helper.icon.yellow.$$.png --text="${helpertext}" > /dev/null 2> /dev/null &
  7219. elif [ "a${helperconnected}" = "a0" ]; then
  7220. ${zenitybin} --notification --window-icon=/tmp/sakis3g.helper.icon.red.$$.png --text="${helpertext}" > /dev/null 2> /dev/null &
  7221. else
  7222. ${zenitybin} --notification --text="${helpertext}" > /dev/null 2> /dev/null &
  7223. fi
  7224. helperpid=$!;
  7225. eval "disown -a" > /dev/null 2> /dev/null
  7226. announcedtext="${helpertext}"
  7227. export helperpid
  7228. elif notrunning "${helperpid}"; then
  7229. unset helperpid; unset previousstatus
  7230. debug "User clicked on icon.\n"
  7231. action_menu "$@"; ret=$?
  7232. if [ "${ret}" -eq "98" ]; then
  7233. if [ "a${forever}" != "a" ]; then
  7234. debug "User selected exit on MENU. However, \"forever\" is set.\n"
  7235. unset MENU; unset MOREMENU;
  7236. else
  7237. debug "User selected exit on MENU. Will ask for confirmation to exit.\n"
  7238. #${zenitybin} --question --title="`translate_text "Confirm exit"`" --text="`translate_text "Exiting will also remove tray icon. Are you sure you want to exit Sakis3G?"`"
  7239. #ret=$?
  7240. #if [ "a${ret}" = "a0" ]; then
  7241. if user_confirm "helperexit" "Confirm exit" "Exiting will also remove tray icon. Are you sure you want to exit Sakis3G?" "Yes" "No" "reset"; then
  7242. debug "User confirmed he wants to exit. Helper will die.\n"
  7243. break
  7244. else
  7245. debug "User did not confirm.\n"
  7246. unset MENU; unset MOREMENU
  7247. fi
  7248. fi
  7249. fi
  7250. debug "Resuming helper operation.\n"
  7251. else
  7252. debug "Helper is sleeping for a second.\n"
  7253. sleep 1
  7254. fi
  7255. done
  7256. helper_cleaner
  7257. return 98
  7258. }
  7259.  
  7260. # Method called when called as a helper to hold HAL lock
  7261. action_holdlock() {
  7262. unset verbosecurrentcount
  7263. [ "a$2" != "a" ] && [ -f "$2" ] && checkfile="$2" && echo "$$" > "${checkfile}"
  7264. unset TRAPS
  7265. while [ "1" = "1" ]
  7266. do
  7267. if [ "a${PROVIDER}" = "a" ]; then
  7268. ppppp=`ps -p $$ -o ppid=`
  7269. else
  7270. ppppp=`ps -p $$ -o ppid=`
  7271. ppppp=`ps -p ${ppppp} -o ppid=`
  7272. fi
  7273. ppppp=`echo $ppppp`
  7274. [ "a${ppppp}" = "a1" ] && exit 0
  7275. [ "a${ppppp}" = "a${PPID}" ] && exit 0
  7276. [ "a${checkfile}" != "a" ] && [ ! -f "${checkfile}" ] && exit 0
  7277. #echo $ppppp
  7278. sleep 1
  7279. done
  7280. }
  7281.  
  7282. action_state() {
  7283. unset verbosecurrentcount
  7284. state_variables "SGUI interactive stick_to_console DEBUG LOCALAUTHORITY BALOONIZER"
  7285. notify "To automatically repeat result of last action, use following command line:\n%s\n" "${ME} ${actors} ${statevariables}"
  7286. return 0
  7287. }
  7288.  
  7289. action_udevrule() {
  7290. unset verbosecurrentcount
  7291. state_variables "SGUI MODEM interactive stick_to_console USBMODEM OTHER DEBUG LOCALAUTHORITY BALOONIZER"
  7292. # statevariables=`echo "${statevariables}" | ${sedbin} -e "s/\"/\\\\\\\\\"/g"`
  7293. statevariables=`echo "${statevariables}" | ${sedbin} -e "s/\"//g"`
  7294. unset content
  7295. candidates="${MODEM}"
  7296. [ "a${candidates}" = "aOTHER" ] && unset candidates
  7297. [ "a${candidates}" = "aOTHER" -a "a${OTHER}" = "aUSBMODEM" -a "a${USBMODEM}" != "a" ] && candidates="${USBMODEM}"
  7298. localactors=`echo ${actors}`
  7299. for candidate in ${candidates}
  7300. do
  7301. modemvendor=`echo ${candidate} | ${cutbin} -d: -f1 -s`
  7302. modemproduct=`echo ${candidate} | ${cutbin} -d: -f2 -s`
  7303. if [ "a${modemvendor}" != "a" -a "a${modemproduct}" != "a" ]; then
  7304. content=`${printfbin} "%s\nACTION==\"add\", SUBSYSTEM==\"usb\", ATTRS{idVendor}==\"%s\", ATTRS{idProduct}==\"%s\", ATTRS{bInterfaceNumber}==\"00\", RUN+=\"%s %s %s MODEM=OTHER OTHER=USBMODEM USBMODEM=%s:%s\"\n\n" "${content}" "${modemvendor}" "${modemproduct}" "${ME}" "${localactors}" "${statevariables}" "${modemvendor}" "${modemproduct}"`
  7305. break;
  7306. fi
  7307. done
  7308. unset localactors; unset modemvendor; unset modemproduct; unset candidate; unset candidates; unset statevariables
  7309. term_clearline
  7310. if [ "a${content}" != "a" ]; then
  7311. format_text "To automatically repeat result of last action, immediately upon device connection, include following line within a %s file within \"%s\" directory:\n%s\n" ".rules" "/etc/udev/rules.d/" "${content}"
  7312. else
  7313. format_text "No USB modem was selected. No need for a udev example.\n"
  7314. fi
  7315. unset content
  7316. return 0
  7317. }
  7318.  
  7319. action_about() {
  7320. unset verbosecurrentcount
  7321. notify "%s\n" "`show_version`"
  7322. return 0
  7323. }
  7324.  
  7325. show_man() {
  7326. destination="$1"
  7327. case "a${destination}" in
  7328. ausb_modeswitch|ausb-modeswitch)
  7329. destination="dependencies/usb-modeswitch/usb_modeswitch.1"
  7330. ;;
  7331. asakis3g.conf)
  7332. destination="man/sakis3g.conf.5"
  7333. ;;
  7334. *)
  7335. destination="man/sakis3g.1"
  7336. ;;
  7337. esac
  7338. debug "Destination man page is: %s\n" "${destination}"
  7339. [ "a${PROVIDER}" = "a" ] && return 1
  7340. [ ! -x "${PROVIDER}" ] && return 1
  7341. need_binary "man"
  7342. need_binary "rm"
  7343. "${PROVIDER}" getfile "${destination}" > "/tmp/sakis3g.man.$$"
  7344. debug "Will now display man page %s.\n" "${destination}"
  7345. [ -s "/tmp/sakis3g.man.$$" ] && ${manbin} -l "/tmp/sakis3g.man.$$"
  7346. ${rmbin} -f "/tmp/sakis3g.man.$$"
  7347. return 0
  7348. }
  7349.  
  7350. action_report() {
  7351. unset verbosecurrentcount
  7352. flow_report "$@"
  7353. ret=$?
  7354. if [ "a${ret}" = "a0" ]; then
  7355. return 0
  7356. else
  7357. return ${ret}
  7358. fi
  7359. }
  7360.  
  7361. # Executes actor $1 with the arguments
  7362. run_action() {
  7363. actioncandidate=$1
  7364. [ "$#" -gt "0" ] && shift
  7365. unset verbosecurrentcount
  7366. newactors="${actors} ${actioncandidate}"
  7367. case "${actioncandidate}" in
  7368. about)
  7369. action_about "$@"
  7370. actionresult=$?
  7371. ;;
  7372. desktop)
  7373. action_desktop "$@"
  7374. actionresult=$?
  7375. ;;
  7376. helper)
  7377. action_helper "$@"
  7378. actionresult=$?
  7379. ;;
  7380. status)
  7381. action_status "$@"
  7382. actionresult=$?
  7383. ;;
  7384. connected)
  7385. ppp_fast_status
  7386. actionresult=$?
  7387. ;;
  7388. disconnected)
  7389. if ppp_fast_status; then
  7390. actionresult=99
  7391. else
  7392. actionresult=0
  7393. fi
  7394. ;;
  7395. plugged)
  7396. if usb_connected_modems; then
  7397. actionresult=0
  7398. else
  7399. actionresult=99
  7400. fi
  7401. ;;
  7402. unplugged)
  7403. if usb_connected_modems; then
  7404. actionresult=99
  7405. else
  7406. actionresult=0
  7407. fi
  7408. ;;
  7409. switched)
  7410. if usb_connected_modems; then
  7411. if modeswitch_switchable_devices; then
  7412. actionresult=99
  7413. else
  7414. actionresult=0
  7415. fi
  7416. else
  7417. actionresult=0
  7418. fi
  7419. ;;
  7420. switchable)
  7421. if modeswitch_switchable_devices; then
  7422. actionresult=0
  7423. else
  7424. actionresult=99
  7425. fi
  7426. ;;
  7427. wait|sleep)
  7428. debug "Sleeping for 1 second.\n"
  7429. sleep 1
  7430. actionresult=0
  7431. ;;
  7432. info)
  7433. action_info "$@"
  7434. actionresult=$?
  7435. ;;
  7436. clicked)
  7437. action_clicked "$@"
  7438. actionresult=$?
  7439. ;;
  7440. modem|select)
  7441. action_modem "$@"
  7442. actionresult=$?
  7443. ;;
  7444. switchonly)
  7445. action_switch "$@"
  7446. actionresult=$?
  7447. ;;
  7448. setup)
  7449. action_setup "$@"
  7450. actionresult=$?
  7451. ;;
  7452. prepare|init)
  7453. action_prepare "$@"
  7454. actionresult=$?
  7455. ;;
  7456. connect|start)
  7457. action_connect "$@"
  7458. actionresult=$?
  7459. ;;
  7460. toggle)
  7461. action_toggle "$@"
  7462. actionresult=$?
  7463. ;;
  7464. reconnect|restart)
  7465. action_reconnect "$@"
  7466. actionresult=$?
  7467. ;;
  7468. disconnect|stop)
  7469. action_disconnect "$@"
  7470. actionresult=$?
  7471. ;;
  7472. more|moremenu|menumore)
  7473. action_more "$@"
  7474. actionresult=$?
  7475. ;;
  7476. menu)
  7477. action_menu "$@"
  7478. actionresult=$?
  7479. ;;
  7480. state)
  7481. action_state "$@"
  7482. actionresult=$?
  7483. ;;
  7484. udevrule)
  7485. action_udevrule "$@"
  7486. actionresult=$?
  7487. ;;
  7488. compile|recompile)
  7489. action_compile "$@"
  7490. actionresult=$?
  7491. ;;
  7492. report)
  7493. action_report "$@"
  7494. actionresult=$?
  7495. ;;
  7496. *)
  7497. newactors="${actors}"
  7498. unset actioncandidate
  7499. actionresult=0
  7500. ;;
  7501. esac
  7502. actors="${newactors}"
  7503. unset actioncandidate; unset newactors
  7504. [ "a${actionresult}" = "a" ] && actionresult=99
  7505. return ${actionresult}
  7506. }
  7507.  
  7508. # Executes actors in the order defined
  7509. action_command() {
  7510. unset actors
  7511. while [ "$#" -gt "0" ]
  7512. do
  7513. if [ "a${1}" = "anot" -o "a${1}" = "aNot" -o "a${1}" = "a!" ]; then
  7514. [ "$#" -gt "0" ] && shift
  7515. actors="${actors} not"
  7516. run_action "$@"
  7517. actionresult=$?
  7518. if [ "${actionresult}" -eq "0" ]; then
  7519. actionresult=99
  7520. debug "Actor \"%s\" returned %d, \"not\" operator instructs to abort.\n" "${1}" "${actionresult}"
  7521. break
  7522. else
  7523. debug "Actor \"%s\" returned %d, but was inverted by \"not\" operator.\n" "${1}" "${actionresult}"
  7524. actionresult=0
  7525. fi
  7526. elif [ "a${1}" = "aignore" -o "a${1}" = "aIgnore" -o "a${1}" = "aignoring" -o "a${1}" = "aIgnoring" -o "a${1}" = "a|" ]; then
  7527. [ "$#" -gt "0" ] && shift
  7528. actors="${actors} ignore"
  7529. run_action "$@"
  7530. actionresult=$?
  7531. if [ "${actionresult}" -ne "0" ]; then
  7532. debug "Actor \"%s\" returned %d, \"ignore\" operator instructs to continue.\n" "${1}" "${actionresult}"
  7533. actionresult=0
  7534. fi
  7535. elif [ "a${1}" != "a" ] && ! run_action "$@"; then
  7536. debug "Aborting execution chain due to actor \"%s\" returning %d.\n" "${1}" "${actionresult}"
  7537. break
  7538. fi
  7539. [ "$#" -gt "0" ] && shift
  7540. done
  7541. unset arg
  7542. actors=`echo ${actors}`
  7543. if [ "a${actors}" = "a" ]; then
  7544. debug "No actors defined.\n"
  7545. unset actors
  7546. return 1
  7547. else
  7548. debug "Following actors executed: %s\n" "${actors}"
  7549. unset actors
  7550. return 0
  7551. fi
  7552. }
  7553.  
  7554. # Checks if arguments defined a dummy action and aborts if it does
  7555. no_action_command() {
  7556. for arg in $@
  7557. do
  7558. case "$arg" in
  7559. --help|help)
  7560. unset arg
  7561. if find_binary "fold"; then
  7562. if [ "a${COLUMNS}" != "a" ]; then
  7563. raw_help | ${foldbin} -s -w "${COLUMNS}"
  7564. else
  7565. raw_help | ${foldbin} -s -w "80"
  7566. fi
  7567. else
  7568. raw_help
  7569. fi
  7570. return 0
  7571. ;;
  7572. --version|version)
  7573. unset arg
  7574. show_version
  7575. return 0
  7576. ;;
  7577. --holdlock|holdlock)
  7578. unset arg
  7579. action_holdlock "$@"
  7580. return 0
  7581. ;;
  7582. usb_modeswitch)
  7583. if [ "a${binaryfree}" != "a" ]; then
  7584. show_fmt_error "You are running a binary free version of Sakis3G. This action is not available.\n"
  7585. fi
  7586. return 0
  7587. ;;
  7588. man)
  7589. if [ "a${stripped}" != "a" ]; then
  7590. show_fmt_error "You are running a stripped version of Sakis3G. This action is not available.\n"
  7591. else
  7592. shift
  7593. show_man "$@"
  7594. fi
  7595. unset arg
  7596. return 0
  7597. ;;
  7598. esac
  7599. done
  7600. unset arg
  7601. return 1
  7602. }
  7603.  
  7604. # When no actor defined, executes default actor of GUI selected.
  7605. default_actor() {
  7606. debug "Executing default actor for \"%s\".\n" "${SGUI}"
  7607. if voodoo_mode; then
  7608. debug "Voodoo actions will happen.\n"
  7609. if action_connect "$@"; then
  7610. actors="connect"
  7611. action_state "$@"
  7612. return $?
  7613. else
  7614. return $?
  7615. fi
  7616. fi
  7617. case "${SGUI}" in
  7618. whiptail|dialog|Xdialog|zenity|kdialog)
  7619. action_menu "$@"
  7620. return $?
  7621. ;;
  7622. "9menu")
  7623. action_menu "$@"
  7624. return $?
  7625. ;;
  7626. "interactive terminal")
  7627. action_menu "$@"
  7628. return $?
  7629. ;;
  7630. "terminal")
  7631. show_help
  7632. return $?
  7633. ;;
  7634. *)
  7635. debug "No default actor for this GUI. Showing help.\n"
  7636. show_help
  7637. return $?
  7638. ;;
  7639. esac
  7640. }
  7641.  
  7642. voodoo_mode() {
  7643. [ "a${voodoo}" != "a" ] && return 0
  7644. return 1
  7645. }
  7646.  
  7647. guruplug_led() {
  7648. if [ "a$1" = "anoexecute" ]; then
  7649. noexecute=1
  7650. shift
  7651. elif [ "a$1" = "aall" ]; then
  7652. shift
  7653. guruplug_leds "$@"
  7654. return 0
  7655. else
  7656. noexecute=0
  7657. fi
  7658. ledcolor=""; ledname=""; othercolor=""; otherled=""; ledblink=""; ledon=""; ledoff=""
  7659. for ledarg in "$@"
  7660. do
  7661. case "a${ledarg}" in
  7662. ared|aRED|aRed|aR|ar|"a${redcolor}")
  7663. ledcolor="${redcolor}"
  7664. othercolor="${othercolor} ${greencolor}"
  7665. ;;
  7666. agreen|aGREEN|aGreen|aG|ag|"a${greencolor}")
  7667. ledcolor="${greencolor}"
  7668. othercolor="${othercolor} ${redcolor}"
  7669. ;;
  7670. ablack|aBLACK|aBlack|aB|ab|anone|aNONE|aNone|aN|an|aNO|aNo|ano|aReset|aRESET|areset)
  7671. ledcolor=""
  7672. othercolor="${othercolor} ${redcolor} ${greencolor}"
  7673. ;;
  7674. aorange|aOrange|aORANGE|aO|ao|ayellow|aYellow|aYellow|aY|ay|aBOTH|aBoth|aboth)
  7675. ledcolor="${ledcolors}"
  7676. othercolor=""
  7677. ;;
  7678. aleft|aLEFT|aLeft|aL|al|"a${leftled}")
  7679. ledname="${leftled}"
  7680. otherled="${otherled}"
  7681. ;;
  7682. aright|aRIGHT|aRight|"a${rightled}")
  7683. ledname="${rightled}"
  7684. otherled="${otherled}"
  7685. ;;
  7686. ablink|aBlink|aBLINK)
  7687. ledblink=1;
  7688. ;;
  7689. anand|aNAND|assd|aSSD|adisk|aDISK|aDisk|aNand|aSsd|anand-disk|ananddisk|assddisk|assd-disk)
  7690. ledblink=2;
  7691. ;;
  7692. ammc|ammc0|aMMC|aMmc|aMMC0|acard|aCard|aCARD)
  7693. ledblink=3;
  7694. ;;
  7695. *)
  7696. ledcheck=`expr ${ledarg} + 1 - 1 2> /dev/null`
  7697. if [ "a${ledcheck}" = "a${ledarg}" ]; then
  7698. if [ "a${ledon}" = "a" ]; then
  7699. ledon="${ledarg}"
  7700. elif [ "a${ledoff}" = "a" ]; then
  7701. ledoff="${ledarg}"
  7702. fi
  7703. fi
  7704. ;;
  7705. esac
  7706. done
  7707. if [ "a${ledcolor}${othercolor}" = "a" -o "a${ledname}" = "a" ]; then
  7708. unset noexecute; unset ledarg; unset ledcolor; unset ledname; unset othercolor; unset otherled;
  7709. unset color; unset oled; unset ledcommand; unset ledblink; unset ledmode; unset ledon; unset ledoff;
  7710. return 1
  7711. fi
  7712. ledcommand=""
  7713. for oled in ${otherled}
  7714. do
  7715. for color in ${ledcolors}
  7716. do
  7717. ledcommand="${ledcommand} echo \"none\" > \"${ledpath}/${color}:${oled}/trigger\";"
  7718. ledcommand="${ledcommand} echo \"0\" > \"${ledpath}/${color}:${oled}/brightness\";"
  7719. done
  7720. done
  7721. if [ "a${ledblink}" = "a1" ]; then
  7722. unset othercolor
  7723. ledmode="timer"
  7724. [ "a${ledon}" = "a" ] && ledon=500
  7725. [ "a${ledoff}" = "a" ] && ledoff=500
  7726. if [ "a${ledcolor}" = "a" ]; then
  7727. ttt="${ledon}"; ledon="${ledoff}"; ledoff="${ttt}"; unset ttt
  7728. ledcheck=`guruplug_led_getstate "${ledname}" | ${grepbin} "_brightness=1" | ${sedbin} -e "s/^${ledname}_\(.*\)_brightness=1$/\1/g" | tr "\n" " "`
  7729. ledcheck=`echo ${ledcheck}`
  7730. for color in ${ledcolors}
  7731. do
  7732. colorname=`echo "${color}" | ${sedbin} -e "s/:/_/g"`
  7733. if strinstr "${colorname}" "${ledcheck}" " "; then
  7734. ledcolor="${ledcolor} ${color}"
  7735. else
  7736. othercolor="${othercolor} ${color}"
  7737. fi
  7738. done
  7739. unset color; unset colorname; unset ledcheck
  7740. fi
  7741. [ "a${ledcolor}" = "a" ] && unset othercolor
  7742. elif [ "a${ledblink}" = "a2" ]; then
  7743. ledmode="nand-disk"
  7744. elif [ "a${ledblink}" = "a3" ]; then
  7745. ledmode="mmc0"
  7746. else
  7747. ledmode="none"
  7748. fi
  7749. for color in ${othercolor}
  7750. do
  7751. ledcommand="${ledcommand} echo \"none\" > \"${ledpath}/${color}:${ledname}/trigger\";"
  7752. ledcommand="${ledcommand} echo \"0\" > \"${ledpath}/${color}:${ledname}/brightness\";"
  7753. done
  7754. if [ "a${ledcolor}" != "a" ]; then
  7755. for color in ${ledcolor}
  7756. do
  7757. ledcommand="${ledcommand} echo \"${ledmode}\" > \"${ledpath}/${color}:${ledname}/trigger\";"
  7758. [ "a${ledmode}" = "anone" ] && ledcommand="${ledcommand} echo \"1\" > \"${ledpath}/${color}:${ledname}/brightness\";"
  7759. if [ "a${ledblink}" = "a1" ]; then
  7760. ledcommand="${ledcommand} echo \"${ledon}\" > \"${ledpath}/${color}:${ledname}/delay_on\";"
  7761. ledcommand="${ledcommand} echo \"${ledoff}\" > \"${ledpath}/${color}:${ledname}/delay_off\";"
  7762. fi
  7763. done
  7764. unset color
  7765. fi
  7766. if [ "a${noexecute}" = "a1" ]; then
  7767. echo "${ledcommand}"
  7768. else
  7769. unset LEDSCLEAN
  7770. eval ${ledcommand}
  7771. fi
  7772. unset noexecute; unset ledarg; unset ledcolor; unset ledname; unset othercolor; unset otherled;
  7773. unset color; unset oled; unset ledcommand; unset ledblink; unset ledmode; unset ledon; unset ledoff;
  7774. return 0
  7775. }
  7776.  
  7777. guruplug_leds() {
  7778. ledscommands=""
  7779. for ledi in ${allleds}
  7780. do
  7781. ledscommands="${ledscommands} `guruplug_led noexecute "${ledi}" "$@"`"
  7782. done
  7783. unset LEDSCLEAN
  7784. eval ${ledscommands}
  7785. unset ledscommands; unset ledi
  7786. return 0
  7787. }
  7788.  
  7789. guruplug_verbose() {
  7790. [ "a${guruplug}" = "a" ] && return 0
  7791. if [ "a${gurucycle}" = "a" -o "a${gurucycle}" = "a0" ]; then
  7792. gurucycle="1"
  7793. led1="left"
  7794. led2="right"
  7795. elif [ "a${gurucycle}" = "a1" ]; then
  7796. gurucycle="0"
  7797. led1="right"
  7798. led2="left"
  7799. fi
  7800. export gurucycle
  7801. ledscommands=""
  7802. ledscommands="${ledscommands} `guruplug_led noexecute ${led1} reset`"
  7803. ledscommands="${ledscommands} `guruplug_led noexecute ${led2} reset`"
  7804. ledscommands="${ledscommands} `guruplug_led noexecute ${led1} green`"
  7805. ledscommands="${ledscommands} `guruplug_led noexecute ${led2} blink green`"
  7806. eval ${ledscommands}
  7807. unset led1; unset led2; unset ledscommands
  7808. return 0
  7809. }
  7810.  
  7811. guruplug_verbose_same() {
  7812. [ "a${guruplug}" = "a" ] && return 0
  7813. if [ "a${gurucycle}" = "a" -o "a${gurucycle}" = "a0" ]; then
  7814. led1="left"
  7815. led2="right"
  7816. elif [ "a${gurucycle}" = "a1" ]; then
  7817. led1="right"
  7818. led2="left"
  7819. fi
  7820. export gurucycle
  7821. ledscommands=""
  7822. ledscommands="${ledscommands} `guruplug_led noexecute ${led1} reset`"
  7823. ledscommands="${ledscommands} `guruplug_led noexecute ${led2} reset`"
  7824. ledscommands="${ledscommands} `guruplug_led noexecute ${led1} green`"
  7825. ledscommands="${ledscommands} `guruplug_led noexecute ${led2} green`"
  7826. ledscommands="${ledscommands} `guruplug_led noexecute ${led2} blink red`"
  7827. eval ${ledscommands}
  7828. unset led1; unset led2; unset ledscommands
  7829. return 0
  7830. }
  7831.  
  7832. guruplug_notify() {
  7833. [ "a${guruplug}" = "a" ] && return 0
  7834. guruplug_leds black
  7835. guruplug_leds blink green
  7836. [ "a${interactive}" = "a" ] && sleep 5
  7837. }
  7838.  
  7839. guruplug_error() {
  7840. [ "a${guruplug}" = "a" ] && return 0
  7841. guruplug_leds black
  7842. guruplug_leds blink red
  7843. [ "a${interactive}" = "a" ] && sleep 5
  7844. }
  7845.  
  7846. guruplug_led_exists() {
  7847. [ "a${guruplug}" = "a" ] && return 1
  7848. [ "a$1" = "a" ] && return 1
  7849. for color in ${ledcolors}
  7850. do
  7851. [ ! -d "${ledpath}/${color}:$1" ] && return 1
  7852. done
  7853. unset color
  7854. return 0
  7855. }
  7856.  
  7857. guruplug_led_getstate() {
  7858. [ "a${guruplug}" = "a" ] && return 0
  7859. ! guruplug_led_exists "$@" && return 1
  7860. for color in ${ledcolors}
  7861. do
  7862. colorname=`echo ${color} | ${sedbin} -e "s/:/_/g"`
  7863. for file in ${ledvariables}
  7864. do
  7865. if [ -r "${ledpath}/${color}:${1}/${file}" ]; then
  7866. value=`${catbin} "${ledpath}/${color}:$1/${file}"`
  7867. eval "$1_${colorname}_${file}=\"${value}\""
  7868. eval "export $1_${colorname}_${file}"
  7869. else
  7870. eval "unset $1_${colorname}_${file}"
  7871. fi
  7872. done
  7873. value=`${catbin} "${ledpath}/${color}:$1/trigger" | ${sedbin} -e "s/^\(.*\)\[\(.*\)\]\(.*\)$/\2/g"`
  7874. eval "$1_${colorname}_trigger=\"${value}\""
  7875. eval "export $1_${colorname}_trigger"
  7876. [ "a${value}" != "anone" ] && eval "unset $1_${colorname}_brightness"
  7877. done
  7878. unset colorname; unset color; unset value; unset file;
  7879. debug "Led $1 saved.\n"
  7880. set | ${grepbin} "^$1_"
  7881. return 0
  7882. }
  7883.  
  7884. guruplug_state() {
  7885. [ "a${guruplug}" = "a" ] && return 0
  7886. for ledi in ${allleds}
  7887. do
  7888. ! guruplug_led_getstate ${ledi} && return 1
  7889. done
  7890. unset ledi
  7891. debug "All leds are saved.\n"
  7892. return 0
  7893. }
  7894.  
  7895. guruplug_restore() {
  7896. [ "a${guruplug}" = "a" ] && return 0
  7897. command=""
  7898. for ledi in ${allleds}
  7899. do
  7900. for color in ${ledcolors}
  7901. do
  7902. colorname=`echo ${color} | ${sedbin} -e "s/:/_/g"`
  7903. for file in trigger ${ledvariables}
  7904. do
  7905. value=`echo "$1" | ${grepbin} "^${ledi}_${colorname}_${file}=" | ${cutbin} -d= -f2- -s`
  7906. if [ "a${value}" != "a" ]; then
  7907. debug "echo \"${value}\" > \"${ledpath}/${color}:${ledi}/${file}\"\n"
  7908. command="${command} echo \"${value}\" > \"${ledpath}/${color}:${ledi}/${file}\";"
  7909. fi
  7910. done
  7911. done
  7912. done
  7913. unset LEDSCLEAN
  7914. eval ${command}
  7915. unset command
  7916. unset value; unset file; unset colorname; unset color; unset ledi;
  7917. return 0
  7918. }
  7919.  
  7920. guruplug_restore_state() {
  7921. [ "a${guruplug}" = "a" ] && return 0
  7922. [ "a${guruplug_original_state}" != "a" ] && guruplug_restore "${guruplug_original_state}" && debug "Original state of GuruPlug leds is now restored.\n" && return 0
  7923. debug "Failed to restore GuruPlug leds into original state.\n"
  7924. return 1
  7925. }
  7926.  
  7927. guruplug_save_state() {
  7928. [ "a${guruplug}" = "a" ] && return 0
  7929. ! guruplug_state > /dev/null && return 1
  7930. guruplug_original_state=`guruplug_state`
  7931. export guruplug_original_state
  7932. addexittrap guruplug_restore_state
  7933. debug "Original state of GuruPlug leds is saved.\n"
  7934. return 0
  7935. }
  7936.  
  7937. guruplug_probe() {
  7938. [ "a${guruplug}" = "a" ] && return 1
  7939. [ ! -w "/sys/class/leds/guruplug:green:health/trigger" ] && unset guruplug
  7940. [ ! -w "/sys/class/leds/guruplug:green:health/brightness" ] && unset guruplug
  7941. [ ! -w "/sys/class/leds/guruplug:red:health/trigger" ] && unset guruplug
  7942. [ ! -w "/sys/class/leds/guruplug:red:health/brightness" ] && unset guruplug
  7943. [ ! -w "/sys/class/leds/guruplug:green:wmode/trigger" ] && unset guruplug
  7944. [ ! -w "/sys/class/leds/guruplug:green:wmode/brightness" ] && unset guruplug
  7945. [ ! -w "/sys/class/leds/guruplug:red:wmode/trigger" ] && unset guruplug
  7946. [ ! -w "/sys/class/leds/guruplug:red:wmode/brightness" ] && unset guruplug
  7947. if [ "a${guruplug}" = "a" ]; then
  7948. debug "GuruPlug leds not found, or not accessible.\n"
  7949. return 1
  7950. else
  7951. ##############################################
  7952. export ledpath="/sys/class/leds"
  7953. ##############################################
  7954. export redcolor="guruplug:red"
  7955. export greencolor="guruplug:green"
  7956. export ledcolors="${redcolor} ${greencolor}"
  7957. ##############################################
  7958. export leftled="health"
  7959. export rightled="wmode"
  7960. export allleds="${leftled} ${rightled}"
  7961. ##############################################
  7962. export ledvariables="brightness delay_off delay_on"
  7963. ##############################################
  7964. debug "GuruPlug leds found.\n"
  7965. fi
  7966. if ! guruplug_save_state; then
  7967. unset guruplug
  7968. debug "Failed to retrieve state of leds.\n"
  7969. return 1
  7970. fi
  7971. }
  7972.  
  7973. config_args_read() {
  7974. for arg in "$@"
  7975. do
  7976. debug "Parsing configuration value: %s.\n" "${arg}"
  7977. parse_eval "readconfig" "${arg}" && continue
  7978. parse_switches "${arg}"
  7979. done
  7980. unset arg
  7981. }
  7982.  
  7983. config_load() {
  7984. # If no PIN supplied from command line and $HOME/.3gnet exists, use PIN from there.
  7985. [ "a${SIM_PIN}" = "a" -a "a${runhome}" != "a" ] && [ -d "${runhome}" ] && [ -r "${runhome}/.3gpin" ] && SIM_PIN=`${headbin} -1 "${runhome}/.3gpin" 2> /dev/null | ${cutbin} -b1-4` && [ "a${SIM_PIN}" != "a" ] && export SIM_PIN && debug "Loaded PIN value from %s.\n" "${runhome}/.3gpin" && ! pin_valid "${SIM_PIN}" && unset SIM_PIN && debug "PIN supplied was not valid.\n"
  7986. # Load PIN from system-wide PIN suppliers
  7987. for pinsupplier in "/etc/default/3gpin" "/etc/sysconfig/3gpin" "/etc/3gpin" "/etc/default/sakis3g.3gpin" "/etc/sysconfig/sakis3g.3gpin" "/etc/sakis3g.3gpin"
  7988. do
  7989. [ -r "${pinsupplier}" ] && unset SIM_PIN && SIM_PIN=`${headbin} -1 "${pinsupplier}" 2> /dev/null | ${cutbin} -b1-4` && [ "a${SIM_PIN}" != "a" ] && export SIM_PIN && debug "Loaded PIN value from %s.\n" "${pinsupplier}" &&! pin_valid "${SIM_PIN}" && unset SIM_PIN && debug "PIN supplied was not valid.\n"
  7990. done
  7991. unset pinsupplier
  7992. # If system-wide configuration exists, load it overriding anything (user configuration/arguments/everything)
  7993. for configfile in "/etc/default/sakis3g" "/etc/sysconfig/sakis3g" "/etc/sakis3g.conf"
  7994. do
  7995. if [ -r "${configfile}" ]; then
  7996. debug "Loading system-wide configuration file %s.\n" "${configfile}"
  7997. debug show_file "${configfile}"
  7998. configargs=`${grepbin} -v "^#" "${configfile}" | ${grepbin} -v "^$" | ${sedbin} -e "s/^\(.*\)$/\"\1\"/g" | ${sedbin} -e "s/^\"\([A-Za-z_]*\)=\"\(.*\)\"\"$/\"\1=\2\"/g" | ${trbin} "\n" " "`
  7999. debug "Configuration arguments are: %s\n" "${configargs}"
  8000. eval config_args_read ${configargs}
  8001. unset configargs
  8002. debug "Finished loading file %s.\n" "${configfile}"
  8003. else
  8004. debug "Configuration file %s does not exist or is not readable.\n" "${configfile}"
  8005. fi
  8006. done
  8007. unset configfile
  8008. debug "Configuration file(s) loaded.\n"
  8009. }
  8010.  
  8011. # Parses command line switch arguments
  8012. parse_switch() {
  8013. if [ "a$1" = "a$2" -o "a$1" = "a--$2" -o "a$1" = "a-$2" ]; then
  8014. debug "Command line switch defined: %s\n" "$1"
  8015. return 0
  8016. fi
  8017. return 1
  8018. }
  8019.  
  8020. # Parses command line variable settings
  8021. parse_eval() {
  8022. ! find_binary "cut" && return 1
  8023. ! find_binary "grep" && return 1
  8024. if [ "a$1" = "areadconfig" ]; then
  8025. readconfig=1
  8026. shift
  8027. [ "a$1" = "a" ] && unset readconfig && return 1
  8028. else
  8029. readconfig=0
  8030. fi
  8031. [ "a`echo "$1" | ${grepbin} "="`" = "a" ] && unset readconfig && return 1
  8032. variable=`echo "${1}" | ${cutbin} -d= -f1`
  8033. [ "a${variable}" = "a" ] && unset variable && unset readconfig && return 1
  8034. value=`echo "${1}" | ${cutbin} -d= -f2-`
  8035. [ "a${value}" = "a" ] && unset variable && unset value && unset readconfig && return 1
  8036. case "${variable}" in
  8037. pppint|PPPINT)
  8038. export pppint="${value}"
  8039. ;;
  8040. CHAT_ABORT_STRINGS)
  8041. # Abort strings that chat program may encounter
  8042. export CHAT_ABORT_STRINGS="${value}"
  8043. ;;
  8044. BAUD)
  8045. # Baud rate
  8046. if [ "a${value}" = "aMAX" -o "a${value}" = "amax" ]; then
  8047. export BAUD="4000000"
  8048. elif [ "a${value}" = "aMIN" -o "a${value}" = "amin" ]; then
  8049. export BAUD="115200"
  8050. elif [ "a${value}" = "aDEFAULT" -o "a${value}" = "adefault" -o "a${value}" = "aNORMAL" -o "a${value}" = "anormal" ]; then
  8051. export BAUD="115200"
  8052. else
  8053. export BAUD="${value}"
  8054. fi
  8055. ;;
  8056. PPPD_OPTIONS)
  8057. # Options passed to pppd when called directly
  8058. export PPPD_OPTIONS="${value}"
  8059. ;;
  8060. PPPD_PEERS)
  8061. # Directory where pppd keeps its peers
  8062. export PPPD_PEERS="${value}"
  8063. ;;
  8064. XOSDFONT)
  8065. # Font that will be used when displaying messages through XOSD (osd_cat)
  8066. export XOSDFONT="${value}"
  8067. ;;
  8068. AOSDFONT)
  8069. # Font that will be used when displaying messages through XOSD (osd_cat)
  8070. export AOSDFONT="${value}"
  8071. ;;
  8072. MENUFONT|menufont)
  8073. # Font that will be used when using 9menu
  8074. export MENUFONT="${value}"
  8075. ;;
  8076. OSDFONT)
  8077. # Font that will be used when displaying messages through XOSD (osd_cat)
  8078. export AOSDFONT="${value}"
  8079. export XOSDFONT="${value}"
  8080. ;;
  8081. LOGPOSITION|logposition)
  8082. # Directory where pppd keeps its peers
  8083. export LOGPOSITION="${value}"
  8084. ;;
  8085. DESKTOPICON)
  8086. # Directory where pppd keeps its peers
  8087. export DESKTOPICON="${value}"
  8088. ;;
  8089. DCOPSERVER)
  8090. # Directory where pppd keeps its peers
  8091. export DCOPSERVER="${value}"
  8092. ;;
  8093. connection_hook|CONNECTION_HOOK|HOOK)
  8094. # Enabling this is a security issue.
  8095. # Users can pass argument to do whatever they want.
  8096. if [ "a${readconfig}" = "a1" ]; then
  8097. export CONNECTION_HOOK="${value}"
  8098. else
  8099. echo "Ignoring ${variable} variable for security reasons." > /dev/stderr
  8100. echo "This variable is only usable through global configuration." > /dev/stderr
  8101. echo "Consult documentation." > /dev/stderr
  8102. fi
  8103. ;;
  8104. DNS)
  8105. export DNS="${value}"
  8106. ;;
  8107. MODEM_PREPARE|PREPARE)
  8108. export MODEM_PREPARE="${value}"
  8109. INIT_STAGE0=`${printfbin} "%s\n" "${value}" | ${sedbin} -e "s/AT/\\nAT/g" | ${sedbin} -e "s/ *$//g" | ${grepbin} -v "^$" | ${grepbin} "^AT" | ${sedbin} -e "s/^\(.*\)$/\\\'\1\\\' OK/g" | ${trbin} "\n" " "`
  8110. export INIT_STAGE0
  8111. ;;
  8112. MODEM_INIT|INIT)
  8113. export MODEM_INIT="${value}"
  8114. ;;
  8115. DIAL|CUSTOM_DIAL)
  8116. export CUSTOM_DIAL="${value}"
  8117. ;;
  8118. FORCE_APN)
  8119. export FORCE_APN="${value}"
  8120. ;;
  8121. CUSTOM_APN)
  8122. export CUSTOM_APN="${value}"
  8123. ;;
  8124. APN)
  8125. export APN="${value}"
  8126. ;;
  8127. APN_USER)
  8128. export APN_USER="${value}"
  8129. ;;
  8130. APN_PASS)
  8131. export APN_PASS="${value}"
  8132. ;;
  8133. FORCE_ISP)
  8134. export FORCE_ISP="${value}"
  8135. ;;
  8136. SIM_PIN)
  8137. export SIM_PIN="${value}"
  8138. ;;
  8139. RFCOMM_TTY)
  8140. export RFCOMM_TTY="${value}"
  8141. ;;
  8142. CUSTOM_TTY)
  8143. export CUSTOM_TTY="${value}"
  8144. ;;
  8145. RFSERVICE)
  8146. export RFSERVICE="${value}"
  8147. ;;
  8148. RFCHANNEL)
  8149. export RFCHANNEL="${value}"
  8150. ;;
  8151. UNDISCOVERABLE)
  8152. export UNDISCOVERABLE="${value}"
  8153. ;;
  8154. BLUETOOTH)
  8155. export BLUETOOTH="${value}"
  8156. ;;
  8157. USBINTERFACE)
  8158. export USBINTERFACE="${value}"
  8159. ;;
  8160. USBDRIVER)
  8161. export USBDRIVER="${value}"
  8162. ;;
  8163. USBMODEM)
  8164. export USBMODEM="${value}"
  8165. ;;
  8166. OTHER)
  8167. export OTHER="${value}"
  8168. ;;
  8169. MODEM)
  8170. export MODEM="${value}"
  8171. ;;
  8172. MOREMENU)
  8173. export MOREMENU="${value}"
  8174. ;;
  8175. MENU)
  8176. export MENU="${value}"
  8177. ;;
  8178. DISPLAY)
  8179. export DISPLAY="${value}"
  8180. ;;
  8181. XAUTHORITY|LOCALAUTHORITY)
  8182. export LOCALAUTHORITY="${value}"
  8183. ;;
  8184. BALOONIZER|BALLOONIZER)
  8185. export BALOONIZER="${value}"
  8186. ;;
  8187. DESKTOP)
  8188. export DESKTOP="${value}"
  8189. ;;
  8190. SGUI)
  8191. export SGUI="${value}"
  8192. ;;
  8193. TRANSLATION)
  8194. export TRANSLATION="${value}"
  8195. ;;
  8196. *)
  8197. debug "Unknown variable %s specified in command line.\n" "${variable}"
  8198. unset variable
  8199. ;;
  8200. esac
  8201. unset readconfig
  8202. if [ "a${variable}" != "a" ]; then
  8203. debug "Command line set variable %s to \"%s\".\n" "${variable}" "`eval echo \\\${${variable}}`"
  8204. unset variable; unset value;
  8205. return 0
  8206. fi
  8207. unset variable; unset value
  8208. return 1
  8209. }
  8210.  
  8211. parse_switches() {
  8212. parse_switch "${1}" "balloons" && export balloons=yes
  8213. parse_switch "${1}" "balloon" && export balloons=yes
  8214. parse_switch "${1}" "baloons" && export balloons=yes
  8215. parse_switch "${1}" "baloon" && export balloons=yes
  8216. parse_switch "${1}" "ballons" && export balloons=yes
  8217. parse_switch "${1}" "ballon" && export balloons=yes
  8218. parse_switch "${1}" "balons" && export balloons=yes
  8219. parse_switch "${1}" "balon" && export balloons=yes
  8220. parse_switch "${1}" "b" && export balloons=yes
  8221.  
  8222. parse_switch "${1}" "console" && export stick_to_console=yes
  8223. parse_switch "${1}" "stick_to_console" && export stick_to_console=yes
  8224. parse_switch "${1}" "c" && export stick_to_console=yes
  8225.  
  8226. parse_switch "${1}" "debug" && export DEBUG=on
  8227. parse_switch "${1}" "DEBUG" && export DEBUG=on
  8228. parse_switch "${1}" "d" && export DEBUG=on
  8229.  
  8230. parse_switch "${1}" "NOSMART" && export NOSMART=yes
  8231. parse_switch "${1}" "NOFIX" && export NOSMART=yes
  8232. parse_switch "${1}" "nosmart" && export NOSMART=yes
  8233. parse_switch "${1}" "nofix" && export NOSMART=yes
  8234. parse_switch "${1}" "smart" && export NOSMART=yes
  8235. parse_switch "${1}" "fix" && export NOSMART=yes
  8236. parse_switch "${1}" "f" && export NOSMART=yes
  8237.  
  8238. parse_switch "${1}" "GOOGLEDNS" && export DNS="8.8.8.8 8.8.4.4"
  8239. parse_switch "${1}" "GOOGLE" && export DNS="8.8.8.8 8.8.4.4"
  8240. parse_switch "${1}" "googledns" && export DNS="8.8.8.8 8.8.4.4"
  8241. parse_switch "${1}" "google" && export DNS="8.8.8.8 8.8.4.4"
  8242. parse_switch "${1}" "g" && export DNS="8.8.8.8 8.8.4.4"
  8243.  
  8244. parse_switch "${1}" "GURUPLUG" && export guruplug=yes
  8245. parse_switch "${1}" "guruplug" && export guruplug=yes
  8246. parse_switch "${1}" "GURU" && export guruplug=yes
  8247. parse_switch "${1}" "guru" && export guruplug=yes
  8248. parse_switch "${1}" "G" && export guruplug=yes
  8249.  
  8250. parse_switch "${1}" "nohal" && export nohal=yes
  8251. parse_switch "${1}" "hal" && export nohal=yes
  8252. parse_switch "${1}" "h" && export nohal=yes
  8253.  
  8254. parse_switch "${1}" "interactive" && export interactive=yes
  8255. parse_switch "${1}" "i" && export interactive=yes
  8256.  
  8257. parse_switch "${1}" "killstorage" && export killstorage=yes
  8258. parse_switch "${1}" "nostorage" && export killstorage=yes
  8259. parse_switch "${1}" "storage" && export killstorage=yes
  8260. parse_switch "${1}" "k" && export killstorage=yes
  8261.  
  8262. parse_switch "${1}" "nohalinform" && export nohalinform=yes
  8263. parse_switch "${1}" "noinform" && export nohalinform=yes
  8264. parse_switch "${1}" "l" && export nohalinform=yes
  8265.  
  8266. parse_switch "${1}" "NOPROBE" && export NOPROBEGSM=yes
  8267. parse_switch "${1}" "NOPROBEGSM" && export NOPROBEGSM=yes
  8268. parse_switch "${1}" "noprobe" && export NOPROBEGSM=yes
  8269. parse_switch "${1}" "noprobegsm" && export NOPROBEGSM=yes
  8270. parse_switch "${1}" "GSM" && export NOPROBEGSM=yes
  8271. parse_switch "${1}" "gsm" && export NOPROBEGSM=yes
  8272. parse_switch "${1}" "n" && export NOPROBEGSM=yes
  8273.  
  8274. parse_switch "${1}" "prefer_osd" && export prefer_osd=yes
  8275. parse_switch "${1}" "preferosd" && export prefer_osd=yes
  8276. parse_switch "${1}" "osd" && export prefer_osd=yes
  8277. parse_switch "${1}" "OSD" && export prefer_osd=yes
  8278. parse_switch "${1}" "o" && export prefer_osd=yes
  8279.  
  8280. parse_switch "${1}" "forever" && export forever=yes
  8281. parse_switch "${1}" "persist" && export forever=yes
  8282. parse_switch "${1}" "P" && export forever=yes
  8283.  
  8284. parse_switch "${1}" "directpppd" && export direct_pppd=yes
  8285. parse_switch "${1}" "direct_pppd" && export direct_pppd=yes
  8286. parse_switch "${1}" "ppp" && export direct_pppd=yes
  8287. parse_switch "${1}" "pppd" && export direct_pppd=yes
  8288. parse_switch "${1}" "p" && export direct_pppd=yes
  8289.  
  8290. parse_switch "${1}" "noverbose" && export noverbose=yes
  8291. parse_switch "${1}" "noinformation" && export nonotify=yes
  8292. parse_switch "${1}" "nonotifications" && export nonotify=yes
  8293. parse_switch "${1}" "nonotify" && export nonotify=yes
  8294. parse_switch "${1}" "noerrors" && export noerror=yes
  8295. parse_switch "${1}" "noerror" && export noerror=yes
  8296. if parse_switch "${1}" "silent" || parse_switch "${1}" "quiet" || parse_switch "${1}" "q"; then
  8297. export noerror=yes
  8298. export nonotify=yes
  8299. export noverbose=yes
  8300. fi
  8301.  
  8302. parse_switch "${1}" "sudo" && export alwayssudo=yes
  8303. parse_switch "${1}" "alwayssudo" && export alwayssudo=yes
  8304. parse_switch "${1}" "always_sudo" && export alwayssudo=yes
  8305. parse_switch "${1}" "s" && export alwayssudo=yes
  8306.  
  8307. parse_switch "${1}" "showtext" && export showtext=yes
  8308. parse_switch "${1}" "T" && export showtext=yes
  8309.  
  8310. parse_switch "${1}" "notranslate" && export notranslate=yes
  8311. parse_switch "${1}" "t" && export notranslate=yes
  8312.  
  8313. parse_switch "${1}" "nosafety" && export nosafety=yes
  8314. parse_switch "${1}" "unsafe" && export nosafety=yes
  8315. parse_switch "${1}" "u" && export nosafety=yes
  8316.  
  8317. parse_switch "${1}" "voodoo" && export voodoo=yes
  8318. parse_switch "${1}" "please" && export voodoo=yes
  8319. parse_switch "${1}" "magic" && export voodoo=yes
  8320. parse_switch "${1}" "Voodoo" && export voodoo=yes
  8321. parse_switch "${1}" "Please" && export voodoo=yes
  8322. parse_switch "${1}" "Magic" && export voodoo=yes
  8323. parse_switch "${1}" "v" && export voodoo=yes
  8324.  
  8325. parse_switch "${1}" "whiptail" && export SGUI="whiptail"
  8326. parse_switch "${1}" "dialog" && export SGUI="dialog"
  8327. parse_switch "${1}" "Xdialog" && export SGUI="Xdialog"
  8328. parse_switch "${1}" "xdialog" && export SGUI="Xdialog"
  8329. parse_switch "${1}" "zenity" && export SGUI="zenity"
  8330. parse_switch "${1}" "kdialog" && export SGUI="kdialog"
  8331. parse_switch "${1}" "9menu" && export SGUI="9menu"
  8332. parse_switch "${1}" "xterm" && export SGUI="xterm"
  8333. parse_switch "${1}" "gnome-terminal" && export SGUI="gnome-terminal"
  8334. parse_switch "${1}" "konsole" && export SGUI="konsole"
  8335. parse_switch "${1}" "interactiveterminal" && export SGUI="interactive terminal"
  8336. parse_switch "${1}" "iterm" && export SGUI="interactive terminal"
  8337. parse_switch "${1}" "terminal" && export SGUI="terminal"
  8338. parse_switch "${1}" "term" && export SGUI="terminal"
  8339. if parse_switch "${1}" "legacy"; then
  8340. export SGUI="9menu"
  8341. export prefer_osd="yes"
  8342. fi
  8343.  
  8344. parse_switch "${1}" "scanyes" && export scanyes="1"
  8345. parse_switch "${1}" "scanno" && export scanno="1"
  8346. }
  8347.  
  8348. state_variables() {
  8349. unset statevariables
  8350. for flagvariable in stick_to_console:console interactive:interactive DEBUG:debug NOSMART:nofix killstorage:nostorage NOPROBEGSM:noprobegsm prefer_osd:osd direct_pppd:pppd alwayssudo:sudo forever:forever balloons:balloons
  8351. do
  8352. variablename=`echo "${flagvariable}" | cut -d: -f1`
  8353. strinstr "${variablename}" "${1}" " " && debug "Variable %s excluded from state variables.\n" "${variablename}" && continue
  8354. variablearg=`echo "${flagvariable}" | cut -d: -f2`
  8355. variablevalue=`eval echo \\\${${variablename}}`
  8356. [ "a${variablevalue}" != "a" ] && statevariables="${statevariables} --${variablearg}"
  8357. done
  8358. unset flagvariable; unset variablename; unset variablearg; unset variablevalue
  8359. [ "a${DNS}" = "a8.8.8.8 8.8.4.4" ] && statevariables="${statevariables} --googledns"
  8360. [ "a${DNS}" != "a8.8.8.8 8.8.4.4" -a "a${DNS}" != "a" ] && statevariables="${statevariables} DNS=\"${DNS}\""
  8361. for valuevariable in MODEM_INIT FORCE_APN APN APN_USER APN_PASS FORCE_ISP SIM_PIN SGUI RFCOMM_TTY CUSTOM_TTY RFSERVICE RFCHANNEL UNDISCOVERABLE BLUETOOTH USBINTERFACE USBDRIVER USBMODEM OTHER MODEM MENU MOREMENU DISPLAY DESKTOP DCOPSERVER LOCALAUTHORITY BALOONIZER
  8362. do
  8363. strinstr "${valuevariable}" "${1}" " " && debug "Variable %s excluded from state variables.\n" "${valuevariable}" && continue
  8364. variablevalue=`eval echo \\\${${valuevariable}}`
  8365. [ "a${variablevalue}" != "a" ] && statevariables="${statevariables} ${valuevariable}=\"${variablevalue}\""
  8366. done
  8367. unset valuevariable; unset variablevalue
  8368. statevariables=`echo ${statevariables}`
  8369. export statevariables
  8370. debug "State variables are: %s\n" "${statevariables}"
  8371. return 0
  8372. }
  8373.  
  8374. # Parses command line arguments
  8375. parse_arguments() {
  8376. need_binary "tr"
  8377. unset allargs
  8378. for arg in "$@"
  8379. do
  8380. varg=`sanitize "${arg}"`
  8381. allargs="${allargs} \"${varg}\""
  8382. parse_eval "${varg}" && continue
  8383. parse_switches "${varg}"
  8384. unset varg
  8385. done
  8386. unset arg
  8387. allargs=`echo ${allargs}`
  8388. export allargs
  8389. }
  8390.  
  8391. # Unsets binary variables if running as root. This eliminates the chance
  8392. # that a malicious user has succeed into setting a "trojan" binary helper
  8393. # that will be executed with root privileges.
  8394. secure_path() {
  8395. PATH="/bin:/sbin:/usr/bin:/usr/sbin"
  8396. export PATH
  8397. unset whichbin
  8398. unset grepbin
  8399. unset cutbin
  8400. need_binary "grep"
  8401. need_binary "cut"
  8402. for variable in `set | ${grepbin} "bin=" | ${cutbin} -d= -f1`
  8403. do
  8404. value=`eval echo \\${${variable}} 2> /dev/null`
  8405. if [ "a${value}" != "a" ] && [ -x "${value}" ]; then
  8406. eval "unset ${variable}"
  8407. debug "Unset variable %s for security reasons.\n" "${variable}"
  8408. fi
  8409. done
  8410. unset variable; unset value
  8411. return 0
  8412. }
  8413.  
  8414. # Removes \, ", ', ` from variables' contents
  8415. secure_variables() {
  8416. need_binary "sed"
  8417. need_binary "tr"
  8418. for variable in DISPLAY XAUTHORITY USERNAME HOME USER DESKTOP
  8419. do
  8420. debug "Sanitizing %s: %s\n" "${variable}" "`eval echo \"\\${${variable}}\"`"
  8421. value=`eval "echo \"\\${${variable}}\"" 2> /dev/null | ${trbin} "\\\\" " " 2> /dev/null | ${trbin} "\"" " " | ${trbin} "'" " " | ${trbin} "\\\`" " "`
  8422. debug "Sanitized value: %s\n" "${value}"
  8423. eval "${variable}=\"""${value}""\""
  8424. eval "${variable}=\"`eval echo \"\\${${variable}}\"`\""
  8425. eval "export ${variable}"
  8426. debug "Sanitized %s: %s\n" "${variable}" "`eval echo \"\\${${variable}}\"`"
  8427. done
  8428. unset value; unset variable
  8429. return 0
  8430. }
  8431.  
  8432. secure_capabilities() {
  8433. debug "Will check shell capabilities.\n"
  8434. set +o monitor > /dev/null 2> /dev/null
  8435. set +m > /dev/null 2> /dev/null
  8436. funcname=`set | grep "^FUNCNAME="`
  8437. if [ "a${funcname}" = "a" ]; then
  8438. debug "Shell does not provide FUNCNAME environment variable.\n"
  8439. debug "USB Safety will be disabled.\n"
  8440. export NOFUNCNAME="1"
  8441. unset YESFUNCNAME=""
  8442. else
  8443. debug "Shell provides FUNCNAME environment variable.\n"
  8444. unset NOFUNCNAME
  8445. export YESFUNCNAME="1"
  8446. fi
  8447. unset funcname
  8448. return 0
  8449. }
  8450.  
  8451. # Makes sure no danger exists when run as root
  8452. secure_environment() {
  8453. secure_capabilities
  8454. ! we_are_root_already && return 0
  8455. secure_path
  8456. secure_variables
  8457. return 0
  8458. }
  8459.  
  8460. # Preparation or death
  8461. startup() {
  8462. # try to set shell as helpful as possible with background jobs
  8463. secure_environment
  8464. addexittrap cleanscreen
  8465. me=$1; [ "$#" -gt "0" ] && shift
  8466. # Draftly set allargs. Will better do it during parse_arguments
  8467. allargs="$@"; export allargs
  8468. no_action_command "$@" && stop_with 0
  8469. verbose "Starting up"
  8470. parse_arguments "$@"
  8471. debug_header "$@"
  8472. resolv_binaries "${me}"; unset me
  8473. check_udevd "$@"
  8474. find_user
  8475. config_load
  8476. find_display
  8477. find_gui
  8478. translate_load
  8479. check_root_deps
  8480. modeswitch_load
  8481. guruplug_probe
  8482. debug "Finished starting up.\n"
  8483. }
  8484.  
  8485. # Main program
  8486. sakis3g_main() {
  8487. # Initialization
  8488. startup "$@"
  8489. # Get action
  8490. action_command "$@" && stop_with ${actionresult}
  8491. default_actor
  8492. ret=$?
  8493. stop_with ${ret}
  8494. }
  8495.  
  8496. # Execute main method
  8497. sakis3g_main $0 "$@"
  8498.  
  8499. # In case all actors performed well
  8500. debug "Reached end of program.\n"
  8501. stop_with 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement