mirror of
https://github.com/gryf/wicd.git
synced 2026-01-09 07:14:13 +01:00
experimental branch:
- Port a bunch of fixes from the trunk - Use an actual Gtk.Menu in the toolbar for the "Network" widget
This commit is contained in:
@@ -35,6 +35,8 @@ import wicd.misc as misc
|
||||
import wicd.wnettools as wnettools
|
||||
|
||||
import re
|
||||
import os
|
||||
import os.path
|
||||
import time
|
||||
|
||||
|
||||
@@ -51,7 +53,7 @@ more stable for some set ups.
|
||||
|
||||
# Compile the regex patterns that will be used to search the output of iwlist
|
||||
# scan for info these are well tested, should work on most cards
|
||||
essid_pattern = re.compile('.*ESSID:"(.*?)"\n', re.I | re.M | re.S)
|
||||
essid_pattern = re.compile('.*ESSID:(.*?)\n', re.I | re.M | re.S)
|
||||
ap_mac_pattern = re.compile('.*Address: (.*?)\n', re.I | re.M | re.S)
|
||||
channel_pattern = re.compile('.*Channel:? ?(\d\d?)', re.I | re.M | re.S)
|
||||
strength_pattern = re.compile('.*Quality:?=? ?(\d+)\s*/?\s*(\d*)', re.I | re.M | re.S)
|
||||
@@ -131,11 +133,23 @@ class Interface(wnettools.BaseInterface):
|
||||
|
||||
def IsUp(self, ifconfig=None):
|
||||
""" Determines if the interface is up.
|
||||
|
||||
|
||||
Returns:
|
||||
True if the interface is up, False otherwise.
|
||||
|
||||
|
||||
"""
|
||||
if not self.iface: return False
|
||||
flags_file = '/sys/class/net/%s/flags' % self.iface
|
||||
try:
|
||||
flags = open(flags_file, "r").read().strip()
|
||||
except IOError:
|
||||
print "Could not open %s, using ifconfig to determine status" % flags_file
|
||||
return self._slow_is_up(ifconfig)
|
||||
return bool(int(flags, 16) & 1)
|
||||
|
||||
|
||||
def _slow_is_up(self, ifconfig=None):
|
||||
""" Determine if an interface is up using ifconfig. """
|
||||
if not ifconfig:
|
||||
cmd = "ifconfig " + self.iface
|
||||
if self.verbose: print cmd
|
||||
@@ -145,11 +159,9 @@ class Interface(wnettools.BaseInterface):
|
||||
lines = output.split('\n')
|
||||
if len(lines) < 5:
|
||||
return False
|
||||
|
||||
for line in lines[1:4]:
|
||||
if line.strip().startswith('UP'):
|
||||
return True
|
||||
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
@@ -168,24 +180,49 @@ class WiredInterface(Interface, wnettools.BaseWiredInterface):
|
||||
|
||||
def GetPluggedIn(self):
|
||||
""" Get the current physical connection state.
|
||||
|
||||
|
||||
The method will first attempt to use ethtool do determine
|
||||
physical connection state. Should ethtool fail to run properly,
|
||||
mii-tool will be used instead.
|
||||
|
||||
|
||||
Returns:
|
||||
True if a link is detected, False otherwise.
|
||||
|
||||
|
||||
"""
|
||||
if not self.iface:
|
||||
return False
|
||||
# check for link using /sys/class/net/iface/carrier
|
||||
# is usually more accurate
|
||||
sys_device = '/sys/class/net/%s/' % self.iface
|
||||
carrier_path = sys_device + 'carrier'
|
||||
if not self.IsUp():
|
||||
MAX_TRIES = 3
|
||||
tries = 0
|
||||
self.Up()
|
||||
while True:
|
||||
tries += 1
|
||||
time.sleep(2)
|
||||
if self.IsUp() or tries > MAX_TRIES: break
|
||||
|
||||
if os.path.exists(carrier_path):
|
||||
carrier = open(carrier_path, 'r')
|
||||
try:
|
||||
link = carrier.read().strip()
|
||||
link = int(link)
|
||||
if link == 1:
|
||||
return True
|
||||
elif link == 0:
|
||||
return False
|
||||
except (IOError, ValueError, TypeError):
|
||||
print 'Error checking link using /sys/class/net/%s/carrier' % self.iface
|
||||
|
||||
if self.ETHTOOL_FOUND and self.link_detect != misc.MIITOOL:
|
||||
return self._eth_get_plugged_in()
|
||||
elif self.MIITOOL_FOUND:
|
||||
return self._mii_get_plugged_in()
|
||||
else:
|
||||
print 'Error: No way of checking for a wired connection. Make \
|
||||
sure that either mii-tool or ethtool is installed.'
|
||||
print 'Error: No way of checking for a wired connection. Make ' + \
|
||||
'sure that either mii-tool or ethtool is installed.'
|
||||
return False
|
||||
|
||||
def _eth_get_plugged_in(self):
|
||||
@@ -254,7 +291,6 @@ class WirelessInterface(Interface, wnettools.BaseWirelessInterface):
|
||||
cmd = 'iwlist ' + self.iface + ' scan'
|
||||
if self.verbose: print cmd
|
||||
results = misc.Run(cmd)
|
||||
|
||||
# Split the networks apart, using Cell as our split point
|
||||
# this way we can look at only one network at a time.
|
||||
# The spaces around ' Cell ' are to minimize the chance that someone
|
||||
@@ -293,17 +329,17 @@ class WirelessInterface(Interface, wnettools.BaseWirelessInterface):
|
||||
|
||||
"""
|
||||
ap = {}
|
||||
# ESSID - Switch '<hidden>' to 'Hidden' to remove
|
||||
# brackets that can mix up formatting.
|
||||
ap['essid'] = misc.RunRegex(essid_pattern, cell)
|
||||
if ap['essid']:
|
||||
ap['essid'] = ap['essid'].strip('"')
|
||||
try:
|
||||
ap['essid'] = misc.to_unicode(ap['essid'])
|
||||
except UnicodeDecodeError, UnicodeEncodeError:
|
||||
print 'Unicode problem with current network essid, ignoring!!'
|
||||
return None
|
||||
if ap['essid'] in ['<hidden>', ""]:
|
||||
ap['essid'] = 'Hidden'
|
||||
ap['hidden'] = True
|
||||
ap['essid'] = "<hidden>"
|
||||
else:
|
||||
ap['hidden'] = False
|
||||
|
||||
|
||||
@@ -306,7 +306,7 @@ class WirelessInterface(Interface, wnettools.BaseWirelessInterface):
|
||||
return None
|
||||
|
||||
if ap['essid'] in [ "", '<hidden>']:
|
||||
ap['essid'] = 'Hidden'
|
||||
ap['essid'] = '<hidden>'
|
||||
ap['hidden'] = True
|
||||
else:
|
||||
ap['hidden'] = False
|
||||
|
||||
@@ -258,11 +258,12 @@ class appGui(object):
|
||||
self.network_list = self.wTree.get_widget("network_list_vbox")
|
||||
self.status_area = self.wTree.get_widget("connecting_hbox")
|
||||
self.status_bar = self.wTree.get_widget("statusbar")
|
||||
menu = self.wTree.get_widget("menu1")
|
||||
|
||||
self.status_area.hide_all()
|
||||
|
||||
if os.path.exists(wpath.etc + "wicd.png"):
|
||||
self.window.set_icon_from_file(wpath.etc + "wicd.png")
|
||||
if os.path.exists(wpath.images + "wicd.png"):
|
||||
self.window.set_icon_from_file(wpath.images + "wicd.png")
|
||||
self.statusID = None
|
||||
self.first_dialog_load = True
|
||||
self.is_visible = True
|
||||
@@ -825,7 +826,6 @@ class appGui(object):
|
||||
daemon.WriteWindowSize(width, height, "main")
|
||||
|
||||
if self.standalone:
|
||||
self.window.destroy()
|
||||
sys.exit(0)
|
||||
|
||||
self.is_visible = False
|
||||
|
||||
@@ -67,9 +67,9 @@ def Run(cmd, include_stderr=False, return_pipe=False):
|
||||
one output string from the command.
|
||||
|
||||
"""
|
||||
|
||||
cmd = to_unicode(str(cmd))
|
||||
cmd = cmd.split()
|
||||
if type(cmd) is not list:
|
||||
cmd = to_unicode(str(cmd))
|
||||
cmd = cmd.split()
|
||||
if include_stderr:
|
||||
err = STDOUT
|
||||
fds = True
|
||||
|
||||
@@ -558,7 +558,7 @@ class TrayIcon(object):
|
||||
self.tray = egg.trayicon.TrayIcon("WicdTrayIcon")
|
||||
self.pic = gtk.Image()
|
||||
self.tooltip.set_tip(self.eb, "Initializing wicd...")
|
||||
self.pic.set_from_file("images/no-signal.png")
|
||||
self.pic.set_from_file(wpath.images + "no-signal.png")
|
||||
|
||||
self.eb.connect('button_press_event', self.tray_clicked)
|
||||
self.eb.add(self.pic)
|
||||
|
||||
@@ -1025,8 +1025,6 @@ class WirelessDaemon(dbus.service.Object):
|
||||
cur_network = self.LastScan[id]
|
||||
essid_key = "essid:" + cur_network["essid"]
|
||||
bssid_key = cur_network["bssid"]
|
||||
if self.debug_mode:
|
||||
print bssid_key
|
||||
|
||||
if self.config.get(essid_key, 'use_settings_globally'):
|
||||
section = essid_key
|
||||
@@ -1041,8 +1039,9 @@ class WirelessDaemon(dbus.service.Object):
|
||||
# Read the essid because we need to name those hidden
|
||||
# wireless networks now - but only read it if it is hidden.
|
||||
if cur_network["hidden"]:
|
||||
cur_network["essid"] = misc.Noneify(self.config.get(section,
|
||||
"essid"))
|
||||
cur_network["essid"] = config.get(section, x)
|
||||
if cur_network["essid"] in ["", "Hidden", "<hidden>"]:
|
||||
cur_network["essid"] = "<hidden>"
|
||||
for x in self.config.options(section):
|
||||
if not cur_network.has_key(x) or x.endswith("script"):
|
||||
cur_network[x] = misc.Noneify(self.config.get(section, x))
|
||||
|
||||
@@ -552,7 +552,7 @@ class BaseWirelessInterface(BaseInterface):
|
||||
|
||||
def SetWpaDriver(self, driver):
|
||||
""" Sets the wpa_driver. """
|
||||
self.wpa_driver = _sanitize_string_strict(driver)
|
||||
self.wpa_driver = _sanitize_string(driver)
|
||||
|
||||
def SetEssid(self, essid):
|
||||
""" Set the essid of the wireless interface.
|
||||
@@ -561,8 +561,8 @@ class BaseWirelessInterface(BaseInterface):
|
||||
essid -- essid to set the interface to
|
||||
|
||||
"""
|
||||
cmd = 'iwconfig %s essid %s' % (self.iface, essid)
|
||||
if self.verbose: print cmd
|
||||
cmd = ['iwconfig', self.iface, 'essid', essid]
|
||||
if self.verbose: print str(cmd)
|
||||
misc.Run(cmd)
|
||||
|
||||
def StopWPA(self):
|
||||
@@ -623,7 +623,7 @@ class BaseWirelessInterface(BaseInterface):
|
||||
return ret
|
||||
|
||||
def _GetRalinkInfo(self):
|
||||
""" Get a network info list used for ralink drivers
|
||||
""" Get a network info dict used for ralink drivers
|
||||
|
||||
Calls iwpriv <wireless interface> get_site_survey, which
|
||||
on some ralink cards will return encryption and signal
|
||||
@@ -631,49 +631,56 @@ class BaseWirelessInterface(BaseInterface):
|
||||
|
||||
"""
|
||||
iwpriv = misc.Run('iwpriv ' + self.iface + ' get_site_survey')
|
||||
lines = iwpriv.splitlines()
|
||||
lines = lines[2:]
|
||||
return lines
|
||||
lines = iwpriv.splitlines()[2:]
|
||||
aps = {}
|
||||
patt = re.compile("((?:[0-9A-Z]{2}:){5}[0-9A-Z]{2})")
|
||||
for x in lines:
|
||||
ap = {}
|
||||
info = x.split(" ")
|
||||
info = filter(None, [x.strip() for x in info])
|
||||
if re.match(patt, info[2].upper()):
|
||||
bssid = info[2].upper()
|
||||
offset = -1
|
||||
elif re.match(patt, info[3].upper()):
|
||||
bssid = info[3].upper()
|
||||
offset = 0
|
||||
else: # Invalid
|
||||
print 'Invalid iwpriv line. Skipping it.'
|
||||
continue
|
||||
ap['strength'] = info[1]
|
||||
if info[5 + offset] == 'WEP' or info[4 + offset] == 'WEP':
|
||||
ap['encryption_method'] = 'WEP'
|
||||
elif info[5 + offset] in ['WPA-PSK', 'WPA']:
|
||||
ap['encryption_method'] = 'WPA'
|
||||
elif info[5 + offset] == 'WPA2-PSK':
|
||||
ap['encryption_method'] = 'WPA2'
|
||||
else:
|
||||
print "Unknown AuthMode, can't assign encryption_method!"
|
||||
ap['encryption_method'] = 'Unknown'
|
||||
aps[bssid] = ap
|
||||
return aps
|
||||
|
||||
def _ParseRalinkAccessPoint(self, ap, ralink_info, cell):
|
||||
""" Parse encryption and signal strength info for ralink cards
|
||||
|
||||
Keyword arguments:
|
||||
ap -- array containing info about the current access point
|
||||
ralink_info -- string containing available network info
|
||||
ralink_info -- dict containing available network info
|
||||
cell -- string containing cell information
|
||||
|
||||
Returns:
|
||||
Updated array containing info about the current access point
|
||||
|
||||
"""
|
||||
lines = ralink_info
|
||||
wep_pattern = re.compile('.*Encryption key:(.*?)\n', re.I | re.M | re.S)
|
||||
for x in lines: # Iterate through all networks found
|
||||
info = x.split()
|
||||
# Make sure we read in a valid entry
|
||||
if len(info) < 5 or info == None or info == '':
|
||||
break
|
||||
if info[2] == ap['essid']:
|
||||
if misc.RunRegex(wep_pattern, cell) == 'on':
|
||||
ap['encryption'] = True
|
||||
if info[5] == 'WEP' or (
|
||||
(info[5] == 'OPEN' or info[5] == 'SHARED') and
|
||||
info[4] == 'WEP'):
|
||||
ap['encryption_method'] = 'WEP'
|
||||
elif info[5] == 'WPA-PSK':
|
||||
ap['encryption_method'] = 'WPA'
|
||||
elif info[5] == 'WPA2-PSK':
|
||||
ap['encryption_method'] = 'WPA2'
|
||||
else:
|
||||
print 'Unknown AuthMode, can\'t assign encryption_method!!'
|
||||
ap['encryption_method'] = 'Unknown'
|
||||
else:
|
||||
ap['encryption'] = False
|
||||
|
||||
# Set signal strength here (in dBm, not %),
|
||||
# ralink drivers don't return link quality
|
||||
ap['strength'] = info[1]
|
||||
if ralink_info.has_key(ap['bssid']):
|
||||
info = ralink_info[ap['bssid']]
|
||||
for key in info.keys():
|
||||
ap[key] = info[key]
|
||||
if misc.RunRegex(wep_pattern, cell) == 'on':
|
||||
ap['encryption'] = True
|
||||
else:
|
||||
ap['encryption'] = False
|
||||
return ap
|
||||
|
||||
def SetMode(self, mode):
|
||||
@@ -729,12 +736,12 @@ class BaseWirelessInterface(BaseInterface):
|
||||
|
||||
"""
|
||||
if not self.iface: return False
|
||||
cmd = 'iwconfig %s essid "%s"' % (self.iface, essid)
|
||||
cmd = ['iwconfig', self.iface, 'essid', essid]
|
||||
if channel:
|
||||
cmd = ''.join([cmd, ' channel ', str(channel)])
|
||||
cmd.extend(['channel', str(channel)])
|
||||
if bssid:
|
||||
cmd = ''.join([cmd, ' ap ', bssid])
|
||||
if self.verbose: print cmd
|
||||
cmd.extend(['ap', bssid])
|
||||
if self.verbose: print str(cmd)
|
||||
misc.Run(cmd)
|
||||
|
||||
def GeneratePSK(self, network):
|
||||
@@ -841,7 +848,7 @@ class BaseWirelessInterface(BaseInterface):
|
||||
cmd_list.append('SSID=' + info[2])
|
||||
|
||||
for cmd in cmd_list:
|
||||
cmd = 'iwpriv ' + self.iface + ' '
|
||||
cmd = 'iwpriv ' + self.iface + ' ' + cmd
|
||||
if self.verbose: print cmd
|
||||
misc.Run(cmd)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user