- fix a mispell bug
[telepathy-butterfly.git] / telepathy-butterfly
blob59bee9da88cabfbd3cf5c285729747d2de5572a4
1 #!/usr/bin/python
3 # telepathy-butterfly - an MSN connection manager for Telepathy
5 # Copyright (C) 2006-2007 Ali Sabil <ali.sabil@gmail.com>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 import gobject
22 import dbus.glib
23 import signal
24 import os
25 import sys
27 import logging
29 def debug_divert_messages(filename):
30 """debug_divert_messages:
31 @filename: A file to which to divert stdout and stderr or None to do
32 nothing.
34 Open the given file for writing and duplicate its file descriptor to
35 be used for stdout and stderr. This has the effect of closing the previous
36 stdout and stderr, and sending all messages that would have gone there
37 to the given file instead.
39 By default the file is truncated and hence overwritten each time the
40 process is executed.
41 If the filename is prefixed with '+' then the file is not truncated and
42 output is added at the end of the file.
43 Passing None to this function is guaranteed to have no effect. This is
44 so you can call it with the recommended usage
45 debug_divert_messages (os.getenv(MYAPP_LOGFILE))
46 and it won't do anything if the environment variable is not set. """
48 # TODO: this function should move to telepathy-python at some point
50 if filename is None:
51 print "caca"
52 return
54 try:
55 if filename.startswith('+'):
56 logfile = open(filename[1:], 'a')
57 else:
58 logfile = open(filename, 'w')
59 except IOError, e:
60 print "Can't open logfile '%s' : '%s'" %(filename, e)
61 return
63 sys.stdout = logfile
64 sys.stderr = logfile
66 debug_divert_messages(os.getenv('BUTTERFLY_LOGFILE'))
68 logging.basicConfig(level=logging.DEBUG)
70 from butterfly import ButterflyConnectionManager
71 from butterfly.util.decorator import async
73 logger = logging.getLogger('Butterfly')
75 IDLE_TIMEOUT = 5000
76 PROCESS_NAME = 'telepathy-butterfly'
78 if __name__ == '__main__':
79 try: # change process name for killall
80 import ctypes
81 libc = ctypes.CDLL('libc.so.6')
82 libc.prctl(15, PROCESS_NAME, 0, 0, 0)
83 except Exception, e:
84 logger.warning('Unable to set processName: %s" % e')
86 @async
87 def quit():
88 manager.quit()
89 mainloop.quit()
91 if 'BUTTERFLY_PERSIST' not in os.environ:
92 def timeout_cb():
93 if len(manager._connections) == 0:
94 logger.info('No connection received - quitting')
95 quit()
96 return False
97 gobject.timeout_add(IDLE_TIMEOUT, timeout_cb)
98 shutdown_callback = quit
99 else:
100 shutdown_callback = None
102 signal.signal(signal.SIGTERM, lambda : quit)
104 manager = ButterflyConnectionManager(shutdown_callback)
105 mainloop = gobject.MainLoop(is_running=True)
107 while mainloop.is_running():
108 try:
109 mainloop.run()
110 except KeyboardInterrupt:
111 quit()